phpDocumentor pond
[ class tree: pond ] [ index: pond ] [ all elements ]

Source for file cronjobs.ctrl.php

Documentation is available at cronjobs.ctrl.php

  1. <?php
  2. /**
  3.  * This file implements the UI controller for Cron table.
  4.  *
  5.  * This file is part of Quam Plures - {@link http://quamplures.net/}
  6.  * See also {@link https://launchpad.net/quam-plures}.
  7.  *
  8.  * @copyright (c) 2009 - 2011 by the Quam Plures developers - {@link http://quamplures.net/}
  9.  * @copyright (c)2003-2009 by Francois PLANQUE - {@link http://fplanque.net/}
  10.  *
  11.  *  {@internal License choice
  12.  *  - If you have received this file as part of a package, please find the license.txt file in
  13.  *    the same folder or the closest folder above for complete license terms.
  14.  *  - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  15.  *    then you must choose one of the following licenses before using the file:
  16.  *    - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  17.  *    - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  18.  *  }}}
  19.  *
  20.  *  {@internal Open Source relicensing agreement:
  21.  *  }}}
  22.  *
  23.  *  {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  24.  * @author fplanque: Francois PLANQUE.
  25.  *
  26.  * @package pond
  27.  */
  28. if!defined('QP_MAIN_INIT') ) die'Please, do not access this page directly.' );
  29.  
  30.  
  31. // Check minimum permission:
  32. $current_User->check_perm'options''view'true );
  33.  
  34. $AdminUI->set_path'tools''cron' );
  35.  
  36. param'action''string''list' );
  37.  
  38. // We want to remember these params from page to page:
  39. param'ctst_pending''integer'0true );
  40. param'ctst_started''integer'0true );
  41. param'ctst_timeout''integer'0true );
  42. param'ctst_error''integer'0true );
  43. param'ctst_finished''integer'0true );
  44. param'results_crontab_order''string''-A'true );
  45. param'results_crontab_page''integer'1true );
  46.  
  47. // Init names and params for "static" available jobs and ask Plugins about their jobs:
  48. if$action == 'new' || $action == 'create' )
  49. {
  50.     // NOTE: keys starting with "plugin_" are reserved for jobs provided by Plugins
  51.     $cron_job_names array(
  52.         'prune_hits_sessions' => T_('Prune old hits & sessions'),
  53.     );
  54.     $cron_job_params array(
  55.         'prune_hits_sessions' => array(
  56.             'ctrl' => 'cron/jobs/_prune_hits_sessions.job.php',
  57.             'params' => NULL ),
  58.     );
  59.  
  60.     // Get additional jobs from Plugins:
  61.     foreach$Plugins->trigger_collect'GetCronJobs' as $plug_ID => $jobs )
  62.     {
  63.         ifis_array$jobs ) )
  64.         {
  65.             $Debuglog->addsprintfT_('GetCronJobs() for plugin #%d did not return array. Ignoring its jobs.')$plug_ID )array('plugins''error') );
  66.             continue;
  67.         }
  68.         foreach$jobs as $job )
  69.         {
  70.             // Validate params from plugin:
  71.             ifisset$job['params') )
  72.             {
  73.                 $job['params'NULL;
  74.             }
  75.             ifis_array$job || isset$job['ctrl']$job['name') )
  76.             {
  77.                 $Debuglog->addsprintfT_('GetCronJobs() for plugin #%d returned an invalid job. Ignoring.')$plug_ID )array('plugins''error') );
  78.                 continue;
  79.             }
  80.             ifisset($job['params']&& is_array($job['params']) )
  81.             {
  82.                 $Debuglog->addsprintfT_('GetCronJobs() for plugin #%d did return invalid job params (not an array). Ignoring.')$plug_ID )array('plugins''error') );
  83.                 continue;
  84.             }
  85.             $ctrl_id 'plugin_'.$plug_ID.'_'.$job['ctrl'];
  86.  
  87.             $cron_job_names[$ctrl_id$job['name'];
  88.             $cron_job_params[$ctrl_idarray(
  89.                 'ctrl' => $ctrl_id,
  90.                 'params' => $job['params'],
  91.             );
  92.         }
  93.     }
  94. }
  95.  
  96. switch$action )
  97. {
  98.     case 'new':
  99.         // Check that we have permission to edit options:
  100.         $current_User->check_perm'options''edit'trueNULL );
  101.         break;
  102.  
  103.     case 'create':
  104.         // Check that we have permission to edit options:
  105.         $current_User->check_perm'options''edit'trueNULL );
  106.  
  107.         // CREATE OBJECT:
  108.         load_class'/cron/model/_cronjob.class.php' );
  109.         $edited_Cronjob new Cronjob();
  110.  
  111.         $cjob_type param'cjob_type''string'true );
  112.         if!isset$cron_job_params[$cjob_type) )
  113.         {
  114.             param_error'cjob_type'T_('Invalid job type') );
  115.         }
  116.  
  117.         // start datetime:
  118.         param_date'cjob_date'T_('Please enter a valid date.')true );
  119.         param_time'cjob_time' );
  120.         $edited_Cronjob->set'start_datetime'form_dateget_param'cjob_date' )get_param'cjob_time' ) ) );
  121.  
  122.         // repeat after:
  123.         $cjob_repeat_after_days param'cjob_repeat_after_days''integer');
  124.         $cjob_repeat_after_hours param'cjob_repeat_after_hours''integer');
  125.         $cjob_repeat_after_minutes param'cjob_repeat_after_minutes''integer');
  126.         $cjob_repeat_after ( ( ($cjob_repeat_after_days*24$cjob_repeat_after_hours )*60 $cjob_repeat_after_minutes)*60// seconds
  127.         if$cjob_repeat_after == )
  128.         {
  129.             $cjob_repeat_after NULL;
  130.         }
  131.         $edited_Cronjob->set'repeat_after'$cjob_repeat_after );
  132.  
  133.         // name:
  134.         $edited_Cronjob->set'name'$cron_job_names[$cjob_type);
  135.  
  136.         // controller:
  137.         $edited_Cronjob->set'controller'$cron_job_params[$cjob_type]['ctrl');
  138.  
  139.         // params:
  140.         $edited_Cronjob->set'params'$cron_job_params[$cjob_type]['params');
  141.  
  142.         ifparam_errors_detected() )
  143.         // No errors
  144.  
  145.             // Save to DB:
  146.             $edited_Cronjob->dbinsert();
  147.  
  148.             $Messages->addT_('New job has been scheduled.')'success' );
  149.  
  150.             $action 'list';
  151.         }
  152.         break;
  153.  
  154.     case 'delete':
  155.         // Make sure we got an ord_ID:
  156.         param'ctsk_ID''integer'true );
  157.  
  158.         // Check that we have permission to edit options:
  159.         $current_User->check_perm'options''edit'trueNULL );
  160.  
  161.         // TODO: prevent deletion of running tasks.
  162.         $DB->begin();
  163.  
  164.         $tsk_status $DB->get_var(
  165.             'SELECT clog_status
  166.                  FROM T_cron__log
  167.                 WHERE clog_ctsk_ID= '.$ctsk_ID,
  168.             00'Check that task is not running' );
  169.  
  170.         if$tsk_status == 'started' )
  171.         {
  172.             $DB->rollback();
  173.             $Messages->add(  sprintfT_('Job #%d is currently running. It cannot be deleted.')$ctsk_ID )'error' );
  174.         }
  175.         else
  176.         {
  177.             // Delete task:
  178.             $DB->query'DELETE
  179.                                          FROM T_cron__task
  180.                                         WHERE ctsk_ID = '.$ctsk_ID );
  181.  
  182.             // Delete log (if exists):
  183.             $DB->query'DELETE
  184.                                          FROM T_cron__log
  185.                                         WHERE clog_ctsk_ID = '.$ctsk_ID );
  186.  
  187.             $DB->commit();
  188.  
  189.             $Messages->add(  sprintfT_('Scheduled job #%d deleted.')$ctsk_ID )'success' );
  190.         }
  191.  
  192.         forget_param'ctsk_ID' );
  193.         $action 'list';
  194.         break;
  195.  
  196.     case 'view':
  197.         $cjob_ID param'cjob_ID''integer'true );
  198.  
  199.         $sql =  'SELECT *
  200.                              FROM T_cron__task
  201.                     LEFT JOIN T_cron__log ON ctsk_ID = clog_ctsk_ID
  202.                             WHERE ctsk_ID = '.$cjob_ID;
  203.         $cjob_row $DB->get_row$sqlOBJECT0'Get cron job and log' );
  204.         ifempty$cjob_row ) )
  205.         {
  206.             $Messages->addsprintfT_('Job #%d does not exist any longer.')$cjob_ID )'error' );
  207.             $action 'list';
  208.         }
  209.         break;
  210.  
  211.     case 'list':
  212.         // Detect timed out tasks:
  213.         $sql "UPDATE T_cron__log
  214.                              SET clog_status = 'timeout'
  215.                          WHERE clog_status = 'started'
  216.                              AND clog_realstart_datetime < ".$DB->quotedate2mysqltime($time_difference $cron_timeout_delay ) );
  217.         $DB->query$sql'Detect cron timeouts.' );
  218.         break;
  219. }
  220.  
  221. // Display <html><head>...</head> section! (Note: should be done early if actions do not redirect)
  222. $AdminUI->disp_html_head();
  223.  
  224. // Display title, menu, messages, etc. (Note: messages MUST be displayed AFTER the actions)
  225. $AdminUI->disp_body_top();
  226.  
  227. // Begin payload block:
  228. $AdminUI->disp_payload_begin();
  229.  
  230. switch$action )
  231. {
  232.     case 'new':
  233.     case 'create':
  234.         // Display VIEW:
  235.         $AdminUI->disp_view'cron/views/_cronjob.form.php' );
  236.         break;
  237.  
  238.     case 'view':
  239.         // Display VIEW:
  240.         $AdminUI->disp_view'cron/views/_cronjob.view.php' )// uses $cjob_row
  241.         break;
  242.  
  243.     default:
  244.         // Display VIEW:
  245.         $AdminUI->disp_view'cron/views/_cronjob_list.view.php' );
  246. }
  247.  
  248. // End payload block:
  249. $AdminUI->disp_payload_end();
  250.  
  251. // Display body bottom, debug info and close </html>:
  252. $AdminUI->disp_global_footer();
  253.  
  254. ?>