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