Merge pull request #17008 from ivan-compucorp/CPS-70-fix-radio-value
[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 * Class constructor.
27 */
28 public function __construct() {
29 parent::__construct();
30 }
31
32 /**
33 * Add the payment-processor type in the db
34 *
35 * @param array $params
36 * An assoc array of name/value pairs.
37 *
38 * @return CRM_Financial_DAO_PaymentProcessorType
39 */
40 public static function create($params) {
41 $job = new CRM_Core_DAO_Job();
42 $job->copyValues($params);
43 return $job->save();
44 }
45
46 /**
47 * Retrieve DB object based on input parameters.
48 *
49 * It also stores all the retrieved values in the default array.
50 *
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.
55 *
56 * @return CRM_Core_DAO_Job|null
57 * object on success, null otherwise
58 */
59 public static function retrieve(&$params, &$defaults) {
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 /**
70 * Update the is_active flag in the db.
71 *
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.
76 *
77 * @return bool
78 * true if we found and updated the object, else false
79 */
80 public static function setIsActive($id, $is_active) {
81 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Job', $id, 'is_active', $is_active);
82 }
83
84 /**
85 * Function to delete scheduled job.
86 *
87 * @param $jobID
88 * ID of the job to be deleted.
89 *
90 * @return bool|null
91 * @throws CRM_Core_Exception
92 */
93 public static function del($jobID) {
94 if (!$jobID) {
95 throw new CRM_Core_Exception(ts('Invalid value passed to delete function.'));
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 /**
110 * Trim job table on a regular basis to keep it at a good size.
111 *
112 * CRM-10513
113 *
114 * @param int $maxEntriesToKeep
115 * @param int $minDaysToKeep
116 */
117 public static function cleanup($maxEntriesToKeep = 1000, $minDaysToKeep = 30) {
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';
121 $count = (int) CRM_Core_DAO::singleValueQuery($query);
122
123 if ($count <= $maxEntriesToKeep) {
124 return;
125 }
126
127 $count = $count - (int) $maxEntriesToKeep;
128
129 $minDaysToKeep = (int) $minDaysToKeep;
130 $query = "DELETE FROM civicrm_job_log WHERE run_time < SUBDATE(NOW(), $minDaysToKeep) ORDER BY id LIMIT $count";
131 CRM_Core_DAO::executeQuery($query);
132 }
133
134 /**
135 * Make a copy of a Job.
136 *
137 * @param int $id The job id to copy.
138 * @param array $params
139 * @return CRM_Core_DAO
140 */
141 public static function copy($id, $params = []) {
142 $fieldsFix = [
143 'suffix' => [
144 'name' => ' - ' . ts('Copy'),
145 ],
146 'replace' => $params,
147 ];
148 $copy = CRM_Core_DAO::copyGeneric('CRM_Core_DAO_Job', ['id' => $id], NULL, $fieldsFix);
149 $copy->save();
150 CRM_Utils_Hook::copy('Job', $copy);
151
152 return $copy;
153 }
154
155 }