Merge pull request #14016 from seamuslee001/mailing_group_grant_friend_new_style
[civicrm-core.git] / CRM / Core / BAO / Job.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 * @package CRM
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 * $Id: $
33 *
34 */
35
36/**
37 * This class contains scheduled jobs related functions.
38 */
39class CRM_Core_BAO_Job extends CRM_Core_DAO_Job {
40
41 /**
fe482240 42 * Class constructor.
6a488035 43 */
00be9182 44 public function __construct() {
6a488035
TO
45 parent::__construct();
46 }
77b97be7 47
6a488035 48 /**
100fef9d 49 * Add the payment-processor type in the db
6a488035 50 *
6a0b768e
TO
51 * @param array $params
52 * An assoc array of name/value pairs.
77b97be7 53 *
c490a46a 54 * @return CRM_Financial_DAO_PaymentProcessorType
6a488035 55 */
00be9182 56 public static function create($params) {
6a488035
TO
57 $job = new CRM_Core_DAO_Job();
58 $job->copyValues($params);
59 return $job->save();
60 }
61
62 /**
fe482240
EM
63 * Retrieve DB object based on input parameters.
64 *
65 * It also stores all the retrieved values in the default array.
6a488035 66 *
6a0b768e
TO
67 * @param array $params
68 * (reference ) an assoc array of name/value pairs.
69 * @param array $defaults
70 * (reference ) an assoc array to hold the flattened values.
6a488035 71 *
16b10e64
CW
72 * @return CRM_Core_DAO_Job|null
73 * object on success, null otherwise
6a488035 74 */
00be9182 75 public static function retrieve(&$params, &$defaults) {
6a488035
TO
76 $job = new CRM_Core_DAO_Job();
77 $job->copyValues($params);
78 if ($job->find(TRUE)) {
79 CRM_Core_DAO::storeValues($job, $defaults);
80 return $job;
81 }
82 return NULL;
83 }
84
85 /**
fe482240 86 * Update the is_active flag in the db.
6a488035 87 *
6a0b768e
TO
88 * @param int $id
89 * Id of the database record.
90 * @param bool $is_active
91 * Value we want to set the is_active field.
6a488035 92 *
8a4fede3 93 * @return bool
94 * true if we found and updated the object, else false
6a488035 95 */
00be9182 96 public static function setIsActive($id, $is_active) {
6a488035
TO
97 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Job', $id, 'is_active', $is_active);
98 }
99
100 /**
fe482240 101 * Function to delete scheduled job.
6a488035 102 *
6a0b768e
TO
103 * @param $jobID
104 * ID of the job to be deleted.
6a488035 105 *
77b97be7 106 * @return bool|null
6a488035 107 */
00be9182 108 public static function del($jobID) {
6a488035 109 if (!$jobID) {
1f21f2cf 110 CRM_Core_Error::fatal(ts('Invalid value passed to delete function.'));
6a488035
TO
111 }
112
113 $dao = new CRM_Core_DAO_Job();
114 $dao->id = $jobID;
115 if (!$dao->find(TRUE)) {
116 return NULL;
117 }
118
119 if ($dao->delete()) {
120 return TRUE;
121 }
122 }
123
124 /**
fe482240 125 * Trim job table on a regular basis to keep it at a good size.
6a488035
TO
126 *
127 * CRM-10513
ad37ac8e 128 *
129 * @param int $maxEntriesToKeep
130 * @param int $minDaysToKeep
6a488035 131 */
00be9182 132 public static function cleanup($maxEntriesToKeep = 1000, $minDaysToKeep = 30) {
6a488035
TO
133 // Prevent the job log from getting too big
134 // For now, keep last minDays days and at least maxEntries records
135 $query = 'SELECT COUNT(*) FROM civicrm_job_log';
136 $count = CRM_Core_DAO::singleValueQuery($query);
137
481a74f4 138 if ($count <= $maxEntriesToKeep) {
6a488035
TO
139 return;
140 }
141
142 $count = $count - $maxEntriesToKeep;
143
144 $query = "DELETE FROM civicrm_job_log WHERE run_time < SUBDATE(NOW(), $minDaysToKeep) LIMIT $count";
145 CRM_Core_DAO::executeQuery($query);
146 }
147
7a6059c2
MW
148 /**
149 * Make a copy of a Job.
150 *
151 * @param int $id The job id to copy.
152 *
153 * @return CRM_Core_DAO
154 */
be2fb01f
CW
155 public static function copy($id, $params = []) {
156 $fieldsFix = [
157 'suffix' => [
7a6059c2 158 'name' => ' - ' . ts('Copy'),
be2fb01f 159 ],
7a6059c2 160 'replace' => $params,
be2fb01f
CW
161 ];
162 $copy = &CRM_Core_DAO::copyGeneric('CRM_Core_DAO_Job', ['id' => $id], NULL, $fieldsFix);
7a6059c2
MW
163 $copy->save();
164 CRM_Utils_Hook::copy('Job', $copy);
165
166 return $copy;
167 }
168
6a488035 169}