Merge pull request #19375 from civicrm/5.34
[civicrm-core.git] / api / v3 / Job.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
a30c801b
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
244bbdd8 13 * This api is used for working with scheduled "cron" jobs.
6a488035
TO
14 *
15 * @package CiviCRM_APIv3
6a488035
TO
16 */
17
6a488035 18/**
c1a920f1
EM
19 * Adjust metadata for "Create" action.
20 *
21 * The metadata is used for setting defaults, documentation & validation.
6a488035 22 *
cf470720 23 * @param array $params
b081365f 24 * Array of parameters determined by getfields.
6a488035
TO
25 */
26function _civicrm_api3_job_create_spec(&$params) {
27 $params['run_frequency']['api.required'] = 1;
28 $params['name']['api.required'] = 1;
29 $params['api_entity']['api.required'] = 1;
30 $params['api_action']['api.required'] = 1;
31
32 $params['domain_id']['api.default'] = CRM_Core_Config::domainID();
33 $params['is_active']['api.default'] = 1;
34}
35
36/**
c1a920f1 37 * Create scheduled job.
6a488035 38 *
cf470720
TO
39 * @param array $params
40 * Associative array of property name/value pairs to insert in new job.
6a488035 41 *
72b3a70c 42 * @return array
6a488035
TO
43 */
44function civicrm_api3_job_create($params) {
a25b46e9 45 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Job');
6a488035
TO
46}
47
7a6059c2
MW
48/**
49 * Adjust metadata for clone spec action.
50 *
51 * @param array $spec
52 */
53function _civicrm_api3_job_clone_spec(&$spec) {
54 $spec['id']['title'] = 'Job ID to clone';
55 $spec['id']['type'] = CRM_Utils_Type::T_INT;
56 $spec['id']['api.required'] = 1;
57 $spec['is_active']['title'] = 'Job is Active?';
58 $spec['is_active']['type'] = CRM_Utils_Type::T_BOOLEAN;
59 $spec['is_active']['api.required'] = 0;
60}
61
62/**
63 * Clone Job.
64 *
65 * @param array $params
66 *
67 * @return array
68 * @throws \API_Exception
69 * @throws \CiviCRM_API3_Exception
70 */
71function civicrm_api3_job_clone($params) {
72 if (empty($params['id'])) {
73 throw new API_Exception("Mandatory key(s) missing from params array: id field is required");
74 }
75 $id = $params['id'];
76 unset($params['id']);
77 $params['last_run'] = 'null';
78 $params['scheduled_run_date'] = 'null';
79 $newJobDAO = CRM_Core_BAO_Job::copy($id, $params);
cf8f0fff 80 return civicrm_api3('Job', 'get', ['id' => $newJobDAO->id]);
7a6059c2
MW
81}
82
6a488035 83/**
211e2fca 84 * Retrieve one or more job.
971d41b1
CW
85 *
86 * @param array $params
87 * input parameters
971d41b1 88 *
211e2fca 89 * @return array
6a488035
TO
90 */
91function civicrm_api3_job_get($params) {
92 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
93}
94
95/**
211e2fca 96 * Delete a job.
6a488035 97 *
9657ccf2 98 * @param array $params
6a488035
TO
99 */
100function civicrm_api3_job_delete($params) {
61ef23bd 101 _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
6a488035
TO
102}
103
104/**
211e2fca
EM
105 * Dumb wrapper to execute scheduled jobs.
106 *
107 * Always creates success - errors and results are handled in the job log.
6a488035 108 *
cf470720 109 * @param array $params
971d41b1 110 * input parameters (unused).
6a488035 111 *
a6c01b45 112 * @return array
72b3a70c 113 * API Result Array
6a488035 114 */
608e6658 115function civicrm_api3_job_execute($params) {
608e6658 116
6a488035
TO
117 $facility = new CRM_Core_JobManager();
118 $facility->execute(FALSE);
119
61fe4988 120 // Always creates success - results are handled elsewhere.
244bbdd8 121 return civicrm_api3_create_success(1, $params, 'Job');
6a488035
TO
122}
123
124/**
211e2fca 125 * Adjust Metadata for Execute action.
6a488035 126 *
cf470720 127 * @param array $params
b081365f 128 * Array of parameters determined by getfields.
6a488035
TO
129 */
130function _civicrm_api3_job_execute_spec(&$params) {
131}
132
133/**
211e2fca 134 * Geocode group of contacts based on given params.
6a488035 135 *
cf470720 136 * @param array $params
971d41b1 137 * input parameters.
6a488035 138 *
a6c01b45 139 * @return array
72b3a70c 140 * API Result Array
6a488035
TO
141 */
142function civicrm_api3_job_geocode($params) {
6a488035
TO
143 $gc = new CRM_Utils_Address_BatchUpdate($params);
144
6a488035
TO
145 $result = $gc->run();
146
147 if ($result['is_error'] == 0) {
148 return civicrm_api3_create_success($result['messages']);
149 }
150 else {
151 return civicrm_api3_create_error($result['messages']);
152 }
153}
9657ccf2 154
6a488035 155/**
c1a920f1 156 * First check on Code documentation.
9657ccf2
EM
157 *
158 * @param array $params
6a488035
TO
159 */
160function _civicrm_api3_job_geocode_spec(&$params) {
cf8f0fff 161 $params['start'] = [
8ebf8a02
DG
162 'title' => 'Starting Contact ID',
163 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
164 ];
165 $params['end'] = [
8ebf8a02
DG
166 'title' => 'Ending Contact ID',
167 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
168 ];
169 $params['geocoding'] = [
d142432b
EM
170 'title' => 'Geocode address?',
171 'type' => CRM_Utils_Type::T_BOOLEAN,
cf8f0fff
CW
172 ];
173 $params['parse'] = [
d142432b
EM
174 'title' => 'Parse street address?',
175 'type' => CRM_Utils_Type::T_BOOLEAN,
cf8f0fff
CW
176 ];
177 $params['throttle'] = [
b2ed8e73 178 'title' => 'Throttle?',
d142432b
EM
179 'description' => 'If enabled, geo-codes at a slow rate',
180 'type' => CRM_Utils_Type::T_BOOLEAN,
cf8f0fff 181 ];
6a488035
TO
182}
183
184/**
2e9914a0 185 * Send the scheduled reminders as configured.
6a488035 186 *
cf470720 187 * @param array $params
2e9914a0 188 * - now - the time to use, in YmdHis format
189 * - makes testing a bit simpler since we can simulate past/future time
6a488035 190 *
c23f45d3 191 * @return array
2e9914a0 192 * @throws \API_Exception
193 * @throws \CRM_Core_Exception
194 * @throws \CiviCRM_API3_Exception
6a488035
TO
195 */
196function civicrm_api3_job_send_reminder($params) {
e27a3472
EM
197 //note that $params['rowCount' can be overridden by one of the preferred syntaxes ($options['limit'] = x
198 //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 199 // in that case (ie. makes it non-configurable via the UI). Another approach would be to set a default of 0
e27a3472
EM
200 // in the _spec function - but since that is a deprecated value it seems more contentious than this approach
201 $params['rowCount'] = 0;
83617886 202 $lock = Civi::lockManager()->acquire('worker.core.ActionSchedule');
39fefc30 203 if (!$lock->isAcquired()) {
2e9914a0 204 throw new API_Exception('Could not acquire lock, another ActionSchedule process is running');
39fefc30
KJ
205 }
206
2e9914a0 207 CRM_Core_BAO_ActionSchedule::processQueue($params['now'] ?? NULL, $params);
39fefc30 208 $lock->release();
2e9914a0 209 return civicrm_api3_create_success(1, $params, 'ActionSchedule', 'send_reminder');
6a488035 210}
7c31ae57 211
73f3e293 212/**
c1a920f1
EM
213 * Adjust metadata for "send_reminder" action.
214 *
215 * The metadata is used for setting defaults, documentation & validation.
73f3e293 216 *
cf470720 217 * @param array $params
b081365f 218 * Array of parameters determined by getfields.
73f3e293
E
219 */
220function _civicrm_api3_job_send_reminder(&$params) {
221 //@todo this function will now take all fields in action_schedule as params
222 // as it is calling the api fn to set the filters - update getfields to reflect
cf8f0fff 223 $params['id'] = [
73f3e293 224 'type' => CRM_Utils_Type::T_INT,
21dfd5f5 225 'title' => 'Action Schedule ID',
cf8f0fff 226 ];
73f3e293 227}
7c31ae57 228
6a488035 229/**
c1a920f1 230 * Execute a specific report instance and send the output via email.
6a488035 231 *
cf470720
TO
232 * @param array $params
233 * (reference ) input parameters.
6a488035
TO
234 * sendmail - Boolean - should email be sent?, required
235 * instanceId - Integer - the report instance ID
236 * resetVal - Integer - should we reset form state (always true)?
237 *
c23f45d3 238 * @return array
6a488035
TO
239 */
240function civicrm_api3_job_mail_report($params) {
6a488035
TO
241 $result = CRM_Report_Utils_Report::processReport($params);
242
243 if ($result['is_error'] == 0) {
244 // this should be handling by throwing exceptions but can't remove until we can test that.
245 return civicrm_api3_create_success();
246 }
247 else {
248 return civicrm_api3_create_error($result['messages']);
249 }
250}
251
252/**
6a488035 253 * This method allows to update Email Greetings, Postal Greetings and Addressee for a specific contact type.
211e2fca 254 *
6a488035
TO
255 * IMPORTANT: You must first create valid option value before using via admin interface.
256 * Check option lists for Email Greetings, Postal Greetings and Addressee
257 *
211e2fca 258 * @todo - is this here by mistake or should it be added to _spec function :id - Integer - greetings option group.
6a488035 259 *
9657ccf2 260 * @param array $params
77b97be7 261 *
3f83a242 262 * @return array
6a488035
TO
263 */
264function civicrm_api3_job_update_greeting($params) {
6a488035 265 if (isset($params['ct']) && isset($params['gt'])) {
6a488035
TO
266 $ct = explode(',', $params['ct']);
267 $gt = explode(',', $params['gt']);
268 foreach ($ct as $ctKey => $ctValue) {
269 foreach ($gt as $gtKey => $gtValue) {
270 $params['ct'] = trim($ctValue);
271 $params['gt'] = trim($gtValue);
3f83a242 272 CRM_Contact_BAO_Contact_Utils::updateGreeting($params);
6a488035
TO
273 }
274 }
275 }
276 else {
3f83a242 277 CRM_Contact_BAO_Contact_Utils::updateGreeting($params);
6a488035 278 }
3f83a242 279 return civicrm_api3_create_success();
6a488035
TO
280}
281
282/**
211e2fca 283 * Adjust Metadata for Get action.
72b3a70c 284 *
c1a920f1
EM
285 * The metadata is used for setting defaults, documentation & validation.
286 *
72b3a70c 287 * @param array $params
b081365f 288 * Array of parameters determined by getfields.
72b3a70c 289 */
6a488035 290function _civicrm_api3_job_update_greeting_spec(&$params) {
cf8f0fff 291 $params['ct'] = [
6a488035
TO
292 'api.required' => 1,
293 'title' => 'Contact Type',
294 'type' => CRM_Utils_Type::T_STRING,
cf8f0fff
CW
295 ];
296 $params['gt'] = [
971d41b1
CW
297 'api.required' => 1,
298 'title' => 'Greeting Type',
299 'type' => CRM_Utils_Type::T_STRING,
cf8f0fff 300 ];
6a488035
TO
301}
302
303/**
c1a920f1 304 * Mass update pledge statuses.
6a488035 305 *
cf470720 306 * @param array $params
c1a920f1 307 *
c23f45d3 308 * @return array
6a488035
TO
309 */
310function 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/**
c1a920f1 325 * Process mail queue.
6a488035
TO
326 *
327 * @param array $params
328 *
329 * @return array
330 */
331function civicrm_api3_job_process_mailing($params) {
ec782609 332 $mailsProcessedOrig = CRM_Mailing_BAO_MailingJob::$mailsProcessed;
6a488035 333
03c5ceba 334 try {
335 CRM_Core_BAO_Setting::isAPIJobAllowedToRun($params);
336 }
337 catch (Exception $e) {
338 return civicrm_api3_create_error($e->getMessage());
339 }
340
6a488035
TO
341 if (!CRM_Mailing_BAO_Mailing::processQueue()) {
342 return civicrm_api3_create_error('Process Queue failed');
343 }
344 else {
cf8f0fff 345 $values = [
ec782609 346 'processed' => CRM_Mailing_BAO_MailingJob::$mailsProcessed - $mailsProcessedOrig,
cf8f0fff 347 ];
244bbdd8 348 return civicrm_api3_create_success($values, $params, 'Job', 'process_mailing');
6a488035
TO
349 }
350}
351
352/**
cd5823ae 353 * Process sms queue.
6a488035
TO
354 *
355 * @param array $params
356 *
357 * @return array
358 */
359function civicrm_api3_job_process_sms($params) {
ec782609
TO
360 $mailsProcessedOrig = CRM_Mailing_BAO_MailingJob::$mailsProcessed;
361
6a488035
TO
362 if (!CRM_Mailing_BAO_Mailing::processQueue('sms')) {
363 return civicrm_api3_create_error('Process Queue failed');
364 }
365 else {
cf8f0fff 366 $values = [
ec782609 367 'processed' => CRM_Mailing_BAO_MailingJob::$mailsProcessed - $mailsProcessedOrig,
cf8f0fff 368 ];
244bbdd8 369 return civicrm_api3_create_success($values, $params, 'Job', 'process_sms');
6a488035
TO
370 }
371}
39fefc30 372
6a488035 373/**
c1a920f1 374 * Job to get mail responses from civiMailing.
9657ccf2
EM
375 *
376 * @param array $params
377 *
378 * @return array
6a488035
TO
379 */
380function civicrm_api3_job_fetch_bounces($params) {
83617886 381 $lock = Civi::lockManager()->acquire('worker.mailing.EmailProcessor');
6a488035
TO
382 if (!$lock->isAcquired()) {
383 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
384 }
3ff238ae 385 CRM_Utils_Mail_EmailProcessor::processBounces($params['is_create_activities']);
6a488035 386 $lock->release();
39fefc30 387
3ff238ae 388 return civicrm_api3_create_success(1, $params, 'Job', 'fetch_bounces');
389}
390
391/**
392 * Metadata for bounce function.
393 *
394 * @param array $params
395 */
396function _civicrm_api3_job_fetch_bounces_spec(&$params) {
cf8f0fff 397 $params['is_create_activities'] = [
3ff238ae 398 'api.default' => 0,
399 'type' => CRM_Utils_Type::T_BOOLEAN,
400 'title' => ts('Create activities for replies?'),
cf8f0fff 401 ];
6a488035
TO
402}
403
404/**
35823763 405 * Job to get mail and create activities.
9657ccf2
EM
406 *
407 * @param array $params
408 *
409 * @return array
6a488035
TO
410 */
411function civicrm_api3_job_fetch_activities($params) {
83617886 412 $lock = Civi::lockManager()->acquire('worker.mailing.EmailProcessor');
6a488035
TO
413 if (!$lock->isAcquired()) {
414 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
415 }
39fefc30
KJ
416
417 try {
418 CRM_Utils_Mail_EmailProcessor::processActivities();
cf8f0fff 419 $values = [];
6a488035 420 $lock->release();
244bbdd8 421 return civicrm_api3_create_success($values, $params, 'Job', 'fetch_activities');
0db6c3e1
TO
422 }
423 catch (Exception $e) {
6a488035 424 $lock->release();
e7b952ac 425 return civicrm_api3_create_error($e->getMessage());
6a488035
TO
426 }
427}
428
429/**
35823763 430 * Process participant statuses.
6a488035 431 *
cf470720 432 * @param array $params
35823763 433 * Input parameters.
6a488035 434 *
a6c01b45 435 * @return array
72b3a70c 436 * array of properties, if error an array with an error id and error message
6a488035
TO
437 */
438function civicrm_api3_job_process_participant($params) {
6a488035
TO
439 $result = CRM_Event_BAO_ParticipantStatusType::process($params);
440
441 if (!$result['is_error']) {
442 return civicrm_api3_create_success(implode("\r\r", $result['messages']));
443 }
444 else {
445 return civicrm_api3_create_error('Error while processing participant statuses');
446 }
447}
448
6a488035 449/**
61fe4988
EM
450 * This api checks and updates the status of all membership records for a given domain.
451 *
452 * The function uses the calc_membership_status and update_contact_membership APIs.
6a488035
TO
453 *
454 * IMPORTANT:
455 * Sending renewal reminders has been migrated from this job to the Scheduled Reminders function as of 4.3.
456 *
cf470720
TO
457 * @param array $params
458 * Input parameters NOT USED.
6a488035 459 *
971d41b1 460 * @return bool
72b3a70c 461 * true if success, else false
6a488035 462 */
608e6658 463function civicrm_api3_job_process_membership($params) {
83617886 464 $lock = Civi::lockManager()->acquire('worker.member.UpdateMembership');
39fefc30 465 if (!$lock->isAcquired()) {
017877e1 466 return civicrm_api3_create_error('Could not acquire lock, another Membership Processing process is running');
39fefc30
KJ
467 }
468
ac4d4d8c
MW
469 // We need to pass this through as a simple array of membership status IDs as values.
470 if (!empty($params['exclude_membership_status_ids'])) {
471 is_array($params['exclude_membership_status_ids']) ?: $params['exclude_membership_status_ids'] = [$params['exclude_membership_status_ids']];
472 }
473 if (!empty($params['exclude_membership_status_ids']['IN'])) {
474 $params['exclude_membership_status_ids'] = $params['exclude_membership_status_ids']['IN'];
475 }
476 $result = CRM_Member_BAO_Membership::updateAllMembershipStatus($params);
39fefc30 477 $lock->release();
6a488035
TO
478
479 if ($result['is_error'] == 0) {
244bbdd8 480 return civicrm_api3_create_success($result['messages'], $params, 'Job', 'process_membership');
6a488035
TO
481 }
482 else {
483 return civicrm_api3_create_error($result['messages']);
484 }
485}
486
ac4d4d8c
MW
487function _civicrm_api3_job_process_membership_spec(&$params) {
488 $params['exclude_test_memberships']['api.default'] = TRUE;
489 $params['exclude_test_memberships']['title'] = 'Exclude test memberships';
490 $params['exclude_test_memberships']['description'] = 'Exclude test memberships from calculations (default = TRUE)';
491 $params['exclude_test_memberships']['type'] = CRM_Utils_Type::T_BOOLEAN;
492 $params['only_active_membership_types']['api.default'] = TRUE;
493 $params['only_active_membership_types']['title'] = 'Exclude disabled membership types';
494 $params['only_active_membership_types']['description'] = 'Exclude disabled membership types from calculations (default = TRUE)';
495 $params['only_active_membership_types']['type'] = CRM_Utils_Type::T_BOOLEAN;
496 $params['exclude_membership_status_ids']['title'] = 'Exclude membership status IDs from calculations';
497 $params['exclude_membership_status_ids']['description'] = 'Default: Exclude Pending, Cancelled, Expired. Deceased will always be excluded';
8a9500e0
MW
498 $params['exclude_membership_status_ids']['type'] = CRM_Utils_Type::T_INT;
499 $params['exclude_membership_status_ids']['pseudoconstant'] = [
500 'table' => 'civicrm_membership_status',
501 'keyColumn' => 'id',
502 'labelColumn' => 'label',
503 ];
ac4d4d8c
MW
504}
505
6a488035 506/**
3f83a242 507 * This api checks and updates the status of all survey respondents.
6a488035 508 *
cf470720
TO
509 * @param array $params
510 * (reference ) input parameters.
6a488035 511 *
971d41b1 512 * @return bool
72b3a70c 513 * true if success, else false
6a488035
TO
514 */
515function civicrm_api3_job_process_respondent($params) {
6a488035
TO
516 $result = CRM_Campaign_BAO_Survey::releaseRespondent($params);
517
518 if ($result['is_error'] == 0) {
519 return civicrm_api3_create_success();
520 }
521 else {
522 return civicrm_api3_create_error($result['messages']);
523 }
524}
525
526/**
527 * Merges given pair of duplicate contacts.
528 *
cf470720
TO
529 * @param array $params
530 * Input parameters.
6a488035 531 *
a6c01b45 532 * @return array
72b3a70c 533 * API Result Array
76ddbc8e 534 * @throws \CiviCRM_API3_Exception
6a488035
TO
535 */
536function civicrm_api3_job_process_batch_merge($params) {
f748c073 537 $rule_group_id = $params['rule_group_id'] ?? NULL;
76ddbc8e 538 if (!$rule_group_id) {
cf8f0fff 539 $rule_group_id = civicrm_api3('RuleGroup', 'getvalue', [
76ddbc8e 540 'contact_type' => 'Individual',
541 'used' => 'Unsupervised',
542 'return' => 'id',
cf8f0fff
CW
543 'options' => ['limit' => 1],
544 ]);
76ddbc8e 545 }
f748c073 546 $rgid = $params['rgid'] ?? NULL;
547 $gid = $params['gid'] ?? NULL;
6a488035 548 $mode = CRM_Utils_Array::value('mode', $params, 'safe');
6a488035 549
997a03fe 550 $result = CRM_Dedupe_Merger::batchMerge($rule_group_id, $gid, $mode, 1, 2, CRM_Utils_Array::value('criteria', $params, []), CRM_Utils_Array::value('check_permissions', $params), NULL, $params['search_limit']);
6a488035 551
76ddbc8e 552 return civicrm_api3_create_success($result, $params);
6a488035
TO
553}
554
3f83a242 555/**
61fe4988
EM
556 * Metadata for batch merge function.
557 *
3f83a242
EM
558 * @param $params
559 */
560function _civicrm_api3_job_process_batch_merge_spec(&$params) {
cf8f0fff 561 $params['rule_group_id'] = [
76ddbc8e 562 'title' => 'Dedupe rule group id, defaults to Contact Unsupervised rule',
3f83a242 563 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
564 'api.aliases' => ['rgid'],
565 ];
566 $params['gid'] = [
3f83a242
EM
567 'title' => 'group id',
568 'type' => CRM_Utils_Type::T_INT,
cf8f0fff
CW
569 ];
570 $params['mode'] = [
3f83a242 571 'title' => 'Mode',
608e6658 572 '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 573 'type' => CRM_Utils_Type::T_STRING,
cf8f0fff
CW
574 ];
575 $params['auto_flip'] = [
3f83a242
EM
576 'title' => 'Auto Flip',
577 'description' => 'let the api decide which contact to retain and which to delete?',
578 'type' => CRM_Utils_Type::T_BOOLEAN,
cf8f0fff 579 ];
997a03fe 580 $params['search_limit'] = [
581 'title' => ts('Number of contacts to look for matches for.'),
582 'type' => CRM_Utils_Type::T_INT,
583 'api.default' => (int) Civi::settings()->get('dedupe_default_limit'),
584 ];
585
3f83a242
EM
586}
587
6a488035 588/**
61fe4988 589 * Runs handlePaymentCron method in the specified payment processor.
6a488035 590 *
cf470720
TO
591 * @param array $params
592 * Input parameters.
6a488035 593 *
61fe4988
EM
594 * Expected @params array keys are: INCORRECTLY DOCUMENTED AND SHOULD BE IN THE _spec function
595 * for retrieval via getfields.
6a488035 596 * {string 'processor_name' - the name of the payment processor, eg: Sagepay}
6a488035
TO
597 */
598function civicrm_api3_job_run_payment_cron($params) {
599
6a488035
TO
600 // live mode
601 CRM_Core_Payment::handlePaymentMethod(
602 'PaymentCron',
603 array_merge(
604 $params,
cf8f0fff 605 [
6a488035 606 'caller' => 'api',
cf8f0fff 607 ]
6a488035
TO
608 )
609 );
610
611 // test mode
612 CRM_Core_Payment::handlePaymentMethod(
613 'PaymentCron',
614 array_merge(
615 $params,
cf8f0fff 616 [
6a488035 617 'mode' => 'test',
cf8f0fff 618 ]
6a488035
TO
619 )
620 );
621}
622
623/**
61fe4988
EM
624 * This api cleans up all the old session entries and temp tables.
625 *
626 * We recommend that sites run this on an hourly basis.
6a488035 627 *
cf470720 628 * @param array $params
971d41b1 629 * Sends in various config parameters to decide what needs to be cleaned.
6a488035 630 */
481a74f4 631function civicrm_api3_job_cleanup($params) {
971d41b1
CW
632 $session = CRM_Utils_Array::value('session', $params, TRUE);
633 $tempTable = CRM_Utils_Array::value('tempTables', $params, TRUE);
634 $jobLog = CRM_Utils_Array::value('jobLog', $params, TRUE);
fd33fead 635 $expired = CRM_Utils_Array::value('expiredDbCache', $params, TRUE);
971d41b1
CW
636 $prevNext = CRM_Utils_Array::value('prevNext', $params, TRUE);
637 $dbCache = CRM_Utils_Array::value('dbCache', $params, FALSE);
638 $memCache = CRM_Utils_Array::value('memCache', $params, FALSE);
9db8dfb7 639 $tplCache = CRM_Utils_Array::value('tplCache', $params, FALSE);
640 $wordRplc = CRM_Utils_Array::value('wordRplc', $params, FALSE);
6a488035 641
fd33fead
TO
642 if ($session || $tempTable || $prevNext || $expired) {
643 CRM_Core_BAO_Cache::cleanup($session, $tempTable, $prevNext, $expired);
6a488035
TO
644 }
645
481a74f4
TO
646 if ($jobLog) {
647 CRM_Core_BAO_Job::cleanup();
6a488035
TO
648 }
649
9db8dfb7 650 if ($tplCache) {
4dd6dceb 651 $config = CRM_Core_Config::singleton();
652 $config->cleanup(1, FALSE);
653 }
654
481a74f4
TO
655 if ($dbCache) {
656 CRM_Core_Config::clearDBCache();
6a488035
TO
657 }
658
481a74f4
TO
659 if ($memCache) {
660 CRM_Utils_System::flushCache();
6a488035 661 }
4dd6dceb 662
9db8dfb7 663 if ($wordRplc) {
4dd6dceb 664 CRM_Core_BAO_WordReplacement::rebuild();
665 }
6a488035
TO
666}
667
668/**
669 * Set expired relationships to disabled.
61fe4988
EM
670 *
671 * @param array $params
672 *
9657ccf2 673 * @return array
3f83a242 674 * @throws \API_Exception
6a488035 675 */
608e6658 676function civicrm_api3_job_disable_expired_relationships($params) {
6a488035 677 $result = CRM_Contact_BAO_Relationship::disableExpiredRelationships();
3f83a242
EM
678 if (!$result) {
679 throw new API_Exception('Failed to disable all expired relationships.');
6a488035 680 }
244bbdd8 681 return civicrm_api3_create_success(1, $params, 'Job', 'disable_expired_relationships');
6a488035
TO
682}
683
684/**
61fe4988
EM
685 * This api reloads all the smart groups.
686 *
687 * If the org has a large number of smart groups it is recommended that they use the limit clause
688 * to limit the number of smart groups evaluated on a per job basis.
689 *
690 * Might also help to increase the smartGroupCacheTimeout and use the cache.
9657ccf2
EM
691 *
692 * @param array $params
61fe4988 693 *
9657ccf2 694 * @return array
3f83a242 695 * @throws \API_Exception
6a488035 696 */
9657ccf2 697function civicrm_api3_job_group_rebuild($params) {
83617886 698 $lock = Civi::lockManager()->acquire('worker.core.GroupRebuild');
39fefc30 699 if (!$lock->isAcquired()) {
0feefbc7 700 throw new API_Exception('Could not acquire lock, another GroupRebuild process is running');
39fefc30
KJ
701 }
702
2975f0aa 703 $limit = $params['limit'] ?? 0;
6a488035 704
971d41b1 705 CRM_Contact_BAO_GroupContactCache::loadAll(NULL, $limit);
39fefc30
KJ
706 $lock->release();
707
f9e16e9a 708 return civicrm_api3_create_success();
6a488035 709}
999128a9 710
a357281f 711/**
712 * Flush smart groups caches.
713 *
714 * This job purges aged smart group cache data (based on the timeout value). Sites can decide whether they want this
715 * job and / or the group cache rebuild job to run. In some cases performance is better when old caches are cleared out
716 * prior to any attempt to rebuild them. Also, many sites are very happy to have caches built on demand, provided the
717 * user is not having to wait for deadlocks to clear when invalidating them.
718 *
719 * @param array $params
720 *
721 * @return array
722 * @throws \API_Exception
723 */
724function civicrm_api3_job_group_cache_flush($params) {
2b68a50c 725 CRM_Contact_BAO_GroupContactCache::deterministicCacheFlush();
a357281f 726 return civicrm_api3_create_success();
727}
728
999128a9
CW
729/**
730 * Check for CiviCRM software updates.
731 *
732 * Anonymous site statistics are sent back to civicrm.org during this check.
733 */
734function civicrm_api3_job_version_check() {
735 $vc = new CRM_Utils_VersionCheck();
736 $vc->fetch();
e9e60a8c 737 return civicrm_api3_create_success();
999128a9 738}