5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.4 |
7 +--------------------------------------------------------------------+
8 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
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 * @return array API Result Array
94 * {@getfields Job_delete}
98 function civicrm_api3_job_delete($params) {
99 if ($params['id'] != NULL && !CRM_Utils_Rule
::integer($params['id'])) {
100 return civicrm_api3_create_error('Invalid value for job ID');
103 $result = CRM_Core_BAO_Job
::del($params['id']);
105 return civicrm_api3_create_error('Could not delete job');
107 return civicrm_api3_create_success($result, $params, 'job', 'delete');
111 * Dumb wrapper to execute scheduled jobs. Always creates success - errors
112 * and results are handled in the job log.
114 * @param array $params (reference ) input parameters
116 * @return array API Result Array
122 function civicrm_api3_job_execute($params) {
123 $facility = new CRM_Core_JobManager();
124 $facility->execute(FALSE);
126 // always creates success - results are handled elsewhere
127 return civicrm_api3_create_success();
131 * Adjust Metadata for Execute action
133 * @param array $params array or parameters determined by getfields
135 function _civicrm_api3_job_execute_spec(&$params) {
139 * Geocode group of contacts based on given params
141 * @param array $params (reference ) input parameters
143 * @return array API Result Array
144 * {@getfields contact_geocode}
151 function civicrm_api3_job_geocode($params) {
152 $gc = new CRM_Utils_Address_BatchUpdate($params);
155 $result = $gc->run();
157 if ($result['is_error'] == 0) {
158 return civicrm_api3_create_success($result['messages']);
161 return civicrm_api3_create_error($result['messages']);
165 * First check on Code documentation
167 function _civicrm_api3_job_geocode_spec(&$params) {
168 $params['start'] = array('title' => 'Start Date');
169 $params['end'] = array('title' => 'End Date');
170 $params['geocoding'] = array('title' => 'Geocode address?');
171 $params['parse'] = array('title' => 'Parse street address?');
172 $params['throttle'] = array('title' => 'Throttle? if enabled, geocodes at a slow rate');
176 * Send the scheduled reminders for all contacts (either for activities or events)
178 * @param array $params (reference ) input parameters
179 * now - the time to use, in YmdHis format
180 * - makes testing a bit simpler since we can simulate past/future time
182 * @return boolean true if success, else false
187 function civicrm_api3_job_send_reminder($params) {
188 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
189 if (!$lock->isAcquired()) {
190 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
193 $result = CRM_Core_BAO_ActionSchedule
::processQueue(CRM_Utils_Array
::value('now', $params));
196 if ($result['is_error'] == 0) {
197 return civicrm_api3_create_success();
200 return civicrm_api3_create_error($result['messages']);
205 * Execute a specific report instance and send the output via email
207 * @param array $params (reference ) input parameters
208 * sendmail - Boolean - should email be sent?, required
209 * instanceId - Integer - the report instance ID
210 * resetVal - Integer - should we reset form state (always true)?
212 * @return boolean true if success, else false
217 function civicrm_api3_job_mail_report($params) {
218 $result = CRM_Report_Utils_Report
::processReport($params);
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();
225 return civicrm_api3_create_error($result['messages']);
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
235 * id - Integer - greetings option group
237 * @return boolean true if success, else false
242 function civicrm_api3_job_update_greeting($params) {
244 if (isset($params['ct']) && isset($params['gt'])) {
246 $ct = explode(',', $params['ct']);
247 $gt = explode(',', $params['gt']);
248 foreach ($ct as $ctKey => $ctValue) {
249 foreach ($gt as $gtKey => $gtValue) {
250 $params['ct'] = trim($ctValue);
251 $params['gt'] = trim($gtValue);
252 $result[] = CRM_Contact_BAO_Contact_Utils
::updateGreeting($params);
257 $result = CRM_Contact_BAO_Contact_Utils
::updateGreeting($params);
260 foreach ($result as $resultKey => $resultValue) {
261 if ($resultValue['is_error'] == 0) {
262 //really we should rely on the exception mechanism here - but we need to test that before removing this line
263 return civicrm_api3_create_success();
266 return civicrm_api3_create_error($resultValue['messages']);
272 * Adjust Metadata for Get action
274 * The metadata is used for setting defaults, documentation & validation
275 * @param array $params array or parameters determined by getfields
277 function _civicrm_api3_job_update_greeting_spec(&$params) {
278 $params['ct'] = array(
280 'title' => 'Contact Type',
281 'type' => CRM_Utils_Type
::T_STRING
,
283 $params['gt'] = array(
285 'title' => 'Greeting Type',
286 'type' => CRM_Utils_Type
::T_STRING
,
291 * Mass update pledge statuses
293 * @param array $params (reference ) input parameters
295 * @return boolean true if success, else false
300 function civicrm_api3_job_process_pledge($params) {
301 // *** Uncomment the next line if you want automated reminders to be sent
302 // $params['send_reminders'] = true;
303 $result = CRM_Pledge_BAO_Pledge
::updatePledgeStatus($params);
305 if ($result['is_error'] == 0) {
306 // experiment: detailed execution log is a result here
307 return civicrm_api3_create_success($result['messages']);
310 return civicrm_api3_create_error($result['error_message']);
317 * @param array $params
321 function civicrm_api3_job_process_mailing($params) {
323 if (!CRM_Mailing_BAO_Mailing
::processQueue()) {
324 return civicrm_api3_create_error('Process Queue failed');
328 return civicrm_api3_create_success($values, $params, 'mailing', 'process');
335 * @param array $params
339 function civicrm_api3_job_process_sms($params) {
340 if (!CRM_Mailing_BAO_Mailing
::processQueue('sms')) {
341 return civicrm_api3_create_error('Process Queue failed');
345 return civicrm_api3_create_success($values, $params, 'mailing', 'process');
350 * Job to get mail responses from civimailing
352 function civicrm_api3_job_fetch_bounces($params) {
353 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
354 if (!$lock->isAcquired()) {
355 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
357 if (!CRM_Utils_Mail_EmailProcessor
::processBounces()) {
359 return civicrm_api3_create_error('Process Bounces failed');
363 // FIXME: processBounces doesn't return true/false on success/failure
365 return civicrm_api3_create_success($values, $params, 'mailing', 'bounces');
369 * Job to get mail and create activities
371 function civicrm_api3_job_fetch_activities($params) {
372 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
373 if (!$lock->isAcquired()) {
374 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
378 CRM_Utils_Mail_EmailProcessor
::processActivities();
381 return civicrm_api3_create_success($values, $params,'mailing','activities');
382 } catch (Exception
$e) {
384 return civicrm_api3_create_error('Process Activities failed');
389 * Process participant statuses
391 * @param array $params (reference ) input parameters
393 * @return array (reference ) array of properties, if error an array with an error id and error message
396 function civicrm_api3_job_process_participant($params) {
397 $result = CRM_Event_BAO_ParticipantStatusType
::process($params);
399 if (!$result['is_error']) {
400 return civicrm_api3_create_success(implode("\r\r", $result['messages']));
403 return civicrm_api3_create_error('Error while processing participant statuses');
409 * This api checks and updates the status of all membership records for a given domain using the calc_membership_status and
410 * update_contact_membership APIs.
413 * Sending renewal reminders has been migrated from this job to the Scheduled Reminders function as of 4.3.
415 * @param array $params input parameters NOT USED
417 * @return boolean true if success, else false
421 function civicrm_api3_job_process_membership($params) {
422 $lock = new CRM_Core_Lock('civimail.job.updateMembership');
423 if (!$lock->isAcquired()) {
424 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
427 $result = CRM_Member_BAO_Membership
::updateAllMembershipStatus();
430 if ($result['is_error'] == 0) {
431 return civicrm_api3_create_success($result['messages']);
434 return civicrm_api3_create_error($result['messages']);
439 * This api checks and updates the status of all survey respondants.
441 * @param array $params (reference ) input parameters
443 * @return boolean true if success, else false
447 function civicrm_api3_job_process_respondent($params) {
448 $result = CRM_Campaign_BAO_Survey
::releaseRespondent($params);
450 if ($result['is_error'] == 0) {
451 return civicrm_api3_create_success();
454 return civicrm_api3_create_error($result['messages']);
459 * Merges given pair of duplicate contacts.
461 * @param array $params input parameters
463 * Allowed @params array keys are:
464 * {int $rgid rule group id}
465 * {int $gid group id}
466 * {string mode helps decide how to behave when there are conflicts.
467 * A 'safe' value skips the merge if there are no conflicts. Does a force merge otherwise.}
468 * {boolean auto_flip wether to let api decide which contact to retain and which to delete.}
470 * @return array API Result Array
475 function civicrm_api3_job_process_batch_merge($params) {
476 $rgid = CRM_Utils_Array
::value('rgid', $params);
477 $gid = CRM_Utils_Array
::value('gid', $params);
479 $mode = CRM_Utils_Array
::value('mode', $params, 'safe');
480 $autoFlip = CRM_Utils_Array
::value('auto_flip', $params, TRUE);
482 $result = CRM_Dedupe_Merger
::batchMerge($rgid, $gid, $mode, $autoFlip);
484 if ($result['is_error'] == 0) {
485 return civicrm_api3_create_success();
488 return civicrm_api3_create_error($result['messages']);
493 * Runs handlePaymentCron method in the specified payment processor
495 * @param array $params input parameters
497 * Expected @params array keys are:
498 * {string 'processor_name' - the name of the payment processor, eg: Sagepay}
502 function civicrm_api3_job_run_payment_cron($params) {
505 CRM_Core_Payment
::handlePaymentMethod(
516 CRM_Core_Payment
::handlePaymentMethod(
528 * This api cleans up all the old session entries and temp tables. We recommend that sites run this on an hourly basis
530 * @param array $params (reference ) - sends in various config parameters to decide what needs to be cleaned
532 * @return boolean true if success, else false
536 function civicrm_api3_job_cleanup( $params ) {
537 $session = CRM_Utils_Array
::value( 'session' , $params, true );
538 $tempTable = CRM_Utils_Array
::value( 'tempTables', $params, true );
539 $jobLog = CRM_Utils_Array
::value( 'jobLog' , $params, true );
540 $prevNext = CRM_Utils_Array
::value( 'prevNext' , $params, true );
541 $dbCache = CRM_Utils_Array
::value( 'dbCache' , $params, false );
542 $memCache = CRM_Utils_Array
::value( 'memCache' , $params, false );
544 if ( $session ||
$tempTable ||
$prevNext ) {
545 CRM_Core_BAO_Cache
::cleanup( $session, $tempTable, $prevNext );
549 CRM_Core_BAO_Job
::cleanup( );
553 CRM_Core_Config
::clearDBCache( );
557 CRM_Utils_System
::flushCache( );
562 * Set expired relationships to disabled.
565 function civicrm_api3_job_disable_expired_relationships($params) {
566 $result = CRM_Contact_BAO_Relationship
::disableExpiredRelationships();
568 return civicrm_api3_create_success();
571 return civicrm_api3_create_error('Failed to disable all expired relationships.');
576 * This api reloads all the smart groups. If the org has a large number of smart groups
577 * it is recommended that they use the limit clause to limit the number of smart groups
578 * evaluated on a per job basis. Might also help to increase the smartGroupCacheTimeout
581 function civicrm_api3_job_group_rebuild( $params ) {
582 $lock = new CRM_Core_Lock('civimail.job.groupRebuild');
583 if (!$lock->isAcquired()) {
584 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
587 $limit = CRM_Utils_Array
::value( 'limit', $params, 0 );
589 CRM_Contact_BAO_GroupContactCache
::loadAll(null, $limit);
592 return civicrm_api3_create_success();