ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilBlogPostingGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 include_once("./Services/COPage/classes/class.ilPageObjectGUI.php");
5 include_once("./Modules/Blog/classes/class.ilBlogPosting.php");
6 
19 {
20  protected $node_id; // [int]
21  protected $access_handler; // [object]
22  protected $enable_public_notes; // [bool]
23  protected $may_contribute; // [bool]
24 
36  function __construct($a_node_id, $a_access_handler = null, $a_id = 0, $a_old_nr = 0, $a_enable_public_notes = true, $a_may_contribute = true, $a_style_sheet_id = 0)
37  {
38  global $tpl, $lng;
39 
40  $lng->loadLanguageModule("blog");
41 
42  $this->node_id = $a_node_id;
43  $this->access_handler = $a_access_handler;
44  $this->enable_public_notes = (bool)$a_enable_public_notes;
45 
46  parent::__construct("blp", $a_id, $a_old_nr);
47 
48  // needed for notification
49  $this->getBlogPosting()->setBlogNodeId($this->node_id, $this->isInWorkspace());
50 
51  // #11151
52  $this->may_contribute = (bool)$a_may_contribute;
53  $this->setEnableEditing($a_may_contribute);
54 
55  // content style
56  include_once("./Services/Style/classes/class.ilObjStyleSheet.php");
57 
58  $tpl->setCurrentBlock("SyntaxStyle");
59  $tpl->setVariable("LOCATION_SYNTAX_STYLESHEET",
61  $tpl->parseCurrentBlock();
62 
63  // #17814
64  $tpl->setCurrentBlock("ContentStyle");
65  $tpl->setVariable("LOCATION_CONTENT_STYLESHEET",
66  ilObjStyleSheet::getContentStylePath($a_style_sheet_id));
67  $tpl->parseCurrentBlock();
68 
69  // needed for editor
70  $this->setStyleId($a_style_sheet_id);
71  }
72 
76  function executeCommand()
77  {
78  global $ilCtrl, $ilTabs, $ilLocator, $tpl;
79 
80  $next_class = $ilCtrl->getNextClass($this);
81  $cmd = $ilCtrl->getCmd();
82 
83  $posting = $this->getBlogPosting();
84  $ilCtrl->setParameter($this, "blpg", $posting->getId());
85 
86  switch($next_class)
87  {
88  case "ilnotegui":
89  // $this->getTabs();
90  // $ilTabs->setTabActive("pg");
91  return $this->previewFullscreen();
92 
93  /*
94  case "ilratinggui":
95  include_once("./Services/Rating/classes/class.ilRatingGUI.php");
96  $rating_gui = new ilRatingGUI();
97  $rating_gui->setObject($this->getBlogPosting()->getParentId(), "blog",
98  $this->getBlogPosting()->getId(), "blp");
99  $this->ctrl->forwardCommand($rating_gui);
100  $ilCtrl->redirect($this, "preview");
101  break;
102  */
103 
104  case "ilpageobjectgui":
105  die("Deprecated. Blog Posting gui forwarding to ilpageobject");
106  return;
107 
108  default:
109  if($posting)
110  {
111  $this->setPresentationTitle($posting->getTitle());
112 
113  $tpl->setTitle(ilObject::_lookupTitle($this->getBlogPosting()->getBlogId()).": ". // #15017
114  $posting->getTitle());
115  $tpl->setTitleIcon(ilUtil::getImagePath("icon_blog.svg"),
116  $this->lng->txt("obj_blog")); // #12879
117 
118  $ilLocator->addItem($posting->getTitle(),
119  $ilCtrl->getLinkTarget($this, "preview"));
120  }
121  return parent::executeCommand();
122  }
123  }
124 
130  function setBlogPosting(ilBlogPosting $a_posting)
131  {
132  $this->setPageObject($a_posting);
133  }
134 
140  function getBlogPosting()
141  {
142  return $this->getPageObject();
143  }
144 
151  protected function checkAccess($a_cmd)
152  {
153  if($a_cmd == "contribute")
154  {
155  return $this->may_contribute;
156  }
157  return $this->access_handler->checkAccess($a_cmd, "", $this->node_id);
158  }
159 
163  function preview($a_mode = null)
164  {
165  global $ilCtrl, $tpl, $ilSetting;
166 
167  $this->getBlogPosting()->increaseViewCnt();
168 
169  $wtpl = new ilTemplate("tpl.blog_page_view_main_column.html",
170  true, true, "Modules/Blog");
171 
172  // page commands
173  if(!$a_mode)
174  {
175  /*
176  // delete
177  $page_commands = false;
178  if ($this->checkAccess("write"))
179  {
180  $wtpl->setCurrentBlock("page_command");
181  $wtpl->setVariable("HREF_PAGE_CMD",
182  $ilCtrl->getLinkTarget($this, "deleteBlogPostingConfirmationScreen"));
183  $wtpl->setVariable("TXT_PAGE_CMD", $lng->txt("delete"));
184  $wtpl->parseCurrentBlock();
185  }
186  if ($page_commands)
187  {
188  $wtpl->setCurrentBlock("page_commands");
189  $wtpl->parseCurrentBlock();
190  }
191  */
192  }
193  else
194  {
195  $callback = array($this, "observeNoteAction");
196 
197  // notes
198 
199  $may_delete_comments = ($this->checkAccess("contribute") &&
200  $ilSetting->get("comments_del_tutor", 1));
201 
202  $wtpl->setVariable("NOTES", $this->getNotesHTML($this->getBlogPosting(),
203  false, $this->enable_public_notes, $may_delete_comments, $callback));
204  }
205 
206  // permanent link
207  if($a_mode != "embedded")
208  {
209  $append = ($_GET["blpg"] != "")
210  ? "_".$_GET["blpg"]
211  : "";
212  if($this->isInWorkspace())
213  {
214  $append .= "_wsp";
215  }
216  $tpl->setPermanentLink("blog", $this->node_id, $append);
217  }
218 
219  $wtpl->setVariable("PAGE", parent::preview());
220 
221  $tpl->setLoginTargetPar("blog_".$this->node_id.$append);
222 
223  $ilCtrl->setParameter($this, "blpg", $this->getBlogPosting()->getId());
224 
225  return $wtpl->get();
226  }
227 
233  function previewEmbedded()
234  {
235  return $this->preview("embedded");
236  }
237 
243  function previewFullscreen()
244  {
245  $this->add_date = true;
246  return $this->preview("fullscreen");
247  }
248 
254  function showPage()
255  {
256  $this->setTemplateOutput(false);
257 
258  if (!$this->getAbstractOnly())
259  {
260  $this->setPresentationTitle($this->getBlogPosting()->getTitle());
261  }
262  $this->getBlogPosting()->increaseViewCnt();
263 
264  return parent::showPage();
265  }
266 
272  protected function isInWorkspace()
273  {
274  return stristr(get_class($this->access_handler), "workspace");
275  }
276 
283  function postOutputProcessing($a_output)
284  {
285  // #8626/#9370
286  if(($this->getOutputMode() == "preview" || $this->getOutputMode() == "offline")
287  && !$this->getAbstractOnly() && $this->add_date)
288  {
289  if(!$this->isInWorkspace())
290  {
291  $author = "";
292  $author_id = $this->getBlogPosting()->getAuthor();
293  if($author_id)
294  {
295  include_once "Services/User/classes/class.ilUserUtil.php";
296  $author = ilUserUtil::getNamePresentation($author_id)." - ";
297  }
298  }
299 
300  // prepend creation date
303  $prefix = "<div class=\"il_BlockInfo\" style=\"text-align:right\">".
304  $author.ilDatePresentation::formatDate($this->getBlogPosting()->getCreated()).
305  "</div>";
307 
308  $a_output = $prefix.$a_output;
309  }
310 
311  return $a_output;
312  }
313 
319  function getTabs($a_activate = "")
320  {
321  global $ilCtrl;
322 
323  $ilCtrl->setParameterByClass("ilobjbloggui", "blpg", $this->getBlogPosting()->getId());
324 
325  parent::getTabs($a_activate);
326  }
327 
332  {
333  global $tpl, $ilCtrl, $lng;
334 
335  if ($this->checkAccess("write") || $this->checkAccess("contribute"))
336  {
337  include_once("./Services/Utilities/classes/class.ilConfirmationGUI.php");
338  $confirmation_gui = new ilConfirmationGUI();
339  $confirmation_gui->setFormAction($ilCtrl->getFormAction($this));
340  $confirmation_gui->setHeaderText($lng->txt("blog_posting_deletion_confirmation"));
341  $confirmation_gui->setCancel($lng->txt("cancel"), "cancelBlogPostingDeletion");
342  $confirmation_gui->setConfirm($lng->txt("delete"), "confirmBlogPostingDeletion");
343 
344  $dtpl = new ilTemplate("tpl.blog_posting_deletion_confirmation.html", true,
345  true, "Modules/Blog");
346 
347  $dtpl->setVariable("PAGE_TITLE", $this->getBlogPosting()->getTitle());
348 
349  // notes/comments
350  include_once("./Services/Notes/classes/class.ilNote.php");
351  $cnt_note_users = ilNote::getUserCount($this->getBlogPosting()->getParentId(),
352  $this->getBlogPosting()->getId(), "wpg");
353  $dtpl->setVariable("TXT_NUMBER_USERS_NOTES_OR_COMMENTS",
354  $lng->txt("blog_number_users_notes_or_comments"));
355  $dtpl->setVariable("TXT_NR_NOTES_COMMENTS", $cnt_note_users);
356 
357  $confirmation_gui->addItem("", "", $dtpl->get());
358 
359  $tpl->setContent($confirmation_gui->getHTML());
360  }
361  }
362 
367  {
368  global $ilCtrl;
369 
370  $ilCtrl->redirect($this, "preview");
371  }
372 
377  {
378  global $ilCtrl, $lng;
379 
380  if ($this->checkAccess("write") || $this->checkAccess("contribute"))
381  {
382  // delete all md keywords
383  $md_section = $this->getBlogPosting()->getMDSection();
384  foreach($md_section->getKeywordIds() as $id)
385  {
386  $md_key = $md_section->getKeyword($id);
387  $md_key->delete();
388  }
389 
390  $this->getBlogPosting()->delete();
391  ilUtil::sendSuccess($lng->txt("blog_posting_deleted"), true);
392  }
393 
394  $ilCtrl->setParameterByClass("ilobjbloggui", "blpg", ""); // #14363
395  $ilCtrl->redirectByClass("ilobjbloggui", "render");
396  }
397 
398  function editTitle($a_form = null)
399  {
400  global $tpl, $ilTabs;
401 
402  $ilTabs->activateTab("edit");
403 
404  if(!$a_form)
405  {
406  $a_form = $this->initTitleForm();
407  }
408 
409  $tpl->setContent($a_form->getHTML());
410  }
411 
412  function updateTitle()
413  {
414  global $ilCtrl, $lng;
415 
416  $form = $this->initTitleForm();
417  if($form->checkInput())
418  {
419  $page = $this->getPageObject();
420  $page->setTitle($form->getInput("title"));
421  $page->update();
422 
423  ilUtil::sendSuccess($lng->txt("settings_saved"), true);
424  $ilCtrl->redirect($this, "preview");
425  }
426 
427  $form->setValuesByPost();
428  $this->editTitle($form);
429  }
430 
431  function initTitleForm()
432  {
433  global $lng, $ilCtrl;
434 
435  include_once('Services/Form/classes/class.ilPropertyFormGUI.php');
436  $form = new ilPropertyFormGUI();
437  $form->setFormAction($ilCtrl->getFormAction($this));
438  $form->setTitle($lng->txt('blog_rename_posting'));
439 
440  $title = new ilTextInputGUI($lng->txt("title"), "title");
441  $title->setRequired(true);
442  $form->addItem($title);
443 
444  $title->setValue($this->getPageObject()->getTitle());
445 
446  $form->addCommandButton('updateTitle', $lng->txt('save'));
447  $form->addCommandButton('preview', $lng->txt('cancel'));
448 
449  return $form;
450  }
451 
452  function editDate($a_form = null)
453  {
454  global $tpl, $ilTabs;
455 
456  $ilTabs->activateTab("edit");
457 
458  if(!$a_form)
459  {
460  $a_form = $this->initDateForm();
461  }
462 
463  $tpl->setContent($a_form->getHTML());
464  }
465 
466  function updateDate()
467  {
468  global $ilCtrl, $lng;
469 
470  $form = $this->initDateForm();
471  if($form->checkInput())
472  {
473  $dt = $form->getInput("date");
474  $dt = new ilDateTime($dt["date"]." ".$dt["time"], IL_CAL_DATETIME);
475 
476  $page = $this->getPageObject();
477  $page->setCreated($dt);
478  $page->update();
479 
480  ilUtil::sendSuccess($lng->txt("settings_saved"), true);
481  $ilCtrl->redirect($this, "preview");
482  }
483 
484  $form->setValuesByPost();
485  $this->editTitle($form);
486  }
487 
488  function initDateForm()
489  {
490  global $lng, $ilCtrl;
491 
492  include_once('Services/Form/classes/class.ilPropertyFormGUI.php');
493  $form = new ilPropertyFormGUI();
494  $form->setFormAction($ilCtrl->getFormAction($this));
495  $form->setTitle($lng->txt('blog_edit_date'));
496 
497  $date = new ilDateTimeInputGUI($lng->txt("date"), "date");
498  $date->setRequired(true);
499  $date->setShowTime(true);
500  $date->setInfo($lng->txt('blog_edit_date_info'));
501  $form->addItem($date);
502 
503  $date->setDate($this->getPageObject()->getCreated());
504 
505  $form->addCommandButton('updateDate', $lng->txt('save'));
506  $form->addCommandButton('preview', $lng->txt('cancel'));
507 
508  return $form;
509  }
510 
511  function observeNoteAction($a_blog_id, $a_posting_id, $a_type, $a_action, $a_note_id)
512  {
513  // #10040 - get note text
514  include_once "Services/Notes/classes/class.ilNote.php";
515  $note = new ilNote($a_note_id);
516  $note = $note->getText();
517 
518  include_once "Modules/Blog/classes/class.ilObjBlog.php";
519  ilObjBlog::sendNotification("comment", $this->isInWorkspace(), $this->node_id, $a_posting_id, $note);
520  }
521 
522  protected function getActivationCaptions()
523  {
524  global $lng;
525 
526  return array("deactivatePage" => $lng->txt("blog_toggle_draft"),
527  "activatePage" => $lng->txt("blog_toggle_final"));
528  }
529 
531  {
532  $this->deactivatePage(true);
533  }
534 
535  function deactivatePage($a_to_list = false)
536  {
537  $this->getBlogPosting()->setApproved(false);
538  $this->getBlogPosting()->setActive(false);
539  $this->getBlogPosting()->update(true, false, false);
540  if(!$a_to_list)
541  {
542  $this->ctrl->redirect($this, "edit");
543  }
544  else
545  {
546  $this->ctrl->redirectByClass("ilobjbloggui", "");
547  }
548  }
549 
551  {
552  $this->activatePage(true);
553  }
554 
555  function activatePage($a_to_list = false)
556  {
557  // send notifications
558  include_once "Modules/Blog/classes/class.ilObjBlog.php";
559  ilObjBlog::sendNotification("new", $this->isInWorkspace(), $this->node_id, $this->getBlogPosting()->getId());
560 
561  $this->getBlogPosting()->setActive(true);
562  $this->getBlogPosting()->update(true, false, false);
563  if(!$a_to_list)
564  {
565  $this->ctrl->redirect($this, "edit");
566  }
567  else
568  {
569  $this->ctrl->redirectByClass("ilobjbloggui", "");
570  }
571  }
572 
573  function editKeywords(ilPropertyFormGUI $a_form = null)
574  {
575  global $ilTabs, $tpl;
576 
577  if (!$this->checkAccess("contribute"))
578  {
579  return;
580  }
581 
582  $ilTabs->activateTab("pg");
583 
584  if(!$a_form)
585  {
586  $a_form = $this->initKeywordsForm();
587  }
588 
589  $tpl->setContent($a_form->getHTML());
590  }
591 
592  protected function initKeywordsForm()
593  {
594  global $ilUser;
595 
596  include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
597  $form = new ilPropertyFormGUI();
598  $form->setFormAction($this->ctrl->getFormAction($this, "saveKeywordsForm"));
599  $form->setTitle($this->lng->txt("blog_edit_keywords"));
600 
601  $txt = new ilTextInputGUI($this->lng->txt("blog_keywords"), "keywords");
602  // $txt->setRequired(true); #10504
603  $txt->setMulti(true);
604  $txt->setDataSource($this->ctrl->getLinkTarget($this, "keywordAutocomplete", "", true));
605  $txt->setMaxLength(200);
606  $txt->setSize(50);
607  $txt->setInfo($this->lng->txt("blog_keywords_info"));
608  $form->addItem($txt);
609 
610  $md_section = $this->getBlogPosting()->getMDSection();
611 
612  $keywords = array();
613  foreach($ids = $md_section->getKeywordIds() as $id)
614  {
615  $md_key = $md_section->getKeyword($id);
616  if (trim($md_key->getKeyword()) != "")
617  {
618  $keywords[$md_key->getKeywordLanguageCode()][]
619  = $md_key->getKeyword();
620  }
621  }
622 
623  // language is not "used" anywhere
624  $ulang = $ilUser->getLanguage();
625  if($keywords[$ulang])
626  {
627  asort($keywords[$ulang]);
628  $txt->setValue($keywords[$ulang]);
629  }
630 
631  // other keywords in blog
632  $other = array();
633  foreach(array_keys(ilBlogPosting::getAllPostings($this->getBlogPosting()->getBlogId())) as $posting_id)
634  {
635  if($posting_id != $this->getBlogPosting()->getId())
636  {
637  $other = array_merge($other, ilBlogPosting::getKeywords($this->getBlogPosting()->getBlogId(), $posting_id));
638  }
639  }
640  // #17414
641  $other = array_unique($other);
642  sort($other, SORT_LOCALE_STRING);
643  if(is_array($keywords[$ulang]))
644  {
645  $other = array_diff($other, $keywords[$ulang]);
646  }
647  if(sizeof($other))
648  {
649  $html = "";
650  foreach($other as $item)
651  {
652  $html .= '<span class="ilTag">'.$item.'</span>';
653  }
654  $info = new ilNonEditableValueGUI($this->lng->txt("blog_keywords_other"), "", true);
655  $info->setInfo($this->lng->txt("blog_keywords_other_info"));
656  $info->setValue($html);
657  $form->addItem($info);
658  }
659 
660  $form->addCommandButton("saveKeywordsForm", $this->lng->txt("save"));
661  $form->addCommandButton("preview", $this->lng->txt("cancel"));
662 
663  return $form;
664  }
665 
666  protected function getParentObjId()
667  {
668  if($this->node_id)
669  {
670  if($this->isInWorkspace())
671  {
672  return $this->access_handler->getTree()->lookupObjectId($this->node_id);
673  }
674  else
675  {
676  return ilObject::_lookupObjId($this->node_id);
677  }
678  }
679  }
680 
681  function saveKeywordsForm()
682  {
683  $form = $this->initKeywordsForm();
684  if($form->checkInput())
685  {
686  $keywords = $form->getInput("keywords");
687  if(is_array($keywords))
688  {
689  $this->getBlogPosting()->updateKeywords($keywords);
690  }
691 
692  $this->ctrl->redirect($this, "preview");
693  }
694 
695  $form->setValuesByPost();
696  $this->editKeywords($form);
697  }
698 
700  {
701  $force_all = (bool)$_GET["fetchall"];
702 
703  include_once("./Services/MetaData/classes/class.ilMDKeyword.php");
705  "blp", $this->getParentObjId());
706 
707  include_once("./Services/Search/classes/class.ilSearchSettings.php");
708  $cut = (int)ilSearchSettings::getInstance()->getAutoCompleteLength();
709 
710  $has_more = false;
711  $result = array();
712  foreach ($res as $r)
713  {
714  if(!$force_all &&
715  sizeof($result["items"]) >= $cut)
716  {
717  $has_more = true;
718  break;
719  }
720  $entry = new stdClass();
721  $entry->value = $r;
722  $entry->label = $r;
723  $result["items"][] = $entry;
724  }
725 
726  $result["hasMoreResults"] = $has_more;
727 
728  include_once './Services/JSON/classes/class.ilJsonUtil.php';
730  exit;
731  }
732 
745  static function getSnippet($a_id, $a_truncate = false, $a_truncate_length = 500, $a_truncate_sign = "...", $a_include_picture = false, $a_picture_width = 144, $a_picture_height = 144, $a_export_directory = null)
746  {
747  $bpgui = new self(0, null, $a_id);
748 
749  // scan the full page for media objects
750  if($a_include_picture)
751  {
752  $img = $bpgui->getFirstMediaObjectAsTag($a_picture_width, $a_picture_height, $a_export_directory);
753  }
754 
755  $bpgui->setRawPageContent(true);
756  $bpgui->setAbstractOnly(true);
757 
758  // #8627: export won't work - should we set offline mode?
759  $bpgui->setFileDownloadLink(".");
760  $bpgui->setFullscreenLink(".");
761  $bpgui->setSourcecodeDownloadScript(".");
762 
763  // render without title
764  $page = $bpgui->showPage();
765 
766  if($a_truncate)
767  {
768  $page = ilPageObject::truncateHTML($page, $a_truncate_length, $a_truncate_sign);
769  }
770 
771  if($img)
772  {
773  $page = '<div>'.$img.$page.'</div><div style="clear:both;"></div>';
774  }
775 
776  return $page;
777  }
778 
779  protected function getFirstMediaObjectAsTag($a_width = 144, $a_height = 144, $a_export_directory = null)
780  {
781  $this->obj->buildDom();
782  $mob_ids = $this->obj->collectMediaObjects();
783  if($mob_ids)
784  {
785  require_once("./Services/MediaObjects/classes/class.ilObjMediaObject.php");
786  foreach($mob_ids as $mob_id)
787  {
788  $mob_obj = new ilObjMediaObject($mob_id);
789  $mob_item = $mob_obj->getMediaItem("Standard");
790  if(stristr($mob_item->getFormat(), "image"))
791  {
792  $mob_size = $mob_item->getOriginalSize();
793  if($mob_size["width"] >= $a_width ||
794  $mob_size["height"] >= $a_height)
795  {
796  if(!$a_export_directory)
797  {
798  $mob_dir = ilObjMediaObject::_getDirectory($mob_obj->getId());
799  }
800  else
801  {
802  // see ilCOPageHTMLExport::exportHTMLMOB()
803  $mob_dir = "./mobs/mm_".$mob_obj->getId();
804  }
805  $mob_res = self::parseImage($mob_size["width"],
806  $mob_size["height"], $a_width, $a_height);
807 
808  return '<img'.
809  ' src="'.$mob_dir."/".$mob_item->getLocation().'"'.
810  ' width="'.$mob_res[0].'"'.
811  ' height="'.$mob_res[1].'"'.
812  ' class="ilBlogListItemSnippetPreviewImage ilFloatLeft noMirror"'.
813  ' />';
814  }
815  }
816  }
817  }
818  }
819 
820  protected static function parseImage($src_width, $src_height, $tgt_width, $tgt_height)
821  {
822  $ratio_width = $ratio_height = 1;
823  if($src_width > $tgt_width)
824  {
825  $ratio_width = $tgt_width / $src_width;
826  }
827  if($src_height > $tgt_height)
828  {
829  $ratio_height = $tgt_height / $src_height;
830  }
831  $shrink_ratio = min($ratio_width, $ratio_height);
832 
833  return array(
834  (int)round($src_width*$shrink_ratio),
835  (int)round($src_height*$shrink_ratio)
836  );
837  }
838 }
839 
840 ?>