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 | */ |
17 | ||
18 | /** | |
19 | * This class contains scheduled jobs related functions. | |
20 | */ | |
21 | class CRM_Core_BAO_Job extends CRM_Core_DAO_Job { | |
22 | ||
6a488035 | 23 | /** |
100fef9d | 24 | * Add the payment-processor type in the db |
6a488035 | 25 | * |
6a0b768e TO |
26 | * @param array $params |
27 | * An assoc array of name/value pairs. | |
77b97be7 | 28 | * |
c490a46a | 29 | * @return CRM_Financial_DAO_PaymentProcessorType |
6a488035 | 30 | */ |
00be9182 | 31 | public static function create($params) { |
6a488035 TO |
32 | $job = new CRM_Core_DAO_Job(); |
33 | $job->copyValues($params); | |
34 | return $job->save(); | |
35 | } | |
36 | ||
37 | /** | |
4f940304 | 38 | * Retrieve DB object and copy to defaults array. |
6a488035 | 39 | * |
6a0b768e | 40 | * @param array $params |
4f940304 | 41 | * Array of criteria values. |
6a0b768e | 42 | * @param array $defaults |
4f940304 CW |
43 | * Array to be populated with found values. |
44 | * | |
45 | * @return self|null | |
46 | * The DAO object, if found. | |
6a488035 | 47 | * |
4f940304 | 48 | * @deprecated |
6a488035 | 49 | */ |
4f940304 CW |
50 | public static function retrieve($params, &$defaults) { |
51 | return self::commonRetrieve(self::class, $params, $defaults); | |
6a488035 TO |
52 | } |
53 | ||
54 | /** | |
fe482240 | 55 | * Update the is_active flag in the db. |
6a488035 | 56 | * |
6a0b768e TO |
57 | * @param int $id |
58 | * Id of the database record. | |
59 | * @param bool $is_active | |
60 | * Value we want to set the is_active field. | |
6a488035 | 61 | * |
8a4fede3 | 62 | * @return bool |
63 | * true if we found and updated the object, else false | |
6a488035 | 64 | */ |
00be9182 | 65 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
66 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Job', $id, 'is_active', $is_active); |
67 | } | |
68 | ||
69 | /** | |
fe482240 | 70 | * Function to delete scheduled job. |
6a488035 | 71 | * |
6a0b768e | 72 | * @param $jobID |
6a488035 | 73 | * |
77b97be7 | 74 | * @return bool|null |
67d870c5 | 75 | * @deprecated |
ac15829d | 76 | * @throws CRM_Core_Exception |
6a488035 | 77 | */ |
00be9182 | 78 | public static function del($jobID) { |
67d870c5 CW |
79 | self::deleteRecord(['id' => $jobID]); |
80 | return TRUE; | |
6a488035 TO |
81 | } |
82 | ||
83 | /** | |
fe482240 | 84 | * Trim job table on a regular basis to keep it at a good size. |
6a488035 | 85 | * |
0e480632 | 86 | * @see https://issues.civicrm.org/jira/browse/CRM-10513 |
ad37ac8e | 87 | * |
88 | * @param int $maxEntriesToKeep | |
89 | * @param int $minDaysToKeep | |
6a488035 | 90 | */ |
00be9182 | 91 | public static function cleanup($maxEntriesToKeep = 1000, $minDaysToKeep = 30) { |
6a488035 TO |
92 | // Prevent the job log from getting too big |
93 | // For now, keep last minDays days and at least maxEntries records | |
94 | $query = 'SELECT COUNT(*) FROM civicrm_job_log'; | |
a441f4a4 | 95 | $count = (int) CRM_Core_DAO::singleValueQuery($query); |
6a488035 | 96 | |
481a74f4 | 97 | if ($count <= $maxEntriesToKeep) { |
6a488035 TO |
98 | return; |
99 | } | |
100 | ||
a441f4a4 | 101 | $count = $count - (int) $maxEntriesToKeep; |
6a488035 | 102 | |
a441f4a4 RLAR |
103 | $minDaysToKeep = (int) $minDaysToKeep; |
104 | $query = "DELETE FROM civicrm_job_log WHERE run_time < SUBDATE(NOW(), $minDaysToKeep) ORDER BY id LIMIT $count"; | |
6a488035 TO |
105 | CRM_Core_DAO::executeQuery($query); |
106 | } | |
107 | ||
7a6059c2 MW |
108 | /** |
109 | * Make a copy of a Job. | |
110 | * | |
111 | * @param int $id The job id to copy. | |
518fa0ee | 112 | * @param array $params |
7a6059c2 MW |
113 | * @return CRM_Core_DAO |
114 | */ | |
be2fb01f CW |
115 | public static function copy($id, $params = []) { |
116 | $fieldsFix = [ | |
117 | 'suffix' => [ | |
7a6059c2 | 118 | 'name' => ' - ' . ts('Copy'), |
be2fb01f | 119 | ], |
7a6059c2 | 120 | 'replace' => $params, |
be2fb01f | 121 | ]; |
3fec1adc | 122 | $copy = CRM_Core_DAO::copyGeneric('CRM_Core_DAO_Job', ['id' => $id], NULL, $fieldsFix); |
7a6059c2 MW |
123 | $copy->save(); |
124 | CRM_Utils_Hook::copy('Job', $copy); | |
125 | ||
126 | return $copy; | |
127 | } | |
128 | ||
6a488035 | 129 | } |