ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
extended_example_for_developers.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
6 
40 {
41  //Set up the gears as always
42  global $DIC;
43  $f = $DIC->ui()->factory();
44  $renderer = $DIC->ui()->renderer();
45  $refinery = $DIC->refinery();
46  $request_wrapper = $DIC->http()->wrapper()->query();
47 
48  //Create some bare UI Components Notification Item to be used all over the place
49  $icon = $f->symbol()->icon()->standard("chtr", "chtr");
50  $title = $f->link()->standard("Some Title", "#");
51  $item = $f->item()->notification($title, $icon);
52 
53  //We provide 4 async endpoints here
54  //Endpoint if element ist closed
55  $async_close = $_SERVER['REQUEST_URI'] . '&close_item=true&async_load_replace=false&async_load_replace_content=false&async_add_aggregate=false';
56  //Attach this directly to all items to be closable (showing the Close Button)
57  $closable_item = $item->withCloseAction($async_close);
58  //Endpoint if replace button is pressed
59  $async_replace_url = $_SERVER['REQUEST_URI'] . '&close_item=false&async_load_replace=true&async_load_replace_content=false&async_add_aggregate=false';
60  //Endpoint if for only replacing data of the item (description, title etc.)
61  $async_replace_content_load_url = $_SERVER['REQUEST_URI'] . '&close_item=false&async_load_replace=false&async_load_replace_content=true&async_add_aggregate=false';
62  //Endpoint for adding one single aggreate item
63  $async_add_aggregate = $_SERVER['REQUEST_URI'] . '&close_item=false&async_load_replace=false&async_load_replace_content=false&async_add_aggregate=true';
64 
65  if ($request_wrapper->has('close_item') && $request_wrapper->retrieve('close_item', $refinery->kindlyTo()->string()) === "true") {
66  //Note that we passe back JS logic here for further processing here
67  $js = $f->legacy("")->withOnLoadCode(function ($id) use ($async_replace_content_load_url) {
68  return "
69  il.DemoScopeRemaining--;
70  il.DemoScopeItem.replaceContentByAsyncItemContent('$async_replace_content_load_url',{remaining: il.DemoScopeRemaining,added: il.DemoScopeAdded});
71  ";
72  });
73  echo $renderer->renderAsync($js);
74  exit;
75  }
76 
77  if ($request_wrapper->has('async_load_replace') && $request_wrapper->retrieve('async_load_replace', $refinery->kindlyTo()->string()) === "true") {
78  $remaining = $request_wrapper->retrieve("remaining", $refinery->kindlyTo()->int());
79  $added = $request_wrapper->retrieve("added", $refinery->kindlyTo()->int());
80 
81  //We create the amount of aggregates send to us by get and put an according
82  //description into the newly create Notification Item
83  $items = [];
84  for ($i = 1; $i < $added + 1; $i++) {
85  $items[] = $closable_item->withDescription("This item is number: " . $i . " of a fix set of 10 entries.");
86  }
87  $replacement = $item->withDescription("Number of Async non-closed Aggregates: " . $remaining . ", totally created: " . $added)
88  ->withAggregateNotifications($items);
89 
90  echo $renderer->renderAsync([$replacement]);
91  exit;
92  }
93 
94  if ($request_wrapper->has('async_load_replace_content') && $request_wrapper->retrieve('async_load_replace_content', $refinery->kindlyTo()->string()) === "true") {
95  $remaining = $request_wrapper->retrieve("remaining", $refinery->kindlyTo()->int());
96  $added = $request_wrapper->retrieve("added", $refinery->kindlyTo()->int());
97  $replacement = $item->withDescription("Number of Async non-closed Aggregates: " . $remaining . ", totally created: " . $added);
98  echo $renderer->renderAsync([$replacement]);
99  exit;
100  }
101 
102  if ($request_wrapper->has('async_add_aggregate') && $request_wrapper->retrieve('async_add_aggregate', $refinery->kindlyTo()->string()) === "true") {
103  $added = $request_wrapper->retrieve("added", $refinery->kindlyTo()->int());
104 
105  $new_aggregate = $closable_item->withDescription("The item has been added, Nr: " . $added);
106 
107  echo $renderer->renderAsync([$new_aggregate]);
108  exit;
109  }
110 
111  //Button with attached js logic to add one new Notification, note, that
112  //we also change the description of the already existing parent Notification
113  //Item holding the aggregates.
114  $add_button = $f->button()->standard("Add Chat Notification", "#")
115  ->withAdditionalOnLoadCode(function ($id) use ($async_replace_url, $async_add_aggregate) {
116  return "
117  $('#$id').click(function() {
118  il.DemoScopeItem.getCounterObjectIfAny().incrementNoveltyCount(1);
119  il.DemoScopeAdded++;
120  il.DemoScopeRemaining++;
121  il.DemoScopeItem.addAsyncAggregate('$async_add_aggregate',{remaining: il.DemoScopeAdded,added: il.DemoScopeAdded});
122  il.DemoScopeItem.replaceContentByAsyncItemContent('$async_replace_url',{remaining: il.DemoScopeRemaining,added: il.DemoScopeAdded});
123  });";
124  });
125 
126  //Resetting all counts to 0, remove all aggregates
127  $reset_button = $f->button()->standard("Reset Chat", "#")
128  ->withAdditionalOnLoadCode(function ($id) use ($async_replace_url) {
129  return "
130  $('#$id').click(function() {
131  il.DemoScopeItem.getCounterObjectIfAny().decrementNoveltyCount(il.DemoScopeRemaining);
132  il.DemoScopeAdded = 0;
133  il.DemoScopeRemaining = 0;
134  il.DemoScopeItem.replaceByAsyncItem('$async_replace_url',{remaining: il.DemoScopeAdded,added: il.DemoScopeAdded});
135  });";
136  });
137 
138  //Set all counts to a fixed value of ten.
139  $set_button = $f->button()->standard("Set to 10 chat entries", "#")
140  ->withAdditionalOnLoadCode(function ($id) use ($async_replace_url) {
141  return "
142  $('#$id').click(function() {
143  il.DemoScopeItem.getCounterObjectIfAny().decrementNoveltyCount(il.DemoScopeRemaining);
144  il.DemoScopeItem.getCounterObjectIfAny().incrementNoveltyCount(10);
145  il.DemoScopeAdded = 10;
146  il.DemoScopeRemaining = 10;
147  il.DemoScopeItem.replaceByAsyncItem('$async_replace_url',{remaining: il.DemoScopeAdded,added: il.DemoScopeAdded});
148  });";
149  });
150 
156  $async_item = $item
157  ->withDescription("This is the original Version after the Page has loaded. Will be replaced completely.")
158  ->withAdditionalOnLoadCode(function ($id) {
159  return "
160  il.DemoScopeAdded = 0;
161  il.DemoScopeRemaining = 0;
162  il.DemoScopeItem = il.UI.item.notification.getNotificationItemObject($($id));
163  ";
164  });
165 
171  return usuallyDoneByGlobalScreenProbablyIgnore($async_item, $f, $renderer, $add_button, $set_button, $reset_button);
172 }
173 
174 function usuallyDoneByGlobalScreenProbablyIgnore($async_item, $f, $renderer, $add_button, $set_button, $reset_button)
175 {
176  //Put the item in some slate.
177  $async_slate = $f->mainControls()->slate()->notification("Chat", [$async_item]);
178 
179 
180  //Just some candy, to give the whole example a more realistic look.
181  $mail_icon = $f->symbol()->icon()->standard("mail", "mail");
182  $mail_title = $f->link()->standard("Inbox", "link_to_inbox");
183  $mail_notification_item = $f->item()->notification($mail_title, $mail_icon)
184  ->withDescription("You have 23 unread mails in your inbox")
185  ->withProperties(["Time" => "3 days ago"]);
186  $mail_slate = $f->mainControls()->slate()->notification("Mail", [$mail_notification_item]);
187 
188 
189  //Note
190  $notification_glyph = $f->symbol()->glyph()->notification("notification", "notification")
191  ->withCounter($f->counter()->novelty(1));
192 
193  $notification_center = $f->mainControls()->slate()
194  ->combined("Notification Center", $notification_glyph)
195  ->withAdditionalEntry($async_slate)
196  ->withAdditionalEntry($mail_slate);
197 
198  $css_fix = "<style>.panel-primary .il-maincontrols-metabar{flex-direction: column;} .panel-primary .il-metabar-slates{position: relative;top: 0px;}</style>";
199  return $css_fix . $renderer->render([buildMetabarWithNotifications($f, $notification_center),$add_button,$set_button,$reset_button]);
200 }
201 
202 function buildMetabarWithNotifications($f, $notification_center)
203 {
204  $help = $f->button()->bulky($f->symbol()->glyph()->help(), 'Help', '#');
205  $search = $f->button()->bulky($f->symbol()->glyph()->search(), 'Search', '#');
206  $user = $f->button()->bulky($f->symbol()->glyph()->user(), 'User', '#');
207 
208 
209  $metabar = $f->mainControls()->metabar()
210  ->withAdditionalEntry('search', $search)
211  ->withAdditionalEntry('help', $help)
212  ->withAdditionalEntry('notification', $notification_center)
213  ->withAdditionalEntry('user', $user);
214 
215  return $metabar;
216 }
$renderer
extended_example_for_developers()
description: > This is a rather extended example on the usage of the Notification Item async functio...
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:24
usuallyDoneByGlobalScreenProbablyIgnore($async_item, $f, $renderer, $add_button, $set_button, $reset_button)