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 * Adjust metadata for "Create" action
45 * The metadata is used for setting defaults, documentation & validation
46 * @param array $params 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 * Function to create scheduled job
61 * @param array $params Associative array of property name/value pairs to insert in new job.
63 * @return success or error
64 * {@getfields Job_create}
66 * {@schema Core/Job.xml}
68 function civicrm_api3_job_create($params) {
69 return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
73 * Retrieve one or more job
74 * @param array input parameters
75 * @return array api result array
76 * {@getfields email_get}
79 function civicrm_api3_job_get($params) {
80 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
88 * @internal param int $id
90 * @return array API Result Array
91 * {@getfields Job_delete}
95 function civicrm_api3_job_delete($params) {
96 _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
100 * Dumb wrapper to execute scheduled jobs. Always creates success - errors
101 * and results are handled in the job log.
103 * @param array $params (reference ) input parameters
105 * @return array API Result Array
111 function civicrm_api3_job_execute($params) {
112 $facility = new CRM_Core_JobManager();
113 $facility->execute(FALSE);
115 // always creates success - results are handled elsewhere
116 return civicrm_api3_create_success();
120 * Adjust Metadata for Execute action
122 * @param array $params array or parameters determined by getfields
124 function _civicrm_api3_job_execute_spec(&$params) {
128 * Geocode group of contacts based on given params
130 * @param array $params (reference ) input parameters
132 * @return array API Result Array
133 * {@getfields contact_geocode}
140 function civicrm_api3_job_geocode($params) {
141 $gc = new CRM_Utils_Address_BatchUpdate($params);
144 $result = $gc->run();
146 if ($result['is_error'] == 0) {
147 return civicrm_api3_create_success($result['messages']);
150 return civicrm_api3_create_error($result['messages']);
154 * First check on Code documentation
156 function _civicrm_api3_job_geocode_spec(&$params) {
157 $params['start'] = array('title' => 'Start Date');
158 $params['end'] = array('title' => 'End Date');
159 $params['geocoding'] = array('title' => 'Geocode address?');
160 $params['parse'] = array('title' => 'Parse street address?');
161 $params['throttle'] = array('title' => 'Throttle? if enabled, geocodes at a slow rate');
165 * Send the scheduled reminders for all contacts (either for activities or events)
167 * @param array $params (reference ) input parameters
168 * now - the time to use, in YmdHis format
169 * - makes testing a bit simpler since we can simulate past/future time
171 * @return boolean true if success, else false
176 function civicrm_api3_job_send_reminder($params) {
177 //note that $params['rowCount' can be overridden by one of the preferred syntaxes ($options['limit'] = x
178 //It's not clear whether than syntax can be passed in via the UI config - but this keeps the pre 4.4.4 behaviour
179 // in that case (ie. makes it unconfigurable via the UI). Another approach would be to set a default of 0
180 // in the _spec function - but since that is a deprecated value it seems more contentious than this approach
181 $params['rowCount'] = 0;
182 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
183 if (!$lock->isAcquired()) {
184 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
187 $result = CRM_Core_BAO_ActionSchedule
::processQueue(CRM_Utils_Array
::value('now', $params), $params);
190 if ($result['is_error'] == 0) {
191 return civicrm_api3_create_success();
194 return civicrm_api3_create_error($result['messages']);
198 * Adjust metadata for "send_reminder" action
200 * The metadata is used for setting defaults, documentation & validation
201 * @param array $params array or parameters determined by getfields
203 function _civicrm_api3_job_send_reminder(&$params) {
204 //@todo this function will now take all fields in action_schedule as params
205 // as it is calling the api fn to set the filters - update getfields to reflect
206 $params['id'] = array(
207 'type' => CRM_Utils_Type
::T_INT
,
208 'title' => 'Action Schedule ID'
212 * Execute a specific report instance and send the output via email
214 * @param array $params (reference ) input parameters
215 * sendmail - Boolean - should email be sent?, required
216 * instanceId - Integer - the report instance ID
217 * resetVal - Integer - should we reset form state (always true)?
219 * @return boolean true if success, else false
224 function civicrm_api3_job_mail_report($params) {
225 $result = CRM_Report_Utils_Report
::processReport($params);
227 if ($result['is_error'] == 0) {
228 // this should be handling by throwing exceptions but can't remove until we can test that.
229 return civicrm_api3_create_success();
232 return civicrm_api3_create_error($result['messages']);
238 * This method allows to update Email Greetings, Postal Greetings and Addressee for a specific contact type.
239 * IMPORTANT: You must first create valid option value before using via admin interface.
240 * Check option lists for Email Greetings, Postal Greetings and Addressee
242 * id - Integer - greetings option group
246 * @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 Membership Processing 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();