Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
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 | */ | |
39 | class CRM_Core_BAO_Job extends CRM_Core_DAO_Job { | |
40 | ||
41 | /** | |
100fef9d | 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 | * |
c490a46a | 51 | * @param array $params an assoc array of name/value pairs |
77b97be7 | 52 | * |
c490a46a | 53 | * @return CRM_Financial_DAO_PaymentProcessorType |
6a488035 | 54 | * @static |
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 | /** | |
63 | * Takes a bunch of params that are needed to match certain criteria and | |
64 | * retrieves the relevant objects. It also stores all the retrieved | |
65 | * values in the default array | |
66 | * | |
67 | * @param array $params (reference ) an assoc array of name/value pairs | |
68 | * @param array $defaults (reference ) an assoc array to hold the flattened values | |
69 | * | |
c490a46a | 70 | * @return CRM_Core_DAO_Job object on success, null otherwise |
6a488035 TO |
71 | * @static |
72 | */ | |
00be9182 | 73 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
74 | $job = new CRM_Core_DAO_Job(); |
75 | $job->copyValues($params); | |
76 | if ($job->find(TRUE)) { | |
77 | CRM_Core_DAO::storeValues($job, $defaults); | |
78 | return $job; | |
79 | } | |
80 | return NULL; | |
81 | } | |
82 | ||
83 | /** | |
100fef9d | 84 | * Update the is_active flag in the db |
6a488035 TO |
85 | * |
86 | * @param int $id id of the database record | |
87 | * @param boolean $is_active value we want to set the is_active field | |
88 | * | |
89 | * @return Object DAO object on sucess, null otherwise | |
90 | * | |
6a488035 TO |
91 | * @static |
92 | */ | |
00be9182 | 93 | public static function setIsActive($id, $is_active) { |
6a488035 TO |
94 | return CRM_Core_DAO::setFieldValue('CRM_Core_DAO_Job', $id, 'is_active', $is_active); |
95 | } | |
96 | ||
97 | /** | |
98 | * Function to delete scheduled job | |
99 | * | |
c490a46a | 100 | * @param $jobID ID of the job to be deleted. |
6a488035 | 101 | * |
77b97be7 | 102 | * @return bool|null |
6a488035 TO |
103 | * @static |
104 | */ | |
00be9182 | 105 | public static function del($jobID) { |
6a488035 | 106 | if (!$jobID) { |
1f21f2cf | 107 | CRM_Core_Error::fatal(ts('Invalid value passed to delete function.')); |
6a488035 TO |
108 | } |
109 | ||
110 | $dao = new CRM_Core_DAO_Job(); | |
111 | $dao->id = $jobID; | |
112 | if (!$dao->find(TRUE)) { | |
113 | return NULL; | |
114 | } | |
115 | ||
116 | if ($dao->delete()) { | |
117 | return TRUE; | |
118 | } | |
119 | } | |
120 | ||
121 | /** | |
122 | * Trim job table on a regular basis to keep it at a good size | |
123 | * | |
124 | * CRM-10513 | |
125 | */ | |
00be9182 | 126 | public static function cleanup($maxEntriesToKeep = 1000, $minDaysToKeep = 30) { |
6a488035 TO |
127 | // Prevent the job log from getting too big |
128 | // For now, keep last minDays days and at least maxEntries records | |
129 | $query = 'SELECT COUNT(*) FROM civicrm_job_log'; | |
130 | $count = CRM_Core_DAO::singleValueQuery($query); | |
131 | ||
132 | if ( $count <= $maxEntriesToKeep) { | |
133 | return; | |
134 | } | |
135 | ||
136 | $count = $count - $maxEntriesToKeep; | |
137 | ||
138 | $query = "DELETE FROM civicrm_job_log WHERE run_time < SUBDATE(NOW(), $minDaysToKeep) LIMIT $count"; | |
139 | CRM_Core_DAO::executeQuery($query); | |
140 | } | |
141 | ||
142 | } |