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 TO |
87 | * @param $jobID |
88 | * ID of the job to be deleted. | |
6a488035 | 89 | * |
77b97be7 | 90 | * @return bool|null |
ac15829d | 91 | * @throws CRM_Core_Exception |
6a488035 | 92 | */ |
00be9182 | 93 | public static function del($jobID) { |
6a488035 | 94 | if (!$jobID) { |
ac15829d | 95 | throw new CRM_Core_Exception(ts('Invalid value passed to delete function.')); |
6a488035 TO |
96 | } |
97 | ||
98 | $dao = new CRM_Core_DAO_Job(); | |
99 | $dao->id = $jobID; | |
100 | if (!$dao->find(TRUE)) { | |
101 | return NULL; | |
102 | } | |
103 | ||
104 | if ($dao->delete()) { | |
105 | return TRUE; | |
106 | } | |
107 | } | |
108 | ||
109 | /** | |
fe482240 | 110 | * Trim job table on a regular basis to keep it at a good size. |
6a488035 | 111 | * |
0e480632 | 112 | * @see https://issues.civicrm.org/jira/browse/CRM-10513 |
ad37ac8e | 113 | * |
114 | * @param int $maxEntriesToKeep | |
115 | * @param int $minDaysToKeep | |
6a488035 | 116 | */ |
00be9182 | 117 | public static function cleanup($maxEntriesToKeep = 1000, $minDaysToKeep = 30) { |
6a488035 TO |
118 | // Prevent the job log from getting too big |
119 | // For now, keep last minDays days and at least maxEntries records | |
120 | $query = 'SELECT COUNT(*) FROM civicrm_job_log'; | |
a441f4a4 | 121 | $count = (int) CRM_Core_DAO::singleValueQuery($query); |
6a488035 | 122 | |
481a74f4 | 123 | if ($count <= $maxEntriesToKeep) { |
6a488035 TO |
124 | return; |
125 | } | |
126 | ||
a441f4a4 | 127 | $count = $count - (int) $maxEntriesToKeep; |
6a488035 | 128 | |
a441f4a4 RLAR |
129 | $minDaysToKeep = (int) $minDaysToKeep; |
130 | $query = "DELETE FROM civicrm_job_log WHERE run_time < SUBDATE(NOW(), $minDaysToKeep) ORDER BY id LIMIT $count"; | |
6a488035 TO |
131 | CRM_Core_DAO::executeQuery($query); |
132 | } | |
133 | ||
7a6059c2 MW |
134 | /** |
135 | * Make a copy of a Job. | |
136 | * | |
137 | * @param int $id The job id to copy. | |
518fa0ee | 138 | * @param array $params |
7a6059c2 MW |
139 | * @return CRM_Core_DAO |
140 | */ | |
be2fb01f CW |
141 | public static function copy($id, $params = []) { |
142 | $fieldsFix = [ | |
143 | 'suffix' => [ | |
7a6059c2 | 144 | 'name' => ' - ' . ts('Copy'), |
be2fb01f | 145 | ], |
7a6059c2 | 146 | 'replace' => $params, |
be2fb01f | 147 | ]; |
3fec1adc | 148 | $copy = CRM_Core_DAO::copyGeneric('CRM_Core_DAO_Job', ['id' => $id], NULL, $fieldsFix); |
7a6059c2 MW |
149 | $copy->save(); |
150 | CRM_Utils_Hook::copy('Job', $copy); | |
151 | ||
152 | return $copy; | |
153 | } | |
154 | ||
6a488035 | 155 | } |