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