scheduled_announcement.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. $cidReset = true;
  4. require_once __DIR__.'/../inc/global.inc.php';
  5. api_protect_admin_script(true);
  6. $sessionId = isset($_GET['session_id']) ? (int) $_GET['session_id'] : 0;
  7. $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
  8. $action = isset($_GET['action']) ? $_GET['action'] : '';
  9. $sessionInfo = api_get_session_info($sessionId);
  10. if (!$sessionInfo) {
  11. api_not_allowed(true);
  12. }
  13. $object = new ScheduledAnnouncement();
  14. if (!$object->allowed()) {
  15. api_not_allowed(true);
  16. }
  17. $sessionUrl = api_get_path(WEB_CODE_PATH).'session/resume_session.php?id_session='.$sessionId;
  18. $htmlHeadXtra[] = api_get_jqgrid_js();
  19. $interbreadcrumb[] = array(
  20. 'url' => "session_list.php",
  21. "name" => get_lang('SessionList')
  22. );
  23. $interbreadcrumb[] = array(
  24. 'url' => $sessionUrl,
  25. "name" => get_lang('SessionOverview')
  26. );
  27. if ($action == 'add') {
  28. $interbreadcrumb[] = array(
  29. 'url' => api_get_self()."?session_id=".$sessionId,
  30. "name" => get_lang('ScheduledAnnouncements')
  31. );
  32. $tool_name = get_lang('Add');
  33. } elseif ($action == 'edit') {
  34. $tool_name = get_lang('Edit');
  35. $interbreadcrumb[] = array(
  36. 'url' => api_get_self()."?session_id=".$sessionId,
  37. "name" => get_lang('ScheduledAnnouncements')
  38. );
  39. } else {
  40. $tool_name = get_lang('ScheduledAnnouncements');
  41. }
  42. switch ($action) {
  43. case 'run':
  44. $messagesSent = $object->sendPendingMessages();
  45. Display::addFlash(
  46. Display::return_message(
  47. get_lang('MessageSent').': '.$messagesSent,
  48. 'confirmation'
  49. )
  50. );
  51. $content = $object->getGrid($sessionId);
  52. break;
  53. case 'add':
  54. $url = api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&session_id='.$sessionId;
  55. $form = $object->returnForm($url, 'add', $sessionInfo);
  56. // The validation or display
  57. if ($form->validate()) {
  58. $values = $form->getSubmitValues();
  59. switch ($values['type']) {
  60. case 'base_date':
  61. $numberDays = (int) $values['days'];
  62. switch ($values['base_date']) {
  63. case 'start_date':
  64. $baseDate = new DateTime($sessionInfo['access_start_date']);
  65. break;
  66. case 'end_date':
  67. $baseDate = new DateTime($sessionInfo['access_end_date']);
  68. break;
  69. }
  70. $interval = new DateInterval('P'.$numberDays.'D');
  71. switch ($values['moment_type']) {
  72. case 'after':
  73. $newDate = $baseDate->add($interval);
  74. break;
  75. case 'before':
  76. $newDate = $baseDate->sub($interval);
  77. break;
  78. }
  79. $values['date'] = $newDate->format('Y-m-d h:i:s');
  80. break;
  81. case 'specific_date':
  82. $values['date'] = api_get_utc_datetime($values['date']);
  83. break;
  84. }
  85. $res = $object->save($values);
  86. if ($res) {
  87. Display::addFlash(
  88. Display::return_message(
  89. get_lang('ItemAdded'),
  90. 'confirmation'
  91. )
  92. );
  93. }
  94. $content = $object->getGrid($sessionId);
  95. } else {
  96. $content = '<div class="actions">';
  97. $content .= Display::url(
  98. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM),
  99. api_get_self().'?session_id='.$sessionId
  100. );
  101. $content .= '</div>';
  102. $form->addElement('hidden', 'sec_token');
  103. $content .= $form->returnForm();
  104. }
  105. break;
  106. case 'edit':
  107. // Action handling: Editing
  108. $url = api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&id='.intval($_GET['id']).'&session_id='.$sessionId;
  109. $form = $object->returnSimpleForm($url, 'edit', $sessionInfo);
  110. if ($form->validate()) {
  111. $values = $form->getSubmitValues();
  112. $values['id'] = $id;
  113. $values['sent'] = isset($values['sent']) ? 1 : '';
  114. $values['date'] = api_get_utc_datetime($values['date']);
  115. $res = $object->update($values);
  116. Display::addFlash(Display::return_message(
  117. get_lang('Updated'),
  118. 'confirmation'
  119. ));
  120. }
  121. $item = $object->get($id);
  122. $item['date'] = api_get_local_time($item['date']);
  123. $form->setDefaults($item);
  124. $content = $form->returnForm();
  125. break;
  126. case 'delete':
  127. $object->delete($_GET['id']);
  128. $content = $object->getGrid($sessionId);
  129. break;
  130. default:
  131. $content = $object->getGrid($sessionId);
  132. break;
  133. }
  134. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_programmed_announcements&session_id='.$sessionId;
  135. $columns = [
  136. get_lang('Subject'),
  137. get_lang('Date'),
  138. get_lang('Sent'),
  139. get_lang('Actions')
  140. ];
  141. $columnModel = [
  142. array(
  143. 'name' => 'subject',
  144. 'index' => 'subject',
  145. 'width' => '250',
  146. 'align' => 'left',
  147. ),
  148. array(
  149. 'name' => 'date',
  150. 'index' => 'date',
  151. //'width' => '90',
  152. //'align' => 'left',
  153. 'sortable' => 'true',
  154. ),
  155. array(
  156. 'name' => 'sent',
  157. 'index' => 'sent',
  158. //'width' => '90',
  159. //'align' => 'left',
  160. 'sortable' => 'true',
  161. ),
  162. array(
  163. 'name' => 'actions',
  164. 'index' => 'actions',
  165. 'width' => '100',
  166. 'align' => 'left',
  167. 'formatter' => 'action_formatter',
  168. 'sortable' => 'false',
  169. )
  170. ];
  171. $actionLinks = 'function action_formatter(cellvalue, options, rowObject) {
  172. return \'<a href="?action=edit&session_id='.$sessionId.'&id=\'+options.rowId+\'">'.Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>'.
  173. '&nbsp;<a onclick="javascript:if(!confirm('."\'".addslashes(api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES))."\'".')) return false;" href="?action=delete&session_id='.$sessionId.'&id=\'+options.rowId+\'">'.Display::return_icon('delete.png', get_lang('Delete'), '', ICON_SIZE_SMALL).'</a>'.
  174. '\';
  175. }';
  176. $extraParams = [];
  177. $extraParams['autowidth'] = 'true';
  178. $htmlHeadXtra[] = '<script>
  179. $(function() {
  180. // grid definition see the $obj->display() function
  181. '.Display::grid_js(
  182. 'programmed',
  183. $url,
  184. $columns,
  185. $columnModel,
  186. $extraParams,
  187. array(),
  188. $actionLinks,
  189. true
  190. ).'
  191. });
  192. </script>';
  193. $tpl = new Template($tool_name);
  194. $sessionTitle = Display::page_header($sessionInfo['name']);
  195. $tpl->assign('content', $sessionTitle.$content);
  196. $tpl->display_one_col_template();