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