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