Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | // $Id$ | |
3 | ||
4 | /* | |
5 | +--------------------------------------------------------------------+ | |
6 | | CiviCRM version 4.3 | | |
7 | +--------------------------------------------------------------------+ | |
8 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
9 | +--------------------------------------------------------------------+ | |
10 | | This file is a part of CiviCRM. | | |
11 | | | | |
12 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
13 | | under the terms of the GNU Affero General Public License | | |
14 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
15 | | | | |
16 | | CiviCRM is distributed in the hope that it will be useful, but | | |
17 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
18 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
19 | | See the GNU Affero General Public License for more details. | | |
20 | | | | |
21 | | You should have received a copy of the GNU Affero General Public | | |
22 | | License and the CiviCRM Licensing Exception along | | |
23 | | with this program; if not, contact CiviCRM LLC | | |
24 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
25 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
26 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
27 | +--------------------------------------------------------------------+ | |
28 | */ | |
29 | ||
30 | /** | |
31 | * new version of civicrm apis. See blog post at | |
32 | * http://civicrm.org/node/131 | |
33 | * @todo Write sth | |
34 | * | |
35 | * @package CiviCRM_APIv3 | |
36 | * @subpackage API_Job | |
37 | * @copyright CiviCRM LLC (c) 2004-2013 | |
38 | * $Id: Contact.php 30879 2010-11-22 15:45:55Z shot $ | |
39 | * | |
40 | */ | |
41 | ||
42 | /** | |
43 | * Include common API util functions | |
44 | */ | |
45 | require_once 'api/v3/utils.php'; | |
46 | ||
47 | /** | |
48 | * Adjust metadata for "Create" action | |
49 | * | |
50 | * The metadata is used for setting defaults, documentation & validation | |
51 | * @param array $params array or parameters determined by getfields | |
52 | */ | |
53 | function _civicrm_api3_job_create_spec(&$params) { | |
54 | $params['run_frequency']['api.required'] = 1; | |
55 | $params['name']['api.required'] = 1; | |
56 | $params['api_entity']['api.required'] = 1; | |
57 | $params['api_action']['api.required'] = 1; | |
58 | ||
59 | $params['domain_id']['api.default'] = CRM_Core_Config::domainID(); | |
60 | $params['is_active']['api.default'] = 1; | |
61 | } | |
62 | ||
63 | /** | |
64 | * Function to create scheduled job | |
65 | * | |
66 | * @param array $params Associative array of property name/value pairs to insert in new job. | |
67 | * | |
68 | * @return success or error | |
69 | * {@getfields Job_create} | |
70 | * @access public | |
71 | * {@schema Core/Job.xml} | |
72 | */ | |
73 | function civicrm_api3_job_create($params) { | |
74 | require_once 'CRM/Utils/Rule.php'; | |
75 | ||
76 | if (isset($params['id']) && !CRM_Utils_Rule::integer($params['id'])) { | |
77 | return civicrm_api3_create_error('Invalid value for job ID'); | |
78 | } | |
79 | ||
80 | $dao = CRM_Core_BAO_Job::create($params); | |
81 | ||
82 | $result = array(); | |
83 | _civicrm_api3_object_to_array($dao, $result[$dao->id]); | |
84 | return civicrm_api3_create_success($result, $params, 'job', 'create', $dao); | |
85 | } | |
86 | ||
87 | /** | |
88 | * Retrieve one or more job | |
89 | * @param array input parameters | |
90 | * @return array api result array | |
91 | * {@getfields email_get} | |
92 | * @access public | |
93 | */ | |
94 | function civicrm_api3_job_get($params) { | |
95 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); | |
96 | } | |
97 | ||
98 | /** | |
99 | * Delete a job | |
100 | * | |
101 | * @param int $id | |
102 | * | |
103 | * @return array API Result Array | |
104 | * {@getfields Job_delete} | |
105 | * @static void | |
106 | * @access public | |
107 | */ | |
108 | function civicrm_api3_job_delete($params) { | |
109 | require_once 'CRM/Utils/Rule.php'; | |
110 | if ($params['id'] != NULL && !CRM_Utils_Rule::integer($params['id'])) { | |
111 | return civicrm_api3_create_error('Invalid value for job ID'); | |
112 | } | |
113 | ||
114 | $result = CRM_Core_BAO_Job::del($params['id']); | |
115 | if (!$result) { | |
116 | return civicrm_api3_create_error('Could not delete job'); | |
117 | } | |
118 | return civicrm_api3_create_success($result, $params, 'job', 'delete'); | |
119 | } | |
120 | ||
121 | /** | |
122 | * Dumb wrapper to execute scheduled jobs. Always creates success - errors | |
123 | * and results are handled in the job log. | |
124 | * | |
125 | * @param array $params (reference ) input parameters | |
126 | * | |
127 | * @return array API Result Array | |
128 | * | |
129 | * @static void | |
130 | * @access public | |
131 | * | |
132 | */ | |
133 | function civicrm_api3_job_execute($params) { | |
134 | require_once 'CRM/Core/JobManager.php'; | |
135 | $facility = new CRM_Core_JobManager(); | |
136 | $facility->execute(FALSE); | |
137 | ||
138 | // always creates success - results are handled elsewhere | |
139 | return civicrm_api3_create_success(); | |
140 | } | |
141 | ||
142 | /** | |
143 | * Adjust Metadata for Execute action | |
144 | * | |
145 | * @param array $params array or parameters determined by getfields | |
146 | */ | |
147 | function _civicrm_api3_job_execute_spec(&$params) { | |
148 | } | |
149 | ||
150 | /** | |
151 | * Geocode group of contacts based on given params | |
152 | * | |
153 | * @param array $params (reference ) input parameters | |
154 | * | |
155 | * @return array API Result Array | |
156 | * {@getfields contact_geocode} | |
157 | * | |
158 | * @static void | |
159 | * @access public | |
160 | * | |
161 | * | |
162 | */ | |
163 | function civicrm_api3_job_geocode($params) { | |
164 | ||
165 | require_once 'CRM/Utils/Address/BatchUpdate.php'; | |
166 | $gc = new CRM_Utils_Address_BatchUpdate($params); | |
167 | ||
168 | ||
169 | $result = $gc->run(); | |
170 | ||
171 | if ($result['is_error'] == 0) { | |
172 | return civicrm_api3_create_success($result['messages']); | |
173 | } | |
174 | else { | |
175 | return civicrm_api3_create_error($result['messages']); | |
176 | } | |
177 | } | |
178 | /** | |
179 | * First check on Code documentation | |
180 | */ | |
181 | function _civicrm_api3_job_geocode_spec(&$params) { | |
182 | $params['start'] = array('title' => 'Start Date'); | |
183 | $params['end'] = array('title' => 'End Date'); | |
184 | $params['geocoding'] = array('title' => 'Is this for GeoCoding? (I think this is a 1,0 field?)'); | |
185 | $params['parse'] = array('title' => 'Is this for parsing? (I think this is a 1,0 field?)'); | |
186 | $params['throttle'] = array('title' => 'Throttle? (no idea what you enter in this field)'); | |
187 | } | |
188 | ||
189 | /** | |
190 | * Send the scheduled reminders for all contacts (either for activities or events) | |
191 | * | |
192 | * @param array $params (reference ) input parameters | |
193 | * now - the time to use, in YmdHis format | |
194 | * - makes testing a bit simpler since we can simulate past/future time | |
195 | * | |
196 | * @return boolean true if success, else false | |
197 | * @static void | |
198 | * @access public | |
199 | * | |
200 | */ | |
201 | function civicrm_api3_job_send_reminder($params) { | |
202 | require_once 'CRM/Core/BAO/ActionSchedule.php'; | |
203 | $result = CRM_Core_BAO_ActionSchedule::processQueue(CRM_Utils_Array::value('now', $params)); | |
204 | ||
205 | if ($result['is_error'] == 0) { | |
206 | return civicrm_api3_create_success(); | |
207 | } | |
208 | else { | |
209 | return civicrm_api3_create_error($result['messages']); | |
210 | } | |
211 | } | |
212 | ||
213 | /** | |
214 | * Execute a specific report instance and send the output via email | |
215 | * | |
216 | * @param array $params (reference ) input parameters | |
217 | * sendmail - Boolean - should email be sent?, required | |
218 | * instanceId - Integer - the report instance ID | |
219 | * resetVal - Integer - should we reset form state (always true)? | |
220 | * | |
221 | * @return boolean true if success, else false | |
222 | * @static void | |
223 | * @access public | |
224 | * | |
225 | */ | |
226 | function civicrm_api3_job_mail_report($params) { | |
227 | ||
228 | $result = CRM_Report_Utils_Report::processReport($params); | |
229 | ||
230 | if ($result['is_error'] == 0) { | |
231 | // this should be handling by throwing exceptions but can't remove until we can test that. | |
232 | return civicrm_api3_create_success(); | |
233 | } | |
234 | else { | |
235 | return civicrm_api3_create_error($result['messages']); | |
236 | } | |
237 | } | |
238 | ||
239 | /** | |
240 | * | |
241 | * This method allows to update Email Greetings, Postal Greetings and Addressee for a specific contact type. | |
242 | * IMPORTANT: You must first create valid option value before using via admin interface. | |
243 | * Check option lists for Email Greetings, Postal Greetings and Addressee | |
244 | * | |
245 | * id - Integer - greetings option group | |
246 | * | |
247 | * @return boolean true if success, else false | |
248 | * @static | |
249 | * @access public | |
250 | * | |
251 | */ | |
252 | function civicrm_api3_job_update_greeting($params) { | |
253 | ||
254 | if (isset($params['ct']) && isset($params['gt'])) { | |
255 | $ct = $gt = array(); | |
256 | $ct = explode(',', $params['ct']); | |
257 | $gt = explode(',', $params['gt']); | |
258 | foreach ($ct as $ctKey => $ctValue) { | |
259 | foreach ($gt as $gtKey => $gtValue) { | |
260 | $params['ct'] = trim($ctValue); | |
261 | $params['gt'] = trim($gtValue); | |
262 | $result[] = CRM_Contact_BAO_Contact_Utils::updateGreeting($params); | |
263 | } | |
264 | } | |
265 | } | |
266 | else { | |
267 | $result = CRM_Contact_BAO_Contact_Utils::updateGreeting($params); | |
268 | } | |
269 | ||
270 | foreach ($result as $resultKey => $resultValue) { | |
271 | if ($resultValue['is_error'] == 0) { | |
272 | //really we should rely on the exception mechanism here - but we need to test that before removing this line | |
273 | return civicrm_api3_create_success(); | |
274 | } | |
275 | else { | |
276 | return civicrm_api3_create_error($resultValue['messages']); | |
277 | } | |
278 | } | |
279 | } | |
280 | ||
281 | /** | |
282 | * Adjust Metadata for Get action | |
283 | * | |
284 | * The metadata is used for setting defaults, documentation & validation | |
285 | * @param array $params array or parameters determined by getfields | |
286 | */ | |
287 | function _civicrm_api3_job_update_greeting_spec(&$params) { | |
288 | $params['ct'] = array( | |
289 | 'api.required' => 1, | |
290 | 'title' => 'Contact Type', | |
291 | 'type' => CRM_Utils_Type::T_STRING, | |
292 | ); | |
293 | $params['gt'] = array( | |
294 | 'api.required' => 1, | |
295 | 'title' => 'Greeting Type', | |
296 | 'type' => CRM_Utils_Type::T_STRING, | |
297 | ); | |
298 | } | |
299 | ||
300 | /** | |
301 | * Mass update pledge statuses | |
302 | * | |
303 | * @param array $params (reference ) input parameters | |
304 | * | |
305 | * @return boolean true if success, else false | |
306 | * @static | |
307 | * @access public | |
308 | * | |
309 | */ | |
310 | function civicrm_api3_job_process_pledge($params) { | |
311 | // *** Uncomment the next line if you want automated reminders to be sent | |
312 | // $params['send_reminders'] = true; | |
313 | $result = CRM_Pledge_BAO_Pledge::updatePledgeStatus($params); | |
314 | ||
315 | if ($result['is_error'] == 0) { | |
316 | // experiment: detailed execution log is a result here | |
317 | return civicrm_api3_create_success($result['messages']); | |
318 | } | |
319 | else { | |
320 | return civicrm_api3_create_error($result['error_message']); | |
321 | } | |
322 | } | |
323 | ||
324 | /** | |
325 | * Process mail queue | |
326 | * | |
327 | * @param array $params | |
328 | * | |
329 | * @return array | |
330 | */ | |
331 | function civicrm_api3_job_process_mailing($params) { | |
332 | ||
333 | if (!CRM_Mailing_BAO_Mailing::processQueue()) { | |
334 | return civicrm_api3_create_error('Process Queue failed'); | |
335 | } | |
336 | else { | |
337 | $values = array(); | |
338 | return civicrm_api3_create_success($values, $params, 'mailing', 'process'); | |
339 | } | |
340 | } | |
341 | ||
342 | /** | |
343 | * Process sms queue | |
344 | * | |
345 | * @param array $params | |
346 | * | |
347 | * @return array | |
348 | */ | |
349 | function civicrm_api3_job_process_sms($params) { | |
350 | ||
351 | if (!CRM_Mailing_BAO_Mailing::processQueue('sms')) { | |
352 | return civicrm_api3_create_error('Process Queue failed'); | |
353 | } | |
354 | else { | |
355 | $values = array(); | |
356 | return civicrm_api3_create_success($values, $params, 'mailing', 'process'); | |
357 | } | |
358 | } | |
359 | /** | |
360 | * Job to get mail responses from civimailing | |
361 | */ | |
362 | function civicrm_api3_job_fetch_bounces($params) { | |
363 | require_once 'CRM/Utils/Mail/EmailProcessor.php'; | |
364 | require_once 'CRM/Core/Lock.php'; | |
365 | $lock = new CRM_Core_Lock('EmailProcessor'); | |
366 | if (!$lock->isAcquired()) { | |
367 | return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running'); | |
368 | } | |
369 | if (!CRM_Utils_Mail_EmailProcessor::processBounces()) { | |
370 | return civicrm_api3_create_error('Process Bounces failed'); | |
371 | } | |
372 | // FIXME: processBounces doesn't return true/false on success/failure | |
373 | $values = array(); | |
374 | $lock->release(); | |
375 | return civicrm_api3_create_success($values, $params, 'mailing', 'bounces'); | |
376 | } | |
377 | ||
378 | /** | |
379 | * Job to get mail and create activities | |
380 | */ | |
381 | function civicrm_api3_job_fetch_activities($params) { | |
382 | require_once 'CRM/Utils/Mail/EmailProcessor.php'; | |
383 | require_once 'CRM/Core/Lock.php'; | |
384 | $lock = new CRM_Core_Lock('EmailProcessor'); | |
385 | if (!$lock->isAcquired()) { | |
386 | return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running'); | |
387 | } | |
388 | try { | |
389 | CRM_Utils_Mail_EmailProcessor::processActivities(); | |
390 | $values = array( ); | |
391 | $lock->release(); | |
392 | return civicrm_api3_create_success($values, $params,'mailing','activities'); | |
393 | } catch (Exception $e) { | |
394 | $lock->release(); | |
395 | return civicrm_api3_create_error('Process Activities failed'); | |
396 | } | |
397 | } | |
398 | ||
399 | /** | |
400 | * Process participant statuses | |
401 | * | |
402 | * @param array $params (reference ) input parameters | |
403 | * | |
404 | * @return array (reference ) array of properties, if error an array with an error id and error message | |
405 | * @access public | |
406 | */ | |
407 | function civicrm_api3_job_process_participant($params) { | |
408 | require_once 'CRM/Event/BAO/ParticipantStatusType.php'; | |
409 | $result = CRM_Event_BAO_ParticipantStatusType::process($params); | |
410 | ||
411 | if (!$result['is_error']) { | |
412 | return civicrm_api3_create_success(implode("\r\r", $result['messages'])); | |
413 | } | |
414 | else { | |
415 | return civicrm_api3_create_error('Error while processing participant statuses'); | |
416 | } | |
417 | } | |
418 | ||
419 | ||
420 | /** | |
421 | * This api checks and updates the status of all membership records for a given domain using the calc_membership_status and | |
422 | * update_contact_membership APIs. | |
423 | * | |
424 | * IMPORTANT: | |
425 | * Sending renewal reminders has been migrated from this job to the Scheduled Reminders function as of 4.3. | |
426 | * | |
427 | * @param array $params input parameters NOT USED | |
428 | * | |
429 | * @return boolean true if success, else false | |
430 | * @static void | |
431 | * @access public | |
432 | */ | |
433 | function civicrm_api3_job_process_membership($params) { | |
434 | require_once 'CRM/Member/BAO/Membership.php'; | |
435 | $result = CRM_Member_BAO_Membership::updateAllMembershipStatus(); | |
436 | ||
437 | if ($result['is_error'] == 0) { | |
438 | return civicrm_api3_create_success($result['messages']); | |
439 | } | |
440 | else { | |
441 | return civicrm_api3_create_error($result['messages']); | |
442 | } | |
443 | } | |
444 | ||
445 | /** | |
446 | * This api checks and updates the status of all survey respondants. | |
447 | * | |
448 | * @param array $params (reference ) input parameters | |
449 | * | |
450 | * @return boolean true if success, else false | |
451 | * @static void | |
452 | * @access public | |
453 | */ | |
454 | function civicrm_api3_job_process_respondent($params) { | |
455 | require_once 'CRM/Campaign/BAO/Survey.php'; | |
456 | $result = CRM_Campaign_BAO_Survey::releaseRespondent($params); | |
457 | ||
458 | if ($result['is_error'] == 0) { | |
459 | return civicrm_api3_create_success(); | |
460 | } | |
461 | else { | |
462 | return civicrm_api3_create_error($result['messages']); | |
463 | } | |
464 | } | |
465 | ||
466 | /** | |
467 | * Merges given pair of duplicate contacts. | |
468 | * | |
469 | * @param array $params input parameters | |
470 | * | |
471 | * Allowed @params array keys are: | |
472 | * {int $rgid rule group id} | |
473 | * {int $gid group id} | |
474 | * {string mode helps decide how to behave when there are conflicts. | |
475 | * A 'safe' value skips the merge if there are no conflicts. Does a force merge otherwise.} | |
476 | * {boolean auto_flip wether to let api decide which contact to retain and which to delete.} | |
477 | * | |
478 | * @return array API Result Array | |
479 | * | |
480 | * @static void | |
481 | * @access public | |
482 | */ | |
483 | function civicrm_api3_job_process_batch_merge($params) { | |
484 | $rgid = CRM_Utils_Array::value('rgid', $params); | |
485 | $gid = CRM_Utils_Array::value('gid', $params); | |
486 | ||
487 | $mode = CRM_Utils_Array::value('mode', $params, 'safe'); | |
488 | $autoFlip = CRM_Utils_Array::value('auto_flip', $params, TRUE); | |
489 | ||
490 | require_once 'CRM/Dedupe/Merger.php'; | |
491 | $result = CRM_Dedupe_Merger::batchMerge($rgid, $gid, $mode, $autoFlip); | |
492 | ||
493 | if ($result['is_error'] == 0) { | |
494 | return civicrm_api3_create_success(); | |
495 | } | |
496 | else { | |
497 | return civicrm_api3_create_error($result['messages']); | |
498 | } | |
499 | } | |
500 | ||
501 | /** | |
502 | * Runs handlePaymentCron method in the specified payment processor | |
503 | * | |
504 | * @param array $params input parameters | |
505 | * | |
506 | * Expected @params array keys are: | |
507 | * {string 'processor_name' - the name of the payment processor, eg: Sagepay} | |
508 | * | |
509 | * @access public | |
510 | */ | |
511 | function civicrm_api3_job_run_payment_cron($params) { | |
512 | ||
513 | require_once 'CRM/Core/Payment.php'; | |
514 | ||
515 | // live mode | |
516 | CRM_Core_Payment::handlePaymentMethod( | |
517 | 'PaymentCron', | |
518 | array_merge( | |
519 | $params, | |
520 | array( | |
521 | 'caller' => 'api', | |
522 | ) | |
523 | ) | |
524 | ); | |
525 | ||
526 | // test mode | |
527 | CRM_Core_Payment::handlePaymentMethod( | |
528 | 'PaymentCron', | |
529 | array_merge( | |
530 | $params, | |
531 | array( | |
532 | 'mode' => 'test', | |
533 | ) | |
534 | ) | |
535 | ); | |
536 | } | |
537 | ||
538 | /** | |
539 | * This api cleans up all the old session entries and temp tables. We recommend that sites run this on an hourly basis | |
540 | * | |
541 | * @param array $params (reference ) - sends in various config parameters to decide what needs to be cleaned | |
542 | * | |
543 | * @return boolean true if success, else false | |
544 | * @static void | |
545 | * @access public | |
546 | */ | |
547 | function civicrm_api3_job_cleanup( $params ) { | |
548 | require_once 'CRM/Utils/Array.php'; | |
549 | ||
550 | $session = CRM_Utils_Array::value( 'session' , $params, true ); | |
551 | $tempTable = CRM_Utils_Array::value( 'tempTables', $params, true ); | |
552 | $jobLog = CRM_Utils_Array::value( 'jobLog' , $params, true ); | |
553 | $prevNext = CRM_Utils_Array::value( 'prevNext' , $params, true ); | |
554 | $dbCache = CRM_Utils_Array::value( 'dbCache' , $params, false ); | |
555 | $memCache = CRM_Utils_Array::value( 'memCache' , $params, false ); | |
556 | ||
557 | if ( $session || $tempTable || $prevNext ) { | |
558 | require_once 'CRM/Core/BAO/Cache.php'; | |
559 | CRM_Core_BAO_Cache::cleanup( $session, $tempTable, $prevNext ); | |
560 | } | |
561 | ||
562 | if ( $jobLog ) { | |
563 | CRM_Core_BAO_Job::cleanup( ); | |
564 | } | |
565 | ||
566 | if ( $dbCache ) { | |
567 | CRM_Core_Config::clearDBCache( ); | |
568 | } | |
569 | ||
570 | if ( $memCache ) { | |
571 | CRM_Utils_System::flushCache( ); | |
572 | } | |
573 | } | |
574 | ||
575 | /** | |
576 | * Set expired relationships to disabled. | |
577 | * | |
578 | */ | |
579 | function civicrm_api3_job_disable_expired_relationships($params) { | |
580 | $result = CRM_Contact_BAO_Relationship::disableExpiredRelationships(); | |
581 | if ($result) { | |
582 | return civicrm_api3_create_success(); | |
583 | } | |
584 | else { | |
585 | return civicrm_api3_create_error('Failed to disable all expired relationships.'); | |
586 | } | |
587 | } | |
588 | ||
589 | /** | |
590 | * This api reloads all the smart groups. If the org has a large number of smart groups | |
591 | * it is recommended that they use the limit clause to limit the number of smart groups | |
592 | * evaluated on a per job basis. Might also help to increase the smartGroupCacheTimeout | |
593 | * and use the cache | |
594 | */ | |
595 | function civicrm_api3_job_group_rebuild( $params ) { | |
596 | $limit = CRM_Utils_Array::value( 'limit', $params, 0 ); | |
597 | ||
598 | CRM_Contact_BAO_GroupContactCache::loadAll(null, $limit); | |
599 | } |