ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilLikeGUI.php
Go to the documentation of this file.
1<?php
2
20
26{
27 protected \ILIAS\Like\InternalGUIService $gui;
28 protected ilLikeData $data;
29 protected \ILIAS\DI\UIServices $ui;
32 protected ilLanguage $lng;
33 protected ilCtrl $ctrl;
34 protected ilObjUser $user;
35 protected int $obj_id;
36 protected string $obj_type;
37 protected int $sub_obj_id;
38 protected string $sub_obj_type;
39 protected int $news_id;
40 protected string $dom_id;
41
42 public function __construct(
44 ?\ilTemplate $main_tpl = null
45 ) {
46 global $DIC;
47
48 $this->main_tpl = ($main_tpl == null)
49 ? $DIC->ui()->mainTemplate()
50 : $main_tpl;
51
52 $this->gui = $DIC->like()->internal()->gui();
53
54 $this->lng = $DIC->language();
55 $this->ctrl = $DIC->ctrl();
56 $this->user = $DIC->user();
57 $this->ui = $DIC->ui();
58
59 $this->data = $data;
60
61 $this->lng->loadLanguageModule("like");
62 $this->request = new StandardGUIRequest(
63 $DIC->http(),
64 $DIC->refinery()
65 );
66
67 $this->initJavascript();
68 }
69
70 protected function initJavascript(): void
71 {
72 $debug = false;
73 $this->gui->initFetch();
74 if ($debug) {
75 $this->main_tpl->addJavaScript("../components/ILIAS/Like/resources/Like.js");
76 } else {
77 $this->main_tpl->addJavaScript("assets/js/Like.js");
78 }
79 }
80
81 public function setObject(
82 int $a_obj_id,
83 string $a_obj_type,
84 int $a_sub_obj_id = 0,
85 string $a_sub_obj_type = "",
86 int $a_news_id = 0
87 ): void {
88 $this->obj_id = $a_obj_id;
89 $this->obj_type = $a_obj_type;
90 $this->sub_obj_id = $a_sub_obj_id;
91 $this->sub_obj_type = $a_sub_obj_type;
92 $this->news_id = $a_news_id;
93 $this->dom_id = "like_" . $this->obj_id . "_" . $this->obj_type . "_" . $this->sub_obj_id . "_" .
94 $this->sub_obj_type . "_" . $this->news_id;
95 }
96
97 public function executeCommand(): string
98 {
99 $ilCtrl = $this->ctrl;
100
101 $next_class = $ilCtrl->getNextClass($this);
102 $cmd = $ilCtrl->getCmd("getHTML");
103
104 switch ($next_class) {
105 default:
106 if (in_array($cmd, array("getHTML", "renderEmoticons", "renderModal", "saveExpression"))) {
107 return $this->$cmd();
108 }
109 break;
110 }
111 return "";
112 }
113
114 public function getHTML(): string
115 {
116 $f = $this->ui->factory();
117 $r = $this->ui->renderer();
118 $ctrl = $this->ctrl;
120
121 $tpl = new ilTemplate("tpl.like.html", true, true, "components/ILIAS/Like");
122
123 // modal
124 $modal_asyn_url = $ctrl->getLinkTarget($this, "renderModal", "", true, false);
125 $modal = $f->modal()->roundtrip('', $f->legacy()->content(""))
126 ->withAsyncRenderUrl($modal_asyn_url);
127
128 $modal_show_sig_id = $modal->getShowSignal()->getId();
129 $this->ctrl->setParameter($this, "modal_show_sig_id", $modal_show_sig_id);
130 $emo_counters = $this->renderEmoCounters($modal->getShowSignal());
131 $tpl->setVariable("EMO_COUNTERS", $emo_counters . $r->render($modal));
132
133
134
135 // emoticon popover
136 $popover = $f->popover()->standard($f->legacy()->content(''))->withTitle('');
137 $ctrl->setParameter($this, "repl_sig", $popover->getReplaceContentSignal()->getId());
138 $asyn_url = $ctrl->getLinkTarget($this, "renderEmoticons", "", true, false);
139 $popover = $popover->withAsyncContentUrl($asyn_url);
140 $button = $f->button()->shy($lng->txt("like"), '#')
141 ->withOnClick($popover->getShowSignal());
142
143 $tpl->setVariable("LIKE", $r->render([$popover, $button]));
144
145 return $tpl->get();
146 }
147
152 protected function renderEmoCounters(
153 ?ILIAS\UI\Component\Signal $modal_signal = null,
154 bool $unavailable = false
155 ): string {
156 $ilCtrl = $this->ctrl;
157
158 $tpl = new ilTemplate("tpl.emo_counters.html", true, true, "components/ILIAS/Like");
159 $f = $this->ui->factory();
160 $r = $this->ui->renderer();
161
162 $cnts = $this->data->getExpressionCounts(
163 $this->obj_id,
164 $this->obj_type,
165 $this->sub_obj_id,
166 $this->sub_obj_type,
167 $this->news_id
168 );
169 $comps = array();
170 foreach ($this->data->getExpressionTypes() as $k => $txt) {
171 if ($cnts[$k] > 0) {
172 $glyph = $this->getGlyphForConst($k)->withCounter($f->counter()->status($cnts[$k]));
173 $comp = $f->button()->shy('', '')->withSymbol($glyph);
174 if ($modal_signal !== null) {
175 $comp = $comp->withOnClick($modal_signal);
176 }
177 if ($unavailable) {
178 $comp = $comp->withUnavailableAction();
179 }
180 $comps[] = $comp;
181 }
182 }
183
184 if ($ilCtrl->isAsynch()) {
185 $tpl->setVariable("MODAL_TRIGGER", $r->renderAsync($comps));
186 } else {
187 $tpl->setVariable("MODAL_TRIGGER", $r->render($comps));
188 }
189 if ($modal_signal !== null) {
190 $tpl->setVariable("ID", $this->dom_id);
191 }
192
193 if (count($comps) > 0 && $modal_signal !== null) {
194 $tpl->setVariable("SEP", $r->render($f->divider()->vertical()));
195 }
196
197 return $tpl->get();
198 }
199
200 protected function getGlyphForConst(int $a_const): ?\ILIAS\UI\Component\Symbol\Glyph\Glyph
201 {
202 $f = $this->ui->factory();
203 $like = null;
204 switch ($a_const) {
205 case ilLikeData::TYPE_LIKE: $like = $f->symbol()->glyph()->like();
206 break;
207 case ilLikeData::TYPE_DISLIKE: $like = $f->symbol()->glyph()->dislike();
208 break;
209 case ilLikeData::TYPE_LOVE: $like = $f->symbol()->glyph()->love();
210 break;
211 case ilLikeData::TYPE_LAUGH: $like = $f->symbol()->glyph()->laugh();
212 break;
213 case ilLikeData::TYPE_ASTOUNDED: $like = $f->symbol()->glyph()->astounded();
214 break;
215 case ilLikeData::TYPE_SAD: $like = $f->symbol()->glyph()->sad();
216 break;
217 case ilLikeData::TYPE_ANGRY: $like = $f->symbol()->glyph()->angry();
218 break;
219 }
220 return $like;
221 }
222
226 public function renderEmoticons(): void
227 {
228 $ilCtrl = $this->ctrl;
229 $r = $this->ui->renderer();
230 $glyphs = [];
231
232 $ilCtrl->saveParameter($this, "modal_show_sig_id");
233
234 $tpl = new ilTemplate("tpl.emoticons.html", true, true, "components/ILIAS/Like");
235 $tpl->setVariable("ID", $this->dom_id);
236
237 $url = $ilCtrl->getLinkTarget($this, "", "", true);
238 foreach ($this->data->getExpressionTypes() as $k => $txt) {
239 $g = $this->getGlyphForConst($k);
240
241 if ($this->data->isExpressionSet(
242 $this->user->getId(),
243 $k,
244 $this->obj_id,
245 $this->obj_type,
246 $this->sub_obj_id,
247 $this->sub_obj_type,
248 $this->news_id
249 )) {
250 $g = $g->withHighlight();
251 }
252 $g = $this->ui->factory()->button()->shy('', '')->withSymbol($g);
253
254 $g = $g->withAdditionalOnLoadCode(function ($id) use ($k, $url) {
255 return
256 "$('#" . $id . "').click(function() { il.Like.toggle('" . $url . "','" . $id . "','" . $this->dom_id . "'," . $k . ");});";
257 });
258 $glyphs[] = $g;
259 }
260
261 $tpl->setVariable("GLYPHS", $r->renderAsync($glyphs));
262
263 echo $tpl->get();
264 exit;
265 }
266
271 protected function saveExpression(): void
272 {
273 $exp_key = $this->request->getExpressionKey();
274 $exp_val = $this->request->getValue();
275 $modal_show_sig_id = $this->request->getModalSignalId();
276 $show_signal = new \ILIAS\UI\Implementation\Component\Signal($modal_show_sig_id);
277
278 if ($exp_val) {
279 $this->data->addExpression(
280 $this->user->getId(),
281 $exp_key,
282 $this->obj_id,
283 $this->obj_type,
284 $this->sub_obj_id,
285 $this->sub_obj_type,
286 $this->news_id
287 );
288 } else {
289 $this->data->removeExpression(
290 $this->user->getId(),
291 $exp_key,
292 $this->obj_id,
293 $this->obj_type,
294 $this->sub_obj_id,
295 $this->sub_obj_type,
296 $this->news_id
297 );
298 }
299 echo $this->renderEmoCounters($show_signal);
300 exit;
301 }
302
303
310 public function renderModal(): void
311 {
312 $user = $this->user;
313
314 $f = $this->ui->factory();
315 $r = $this->ui->renderer();
316
317
318 $list_items = [];
319
320 $glyph_renderings = [];
321 foreach ($this->data->getExpressionEntries(
322 $this->obj_id,
323 $this->obj_type,
324 $this->sub_obj_id,
325 $this->sub_obj_type,
326 $this->news_id
327 ) as $exp) {
328 $name = ilUserUtil::getNamePresentation($exp["user_id"]);
329
330 $image = $f->image()->responsive(
331 ilObjUser::_getPersonalPicturePath($exp["user_id"]),
332 $name
333 );
334
335 $g = $f->button()->shy('', '')->withSymbol($this->getGlyphForConst($exp["expression"]))
336 ->withUnavailableAction();
337 $placeholder = "###" . $exp["expression"] . "###";
338 $glyph_renderings[$placeholder] = $r->render($g);
339
340 $list_items[] = $f->item()->standard($name)
341 ->withDescription($placeholder . " " .
343 ->withLeadImage($image);
344 }
345
346 $std_list = $f->panel()->listing()->standard("", array(
347 $f->item()->group("", $list_items)
348 ));
349
350 $header = $f->legacy()->content($this->renderEmoCounters(null, true));
351 //$header = $f->legacy("---");
352
353 $modal = $f->modal()->roundtrip('', [$header, $std_list]);
354 $html = $r->render($modal);
355 foreach ($glyph_renderings as $pl => $gl) {
356 $html = str_replace($pl, $gl, $html);
357 }
358 echo $html;
359 exit;
360 }
361
365 public static function getExpressionText(
366 int $a_const
367 ): string {
368 global $DIC;
369
370 $lng = $DIC->language();
371
372 switch ($a_const) {
373 case ilLikeData::TYPE_LIKE: return $lng->txt("like");
374 case ilLikeData::TYPE_DISLIKE: return $lng->txt("dislike");
375 case ilLikeData::TYPE_LOVE: return $lng->txt("love");
376 case ilLikeData::TYPE_LAUGH: return $lng->txt("laugh");
377 case ilLikeData::TYPE_ASTOUNDED: return $lng->txt("astounded");
378 case ilLikeData::TYPE_SAD: return $lng->txt("sad");
379 case ilLikeData::TYPE_ANGRY: return $lng->txt("angry");
380 }
381 return "";
382 }
383}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
const IL_CAL_DATETIME
Class ilCtrl provides processing control methods.
getLinkTarget(object $a_gui_obj, ?string $a_cmd=null, ?string $a_anchor=null, bool $is_async=false, bool $has_xml_style=false)
@inheritDoc
setParameter(object $a_gui_obj, string $a_parameter, $a_value)
@inheritDoc
static formatDate(ilDateTime $date, bool $a_skip_day=false, bool $a_include_wd=false, bool $include_seconds=false, ?ilObjUser $user=null,)
@classDescription Date and time handling
language handling
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
const TYPE_ASTOUNDED
User interface for like feature.
ilLikeData $data
ilObjUser $user
StandardGUIRequest $request
saveExpression()
Save expression (asynch)
setObject(int $a_obj_id, string $a_obj_type, int $a_sub_obj_id=0, string $a_sub_obj_type="", int $a_news_id=0)
renderEmoCounters(?ILIAS\UI\Component\Signal $modal_signal=null, bool $unavailable=false)
Render emo counters.
string $sub_obj_type
string $dom_id
static getExpressionText(int $a_const)
Get expression text for const.
ilGlobalTemplateInterface $main_tpl
getGlyphForConst(int $a_const)
renderModal()
Render modal (asynch)
__construct(\ilLikeData $data, ?\ilTemplate $main_tpl=null)
ilLanguage $lng
string $obj_type
ILIAS DI UIServices $ui
renderEmoticons()
Render emoticons (asynch)
ILIAS Like InternalGUIService $gui
User class.
static _getPersonalPicturePath(int $usr_id, string $size='small', bool $force_pic=false)
special template class to simplify handling of ITX/PEAR
static getNamePresentation( $a_user_id, bool $a_user_image=false, bool $a_profile_link=false, string $a_profile_back_link='', bool $a_force_first_lastname=false, bool $a_omit_login=false, bool $a_sortable=true, bool $a_return_data_array=false, $a_ctrl_path=null)
Default behaviour is:
exit
Interface Observer \BackgroundTasks Contains several chained tasks and infos about them.
global $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26
$url
Definition: shib_logout.php:70