| 1 | <?php |
| 2 | /* |
| 3 | +--------------------------------------------------------------------+ |
| 4 | | CiviCRM version 4.4 | |
| 5 | +--------------------------------------------------------------------+ |
| 6 | | Copyright CiviCRM LLC (c) 2004-2013 | |
| 7 | +--------------------------------------------------------------------+ |
| 8 | | This file is a part of CiviCRM. | |
| 9 | | | |
| 10 | | CiviCRM is free software; you can copy, modify, and distribute it | |
| 11 | | under the terms of the GNU Affero General Public License | |
| 12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | |
| 13 | | | |
| 14 | | CiviCRM is distributed in the hope that it will be useful, but | |
| 15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
| 17 | | See the GNU Affero General Public License for more details. | |
| 18 | | | |
| 19 | | You should have received a copy of the GNU Affero General Public | |
| 20 | | License and the CiviCRM Licensing Exception along | |
| 21 | | with this program; if not, contact CiviCRM LLC | |
| 22 | | at info[AT]civicrm[DOT]org. If you have questions about the | |
| 23 | | GNU Affero General Public License or the licensing of CiviCRM, | |
| 24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | |
| 25 | +--------------------------------------------------------------------+ |
| 26 | */ |
| 27 | |
| 28 | /** |
| 29 | * This interface defines methods that need to be implemented |
| 30 | * by every scheduled job (cron task) in CiviCRM. |
| 31 | * |
| 32 | * @package CRM |
| 33 | * @copyright CiviCRM LLC (c) 2004-2013 |
| 34 | * $Id$ |
| 35 | * |
| 36 | */ |
| 37 | class CRM_Core_ScheduledJob { |
| 38 | |
| 39 | var $version = 3; |
| 40 | |
| 41 | var $name = NULL; |
| 42 | |
| 43 | var $apiParams = array(); |
| 44 | |
| 45 | var $remarks = array(); |
| 46 | |
| 47 | /* |
| 48 | * Class constructor |
| 49 | * |
| 50 | * @param string $namespace namespace prefix for component's files |
| 51 | * @access public |
| 52 | * |
| 53 | */ |
| 54 | |
| 55 | public function __construct($params) { |
| 56 | foreach ($params as $name => $param) { |
| 57 | $this->$name = $param; |
| 58 | } |
| 59 | |
| 60 | // version is set to 3 by default - if different number |
| 61 | // defined in params, it's replaced later on, however, |
| 62 | // it's practically useles, since it seems none of api v2 |
| 63 | // will work properly in cron job setup. It might become |
| 64 | // useful when/if api v4 starts to emerge and will need |
| 65 | // testing in the cron job setup. To permanenty require |
| 66 | // hardcoded api version, it's enough to move below line |
| 67 | // under following if block. |
| 68 | $this->apiParams = array('version' => $this->version); |
| 69 | |
| 70 | if (!empty($this->parameters)) { |
| 71 | $lines = explode("\n", $this->parameters); |
| 72 | |
| 73 | foreach ($lines as $line) { |
| 74 | $pair = explode("=", $line); |
| 75 | if (empty($pair[0]) || empty($pair[1])) { |
| 76 | $this->remarks[] .= 'Malformed parameters!'; |
| 77 | break; |
| 78 | } |
| 79 | $this->apiParams[trim($pair[0])] = trim($pair[1]); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | public function saveLastRun($date = NULL) { |
| 85 | $dao = new CRM_Core_DAO_Job(); |
| 86 | $dao->id = $this->id; |
| 87 | $dao->last_run = ($date == NULL) ? CRM_Utils_Date::currentDBDate() : CRM_Utils_Date::currentDBDate($date); |
| 88 | $dao->save(); |
| 89 | } |
| 90 | |
| 91 | public function needsRunning() { |
| 92 | // run if it was never run |
| 93 | if (empty($this->last_run)) { |
| 94 | return TRUE; |
| 95 | } |
| 96 | |
| 97 | // run_frequency check |
| 98 | switch ($this->run_frequency) { |
| 99 | case 'Always': |
| 100 | return TRUE; |
| 101 | |
| 102 | case 'Hourly': |
| 103 | $now = CRM_Utils_Date::currentDBDate(); |
| 104 | $hourAgo = strtotime('-1 hour', strtotime($now)); |
| 105 | $lastRun = strtotime($this->last_run); |
| 106 | if ($lastRun < $hourAgo) { |
| 107 | return TRUE; |
| 108 | } |
| 109 | |
| 110 | case 'Daily': |
| 111 | $now = CRM_Utils_Date::currentDBDate(); |
| 112 | $dayAgo = strtotime('-1 day', strtotime($now)); |
| 113 | $lastRun = strtotime($this->last_run); |
| 114 | if ($lastRun < $dayAgo) { |
| 115 | return TRUE; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return FALSE; |
| 120 | } |
| 121 | |
| 122 | public function __destruct() {} |
| 123 | } |
| 124 | |