Events.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Functions for event management.
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin\Rte;
  9. use PhpMyAdmin\Message;
  10. use PhpMyAdmin\Response;
  11. use PhpMyAdmin\Rte\Export;
  12. use PhpMyAdmin\Rte\Footer;
  13. use PhpMyAdmin\Rte\General;
  14. use PhpMyAdmin\Rte\RteList;
  15. use PhpMyAdmin\Rte\Words;
  16. use PhpMyAdmin\Url;
  17. use PhpMyAdmin\Util;
  18. /**
  19. * PhpMyAdmin\Rte\Events class
  20. *
  21. * @package PhpMyAdmin
  22. */
  23. class Events
  24. {
  25. /**
  26. * Sets required globals
  27. *
  28. * @return void
  29. */
  30. public static function setGlobals()
  31. {
  32. global $event_status, $event_type, $event_interval;
  33. $event_status = array(
  34. 'query' => array('ENABLE',
  35. 'DISABLE',
  36. 'DISABLE ON SLAVE'),
  37. 'display' => array('ENABLED',
  38. 'DISABLED',
  39. 'SLAVESIDE_DISABLED')
  40. );
  41. $event_type = array('RECURRING',
  42. 'ONE TIME');
  43. $event_interval = array('YEAR',
  44. 'QUARTER',
  45. 'MONTH',
  46. 'DAY',
  47. 'HOUR',
  48. 'MINUTE',
  49. 'WEEK',
  50. 'SECOND',
  51. 'YEAR_MONTH',
  52. 'DAY_HOUR',
  53. 'DAY_MINUTE',
  54. 'DAY_SECOND',
  55. 'HOUR_MINUTE',
  56. 'HOUR_SECOND',
  57. 'MINUTE_SECOND');
  58. }
  59. /**
  60. * Main function for the events functionality
  61. *
  62. * @return void
  63. */
  64. public static function main()
  65. {
  66. global $db;
  67. self::setGlobals();
  68. /**
  69. * Process all requests
  70. */
  71. self::handleEditor();
  72. Export::events();
  73. /**
  74. * Display a list of available events
  75. */
  76. $items = $GLOBALS['dbi']->getEvents($db);
  77. echo RteList::get('event', $items);
  78. /**
  79. * Display a link for adding a new event, if
  80. * the user has the privileges and a link to
  81. * toggle the state of the event scheduler.
  82. */
  83. echo Footer::events();
  84. } // end self::main()
  85. /**
  86. * Handles editor requests for adding or editing an item
  87. *
  88. * @return void
  89. */
  90. public static function handleEditor()
  91. {
  92. global $_REQUEST, $_POST, $errors, $db;
  93. if (! empty($_POST['editor_process_add'])
  94. || ! empty($_POST['editor_process_edit'])
  95. ) {
  96. $sql_query = '';
  97. $item_query = self::getQueryFromRequest();
  98. if (! count($errors)) { // set by PhpMyAdmin\Rte\Routines::getQueryFromRequest()
  99. // Execute the created query
  100. if (! empty($_POST['editor_process_edit'])) {
  101. // Backup the old trigger, in case something goes wrong
  102. $create_item = $GLOBALS['dbi']->getDefinition(
  103. $db,
  104. 'EVENT',
  105. $_POST['item_original_name']
  106. );
  107. $drop_item = "DROP EVENT "
  108. . Util::backquote($_POST['item_original_name'])
  109. . ";\n";
  110. $result = $GLOBALS['dbi']->tryQuery($drop_item);
  111. if (! $result) {
  112. $errors[] = sprintf(
  113. __('The following query has failed: "%s"'),
  114. htmlspecialchars($drop_item)
  115. )
  116. . '<br />'
  117. . __('MySQL said: ') . $GLOBALS['dbi']->getError();
  118. } else {
  119. $result = $GLOBALS['dbi']->tryQuery($item_query);
  120. if (! $result) {
  121. $errors[] = sprintf(
  122. __('The following query has failed: "%s"'),
  123. htmlspecialchars($item_query)
  124. )
  125. . '<br />'
  126. . __('MySQL said: ') . $GLOBALS['dbi']->getError();
  127. // We dropped the old item, but were unable to create
  128. // the new one. Try to restore the backup query
  129. $result = $GLOBALS['dbi']->tryQuery($create_item);
  130. $errors = General::checkResult(
  131. $result,
  132. __(
  133. 'Sorry, we failed to restore the dropped event.'
  134. ),
  135. $create_item,
  136. $errors
  137. );
  138. } else {
  139. $message = Message::success(
  140. __('Event %1$s has been modified.')
  141. );
  142. $message->addParam(
  143. Util::backquote($_POST['item_name'])
  144. );
  145. $sql_query = $drop_item . $item_query;
  146. }
  147. }
  148. } else {
  149. // 'Add a new item' mode
  150. $result = $GLOBALS['dbi']->tryQuery($item_query);
  151. if (! $result) {
  152. $errors[] = sprintf(
  153. __('The following query has failed: "%s"'),
  154. htmlspecialchars($item_query)
  155. )
  156. . '<br /><br />'
  157. . __('MySQL said: ') . $GLOBALS['dbi']->getError();
  158. } else {
  159. $message = Message::success(
  160. __('Event %1$s has been created.')
  161. );
  162. $message->addParam(
  163. Util::backquote($_POST['item_name'])
  164. );
  165. $sql_query = $item_query;
  166. }
  167. }
  168. }
  169. if (count($errors)) {
  170. $message = Message::error(
  171. '<b>'
  172. . __(
  173. 'One or more errors have occurred while processing your request:'
  174. )
  175. . '</b>'
  176. );
  177. $message->addHtml('<ul>');
  178. foreach ($errors as $string) {
  179. $message->addHtml('<li>' . $string . '</li>');
  180. }
  181. $message->addHtml('</ul>');
  182. }
  183. $output = Util::getMessage($message, $sql_query);
  184. $response = Response::getInstance();
  185. if ($response->isAjax()) {
  186. if ($message->isSuccess()) {
  187. $events = $GLOBALS['dbi']->getEvents($db, $_POST['item_name']);
  188. $event = $events[0];
  189. $response->addJSON(
  190. 'name',
  191. htmlspecialchars(
  192. mb_strtoupper($_POST['item_name'])
  193. )
  194. );
  195. if (! empty($event)) {
  196. $response->addJSON('new_row', RteList::getEventRow($event));
  197. }
  198. $response->addJSON('insert', ! empty($event));
  199. $response->addJSON('message', $output);
  200. } else {
  201. $response->setRequestStatus(false);
  202. $response->addJSON('message', $message);
  203. }
  204. exit;
  205. }
  206. }
  207. /**
  208. * Display a form used to add/edit a trigger, if necessary
  209. */
  210. if (count($errors)
  211. || (empty($_POST['editor_process_add'])
  212. && empty($_POST['editor_process_edit'])
  213. && (! empty($_REQUEST['add_item'])
  214. || ! empty($_REQUEST['edit_item'])
  215. || ! empty($_POST['item_changetype'])))
  216. ) { // FIXME: this must be simpler than that
  217. $operation = '';
  218. if (! empty($_POST['item_changetype'])) {
  219. $operation = 'change';
  220. }
  221. // Get the data for the form (if any)
  222. if (! empty($_REQUEST['add_item'])) {
  223. $title = Words::get('add');
  224. $item = self::getDataFromRequest();
  225. $mode = 'add';
  226. } elseif (! empty($_REQUEST['edit_item'])) {
  227. $title = __("Edit event");
  228. if (! empty($_REQUEST['item_name'])
  229. && empty($_POST['editor_process_edit'])
  230. && empty($_POST['item_changetype'])
  231. ) {
  232. $item = self::getDataFromName($_REQUEST['item_name']);
  233. if ($item !== false) {
  234. $item['item_original_name'] = $item['item_name'];
  235. }
  236. } else {
  237. $item = self::getDataFromRequest();
  238. }
  239. $mode = 'edit';
  240. }
  241. General::sendEditor('EVN', $mode, $item, $title, $db, $operation);
  242. }
  243. } // end self::handleEditor()
  244. /**
  245. * This function will generate the values that are required to for the editor
  246. *
  247. * @return array Data necessary to create the editor.
  248. */
  249. public static function getDataFromRequest()
  250. {
  251. $retval = array();
  252. $indices = array('item_name',
  253. 'item_original_name',
  254. 'item_status',
  255. 'item_execute_at',
  256. 'item_interval_value',
  257. 'item_interval_field',
  258. 'item_starts',
  259. 'item_ends',
  260. 'item_definition',
  261. 'item_preserve',
  262. 'item_comment',
  263. 'item_definer');
  264. foreach ($indices as $index) {
  265. $retval[$index] = isset($_POST[$index]) ? $_POST[$index] : '';
  266. }
  267. $retval['item_type'] = 'ONE TIME';
  268. $retval['item_type_toggle'] = 'RECURRING';
  269. if (isset($_POST['item_type']) && $_POST['item_type'] == 'RECURRING') {
  270. $retval['item_type'] = 'RECURRING';
  271. $retval['item_type_toggle'] = 'ONE TIME';
  272. }
  273. return $retval;
  274. } // end self::getDataFromRequest()
  275. /**
  276. * This function will generate the values that are required to complete
  277. * the "Edit event" form given the name of a event.
  278. *
  279. * @param string $name The name of the event.
  280. *
  281. * @return array Data necessary to create the editor.
  282. */
  283. public static function getDataFromName($name)
  284. {
  285. global $db;
  286. $retval = array();
  287. $columns = "`EVENT_NAME`, `STATUS`, `EVENT_TYPE`, `EXECUTE_AT`, "
  288. . "`INTERVAL_VALUE`, `INTERVAL_FIELD`, `STARTS`, `ENDS`, "
  289. . "`EVENT_DEFINITION`, `ON_COMPLETION`, `DEFINER`, `EVENT_COMMENT`";
  290. $where = "EVENT_SCHEMA " . Util::getCollateForIS() . "="
  291. . "'" . $GLOBALS['dbi']->escapeString($db) . "' "
  292. . "AND EVENT_NAME='" . $GLOBALS['dbi']->escapeString($name) . "'";
  293. $query = "SELECT $columns FROM `INFORMATION_SCHEMA`.`EVENTS` WHERE $where;";
  294. $item = $GLOBALS['dbi']->fetchSingleRow($query);
  295. if (! $item) {
  296. return false;
  297. }
  298. $retval['item_name'] = $item['EVENT_NAME'];
  299. $retval['item_status'] = $item['STATUS'];
  300. $retval['item_type'] = $item['EVENT_TYPE'];
  301. if ($retval['item_type'] == 'RECURRING') {
  302. $retval['item_type_toggle'] = 'ONE TIME';
  303. } else {
  304. $retval['item_type_toggle'] = 'RECURRING';
  305. }
  306. $retval['item_execute_at'] = $item['EXECUTE_AT'];
  307. $retval['item_interval_value'] = $item['INTERVAL_VALUE'];
  308. $retval['item_interval_field'] = $item['INTERVAL_FIELD'];
  309. $retval['item_starts'] = $item['STARTS'];
  310. $retval['item_ends'] = $item['ENDS'];
  311. $retval['item_preserve'] = '';
  312. if ($item['ON_COMPLETION'] == 'PRESERVE') {
  313. $retval['item_preserve'] = " checked='checked'";
  314. }
  315. $retval['item_definition'] = $item['EVENT_DEFINITION'];
  316. $retval['item_definer'] = $item['DEFINER'];
  317. $retval['item_comment'] = $item['EVENT_COMMENT'];
  318. return $retval;
  319. } // end self::getDataFromName()
  320. /**
  321. * Displays a form used to add/edit an event
  322. *
  323. * @param string $mode If the editor will be used to edit an event
  324. * or add a new one: 'edit' or 'add'.
  325. * @param string $operation If the editor was previously invoked with
  326. * JS turned off, this will hold the name of
  327. * the current operation
  328. * @param array $item Data for the event returned by
  329. * self::getDataFromRequest() or
  330. * self::getDataFromName()
  331. *
  332. * @return string HTML code for the editor.
  333. */
  334. public static function getEditorForm($mode, $operation, array $item)
  335. {
  336. global $db, $table, $event_status, $event_type, $event_interval;
  337. $modeToUpper = mb_strtoupper($mode);
  338. $response = Response::getInstance();
  339. // Escape special characters
  340. $need_escape = array(
  341. 'item_original_name',
  342. 'item_name',
  343. 'item_type',
  344. 'item_execute_at',
  345. 'item_interval_value',
  346. 'item_starts',
  347. 'item_ends',
  348. 'item_definition',
  349. 'item_definer',
  350. 'item_comment'
  351. );
  352. foreach ($need_escape as $index) {
  353. $item[$index] = htmlentities($item[$index], ENT_QUOTES);
  354. }
  355. $original_data = '';
  356. if ($mode == 'edit') {
  357. $original_data = "<input name='item_original_name' "
  358. . "type='hidden' value='{$item['item_original_name']}'/>\n";
  359. }
  360. // Handle some logic first
  361. if ($operation == 'change') {
  362. if ($item['item_type'] == 'RECURRING') {
  363. $item['item_type'] = 'ONE TIME';
  364. $item['item_type_toggle'] = 'RECURRING';
  365. } else {
  366. $item['item_type'] = 'RECURRING';
  367. $item['item_type_toggle'] = 'ONE TIME';
  368. }
  369. }
  370. if ($item['item_type'] == 'ONE TIME') {
  371. $isrecurring_class = ' hide';
  372. $isonetime_class = '';
  373. } else {
  374. $isrecurring_class = '';
  375. $isonetime_class = ' hide';
  376. }
  377. // Create the output
  378. $retval = "";
  379. $retval .= "<!-- START " . $modeToUpper . " EVENT FORM -->\n\n";
  380. $retval .= "<form class='rte_form' action='db_events.php' method='post'>\n";
  381. $retval .= "<input name='{$mode}_item' type='hidden' value='1' />\n";
  382. $retval .= $original_data;
  383. $retval .= Url::getHiddenInputs($db, $table) . "\n";
  384. $retval .= "<fieldset>\n";
  385. $retval .= "<legend>" . __('Details') . "</legend>\n";
  386. $retval .= "<table class='rte_table' style='width: 100%'>\n";
  387. $retval .= "<tr>\n";
  388. $retval .= " <td style='width: 20%;'>" . __('Event name') . "</td>\n";
  389. $retval .= " <td><input type='text' name='item_name' \n";
  390. $retval .= " value='{$item['item_name']}'\n";
  391. $retval .= " maxlength='64' /></td>\n";
  392. $retval .= "</tr>\n";
  393. $retval .= "<tr>\n";
  394. $retval .= " <td>" . __('Status') . "</td>\n";
  395. $retval .= " <td>\n";
  396. $retval .= " <select name='item_status'>\n";
  397. foreach ($event_status['display'] as $key => $value) {
  398. $selected = "";
  399. if (! empty($item['item_status']) && $item['item_status'] == $value) {
  400. $selected = " selected='selected'";
  401. }
  402. $retval .= "<option$selected>$value</option>";
  403. }
  404. $retval .= " </select>\n";
  405. $retval .= " </td>\n";
  406. $retval .= "</tr>\n";
  407. $retval .= "<tr>\n";
  408. $retval .= " <td>" . __('Event type') . "</td>\n";
  409. $retval .= " <td>\n";
  410. if ($response->isAjax()) {
  411. $retval .= " <select name='item_type'>";
  412. foreach ($event_type as $key => $value) {
  413. $selected = "";
  414. if (! empty($item['item_type']) && $item['item_type'] == $value) {
  415. $selected = " selected='selected'";
  416. }
  417. $retval .= "<option$selected>$value</option>";
  418. }
  419. $retval .= " </select>\n";
  420. } else {
  421. $retval .= " <input name='item_type' type='hidden' \n";
  422. $retval .= " value='{$item['item_type']}' />\n";
  423. $retval .= " <div class='floatleft' style='width: 49%; "
  424. . "text-align: center; font-weight: bold;'>\n";
  425. $retval .= " {$item['item_type']}\n";
  426. $retval .= " </div>\n";
  427. $retval .= " <input style='width: 49%;' type='submit'\n";
  428. $retval .= " name='item_changetype'\n";
  429. $retval .= " value='";
  430. $retval .= sprintf(__('Change to %s'), $item['item_type_toggle']);
  431. $retval .= "' />\n";
  432. }
  433. $retval .= " </td>\n";
  434. $retval .= "</tr>\n";
  435. $retval .= "<tr class='onetime_event_row $isonetime_class'>\n";
  436. $retval .= " <td>" . __('Execute at') . "</td>\n";
  437. $retval .= " <td class='nowrap'>\n";
  438. $retval .= " <input type='text' name='item_execute_at'\n";
  439. $retval .= " value='{$item['item_execute_at']}'\n";
  440. $retval .= " class='datetimefield' />\n";
  441. $retval .= " </td>\n";
  442. $retval .= "</tr>\n";
  443. $retval .= "<tr class='recurring_event_row $isrecurring_class'>\n";
  444. $retval .= " <td>" . __('Execute every') . "</td>\n";
  445. $retval .= " <td>\n";
  446. $retval .= " <input style='width: 49%;' type='text'\n";
  447. $retval .= " name='item_interval_value'\n";
  448. $retval .= " value='{$item['item_interval_value']}' />\n";
  449. $retval .= " <select style='width: 49%;' name='item_interval_field'>";
  450. foreach ($event_interval as $key => $value) {
  451. $selected = "";
  452. if (! empty($item['item_interval_field'])
  453. && $item['item_interval_field'] == $value
  454. ) {
  455. $selected = " selected='selected'";
  456. }
  457. $retval .= "<option$selected>$value</option>";
  458. }
  459. $retval .= " </select>\n";
  460. $retval .= " </td>\n";
  461. $retval .= "</tr>\n";
  462. $retval .= "<tr class='recurring_event_row$isrecurring_class'>\n";
  463. $retval .= " <td>" . _pgettext('Start of recurring event', 'Start');
  464. $retval .= " </td>\n";
  465. $retval .= " <td class='nowrap'>\n";
  466. $retval .= " <input type='text'\n name='item_starts'\n";
  467. $retval .= " value='{$item['item_starts']}'\n";
  468. $retval .= " class='datetimefield' />\n";
  469. $retval .= " </td>\n";
  470. $retval .= "</tr>\n";
  471. $retval .= "<tr class='recurring_event_row$isrecurring_class'>\n";
  472. $retval .= " <td>" . _pgettext('End of recurring event', 'End') . "</td>\n";
  473. $retval .= " <td class='nowrap'>\n";
  474. $retval .= " <input type='text' name='item_ends'\n";
  475. $retval .= " value='{$item['item_ends']}'\n";
  476. $retval .= " class='datetimefield' />\n";
  477. $retval .= " </td>\n";
  478. $retval .= "</tr>\n";
  479. $retval .= "<tr>\n";
  480. $retval .= " <td>" . __('Definition') . "</td>\n";
  481. $retval .= " <td><textarea name='item_definition' rows='15' cols='40'>";
  482. $retval .= $item['item_definition'];
  483. $retval .= "</textarea></td>\n";
  484. $retval .= "</tr>\n";
  485. $retval .= "<tr>\n";
  486. $retval .= " <td>" . __('On completion preserve') . "</td>\n";
  487. $retval .= " <td><input type='checkbox'\n";
  488. $retval .= " name='item_preserve'{$item['item_preserve']} /></td>\n";
  489. $retval .= "</tr>\n";
  490. $retval .= "<tr>\n";
  491. $retval .= " <td>" . __('Definer') . "</td>\n";
  492. $retval .= " <td><input type='text' name='item_definer'\n";
  493. $retval .= " value='{$item['item_definer']}' /></td>\n";
  494. $retval .= "</tr>\n";
  495. $retval .= "<tr>\n";
  496. $retval .= " <td>" . __('Comment') . "</td>\n";
  497. $retval .= " <td><input type='text' name='item_comment' maxlength='64'\n";
  498. $retval .= " value='{$item['item_comment']}' /></td>\n";
  499. $retval .= "</tr>\n";
  500. $retval .= "</table>\n";
  501. $retval .= "</fieldset>\n";
  502. if ($response->isAjax()) {
  503. $retval .= "<input type='hidden' name='editor_process_{$mode}'\n";
  504. $retval .= " value='true' />\n";
  505. $retval .= "<input type='hidden' name='ajax_request' value='true' />\n";
  506. } else {
  507. $retval .= "<fieldset class='tblFooters'>\n";
  508. $retval .= " <input type='submit' name='editor_process_{$mode}'\n";
  509. $retval .= " value='" . __('Go') . "' />\n";
  510. $retval .= "</fieldset>\n";
  511. }
  512. $retval .= "</form>\n\n";
  513. $retval .= "<!-- END " . $modeToUpper . " EVENT FORM -->\n\n";
  514. return $retval;
  515. } // end self::getEditorForm()
  516. /**
  517. * Composes the query necessary to create an event from an HTTP request.
  518. *
  519. * @return string The CREATE EVENT query.
  520. */
  521. public static function getQueryFromRequest()
  522. {
  523. global $_REQUEST, $errors, $event_status, $event_type, $event_interval;
  524. $query = 'CREATE ';
  525. if (! empty($_POST['item_definer'])) {
  526. if (mb_strpos($_POST['item_definer'], '@') !== false
  527. ) {
  528. $arr = explode('@', $_POST['item_definer']);
  529. $query .= 'DEFINER=' . Util::backquote($arr[0]);
  530. $query .= '@' . Util::backquote($arr[1]) . ' ';
  531. } else {
  532. $errors[] = __('The definer must be in the "username@hostname" format!');
  533. }
  534. }
  535. $query .= 'EVENT ';
  536. if (! empty($_POST['item_name'])) {
  537. $query .= Util::backquote($_POST['item_name']) . ' ';
  538. } else {
  539. $errors[] = __('You must provide an event name!');
  540. }
  541. $query .= 'ON SCHEDULE ';
  542. if (! empty($_POST['item_type'])
  543. && in_array($_POST['item_type'], $event_type)
  544. ) {
  545. if ($_POST['item_type'] == 'RECURRING') {
  546. if (! empty($_POST['item_interval_value'])
  547. && !empty($_POST['item_interval_field'])
  548. && in_array($_POST['item_interval_field'], $event_interval)
  549. ) {
  550. $query .= 'EVERY ' . intval($_POST['item_interval_value']) . ' ';
  551. $query .= $_POST['item_interval_field'] . ' ';
  552. } else {
  553. $errors[]
  554. = __('You must provide a valid interval value for the event.');
  555. }
  556. if (! empty($_POST['item_starts'])) {
  557. $query .= "STARTS '"
  558. . $GLOBALS['dbi']->escapeString($_POST['item_starts'])
  559. . "' ";
  560. }
  561. if (! empty($_POST['item_ends'])) {
  562. $query .= "ENDS '"
  563. . $GLOBALS['dbi']->escapeString($_POST['item_ends'])
  564. . "' ";
  565. }
  566. } else {
  567. if (! empty($_POST['item_execute_at'])) {
  568. $query .= "AT '"
  569. . $GLOBALS['dbi']->escapeString($_POST['item_execute_at'])
  570. . "' ";
  571. } else {
  572. $errors[]
  573. = __('You must provide a valid execution time for the event.');
  574. }
  575. }
  576. } else {
  577. $errors[] = __('You must provide a valid type for the event.');
  578. }
  579. $query .= 'ON COMPLETION ';
  580. if (empty($_POST['item_preserve'])) {
  581. $query .= 'NOT ';
  582. }
  583. $query .= 'PRESERVE ';
  584. if (! empty($_POST['item_status'])) {
  585. foreach ($event_status['display'] as $key => $value) {
  586. if ($value == $_POST['item_status']) {
  587. $query .= $event_status['query'][$key] . ' ';
  588. break;
  589. }
  590. }
  591. }
  592. if (! empty($_POST['item_comment'])) {
  593. $query .= "COMMENT '" . $GLOBALS['dbi']->escapeString(
  594. $_POST['item_comment']
  595. ) . "' ";
  596. }
  597. $query .= 'DO ';
  598. if (! empty($_POST['item_definition'])) {
  599. $query .= $_POST['item_definition'];
  600. } else {
  601. $errors[] = __('You must provide an event definition.');
  602. }
  603. return $query;
  604. } // end self::getQueryFromRequest()
  605. }