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