5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.6 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2014 |
9 +--------------------------------------------------------------------+
10 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * new version of civicrm APIs. See blog post at
32 * http://civicrm.org/node/131
34 * @package CiviCRM_APIv3
36 * @copyright CiviCRM LLC (c) 2004-2014
37 * $Id: Contact.php 30879 2010-11-22 15:45:55Z shot $
41 * Adjust metadata for "Create" action.
43 * The metadata is used for setting defaults, documentation & validation.
45 * @param array $params
46 * Array or parameters determined by getfields.
48 function _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;
54 $params['domain_id']['api.default'] = CRM_Core_Config
::domainID();
55 $params['is_active']['api.default'] = 1;
59 * Create scheduled job.
61 * @param array $params
62 * Associative array of property name/value pairs to insert in new job.
66 function civicrm_api3_job_create($params) {
67 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
71 * Retrieve one or more job.
73 * @param array $params
78 function civicrm_api3_job_get($params) {
79 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
85 * @param array $params
87 function civicrm_api3_job_delete($params) {
88 _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
92 * Dumb wrapper to execute scheduled jobs.
94 * Always creates success - errors and results are handled in the job log.
96 * @param array $params
97 * input parameters (unused).
102 function civicrm_api3_job_execute($params) {
104 $facility = new CRM_Core_JobManager();
105 $facility->execute(FALSE);
107 // Always creates success - results are handled elsewhere.
108 return civicrm_api3_create_success(1, $params);
112 * Adjust Metadata for Execute action.
114 * @param array $params
115 * Array or parameters determined by getfields.
117 function _civicrm_api3_job_execute_spec(&$params) {
121 * Geocode group of contacts based on given params.
123 * @param array $params
129 function civicrm_api3_job_geocode($params) {
130 $gc = new CRM_Utils_Address_BatchUpdate($params);
132 $result = $gc->run();
134 if ($result['is_error'] == 0) {
135 return civicrm_api3_create_success($result['messages']);
138 return civicrm_api3_create_error($result['messages']);
143 * First check on Code documentation.
145 * @param array $params
147 function _civicrm_api3_job_geocode_spec(&$params) {
148 $params['start'] = array('title' => 'Start Date');
149 $params['end'] = array('title' => 'End Date');
150 $params['geocoding'] = array('title' => 'Geocode address?');
151 $params['parse'] = array('title' => 'Parse street address?');
152 $params['throttle'] = array('title' => 'Throttle? if enabled, geo-codes at a slow rate');
156 * Send the scheduled reminders for all contacts (either for activities or events).
158 * @param array $params
159 * (reference ) input parameters.
160 * now - the time to use, in YmdHis format
161 * - makes testing a bit simpler since we can simulate past/future time
165 function civicrm_api3_job_send_reminder($params) {
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
168 // in that case (ie. makes it non-configurable via the UI). Another approach would be to set a default of 0
169 // in the _spec function - but since that is a deprecated value it seems more contentious than this approach
170 $params['rowCount'] = 0;
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');
176 $result = CRM_Core_BAO_ActionSchedule
::processQueue(CRM_Utils_Array
::value('now', $params), $params);
179 if ($result['is_error'] == 0) {
180 return civicrm_api3_create_success();
183 return civicrm_api3_create_error($result['messages']);
187 * Adjust metadata for "send_reminder" action.
189 * The metadata is used for setting defaults, documentation & validation.
191 * @param array $params
192 * Array or parameters determined by getfields.
194 function _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
,
199 'title' => 'Action Schedule ID',
203 * Execute a specific report instance and send the output via email.
205 * @param array $params
206 * (reference ) input parameters.
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)?
213 function civicrm_api3_job_mail_report($params) {
214 $result = CRM_Report_Utils_Report
::processReport($params);
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();
221 return civicrm_api3_create_error($result['messages']);
226 * This method allows to update Email Greetings, Postal Greetings and Addressee for a specific contact type.
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
231 * @todo - is this here by mistake or should it be added to _spec function :id - Integer - greetings option group.
233 * @param array $params
237 function civicrm_api3_job_update_greeting($params) {
238 if (isset($params['ct']) && isset($params['gt'])) {
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);
245 CRM_Contact_BAO_Contact_Utils
::updateGreeting($params);
250 CRM_Contact_BAO_Contact_Utils
::updateGreeting($params);
252 return civicrm_api3_create_success();
256 * Adjust Metadata for Get action.
258 * The metadata is used for setting defaults, documentation & validation.
260 * @param array $params
261 * Array or parameters determined by getfields.
263 function _civicrm_api3_job_update_greeting_spec(&$params) {
264 $params['ct'] = array(
266 'title' => 'Contact Type',
267 'type' => CRM_Utils_Type
::T_STRING
,
269 $params['gt'] = array(
271 'title' => 'Greeting Type',
272 'type' => CRM_Utils_Type
::T_STRING
,
277 * Mass update pledge statuses.
279 * @param array $params
283 function 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);
288 if ($result['is_error'] == 0) {
289 // experiment: detailed execution log is a result here
290 return civicrm_api3_create_success($result['messages']);
293 return civicrm_api3_create_error($result['error_message']);
298 * Process mail queue.
300 * @param array $params
304 function civicrm_api3_job_process_mailing($params) {
306 if (!CRM_Mailing_BAO_Mailing
::processQueue()) {
307 return civicrm_api3_create_error('Process Queue failed');
311 return civicrm_api3_create_success($values, $params, 'mailing', 'process');
318 * @param array $params
322 function civicrm_api3_job_process_sms($params) {
323 if (!CRM_Mailing_BAO_Mailing
::processQueue('sms')) {
324 return civicrm_api3_create_error('Process Queue failed');
328 return civicrm_api3_create_success($values, $params, 'mailing', 'process');
333 * Job to get mail responses from civiMailing.
335 * @param array $params
339 function civicrm_api3_job_fetch_bounces($params) {
340 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
341 if (!$lock->isAcquired()) {
342 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
344 if (!CRM_Utils_Mail_EmailProcessor
::processBounces()) {
346 return civicrm_api3_create_error('Process Bounces failed');
350 // FIXME: processBounces doesn't return true/false on success/failure
352 return civicrm_api3_create_success($values, $params, 'mailing', 'bounces');
356 * Job to get mail and create activities.
358 * @param array $params
362 function civicrm_api3_job_fetch_activities($params) {
363 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
364 if (!$lock->isAcquired()) {
365 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
369 CRM_Utils_Mail_EmailProcessor
::processActivities();
372 return civicrm_api3_create_success($values, $params, 'mailing', 'activities');
374 catch (Exception
$e) {
376 return civicrm_api3_create_error('Process Activities failed');
381 * Process participant statuses.
383 * @param array $params
387 * array of properties, if error an array with an error id and error message
389 function civicrm_api3_job_process_participant($params) {
390 $result = CRM_Event_BAO_ParticipantStatusType
::process($params);
392 if (!$result['is_error']) {
393 return civicrm_api3_create_success(implode("\r\r", $result['messages']));
396 return civicrm_api3_create_error('Error while processing participant statuses');
402 * This api checks and updates the status of all membership records for a given domain.
404 * The function uses the calc_membership_status and update_contact_membership APIs.
407 * Sending renewal reminders has been migrated from this job to the Scheduled Reminders function as of 4.3.
409 * @param array $params
410 * Input parameters NOT USED.
413 * true if success, else false
415 function civicrm_api3_job_process_membership($params) {
416 $lock = new CRM_Core_Lock('civimail.job.updateMembership');
417 if (!$lock->isAcquired()) {
418 return civicrm_api3_create_error('Could not acquire lock, another Membership Processing process is running');
421 $result = CRM_Member_BAO_Membership
::updateAllMembershipStatus();
424 if ($result['is_error'] == 0) {
425 return civicrm_api3_create_success($result['messages'], $params);
428 return civicrm_api3_create_error($result['messages']);
433 * This api checks and updates the status of all survey respondents.
435 * @param array $params
436 * (reference ) input parameters.
439 * true if success, else false
441 function civicrm_api3_job_process_respondent($params) {
442 $result = CRM_Campaign_BAO_Survey
::releaseRespondent($params);
444 if ($result['is_error'] == 0) {
445 return civicrm_api3_create_success();
448 return civicrm_api3_create_error($result['messages']);
453 * Merges given pair of duplicate contacts.
455 * @param array $params
461 function civicrm_api3_job_process_batch_merge($params) {
462 $rgid = CRM_Utils_Array
::value('rgid', $params);
463 $gid = CRM_Utils_Array
::value('gid', $params);
465 $mode = CRM_Utils_Array
::value('mode', $params, 'safe');
466 $autoFlip = CRM_Utils_Array
::value('auto_flip', $params, TRUE);
468 $result = CRM_Dedupe_Merger
::batchMerge($rgid, $gid, $mode, $autoFlip);
470 if ($result['is_error'] == 0) {
471 return civicrm_api3_create_success();
474 return civicrm_api3_create_error($result['messages']);
479 * Metadata for batch merge function.
483 function _civicrm_api3_job_process_batch_merge_spec(&$params) {
484 $params['rgid'] = array(
485 'title' => 'rule group id',
486 'type' => CRM_Utils_Type
::T_INT
,
488 $params['gid'] = array(
489 'title' => 'group id',
490 'type' => CRM_Utils_Type
::T_INT
,
492 $params['mode'] = array(
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.',
495 'type' => CRM_Utils_Type
::T_STRING
,
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
,
505 * Runs handlePaymentCron method in the specified payment processor.
507 * @param array $params
510 * Expected @params array keys are: INCORRECTLY DOCUMENTED AND SHOULD BE IN THE _spec function
511 * for retrieval via getfields.
512 * {string 'processor_name' - the name of the payment processor, eg: Sagepay}
514 function civicrm_api3_job_run_payment_cron($params) {
517 CRM_Core_Payment
::handlePaymentMethod(
528 CRM_Core_Payment
::handlePaymentMethod(
540 * This api cleans up all the old session entries and temp tables.
542 * We recommend that sites run this on an hourly basis.
544 * @param array $params
545 * Sends in various config parameters to decide what needs to be cleaned.
547 function civicrm_api3_job_cleanup($params) {
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);
555 if ($session ||
$tempTable ||
$prevNext) {
556 CRM_Core_BAO_Cache
::cleanup($session, $tempTable, $prevNext);
560 CRM_Core_BAO_Job
::cleanup();
564 CRM_Core_Config
::clearDBCache();
568 CRM_Utils_System
::flushCache();
573 * Set expired relationships to disabled.
575 * @param array $params
578 * @throws \API_Exception
580 function civicrm_api3_job_disable_expired_relationships($params) {
581 $result = CRM_Contact_BAO_Relationship
::disableExpiredRelationships();
583 throw new API_Exception('Failed to disable all expired relationships.');
585 return civicrm_api3_create_success(1, $params);
589 * This api reloads all the smart groups.
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.
594 * Might also help to increase the smartGroupCacheTimeout and use the cache.
596 * @param array $params
599 * @throws \API_Exception
601 function civicrm_api3_job_group_rebuild($params) {
602 $lock = new CRM_Core_Lock('civimail.job.groupRebuild');
603 if (!$lock->isAcquired()) {
604 throw new API_Exception('Could not acquire lock, another EmailProcessor process is running');
607 $limit = CRM_Utils_Array
::value('limit', $params, 0);
609 CRM_Contact_BAO_GroupContactCache
::loadAll(NULL, $limit);
612 return civicrm_api3_create_success();