Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CRM | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | * $Id: $ |
17 | * | |
18 | */ | |
19 | ||
20 | /** | |
21 | * This class contains scheduled jobs related functions. | |
22 | */ | |
23 | class CRM_Core_BAO_Job extends CRM_Core_DAO_Job { | |
24 | ||
25 | /** | |
fe482240 | 26 | * Class constructor. |
6a488035 | 27 | */ |
00be9182 | 28 | public function __construct() { |
6a488035 TO |
29 | parent::__construct(); |
30 | } | |
77b97be7 | 31 | |
6a488035 | 32 | /** |
100fef9d | 33 | * Add the payment-processor type in the db |
6a488035 | 34 | * |
6a0b768e TO |
35 | * @param array $params |
36 | * An assoc array of name/value pairs. | |
77b97be7 | 37 | * |
c490a46a | 38 | * @return CRM_Financial_DAO_PaymentProcessorType |
6a488035 | 39 | */ |
00be9182 | 40 | public static function create($params) { |
6a488035 TO |
41 | $job = new CRM_Core_DAO_Job(); |
42 | $job->copyValues($params); | |
43 | return $job->save(); | |
44 | } | |
45 | ||
46 | /** | |
fe482240 EM |
47 | * Retrieve DB object based on input parameters. |
48 | * | |
49 | * It also stores all the retrieved values in the default array. | |
6a488035 | 50 | * |
6a0b768e TO |
51 | * @param array $params |
52 | * (reference ) an assoc array of name/value pairs. | |
53 | * @param array $defaults | |
54 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 55 | * |
16b10e64 CW |
56 | * @return CRM_Core_DAO_Job|null |
57 | * object on success, null otherwise | |
6a488035 | 58 | */ |
00be9182 | 59 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
60 | $job = new CRM_Core_DAO_Job(); |
61 | $job->copyValues($params); | |
62 | if ($job->find(TRUE)) { | |
63 | CRM_Core_DAO::storeValues($job, $defaults); | |
64 | return $job; | |
65 | } | |
66 | return NULL; | |
67 | } | |
68 | ||
69 | /** | |
fe482240 | 70 | * Update the is_active flag in the db. |
6a488035 | 71 | * |
6a0b768e TO |
72 | * @param int $id |
73 | * Id of the database record. | |
74 | * @param bool $is_active | |
75 | * Value we want to set the is_active field. | |
6a488035 | 76 | * |
8a4fede3 | 77 | * @return bool |
78 | * true if we found and updated the object, else false | |
6a488035 | 79 | */ |
00be9182 | 80 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
81 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Job', $id, 'is_active', $is_active); |
82 | } | |
83 | ||
84 | /** | |
fe482240 | 85 | * Function to delete scheduled job. |
6a488035 | 86 | * |
6a0b768e | 87 | * @param $jobID |
6a488035 | 88 | * |
77b97be7 | 89 | * @return bool|null |
67d870c5 | 90 | * @deprecated |
ac15829d | 91 | * @throws CRM_Core_Exception |
6a488035 | 92 | */ |
00be9182 | 93 | public static function del($jobID) { |
67d870c5 CW |
94 | self::deleteRecord(['id' => $jobID]); |
95 | return TRUE; | |
6a488035 TO |
96 | } |
97 | ||
98 | /** | |
fe482240 | 99 | * Trim job table on a regular basis to keep it at a good size. |
6a488035 | 100 | * |
0e480632 | 101 | * @see https://issues.civicrm.org/jira/browse/CRM-10513 |
ad37ac8e | 102 | * |
103 | * @param int $maxEntriesToKeep | |
104 | * @param int $minDaysToKeep | |
6a488035 | 105 | */ |
00be9182 | 106 | public static function cleanup($maxEntriesToKeep = 1000, $minDaysToKeep = 30) { |
6a488035 TO |
107 | // Prevent the job log from getting too big |
108 | // For now, keep last minDays days and at least maxEntries records | |
109 | $query = 'SELECT COUNT(*) FROM civicrm_job_log'; | |
a441f4a4 | 110 | $count = (int) CRM_Core_DAO::singleValueQuery($query); |
6a488035 | 111 | |
481a74f4 | 112 | if ($count <= $maxEntriesToKeep) { |
6a488035 TO |
113 | return; |
114 | } | |
115 | ||
a441f4a4 | 116 | $count = $count - (int) $maxEntriesToKeep; |
6a488035 | 117 | |
a441f4a4 RLAR |
118 | $minDaysToKeep = (int) $minDaysToKeep; |
119 | $query = "DELETE FROM civicrm_job_log WHERE run_time < SUBDATE(NOW(), $minDaysToKeep) ORDER BY id LIMIT $count"; | |
6a488035 TO |
120 | CRM_Core_DAO::executeQuery($query); |
121 | } | |
122 | ||
7a6059c2 MW |
123 | /** |
124 | * Make a copy of a Job. | |
125 | * | |
126 | * @param int $id The job id to copy. | |
518fa0ee | 127 | * @param array $params |
7a6059c2 MW |
128 | * @return CRM_Core_DAO |
129 | */ | |
be2fb01f CW |
130 | public static function copy($id, $params = []) { |
131 | $fieldsFix = [ | |
132 | 'suffix' => [ | |
7a6059c2 | 133 | 'name' => ' - ' . ts('Copy'), |
be2fb01f | 134 | ], |
7a6059c2 | 135 | 'replace' => $params, |
be2fb01f | 136 | ]; |
3fec1adc | 137 | $copy = CRM_Core_DAO::copyGeneric('CRM_Core_DAO_Job', ['id' => $id], NULL, $fieldsFix); |
7a6059c2 MW |
138 | $copy->save(); |
139 | CRM_Utils_Hook::copy('Job', $copy); | |
140 | ||
141 | return $copy; | |
142 | } | |
143 | ||
6a488035 | 144 | } |