handle recurring entity delete
[civicrm-core.git] / CRM / Core / BAO / Job.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
6a488035 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26*/
27
28/**
29 *
30 * @package CRM
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
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 /**
42 * class constructor
43 */
44 function __construct() {
45 parent::__construct();
46 }
77b97be7 47
6a488035
TO
48 /**
49 * Function to add the payment-processor type in the db
50 *
51 * @param array $params (reference ) an assoc array of name/value pairs
77b97be7
EM
52 *
53 * @internal param array $ids the array that holds all the db ids
6a488035
TO
54 *
55 * @return object CRM_Financial_DAO_PaymentProcessorType
56 * @access public
57 * @static
6a488035
TO
58 */
59 static function create($params) {
60 $job = new CRM_Core_DAO_Job();
61 $job->copyValues($params);
62 return $job->save();
63 }
64
65 /**
66 * Takes a bunch of params that are needed to match certain criteria and
67 * retrieves the relevant objects. It also stores all the retrieved
68 * values in the default array
69 *
70 * @param array $params (reference ) an assoc array of name/value pairs
71 * @param array $defaults (reference ) an assoc array to hold the flattened values
72 *
73 * @return object CRM_Core_DAO_Job object on success, null otherwise
74 * @access public
75 * @static
76 */
77 static function retrieve(&$params, &$defaults) {
78 $job = new CRM_Core_DAO_Job();
79 $job->copyValues($params);
80 if ($job->find(TRUE)) {
81 CRM_Core_DAO::storeValues($job, $defaults);
82 return $job;
83 }
84 return NULL;
85 }
86
87 /**
88 * update the is_active flag in the db
89 *
90 * @param int $id id of the database record
91 * @param boolean $is_active value we want to set the is_active field
92 *
93 * @return Object DAO object on sucess, null otherwise
94 *
95 * @access public
96 * @static
97 */
98 static function setIsActive($id, $is_active) {
99 return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Job', $id, 'is_active', $is_active);
100 }
101
102 /**
103 * Function to delete scheduled job
104 *
77b97be7
EM
105 * @param $jobID
106 *
107 * @internal param int $jobId ID of the job to be deleted.
6a488035 108 *
77b97be7 109 * @return bool|null
6a488035
TO
110 * @access public
111 * @static
112 */
113 static function del($jobID) {
114 if (!$jobID) {
115 CRM_Core_Error::fatal(ts('Invalid value passed to delete function'));
116 }
117
118 $dao = new CRM_Core_DAO_Job();
119 $dao->id = $jobID;
120 if (!$dao->find(TRUE)) {
121 return NULL;
122 }
123
124 if ($dao->delete()) {
125 return TRUE;
126 }
127 }
128
129 /**
130 * Trim job table on a regular basis to keep it at a good size
131 *
132 * CRM-10513
133 */
134 static function cleanup($maxEntriesToKeep = 1000, $minDaysToKeep = 30) {
135 // Prevent the job log from getting too big
136 // For now, keep last minDays days and at least maxEntries records
137 $query = 'SELECT COUNT(*) FROM civicrm_job_log';
138 $count = CRM_Core_DAO::singleValueQuery($query);
139
140 if ( $count <= $maxEntriesToKeep) {
141 return;
142 }
143
144 $count = $count - $maxEntriesToKeep;
145
146 $query = "DELETE FROM civicrm_job_log WHERE run_time < SUBDATE(NOW(), $minDaysToKeep) LIMIT $count";
147 CRM_Core_DAO::executeQuery($query);
148 }
149
150}