elysia_cron.admin.inc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. <?php
  2. /*******************************************************************************
  3. * INTERFACE
  4. *
  5. * WARN: Below this point the word "context" should be avoided (use channel)
  6. * Disabled should always be referenced as "disabled" (in db is "disable" for
  7. * compatibility with Ctools )
  8. ******************************************************************************/
  9. function elysia_cron_admin_page() {
  10. $aoutput = array();
  11. $aoutput[] = drupal_get_form('elysia_cron_run_form');
  12. $output = '';
  13. elysia_cron_initialize();
  14. global $elysia_cron_settings, $elysia_cron_settings_by_channel, $elysia_cron_current_channel, $cron_completed, $cron_completed_time;
  15. $v = variable_get('elysia_cron_disabled', false);
  16. $output .= '<p>' . t('Global disable') . ': <i>' . ($v ? '<span class="warn">' . t('YES') . '</span>' : 'no') . '</i></p>';
  17. $output .= '<p>' . t('Last channel executed') . ': <i>' . (($c = elysia_cron_last_channel()) ? $c : t('n/a')) . '</i></p>';
  18. if (EC_DRUPAL_VERSION < 7) {
  19. if (_ec_variable_get('elysia_cron_semaphore', 0)) {
  20. $output .= '<p><span class="warn">' . t('Global semaphore active since !date', array('!date' => elysia_cron_date(_ec_variable_get('elysia_cron_semaphore', 0)))) . '</span></p>';
  21. }
  22. }
  23. $running = '';
  24. foreach ($elysia_cron_settings_by_channel as $channel => $data) {
  25. if (elysia_cron_is_channel_running($channel)) {
  26. $running .= $channel . ' ';
  27. }
  28. }
  29. if ($running) {
  30. $output .= '<p>' . t('Running channels') . ': <span class="warn">' . $running . '</span></p>';
  31. }
  32. $output .= '<p>' . t('Last run') . ': ' . elysia_cron_date(_ec_variable_get('elysia_cron_last_run', 0)) . '</p>';
  33. $rows = array();
  34. $ipath = url(drupal_get_path('module', 'elysia_cron') . '/images/icon_');
  35. foreach ($elysia_cron_settings_by_channel as $channel => $data) {
  36. $running = elysia_cron_is_channel_running($channel);
  37. $rows[] = array(
  38. array('data' => '<h3>' . t('Channel') . ': ' . $channel . ($data['#data']['disabled'] ? ' <span class="warn">(' . t('DISABLED') . ')</span>' : '') . '</h3>', 'colspan' => 2, 'header' => 'true'),
  39. array('data' => elysia_cron_date($data['#data']['last_run']), 'header' => 'true'),
  40. array('data' => $data['#data']['last_execution_time'] . 's ' . t('(Shutdown: !shutdown)', array('!shutdown' => $data['#data']['last_shutdown_time'] . 's')), 'header' => 'true'),
  41. array('data' => $data['#data']['execution_count'], 'header' => 'true'),
  42. array('data' => $data['#data']['avg_execution_time'] . 's / ' . $data['#data']['max_execution_time'] . 's', 'header' => 'true'),
  43. );
  44. $messages = '';
  45. if ($running) {
  46. $messages .= t('Running since !date', array('!date' => elysia_cron_date($running))) . '<br />';
  47. }
  48. if ($data['#data']['last_aborted'] || $data['#data']['abort_count']) {
  49. $msg = array();
  50. if ($data['#data']['last_aborted']) {
  51. $msg[] = t('Last aborted') . (!empty($data['#data']['last_abort_function']) ? ': <span class="warn">' . t('On function !function', array('!function' => $data['#data']['last_abort_function'])) . '</span>' : '');
  52. }
  53. if ($data['#data']['abort_count']) {
  54. $msg[] = t('Abort count') . ': <span class="warn">' . $data['#data']['abort_count'] . '</span>';
  55. }
  56. $messages .= implode(', ', $msg) . '<br />';
  57. }
  58. if ($messages) {
  59. $rows[] = array( '', '', array('data' => $messages, 'colspan' => 4, 'header' => true) );
  60. $rows[] = array( array('data' => '', 'colspan' => 6) );
  61. }
  62. foreach ($data as $job => $conf) {
  63. $icon = 'idle';
  64. $caption = '<b>' . $job . '</b>';
  65. $tip = t('Idle');
  66. if ($conf['disabled']) {
  67. $icon = 'disabled';
  68. $caption = '<strike><b>' . $job . '</b></strike>';
  69. $tip = t('Disabled');
  70. }
  71. elseif (!empty($conf['running'])) {
  72. $icon = 'running';
  73. $caption = '<b><u>' . $job . '</u></b>';
  74. $tip = t('Running');
  75. }
  76. elseif (elysia_cron_should_run($conf)) {
  77. $icon = 'waiting';
  78. $tip = t('Waiting for execution');
  79. }
  80. if ($job != '#data') {
  81. $rows[] = array(
  82. array( 'data' => '<img src="' . $ipath . $icon . '.png" width="16" height="16" align="top" alt="' . $tip . '" title="' . $tip . '" />', 'align' => 'right' ),
  83. array( 'data' => $caption . ': <i>' . elysia_cron_description($job) . '</i> ', 'colspan' => 4 ),
  84. array( 'data' => _dco_l(t('Force run'), _dcf_internal_path('admin/config/system/cron/execute/') . $job, array('attributes' => array('onclick' => 'return confirm("' . t('Force execution of !job?', array('!job' => $job)) . '");'))), 'align' => 'right'),
  85. );
  86. $rows[] = array(
  87. '',
  88. $conf['rule'] . (!empty($conf['weight']) ? ' <small>(' . t('Weight') . ': ' . $conf['weight'] . ')</small>' : ''),
  89. elysia_cron_date($conf['last_run']),
  90. $conf['last_execution_time'] . 's',
  91. $conf['execution_count'],
  92. $conf['avg_execution_time'] . 's / ' . $conf['max_execution_time'] . 's',
  93. );
  94. }
  95. }
  96. $rows[] = array('&nbsp;','','','','','');
  97. }
  98. $output .= _dco_theme('table', array('header' => array('', t('Job / Rule'), t('Last run'), t('Last exec time'), t('Exec count'), t('Avg/Max Exec time')), 'rows' => $rows));
  99. $output .= '<br />';
  100. $output .= _dco_theme('table', array(
  101. 'header' => array(t('Legend')),
  102. 'rows' => array(
  103. array('<img src="' . $ipath . 'idle.png" width="16" height="16" align="top" alt="' . $tip . '" title="' . $tip . '" /> ' . t('Job shouldn\'t do anything right now')),
  104. array('<img src="' . $ipath . 'waiting.png" width="16" height="16" align="top" alt="' . $tip . '" title="' . $tip . '" /> ' . t('Job is ready to be executed, and is waiting for system cron call')),
  105. array('<img src="' . $ipath . 'running.png" width="16" height="16" align="top" alt="' . $tip . '" title="' . $tip . '" /> ' . t('Job is running right now')),
  106. array('<img src="' . $ipath . 'disabled.png" width="16" height="16" align="top" alt="' . $tip . '" title="' . $tip . '" /> ' . t('Job is disabled by settings, and won\'t run until enabled again')),
  107. array(t('Notes: job times don\'t include shutdown times (only shown on channel times).')),
  108. array(t('If an abort occours usually the job is not properly terminated, and so job timings can be inaccurate or wrong.')),
  109. ),
  110. ));
  111. $aoutput[] = array(
  112. '#type' => 'markup',
  113. '#markup' => $output,
  114. );
  115. return _dcr_render_array($aoutput);
  116. }
  117. function elysia_cron_settings_form() {
  118. global $elysia_cron_settings, $elysia_cron_settings_by_channel;
  119. elysia_cron_initialize();
  120. $form = array();
  121. $form['prefix_1'] = array(
  122. '#type' => 'fieldset',
  123. '#title' => t('Click for help and cron rules and script syntax'),
  124. '#collapsible' => true,
  125. '#collapsed' => true,
  126. '#description' => t(<<<EOT
  127. <h3>Fields order</h3>
  128. <pre>
  129. +---------------- minute (0 - 59)
  130. | +------------- hour (0 - 23)
  131. | | +---------- day of month (1 - 31)
  132. | | | +------- month (1 - 12)
  133. | | | | +---- day of week (0 - 6) (Sunday=0)
  134. | | | | |
  135. * * * * *
  136. </pre>
  137. <p>Each of the patterns from the first five fields may be either * (an asterisk),
  138. which matches all legal values, or a list of elements separated by commas (see below).</p>
  139. <p>For "day of the week" (field 5), 0 is considered Sunday, 6 is Saturday
  140. (7 is an illegal value)</p>
  141. <p>A job is executed when the time/date specification fields all match the current
  142. time and date. There is one exception: if both "day of month" and "day of week"
  143. are restricted (not "*"), then either the "day of month" field (3) or the "day of week"
  144. field (5) must match the current day (even though the other of the two fields
  145. need not match the current day).</p>
  146. <h3>Fields operators</h3>
  147. <p>There are several ways of specifying multiple date/time values in a field:</p>
  148. <ul>
  149. <li>The comma (',') operator specifies a list of values, for example: "1,3,4,7,8"</li>
  150. <li>The dash ('-') operator specifies a range of values, for example: "1-6", which is equivalent to "1,2,3,4,5,6"</li>
  151. <li>The asterisk ('*') operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to 'every hour' (subject to matching other specified fields).</li>
  152. <li>The slash ('/') operator (called "step") can be used to skip a given number of values. For example, "*/3" in the hour time field is equivalent to "0,3,6,9,12,15,18,21".</li>
  153. </ul>
  154. <h3>Examples</h3>
  155. <pre>
  156. */15 * * * * : Execute job every 15 minutes
  157. 0 2,14 * * *: Execute job every day at 2:00 and 14:00
  158. 0 2 * * 1-5: Execute job at 2:00 of every working day
  159. 0 12 1 */2 1: Execute job every 2 month, at 12:00 of first day of the month OR at every monday.
  160. </pre>
  161. <h3>Script</h3>
  162. <p>You can use the script section to easily create new jobs (by calling a php function)
  163. or to change the scheduling of an existing job.</p>
  164. <p>Every line of the script can be a comment (if it starts with #) or a job definition.</p>
  165. <p>The syntax of a job definition is:</p>
  166. <code>
  167. &lt;-&gt; [rule] &lt;ch:CHANNEL&gt; [job]
  168. </code>
  169. <p>(Tokens betweens [] are mandatory)</p>
  170. <ul>
  171. <li>&lt;-&gt;: a line starting with "-" means that the job is DISABLED.</li>
  172. <li>[rule]: a crontab schedule rule. See above.</li>
  173. <li>&lt;ch:CHANNEL&gt;: set the channel of the job.</li>
  174. <li>[job]: could be the name of a supported job (for example: 'search_cron') or a function call, ending with ; (for example: 'process_queue();').</li>
  175. </ul>
  176. <p>A comment on the line just preceding a job definition is considered the job description.</p>
  177. <p>Remember that script OVERRIDES all settings on single jobs sections or channel sections of the configuration</p>
  178. <h3>Examples of script</h3>
  179. <pre>
  180. # Search indexing every 2 hours (i'm setting this as the job description)
  181. 0 */2 * * * search_cron
  182. # I'll check for module status only on sunday nights
  183. # (and this is will not be the job description, see the empty line below)
  184. 0 2 * * 0 update_status_cron
  185. # Trackback ping process every 15min and on a channel called "net"
  186. */15 * * * * ch:net trackback_cron
  187. # Disable node_cron (i must set the cron rule even if disabled)
  188. - */15 * * * * node_cron
  189. # Launch function send_summary_mail('test@test.com', false); every night
  190. # And set its description to "Send daily summary"
  191. # Send daily summary
  192. 0 1 * * * send_summary_mail('test@test.com', false);
  193. </pre>
  194. EOT
  195. ),
  196. );
  197. $form['prefix_2'] = array(
  198. '#markup' => '<hr>',
  199. );
  200. $form['main'] = array(
  201. '#title' => t('Main'),
  202. '#type' => 'fieldset',
  203. '#collapsible' => false,
  204. '#collapsed' => false,
  205. );
  206. $form['main']['elysia_cron_disabled'] = array(
  207. '#title' => t('Global disable'),
  208. '#type' => 'checkbox',
  209. '#default_value' => variable_get('elysia_cron_disabled', false),
  210. );
  211. $form['installation'] = array(
  212. '#title' => t('Installation settings'),
  213. '#type' => 'fieldset',
  214. '#collapsible' => true,
  215. '#collapsed' => true,
  216. );
  217. if (EC_DRUPAL_VERSION >= 7) {
  218. $form['installation']['cron_safe_threshold'] = array(
  219. '#type' => 'select',
  220. '#title' => t('Run cron on visitor\'s requests, every'),
  221. '#default_value' => variable_get('cron_safe_threshold', DRUPAL_CRON_DEFAULT_THRESHOLD),
  222. '#description' => t('Setting a time here will enable the "poormanscron" method, which runs the Drupal cron operation using normal browser/page requests instead of having to set up a crontab to request the cron.php script. This approach requires that your site gets regular traffic/visitors in order to trigger the cron request.') . '<br />' .
  223. t('This way is fine if you don\'t need a great control over job starting times and execution frequency.') . '<br />' .
  224. t('If you need a fine grained control over cron timings use the crontab metod, as <a href="!cron_url">described in Drupal installation guide</a>.', array('!cron_url' => url('http://drupal.org/cron'))) . '<br />' .
  225. t('If you have a very large site, or you need to execute some jobs very often (more than once an hour) refer to Elysia cron\'s INSTALL.TXT to improve main cron setup.'),
  226. '#options' => array(0 => t('Never / Use external crontab')) + drupal_map_assoc(array(3600, 10800, 21600, 43200, 86400, 604800), 'format_interval'),
  227. );
  228. }
  229. $form['installation']['cron_key'] = array(
  230. '#title' => t('Cron key'),
  231. '#type' => 'textfield',
  232. '#default_value' => variable_get('cron_key', ''),
  233. '#description' => t('This is used to avoid external cron calling. If you set this cron will by accessible only by calling http://site/cron.php?cron_key=XXX, so you\'ll need to modify system crontab to support this (Logged user with [administer elysia_cron] permission avoid this check).'),
  234. );
  235. $form['installation']['elysia_cron_allowed_hosts'] = array(
  236. '#title' => t('Allowed hosts'),
  237. '#type' => 'textfield',
  238. '#default_value' => variable_get('elysia_cron_allowed_hosts', ''),
  239. '#description' => t('Insert a list of ip addresses separated by , that can run cron.php (Logged user with [administer elysia_cron] permission avoid this check).'),
  240. );
  241. $form['installation']['elysia_cron_default_rule'] = array(
  242. '#title' => t('Default schedule rule'),
  243. '#type' => 'textfield',
  244. '#default_value' => variable_get('elysia_cron_default_rule', false),
  245. '#description' => t('If you don\'t specify a rule for a process, and if it has not a module specified one, this rule will apply'),
  246. );
  247. if (!ini_get('safe_mode')) {
  248. $form['installation']['elysia_cron_time_limit'] = array(
  249. '#title' => t('Time limit'),
  250. '#type' => 'textfield',
  251. '#default_value' => variable_get('elysia_cron_time_limit', 240),
  252. '#description' => t('Set the number of seconds a channel is allowed to run. If you have some jobs that needs more time to execute increase it or set to 0 to disable the limit (WARN: that way a stuck job will block the channel forever!).'),
  253. );
  254. }
  255. $form['installation']['elysia_cron_stuck_time'] = array(
  256. '#title' => t('Stuck time'),
  257. '#type' => 'textfield',
  258. '#default_value' => variable_get('elysia_cron_stuck_time', 3600),
  259. '#description' => t('How many seconds the process should wait to consider the job as stuck (so the channel can run again)'),
  260. );
  261. $form['installation']['elysia_cron_debug_messages'] = array(
  262. '#title' => t('Debug'),
  263. '#type' => 'select',
  264. '#default_value' => variable_get('elysia_cron_debug_messages', 0),
  265. '#options' => array(
  266. 0 => t('Disabled'),
  267. 1 => t('Enabled'),
  268. ),
  269. '#description' => t('Enable extended logging (in watchdog)'),
  270. );
  271. $default_ruless = '';
  272. $default_rules = variable_get('elysia_cron_default_rules', $GLOBALS['elysia_cron_default_rules']);
  273. foreach ($default_rules as $dk => $dr) {
  274. $default_ruless .= $dr . ' = ' . $dk . "\n";
  275. }
  276. $form['installation']['elysia_cron_default_rules'] = array(
  277. '#title' => t('Predefined rules'),
  278. '#type' => 'textarea',
  279. '#rows' => 5,
  280. '#default_value' => $default_ruless,
  281. '#description' => t('You can put here standard rules used in your system, each one with its own caption. Put each rule in a separate line, in the form "caption = rule". For example: <i>"every 15 minutes = */15 * * * *"</i>.'),
  282. );
  283. $form['installation']['elysia_cron_alert_fieldset'] = array(
  284. '#title' => t('External cron tracking'),
  285. '#type' => 'fieldset',
  286. '#collapsible' => true,
  287. '#collapsed' => true,
  288. '#description' => t('This lets you use an external tracking system like <a href="http://www.host-tracker.com/">Host Tracker</a> to be used to monitor the health of cron on your site. Point the tracking service to <a href="!cron-ping-url">!cron-ping-url</a>. If Elysia cron has been called within the time interval specified below, the ping page will return HTTP 200. If not, the ping page will throw a 404 (page not found).', array('!cron-ping-url' => url('admin/build/cron/ping'))),
  289. );
  290. $form['installation']['elysia_cron_alert_fieldset']['elysia_cron_alert_interval'] = array(
  291. '#title' => t('Lapse interval (minutes)'),
  292. '#type' => 'textfield',
  293. '#size' => 20,
  294. '#default_value' => variable_get('elysia_cron_alert_interval', 60),
  295. '#description' => t('Specify the number of minutes to allow to lapse before the cron ping page returns a 404 (page not found).'),
  296. );
  297. $form['elysia_cron_script_fieldset'] = array(
  298. '#title' => t('Script'),
  299. '#type' => 'fieldset',
  300. '#collapsible' => true,
  301. '#collapsed' => !variable_get('elysia_cron_script', ''),
  302. );
  303. $form['elysia_cron_script_fieldset']['elysia_cron_script'] = array(
  304. '#type' => 'textarea',
  305. '#rows' => 20,
  306. '#default_value' => variable_get('elysia_cron_script', ''),
  307. '#description' => t('You can specify new cron jobs or modify existing schedules by adding lines to the script.<br>' .
  308. '<b>Warning</b> All rules specified in the script will OVERRIDE single job settings and channel settings (sections below).'),
  309. );
  310. $form['single_job'] = array(
  311. '#title' => t('Single job settings'),
  312. '#description' =>
  313. '<b>'.t('Disabled').'</b>: '.t('Flag this to disable job execution').'<br />'.
  314. '<b>'.t('Schedule rule').'</b>: '.t('Timing rule for the job. Leave empty to use default rule (shown after the field in parenthesis)').'<br />'.
  315. '<b>'.t('Weight').'</b>: '.t('Use this to specify execution order: low weights are executed before high weights. Default value shown in parenthesis').'<br />'.
  316. '<b>'.t('Channel').'</b>: '.t('Specify a channel for the job (create the channel if not exists)').'<br /><br />',
  317. '#type' => 'fieldset',
  318. '#collapsible' => true,
  319. //'#collapsed' => true,
  320. );
  321. $jobchannels = array(
  322. '#title' => t('Job channel associations'),
  323. '#description' => t('Leave empty for default channel'),
  324. '#type' => 'fieldset',
  325. '#collapsible' => true,
  326. '#collapsed' => true,
  327. );
  328. foreach ($elysia_cron_settings_by_channel as $channel => $cconf) {
  329. foreach ($cconf as $job => $conf) {
  330. if ($job != '#data' && empty($conf['expression'])) {
  331. $form['single_job']['elysia_cron_' . $job] = array(
  332. '#title' => $job, // t('Job !job', array('!job' => $job)),
  333. '#description' => elysia_cron_description($job),
  334. '#type' => 'fieldset',
  335. '#collapsible' => true,
  336. '#collapsed' => !elysia_cron_get_job_rule($job) && !elysia_cron_get_job_weight($job) && !elysia_cron_is_job_disabled($job) && !elysia_cron_get_job_channel($job),
  337. );
  338. //if (!$form['single_job']['elysia_cron_'.$job]['#collapsed'])
  339. // $form['single_job']['#collapsed'] = false;
  340. $rule = elysia_cron_get_job_rule($job);
  341. $options = array_merge(array('default' => t('Default') . ' (' . (!empty($default_rules[$conf['default_rule']]) ? $default_rules[$conf['default_rule']] : $conf['default_rule']) . ')'), $default_rules);
  342. if ($rule && !isset($options[$rule])) {
  343. $options[$rule] = $rule;
  344. }
  345. $options['custom'] = t('Custom') . ' ...';
  346. $form['single_job']['elysia_cron_' . $job]['_elysia_cron_seljob_rule_' . $job] = array(
  347. '#title' => t('Schedule rule'),
  348. '#type' => 'select',
  349. '#options' => $options,
  350. '#default_value' => $rule ? $rule : 'default',
  351. );
  352. $form['single_job']['elysia_cron_' . $job]['_elysia_cron_job_rule_' . $job] = array(
  353. '#title' => t('Schedule rule'),
  354. '#type' => 'textfield',
  355. '#size' => 20,
  356. '#default_value' => $rule ? $rule : $conf['default_rule'],
  357. );
  358. $form['single_job']['elysia_cron_' . $job]['_elysia_cron_job_weight_' . $job] = array(
  359. '#title' => t('Weight'),
  360. '#type' => 'textfield',
  361. '#size' => 4,
  362. '#default_value' => elysia_cron_get_job_weight($job),
  363. '#description' => '(' . $conf['default_weight'] . ')',
  364. );
  365. //$form['single_job']['elysia_cron_'.$job]['elysia_cron_'.$job.'_disabled'] = array(
  366. $form['single_job']['elysia_cron_' . $job]['_elysia_cron_job_disabled_' . $job] = array(
  367. '#title' => t('Disabled'),
  368. '#type' => 'checkbox',
  369. '#default_value' => elysia_cron_is_job_disabled($job, false),
  370. );
  371. //$jobchannels['elysia_cron_'.$job.'_channel'] = array(
  372. $form['single_job']['elysia_cron_' . $job]['_elysia_cron_job_channel_' . $job] = array(
  373. '#title' => t('Channel'), // t('Channel for !job', array('!job' => $job)),
  374. '#type' => 'textfield',
  375. '#size' => 20,
  376. '#default_value' => elysia_cron_get_job_channel($job),
  377. );
  378. //if (elysia_cron_get_job_channel($job))
  379. // $jobchannels['#collapsed'] = false;
  380. }
  381. }
  382. }
  383. $form['channels'] = array(
  384. '#title' => t('Channels settings'),
  385. '#type' => 'fieldset',
  386. '#collapsible' => true,
  387. //'#collapsed' => $jobchannels['#collapsed'],
  388. );
  389. foreach ($elysia_cron_settings_by_channel as $channel => $conf) {
  390. $form['channels']['elysia_cron_ch_' . $channel] = array(
  391. '#title' => $channel, // t('Channel !channel', array('!channel' => $channel)),
  392. '#type' => 'fieldset',
  393. );
  394. $form['channels']['elysia_cron_ch_' . $channel]['_elysia_cron_ch_disabled_' . $channel] = array(
  395. '#title' => t('Disabled'),
  396. '#type' => 'checkbox',
  397. '#default_value' => elysia_cron_is_channel_disabled($channel, ''),
  398. );
  399. $form['channels']['elysia_cron_ch_' . $channel]['_elysia_cron_ch_rule_' . $channel] = array(
  400. '#title' => t('Default schedule rule'),
  401. '#type' => 'textfield',
  402. '#size' => 20,
  403. '#default_value' => elysia_cron_get_channel_rule($channel),
  404. );
  405. //if (elysia_cron_is_channel_disabled($channel))
  406. // $form['channels']['#collapsed'] = false;
  407. }
  408. //$form['channels']['jobchannels'] = $jobchannels;
  409. $form['buttons'] = array('#type' => 'actions');
  410. $form['buttons']['submit'] = array(
  411. '#type' => 'submit',
  412. '#value' => t('Save configuration'),
  413. );
  414. $form['buttons']['reset'] = array(
  415. '#type' => 'submit',
  416. '#value' => t('Reset to defaults'),
  417. );
  418. if (!empty($_POST) && form_get_errors()) {
  419. elysia_cron_error('The settings have not been saved because of the errors.');
  420. }
  421. return _dcr_form($form);
  422. }
  423. function theme_elysia_cron_settings_form($_dco_variables) {
  424. extract(_dcf_theme_form($_dco_variables));
  425. $form = &$variables['form'];
  426. $output = '<script type="text/javascript"><!--' . "\n" .
  427. /*'function _ec_select(editid, select) { if (select.value == \'custom\') {'.
  428. '$ = jQuery; $(select).hide();$("#"+editid).show();$("#"+editid).focus();'.
  429. '}}'.*/
  430. 'function _ec_select(key, select) { if (select.value == \'custom\') {' .
  431. '$ = jQuery; $("#_ec_select_"+key).hide();$("#_ec_custom_"+key).show();$("#_ec_custom_"+key).focus();' .
  432. '}}' .
  433. "\n" . '--></script>';
  434. $coutput = '<table>';
  435. $i = 0;
  436. foreach (element_children($form['single_job']) as $c) {
  437. $key = substr($c, 12);
  438. //print_r($form['single_job'][$c]);
  439. if ($i++ == 0) {
  440. $coutput .= '<tr>' .
  441. '<th>' . $form['single_job'][$c]['_elysia_cron_job_disabled_' . $key]['#title'] . '</th>' .
  442. '<th>' . $form['single_job'][$c]['_elysia_cron_job_rule_' . $key]['#title'] . '</th>' .
  443. '<th colspan="2">' . $form['single_job'][$c]['_elysia_cron_job_weight_' . $key]['#title'] . '</th>' .
  444. '<th>' . $form['single_job'][$c]['_elysia_cron_job_channel_' . $key]['#title'] . '</th>' .
  445. '</tr>';
  446. }
  447. //$def_rule = $form['single_job'][$c]['_elysia_cron_job_rule_'.$key]['#description'];
  448. $def_weight = $form['single_job'][$c]['_elysia_cron_job_weight_' . $key]['#description'];
  449. $posted_key = $form['single_job'][$c]['_elysia_cron_seljob_rule_' . $key]['#name'];
  450. $posted_val = !empty($_REQUEST[$posted_key]) ? $_REQUEST[$posted_key] : false;
  451. $form['single_job'][$c]['_elysia_cron_job_rule_' . $key]['#prefix'] = '<span id="_ec_custom_' . $key . '" style="' . ($posted_val != 'custom' ? 'display: none;' : '') . '">';
  452. $form['single_job'][$c]['_elysia_cron_job_rule_' . $key]['#suffix'] = '</span>';
  453. $form['single_job'][$c]['_elysia_cron_job_rule_' . $key]['#title'] = NULL;
  454. $form['single_job'][$c]['_elysia_cron_job_rule_' . $key]['#description'] = NULL;
  455. //$form['single_job'][$c]['_elysia_cron_job_rule_'.$key]['#attributes']['style'] = ($posted_val != 'custom' ? 'display: none;' : '').'width: 20em; margin: 0';
  456. $form['single_job'][$c]['_elysia_cron_seljob_rule_' . $key]['#prefix'] = '<span id="_ec_select_' . $key . '" style="' . ($posted_val == 'custom' ? 'display: none;' : '') . '">';
  457. $form['single_job'][$c]['_elysia_cron_seljob_rule_' . $key]['#suffix'] = '</span>';
  458. $form['single_job'][$c]['_elysia_cron_seljob_rule_' . $key]['#title'] = NULL;
  459. $form['single_job'][$c]['_elysia_cron_seljob_rule_' . $key]['#description'] = NULL;
  460. //$form['single_job'][$c]['_elysia_cron_seljob_rule_'.$key]['#attributes']['style'] = ($posted_val == 'custom' ? 'display: none;' : '').'width: 20em; margin: 0';
  461. //$form['single_job'][$c]['_elysia_cron_seljob_rule_'.$key]['#attributes']['onchange'] = '_ec_select(\''.$form['single_job'][$c]['_elysia_cron_job_rule_'.$key]['#id'].'\', this)';
  462. $form['single_job'][$c]['_elysia_cron_seljob_rule_' . $key]['#attributes']['onchange'] = '_ec_select(\'' . $key . '\', this)';
  463. $form['single_job'][$c]['_elysia_cron_job_weight_' . $key]['#title'] = NULL;
  464. $form['single_job'][$c]['_elysia_cron_job_weight_' . $key]['#description'] = NULL;
  465. $form['single_job'][$c]['_elysia_cron_job_weight_' . $key]['#attributes']['style'] = 'margin: 0';
  466. $form['single_job'][$c]['_elysia_cron_job_disabled_' . $key]['#title'] = NULL;
  467. $form['single_job'][$c]['_elysia_cron_job_disabled_' . $key]['#attributes']['style'] = 'margin: 0';
  468. $form['single_job'][$c]['_elysia_cron_job_channel_' . $key]['#title'] = NULL;
  469. $form['single_job'][$c]['_elysia_cron_job_channel_' . $key]['#attributes']['style'] = 'margin: 0';
  470. $coutput .= '<tr><td colspan="6"><b>' . $form['single_job'][$c]['#title'] . '</b>' . (($d = $form['single_job'][$c]['#description']) && $d != '-' ? ' <i>(' . $d . ')</i>' : '' ) . '</td></tr>';
  471. $coutput .= '<tr>' .
  472. '<td align="center">' . drupal_render($form['single_job'][$c]['_elysia_cron_job_disabled_' . $key]) . '</td>' .
  473. '<td>' . drupal_render($form['single_job'][$c]['_elysia_cron_seljob_rule_' . $key]) . drupal_render($form['single_job'][$c]['_elysia_cron_job_rule_' . $key]) . '</td>' .//'<td><small>'.$def_rule.'</small></td>'.
  474. '<td>' . drupal_render($form['single_job'][$c]['_elysia_cron_job_weight_' . $key]) . '</td><td><small>' . $def_weight . '</small></td>' .
  475. '<td>' . drupal_render($form['single_job'][$c]['_elysia_cron_job_channel_' . $key]) . '</td>' .
  476. '</tr>';
  477. drupal_render($form['single_job'][$c]);
  478. }
  479. $coutput .= '</table>';
  480. $form['single_job']['#children'] = $coutput;
  481. //$form['single_job'][] = array('#type' => 'markup', '#markup' => $output);
  482. $coutput = '<table>';
  483. $i = 0;
  484. foreach (element_children($form['channels']) as $c) {
  485. $key = substr($c, 15);
  486. if ($i++ == 0) {
  487. $coutput .= '<tr>' .
  488. '<th>' . t('Name') . '</th>' .
  489. '<th>' . $form['channels'][$c]['_elysia_cron_ch_disabled_' . $key]['#title'] . '</th>' .
  490. '<th>' . $form['channels'][$c]['_elysia_cron_ch_rule_' . $key]['#title'] . '</th>' .
  491. '</tr>';
  492. }
  493. $form['channels'][$c]['_elysia_cron_ch_disabled_' . $key]['#title'] = NULL;
  494. $form['channels'][$c]['_elysia_cron_ch_disabled_' . $key]['#attributes']['style'] = 'margin: 0';
  495. $form['channels'][$c]['_elysia_cron_ch_rule_' . $key]['#title'] = NULL;
  496. $form['channels'][$c]['_elysia_cron_ch_rule_' . $key]['#attributes']['style'] = 'margin: 0';
  497. $coutput .= '<tr>' .
  498. '<td><b>' . $form['channels'][$c]['#title'] . '</b></td>' .
  499. '<td>' . drupal_render($form['channels'][$c]['_elysia_cron_ch_disabled_' . $key]) . '</td>' .
  500. '<td>' . drupal_render($form['channels'][$c]['_elysia_cron_ch_rule_' . $key]) . '</td>' .
  501. '</tr>';
  502. drupal_render($form['channels'][$c]);
  503. }
  504. $coutput .= '</table>';
  505. $form['channels']['#children'] = $coutput;
  506. return $output . drupal_render_children($form);
  507. //$form['channels'][] = array('#type' => 'markup', '#markup' => $output);
  508. //return drupal_render(_dcr_form($form));
  509. }
  510. function elysia_cron_settings_form_validate($_dco_form, &$_dco_form_state) {
  511. extract(_dcf_form_validate($_dco_form, $_dco_form_state));
  512. global $elysia_cron_settings;
  513. $script = $form_state['values']['elysia_cron_script'];
  514. if ($script) {
  515. $errors = elysia_cron_decode_script($script, false);
  516. if ($errors) {
  517. form_set_error('elysia_cron_script', t('Invalid lines:') . implode('<br>', $errors));
  518. }
  519. }
  520. foreach ($form_state['values'] as $key => $value) {
  521. if ($value && preg_match('/^_elysia_cron_([^_]+_[^_]+)_(.*)$/', $key, $r) && ($r[1] == 'job_rule' || $r[1] == 'ch_rule')) {
  522. if (!preg_match('/^\\s*([0-9*,\/-]+[ ]+[0-9*,\/-]+[ ]+[0-9*,\/-]+[ ]+[0-9*,\/-]+[ ]+[0-9*,\/-]+)\\s*$/', $value)) {
  523. form_set_error($key, t('Invalid rule: !rule', array('!rule' => $value)));
  524. }
  525. }
  526. }
  527. }
  528. function elysia_cron_settings_form_submit($_dco_form, &$_dco_form_state) {
  529. extract(_dcf_form_validate($_dco_form, $_dco_form_state));
  530. $form_values = $form_state['values'];
  531. $op = isset($form_values['op']) ? $form_values['op'] : '';
  532. // Exclude unnecessary elements.
  533. unset($form_values['submit'], $form_values['reset'], $form_values['form_id'], $form_values['op'], $form_values['form_token']);
  534. $elysia_cron_default_rules = array();
  535. $rules = explode("\n", $form_values['elysia_cron_default_rules']);
  536. foreach ($rules as $r) {
  537. if (trim($r)) {
  538. $rr = explode("=", $r);
  539. $elysia_cron_default_rules[trim($rr[1])] = trim($rr[0]);
  540. }
  541. }
  542. variable_set('elysia_cron_default_rules', $elysia_cron_default_rules);
  543. foreach ($form_values as $key => $value) {
  544. $value = trim($value);
  545. if (!preg_match('/^_elysia_cron_([^_]+_[^_]+)_(.*)$/', $key, $r)) {
  546. if ($op == t('Reset to defaults') || ($key != 'cron_safe_threshold' && !$value)) {
  547. variable_del($key);
  548. }
  549. elseif ($key != 'elysia_cron_default_rules') {
  550. if (is_array($value) && isset($form_values['array_filter'])) {
  551. $value = array_keys(array_filter($value));
  552. }
  553. variable_set($key, $value);
  554. }
  555. }
  556. else {
  557. $nullvalue = $r[1] != 'job_weight' ? !$value : !$value && $value !== '0';
  558. //dprint($r[1].' '.$r[1].' '.$r[2]);
  559. if ($op == t('Reset to defaults') || $nullvalue) {
  560. switch ($r[1]) {
  561. case 'job_channel':
  562. elysia_cron_reset_job_channel($r[2]);
  563. break;
  564. case 'job_rule':
  565. elysia_cron_reset_job_rule($r[2]);
  566. break;
  567. case 'job_weight':
  568. elysia_cron_reset_job_weight($r[2]);
  569. break;
  570. case 'job_disabled':
  571. elysia_cron_reset_job_disabled($r[2]);
  572. break;
  573. case 'ch_disabled':
  574. elysia_cron_reset_channel_disabled($r[2]);
  575. break;
  576. case 'ch_rule':
  577. elysia_cron_reset_channel_rule($r[2]);
  578. break;
  579. }
  580. }
  581. else {
  582. switch ($r[1]) {
  583. case 'job_channel':
  584. elysia_cron_set_job_channel($r[2], $value);
  585. break;
  586. case 'job_rule':
  587. if ($form_values['_elysia_cron_seljob_rule_' . $r[2]] == 'custom') {
  588. elysia_cron_set_job_rule($r[2], $value);
  589. }
  590. break;
  591. case 'seljob_rule':
  592. if ($value != 'custom') {
  593. if ($value == 'default') {
  594. elysia_cron_reset_job_rule($r[2]);
  595. }
  596. else {
  597. elysia_cron_set_job_rule($r[2], $value);
  598. }
  599. }
  600. break;
  601. case 'job_weight':
  602. elysia_cron_set_job_weight($r[2], $value);
  603. break;
  604. case 'job_disabled':
  605. elysia_cron_set_job_disabled($r[2], $value);
  606. break;
  607. case 'ch_disabled':
  608. elysia_cron_set_channel_disabled($r[2], $value);
  609. break;
  610. case 'ch_rule':
  611. elysia_cron_set_channel_rule($r[2], $value);
  612. break;
  613. }
  614. }
  615. }
  616. }
  617. if ($op == t('Reset to defaults')) {
  618. elysia_cron_message('The configuration options have been reset to their default values.');
  619. }
  620. else {
  621. elysia_cron_message('The configuration options have been saved.');
  622. }
  623. }
  624. function elysia_cron_date($timestamp) {
  625. return $timestamp > 0 ? format_date($timestamp, EC_DRUPAL_VERSION >= 7 ? 'short' : 'small') : t('n/a');
  626. //return date(variable_get('date_format_short', 'm/d/Y - H:i'), $timestamp);
  627. }
  628. function elysia_cron_run_form() {
  629. $form = array();
  630. $form['runf'] = array(
  631. '#type' => 'fieldset',
  632. );
  633. $form['runf']['run'] = array(
  634. '#type' => 'submit',
  635. '#value' => t('Run cron'),
  636. );
  637. return $form;
  638. }
  639. function elysia_cron_run_form_submit($_dco_form, &$_dco_form_state) {
  640. // Run cron manually from Cron form.
  641. if (elysia_cron_run()) {
  642. elysia_cron_message('Cron run successfully.');
  643. }
  644. else {
  645. elysia_cron_error('Cron run failed.');
  646. }
  647. drupal_goto(_dcf_internal_path('admin/config/system/cron'));
  648. }
  649. function elysia_cron_execute_page($job = false) {
  650. if (!$job) {
  651. elysia_cron_error('No job specified');
  652. drupal_goto(_dcf_internal_path('admin/config/system/cron'));
  653. }
  654. elysia_cron_run_job($job, true, true, false); // Run also if disabled or not ready (but not if it's already running)
  655. drupal_goto(_dcf_internal_path('admin/config/system/cron'));
  656. }
  657. function elysia_cron_maintenance_page() {
  658. $output = array();
  659. $output[] = drupal_get_form('elysia_cron_reset_statistics_form');
  660. return _dcr_render_array($output);
  661. }
  662. function elysia_cron_reset_statistics_form() {
  663. $form = array();
  664. $form['fieldset'] = array(
  665. '#type' => 'fieldset',
  666. '#title' => t('Reset statistics'),
  667. '#description' => t('Deletes all cron execution statistics (Last run, last exec time, exec count, avg/max exec time...). Do not touch cron settings.<br /><b>This operation could not be reverted</b><br />'),
  668. );
  669. $form['fieldset']['reset'] = array(
  670. '#type' => 'submit',
  671. '#value' => t('Reset'),
  672. '#attributes' => array(
  673. 'onclick' => 'return confirm(\'' . htmlentities(t('Are you sure you want to reset statistics?')) . '\')',
  674. ),
  675. );
  676. return $form;
  677. }
  678. function elysia_cron_reset_statistics_form_submit($_dco_form, &$_dco_form_state) {
  679. elysia_cron_reset_stats();
  680. elysia_cron_message('Reset done.');
  681. drupal_goto(_dcf_internal_path('admin/config/system/cron/maintenance'));
  682. }
  683. function elysia_cron_reset_page() {
  684. elysia_cron_reset_statistics_form_submit(false, $res = array());
  685. }