5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.5 |
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
35 * @package CiviCRM_APIv3
37 * @copyright CiviCRM LLC (c) 2004-2014
38 * $Id: Contact.php 30879 2010-11-22 15:45:55Z shot $
43 * Include common API util functions
45 require_once 'api/v3/utils.php';
48 * Adjust metadata for "Create" action
50 * The metadata is used for setting defaults, documentation & validation
51 * @param array $params array or parameters determined by getfields
53 function _civicrm_api3_job_create_spec(&$params) {
54 $params['run_frequency']['api.required'] = 1;
55 $params['name']['api.required'] = 1;
56 $params['api_entity']['api.required'] = 1;
57 $params['api_action']['api.required'] = 1;
59 $params['domain_id']['api.default'] = CRM_Core_Config
::domainID();
60 $params['is_active']['api.default'] = 1;
64 * Function to create scheduled job
66 * @param array $params Associative array of property name/value pairs to insert in new job.
68 * @return success or error
69 * {@getfields Job_create}
71 * {@schema Core/Job.xml}
73 function civicrm_api3_job_create($params) {
74 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
78 * Retrieve one or more job
79 * @param array input parameters
80 * @return array api result array
81 * {@getfields email_get}
84 function civicrm_api3_job_get($params) {
85 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
93 * @internal param int $id
95 * @return array API Result Array
96 * {@getfields Job_delete}
100 function civicrm_api3_job_delete($params) {
101 _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
105 * Dumb wrapper to execute scheduled jobs. Always creates success - errors
106 * and results are handled in the job log.
108 * @param array $params (reference ) input parameters
110 * @return array API Result Array
116 function civicrm_api3_job_execute($params) {
117 $facility = new CRM_Core_JobManager();
118 $facility->execute(FALSE);
120 // always creates success - results are handled elsewhere
121 return civicrm_api3_create_success();
125 * Adjust Metadata for Execute action
127 * @param array $params array or parameters determined by getfields
129 function _civicrm_api3_job_execute_spec(&$params) {
133 * Geocode group of contacts based on given params
135 * @param array $params (reference ) input parameters
137 * @return array API Result Array
138 * {@getfields contact_geocode}
145 function civicrm_api3_job_geocode($params) {
146 $gc = new CRM_Utils_Address_BatchUpdate($params);
149 $result = $gc->run();
151 if ($result['is_error'] == 0) {
152 return civicrm_api3_create_success($result['messages']);
155 return civicrm_api3_create_error($result['messages']);
159 * First check on Code documentation
161 function _civicrm_api3_job_geocode_spec(&$params) {
162 $params['start'] = array('title' => 'Start Date');
163 $params['end'] = array('title' => 'End Date');
164 $params['geocoding'] = array('title' => 'Geocode address?');
165 $params['parse'] = array('title' => 'Parse street address?');
166 $params['throttle'] = array('title' => 'Throttle? if enabled, geocodes at a slow rate');
170 * Send the scheduled reminders for all contacts (either for activities or events)
172 * @param array $params (reference ) input parameters
173 * now - the time to use, in YmdHis format
174 * - makes testing a bit simpler since we can simulate past/future time
176 * @return boolean true if success, else false
181 function civicrm_api3_job_send_reminder($params) {
182 //note that $params['rowCount' can be overridden by one of the preferred syntaxes ($options['limit'] = x
183 //It's not clear whether than syntax can be passed in via the UI config - but this keeps the pre 4.4.4 behaviour
184 // in that case (ie. makes it unconfigurable via the UI). Another approach would be to set a default of 0
185 // in the _spec function - but since that is a deprecated value it seems more contentious than this approach
186 $params['rowCount'] = 0;
187 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
188 if (!$lock->isAcquired()) {
189 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
192 $result = CRM_Core_BAO_ActionSchedule
::processQueue(CRM_Utils_Array
::value('now', $params), $params);
195 if ($result['is_error'] == 0) {
196 return civicrm_api3_create_success();
199 return civicrm_api3_create_error($result['messages']);
203 * Adjust metadata for "send_reminder" action
205 * The metadata is used for setting defaults, documentation & validation
206 * @param array $params array or parameters determined by getfields
208 function _civicrm_api3_job_send_reminder(&$params) {
209 //@todo this function will now take all fields in action_schedule as params
210 // as it is calling the api fn to set the filters - update getfields to reflect
211 $params['id'] = array(
212 'type' => CRM_Utils_Type
::T_INT
,
213 'title' => 'Action Schedule ID'
217 * Execute a specific report instance and send the output via email
219 * @param array $params (reference ) input parameters
220 * sendmail - Boolean - should email be sent?, required
221 * instanceId - Integer - the report instance ID
222 * resetVal - Integer - should we reset form state (always true)?
224 * @return boolean true if success, else false
229 function civicrm_api3_job_mail_report($params) {
230 $result = CRM_Report_Utils_Report
::processReport($params);
232 if ($result['is_error'] == 0) {
233 // this should be handling by throwing exceptions but can't remove until we can test that.
234 return civicrm_api3_create_success();
237 return civicrm_api3_create_error($result['messages']);
243 * This method allows to update Email Greetings, Postal Greetings and Addressee for a specific contact type.
244 * IMPORTANT: You must first create valid option value before using via admin interface.
245 * Check option lists for Email Greetings, Postal Greetings and Addressee
247 * id - Integer - greetings option group
249 * @return boolean true if success, else false
254 function civicrm_api3_job_update_greeting($params) {
256 if (isset($params['ct']) && isset($params['gt'])) {
258 $ct = explode(',', $params['ct']);
259 $gt = explode(',', $params['gt']);
260 foreach ($ct as $ctKey => $ctValue) {
261 foreach ($gt as $gtKey => $gtValue) {
262 $params['ct'] = trim($ctValue);
263 $params['gt'] = trim($gtValue);
264 $result[] = CRM_Contact_BAO_Contact_Utils
::updateGreeting($params);
269 $result = CRM_Contact_BAO_Contact_Utils
::updateGreeting($params);
272 foreach ($result as $resultKey => $resultValue) {
273 if ($resultValue['is_error'] == 0) {
274 //really we should rely on the exception mechanism here - but we need to test that before removing this line
275 return civicrm_api3_create_success();
278 return civicrm_api3_create_error($resultValue['messages']);
284 * Adjust Metadata for Get action
286 * The metadata is used for setting defaults, documentation & validation
287 * @param array $params array or parameters determined by getfields
289 function _civicrm_api3_job_update_greeting_spec(&$params) {
290 $params['ct'] = array(
292 'title' => 'Contact Type',
293 'type' => CRM_Utils_Type
::T_STRING
,
295 $params['gt'] = array(
297 'title' => 'Greeting Type',
298 'type' => CRM_Utils_Type
::T_STRING
,
303 * Mass update pledge statuses
305 * @param array $params (reference ) input parameters
307 * @return boolean true if success, else false
312 function civicrm_api3_job_process_pledge($params) {
313 // *** Uncomment the next line if you want automated reminders to be sent
314 // $params['send_reminders'] = true;
315 $result = CRM_Pledge_BAO_Pledge
::updatePledgeStatus($params);
317 if ($result['is_error'] == 0) {
318 // experiment: detailed execution log is a result here
319 return civicrm_api3_create_success($result['messages']);
322 return civicrm_api3_create_error($result['error_message']);
329 * @param array $params
333 function civicrm_api3_job_process_mailing($params) {
335 if (!CRM_Mailing_BAO_Mailing
::processQueue()) {
336 return civicrm_api3_create_error('Process Queue failed');
340 return civicrm_api3_create_success($values, $params, 'mailing', 'process');
347 * @param array $params
351 function civicrm_api3_job_process_sms($params) {
352 if (!CRM_Mailing_BAO_Mailing
::processQueue('sms')) {
353 return civicrm_api3_create_error('Process Queue failed');
357 return civicrm_api3_create_success($values, $params, 'mailing', 'process');
362 * Job to get mail responses from civimailing
364 function civicrm_api3_job_fetch_bounces($params) {
365 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
366 if (!$lock->isAcquired()) {
367 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
369 if (!CRM_Utils_Mail_EmailProcessor
::processBounces()) {
371 return civicrm_api3_create_error('Process Bounces failed');
375 // FIXME: processBounces doesn't return true/false on success/failure
377 return civicrm_api3_create_success($values, $params, 'mailing', 'bounces');
381 * Job to get mail and create activities
383 function civicrm_api3_job_fetch_activities($params) {
384 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
385 if (!$lock->isAcquired()) {
386 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
390 CRM_Utils_Mail_EmailProcessor
::processActivities();
393 return civicrm_api3_create_success($values, $params,'mailing','activities');
394 } catch (Exception
$e) {
396 return civicrm_api3_create_error('Process Activities failed');
401 * Process participant statuses
403 * @param array $params (reference ) input parameters
405 * @return array (reference ) array of properties, if error an array with an error id and error message
408 function civicrm_api3_job_process_participant($params) {
409 $result = CRM_Event_BAO_ParticipantStatusType
::process($params);
411 if (!$result['is_error']) {
412 return civicrm_api3_create_success(implode("\r\r", $result['messages']));
415 return civicrm_api3_create_error('Error while processing participant statuses');
421 * This api checks and updates the status of all membership records for a given domain using the calc_membership_status and
422 * update_contact_membership APIs.
425 * Sending renewal reminders has been migrated from this job to the Scheduled Reminders function as of 4.3.
427 * @param array $params input parameters NOT USED
429 * @return boolean true if success, else false
433 function civicrm_api3_job_process_membership($params) {
434 $lock = new CRM_Core_Lock('civimail.job.updateMembership');
435 if (!$lock->isAcquired()) {
436 return civicrm_api3_create_error('Could not acquire lock, another Membership Processing process is running');
439 $result = CRM_Member_BAO_Membership
::updateAllMembershipStatus();
442 if ($result['is_error'] == 0) {
443 return civicrm_api3_create_success($result['messages']);
446 return civicrm_api3_create_error($result['messages']);
451 * This api checks and updates the status of all survey respondants.
453 * @param array $params (reference ) input parameters
455 * @return boolean true if success, else false
459 function civicrm_api3_job_process_respondent($params) {
460 $result = CRM_Campaign_BAO_Survey
::releaseRespondent($params);
462 if ($result['is_error'] == 0) {
463 return civicrm_api3_create_success();
466 return civicrm_api3_create_error($result['messages']);
471 * Merges given pair of duplicate contacts.
473 * @param array $params input parameters
475 * Allowed @params array keys are:
476 * {int $rgid rule group id}
477 * {int $gid group id}
478 * {string mode helps decide how to behave when there are conflicts.
479 * A 'safe' value skips the merge if there are no conflicts. Does a force merge otherwise.}
480 * {boolean auto_flip wether to let api decide which contact to retain and which to delete.}
482 * @return array API Result Array
487 function civicrm_api3_job_process_batch_merge($params) {
488 $rgid = CRM_Utils_Array
::value('rgid', $params);
489 $gid = CRM_Utils_Array
::value('gid', $params);
491 $mode = CRM_Utils_Array
::value('mode', $params, 'safe');
492 $autoFlip = CRM_Utils_Array
::value('auto_flip', $params, TRUE);
494 $result = CRM_Dedupe_Merger
::batchMerge($rgid, $gid, $mode, $autoFlip);
496 if ($result['is_error'] == 0) {
497 return civicrm_api3_create_success();
500 return civicrm_api3_create_error($result['messages']);
505 * Runs handlePaymentCron method in the specified payment processor
507 * @param array $params input parameters
509 * Expected @params array keys are:
510 * {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. We recommend that sites run this on an hourly basis
542 * @param array $params (reference ) - sends in various config parameters to decide what needs to be cleaned
544 * @return boolean true if success, else false
548 function civicrm_api3_job_cleanup( $params ) {
549 $session = CRM_Utils_Array
::value( 'session' , $params, true );
550 $tempTable = CRM_Utils_Array
::value( 'tempTables', $params, true );
551 $jobLog = CRM_Utils_Array
::value( 'jobLog' , $params, true );
552 $prevNext = CRM_Utils_Array
::value( 'prevNext' , $params, true );
553 $dbCache = CRM_Utils_Array
::value( 'dbCache' , $params, false );
554 $memCache = CRM_Utils_Array
::value( 'memCache' , $params, false );
556 if ( $session ||
$tempTable ||
$prevNext ) {
557 CRM_Core_BAO_Cache
::cleanup( $session, $tempTable, $prevNext );
561 CRM_Core_BAO_Job
::cleanup( );
565 CRM_Core_Config
::clearDBCache( );
569 CRM_Utils_System
::flushCache( );
574 * Set expired relationships to disabled.
577 function civicrm_api3_job_disable_expired_relationships($params) {
578 $result = CRM_Contact_BAO_Relationship
::disableExpiredRelationships();
580 return civicrm_api3_create_success();
583 return civicrm_api3_create_error('Failed to disable all expired relationships.');
588 * This api reloads all the smart groups. If the org has a large number of smart groups
589 * it is recommended that they use the limit clause to limit the number of smart groups
590 * evaluated on a per job basis. Might also help to increase the smartGroupCacheTimeout
593 function civicrm_api3_job_group_rebuild( $params ) {
594 $lock = new CRM_Core_Lock('civimail.job.groupRebuild');
595 if (!$lock->isAcquired()) {
596 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
599 $limit = CRM_Utils_Array
::value( 'limit', $params, 0 );
601 CRM_Contact_BAO_GroupContactCache
::loadAll(null, $limit);
604 return civicrm_api3_create_success();