scheduled_announcement.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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[] = [
  20. 'url' => 'session_list.php',
  21. 'name' => get_lang('SessionList'),
  22. ];
  23. $interbreadcrumb[] = [
  24. 'url' => $sessionUrl,
  25. 'name' => get_lang('SessionOverview'),
  26. ];
  27. if ($action == 'add') {
  28. $interbreadcrumb[] = [
  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[] = [
  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. $extraFieldValue = new ExtraFieldValue('scheduled_announcement');
  88. $values['item_id'] = $res;
  89. $extraFieldValue->saveFieldValues($values);
  90. Display::addFlash(
  91. Display::return_message(
  92. get_lang('ItemAdded'),
  93. 'confirmation'
  94. )
  95. );
  96. }
  97. $content = $object->getGrid($sessionId);
  98. } else {
  99. $content = '<div class="actions">';
  100. $content .= Display::url(
  101. Display::return_icon('back.png', get_lang('Back'), '', ICON_SIZE_MEDIUM),
  102. api_get_self().'?session_id='.$sessionId
  103. );
  104. $content .= '</div>';
  105. $form->addElement('hidden', 'sec_token');
  106. $content .= $form->returnForm();
  107. }
  108. break;
  109. case 'edit':
  110. // Action handling: Editing
  111. $url = api_get_self().'?action='.Security::remove_XSS($_GET['action']).'&id='.$id.'&session_id='.$sessionId;
  112. $form = $object->returnSimpleForm($id, $url, 'edit', $sessionInfo);
  113. if ($form->validate()) {
  114. $values = $form->getSubmitValues();
  115. $values['id'] = $id;
  116. $values['sent'] = isset($values['sent']) ? 1 : '';
  117. $values['date'] = api_get_utc_datetime($values['date']);
  118. $res = $object->update($values);
  119. $extraFieldValue = new ExtraFieldValue('scheduled_announcement');
  120. $values['item_id'] = $id;
  121. $extraFieldValue->saveFieldValues($values);
  122. Display::addFlash(Display::return_message(
  123. get_lang('Updated'),
  124. 'confirmation'
  125. ));
  126. header("Location: $url");
  127. exit;
  128. }
  129. $item = $object->get($id);
  130. $item['date'] = api_get_local_time($item['date']);
  131. $form->setDefaults($item);
  132. $content = $form->returnForm();
  133. break;
  134. case 'delete':
  135. $object->delete($id);
  136. $content = $object->getGrid($sessionId);
  137. break;
  138. default:
  139. $content = $object->getGrid($sessionId);
  140. break;
  141. }
  142. $url = api_get_path(WEB_AJAX_PATH).'model.ajax.php?a=get_programmed_announcements&session_id='.$sessionId;
  143. $columns = [
  144. get_lang('Subject'),
  145. get_lang('Date'),
  146. get_lang('Sent'),
  147. get_lang('Actions'),
  148. ];
  149. $columnModel = [
  150. [
  151. 'name' => 'subject',
  152. 'index' => 'subject',
  153. 'width' => '250',
  154. 'align' => 'left',
  155. ],
  156. [
  157. 'name' => 'date',
  158. 'index' => 'date',
  159. //'width' => '90',
  160. //'align' => 'left',
  161. 'sortable' => 'true',
  162. ],
  163. [
  164. 'name' => 'sent',
  165. 'index' => 'sent',
  166. //'width' => '90',
  167. //'align' => 'left',
  168. 'sortable' => 'true',
  169. ],
  170. [
  171. 'name' => 'actions',
  172. 'index' => 'actions',
  173. 'width' => '100',
  174. 'align' => 'left',
  175. 'formatter' => 'action_formatter',
  176. 'sortable' => 'false',
  177. ],
  178. ];
  179. $actionLinks = 'function action_formatter(cellvalue, options, rowObject) {
  180. return \'<a href="?action=edit&session_id='.$sessionId.'&id=\'+options.rowId+\'">'.Display::return_icon('edit.png', get_lang('Edit'), '', ICON_SIZE_SMALL).'</a>'.
  181. '&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>'.
  182. '\';
  183. }';
  184. $extraParams = [];
  185. $extraParams['autowidth'] = 'true';
  186. $htmlHeadXtra[] = '<script>
  187. $(function() {
  188. // grid definition see the $obj->display() function
  189. '.Display::grid_js(
  190. 'programmed',
  191. $url,
  192. $columns,
  193. $columnModel,
  194. $extraParams,
  195. [],
  196. $actionLinks,
  197. true
  198. ).'
  199. });
  200. </script>';
  201. $tpl = new Template($tool_name);
  202. $sessionTitle = Display::page_header($sessionInfo['name']);
  203. $tpl->assign('content', $sessionTitle.$content);
  204. $tpl->display_one_col_template();