b2evolution PHP Cross Reference Blogging Systems

Source: /inc/cron/model/_cronjob.class.php - 234 lines - 5845 bytes - Summary - Text - Print

Description: This file implements the Cronjob class, which manages a single cron job as registered in the DB. This file is part of the evoCore framework - {@link http://evocore.net/} See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**
   3   * This file implements the Cronjob class, which manages a single cron job as registered in the DB.
   4   *
   5   * This file is part of the evoCore framework - {@link http://evocore.net/}
   6   * See also {@link http://sourceforge.net/projects/evocms/}.
   7   *
   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}
   9   *
  10   * {@internal License choice
  11   * - If you have received this file as part of a package, please find the license.txt file in
  12   *   the same folder or the closest folder above for complete license terms.
  13   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)
  14   *   then you must choose one of the following licenses before using the file:
  15   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php
  16   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php
  17   * }}
  18   *
  19   * {@internal Open Source relicensing agreement:
  20   * }}
  21   *
  22   * @package evocore
  23   *
  24   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}
  25   * @author fplanque: Francois PLANQUE.
  26   *
  27   * @version $Id: _cronjob.class.php 6136 2014-03-08 07:59:48Z manuel $
  28   */
  29  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  30  
  31  load_class( '_core/model/dataobjects/_dataobject.class.php', 'DataObject' );
  32  
  33  /**
  34   * Cronjob
  35   *
  36   * Manages a single cron job as registered in the DB.
  37   *
  38   * @package evocore
  39   */
  40  class Cronjob extends DataObject
  41  {
  42      var $start_datetime;
  43      var $repeat_after = NULL;
  44      var $name;
  45      var $controller;
  46  
  47      /**
  48       * @var array
  49       */
  50      var $params;
  51  
  52      /**
  53       * Constructor
  54       *
  55       * @param table Database row
  56       */
  57  	function Cronjob( $db_row = NULL )
  58      {
  59          // Call parent constructor:
  60          parent::DataObject( 'T_cron__task', 'ctsk_', 'ctsk_ID', '', '', '', '' );
  61  
  62          if( $db_row != NULL )
  63          {    // Loading an object from DB:
  64              $this->ID              = $db_row->ctsk_ID;
  65              $this->start_datetime  = $db_row->ctsk_start_datetime;
  66              $this->start_timestamp = strtotime( $db_row->ctsk_start_datetime );
  67              $this->repeat_after    = $db_row->ctsk_repeat_after;
  68              $this->name            = $db_row->ctsk_name;
  69              $this->controller      = $db_row->ctsk_controller;
  70              $this->params          = $db_row->ctsk_params;
  71          }
  72          else
  73          {    // New object:
  74              global $localtimenow;
  75              $this->start_timestamp = $localtimenow;
  76          }
  77      }
  78  
  79      /**
  80       * Set param value
  81       *
  82       * By default, all values will be considered strings
  83       *
  84       * @param string parameter name
  85       * @param mixed parameter value
  86       * @param boolean true to set to NULL if empty value
  87       * @return boolean true, if a value has been set; false if it has not changed
  88       */
  89  	function set( $parname, $parvalue, $make_null = false )
  90      {
  91          switch( $parname )
  92          {
  93              case 'params':
  94                  return $this->set_param( 'params', 'string', serialize($parvalue), false );
  95  
  96              case 'name':
  97                  return $this->set_param( $parname, 'string', evo_substr( $parvalue, 0, 255 ), false );
  98          }
  99  
 100          return $this->set_param( $parname, 'string', $parvalue, $make_null );
 101      }
 102  
 103  
 104      /**
 105       * Get a member param by its name
 106       *
 107       * @param mixed Name of parameter
 108       * @return mixed Value of parameter
 109       */
 110  	function get( $parname )
 111      {
 112          switch( $parname )
 113          {
 114              case 'params':
 115                  return unserialize( $this->params );
 116          }
 117  
 118          return parent::get( $parname );
 119      }
 120  
 121  
 122      /**
 123       * Load data from Request form fields.
 124       *
 125       * @return boolean true if loaded data seems valid.
 126       */
 127  	function load_from_Request( $cron_job_names = array(), $cron_job_params = array() )
 128      {
 129          if( $this->ID > 0 || get_param( 'ctsk_ID' ) > 0 )
 130          {    // Update or copy cron job
 131              $cjob_name = param( 'cjob_name', 'string', true );
 132              param_check_not_empty( 'cjob_name', T_('Please enter job name') );
 133          }
 134          else
 135          {    // Create new cron job
 136              $cjob_type = param( 'cjob_type', 'string', true );
 137              if( !isset( $cron_job_params[$cjob_type] ) )
 138              { // This cron job type doesn't exist, so this is an invalid state
 139                  debug_die('Invalid job type received');
 140                  $cjob_name = '';
 141              }
 142              else
 143              {
 144                  $cjob_name = $cron_job_names[$cjob_type];
 145              }
 146          }
 147  
 148          // start datetime:
 149          param_date( 'cjob_date', T_('Please enter a valid date.'), true );
 150          param_time( 'cjob_time' );
 151          $this->set( 'start_datetime', form_date( get_param( 'cjob_date' ), get_param( 'cjob_time' ) ) );
 152  
 153          // repeat after:
 154          $cjob_repeat_after = param_duration( 'cjob_repeat_after' );
 155          if( $cjob_repeat_after == 0 )
 156          {
 157              $cjob_repeat_after = NULL;
 158          }
 159          $this->set( 'repeat_after', $cjob_repeat_after );
 160  
 161          // name:
 162          if( !empty( $cjob_name ) && $cjob_name != $this->get( 'name' ) )
 163          {
 164              $this->set( 'name', $cjob_name );
 165          }
 166  
 167          if( $this->ID == 0 && get_param( 'ctsk_ID' ) == 0 )
 168          {    // Set these params only on creating and copying actions
 169              // controller:
 170              $this->set( 'controller', $cron_job_params[$cjob_type]['ctrl'] );
 171  
 172              // params:
 173              $this->set( 'params', $cron_job_params[$cjob_type]['params'] );
 174          }
 175  
 176          return ! param_errors_detected();
 177      }
 178  
 179  
 180      /**
 181       * Get status
 182       *
 183       * @return string Status
 184       */
 185  	function get_status()
 186      {
 187          global $DB;;
 188  
 189          if( $this->ID > 0 )
 190          {
 191              $SQL = new SQL( 'Get status of scheduled job' );
 192              $SQL->SELECT( 'clog_status' );
 193              $SQL->FROM( 'T_cron__log' );
 194              $SQL->WHERE( 'clog_ctsk_ID = '.$DB->quote( $this->ID ) );
 195              $status = $DB->get_var( $SQL->get() );
 196          }
 197  
 198          if( empty( $status ) )
 199          {    // Set default status for new cron jobs and for cron jobs without log
 200              $status = 'pending';
 201          }
 202  
 203          return $status;
 204      }
 205  
 206  
 207      /**
 208       * Update the DB based on previously recorded changes
 209       *
 210       * @return boolean true
 211       */
 212  	function dbupdate()
 213      {
 214          global $DB;
 215  
 216          $DB->begin();
 217  
 218          if( $this->get_status() == 'pending' )
 219          {    // Update crob jobs only with "pending" status
 220              $result = parent::dbupdate();
 221          }
 222          else
 223          {    // Don't update this cron job
 224              $DB->rollback();
 225              return false;
 226          }
 227  
 228          $DB->commit();
 229  
 230          return $result;
 231      }
 232  }
 233  
 234  ?>

title

Description

title

Description

title

Description

title

title

Body