5 +--------------------------------------------------------------------+
6 | CiviCRM version 4.6 |
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 * 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);
86 * @param array $params
88 * @return array API Result Array
89 * {@getfields Job_delete}
93 function civicrm_api3_job_delete($params) {
94 _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__
), $params);
98 * Dumb wrapper to execute scheduled jobs. Always creates success - errors
99 * and results are handled in the job log.
101 * @param array $params (reference ) input parameters
103 * @return array API Result Array
109 function civicrm_api3_job_execute($params) {
110 $facility = new CRM_Core_JobManager();
111 $facility->execute(FALSE);
113 // always creates success - results are handled elsewhere
114 return civicrm_api3_create_success();
118 * Adjust Metadata for Execute action
120 * @param array $params array or parameters determined by getfields
122 function _civicrm_api3_job_execute_spec(&$params) {
126 * Geocode group of contacts based on given params
128 * @param array $params (reference ) input parameters
130 * @return array API Result Array
131 * {@getfields contact_geocode}
138 function civicrm_api3_job_geocode($params) {
139 $gc = new CRM_Utils_Address_BatchUpdate($params);
142 $result = $gc->run();
144 if ($result['is_error'] == 0) {
145 return civicrm_api3_create_success($result['messages']);
148 return civicrm_api3_create_error($result['messages']);
153 * First check on Code documentation
155 * @param array $params
157 function _civicrm_api3_job_geocode_spec(&$params) {
158 $params['start'] = array('title' => 'Start Date');
159 $params['end'] = array('title' => 'End Date');
160 $params['geocoding'] = array('title' => 'Geocode address?');
161 $params['parse'] = array('title' => 'Parse street address?');
162 $params['throttle'] = array('title' => 'Throttle? if enabled, geocodes at a slow rate');
166 * Send the scheduled reminders for all contacts (either for activities or events)
168 * @param array $params (reference ) input parameters
169 * now - the time to use, in YmdHis format
170 * - makes testing a bit simpler since we can simulate past/future time
172 * @return boolean true if success, else false
177 function civicrm_api3_job_send_reminder($params) {
178 //note that $params['rowCount' can be overridden by one of the preferred syntaxes ($options['limit'] = x
179 //It's not clear whether than syntax can be passed in via the UI config - but this keeps the pre 4.4.4 behaviour
180 // in that case (ie. makes it unconfigurable via the UI). Another approach would be to set a default of 0
181 // in the _spec function - but since that is a deprecated value it seems more contentious than this approach
182 $params['rowCount'] = 0;
183 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
184 if (!$lock->isAcquired()) {
185 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
188 $result = CRM_Core_BAO_ActionSchedule
::processQueue(CRM_Utils_Array
::value('now', $params), $params);
191 if ($result['is_error'] == 0) {
192 return civicrm_api3_create_success();
195 return civicrm_api3_create_error($result['messages']);
199 * Adjust metadata for "send_reminder" action
201 * The metadata is used for setting defaults, documentation & validation
202 * @param array $params array or parameters determined by getfields
204 function _civicrm_api3_job_send_reminder(&$params) {
205 //@todo this function will now take all fields in action_schedule as params
206 // as it is calling the api fn to set the filters - update getfields to reflect
207 $params['id'] = array(
208 'type' => CRM_Utils_Type
::T_INT
,
209 'title' => 'Action Schedule ID'
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 * @param array $params
247 * @return boolean true if success, else false
251 function civicrm_api3_job_update_greeting($params) {
253 if (isset($params['ct']) && isset($params['gt'])) {
255 $ct = explode(',', $params['ct']);
256 $gt = explode(',', $params['gt']);
257 foreach ($ct as $ctKey => $ctValue) {
258 foreach ($gt as $gtKey => $gtValue) {
259 $params['ct'] = trim($ctValue);
260 $params['gt'] = trim($gtValue);
261 $result[] = CRM_Contact_BAO_Contact_Utils
::updateGreeting($params);
266 $result = CRM_Contact_BAO_Contact_Utils
::updateGreeting($params);
269 foreach ($result as $resultKey => $resultValue) {
270 if ($resultValue['is_error'] == 0) {
271 //really we should rely on the exception mechanism here - but we need to test that before removing this line
272 return civicrm_api3_create_success();
275 return civicrm_api3_create_error($resultValue['messages']);
281 * Adjust Metadata for Get action
283 * The metadata is used for setting defaults, documentation & validation
284 * @param array $params array or parameters determined by getfields
286 function _civicrm_api3_job_update_greeting_spec(&$params) {
287 $params['ct'] = array(
289 'title' => 'Contact Type',
290 'type' => CRM_Utils_Type
::T_STRING
,
292 $params['gt'] = array(
294 'title' => 'Greeting Type',
295 'type' => CRM_Utils_Type
::T_STRING
,
300 * Mass update pledge statuses
302 * @param array $params (reference ) input parameters
304 * @return boolean true if success, else false
309 function civicrm_api3_job_process_pledge($params) {
310 // *** Uncomment the next line if you want automated reminders to be sent
311 // $params['send_reminders'] = true;
312 $result = CRM_Pledge_BAO_Pledge
::updatePledgeStatus($params);
314 if ($result['is_error'] == 0) {
315 // experiment: detailed execution log is a result here
316 return civicrm_api3_create_success($result['messages']);
319 return civicrm_api3_create_error($result['error_message']);
326 * @param array $params
330 function civicrm_api3_job_process_mailing($params) {
332 if (!CRM_Mailing_BAO_Mailing
::processQueue()) {
333 return civicrm_api3_create_error('Process Queue failed');
337 return civicrm_api3_create_success($values, $params, 'mailing', 'process');
344 * @param array $params
348 function civicrm_api3_job_process_sms($params) {
349 if (!CRM_Mailing_BAO_Mailing
::processQueue('sms')) {
350 return civicrm_api3_create_error('Process Queue failed');
354 return civicrm_api3_create_success($values, $params, 'mailing', 'process');
359 * Job to get mail responses from civimailing
361 * @param array $params
365 function civicrm_api3_job_fetch_bounces($params) {
366 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
367 if (!$lock->isAcquired()) {
368 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
370 if (!CRM_Utils_Mail_EmailProcessor
::processBounces()) {
372 return civicrm_api3_create_error('Process Bounces failed');
376 // FIXME: processBounces doesn't return true/false on success/failure
378 return civicrm_api3_create_success($values, $params, 'mailing', 'bounces');
382 * Job to get mail and create activities
384 * @param array $params
388 function civicrm_api3_job_fetch_activities($params) {
389 $lock = new CRM_Core_Lock('civimail.job.EmailProcessor');
390 if (!$lock->isAcquired()) {
391 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
395 CRM_Utils_Mail_EmailProcessor
::processActivities();
398 return civicrm_api3_create_success($values, $params,'mailing','activities');
399 } catch (Exception
$e) {
401 return civicrm_api3_create_error('Process Activities failed');
406 * Process participant statuses
408 * @param array $params (reference ) input parameters
410 * @return array (reference ) array of properties, if error an array with an error id and error message
413 function civicrm_api3_job_process_participant($params) {
414 $result = CRM_Event_BAO_ParticipantStatusType
::process($params);
416 if (!$result['is_error']) {
417 return civicrm_api3_create_success(implode("\r\r", $result['messages']));
420 return civicrm_api3_create_error('Error while processing participant statuses');
426 * This api checks and updates the status of all membership records for a given domain using the calc_membership_status and
427 * update_contact_membership APIs.
430 * Sending renewal reminders has been migrated from this job to the Scheduled Reminders function as of 4.3.
432 * @param array $params input parameters NOT USED
434 * @return boolean true if success, else false
438 function civicrm_api3_job_process_membership($params) {
439 $lock = new CRM_Core_Lock('civimail.job.updateMembership');
440 if (!$lock->isAcquired()) {
441 return civicrm_api3_create_error('Could not acquire lock, another Membership Processing process is running');
444 $result = CRM_Member_BAO_Membership
::updateAllMembershipStatus();
447 if ($result['is_error'] == 0) {
448 return civicrm_api3_create_success($result['messages']);
451 return civicrm_api3_create_error($result['messages']);
456 * This api checks and updates the status of all survey respondants.
458 * @param array $params (reference ) input parameters
460 * @return boolean true if success, else false
464 function civicrm_api3_job_process_respondent($params) {
465 $result = CRM_Campaign_BAO_Survey
::releaseRespondent($params);
467 if ($result['is_error'] == 0) {
468 return civicrm_api3_create_success();
471 return civicrm_api3_create_error($result['messages']);
476 * Merges given pair of duplicate contacts.
478 * @param array $params input parameters
480 * Allowed @params array keys are:
481 * {int $rgid rule group id}
482 * {int $gid group id}
483 * {string mode helps decide how to behave when there are conflicts.
484 * A 'safe' value skips the merge if there are no conflicts. Does a force merge otherwise.}
485 * {boolean auto_flip wether to let api decide which contact to retain and which to delete.}
487 * @return array API Result Array
492 function civicrm_api3_job_process_batch_merge($params) {
493 $rgid = CRM_Utils_Array
::value('rgid', $params);
494 $gid = CRM_Utils_Array
::value('gid', $params);
496 $mode = CRM_Utils_Array
::value('mode', $params, 'safe');
497 $autoFlip = CRM_Utils_Array
::value('auto_flip', $params, TRUE);
499 $result = CRM_Dedupe_Merger
::batchMerge($rgid, $gid, $mode, $autoFlip);
501 if ($result['is_error'] == 0) {
502 return civicrm_api3_create_success();
505 return civicrm_api3_create_error($result['messages']);
510 * Runs handlePaymentCron method in the specified payment processor
512 * @param array $params input parameters
514 * Expected @params array keys are:
515 * {string 'processor_name' - the name of the payment processor, eg: Sagepay}
519 function civicrm_api3_job_run_payment_cron($params) {
522 CRM_Core_Payment
::handlePaymentMethod(
533 CRM_Core_Payment
::handlePaymentMethod(
545 * This api cleans up all the old session entries and temp tables. We recommend that sites run this on an hourly basis
547 * @param array $params (reference ) - sends in various config parameters to decide what needs to be cleaned
549 * @return boolean true if success, else false
553 function civicrm_api3_job_cleanup( $params ) {
554 $session = CRM_Utils_Array
::value( 'session' , $params, true );
555 $tempTable = CRM_Utils_Array
::value( 'tempTables', $params, true );
556 $jobLog = CRM_Utils_Array
::value( 'jobLog' , $params, true );
557 $prevNext = CRM_Utils_Array
::value( 'prevNext' , $params, true );
558 $dbCache = CRM_Utils_Array
::value( 'dbCache' , $params, false );
559 $memCache = CRM_Utils_Array
::value( 'memCache' , $params, false );
561 if ( $session ||
$tempTable ||
$prevNext ) {
562 CRM_Core_BAO_Cache
::cleanup( $session, $tempTable, $prevNext );
566 CRM_Core_BAO_Job
::cleanup( );
570 CRM_Core_Config
::clearDBCache( );
574 CRM_Utils_System
::flushCache( );
579 * Set expired relationships to disabled.
581 * @param array $params
585 function civicrm_api3_job_disable_expired_relationships($params) {
586 $result = CRM_Contact_BAO_Relationship
::disableExpiredRelationships();
588 return civicrm_api3_create_success();
591 return civicrm_api3_create_error('Failed to disable all expired relationships.');
596 * This api reloads all the smart groups. If the org has a large number of smart groups
597 * it is recommended that they use the limit clause to limit the number of smart groups
598 * evaluated on a per job basis. Might also help to increase the smartGroupCacheTimeout
601 * @param array $params
605 function civicrm_api3_job_group_rebuild($params) {
606 $lock = new CRM_Core_Lock('civimail.job.groupRebuild');
607 if (!$lock->isAcquired()) {
608 return civicrm_api3_create_error('Could not acquire lock, another EmailProcessor process is running');
611 $limit = CRM_Utils_Array
::value( 'limit', $params, 0 );
613 CRM_Contact_BAO_GroupContactCache
::loadAll(null, $limit);
616 return civicrm_api3_create_success();