5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.3 |
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 require_once 'CRM/Utils/Rule.php';
76 if (isset($params['id']) && !CRM_Utils_Rule
::integer($params['id'])) {
77 return civicrm_api3_create_error('Invalid value for job ID');
80 $dao = CRM_Core_BAO_Job
::create($params);
83 _civicrm_api3_object_to_array($dao, $result[$dao->id
]);
84 return civicrm_api3_create_success($result, $params, 'job', 'create', $dao);
88 * Retrieve one or more job
89 * @param array input parameters
90 * @return array api result array
91 * {@getfields email_get}
94 function civicrm_api3_job_get($params) {
95 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
103 * @return array API Result Array
104 * {@getfields Job_delete}
108 function civicrm_api3_job_delete($params) {
109 require_once 'CRM/Utils/Rule.php';
110 if ($params['id'] != NULL && !CRM_Utils_Rule
::integer($params['id'])) {
111 return civicrm_api3_create_error('Invalid value for job ID');
114 $result = CRM_Core_BAO_Job
::del($params['id']);
116 return civicrm_api3_create_error('Could not delete job');
118 return civicrm_api3_create_success($result, $params, 'job', 'delete');
122 * Dumb wrapper to execute scheduled jobs. Always creates success - errors
123 * and results are handled in the job log.
125 * @param array $params (reference ) input parameters
127 * @return array API Result Array
133 function civicrm_api3_job_execute($params) {
134 require_once 'CRM/Core/JobManager.php';
135 $facility = new CRM_Core_JobManager();
136 $facility->execute(FALSE);
138 // always creates success - results are handled elsewhere
139 return civicrm_api3_create_success();
143 * Adjust Metadata for Execute action
145 * @param array $params array or parameters determined by getfields
147 function _civicrm_api3_job_execute_spec(&$params) {
151 * Geocode group of contacts based on given params
153 * @param array $params (reference ) input parameters
155 * @return array API Result Array
156 * {@getfields contact_geocode}
163 function civicrm_api3_job_geocode($params) {
165 require_once 'CRM/Utils/Address/BatchUpdate.php';
166 $gc = new CRM_Utils_Address_BatchUpdate($params);
169 $result = $gc->run();
171 if ($result['is_error'] == 0) {
172 return civicrm_api3_create_success($result['messages']);
175 return civicrm_api3_create_error($result['messages']);
179 * First check on Code documentation
181 function _civicrm_api3_job_geocode_spec(&$params) {
182 $params['start'] = array('title' => 'Start Date');
183 $params['end'] = array('title' => 'End Date');
184 $params['geocoding'] = array('title' => 'Is this for GeoCoding? (I think this is a 1,0 field?)');
185 $params['parse'] = array('title' => 'Is this for parsing? (I think this is a 1,0 field?)');
186 $params['throttle'] = array('title' => 'Throttle? (no idea what you enter in this field)');
190 * Send the scheduled reminders for all contacts (either for activities or events)
192 * @param array $params (reference ) input parameters
193 * now - the time to use, in YmdHis format
194 * - makes testing a bit simpler since we can simulate past/future time
196 * @return boolean true if success, else false
201 function civicrm_api3_job_send_reminder($params) {
202 require_once 'CRM/Core/BAO/ActionSchedule.php';
203 $result = CRM_Core_BAO_ActionSchedule
::processQueue(CRM_Utils_Array
::value('now', $params));
205 if ($result['is_error'] == 0) {
206 return civicrm_api3_create_success();
209 return civicrm_api3_create_error($result['messages']);
214 * Execute a specific report instance and send the output via email
216 * @param array $params (reference ) input parameters
217 * sendmail - Boolean - should email be sent?, required
218 * instanceId - Integer - the report instance ID
219 * resetVal - Integer - should we reset form state (always true)?
221 * @return boolean true if success, else false
226 function civicrm_api3_job_mail_report($params) {
228 $result = CRM_Report_Utils_Report
::processReport($params);
230 if ($result['is_error'] == 0) {
231 // this should be handling by throwing exceptions but can't remove until we can test that.
232 return civicrm_api3_create_success();
235 return civicrm_api3_create_error($result['messages']);
241 * This method allows to update Email Greetings, Postal Greetings and Addressee for a specific contact type.
242 * IMPORTANT: You must first create valid option value before using via admin interface.
243 * Check option lists for Email Greetings, Postal Greetings and Addressee
245 * id - Integer - greetings option group
247 * @return boolean true if success, else false
252 function civicrm_api3_job_update_greeting($params) {
254 if (isset($params['ct']) && isset($params['gt'])) {
256 $ct = explode(',', $params['ct']);
257 $gt = explode(',', $params['gt']);
258 foreach ($ct as $ctKey => $ctValue) {
259 foreach ($gt as $gtKey => $gtValue) {
260 $params['ct'] = trim($ctValue);
261 $params['gt'] = trim($gtValue);
262 $result[] = CRM_Contact_BAO_Contact_Utils
::updateGreeting($params);
267 $result = CRM_Contact_BAO_Contact_Utils
::updateGreeting($params);
270 foreach ($result as $resultKey => $resultValue) {
271 if ($resultValue['is_error'] == 0) {
272 //really we should rely on the exception mechanism here - but we need to test that before removing this line
273 return civicrm_api3_create_success();
276 return civicrm_api3_create_error($resultValue['messages']);
282 * Adjust Metadata for Get action
284 * The metadata is used for setting defaults, documentation & validation
285 * @param array $params array or parameters determined by getfields
287 function _civicrm_api3_job_update_greeting_spec(&$params) {
288 $params['ct'] = array(
290 'title' => 'Contact Type',
291 'type' => CRM_Utils_Type
::T_STRING
,
293 $params['gt'] = array(
295 'title' => 'Greeting Type',
296 'type' => CRM_Utils_Type
::T_STRING
,
301 * Mass update pledge statuses
303 * @param array $params (reference ) input parameters
305 * @return boolean true if success, else false
310 function 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);
315 if ($result['is_error'] == 0) {
316 // experiment: detailed execution log is a result here
317 return civicrm_api3_create_success($result['messages']);
320 return civicrm_api3_create_error($result['error_message']);
327 * @param array $params
331 function civicrm_api3_job_process_mailing($params) {
333 if (!CRM_Mailing_BAO_Mailing
::processQueue()) {
334 return civicrm_api3_create_error('Process Queue failed');
338 return civicrm_api3_create_success($values, $params, 'mailing', 'process');
345 * @param array $params
349 function civicrm_api3_job_process_sms($params) {
351 if (!CRM_Mailing_BAO_Mailing
::processQueue('sms')) {
352 return civicrm_api3_create_error('Process Queue failed');
356 return civicrm_api3_create_success($values, $params, 'mailing', 'process');
360 * Job to get mail responses from civimailing
362 function civicrm_api3_job_fetch_bounces($params) {
363 require_once 'CRM/Utils/Mail/EmailProcessor.php';
364 require_once 'CRM/Core/Lock.php';
365 $lock = new CRM_Core_Lock('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()) {
370 return civicrm_api3_create_error('Process Bounces failed');
372 // FIXME: processBounces doesn't return true/false on success/failure
375 return civicrm_api3_create_success($values, $params, 'mailing', 'bounces');
379 * Job to get mail and create activities
381 function civicrm_api3_job_fetch_activities($params) {
382 require_once 'CRM/Utils/Mail/EmailProcessor.php';
383 require_once 'CRM/Core/Lock.php';
384 $lock = new CRM_Core_Lock('EmailProcessor');
385 if (!$lock->isAcquired()) {
386 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
389 CRM_Utils_Mail_EmailProcessor
::processActivities();
392 return civicrm_api3_create_success($values, $params,'mailing','activities');
393 } catch (Exception
$e) {
395 return civicrm_api3_create_error('Process Activities failed');
400 * Process participant statuses
402 * @param array $params (reference ) input parameters
404 * @return array (reference ) array of properties, if error an array with an error id and error message
407 function civicrm_api3_job_process_participant($params) {
408 require_once 'CRM/Event/BAO/ParticipantStatusType.php';
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 require_once 'CRM/Member/BAO/Membership.php';
435 $result = CRM_Member_BAO_Membership
::updateAllMembershipStatus();
437 if ($result['is_error'] == 0) {
438 return civicrm_api3_create_success($result['messages']);
441 return civicrm_api3_create_error($result['messages']);
446 * This api checks and updates the status of all survey respondants.
448 * @param array $params (reference ) input parameters
450 * @return boolean true if success, else false
454 function civicrm_api3_job_process_respondent($params) {
455 require_once 'CRM/Campaign/BAO/Survey.php';
456 $result = CRM_Campaign_BAO_Survey
::releaseRespondent($params);
458 if ($result['is_error'] == 0) {
459 return civicrm_api3_create_success();
462 return civicrm_api3_create_error($result['messages']);
467 * Merges given pair of duplicate contacts.
469 * @param array $params input parameters
471 * Allowed @params array keys are:
472 * {int $rgid rule group id}
473 * {int $gid group id}
474 * {string mode helps decide how to behave when there are conflicts.
475 * A 'safe' value skips the merge if there are no conflicts. Does a force merge otherwise.}
476 * {boolean auto_flip wether to let api decide which contact to retain and which to delete.}
478 * @return array API Result Array
483 function civicrm_api3_job_process_batch_merge($params) {
484 $rgid = CRM_Utils_Array
::value('rgid', $params);
485 $gid = CRM_Utils_Array
::value('gid', $params);
487 $mode = CRM_Utils_Array
::value('mode', $params, 'safe');
488 $autoFlip = CRM_Utils_Array
::value('auto_flip', $params, TRUE);
490 require_once 'CRM/Dedupe/Merger.php';
491 $result = CRM_Dedupe_Merger
::batchMerge($rgid, $gid, $mode, $autoFlip);
493 if ($result['is_error'] == 0) {
494 return civicrm_api3_create_success();
497 return civicrm_api3_create_error($result['messages']);
502 * Runs handlePaymentCron method in the specified payment processor
504 * @param array $params input parameters
506 * Expected @params array keys are:
507 * {string 'processor_name' - the name of the payment processor, eg: Sagepay}
511 function civicrm_api3_job_run_payment_cron($params) {
513 require_once 'CRM/Core/Payment.php';
516 CRM_Core_Payment
::handlePaymentMethod(
527 CRM_Core_Payment
::handlePaymentMethod(
539 * This api cleans up all the old session entries and temp tables. We recommend that sites run this on an hourly basis
541 * @param array $params (reference ) - sends in various config parameters to decide what needs to be cleaned
543 * @return boolean true if success, else false
547 function civicrm_api3_job_cleanup( $params ) {
548 require_once 'CRM/Utils/Array.php';
550 $session = CRM_Utils_Array
::value( 'session' , $params, true );
551 $tempTable = CRM_Utils_Array
::value( 'tempTables', $params, true );
552 $jobLog = CRM_Utils_Array
::value( 'jobLog' , $params, true );
553 $prevNext = CRM_Utils_Array
::value( 'prevNext' , $params, true );
554 $dbCache = CRM_Utils_Array
::value( 'dbCache' , $params, false );
555 $memCache = CRM_Utils_Array
::value( 'memCache' , $params, false );
557 if ( $session ||
$tempTable ||
$prevNext ) {
558 require_once 'CRM/Core/BAO/Cache.php';
559 CRM_Core_BAO_Cache
::cleanup( $session, $tempTable, $prevNext );
563 CRM_Core_BAO_Job
::cleanup( );
567 CRM_Core_Config
::clearDBCache( );
571 CRM_Utils_System
::flushCache( );
576 * Set expired relationships to disabled.
579 function civicrm_api3_job_disable_expired_relationships($params) {
580 $result = CRM_Contact_BAO_Relationship
::disableExpiredRelationships();
582 return civicrm_api3_create_success();
585 return civicrm_api3_create_error('Failed to disable all expired relationships.');
590 * This api reloads all the smart groups. If the org has a large number of smart groups
591 * it is recommended that they use the limit clause to limit the number of smart groups
592 * evaluated on a per job basis. Might also help to increase the smartGroupCacheTimeout
595 function civicrm_api3_job_group_rebuild( $params ) {
596 $limit = CRM_Utils_Array
::value( 'limit', $params, 0 );
598 CRM_Contact_BAO_GroupContactCache
::loadAll(null, $limit);