ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilShopNewsGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2010 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 
5 include_once 'Services/Payment/classes/class.ilShopBaseGUI.php';
6 include_once 'Services/Payment/classes/class.ilShopNewsItem.php';
7 include_once 'Services/Form/classes/class.ilPropertyFormGUI.php';
8 
9 
18 {
19  private $form_gui = null;
20  private $settings_form = null;
21  private $oCurrentNewsItem = null;
22 
23  public function __construct()
24  {
26 
27  $this->oCurrentNewsItem = new ilShopNewsItem((int)$_GET['news_id']);
28 
29  $this->lng->loadLanguageModule('news');
30  }
31 
32  function executeCommand()
33  {
34  global $ilTabs;
35 
36  $next_class = $this->ctrl->getNextClass($this);
37  $cmd = $this->ctrl->getCmd();
38 
39  switch($cmd)
40  {
41  default:
42  if(!$cmd)
43  {
44  $cmd = 'showNews';
45  }
46  $this->prepareOutput();
47  $this->$cmd();
48  break;
49  }
50 
51  return true;
52  }
53 
54  protected function buildSubTabs()
55  {
56  global $ilTabs, $ilUser, $rbacreview;
57 
58  $ilTabs->addSubTabTarget('news', $this->ctrl->getLinkTarget($this, 'showNews'), '', '', '','showNews');
59  $ilTabs->addSubTabTarget('archive', $this->ctrl->getLinkTarget($this, 'showArchive'), '', '', '','archive');
60 
61  if($rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID))
62  {
63  $ilTabs->addSubTabTarget('settings', $this->ctrl->getLinkTarget($this, 'showSettings'), '', '', '','settings');
64  }
65  }
66 
67  public function saveSettings()
68  {
69  global $ilTabs, $ilSetting, $rbacreview, $ilUser;
70 
71  if(!$rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID))
72  {
73  $this->ilias->raiseError($this->lng->txt('permission_denied'), $this->ilias->error_obj->MESSAGE);
74  }
75 
76  $ilTabs->setSubTabActive('settings');
77 
78  $this->initSettingsForm('edit');
79  if($this->settings_form->checkInput())
80  {
81  $ilSetting->set('payment_news_archive_period', $this->settings_form->getInput('archive_period'));
82  $this->getSettingsValues();
83  ilUtil::sendInfo($this->lng->txt('payment_news_settings_saved'));
84 
85  return $this->tpl->setVariable('ADM_CONTENT', $this->settings_form->getHtml());
86  }
87  else
88  {
89  $this->form_gui->setValuesByPost();
90  return $this->tpl->setVariable('ADM_CONTENT', $this->settings_form->getHtml());
91  }
92  }
93 
94  public function initSettingsForm($a_mode = 'edit')
95  {
96 
97  $this->settings_form = new ilPropertyFormGUI();
98  $this->settings_form->setTitle($this->lng->txt('payment_news_settings'));
99  $this->settings_form->setTitleIcon(ilUtil::getImagePath('icon_news.gif'));
100 
101  $oSelectBox = new ilSelectInputGUI($this->lng->txt('payment_news_archive_period'), 'archive_period');
102  $oSelectBox->setInfo($this->lng->txt('payment_news_archive_period_info'));
103  $options = array();
104  for($i = 5; $i <= 100; $i += 5)
105  {
106  $options[$i] = $i;
107  }
108  $oSelectBox->setOptions($options);
109  $this->settings_form->addItem($oSelectBox);
110 
111  $this->settings_form->addCommandButton('saveSettings', $this->lng->txt('save'));
112  $this->settings_form->addCommandButton('showNews', $this->lng->txt('cancel'));
113  $this->settings_form->setFormAction($this->ctrl->getFormaction($this, 'saveSettings'));
114  }
115 
116  public function showSettings()
117  {
118  global $ilTabs, $ilUser, $rbacreview;
119 
120  if(!$rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID))
121  {
122  $this->ilias->raiseError($this->lng->txt('permission_denied'), $this->ilias->error_obj->MESSAGE);
123  }
124 
125  $ilTabs->setSubTabActive('settings');
126 
127  $this->initSettingsForm('edit');
128  $this->getSettingsValues();
129 
130  return $this->tpl->setVariable('ADM_CONTENT', $this->settings_form->getHtml());
131  }
132 
133  public function saveNews()
134  {
135  global $ilUser, $rbacreview;
136 
137  if(!$rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID))
138  {
139  $this->ilias->raiseError($this->lng->txt('permission_denied'), $this->ilias->error_obj->MESSAGE);
140  }
141 
142  $this->initNewsForm('create');
143  if ($this->form_gui->checkInput())
144  {
145  $this->oCurrentNewsItem->setTitle($this->form_gui->getInput('news_title'));
146  $this->oCurrentNewsItem->setContent($this->form_gui->getInput('news_content'));
147  $this->oCurrentNewsItem->setVisibility($this->form_gui->getInput('news_visibility'));
148  $this->oCurrentNewsItem->setUserId($ilUser->getId());
149  $this->oCurrentNewsItem->create();
150 
151  ilUtil::sendInfo($this->lng->txt('payment_news_created'));
152  return $this->showNews();
153  }
154  else
155  {
156  $this->form_gui->setValuesByPost();
157  return $this->tpl->setVariable('ADM_CONTENT', $this->form_gui->getHtml());
158  }
159  }
160 
161  public function updateArchiveNews()
162  {
163  return $this->update('archive');
164  }
165 
166  public function updateNews()
167  {
168  return $this->update('news');
169  }
170 
171  public function update($view)
172  {
173  global $ilUser, $oCurrentNewsItem, $rbacreview;
174 
175  if(!$rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID))
176  {
177  $this->ilias->raiseError($this->lng->txt('permission_denied'), $this->ilias->error_obj->MESSAGE);
178  }
179 
180  $this->initNewsForm('edit', $view);
181 
182  if ($this->form_gui->checkInput())
183  {
184  $this->oCurrentNewsItem->setTitle($this->form_gui->getInput('news_title'));
185  $this->oCurrentNewsItem->setContent($this->form_gui->getInput('news_content'));
186  $this->oCurrentNewsItem->setVisibility($this->form_gui->getInput('news_visibility'));
187  $this->oCurrentNewsItem->setUserId($ilUser->getId());
188 
189  $this->oCurrentNewsItem->update();
190 
191  ilUtil::sendInfo($this->lng->txt('payment_news_updated'));
192 
193  if($view == 'archive')
194  {
195  return $this->showArchive();
196  }
197  else
198  {
199  return $this->showNews();
200  }
201 
202  }
203  else
204  {
205  $this->form_gui->setValuesByPost();
206  return $this->tpl->setVariable('ADM_CONTENT', $this->form_gui->getHtml());
207  }
208  }
209 
210  public function performDeleteNews()
211  {
212  return $this->performDelete('news');
213  }
214 
215  public function performDeleteArchiveNews()
216  {
217  return $this->performDelete('archive');
218  }
219 
220  public function performDelete($view)
221  {
222  global $ilUser, $rbacreview;
223 
224  if(!$rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID))
225  {
226  $this->ilias->raiseError($this->lng->txt('permission_denied'), $this->ilias->error_obj->MESSAGE);
227  }
228 
229  if(!(int)$_POST['news_id'])
230  {
231  ilUtil::sendInfo($this->lng->txt('payment_news_missing_id'));
232  switch($view)
233  {
234  case 'archive':
235  return $this->showArchive();
236  break;
237 
238  case 'news':
239  default:
240  return $this->showNews();
241  break;
242  }
243  }
244  else
245  {
246  $this->oCurrentNewsItem->setId($_POST['news_id']);
247  $this->oCurrentNewsItem->delete();
248  ilUtil::sendInfo($this->lng->txt('payment_news_deleted'));
249  }
250 
251  switch($view)
252  {
253  case 'archive':
254  return $this->showArchive();
255  break;
256 
257  case 'news':
258  default:
259  return $this->showNews();
260  break;
261  }
262 
263  return true;
264  }
265 
266  public function confirmDeleteNews()
267  {
268  $this->confirmDelete('news');
269  }
270 
271  public function confirmDeleteArchiveNews()
272  {
273  $this->confirmDelete('archive');
274  }
275 
276  public function confirmDelete($view)
277  {
278  global $ilUser, $rbacreview;
279 
280  if(!$rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID))
281  {
282  $this->ilias->raiseError($this->lng->txt('permission_denied'), $this->ilias->error_obj->MESSAGE);
283  }
284 
285  if(!isset($_GET['news_id']))
286  {
287  ilUtil::sendInfo($this->lng->txt('payment_news_missing_id'));
288  switch($view)
289  {
290  case 'archive':
291  return $this->showArchive();
292  break;
293 
294  case 'news':
295  default:
296  return $this->showNews();
297  break;
298  }
299  }
300 
301  include_once 'Services/Utilities/classes/class.ilConfirmationGUI.php';
302  $c_gui = new ilConfirmationGUI();
303  $c_gui->setHeaderText($this->lng->txt('payment_news_delete_sure'));
304  $c_gui->addHiddenItem('news_id', (int)$_GET['news_id']);
305 
306  $oNewsItem = new ilShopNewsItem($_GET['news_id']);
307  $title=$oNewsItem->getTitle();
308  $c_gui->addItem($news_title,$_GET['news_id'],$title);
309 
310 
311  switch($view)
312  {
313  case 'archive':
314  $c_gui->setFormAction($this->ctrl->getFormAction($this, 'performDeleteArchiveNews'));
315  $c_gui->setConfirm($this->lng->txt('confirm'), 'performDeleteArchiveNews');
316  $c_gui->setCancel($this->lng->txt('cancel'), 'showArchive');
317  $this->showArchive($c_gui->getHTML());
318  break;
319 
320  case 'news':
321  default:
322  $c_gui->setFormAction($this->ctrl->getFormAction($this, 'performDeleteNews'));
323  $c_gui->setConfirm($this->lng->txt('confirm'), 'performDeleteNews');
324  $c_gui->setCancel($this->lng->txt('cancel'), 'showNews');
325  $this->showNews($c_gui->getHTML());
326  break;
327  }
328 
329  return true;
330  }
331 
332  public function initNewsForm($a_mode = 'create', $view = 'news')
333  {
334  global $ilTabs, $rbacreview, $ilUser;
335 
336  if(!$rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID))
337  {
338  $this->ilias->raiseError($this->lng->txt('permission_denied'), $this->ilias->error_obj->MESSAGE);
339  }
340 
341  if($view == 'archive')
342  {
343  $ilTabs->setSubTabActive('archive');
344  }
345  else
346  {
347  $ilTabs->setSubTabActive('news');
348  }
349 
350 // include_once('./Services/Form/classes/class.ilPropertyFormGUI.php');
351  $this->form_gui = new ilPropertyFormGUI();
352  $this->form_gui->setTitle($this->lng->txt('shopnews_settings'));
353  $this->form_gui->setTitleIcon(ilUtil::getImagePath('icon_news.gif'));
354 
355  // Property Title
356  $text_input = new ilTextInputGUI($this->lng->txt('news_news_item_title'), 'news_title');
357  $text_input->setInfo('');
358  $text_input->setRequired(true);
359  $text_input->setMaxLength(200);
360  $text_input->setSize(93);
361 
362  $this->form_gui->addItem($text_input);
363 
364  // Property Content
365  $text_area = new ilTextAreaInputGUI($this->lng->txt('news_news_item_content'), 'news_content');
366  $text_area->setInfo('');
367  $text_area->setRequired(false);
368  $text_area->setCols(90);
369  $text_area->setRows(10);
370  $this->form_gui->addItem($text_area);
371 
372  // Property Visibility
373  $radio_group = new ilRadioGroupInputGUI($this->lng->txt('news_news_item_visibility'), 'news_visibility');
374  $radio_option = new ilRadioOption($this->lng->txt('news_visibility_users'), 'users');
375  $radio_group->addOption($radio_option);
376  $radio_option = new ilRadioOption($this->lng->txt('news_visibility_public'), 'public');
377  $radio_group->addOption($radio_option);
378  $radio_group->setInfo($this->lng->txt('news_news_item_visibility_info'));
379  $radio_group->setRequired(false);
380  $radio_group->setValue('users');
381  $this->form_gui->addItem($radio_group);
382 
383  // save and cancel commands
384  if($a_mode == 'create')
385  {
386  $this->form_gui->addCommandButton('saveNews', $this->lng->txt('save'));
387  $this->form_gui->addCommandButton('showNews', $this->lng->txt('cancel'));
388  $this->form_gui->setFormAction($this->ctrl->getFormAction($this, 'saveNews'));
389  }
390  else
391  {
392  $this->ctrl->setParameter($this, 'news_id', $this->oCurrentNewsItem->getId());
393 
394  if($view == 'archive')
395  {
396  $this->form_gui->addCommandButton('updateArchiveNews', $this->lng->txt('save'));
397  $this->form_gui->addCommandButton('showArchive', $this->lng->txt('cancel'));
398  $this->form_gui->setFormAction($this->ctrl->getFormAction($this, 'updateArchiveNews'));
399  }
400  else
401  {
402  $this->form_gui->addCommandButton('updateNews', $this->lng->txt('save'));
403  $this->form_gui->addCommandButton('showNews', $this->lng->txt('cancel'));
404  $this->form_gui->setFormAction($this->ctrl->getFormAction($this, 'updateNews'));
405  }
406  }
407 
408  $this->form_gui->setTitle($this->lng->txt('news_news_item_head'));
409  $this->tpl->setVariable('ADM_CONTENT', $this->form_gui->getHtml());
410  }
411 
412  public function addNewsForm()
413  {
414  $this->initNewsForm('create');
415  $this->tpl->setVariable('ADM_CONTENT', $this->form_gui->getHtml());
416  }
417 
418  public function editNews()
419  {
420  return $this->editNewsForm('news');
421  }
422 
423  public function editArchiveNews()
424  {
425  return $this->editNewsForm('archive');
426  }
427 
428  public function editNewsForm($view)
429  {
430  $this->initNewsForm('edit', $view);
431  $this->getPropertiesValues();
432  $this->tpl->setVariable('ADM_CONTENT', $this->form_gui->getHtml());
433  }
434 
435  public function getPropertiesValues()
436  {
437  $this->form_gui->setValuesByArray(array(
438  'news_creation_date' => $this->oCurrentNewsItem->getCreationDate(),
439  'news_title' => $this->oCurrentNewsItem->getTitle(),
440  'news_content' => $this->oCurrentNewsItem->getContent(),
441  'news_visibility' =>$this->oCurrentNewsItem->getVisibility()
442  ));
443  }
444 
445  public function getSettingsValues()
446  {
447  global $ilSetting;
448 
449  $this->settings_form->setValuesByArray(array(
450  'archive_period' => $ilSetting->get('payment_news_archive_period')
451  ));
452  }
453 
454  public function showNews($confirmation_gui = '')
455  {
456  global $ilUser, $lng, $tpl, $ilCtrl, $ilTabs, $ilShopNewsItem, $ilSetting, $rbacreview, $ilToolbar;
457 
458  $ilTabs->setSubTabActive('news');
459 
460  include_once 'Services/Payment/classes/class.ilShopNewsItemList.php';
461  include_once 'Services/Table/classes/class.ilTable2GUI.php';
462 
463  if($rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID) == true)
464  {
465  $ilToolbar->addButton($this->lng->txt('payment_news_add_news'), $this->ctrl->getLinkTarget($this, 'addNewsForm'));
466  }
467 
468  $news_tpl = new ilTemplate('tpl.main_view.html', true, true, 'Services/Payment');
469 
470  if((int)strlen($confirmation_gui))
471  {
472  $news_tpl->setVariable('CONFIRMATION', $confirmation_gui);
473  }
474 
475  $tbl = new ilTable2GUI($this);
476  $tbl->setId('shop_news_tbl');
477  $tbl->setTitle($this->lng->txt('news'), 'icon_news.gif', $this->lng->txt('news'));
478  $tbl->setRowTemplate('tpl.shop_news_row.html', 'Services/Payment');
479  $tbl->setFormAction($this->ctrl->getFormAction($this), 'showNews');
480  $tbl->addColumn($this->lng->txt('news'), 'title', '100%');
481 
482  $oNewsList = ilShopNewsItemList::_getInstance();
483  $oNewsList->setMode(ilShopNewsItemList::TYPE_NEWS)
484  ->setPublicSection($ilUser->getId() == ANONYMOUS_USER_ID)
485  ->setArchiveDate(time() - ($ilSetting->get('payment_news_archive_period') * 24 * 60 * 60))
486  ->read();
487 
488  $result = array();
489  if($oNewsList->hasItems())
490  {
491  $tbl->setEnableHeader(true);
492  $tbl->setEnableTitle(true);
493 
494  $counter = 0;
495  foreach($oNewsList as $entry)
496  {
497  if($rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID) == true)
498  {
499  $this->ctrl->setParameter($this, 'news_id', $entry->getId());
500 
501  $result[$counter]['news_id'] = $entry->getId();
502  $result[$counter]['edit_src'] = $this->ctrl->getLinkTarget($this, 'editNews');
503  $result[$counter]['edit_txt'] = $this->lng->txt('edit');
504  $result[$counter]['delete_src'] = $this->ctrl->getLinkTarget($this, 'confirmDeleteNews');
505  $result[$counter]['delete_txt'] = $this->lng->txt('delete');
506 
507  $this->ctrl->clearParameters($this);
508  }
509 
510  $result[$counter]['title'] = $entry->getTitle();
511  $result[$counter]['content'] = $entry->getContent();
512  $result[$counter]['user_id'] = $entry->getUserId();
513 
514  $result[$counter]['txt_author'] = $this->lng->txt('author');
515  $result[$counter]['author'] = $ilUser->getLogin();
516 
517  $result[$counter]['txt_creation_date'] = $this->lng->txt('create_date');
518  $result[$counter]['creation_date'] = ilDatePresentation::formatDate(
519  new ilDateTime($entry->getCreationDate(), IL_CAL_DATETIME));
520 
521  $result[$counter]['txt_update_date'] = $this->lng->txt('last_update');
522  $result[$counter]['update_date'] = ilDatePresentation::formatDate(
523  new ilDateTime($entry->getUpdateDate(), IL_CAL_DATETIME));
524 
525  $result[$counter]['txt_access'] = $this->lng->txt('access');
526  $result[$counter]['access'] = $entry->getVisibility();
527 
528  ++$counter;
529  }
530  }
531  else
532  {
533 
534  $tbl->setNoEntriesText($this->lng->txt('payment_news_no_news_items'));
535  }
536 
537  $tbl->setData($result);
538  $news_tpl->setVariable('TABLE', $tbl->getHTML());
539  $tpl->setContent($news_tpl->get());
540  }
541 
542  public function showArchive($confirmation_gui = '')
543  {
544  global $ilUser, $lng, $tpl, $ilCtrl, $ilTabs, $ilShopNewsItem, $ilSetting, $rbacreview;
545 
546  $ilTabs->setSubTabActive('archive');
547 
548  include_once 'Services/Payment/classes/class.ilShopNewsItemList.php';
549  include_once 'Services/Table/classes/class.ilTable2GUI.php';
550 
551  $news_tpl = new ilTemplate('tpl.main_view.html', true, true, 'Services/Payment');
552 
553  if((int)strlen($confirmation_gui))
554  {
555  $news_tpl->setVariable('CONFIRMATION', $confirmation_gui);
556  }
557 
558  $tbl = new ilTable2GUI($this);
559  $tbl->setId('shop_news_archive_tbl');
560  $tbl->setTitle($this->lng->txt('archive'), 'icon_news.gif',$this->lng->txt('news'));
561  $tbl->setRowTemplate('tpl.shop_news_row.html', 'Services/Payment');
562  $tbl->setFormAction($this->ctrl->getFormAction($this), 'showArchive');
563  $tbl->addColumn($this->lng->txt('archive'), 'title', '100%');
564 
565  $oNewsList = ilShopNewsItemList::_getInstance();
566  $oNewsList->setMode(ilShopNewsItemList::TYPE_ARCHIVE)
567  ->setPublicSection($ilUser->getId() == ANONYMOUS_USER_ID)
568  ->setArchiveDate(time() - ($ilSetting->get('payment_news_archive_period') * 24 * 60 * 60))
569  ->read();
570  $result = array();
571  if($oNewsList->hasItems())
572  {
573  $tbl->setEnableTitle(true);
574  $tbl->setEnableHeader(true);
575 
576  $counter = 0;
577  foreach($oNewsList as $entry)
578  {
579  if($entry->getVisibility() != 'public' && $ilUser->getId() == ANONYMOUS_USER_ID) continue;
580  if(strtotime($entry->getCreationDate()) > time() - ($ilSetting->get('payment_news_archive_period') * 24 * 60 * 60)) continue;
581 
582  if($rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID) == true)
583  {
584  $this->ctrl->setParameter($this, 'news_id', $entry->getId());
585 
586  $result[$counter]['news_id']= $entry->getId();
587  $result[$counter]['edit_src'] = $this->ctrl->getLinkTarget($this, 'editArchiveNews');
588  $result[$counter]['edit_txt'] = $this->lng->txt('edit');
589  $result[$counter]['delete_src'] = $this->ctrl->getLinkTarget($this, 'confirmDeleteArchiveNews');
590  $result[$counter]['delete_txt'] = $this->lng->txt('delete');
591 
592  $this->ctrl->clearParameters($this);
593  }
594 
595  $result[$counter]['creation_date'] = ilDatePresentation::formatDate(
596  new ilDateTime($entry->getCreationDate(), IL_CAL_DATETIME));
597  $result[$counter]['title'] = $entry->getTitle();
598  $result[$counter]['content'] = $entry->getContent();
599  $result[$counter]['user_id'] = $entry->getUserId();
600 
601  $result[$counter]['author'] = $ilUser->getLogin();
602  $result[$counter]['creation_date'] = ilDatePresentation::formatDate(
603  new ilDateTime($entry->getCreationDate(), IL_CAL_DATETIME));
604 
605  $result[$counter]['update_date'] = ilDatePresentation::formatDate(
606  new ilDateTime($entry->getCreationDate(), IL_CAL_DATETIME));
607 
608  $result[$counter]['txt_author'] = $this->lng->txt('author');
609  $result[$counter]['txt_creation_date'] = $this->lng->txt('create_date');
610  $result[$counter]['txt_update_date'] = $this->lng->txt('last_update');
611 
612  $this->ctrl->clearParameters($this);
613 
614  ++$counter;
615  }
616  }
617  else
618  {
619  $tbl->setNoEntriesText($this->lng->txt('payment_news_no_news_items'));
620  }
621  $tbl->setData($result);
622  $news_tpl->setVariable('TABLE', $tbl->getHTML());
623  $tpl->setContent($news_tpl->get());
624  }
625 
626  protected function prepareOutput()
627  {
628  global $ilTabs;
629 
631 
632  $ilTabs->setTabActive('payment_news');
633 
634  }
635 }
636 ?>