Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2015-03-09-21-44-34
[civicrm-core.git] / api / v3 / Job.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
731a0992 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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
244bbdd8 29 * This api is used for working with scheduled "cron" jobs.
6a488035
TO
30 *
31 * @package CiviCRM_APIv3
6a488035
TO
32 */
33
6a488035 34/**
c1a920f1
EM
35 * Adjust metadata for "Create" action.
36 *
37 * The metadata is used for setting defaults, documentation & validation.
6a488035 38 *
cf470720 39 * @param array $params
b081365f 40 * Array of parameters determined by getfields.
6a488035
TO
41 */
42function _civicrm_api3_job_create_spec(&$params) {
43 $params['run_frequency']['api.required'] = 1;
44 $params['name']['api.required'] = 1;
45 $params['api_entity']['api.required'] = 1;
46 $params['api_action']['api.required'] = 1;
47
48 $params['domain_id']['api.default'] = CRM_Core_Config::domainID();
49 $params['is_active']['api.default'] = 1;
50}
51
52/**
c1a920f1 53 * Create scheduled job.
6a488035 54 *
cf470720
TO
55 * @param array $params
56 * Associative array of property name/value pairs to insert in new job.
6a488035 57 *
72b3a70c 58 * @return array
6a488035
TO
59 */
60function civicrm_api3_job_create($params) {
f39bacdf 61 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params);
6a488035
TO
62}
63
64/**
211e2fca 65 * Retrieve one or more job.
971d41b1
CW
66 *
67 * @param array $params
68 * input parameters
971d41b1 69 *
211e2fca 70 * @return array
6a488035
TO
71 */
72function civicrm_api3_job_get($params) {
73 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
74}
75
76/**
211e2fca 77 * Delete a job.
6a488035 78 *
9657ccf2 79 * @param array $params
6a488035
TO
80 */
81function civicrm_api3_job_delete($params) {
61ef23bd 82 _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
6a488035
TO
83}
84
85/**
211e2fca
EM
86 * Dumb wrapper to execute scheduled jobs.
87 *
88 * Always creates success - errors and results are handled in the job log.
6a488035 89 *
cf470720 90 * @param array $params
971d41b1 91 * input parameters (unused).
6a488035 92 *
a6c01b45 93 * @return array
72b3a70c 94 * API Result Array
6a488035 95 */
608e6658 96function civicrm_api3_job_execute($params) {
608e6658 97
6a488035
TO
98 $facility = new CRM_Core_JobManager();
99 $facility->execute(FALSE);
100
61fe4988 101 // Always creates success - results are handled elsewhere.
244bbdd8 102 return civicrm_api3_create_success(1, $params, 'Job');
6a488035
TO
103}
104
105/**
211e2fca 106 * Adjust Metadata for Execute action.
6a488035 107 *
cf470720 108 * @param array $params
b081365f 109 * Array of parameters determined by getfields.
6a488035
TO
110 */
111function _civicrm_api3_job_execute_spec(&$params) {
112}
113
114/**
211e2fca 115 * Geocode group of contacts based on given params.
6a488035 116 *
cf470720 117 * @param array $params
971d41b1 118 * input parameters.
6a488035 119 *
a6c01b45 120 * @return array
72b3a70c 121 * API Result Array
6a488035
TO
122 */
123function civicrm_api3_job_geocode($params) {
6a488035
TO
124 $gc = new CRM_Utils_Address_BatchUpdate($params);
125
6a488035
TO
126 $result = $gc->run();
127
128 if ($result['is_error'] == 0) {
129 return civicrm_api3_create_success($result['messages']);
130 }
131 else {
132 return civicrm_api3_create_error($result['messages']);
133 }
134}
9657ccf2 135
6a488035 136/**
c1a920f1 137 * First check on Code documentation.
9657ccf2
EM
138 *
139 * @param array $params
6a488035
TO
140 */
141function _civicrm_api3_job_geocode_spec(&$params) {
142 $params['start'] = array('title' => 'Start Date');
143 $params['end'] = array('title' => 'End Date');
79f1148d
DL
144 $params['geocoding'] = array('title' => 'Geocode address?');
145 $params['parse'] = array('title' => 'Parse street address?');
3f83a242 146 $params['throttle'] = array('title' => 'Throttle? if enabled, geo-codes at a slow rate');
6a488035
TO
147}
148
149/**
c1a920f1 150 * Send the scheduled reminders for all contacts (either for activities or events).
6a488035 151 *
cf470720
TO
152 * @param array $params
153 * (reference ) input parameters.
6a488035
TO
154 * now - the time to use, in YmdHis format
155 * - makes testing a bit simpler since we can simulate past/future time
156 *
c23f45d3 157 * @return array
6a488035
TO
158 */
159function civicrm_api3_job_send_reminder($params) {
e27a3472
EM
160 //note that $params['rowCount' can be overridden by one of the preferred syntaxes ($options['limit'] = x
161 //It's not clear whether than syntax can be passed in via the UI config - but this keeps the pre 4.4.4 behaviour
c23f45d3 162 // in that case (ie. makes it non-configurable via the UI). Another approach would be to set a default of 0
e27a3472
EM
163 // in the _spec function - but since that is a deprecated value it seems more contentious than this approach
164 $params['rowCount'] = 0;
39fefc30
KJ
165 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
166 if (!$lock->isAcquired()) {
167 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
168 }
169
73f3e293 170 $result = CRM_Core_BAO_ActionSchedule::processQueue(CRM_Utils_Array::value('now', $params), $params);
39fefc30 171 $lock->release();
6a488035
TO
172
173 if ($result['is_error'] == 0) {
174 return civicrm_api3_create_success();
175 }
176 else {
177 return civicrm_api3_create_error($result['messages']);
178 }
179}
73f3e293 180/**
c1a920f1
EM
181 * Adjust metadata for "send_reminder" action.
182 *
183 * The metadata is used for setting defaults, documentation & validation.
73f3e293 184 *
cf470720 185 * @param array $params
b081365f 186 * Array of parameters determined by getfields.
73f3e293
E
187 */
188function _civicrm_api3_job_send_reminder(&$params) {
189 //@todo this function will now take all fields in action_schedule as params
190 // as it is calling the api fn to set the filters - update getfields to reflect
191 $params['id'] = array(
192 'type' => CRM_Utils_Type::T_INT,
21dfd5f5 193 'title' => 'Action Schedule ID',
73f3e293
E
194 );
195}
6a488035 196/**
c1a920f1 197 * Execute a specific report instance and send the output via email.
6a488035 198 *
cf470720
TO
199 * @param array $params
200 * (reference ) input parameters.
6a488035
TO
201 * sendmail - Boolean - should email be sent?, required
202 * instanceId - Integer - the report instance ID
203 * resetVal - Integer - should we reset form state (always true)?
204 *
c23f45d3 205 * @return array
6a488035
TO
206 */
207function civicrm_api3_job_mail_report($params) {
6a488035
TO
208 $result = CRM_Report_Utils_Report::processReport($params);
209
210 if ($result['is_error'] == 0) {
211 // this should be handling by throwing exceptions but can't remove until we can test that.
212 return civicrm_api3_create_success();
213 }
214 else {
215 return civicrm_api3_create_error($result['messages']);
216 }
217}
218
219/**
6a488035 220 * This method allows to update Email Greetings, Postal Greetings and Addressee for a specific contact type.
211e2fca 221 *
6a488035
TO
222 * IMPORTANT: You must first create valid option value before using via admin interface.
223 * Check option lists for Email Greetings, Postal Greetings and Addressee
224 *
211e2fca 225 * @todo - is this here by mistake or should it be added to _spec function :id - Integer - greetings option group.
6a488035 226 *
9657ccf2 227 * @param array $params
77b97be7 228 *
3f83a242 229 * @return array
6a488035
TO
230 */
231function civicrm_api3_job_update_greeting($params) {
6a488035 232 if (isset($params['ct']) && isset($params['gt'])) {
6a488035
TO
233 $ct = explode(',', $params['ct']);
234 $gt = explode(',', $params['gt']);
235 foreach ($ct as $ctKey => $ctValue) {
236 foreach ($gt as $gtKey => $gtValue) {
237 $params['ct'] = trim($ctValue);
238 $params['gt'] = trim($gtValue);
3f83a242 239 CRM_Contact_BAO_Contact_Utils::updateGreeting($params);
6a488035
TO
240 }
241 }
242 }
243 else {
3f83a242 244 CRM_Contact_BAO_Contact_Utils::updateGreeting($params);
6a488035 245 }
3f83a242 246 return civicrm_api3_create_success();
6a488035
TO
247}
248
249/**
211e2fca 250 * Adjust Metadata for Get action.
72b3a70c 251 *
c1a920f1
EM
252 * The metadata is used for setting defaults, documentation & validation.
253 *
72b3a70c 254 * @param array $params
b081365f 255 * Array of parameters determined by getfields.
72b3a70c 256 */
6a488035
TO
257function _civicrm_api3_job_update_greeting_spec(&$params) {
258 $params['ct'] = array(
259 'api.required' => 1,
260 'title' => 'Contact Type',
261 'type' => CRM_Utils_Type::T_STRING,
262 );
263 $params['gt'] = array(
971d41b1
CW
264 'api.required' => 1,
265 'title' => 'Greeting Type',
266 'type' => CRM_Utils_Type::T_STRING,
6a488035
TO
267 );
268}
269
270/**
c1a920f1 271 * Mass update pledge statuses.
6a488035 272 *
cf470720 273 * @param array $params
c1a920f1 274 *
c23f45d3 275 * @return array
6a488035
TO
276 */
277function civicrm_api3_job_process_pledge($params) {
278 // *** Uncomment the next line if you want automated reminders to be sent
279 // $params['send_reminders'] = true;
280 $result = CRM_Pledge_BAO_Pledge::updatePledgeStatus($params);
281
282 if ($result['is_error'] == 0) {
283 // experiment: detailed execution log is a result here
284 return civicrm_api3_create_success($result['messages']);
285 }
286 else {
287 return civicrm_api3_create_error($result['error_message']);
288 }
289}
290
291/**
c1a920f1 292 * Process mail queue.
6a488035
TO
293 *
294 * @param array $params
295 *
296 * @return array
297 */
298function civicrm_api3_job_process_mailing($params) {
299
300 if (!CRM_Mailing_BAO_Mailing::processQueue()) {
301 return civicrm_api3_create_error('Process Queue failed');
302 }
303 else {
304 $values = array();
244bbdd8 305 return civicrm_api3_create_success($values, $params, 'Job', 'process_mailing');
6a488035
TO
306 }
307}
308
309/**
cd5823ae 310 * Process sms queue.
6a488035
TO
311 *
312 * @param array $params
313 *
314 * @return array
315 */
316function civicrm_api3_job_process_sms($params) {
6a488035
TO
317 if (!CRM_Mailing_BAO_Mailing::processQueue('sms')) {
318 return civicrm_api3_create_error('Process Queue failed');
319 }
320 else {
321 $values = array();
244bbdd8 322 return civicrm_api3_create_success($values, $params, 'Job', 'process_sms');
6a488035
TO
323 }
324}
39fefc30 325
6a488035 326/**
c1a920f1 327 * Job to get mail responses from civiMailing.
9657ccf2
EM
328 *
329 * @param array $params
330 *
331 * @return array
6a488035
TO
332 */
333function civicrm_api3_job_fetch_bounces($params) {
39fefc30 334 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
6a488035
TO
335 if (!$lock->isAcquired()) {
336 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
337 }
338 if (!CRM_Utils_Mail_EmailProcessor::processBounces()) {
39fefc30 339 $lock->release();
6a488035
TO
340 return civicrm_api3_create_error('Process Bounces failed');
341 }
6a488035 342 $lock->release();
39fefc30
KJ
343
344 // FIXME: processBounces doesn't return true/false on success/failure
345 $values = array();
244bbdd8 346 return civicrm_api3_create_success($values, $params, 'Job', 'fetch_bounces');
6a488035
TO
347}
348
349/**
35823763 350 * Job to get mail and create activities.
9657ccf2
EM
351 *
352 * @param array $params
353 *
354 * @return array
6a488035
TO
355 */
356function civicrm_api3_job_fetch_activities($params) {
39fefc30 357 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
6a488035
TO
358 if (!$lock->isAcquired()) {
359 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
360 }
39fefc30
KJ
361
362 try {
363 CRM_Utils_Mail_EmailProcessor::processActivities();
481a74f4 364 $values = array();
6a488035 365 $lock->release();
244bbdd8 366 return civicrm_api3_create_success($values, $params, 'Job', 'fetch_activities');
0db6c3e1
TO
367 }
368 catch (Exception $e) {
6a488035
TO
369 $lock->release();
370 return civicrm_api3_create_error('Process Activities failed');
371 }
372}
373
374/**
35823763 375 * Process participant statuses.
6a488035 376 *
cf470720 377 * @param array $params
35823763 378 * Input parameters.
6a488035 379 *
a6c01b45 380 * @return array
72b3a70c 381 * array of properties, if error an array with an error id and error message
6a488035
TO
382 */
383function civicrm_api3_job_process_participant($params) {
6a488035
TO
384 $result = CRM_Event_BAO_ParticipantStatusType::process($params);
385
386 if (!$result['is_error']) {
387 return civicrm_api3_create_success(implode("\r\r", $result['messages']));
388 }
389 else {
390 return civicrm_api3_create_error('Error while processing participant statuses');
391 }
392}
393
394
395/**
61fe4988
EM
396 * This api checks and updates the status of all membership records for a given domain.
397 *
398 * The function uses the calc_membership_status and update_contact_membership APIs.
6a488035
TO
399 *
400 * IMPORTANT:
401 * Sending renewal reminders has been migrated from this job to the Scheduled Reminders function as of 4.3.
402 *
cf470720
TO
403 * @param array $params
404 * Input parameters NOT USED.
6a488035 405 *
971d41b1 406 * @return bool
72b3a70c 407 * true if success, else false
6a488035 408 */
608e6658 409function civicrm_api3_job_process_membership($params) {
39fefc30
KJ
410 $lock = new CRM_Core_Lock('civimail.job.updateMembership');
411 if (!$lock->isAcquired()) {
017877e1 412 return civicrm_api3_create_error('Could not acquire lock, another Membership Processing process is running');
39fefc30
KJ
413 }
414
6a488035 415 $result = CRM_Member_BAO_Membership::updateAllMembershipStatus();
39fefc30 416 $lock->release();
6a488035
TO
417
418 if ($result['is_error'] == 0) {
244bbdd8 419 return civicrm_api3_create_success($result['messages'], $params, 'Job', 'process_membership');
6a488035
TO
420 }
421 else {
422 return civicrm_api3_create_error($result['messages']);
423 }
424}
425
426/**
3f83a242 427 * This api checks and updates the status of all survey respondents.
6a488035 428 *
cf470720
TO
429 * @param array $params
430 * (reference ) input parameters.
6a488035 431 *
971d41b1 432 * @return bool
72b3a70c 433 * true if success, else false
6a488035
TO
434 */
435function civicrm_api3_job_process_respondent($params) {
6a488035
TO
436 $result = CRM_Campaign_BAO_Survey::releaseRespondent($params);
437
438 if ($result['is_error'] == 0) {
439 return civicrm_api3_create_success();
440 }
441 else {
442 return civicrm_api3_create_error($result['messages']);
443 }
444}
445
446/**
447 * Merges given pair of duplicate contacts.
448 *
cf470720
TO
449 * @param array $params
450 * Input parameters.
6a488035 451 *
a6c01b45 452 * @return array
72b3a70c 453 * API Result Array
6a488035
TO
454 */
455function civicrm_api3_job_process_batch_merge($params) {
456 $rgid = CRM_Utils_Array::value('rgid', $params);
457 $gid = CRM_Utils_Array::value('gid', $params);
458
459 $mode = CRM_Utils_Array::value('mode', $params, 'safe');
460 $autoFlip = CRM_Utils_Array::value('auto_flip', $params, TRUE);
461
6a488035
TO
462 $result = CRM_Dedupe_Merger::batchMerge($rgid, $gid, $mode, $autoFlip);
463
464 if ($result['is_error'] == 0) {
465 return civicrm_api3_create_success();
466 }
467 else {
468 return civicrm_api3_create_error($result['messages']);
469 }
470}
471
3f83a242 472/**
61fe4988
EM
473 * Metadata for batch merge function.
474 *
3f83a242
EM
475 * @param $params
476 */
477function _civicrm_api3_job_process_batch_merge_spec(&$params) {
478 $params['rgid'] = array(
479 'title' => 'rule group id',
480 'type' => CRM_Utils_Type::T_INT,
481 );
482 $params['gid'] = array(
483 'title' => 'group id',
484 'type' => CRM_Utils_Type::T_INT,
485 );
486 $params['mode'] = array(
487 'title' => 'Mode',
608e6658 488 'description' => 'helps decide how to behave when there are conflicts. A \'safe\' value skips the merge if there are no conflicts. Does a force merge otherwise.',
3f83a242
EM
489 'type' => CRM_Utils_Type::T_STRING,
490 );
491 $params['auto_flip'] = array(
492 'title' => 'Auto Flip',
493 'description' => 'let the api decide which contact to retain and which to delete?',
494 'type' => CRM_Utils_Type::T_BOOLEAN,
495 );
496}
497
6a488035 498/**
61fe4988 499 * Runs handlePaymentCron method in the specified payment processor.
6a488035 500 *
cf470720
TO
501 * @param array $params
502 * Input parameters.
6a488035 503 *
61fe4988
EM
504 * Expected @params array keys are: INCORRECTLY DOCUMENTED AND SHOULD BE IN THE _spec function
505 * for retrieval via getfields.
6a488035 506 * {string 'processor_name' - the name of the payment processor, eg: Sagepay}
6a488035
TO
507 */
508function civicrm_api3_job_run_payment_cron($params) {
509
6a488035
TO
510 // live mode
511 CRM_Core_Payment::handlePaymentMethod(
512 'PaymentCron',
513 array_merge(
514 $params,
515 array(
516 'caller' => 'api',
517 )
518 )
519 );
520
521 // test mode
522 CRM_Core_Payment::handlePaymentMethod(
523 'PaymentCron',
524 array_merge(
525 $params,
526 array(
527 'mode' => 'test',
528 )
529 )
530 );
531}
532
533/**
61fe4988
EM
534 * This api cleans up all the old session entries and temp tables.
535 *
536 * We recommend that sites run this on an hourly basis.
6a488035 537 *
cf470720 538 * @param array $params
971d41b1 539 * Sends in various config parameters to decide what needs to be cleaned.
6a488035 540 */
481a74f4 541function civicrm_api3_job_cleanup($params) {
971d41b1
CW
542 $session = CRM_Utils_Array::value('session', $params, TRUE);
543 $tempTable = CRM_Utils_Array::value('tempTables', $params, TRUE);
544 $jobLog = CRM_Utils_Array::value('jobLog', $params, TRUE);
545 $prevNext = CRM_Utils_Array::value('prevNext', $params, TRUE);
546 $dbCache = CRM_Utils_Array::value('dbCache', $params, FALSE);
547 $memCache = CRM_Utils_Array::value('memCache', $params, FALSE);
6a488035 548
481a74f4
TO
549 if ($session || $tempTable || $prevNext) {
550 CRM_Core_BAO_Cache::cleanup($session, $tempTable, $prevNext);
6a488035
TO
551 }
552
481a74f4
TO
553 if ($jobLog) {
554 CRM_Core_BAO_Job::cleanup();
6a488035
TO
555 }
556
481a74f4
TO
557 if ($dbCache) {
558 CRM_Core_Config::clearDBCache();
6a488035
TO
559 }
560
481a74f4
TO
561 if ($memCache) {
562 CRM_Utils_System::flushCache();
6a488035
TO
563 }
564}
565
566/**
567 * Set expired relationships to disabled.
61fe4988
EM
568 *
569 * @param array $params
570 *
9657ccf2 571 * @return array
3f83a242 572 * @throws \API_Exception
6a488035 573 */
608e6658 574function civicrm_api3_job_disable_expired_relationships($params) {
6a488035 575 $result = CRM_Contact_BAO_Relationship::disableExpiredRelationships();
3f83a242
EM
576 if (!$result) {
577 throw new API_Exception('Failed to disable all expired relationships.');
6a488035 578 }
244bbdd8 579 return civicrm_api3_create_success(1, $params, 'Job', 'disable_expired_relationships');
6a488035
TO
580}
581
582/**
61fe4988
EM
583 * This api reloads all the smart groups.
584 *
585 * If the org has a large number of smart groups it is recommended that they use the limit clause
586 * to limit the number of smart groups evaluated on a per job basis.
587 *
588 * Might also help to increase the smartGroupCacheTimeout and use the cache.
9657ccf2
EM
589 *
590 * @param array $params
61fe4988 591 *
9657ccf2 592 * @return array
3f83a242 593 * @throws \API_Exception
6a488035 594 */
9657ccf2 595function civicrm_api3_job_group_rebuild($params) {
39fefc30
KJ
596 $lock = new CRM_Core_Lock('civimail.job.groupRebuild');
597 if (!$lock->isAcquired()) {
3f83a242 598 throw new API_Exception('Could not acquire lock, another EmailProcessor process is running');
39fefc30
KJ
599 }
600
481a74f4 601 $limit = CRM_Utils_Array::value('limit', $params, 0);
6a488035 602
971d41b1 603 CRM_Contact_BAO_GroupContactCache::loadAll(NULL, $limit);
39fefc30
KJ
604 $lock->release();
605
f9e16e9a 606 return civicrm_api3_create_success();
6a488035 607}