3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2018
37 * This class contains scheduled jobs related functions.
39 class CRM_Core_BAO_Job
extends CRM_Core_DAO_Job
{
44 public function __construct() {
45 parent
::__construct();
49 * Add the payment-processor type in the db
51 * @param array $params
52 * An assoc array of name/value pairs.
54 * @return CRM_Financial_DAO_PaymentProcessorType
56 public static function create($params) {
57 $job = new CRM_Core_DAO_Job();
58 $job->copyValues($params);
63 * Retrieve DB object based on input parameters.
65 * It also stores all the retrieved values in the default array.
67 * @param array $params
68 * (reference ) an assoc array of name/value pairs.
69 * @param array $defaults
70 * (reference ) an assoc array to hold the flattened values.
72 * @return CRM_Core_DAO_Job|null
73 * object on success, null otherwise
75 public static function retrieve(&$params, &$defaults) {
76 $job = new CRM_Core_DAO_Job();
77 $job->copyValues($params);
78 if ($job->find(TRUE)) {
79 CRM_Core_DAO
::storeValues($job, $defaults);
86 * Update the is_active flag in the db.
89 * Id of the database record.
90 * @param bool $is_active
91 * Value we want to set the is_active field.
94 * DAO object on success, null otherwise
97 public static function setIsActive($id, $is_active) {
98 return CRM_Core_DAO
::setFieldValue('CRM_Core_DAO_Job', $id, 'is_active', $is_active);
102 * Function to delete scheduled job.
105 * ID of the job to be deleted.
109 public static function del($jobID) {
111 CRM_Core_Error
::fatal(ts('Invalid value passed to delete function.'));
114 $dao = new CRM_Core_DAO_Job();
116 if (!$dao->find(TRUE)) {
120 if ($dao->delete()) {
126 * Trim job table on a regular basis to keep it at a good size.
130 * @param int $maxEntriesToKeep
131 * @param int $minDaysToKeep
133 public static function cleanup($maxEntriesToKeep = 1000, $minDaysToKeep = 30) {
134 // Prevent the job log from getting too big
135 // For now, keep last minDays days and at least maxEntries records
136 $query = 'SELECT COUNT(*) FROM civicrm_job_log';
137 $count = CRM_Core_DAO
::singleValueQuery($query);
139 if ($count <= $maxEntriesToKeep) {
143 $count = $count - $maxEntriesToKeep;
145 $query = "DELETE FROM civicrm_job_log WHERE run_time < SUBDATE(NOW(), $minDaysToKeep) LIMIT $count";
146 CRM_Core_DAO
::executeQuery($query);
150 * Make a copy of a Job.
152 * @param int $id The job id to copy.
154 * @return CRM_Core_DAO
156 public static function copy($id, $params = array()) {
159 'name' => ' - ' . ts('Copy'),
161 'replace' => $params,
163 $copy = &CRM_Core_DAO
::copyGeneric('CRM_Core_DAO_Job', array('id' => $id), NULL, $fieldsFix);
165 CRM_Utils_Hook
::copy('Job', $copy);