ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilLikeGUI.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
13 class ilLikeGUI
14 {
18  protected $lng;
19 
23  protected $ctrl;
24 
28  protected $user;
29 
33  protected $obj_id;
34 
38  protected $obj_type;
39 
43  protected $sub_obj_id;
44 
48  protected $sub_obj_type;
49 
53  protected $news_id;
54 
58  protected $dom_id;
59 
63  protected $language;
64 
70  public function __construct(\ilLikeData $data, \ilTemplate $main_tpl = null)
71  {
72  global $DIC;
73 
74  $this->main_tpl = ($main_tpl == null)
75  ? $DIC->ui()->mainTemplate()
76  : $main_tpl;
77 
78  $this->lng = $DIC->language();
79  $this->ctrl = $DIC->ctrl();
80  $this->user = $DIC->user();
81  $this->ui = $DIC->ui();
82  $this->lng = $DIC->language();
83 
84  $this->data = $data;
85 
86  $this->lng->loadLanguageModule("like");
87 
88  $this->initJavascript();
89  }
90 
94  protected function initJavascript()
95  {
96  $this->main_tpl->addJavaScript("./Services/Like/js/Like.js");
97  }
98 
99 
108  public function setObject($a_obj_id, $a_obj_type, $a_sub_obj_id = 0, $a_sub_obj_type = "", $a_news_id = 0)
109  {
110  $this->obj_id = $a_obj_id;
111  $this->obj_type = $a_obj_type;
112  $this->sub_obj_id = $a_sub_obj_id;
113  $this->sub_obj_type = $a_sub_obj_type;
114  $this->news_id = $a_news_id;
115  $this->dom_id = "like_" . $this->obj_id . "_" . $this->obj_type . "_" . $this->sub_obj_id . "_" .
116  $this->sub_obj_type . "_" . $this->news_id;
117  }
118 
123  public function executeCommand()
124  {
126 
127  $next_class = $ilCtrl->getNextClass($this);
128  $cmd = $ilCtrl->getCmd("getHTML");
129 
130  switch ($next_class) {
131  default:
132  if (in_array($cmd, array("getHTML", "renderEmoticons", "renderModal", "saveExpression"))) {
133  return $this->$cmd();
134  }
135  break;
136  }
137  return "";
138  }
139 
151  public function getHTML()
152  {
153  $f = $this->ui->factory();
154  $r = $this->ui->renderer();
155  $ctrl = $this->ctrl;
156  $lng = $this->lng;
157 
158  $tpl = new ilTemplate("tpl.like.html", true, true, "Services/Like");
159 
160  // modal
161  $modal_asyn_url = $ctrl->getLinkTarget($this, "renderModal", "", true, false);
162  $modal = $f->modal()->roundtrip('', $f->legacy(""))
163  ->withAsyncRenderUrl($modal_asyn_url);
164 
165  $modal_show_sig_id = $modal->getShowSignal()->getId();
166  $this->ctrl->setParameter($this, "modal_show_sig_id", $modal_show_sig_id);
167  $emo_counters = $this->renderEmoCounters($modal->getShowSignal());
168  $tpl->setVariable("EMO_COUNTERS", $emo_counters . $r->render($modal));
169 
170 
171 
172  // emoticon popover
173  $popover = $f->popover()->standard($f->legacy(''))->withTitle('');
174  $ctrl->setParameter($this, "repl_sig", $popover->getReplaceContentSignal()->getId());
175  $asyn_url = $ctrl->getLinkTarget($this, "renderEmoticons", "", true, false);
176  $popover = $popover->withAsyncContentUrl($asyn_url);
177  $button = $f->button()->shy($lng->txt("like"), '#')
178  ->withOnClick($popover->getShowSignal());
179 
180  $tpl->setVariable("LIKE", $r->render([$popover, $button]));
181 
182  return $tpl->get();
183  }
184 
192  protected function renderEmoCounters($modal_signal = null)
193  {
195 
196  $tpl = new ilTemplate("tpl.emo_counters.html", true, true, "Services/Like");
197  $f = $this->ui->factory();
198  $r = $this->ui->renderer();
199 
200  $cnts = $this->data->getExpressionCounts(
201  $this->obj_id,
202  $this->obj_type,
203  $this->sub_obj_id,
204  $this->sub_obj_type,
205  $this->news_id
206  );
207  $comps = array();
208  foreach ($this->data->getExpressionTypes() as $k => $txt) {
209  if ($cnts[$k] > 0) {
210  $glyph = $this->getGlyphForConst($k);
211  if ($modal_signal !== null) {
212  $glyph = $glyph->withOnClick($modal_signal);
213  }
214  $comps[] = $glyph->withCounter($f->counter()->status($cnts[$k]));
215  }
216  }
217 
218  if ($ilCtrl->isAsynch()) {
219  $tpl->setVariable("MODAL_TRIGGER", $r->renderAsync($comps));
220  } else {
221  $tpl->setVariable("MODAL_TRIGGER", $r->render($comps));
222  }
223  if ($modal_signal !== null) {
224  $tpl->setVariable("ID", $this->dom_id);
225  }
226 
227  if (count($comps) > 0 && $modal_signal !== null) {
228  $tpl->setVariable("SEP", $r->render($f->divider()->vertical()));
229  }
230 
231  return $tpl->get();
232  }
233 
234 
241  protected function getGlyphForConst($a_const)
242  {
243  $f = $this->ui->factory();
244  $like = null;
245  switch ($a_const) {
246  case ilLikeData::TYPE_LIKE: $like = $f->glyph()->like(); break;
247  case ilLikeData::TYPE_DISLIKE: $like = $f->glyph()->dislike(); break;
248  case ilLikeData::TYPE_LOVE: $like = $f->glyph()->love(); break;
249  case ilLikeData::TYPE_LAUGH: $like = $f->glyph()->laugh(); break;
250  case ilLikeData::TYPE_ASTOUNDED: $like = $f->glyph()->astounded(); break;
251  case ilLikeData::TYPE_SAD: $like = $f->glyph()->sad(); break;
252  case ilLikeData::TYPE_ANGRY: $like = $f->glyph()->angry(); break;
253  }
254  return $like;
255  }
256 
257 
258 
262  public function renderEmoticons()
263  {
265  $r = $this->ui->renderer();
266 
267  $ilCtrl->saveParameter($this, "modal_show_sig_id");
268 
269  $tpl = new ilTemplate("tpl.emoticons.html", true, true, "Services/Like");
270  $tpl->setVariable("ID", $this->dom_id);
271 
272  $url = $ilCtrl->getLinkTarget($this, "", "", true);
273  foreach ($this->data->getExpressionTypes() as $k => $txt) {
274  $g = $this->getGlyphForConst($k);
275 
276  if ($this->data->isExpressionSet(
277  $this->user->getId(),
278  $k,
284  )) {
285  $g = $g->withHighlight();
286  }
287 
288  $g = $g->withAdditionalOnLoadCode(function ($id) use ($k, $url) {
289  return
290  "$('#" . $id . "').click(function() { il.Like.toggle('" . $url . "','" . $id . "','" . $this->dom_id . "'," . $k . ");});";
291  });
292  $glyphs[] = $g;
293  }
294 
295  $tpl->setVariable("GLYPHS", $r->renderAsync($glyphs));
296 
297  echo $tpl->get();
298  exit;
299  }
300 
306  protected function saveExpression()
307  {
308  $exp_key = (int) $_GET["exp"];
309  $exp_val = (int) $_GET["val"];
310  $modal_show_sig_id = ilUtil::stripSlashes($_GET["modal_show_sig_id"]);
311  $show_signal = new \ILIAS\UI\Implementation\Component\Signal($modal_show_sig_id);
312 
313  if ($exp_val) {
314  $this->data->addExpression(
315  $this->user->getId(),
316  $exp_key,
322  );
323  } else {
324  $this->data->removeExpression(
325  $this->user->getId(),
326  $exp_key,
332  );
333  }
334  echo $this->renderEmoCounters($show_signal);
335  exit;
336  }
337 
338 
343  public function renderModal()
344  {
345  $user = $this->user;
346 
347  $f = $this->ui->factory();
348  $r = $this->ui->renderer();
349 
350 
351  $list_items = [];
352  foreach ($this->data->getExpressionEntries(
353  $this->obj_id,
354  $this->obj_type,
355  $this->sub_obj_id,
356  $this->sub_obj_type,
357  $this->news_id
358  ) as $exp) {
359  $name = ilUserUtil::getNamePresentation($exp["user_id"]);
360 
361  $image = $f->image()->responsive(
362  ilObjUser::_getPersonalPicturePath($exp["user_id"]),
363  $name
364  );
365 
366  $g = $this->getGlyphForConst($exp["expression"]);
367 
368  $list_items[] = $f->item()->standard($name)
369  ->withDescription($r->render($g) . " " .
371  ->withLeadImage($image);
372  }
373 
374  $std_list = $f->panel()->listing()->standard("", array(
375  $f->item()->group("", $list_items)
376  ));
377 
378  $header = $f->legacy($this->renderEmoCounters());
379  //$header = $f->legacy("---");
380 
381  $modal = $f->modal()->roundtrip('', [$header, $std_list]);
382  echo $r->render($modal);
383  exit;
384  }
385 
386 
393  /*
394  static public function getCharacter($a_const)
395  {
396  $tpl = new ilTemplate("tpl.unicodes.html", true, true, "Services/Like");
397  $tpl->touchBlock("u".$a_const);
398  return $tpl->get();
399  }*/
400 
407  public static function getExpressionText($a_const)
408  {
409  global $DIC;
410 
411  $lng = $DIC->language();
412 
413  switch ($a_const) {
414  case ilLikeData::TYPE_LIKE: return $lng->txt("like"); break;
415  case ilLikeData::TYPE_DISLIKE: return $lng->txt("dislike"); break;
416  case ilLikeData::TYPE_LOVE: return $lng->txt("love"); break;
417  case ilLikeData::TYPE_LAUGH: return $lng->txt("laugh"); break;
418  case ilLikeData::TYPE_ASTOUNDED: return $lng->txt("astounded"); break;
419  case ilLikeData::TYPE_SAD: return $lng->txt("sad"); break;
420  case ilLikeData::TYPE_ANGRY: return $lng->txt("angry"); break;
421  }
422  return "";
423  }
424 }
const IL_CAL_DATETIME
global $DIC
Definition: saml.php:7
$_GET["client_id"]
$tpl
Definition: ilias.php:10
initJavascript()
Init javascript.
static getExpressionText($a_const)
Get unicode for const.
if(!array_key_exists('StateId', $_REQUEST)) $id
renderModal()
Render modal.
renderEmoticons()
Render emoticons.
const TYPE_ASTOUNDED
Data class for like feature.
static formatDate(ilDateTime $date, $a_skip_day=false, $a_include_wd=false, $include_seconds=false)
Format a date public.
user()
Definition: user.php:4
setObject($a_obj_id, $a_obj_type, $a_sub_obj_id=0, $a_sub_obj_type="", $a_news_id=0)
Set Object.
global $ilCtrl
Definition: ilias.php:18
$r
Definition: example_031.php:79
executeCommand()
Execute command.
special template class to simplify handling of ITX/PEAR
Date and time handling
static stripSlashes($a_str, $a_strip_html=true, $a_allow="")
strip slashes if magic qoutes is enabled
static getNamePresentation( $a_user_id, $a_user_image=false, $a_profile_link=false, $a_profile_back_link="", $a_force_first_lastname=false, $a_omit_login=false, $a_sortable=true, $a_return_data_array=false, $a_ctrl_path="ilpublicuserprofilegui")
Default behaviour is:
$txt
Definition: error.php:11
getHTML()
Get HTML.
renderEmoCounters($modal_signal=null)
Render emo counters.
exit
Definition: backend.php:16
$this data['403_header']
getGlyphForConst($a_const)
Get glyph for const.
__construct(\ilLikeData $data, \ilTemplate $main_tpl=null)
ilLikeGUI constructor.
$url
User interface for like feature.
saveExpression()
Save expresseion.
$data
Definition: bench.php:6