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 if (isset($params['id']) && !CRM_Utils_Rule
::integer($params['id'])) {
75 return civicrm_api3_create_error('Invalid value for job ID');
78 $dao = CRM_Core_BAO_Job
::create($params);
81 _civicrm_api3_object_to_array($dao, $result[$dao->id
]);
82 return civicrm_api3_create_success($result, $params, 'job', 'create', $dao);
86 * Retrieve one or more job
87 * @param array input parameters
88 * @return array api result array
89 * {@getfields email_get}
92 function civicrm_api3_job_get($params) {
93 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
101 * @return array API Result Array
102 * {@getfields Job_delete}
106 function civicrm_api3_job_delete($params) {
107 if ($params['id'] != NULL && !CRM_Utils_Rule
::integer($params['id'])) {
108 return civicrm_api3_create_error('Invalid value for job ID');
111 $result = CRM_Core_BAO_Job
::del($params['id']);
113 return civicrm_api3_create_error('Could not delete job');
115 return civicrm_api3_create_success($result, $params, 'job', 'delete');
119 * Dumb wrapper to execute scheduled jobs. Always creates success - errors
120 * and results are handled in the job log.
122 * @param array $params (reference ) input parameters
124 * @return array API Result Array
130 function civicrm_api3_job_execute($params) {
131 $facility = new CRM_Core_JobManager();
132 $facility->execute(FALSE);
134 // always creates success - results are handled elsewhere
135 return civicrm_api3_create_success();
139 * Adjust Metadata for Execute action
141 * @param array $params array or parameters determined by getfields
143 function _civicrm_api3_job_execute_spec(&$params) {
147 * Geocode group of contacts based on given params
149 * @param array $params (reference ) input parameters
151 * @return array API Result Array
152 * {@getfields contact_geocode}
159 function civicrm_api3_job_geocode($params) {
160 $gc = new CRM_Utils_Address_BatchUpdate($params);
163 $result = $gc->run();
165 if ($result['is_error'] == 0) {
166 return civicrm_api3_create_success($result['messages']);
169 return civicrm_api3_create_error($result['messages']);
173 * First check on Code documentation
175 function _civicrm_api3_job_geocode_spec(&$params) {
176 $params['start'] = array('title' => 'Start Date');
177 $params['end'] = array('title' => 'End Date');
178 $params['geocoding'] = array('title' => 'Is this for GeoCoding? (I think this is a 1,0 field?)');
179 $params['parse'] = array('title' => 'Is this for parsing? (I think this is a 1,0 field?)');
180 $params['throttle'] = array('title' => 'Throttle? (no idea what you enter in this field)');
184 * Send the scheduled reminders for all contacts (either for activities or events)
186 * @param array $params (reference ) input parameters
187 * now - the time to use, in YmdHis format
188 * - makes testing a bit simpler since we can simulate past/future time
190 * @return boolean true if success, else false
195 function civicrm_api3_job_send_reminder($params) {
196 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
197 if (!$lock->isAcquired()) {
198 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
201 $result = CRM_Core_BAO_ActionSchedule
::processQueue(CRM_Utils_Array
::value('now', $params));
204 if ($result['is_error'] == 0) {
205 return civicrm_api3_create_success();
208 return civicrm_api3_create_error($result['messages']);
213 * Execute a specific report instance and send the output via email
215 * @param array $params (reference ) input parameters
216 * sendmail - Boolean - should email be sent?, required
217 * instanceId - Integer - the report instance ID
218 * resetVal - Integer - should we reset form state (always true)?
220 * @return boolean true if success, else false
225 function civicrm_api3_job_mail_report($params) {
226 $result = CRM_Report_Utils_Report
::processReport($params);
228 if ($result['is_error'] == 0) {
229 // this should be handling by throwing exceptions but can't remove until we can test that.
230 return civicrm_api3_create_success();
233 return civicrm_api3_create_error($result['messages']);
239 * This method allows to update Email Greetings, Postal Greetings and Addressee for a specific contact type.
240 * IMPORTANT: You must first create valid option value before using via admin interface.
241 * Check option lists for Email Greetings, Postal Greetings and Addressee
243 * id - Integer - greetings option group
245 * @return boolean true if success, else false
250 function civicrm_api3_job_update_greeting($params) {
252 if (isset($params['ct']) && isset($params['gt'])) {
254 $ct = explode(',', $params['ct']);
255 $gt = explode(',', $params['gt']);
256 foreach ($ct as $ctKey => $ctValue) {
257 foreach ($gt as $gtKey => $gtValue) {
258 $params['ct'] = trim($ctValue);
259 $params['gt'] = trim($gtValue);
260 $result[] = CRM_Contact_BAO_Contact_Utils
::updateGreeting($params);
265 $result = CRM_Contact_BAO_Contact_Utils
::updateGreeting($params);
268 foreach ($result as $resultKey => $resultValue) {
269 if ($resultValue['is_error'] == 0) {
270 //really we should rely on the exception mechanism here - but we need to test that before removing this line
271 return civicrm_api3_create_success();
274 return civicrm_api3_create_error($resultValue['messages']);
280 * Adjust Metadata for Get action
282 * The metadata is used for setting defaults, documentation & validation
283 * @param array $params array or parameters determined by getfields
285 function _civicrm_api3_job_update_greeting_spec(&$params) {
286 $params['ct'] = array(
288 'title' => 'Contact Type',
289 'type' => CRM_Utils_Type
::T_STRING
,
291 $params['gt'] = array(
293 'title' => 'Greeting Type',
294 'type' => CRM_Utils_Type
::T_STRING
,
299 * Mass update pledge statuses
301 * @param array $params (reference ) input parameters
303 * @return boolean true if success, else false
308 function civicrm_api3_job_process_pledge($params) {
309 // *** Uncomment the next line if you want automated reminders to be sent
310 // $params['send_reminders'] = true;
311 $result = CRM_Pledge_BAO_Pledge
::updatePledgeStatus($params);
313 if ($result['is_error'] == 0) {
314 // experiment: detailed execution log is a result here
315 return civicrm_api3_create_success($result['messages']);
318 return civicrm_api3_create_error($result['error_message']);
325 * @param array $params
329 function civicrm_api3_job_process_mailing($params) {
331 if (!CRM_Mailing_BAO_Mailing
::processQueue()) {
332 return civicrm_api3_create_error('Process Queue failed');
336 return civicrm_api3_create_success($values, $params, 'mailing', 'process');
343 * @param array $params
347 function civicrm_api3_job_process_sms($params) {
348 if (!CRM_Mailing_BAO_Mailing
::processQueue('sms')) {
349 return civicrm_api3_create_error('Process Queue failed');
353 return civicrm_api3_create_success($values, $params, 'mailing', 'process');
358 * Job to get mail responses from civimailing
360 function civicrm_api3_job_fetch_bounces($params) {
361 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
362 if (!$lock->isAcquired()) {
363 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
365 if (!CRM_Utils_Mail_EmailProcessor
::processBounces()) {
367 return civicrm_api3_create_error('Process Bounces failed');
371 // FIXME: processBounces doesn't return true/false on success/failure
373 return civicrm_api3_create_success($values, $params, 'mailing', 'bounces');
377 * Job to get mail and create activities
379 function civicrm_api3_job_fetch_activities($params) {
380 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
381 if (!$lock->isAcquired()) {
382 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
386 CRM_Utils_Mail_EmailProcessor
::processActivities();
389 return civicrm_api3_create_success($values, $params,'mailing','activities');
390 } catch (Exception
$e) {
392 return civicrm_api3_create_error('Process Activities failed');
397 * Process participant statuses
399 * @param array $params (reference ) input parameters
401 * @return array (reference ) array of properties, if error an array with an error id and error message
404 function civicrm_api3_job_process_participant($params) {
405 $result = CRM_Event_BAO_ParticipantStatusType
::process($params);
407 if (!$result['is_error']) {
408 return civicrm_api3_create_success(implode("\r\r", $result['messages']));
411 return civicrm_api3_create_error('Error while processing participant statuses');
417 * This api checks and updates the status of all membership records for a given domain using the calc_membership_status and
418 * update_contact_membership APIs.
421 * Sending renewal reminders has been migrated from this job to the Scheduled Reminders function as of 4.3.
423 * @param array $params input parameters NOT USED
425 * @return boolean true if success, else false
429 function civicrm_api3_job_process_membership($params) {
430 $lock = new CRM_Core_Lock('civimail.job.updateMembership');
431 if (!$lock->isAcquired()) {
432 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
435 $result = CRM_Member_BAO_Membership
::updateAllMembershipStatus();
438 if ($result['is_error'] == 0) {
439 return civicrm_api3_create_success($result['messages']);
442 return civicrm_api3_create_error($result['messages']);
447 * This api checks and updates the status of all survey respondants.
449 * @param array $params (reference ) input parameters
451 * @return boolean true if success, else false
455 function civicrm_api3_job_process_respondent($params) {
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 $result = CRM_Dedupe_Merger
::batchMerge($rgid, $gid, $mode, $autoFlip);
492 if ($result['is_error'] == 0) {
493 return civicrm_api3_create_success();
496 return civicrm_api3_create_error($result['messages']);
501 * Runs handlePaymentCron method in the specified payment processor
503 * @param array $params input parameters
505 * Expected @params array keys are:
506 * {string 'processor_name' - the name of the payment processor, eg: Sagepay}
510 function civicrm_api3_job_run_payment_cron($params) {
513 CRM_Core_Payment
::handlePaymentMethod(
524 CRM_Core_Payment
::handlePaymentMethod(
536 * This api cleans up all the old session entries and temp tables. We recommend that sites run this on an hourly basis
538 * @param array $params (reference ) - sends in various config parameters to decide what needs to be cleaned
540 * @return boolean true if success, else false
544 function civicrm_api3_job_cleanup( $params ) {
545 $session = CRM_Utils_Array
::value( 'session' , $params, true );
546 $tempTable = CRM_Utils_Array
::value( 'tempTables', $params, true );
547 $jobLog = CRM_Utils_Array
::value( 'jobLog' , $params, true );
548 $prevNext = CRM_Utils_Array
::value( 'prevNext' , $params, true );
549 $dbCache = CRM_Utils_Array
::value( 'dbCache' , $params, false );
550 $memCache = CRM_Utils_Array
::value( 'memCache' , $params, false );
552 if ( $session ||
$tempTable ||
$prevNext ) {
553 CRM_Core_BAO_Cache
::cleanup( $session, $tempTable, $prevNext );
557 CRM_Core_BAO_Job
::cleanup( );
561 CRM_Core_Config
::clearDBCache( );
565 CRM_Utils_System
::flushCache( );
570 * Set expired relationships to disabled.
573 function civicrm_api3_job_disable_expired_relationships($params) {
574 $result = CRM_Contact_BAO_Relationship
::disableExpiredRelationships();
576 return civicrm_api3_create_success();
579 return civicrm_api3_create_error('Failed to disable all expired relationships.');
584 * This api reloads all the smart groups. If the org has a large number of smart groups
585 * it is recommended that they use the limit clause to limit the number of smart groups
586 * evaluated on a per job basis. Might also help to increase the smartGroupCacheTimeout
589 function civicrm_api3_job_group_rebuild( $params ) {
590 $lock = new CRM_Core_Lock('civimail.job.groupRebuild');
591 if (!$lock->isAcquired()) {
592 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
595 $limit = CRM_Utils_Array
::value( 'limit', $params, 0 );
597 CRM_Contact_BAO_GroupContactCache
::loadAll(null, $limit);
600 return civicrm_api3_create_success();