scheduled_announcement.php 7.1 KB

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