Merge pull request #20093 from larssandergreen/mailings-AB-test-improvements
[civicrm-core.git] / CRM / Core / BAO / Job.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
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 /**
26 * Add the payment-processor type in the db
27 *
28 * @param array $params
29 * An assoc array of name/value pairs.
30 *
31 * @return CRM_Financial_DAO_PaymentProcessorType
32 */
33 public static function create($params) {
34 $job = new CRM_Core_DAO_Job();
35 $job->copyValues($params);
36 return $job->save();
37 }
38
39 /**
40 * Retrieve DB object and copy to defaults array.
41 *
42 * @param array $params
43 * Array of criteria values.
44 * @param array $defaults
45 * Array to be populated with found values.
46 *
47 * @return self|null
48 * The DAO object, if found.
49 *
50 * @deprecated
51 */
52 public static function retrieve($params, &$defaults) {
53 return self::commonRetrieve(self::class, $params, $defaults);
54 }
55
56 /**
57 * Update the is_active flag in the db.
58 *
59 * @param int $id
60 * Id of the database record.
61 * @param bool $is_active
62 * Value we want to set the is_active field.
63 *
64 * @return bool
65 * true if we found and updated the object, else false
66 */
67 public static function setIsActive($id, $is_active) {
68 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Job', $id, 'is_active', $is_active);
69 }
70
71 /**
72 * Function to delete scheduled job.
73 *
74 * @param $jobID
75 *
76 * @return bool|null
77 * @deprecated
78 * @throws CRM_Core_Exception
79 */
80 public static function del($jobID) {
81 self::deleteRecord(['id' => $jobID]);
82 return TRUE;
83 }
84
85 /**
86 * Trim job table on a regular basis to keep it at a good size.
87 *
88 * @see https://issues.civicrm.org/jira/browse/CRM-10513
89 *
90 * @param int $maxEntriesToKeep
91 * @param int $minDaysToKeep
92 */
93 public static function cleanup($maxEntriesToKeep = 1000, $minDaysToKeep = 30) {
94 // Prevent the job log from getting too big
95 // For now, keep last minDays days and at least maxEntries records
96 $query = 'SELECT COUNT(*) FROM civicrm_job_log';
97 $count = (int) CRM_Core_DAO::singleValueQuery($query);
98
99 if ($count <= $maxEntriesToKeep) {
100 return;
101 }
102
103 $count = $count - (int) $maxEntriesToKeep;
104
105 $minDaysToKeep = (int) $minDaysToKeep;
106 $query = "DELETE FROM civicrm_job_log WHERE run_time < SUBDATE(NOW(), $minDaysToKeep) ORDER BY id LIMIT $count";
107 CRM_Core_DAO::executeQuery($query);
108 }
109
110 /**
111 * Make a copy of a Job.
112 *
113 * @param int $id The job id to copy.
114 * @param array $params
115 * @return CRM_Core_DAO
116 */
117 public static function copy($id, $params = []) {
118 $fieldsFix = [
119 'suffix' => [
120 'name' => ' - ' . ts('Copy'),
121 ],
122 'replace' => $params,
123 ];
124 $copy = CRM_Core_DAO::copyGeneric('CRM_Core_DAO_Job', ['id' => $id], NULL, $fieldsFix);
125 $copy->save();
126 CRM_Utils_Hook::copy('Job', $copy);
127
128 return $copy;
129 }
130
131 }