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