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