ILIAS  release_4-3 Revision
 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 $ilErr = null;
24 
25  public function __construct()
26  {
27  global $ilErr;
29 
30  $this->oCurrentNewsItem = new ilShopNewsItem((int)$_GET['news_id']);
31 
32  $this->lng->loadLanguageModule('news');
33  $this->ilErr = $ilErr;
34  }
35 
36  public function executeCommand()
37  {
38 // $next_class = $this->ctrl->getNextClass($this);
39  $cmd = $this->ctrl->getCmd();
40 
41  switch($cmd)
42  {
43  default:
44  if(!$cmd)
45  {
46  $cmd = 'showNews';
47  }
48  $this->prepareOutput();
49  $this->$cmd();
50  break;
51  }
52 
53  return true;
54  }
55 
56  protected function buildSubTabs()
57  {
58  global $ilTabs, $ilUser, $rbacreview;
59 
60  $ilTabs->addSubTabTarget('news', $this->ctrl->getLinkTarget($this, 'showNews'), '', '', '','showNews');
61  $ilTabs->addSubTabTarget('archive', $this->ctrl->getLinkTarget($this, 'showArchive'), '', '', '','archive');
62 
63  if($rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID))
64  {
65  $ilTabs->addSubTabTarget('settings', $this->ctrl->getLinkTarget($this, 'showSettings'), '', '', '','settings');
66  }
67  }
68 
69  public function saveSettings()
70  {
71  global $ilTabs, $ilSetting, $rbacreview, $ilUser;
72 
73  if(!$rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID))
74  {
75  $this->ilErr->raiseError($this->lng->txt('permission_denied'), $this->ilErr->MESSAGE);
76  }
77 
78  $ilTabs->setSubTabActive('settings');
79 
80  $this->initSettingsForm('edit');
81  if($this->settings_form->checkInput())
82  {
83  $ilSetting->set('payment_news_archive_period', $this->settings_form->getInput('archive_period'));
84  $this->getSettingsValues();
85  ilUtil::sendInfo($this->lng->txt('payment_news_settings_saved'));
86 
87  return $this->tpl->setVariable('ADM_CONTENT', $this->settings_form->getHtml());
88  }
89  else
90  {
91  $this->form_gui->setValuesByPost();
92  return $this->tpl->setVariable('ADM_CONTENT', $this->settings_form->getHtml());
93  }
94  }
95 
96  public function initSettingsForm($a_mode = 'edit')
97  {
98 
99  $this->settings_form = new ilPropertyFormGUI();
100  $this->settings_form->setTitle($this->lng->txt('payment_news_settings'));
101  $this->settings_form->setTitleIcon(ilUtil::getImagePath('icon_news.png'));
102 
103  $oSelectBox = new ilSelectInputGUI($this->lng->txt('payment_news_archive_period'), 'archive_period');
104  $oSelectBox->setInfo($this->lng->txt('payment_news_archive_period_info'));
105  $options = array();
106  for($i = 5; $i <= 100; $i += 5)
107  {
108  $options[$i] = $i;
109  }
110  $oSelectBox->setOptions($options);
111  $this->settings_form->addItem($oSelectBox);
112 
113  $this->settings_form->addCommandButton('saveSettings', $this->lng->txt('save'));
114  $this->settings_form->addCommandButton('showNews', $this->lng->txt('cancel'));
115  $this->settings_form->setFormAction($this->ctrl->getFormaction($this, 'saveSettings'));
116  }
117 
118  public function showSettings()
119  {
120  global $ilTabs, $ilUser, $rbacreview;
121 
122  if(!$rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID))
123  {
124  $this->ilErr->raiseError($this->lng->txt('permission_denied'),$this->ilErr->MESSAGE);
125  }
126 
127  $ilTabs->setSubTabActive('settings');
128 
129  $this->initSettingsForm('edit');
130  $this->getSettingsValues();
131 
132  return $this->tpl->setVariable('ADM_CONTENT', $this->settings_form->getHtml());
133  }
134 
135  public function saveNews()
136  {
137  global $ilUser, $rbacreview;
138 
139  if(!$rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID))
140  {
141  $this->ilErr->raiseError($this->lng->txt('permission_denied'),$this->ilErr->MESSAGE);
142  }
143 
144  $this->initNewsForm('create');
145  if ($this->form_gui->checkInput())
146  {
147  $this->oCurrentNewsItem->setTitle($this->form_gui->getInput('news_title'));
148  $this->oCurrentNewsItem->setContent($this->form_gui->getInput('news_content'));
149  $this->oCurrentNewsItem->setVisibility($this->form_gui->getInput('news_visibility'));
150  $this->oCurrentNewsItem->setUserId($ilUser->getId());
151  $this->oCurrentNewsItem->create();
152 
153  ilUtil::sendInfo($this->lng->txt('payment_news_created'));
154  return $this->showNews();
155  }
156  else
157  {
158  $this->form_gui->setValuesByPost();
159  return $this->tpl->setVariable('ADM_CONTENT', $this->form_gui->getHtml());
160  }
161  }
162 
163  public function updateArchiveNews()
164  {
165  return $this->update('archive');
166  }
167 
168  public function updateNews()
169  {
170  return $this->update('news');
171  }
172 
173  public function update($view)
174  {
175  global $ilUser, $rbacreview;
176 
177  if(!$rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID))
178  {
179  $this->ilErr->raiseError($this->lng->txt('permission_denied'),$this->ilErr->MESSAGE);
180  }
181 
182  $this->initNewsForm('edit', $view);
183 
184  if ($this->form_gui->checkInput())
185  {
186  $this->oCurrentNewsItem->setTitle($this->form_gui->getInput('news_title'));
187  $this->oCurrentNewsItem->setContent($this->form_gui->getInput('news_content'));
188  $this->oCurrentNewsItem->setVisibility($this->form_gui->getInput('news_visibility'));
189  $this->oCurrentNewsItem->setUserId($ilUser->getId());
190 
191  $this->oCurrentNewsItem->update();
192 
193  ilUtil::sendInfo($this->lng->txt('payment_news_updated'));
194 
195  if($view == 'archive')
196  {
197  return $this->showArchive();
198  }
199  else
200  {
201  return $this->showNews();
202  }
203 
204  }
205  else
206  {
207  $this->form_gui->setValuesByPost();
208  return $this->tpl->setVariable('ADM_CONTENT', $this->form_gui->getHtml());
209  }
210  }
211 
212  public function performDeleteNews()
213  {
214  return $this->performDelete('news');
215  }
216 
217  public function performDeleteArchiveNews()
218  {
219  return $this->performDelete('archive');
220  }
221 
222  public function performDelete($view)
223  {
224  global $ilUser, $rbacreview;
225 
226  if(!$rbacreview->isAssigned($ilUser->getId(), SYSTEM_ROLE_ID))
227  {
228  $this->ilErr->raiseError($this->lng->txt('permission_denied'),$this->ilErr->MESSAGE);
229  }
230 
231  if(!(int)$_POST['news_id'])
232  {
233  ilUtil::sendInfo($this->lng->txt('payment_news_missing_id'));
234  switch($view)
235  {
236  case 'archive':
237  return $this->showArchive();
238  break;
239 
240  case 'news':
241  default:
242  return $this->showNews();
243  break;
244  }
245  }
246  else
247  {
248  $this->oCurrentNewsItem->setId($_POST['news_id']);
249  $this->oCurrentNewsItem->delete();
250  ilUtil::sendInfo($this->lng->txt('payment_news_deleted'));
251  }
252 
253  switch($view)
254  {
255  case 'archive':
256  return $this->showArchive();
257  break;
258 
259  case 'news':
260  default:
261  return $this->showNews();
262  break;
263  }
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->ilErr->raiseError($this->lng->txt('permission_denied'),$this->ilErr->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->ilErr->raiseError($this->lng->txt('permission_denied'),$this->ilErr->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.png'));
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, $tpl, $ilTabs, $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.png', $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, $tpl, $ilTabs, $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.png',$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 ?>