ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ToolbarNavigationRenderer.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\Blog\Navigation;
22 
26 
28 {
29  protected array $items;
31  protected int $portfolio_page;
32  protected bool $prtf_embed;
33  protected int $blog_page;
34  protected \ILIAS\Blog\Presentation\Util $util;
35  protected $current_month;
36  protected \ilCtrl $ctrl;
39 
40  public function __construct(
41  InternalDomainService $domain,
43  ) {
44  $this->domain = $domain;
45  $this->gui = $gui;
46  $this->util = $gui->presentation()->util();
47  }
48 
49  public function renderToolbarNavigation(
50  BlogAccess $blog_acces,
51  array $a_items,
52  int $blog_page,
53  bool $single_posting,
54  bool $prtf_embed,
55  $month,
56  int $portfolio_page
57  ): void {
58 
59  $this->blog_access = $blog_acces;
60  $toolbar = $this->gui->toolbar();
61  $lng = $this->domain->lng();
62  $this->ctrl = $ctrl = $this->gui->ctrl();
63  $f = $this->gui->ui()->factory();
64  $this->items = $a_items;
65  $this->current_month = $month;
66  $this->blog_page = $blog_page;
67  $this->prtf_embed = $prtf_embed;
68  $this->portfolio_page = $portfolio_page;
69 
70  $cmd = ($prtf_embed)
71  ? "previewEmbedded"
72  : "previewFullscreen";
73 
74  if ($single_posting) { // single posting view
75  $next_posting = $this->getNextPosting($blog_page);
76  if ($next_posting > 0) {
77  $this->renderPreviousButton($this->getPostingTarget($next_posting, $cmd));
78  } else {
79  $this->renderPreviousButton("");
80  }
81 
82  $this->renderPostingDropdown($cmd);
83 
84  $prev_posting = $this->getPreviousPosting($blog_page);
85  if ($prev_posting > 0) {
86  $this->renderNextButton($this->getPostingTarget($prev_posting, $cmd));
87  } else {
88  $this->renderNextButton("");
89  }
90 
91 
92  $ctrl->setParameterByClass(\ilObjBlogGUI::class, "blpg", $blog_page);
93 
94  $this->renderActionDropdown(true);
95 
96  } else { // month view
97  $next_month = $this->getNextMonth($month);
98  if ($next_month !== "") {
99  $this->renderPreviousButton($this->getMonthTarget($next_month));
100  } else {
101  $this->renderPreviousButton("");
102  }
103 
104  $this->renderMonthDropdown();
105 
106  $prev_month = $this->getPreviousMonth($month);
107  if ($prev_month !== "") {
108  $this->renderNextButton($this->getMonthTarget($prev_month));
109  } else {
110  $this->renderNextButton("");
111  }
112 
113  $ctrl->setParameterByClass(\ilObjBlogGUI::class, "bmn", $month);
114 
115  $this->renderActionDropdown(false);
116  }
117  }
118 
119  protected function renderActionDropdown(bool $single_posting): void
120  {
121  $lng = $this->domain->lng();
122  $toolbar = $this->gui->toolbar();
123  $f = $this->gui->ui()->factory();
124  $ctrl = $this->ctrl;
125  $actions = [];
126  if ($this->blog_access->mayContribute()) {
127  $ctrl->setParameterByClass(\ilObjBlogGUI::class, "prvm", "");
128 
129  $ctrl->setParameterByClass(\ilObjBlogGUI::class, "bmn", "");
130  $ctrl->setParameterByClass(\ilObjBlogGUI::class, "blpg", "");
131  $link = $ctrl->getLinkTargetByClass(\ilObjBlogGUI::class, "");
132  $ctrl->setParameterByClass(\ilObjBlogGUI::class, "blpg", $this->blog_page);
133  $ctrl->setParameterByClass(\ilObjBlogGUI::class, "bmn", $this->current_month);
134  $actions[] = $f->button()->shy(
135  $lng->txt("blog_edit"),
136  $link
137  );
138  }
139 
140  if ($single_posting && $this->blog_access->mayContribute() && $this->blog_access->mayEditPosting($this->blog_page)) {
141  $ctrl->setParameterByClass(\ilBlogPostingGUI::class, "blpg", $this->blog_page);
142  if ($this->prtf_embed) {
143  $ctrl->setParameterByClass(\ilObjPortfolioGUI::class, "ppage", $this->portfolio_page);
144  }
145  $link = $ctrl->getLinkTargetByClass(\ilBlogPostingGUI::class, "edit");
146  $actions[] = $f->button()->shy(
147  $lng->txt("blog_edit_posting"),
148  $link
149  );
150  }
151  if (count($actions) > 0) {
152  $toolbar->addStickyItem($f->dropdown()->standard($actions));
153  }
154  }
155 
156  protected function getLatestPosting(): int
157  {
158  reset($this->items);
159  $month = current($this->items);
160  if (is_array($month)) {
161  return (int) current($month)["id"];
162  }
163  return 0;
164  }
165 
166  public function getNextPosting(
167  int $blog_page
168  ): int {
169  reset($this->items);
170  $found = "";
171  $next_blpg = 0;
172  foreach ($this->items as $month => $items) {
173  foreach ($items as $item) {
174  if ($item["id"] == $blog_page) {
175  $found = true;
176  }
177  if (!$found) {
178  $next_blpg = (int) $item["id"];
179  }
180  }
181  }
182  return $next_blpg;
183  }
184 
185  protected function getPreviousPosting(
186  int $blog_page
187  ): int {
188  reset($this->items);
189  $found = "";
190  $prev_blpg = 0;
191  foreach ($this->items as $month => $items) {
192  foreach ($items as $item) {
193  if ($found && $prev_blpg === 0) {
194  $prev_blpg = (int) $item["id"];
195  }
196  if ((int) $item["id"] === $blog_page) {
197  $found = true;
198  }
199  }
200  }
201  return $prev_blpg;
202  }
203 
204  protected function getPostingTarget(int $posting, string $cmd): string
205  {
206  $this->ctrl->setParameterByClass(\ilBlogPostingGUI::class, "blpg", (string) $posting);
207  return $this->ctrl->getLinkTargetByClass(\ilBlogPostingGUI::class, $cmd);
208  }
209 
210  protected function getMonthTarget(string $month): string
211  {
212  $this->ctrl->setParameterByClass(\ilObjBlogGUI::class, "bmn", $month);
213  return $this->ctrl->getLinkTargetByClass(\ilObjBlogGUI::class, "preview");
214  }
215 
216  protected function renderMonthDropdown(): void
217  {
218  $toolbar = $this->gui->toolbar();
219  $f = $this->gui->ui()->factory();
220  $m = [];
221  foreach ($this->items as $month => $items) {
222  $label = $this->util->getMonthPresentation($month);
223  if ($month === $this->current_month) {
224  $label = "» " . $label;
225  }
226  $m[] = $f->link()->standard(
227  $label,
228  $this->getMonthTarget($month)
229  );
230  }
231  if (count($m) > 0) {
232  $toolbar->addStickyItem($f->dropdown()->standard($m)->withLabel(
233  $this->getDropdownLabel($this->util->getMonthPresentation($this->current_month))
234  ));
235  }
236  }
237 
238  protected function getNextMonth(
240  ): string {
241  reset($this->items);
242  $found = "";
243  foreach ($this->items as $month => $items) {
244  if ($month > $current_month) {
245  $found = $month;
246  }
247  }
248  return $found;
249  }
250 
251  protected function getPreviousMonth(
253  ): string {
254  reset($this->items);
255  $found = "";
256  foreach ($this->items as $month => $items) {
257  if ($month < $current_month && $found === "") {
258  $found = $month;
259  }
260  }
261  return $found;
262  }
263 
267  protected function getLatestMonth(): string
268  {
269  reset($this->items);
270  return key($this->items);
271  }
272 
273  protected function renderNextButton(string $href = ""): void
274  {
275  $this->renderNavButton("right", $href);
276  }
277 
278  protected function renderPreviousButton(string $href = ""): void
279  {
280  $this->renderNavButton("left", $href);
281  }
282 
283  protected function renderNavButton(string $dir, string $href = ""): void
284  {
285  $toolbar = $this->gui->toolbar();
286  $b = $this->gui->ui()->factory()->button()->standard(
287  "<span class=\"glyphicon glyphicon-chevron-" . $dir . " \" aria-hidden=\"true\"></span>",
288  $href
289  );
290  if ($href === "") {
291  $b = $b->withUnavailableAction();
292  }
293  $toolbar->addStickyItem($b);
294  }
295 
296  protected function renderPostingDropdown(string $cmd): void
297  {
298  $toolbar = $this->gui->toolbar();
299  $f = $this->gui->ui()->factory();
300  $m = [];
301  $dd_title = "";
302  foreach ($this->items as $month => $items) {
303  $label = $this->util->getMonthPresentation($month);
304  $m[] = $f->button()->shy(
305  $label,
306  $this->getMonthTarget($month)
307  )->withUnavailableAction();
308  foreach ($items as $item) {
309  $label = $item["title"];
310  if ((int) $item["id"] === $this->blog_page) {
311  $label = "» " . $label;
312  $dd_title = $item["title"];
313  }
314  $label = str_pad("", 12, "&nbsp;") . $label;
315  $m[] = $f->link()->standard(
316  $label,
317  $this->getPostingTarget((int) $item["id"], $cmd)
318  );
319  }
320  }
321  if (count($m) > 0) {
322  $toolbar->addStickyItem($f->dropdown()->standard($m)->withLabel(
323  $this->getDropdownLabel($dd_title)
324  ));
325  }
326  }
327 
328  protected function getDropdownLabel(string $label): string
329  {
330  return "<span style='vertical-align: bottom; max-width:60px; display: inline-block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;'>" . $label . "</span>";
331  }
332 
333 }
renderToolbarNavigation(BlogAccess $blog_acces, array $a_items, int $blog_page, bool $single_posting, bool $prtf_embed, $month, int $portfolio_page)
setParameterByClass(string $a_class, string $a_parameter, $a_value)
$lng
__construct(InternalDomainService $domain, InternalGUIService $gui)