Merge pull request #23190 from eileenmcnaughton/pradeep
[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 */
17
18 /**
19 * This class contains scheduled jobs related functions.
20 */
21 class CRM_Core_BAO_Job extends CRM_Core_DAO_Job {
22
23 /**
24 * Add the payment-processor type in the db
25 *
26 * @param array $params
27 * An assoc array of name/value pairs.
28 *
29 * @return CRM_Financial_DAO_PaymentProcessorType
30 */
31 public static function create($params) {
32 $job = new CRM_Core_DAO_Job();
33 $job->copyValues($params);
34 return $job->save();
35 }
36
37 /**
38 * Retrieve DB object and copy to defaults array.
39 *
40 * @param array $params
41 * Array of criteria values.
42 * @param array $defaults
43 * Array to be populated with found values.
44 *
45 * @return self|null
46 * The DAO object, if found.
47 *
48 * @deprecated
49 */
50 public static function retrieve($params, &$defaults) {
51 return self::commonRetrieve(self::class, $params, $defaults);
52 }
53
54 /**
55 * Update the is_active flag in the db.
56 *
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.
61 *
62 * @return bool
63 * true if we found and updated the object, else false
64 */
65 public static function setIsActive($id, $is_active) {
66 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Job', $id, 'is_active', $is_active);
67 }
68
69 /**
70 * Function to delete scheduled job.
71 *
72 * @param $jobID
73 *
74 * @return bool|null
75 * @deprecated
76 * @throws CRM_Core_Exception
77 */
78 public static function del($jobID) {
79 self::deleteRecord(['id' => $jobID]);
80 return TRUE;
81 }
82
83 /**
84 * Trim job table on a regular basis to keep it at a good size.
85 *
86 * @see https://issues.civicrm.org/jira/browse/CRM-10513
87 *
88 * @param int $maxEntriesToKeep
89 * @param int $minDaysToKeep
90 */
91 public static function cleanup($maxEntriesToKeep = 1000, $minDaysToKeep = 30) {
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';
95 $count = (int) CRM_Core_DAO::singleValueQuery($query);
96
97 if ($count <= $maxEntriesToKeep) {
98 return;
99 }
100
101 $count = $count - (int) $maxEntriesToKeep;
102
103 $minDaysToKeep = (int) $minDaysToKeep;
104 $query = "DELETE FROM civicrm_job_log WHERE run_time < SUBDATE(NOW(), $minDaysToKeep) ORDER BY id LIMIT $count";
105 CRM_Core_DAO::executeQuery($query);
106 }
107
108 /**
109 * Make a copy of a Job.
110 *
111 * @param int $id The job id to copy.
112 * @param array $params
113 * @return CRM_Core_DAO
114 */
115 public static function copy($id, $params = []) {
116 $fieldsFix = [
117 'suffix' => [
118 'name' => ' - ' . ts('Copy'),
119 ],
120 'replace' => $params,
121 ];
122 $copy = CRM_Core_DAO::copyGeneric('CRM_Core_DAO_Job', ['id' => $id], NULL, $fieldsFix);
123 $copy->save();
124 CRM_Utils_Hook::copy('Job', $copy);
125
126 return $copy;
127 }
128
129 }