Skip to content
Snippets Groups Projects
Commit e6cc6fdc authored by colemanw's avatar colemanw
Browse files

Remove v2 api CRM-9794

----------------------------------------
* CRM-9794: Remove API v2
  http://issues.civicrm.org/jira/browse/CRM-9794
parent d61cf286
No related branches found
No related tags found
No related merge requests found
Showing
with 0 additions and 6327 deletions
<?php
// $Id: Activity.php 45502 2013-02-08 13:32:55Z kurund $
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* File for the CiviCRM APIv2 activity functions
*
* @package CiviCRM_APIv2
* @subpackage API_Activity
* @copyright CiviCRM LLC (c) 2004-2013
* @version $Id: Activity.php 45502 2013-02-08 13:32:55Z kurund $
*
*/
/**
* Include common API util functions
*/
require_once 'api/v2/utils.php';
require_once 'CRM/Activity/BAO/Activity.php';
require_once 'CRM/Core/DAO/OptionGroup.php';
// require these to call new function names from deprecated ones in here
require_once 'api/v2/ActivityType.php';
require_once 'api/v2/ActivityContact.php';
/**
* Create a new Activity.
*
* Creates a new Activity record and returns the newly created
* activity object (including the contact_id property). Minimum
* required data values for the various contact_type are:
*
* Properties which have administratively assigned sets of values
* If an unrecognized value is passed, an error
* will be returned.
*
* Modules may invoke crm_get_contact_values($contactID) to
* retrieve a list of currently available values for a given
* property.
*
* @param array $params Associative array of property name/value
* pairs to insert in new contact.
* @param string $activity_type Which class of contact is being created.
* Valid values = 'SMS', 'Meeting', 'Event', 'PhoneCall'.
* {@schema Activity/Activity.xml}
*
* @return CRM_Activity|CRM_Error Newly created Activity object
*/
function &civicrm_activity_create(&$params) {
_civicrm_initialize();
$errors = array();
// check for various error and required conditions
$errors = _civicrm_activity_check_params($params, TRUE);
if (!empty($errors)) {
return $errors;
}
// processing for custom data
$values = array();
_civicrm_custom_format_params($params, $values, 'Activity');
if (!empty($values['custom'])) {
$params['custom'] = $values['custom'];
}
// create activity
$activity = CRM_Activity_BAO_Activity::create($params);
if (!is_a($activity, 'CRM_Core_Error') && isset($activity->id)) {
$activityArray = array('is_error' => 0);
}
else {
$activityArray = array('is_error' => 1);
}
_civicrm_object_to_array($activity, $activityArray);
return $activityArray;
}
/**
*
* @param <type> $params
* @param <type> $returnCustom
*
* @return <type>
*/
function civicrm_activity_get($params, $returnCustom = FALSE) {
_civicrm_initialize();
$activityId = CRM_Utils_Array::value('activity_id', $params);
if (empty($activityId)) {
return civicrm_create_error(ts("Required parameter not found"));
}
if (!is_numeric($activityId)) {
return civicrm_create_error(ts("Invalid activity Id"));
}
$activity = _civicrm_activity_get($activityId, $returnCustom);
if ($activity) {
return civicrm_create_success($activity);
}
else {
return civicrm_create_error(ts('Invalid Data'));
}
}
/**
* Wrapper to make this function compatible with the REST API
*
* Obsolete now; if no one is using this, it should be removed. -- Wes Morgan
*/
function civicrm_activity_get_contact($params) {
// TODO: Spit out deprecation warning here
return civicrm_activities_get_contact($params);
}
/**
* Retrieve a set of activities, specific to given input params.
*
* @param array $params (reference ) input parameters.
* @deprecated from 3.4 - use civicrm_activity_contact_get
*
* @return array (reference) array of activities / error message.
* @access public
*/
function civicrm_activities_get_contact($params) {
// TODO: Spit out deprecation warning here
return civicrm_activity_contact_get($params);
}
/**
* Update a specified activity.
*
* Updates activity with the values passed in the 'params' array. An
* error is returned if an invalid id or activity Name is passed
*
* @param CRM_Activity $activity A valid Activity object
* @param array $params Associative array of property
* name/value pairs to be updated.
*
* @return CRM_Activity|CRM_Core_Error Return the updated ActivtyType Object else
* Error Object (if integrity violation)
*
* @access public
*
*/
function &civicrm_activity_update(&$params) {
$errors = array();
//check for various error and required conditions
$errors = _civicrm_activity_check_params($params);
if (!empty($errors)) {
return $errors;
}
// processing for custom data
$values = array();
_civicrm_custom_format_params($params, $values, 'Activity');
if (!empty($values['custom'])) {
$params['custom'] = $values['custom'];
}
$activity = CRM_Activity_BAO_Activity::create($params);
$activityArray = array();
_civicrm_object_to_array($activity, $activityArray);
return $activityArray;
}
/**
* Delete a specified Activity.
*
* @param CRM_Activity $activity Activity object to be deleted
*
* @return void|CRM_Core_Error An error if 'activityName or ID' is invalid,
* permissions are insufficient, etc.
*
* @access public
*
*/
function civicrm_activity_delete(&$params) {
_civicrm_initialize();
$errors = array();
//check for various error and required conditions
$errors = _civicrm_activity_check_params($params);
if (!empty($errors)) {
return $errors;
}
if (CRM_Activity_BAO_Activity::deleteActivity($params)) {
return civicrm_create_success();
}
else {
return civicrm_create_error(ts('Could not delete activity'));
}
}
/**
* Retrieve a specific Activity by Id.
*
* @param int $activityId
*
* @return array (reference) activity object
* @access public
*/
function _civicrm_activity_get($activityId, $returnCustom = FALSE) {
$dao = new CRM_Activity_BAO_Activity();
$dao->id = $activityId;
if ($dao->find(TRUE)) {
$activity = array();
_civicrm_object_to_array($dao, $activity);
//also return custom data if needed.
if ($returnCustom && !empty($activity)) {
$customdata = civicrm_activity_custom_get(array(
'activity_id' => $activityId,
'activity_type_id' => $activity['activity_type_id'],
));
$activity = array_merge($activity, $customdata);
}
return $activity;
}
else {
return FALSE;
}
}
/**
* Function to check for required params
*
* @param array $params associated array of fields
* @param boolean $addMode true for add mode
*
* @return array $error array with errors
*/
function _civicrm_activity_check_params(&$params, $addMode = FALSE) {
// return error if we do not get any params
if (empty($params)) {
return civicrm_create_error(ts('Input Parameters empty'));
}
$contactIds = array('source' => CRM_Utils_Array::value('source_contact_id', $params),
'assignee' => CRM_Utils_Array::value('assignee_contact_id', $params),
'target' => CRM_Utils_Array::value('target_contact_id', $params),
);
foreach ($contactIds as $key => $value) {
if (empty($value)) {
continue;
}
$valueIds = array($value);
if (is_array($value)) {
$valueIds = array();
foreach ($value as $id) {
if (is_numeric($id)) {
$valueIds[$id] = $id;
}
}
}
elseif (!is_numeric($value)) {
return civicrm_create_error(ts('Invalid %1 Contact Id', array(
1 => ucfirst(
$key
))));
}
if (empty($valueIds)) {
continue;
}
$sql = '
SELECT count(*)
FROM civicrm_contact
WHERE id IN (' . implode(', ', $valueIds) . ' )';
if (count($valueIds) != CRM_Core_DAO::singleValueQuery($sql)) {
return civicrm_create_error(ts('Invalid %1 Contact Id', array(1 => ucfirst($key))));
}
}
$activityIds = array('activity' => CRM_Utils_Array::value('id', $params),
'parent' => CRM_Utils_Array::value('parent_id', $params),
'original' => CRM_Utils_Array::value('original_id', $params),
);
foreach ($activityIds as $id => $value) {
if ($value &&
!CRM_Core_DAO::getFieldValue('CRM_Activity_DAO_Activity', $value, 'id')
) {
return civicrm_create_error(ts('Invalid %1 Id', array(1 => ucfirst($id))));
}
}
if (!$addMode && !isset($params['id'])) {
return civicrm_create_error(ts('Required parameter "id" not found'));
}
// check for source contact id
if ($addMode && empty($params['source_contact_id'])) {
return civicrm_create_error(ts('Missing Source Contact'));
}
// check for activity subject if add mode
if ($addMode && !isset($params['subject'])) {
return civicrm_create_error(ts('Missing Subject'));
}
if (!$addMode && $params['id'] && !is_numeric($params['id'])) {
return civicrm_create_error(ts('Invalid activity "id"'));
}
require_once 'CRM/Core/PseudoConstant.php';
$activityTypes = CRM_Core_PseudoConstant::activityType(TRUE, TRUE, TRUE, 'name');
// check if activity type_id is passed in
if ($addMode && !isset($params['activity_name']) && !isset($params['activity_type_id'])) {
//when name AND id are both absent
return civicrm_create_error(ts('Missing Activity Type'));
}
else {
$activityName = CRM_Utils_Array::value('activity_name', $params);
$activityTypeId = CRM_Utils_Array::value('activity_type_id', $params);
if ($activityName) {
$activityNameId = array_search(ucfirst($activityName), $activityTypes);
if (!$activityNameId) {
return civicrm_create_error(ts('Invalid Activity Name'));
}
elseif ($activityTypeId && ($activityTypeId != $activityNameId)) {
return civicrm_create_error(ts('Mismatch in Activity'));
}
$params['activity_type_id'] = $activityNameId;
}
elseif ($activityTypeId &&
!array_key_exists($activityTypeId, $activityTypes)
) {
return civicrm_create_error(ts('Invalid Activity Type ID'));
}
}
// check for activity status is passed in
if (isset($params['status_id'])) {
require_once "CRM/Core/PseudoConstant.php";
$activityStatus = CRM_Core_PseudoConstant::activityStatus();
if (is_numeric($params['status_id']) && !array_key_exists($params['status_id'], $activityStatus)) {
return civicrm_create_error(ts('Invalid Activity Status'));
}
elseif (!is_numeric($params['status_id'])) {
$statusId = array_search($params['status_id'], $activityStatus);
if (!is_numeric($statusId)) {
return civicrm_create_error(ts('Invalid Activity Status'));
}
}
}
if (isset($params['priority_id']) && is_numeric($params['priority_id'])) {
require_once "CRM/Core/PseudoConstant.php";
$activityPriority = CRM_Core_PseudoConstant::priority();
if (!array_key_exists($params['priority_id'], $activityPriority)) {
return civicrm_create_error(ts('Invalid Priority'));
}
}
// check for activity duration minutes
if (isset($params['duration_minutes']) && !is_numeric($params['duration_minutes'])) {
return civicrm_create_error(ts('Invalid Activity Duration (in minutes)'));
}
if ($addMode &&
!CRM_Utils_Array::value('activity_date_time', $params)
) {
$params['activity_date_time'] = CRM_Utils_Date::processDate(date('Y-m-d H:i:s'));
}
else {
if (CRM_Utils_Array::value('activity_date_time', $params)) {
$params['activity_date_time'] = CRM_Utils_Date::processDate($params['activity_date_time']);
}
}
return NULL;
}
/**
* Convert an email file to an activity
*/
function civicrm_activity_processemail($file, $activityTypeID, $result = array(
)) {
// do not parse if result array already passed (towards EmailProcessor..)
if (empty($result)) {
// might want to check that email is ok here
if (!file_exists($file) ||
!is_readable($file)
) {
return CRM_Core_Error::createAPIError(ts('File %1 does not exist or is not readable',
array(1 => $file)
));
}
}
require_once 'CRM/Utils/Mail/Incoming.php';
$result = CRM_Utils_Mail_Incoming::parse($file);
if ($result['is_error']) {
return $result;
}
$params = _civicrm_activity_buildmailparams($result, $activityTypeID);
return civicrm_activity_create($params);
}
/**
*
* @param <type> $result
* @param <type> $activityTypeID
*
* @return <type>
*/
function _civicrm_activity_buildmailparams($result, $activityTypeID) {
// get ready for collecting data about activity to be created
$params = array();
$params['activity_type_id'] = $activityTypeID;
$params['status_id'] = 2;
$params['source_contact_id'] = $params['assignee_contact_id'] = $result['from']['id'];
$params['target_contact_id'] = array();
$keys = array('to', 'cc', 'bcc');
foreach ($keys as $key) {
if (is_array($result[$key])) {
foreach ($result[$key] as $key => $keyValue) {
if (!empty($keyValue['id'])) {
$params['target_contact_id'][] = $keyValue['id'];
}
}
}
}
$params['subject'] = $result['subject'];
$params['activity_date_time'] = $result['date'];
$params['details'] = $result['body'];
for ($i = 1; $i <= 5; $i++) {
if (isset($result["attachFile_$i"])) {
$params["attachFile_$i"] = $result["attachFile_$i"];
}
}
return $params;
}
/**
*
* @param <type> $file
* @param <type> $activityTypeID
*
* @return <type>
* @deprecated since 3.4 use civicrm_activity_processemail
*/
function civicrm_activity_process_email($file, $activityTypeID) {
// TODO: Spit out deprecation warning here
return civicrm_activity_processemail($file, $activityTypeID);
}
/**
* @deprecated since 3.4 use civicrm_activity_type_get
*
* @return <type>
*/
function civicrm_activity_get_types() {
// TODO: Spit out deprecation warning here
return civicrm_activity_type_get();
}
/**
* Function retrieve activity custom data.
*
* @param array $params key => value array.
*
* @return array $customData activity custom data
*
* @access public
*/
function civicrm_activity_custom_get($params) {
$customData = array();
if (!CRM_Utils_Array::value('activity_id', $params)) {
return $customData;
}
require_once 'CRM/Core/BAO/CustomGroup.php';
$groupTree = &CRM_Core_BAO_CustomGroup::getTree('Activity',
CRM_Core_DAO::$_nullObject,
$params['activity_id'],
NULL,
CRM_Utils_Array::value('activity_type_id', $params)
);
//get the group count.
$groupCount = 0;
foreach ($groupTree as $key => $value) {
if ($key === 'info') {
continue;
}
$groupCount++;
}
$formattedGroupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree,
$groupCount,
CRM_Core_DAO::$_nullObject
);
$defaults = array();
CRM_Core_BAO_CustomGroup::setDefaults($formattedGroupTree, $defaults);
if (!empty($defaults)) {
foreach ($defaults as $key => $val) {
$customData[$key] = $val;
}
}
return $customData;
}
<?php
// $Id: ActivityContact.php 45502 2013-02-08 13:32:55Z kurund $
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* File for the CiviCRM APIv2 activity contact functions
*
* @package CiviCRM_APIv2
* @subpackage API_Activity
*
* @copyright CiviCRM LLC (c) 2004-2013
* @version $Id: ActivityContact.php 45502 2013-02-08 13:32:55Z kurund $
*
*/
/**
* Files required for this package
*/
require_once 'api/v2/utils.php';
require_once 'CRM/Activity/BAO/Activity.php';
/**
* Retrieve a set of activities, specific to given input params.
*
* @param array $params (reference ) input parameters.
*
* @return array (reference) array of activities / error message.
* @access public
*/
function civicrm_activity_contact_get($params) {
_civicrm_initialize();
$contactId = CRM_Utils_Array::value('contact_id', $params);
if (empty($contactId)) {
return civicrm_create_error(ts("Required parameter not found"));
}
//check if $contactId is valid
if (!is_numeric($contactId) || !preg_match('/^\d+$/', $contactId)) {
return civicrm_create_error(ts("Invalid contact Id"));
}
$activities = &_civicrm_activities_get($contactId);
//show success for empty $activities array
if (empty($activities)) {
return civicrm_create_success(ts("0 activity record matching input params"));
}
if ($activities) {
return civicrm_create_success($activities);
}
else {
return civicrm_create_error(ts('Invalid Data'));
}
}
/**
* Retrieve a set of Activities specific to given contact Id.
*
* @param int $contactID.
*
* @return array (reference) array of activities.
* @access public
*/
function &_civicrm_activities_get($contactID, $type = 'all') {
$activities = CRM_Activity_BAO_Activity::getContactActivity($contactID);
//get the custom data.
if (is_array($activities) && !empty($activities)) {
require_once 'api/v2/Activity.php';
foreach ($activities as $activityId => $values) {
$customParams = array(
'activity_id' => $activityId,
'activity_type_id' => CRM_Utils_Array::value('activity_type_id', $values),
);
$customData = civicrm_activity_custom_get($customParams);
if (is_array($customData) && !empty($customData)) {
$activities[$activityId] = array_merge($activities[$activityId], $customData);
}
}
}
return $activities;
}
<?php
// $Id: ActivityType.php 45502 2013-02-08 13:32:55Z kurund $
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* Definition of the ActivityType part of the CRM API.
* More detailed documentation can be found
* {@link http://objectledge.org/confluence/display/CRM/CRM+v1.0+Public+APIs
* here}
*
* @package CiviCRM_APIv2
* @subpackage API_Activity
*
* @copyright CiviCRM LLC (c) 2004-2013
* $Id: ActivityType.php 45502 2013-02-08 13:32:55Z kurund $
*
*/
/**
* Include common API util functions
*/
require_once 'api/v2/utils.php';
/**
* Function to retrieve activity types
*
* @return array $activityTypes activity types keyed by id
* @access public
*/
function civicrm_activity_type_get() {
require_once 'CRM/Core/OptionGroup.php';
$activityTypes = CRM_Core_OptionGroup::values('activity_type');
return $activityTypes;
}
/**
* Function to create activity type
*
* @param array $params associated array of fields
* $params['option_value_id'] is required for updation of activity type
*
* @return array $activityType created / updated activity type
*
* @access public
*/
function civicrm_activity_type_create($params) {
require_once 'CRM/Core/OptionGroup.php';
if (!isset($params['label']) || !isset($params['weight'])) {
return civicrm_create_error(ts('Required parameter "label / weight" not found'));
}
$action = 1;
$groupParams = array('name' => 'activity_type');
if ($optionValueID = CRM_Utils_Array::value('option_value_id', $params)) {
$action = 2;
}
require_once 'CRM/Core/OptionValue.php';
$activityObject = CRM_Core_OptionValue::addOptionValue($params, $groupParams, $action, $optionValueID);
$activityType = array();
_civicrm_object_to_array($activityObject, $activityType);
return $activityType;
}
/**
* Function to delete activity type
*
* @param activityTypeId int activity type id to delete
*
* @return boolen
*
* @access public
*/
function civicrm_activity_type_delete($params) {
if (!isset($params['activity_type_id'])) {
return civicrm_create_error(ts('Required parameter "activity_type_id" not found'));
}
$activityTypeId = $params['activity_type_id'];
require_once 'CRM/Core/BAO/OptionValue.php';
return CRM_Core_BAO_OptionValue::del($activityTypeId);
}
<?php
// $Id$
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* File for the CiviCRM APIv2 Case functions
* Developed by woolman.org
*
* @package CiviCRM_APIv2
* @subpackage API_Case
* @copyright CiviCRM LLC (c) 2004-2013
*
*/
require_once 'api/v2/utils.php';
require_once 'CRM/Case/BAO/Case.php';
require_once 'CRM/Case/PseudoConstant.php';
/**
* Open a new case, add client and manager roles, and add standard timeline
*
* @param array(
//REQUIRED:
* 'case_type_id' => int OR 'case_type' => str (provide one or the other)
* 'contact_id' => int // case client
* 'creator_id' => int // case manager
* 'subject' => str
* 'medium_id' => int // see civicrm option values for possibilities
*
* //OPTIONAL
* 'status_id' => int // defaults to 1 "ongoing"
* 'location' => str
* 'start_date' => str datestamp // defaults to: date('YmdHis')
* 'duration' => int // in minutes
* 'details' => str // html format
*
* @return sucessfully opened case
*
* @access public
*/
function civicrm_case_create(&$params) {
_civicrm_initialize();
//check parameters
$errors = _civicrm_case_check_params($params, 'create');
if ($errors) {
return $errors;
}
_civicrm_case_format_params($params, 'create');
// If format_params didn't find what it was looking for, return error
if (!$params['case_type_id']) {
return civicrm_create_error(ts('Invalid case_type. No such case type exists.'));
}
if (!$params['case_type']) {
return civicrm_create_error(ts('Invalid case_type_id. No such case type exists.'));
}
// format input with value separators
$sep = CRM_Core_DAO::VALUE_SEPARATOR;
$newParams = array(
'case_type_id' => $sep . $params['case_type_id'] . $sep,
'creator_id' => $params['creator_id'],
'status_id' => $params['status_id'],
'start_date' => $params['start_date'],
'subject' => $params['subject'],
);
$case = CRM_Case_BAO_Case::create($newParams);
if (!$case) {
return civicrm_create_error(ts('Case not created. Please check your input params.'));
}
// Add client role
$contactParams = array(
'case_id' => $case->id,
'contact_id' => $params['contact_id'],
);
CRM_Case_BAO_Case::addCaseToContact($contactParams);
// Initialize XML processor with $params
require_once 'CRM/Case/XMLProcessor/Process.php';
$xmlProcessor = new CRM_Case_XMLProcessor_Process();
$xmlProcessorParams = array(
'clientID' => $params['contact_id'],
'creatorID' => $params['creator_id'],
'standardTimeline' => 1,
'activityTypeName' => 'Open Case',
'caseID' => $case->id,
'subject' => $params['subject'],
'location' => $params['location'],
'activity_date_time' => $params['start_date'],
'duration' => $params['duration'],
'medium_id' => $params['medium_id'],
'details' => $params['details'],
'custom' => array(),
);
// Do it! :-D
$xmlProcessor->run($params['case_type'], $xmlProcessorParams);
// status msg
$params['statusMsg'] = ts('Case opened successfully.');
// return case
$details = _civicrm_case_read($case->id);
return civicrm_create_success($details);
}
/**
* Get details of a particular case, or search for cases, depending on params
*
* Please provide one (and only one) of the four get/search parameters:
*
* @param array(
'case_id' => if set, will get all available info about a case, including contacts and activities
*
* // if no case_id provided, this function will use one of the following search parameters:
* 'client_id' => finds all cases with a specific client
* 'activity_id' => returns the case containing a specific activity
* 'contact_id' => finds all cases associated with a contact (in any role, not just client)
*
*
* @return (get mode, case_id provided): Array with case details, case roles, case activity ids, (search mode, case_id not provided): Array of cases found
* @access public
*/
function civicrm_case_get(&$params) {
_civicrm_initialize();
//get mode
if ($caseId = $params['case_id']) {
//validate param
if (!is_numeric($caseId)) {
return civicrm_create_error(ts('Invalid parameter: case_id. Must provide a numeric value.'));
}
$case = _civicrm_case_read($caseId);
if ($case) {
//get case contacts
$contacts = CRM_Case_BAO_Case::getcontactNames($caseId);
$relations = CRM_Case_BAO_Case::getRelatedContacts($caseId);
$case['contacts'] = array_merge($contacts, $relations);
//get case activities
$query = "SELECT activity_id FROM civicrm_case_activity WHERE case_id = $caseId";
$dao = CRM_Core_DAO::executeQuery($query);
$case['activities'] = array();
while ($dao->fetch()) {
$case['activities'][] = $dao->activity_id;
}
return civicrm_create_success($case);
}
else {
return civicrm_create_success(array());
}
}
//search by client
if ($client = $params['client_id']) {
if (!is_numeric($client)) {
return civicrm_create_error(ts('Invalid parameter: client_id. Must provide a numeric value.'));
}
$ids = CRM_Case_BAO_Case::retrieveCaseIdsByContactId($client, TRUE);
if (empty($ids)) {
return civicrm_create_success(array());
}
$cases = array();
foreach ($ids as $id) {
$cases[$id] = _civicrm_case_read($id);
}
return civicrm_create_success($cases);
}
//search by activity
if ($act = $params['activity_id']) {
if (!is_numeric($act)) {
return civicrm_create_error(ts('Invalid parameter: activity_id. Must provide a numeric value.'));
}
$sql = "SELECT case_id FROM civicrm_case_activity WHERE activity_id = $act";
$caseId = CRM_Core_DAO::singleValueQuery($sql);
if (!$caseId) {
return civicrm_create_success(array());
}
$case = array($caseId => _civicrm_case_read($caseId));
return civicrm_create_success($case);
}
//search by contacts
if ($contact = $params['contact_id']) {
if (!is_numeric($contact)) {
return civicrm_create_error(ts('Invalid parameter: contact_id. Must provide a numeric value.'));
}
$sql = "
SELECT DISTINCT case_id
FROM civicrm_relationship
WHERE (contact_id_a = $contact
OR contact_id_b = $contact)
AND case_id IS NOT NULL";
$dao = CRM_Core_DAO::executeQuery($sql);
$cases = array();
while ($dao->fetch()) {
$cases[$dao->case_id] = _civicrm_case_read($dao->case_id);
}
return civicrm_create_success($cases);
}
return civicrm_create_error(ts('Missing required parameter. Must provide case_id, client_id, activity_id, or contact_id.'));
}
/**
* Create new activity for a case
*
* @param array(
//REQUIRED:
* 'case_id' => int
* 'activity_type_id' => int
* 'source_contact_id' => int
* 'status_id' => int
* 'medium_id' => int // see civicrm option values for possibilities
*
* //OPTIONAL
* 'subject' => str
* 'activity_date_time' => date string // defaults to: date('YmdHis')
* 'details => str
*
* @return activity id
*
* NOTE: For other case activity functions (update, delete, etc) use the Activity API
*
*/
function civicrm_case_activity_create(&$params) {
_civicrm_initialize();
//check parameters
$errors = _civicrm_case_check_params($params, 'activity');
_civicrm_case_format_params($params, 'activity');
if ($errors) {
return $errors;
}
require_once 'CRM/Activity/BAO/Activity.php';
$activity = CRM_Activity_BAO_Activity::create($params);
$caseParams = array(
'activity_id' => $activity->id,
'case_id' => $params['case_id'],
);
CRM_Case_BAO_Case::processCaseActivity($caseParams);
return civicrm_create_success($activity->id);
}
/**
* Update a specified case.
*
* @param array(
//REQUIRED:
* 'case_id' => int
*
* //OPTIONAL
* 'status_id' => int
* 'start_date' => str datestamp
* 'contact_id' => int // case client
* 'creator_id' => int // case manager
*
* @return Updated case
*
* @access public
*
*/
function civicrm_case_update(&$params) {
_civicrm_initialize();
$errors = array();
//check for various error and required conditions
$errors = _civicrm_case_check_params($params, 'update');
if (!empty($errors)) {
return $errors;
}
// return error if modifing creator id
if (array_key_exists('creator_id', $params)) {
return civicrm_create_error(ts('You have no provision to update creator id'));
}
$mCaseId = array();
$origContactIds = array();
// get original contact id and creator id of case
if ($params['contact_id']) {
$origContactIds = CRM_Case_BAO_Case::retrieveContactIdsByCaseId($params['case_id']);
$origContactId = $origContactIds[1];
}
if (count($origContactIds) > 1) {
// check valid orig contact id
if ($params['orig_contact_id'] && !in_array($params['orig_contact_id'], $origContactIds)) {
return civicrm_create_error(ts('Invalid case contact id (orig_contact_id)'));
}
elseif (!$params['orig_contact_id']) {
return civicrm_create_error(ts('Case is linked with more than one contact id. Provide the required params orig_contact_id to be replaced'));
}
$origContactId = $params['orig_contact_id'];
}
// check for same contact id for edit Client
if ($params['contact_id'] && !in_array($params['contact_id'], $origContactIds)) {
$mCaseId = CRM_Case_BAO_Case::mergeCases($params['contact_id'], $params['case_id'],
$origContactId, NULL, TRUE
);
}
if (CRM_Utils_Array::value('0', $mCaseId)) {
$params['case_id'] = $mCaseId[0];
}
$dao = new CRM_Case_BAO_Case();
$dao->id = $params['case_id'];
$dao->copyValues($params);
$dao->save();
$case = array();
_civicrm_object_to_array($dao, $case);
return civicrm_create_success($case);
}
/**
* Delete a specified case.
*
* @param array(
//REQUIRED:
* 'case_id' => int
*
* //OPTIONAL
* 'move_to_trash' => bool (defaults to false)
*
* @return boolean: true if success, else false
*
* @access public
*/
function civicrm_case_delete(&$params) {
_civicrm_initialize();
//check parameters
$errors = _civicrm_case_check_params($params, 'delete');
if ($errors) {
return $errors;
}
if (CRM_Case_BAO_Case::deleteCase($params['case_id'], $params['move_to_trash'])) {
return civicrm_create_success(ts('Case Deleted'));
}
else {
return civicrm_create_error(ts('Could not delete case.'));
}
}
/***********************************/
/* */
/* INTERNAL FUNCTIONS */
/* */
/***********************************/
/**
* Internal function to retrieve a case.
*
* @param int $caseId
*
* @return array (reference) case object
*
*/
function _civicrm_case_read($caseId) {
$dao = new CRM_Case_BAO_Case();
$dao->id = $caseId;
if ($dao->find(TRUE)) {
$case = array();
_civicrm_object_to_array($dao, $case);
//handle multi-value case type
$sep = CRM_Core_DAO::VALUE_SEPARATOR;
$case['case_type_id'] = trim(str_replace($sep, ',', $case['case_type_id']), ',');
return $case;
}
else {
return FALSE;
}
}
/**
* Internal function to format params for processing
*/
function _civicrm_case_format_params(&$params, $mode) {
switch ($mode) {
case 'create':
// set defaults
if (!$params['status_id']) {
$params['status_id'] = 1;
}
if (!$params['start_date']) {
$params['start_date'] = date('YmdHis');
}
// figure out case type id, if not supplied
if (!$params['case_type_id']) {
$sql = "
SELECT ov.value
FROM civicrm_option_value ov
JOIN civicrm_option_group og ON og.id = ov.option_group_id
WHERE ov.label = %1 AND og.name = 'case_type'";
$values = array(1 => array($params['case_type'], 'String'));
$params['case_type_id'] = CRM_Core_DAO::singleValueQuery($sql, $values);
}
elseif (!$params['case_type']) {
// figure out case type, if not supplied
$sql = "
SELECT ov.name
FROM civicrm_option_value ov
JOIN civicrm_option_group og ON og.id = ov.option_group_id
WHERE ov.value = %1 AND og.name = 'case_type'";
$values = array(1 => array($params['case_type_id'], 'Integer'));
$params['case_type'] = CRM_Core_DAO::singleValueQuery($sql, $values);
}
break;
case 'activity':
//set defaults
if (!$params['activity_date_time']) {
$params['activity_date_time'] = date('YmdHis');
}
break;
}
}
/**
* Internal function to check for valid parameters
*/
function _civicrm_case_check_params(&$params, $mode = NULL) {
// return error if we do not get any params
if (is_null($params) || !is_array($params) || empty($params)) {
return civicrm_create_error(ts('Invalid or missing input parameters. Must provide an associative array.'));
}
switch ($mode) {
case 'create':
if (!$params['case_type_id'] && !$params['case_type']) {
return civicrm_create_error(ts('Missing input parameters. Must provide case_type or case_type_id.'));
}
$required = array(
'contact_id' => 'num',
'status_id' => 'num',
'medium_id' => 'num',
'creator_id' => 'num',
'subject' => 'str',
);
if (!$params['case_type']) {
$required['case_type_id'] = 'num';
}
if (!$params['case_type_id']) {
$required['case_type'] = 'str';
}
break;
case 'activity':
$required = array(
'case_id' => 'num',
'activity_type_id' => 'num',
'source_contact_id' => 'num',
'status_id' => 'num',
'medium_id' => 'num',
);
break;
case 'update':
case 'delete':
$required = array('case_id' => 'num');
break;
default:
return NULL;
}
foreach ($required as $req => $type) {
if (!$params[$req]) {
return civicrm_create_error(ts('Missing required parameter: %1.', array(1 => $req)));
}
if ($type == 'num' && !is_numeric($params[$req])) {
return civicrm_create_error(ts('Invalid parameter: %1. Must provide a numeric value.', array(1 => $req)));
}
if ($type == 'str' && !is_string($params[$req])) {
return civicrm_create_error(ts('Invalid parameter: %1. Must provide a string.', array(1 => $req)));
}
}
$caseTypes = CRM_Case_PseudoConstant::caseType();
if (CRM_Utils_Array::value('case_type', $params) && !in_array($params['case_type'], $caseTypes)) {
return civicrm_create_error(ts('Invalid Case Type'));
}
if (CRM_Utils_Array::value('case_type_id', $params)) {
if (!array_key_exists($params['case_type_id'], $caseTypes)) {
return civicrm_create_error(ts('Invalid Case Type Id'));
}
// check case type miss match error
if (CRM_Utils_Array::value('case_type', $params) &&
$params['case_type_id'] != array_search($params['case_type'], $caseTypes)
) {
return civicrm_create_error(ts('Case type and case type id mismatch'));
}
$sep = CRM_Case_BAO_Case::VALUE_SEPARATOR;
$params['case_type'] = $caseTypes[$params['case_type_id']];
$params['case_type_id'] = $sep . $params['case_type_id'] . $sep;
}
// check for valid status id
$caseStatusIds = CRM_Case_PseudoConstant::caseStatus();
if (CRM_Utils_Array::value('status_id', $params) &&
!array_key_exists($params['status_id'], $caseStatusIds) &&
$mode != 'activity'
) {
return civicrm_create_error(ts('Invalid Case Status Id'));
}
// check for valid medium id
$encounterMedium = CRM_Core_OptionGroup::values('encounter_medium');
if (CRM_Utils_Array::value('medium_id', $params) &&
!array_key_exists($params['medium_id'], $encounterMedium)
) {
return civicrm_create_error(ts('Invalid Case Medium Id'));
}
$contactIds = array('creator' => CRM_Utils_Array::value('creator_id', $params),
'contact' => CRM_Utils_Array::value('contact_id', $params),
);
foreach ($contactIds as $key => $value) {
if ($value &&
!CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $value, 'id')
) {
return civicrm_create_error(ts('Invalid %1 Id', array(1 => ucfirst($key))));
}
}
}
<?php
// $Id: Constant.php 45502 2013-02-08 13:32:55Z kurund $
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* File for CiviCRM APIv2 pseudoconstants
*
* @package CiviCRM_APIv2
* @subpackage API_Constant
* @copyright CiviCRM LLC (c) 2004-2013
* @version $Id: Constant.php 45502 2013-02-08 13:32:55Z kurund $
*
*/
/**
* Include utility functions
*/
require_once 'api/v2/utils.php';
/**
* Generic file to retrieve all the constants and
* pseudo constants used in CiviCRM
*
* @param string Name of a public static method of
* CRM_Core_PseudoContant: one of
* <ul>
* <li>activityStatus</li>
* <li>activityType</li>
* <li>addressee</li>
* <li>allGroup</li>
* <li>country</li>
* <li>countryIsoCode</li>
* <li>county</li>
* <li>currencyCode</li>
* <li>currencySymbols</li>
* <li>customGroup</li>
* <li>emailGreeting</li>
* <li>fromEmailAddress</li>
* <li>gender</li>
* <li>group</li>
* <li>groupIterator</li>
* <li>honor</li>
* <li>IMProvider</li>
* <li>individualPrefix</li>
* <li>individualSuffix</li>
* <li>locationType</li>
* <li>locationVcardName</li>
* <li>mailProtocol</li>
* <li>mappingTypes</li>
* <li>paymentProcessor</li>
* <li>paymentProcessorType</li>
* <li>pcm</li>
* <li>phoneType</li>
* <li>postalGreeting</li>
* <li>priority</li>
* <li>relationshipType</li>
* <li>stateProvince</li>
* <li>stateProvinceAbbreviation</li>
* <li>stateProvinceForCountry</li>
* <li>staticGroup</li>
* <li>tag</li>
* <li>tasks</li>
* <li>ufGroup</li>
* <li>visibility</li>
* <li>worldRegion</li>
* <li>wysiwygEditor</li>
* </ul>
*/
function civicrm_constant_get($name, $params = array(
)) {
require_once 'CRM/Core/PseudoConstant.php';
$className = 'CRM_Core_PseudoConstant';
$callable = "$className::$name";
if (is_callable($callable)) {
if (empty($params)) {
$values = call_user_func(array($className, $name));
}
else {
$values = call_user_func_array(array($className, $name), $params);
}
return $values;
}
return civicrm_create_error(ts('Unknown civicrm constant or method not callable'));
}
<?php
// $Id: Contact.php 45502 2013-02-08 13:32:55Z kurund $
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* new version of civicrm apis. See blog post at
* http://civicrm.org/node/131
* @todo Write sth
*
* @package CiviCRM_APIv2
* @subpackage API_Contact
* @copyright CiviCRM LLC (c) 2004-2013
* $Id: Contact.php 45502 2013-02-08 13:32:55Z kurund $
*
*/
/**
* Include common API util functions
*/
require_once 'api/v2/utils.php';
require_once 'CRM/Contact/BAO/Contact.php';
/**
* @todo Write sth
*
* @param array $params (reference ) input parameters
*
* Allowed @params array keys are:
* {@schema Contact/Contact.xml}
* {@schema Core/Address.xml}}
*
* @return array (reference ) contact_id of created or updated contact
*
* @static void
* @access public
*/
function civicrm_contact_create(&$params) {
// call update and tell it to create a new contact
_civicrm_initialize();
$errorScope = CRM_Core_TemporaryErrorScope::useException();
try {
civicrm_api_check_permission(__FUNCTION__, $params, TRUE);
$create_new = TRUE;
return civicrm_contact_update($params, $create_new);
}
catch(Exception$e) {
return civicrm_create_error($e->getMessage());
}
}
/**
* @todo Write sth
* @todo Serious FIXMES in the code! File issues.
*/
function civicrm_contact_update(&$params, $create_new = FALSE) {
_civicrm_initialize();
try {
civicrm_api_check_permission(__FUNCTION__, $params, TRUE);
}
catch(Exception$e) {
return civicrm_create_error($e->getMessage());
}
require_once 'CRM/Utils/Array.php';
$entityId = CRM_Utils_Array::value('contact_id', $params, NULL);
if (!CRM_Utils_Array::value('contact_type', $params) &&
$entityId
) {
$params['contact_type'] = CRM_Contact_BAO_Contact::getContactType($entityId);
}
$dupeCheck = CRM_Utils_Array::value('dupe_check', $params, FALSE);
$values = civicrm_contact_check_params($params, $dupeCheck);
if ($values) {
return $values;
}
if ($create_new) {
// Make sure nothing is screwed up before we create a new contact
if (!empty($entityId)) {
return civicrm_create_error('Cannot create new contact when contact_id is present');
}
if (empty($params['contact_type'])) {
return civicrm_create_error('Contact Type not specified');
}
// If we get here, we're ready to create a new contact
if (($email = CRM_Utils_Array::value('email', $params)) && !is_array($params['email'])) {
require_once 'CRM/Core/BAO/LocationType.php';
$defLocType = CRM_Core_BAO_LocationType::getDefault();
$params['email'] = array(
1 => array('email' => $email,
'is_primary' => 1,
'location_type_id' => ($defLocType->id) ? $defLocType->id : 1,
),
);
}
}
if ($homeUrl = CRM_Utils_Array::value('home_url', $params)) {
require_once 'CRM/Core/PseudoConstant.php';
$websiteTypes = CRM_Core_PseudoConstant::websiteType();
$params['website'] = array(1 => array('website_type_id' => key($websiteTypes),
'url' => $homeUrl,
),
);
}
// FIXME: Some legacy support cruft, should get rid of this in 3.1
$change = array(
'individual_prefix' => 'prefix',
'prefix' => 'prefix_id',
'individual_suffix' => 'suffix',
'suffix' => 'suffix_id',
'gender' => 'gender_id',
);
foreach ($change as $field => $changeAs) {
if (array_key_exists($field, $params)) {
$params[$changeAs] = $params[$field];
unset($params[$field]);
}
}
// End legacy support cruft
if (isset($params['suffix_id']) &&
!(is_numeric($params['suffix_id']))
) {
$params['suffix_id'] = array_search($params['suffix_id'], CRM_Core_PseudoConstant::individualSuffix());
}
if (isset($params['prefix_id']) &&
!(is_numeric($params['prefix_id']))
) {
$params['prefix_id'] = array_search($params['prefix_id'], CRM_Core_PseudoConstant::individualPrefix());
}
if (isset($params['gender_id'])
&& !(is_numeric($params['gender_id']))
) {
$params['gender_id'] = array_search($params['gender_id'], CRM_Core_PseudoConstant::gender());
}
$error = _civicrm_greeting_format_params($params);
if (civicrm_error($error)) {
return $error;
}
$values = array();
if (!($csType = CRM_Utils_Array::value('contact_sub_type', $params)) &&
$entityId
) {
require_once 'CRM/Contact/BAO/Contact.php';
$csType = CRM_Contact_BAO_Contact::getContactSubType($entityId);
}
$customValue = civicrm_contact_check_custom_params($params, $csType);
if ($customValue) {
return $customValue;
}
_civicrm_custom_format_params($params, $values, $params['contact_type'], $entityId);
$params = array_merge($params, $values);
$contact = &_civicrm_contact_update($params, $entityId);
if (is_a($contact, 'CRM_Core_Error')) {
return civicrm_create_error($contact->_errors[0]['message']);
}
else {
$values = array();
$values['contact_id'] = $contact->id;
$values['is_error'] = 0;
}
return $values;
}
/**
* Add or update a contact. If a dupe is found, check for
* ignoreDupe flag to ignore or return error
*
* @deprecated deprecated since version 2.2.3; use civicrm_contact_create or civicrm_contact_update instead
*
* @param array $params (reference ) input parameters
*
* @return array (reference ) contact_id of created or updated contact
* @static void
* @access public
*/
function &civicrm_contact_add(&$params) {
_civicrm_initialize();
$contactID = CRM_Utils_Array::value('contact_id', $params);
if (!empty($contactID)) {
$result = civicrm_contact_update($params);
}
else {
$result = civicrm_contact_create($params);
}
return $result;
}
/**
* Validate the addressee or email or postal greetings
*
* @param $params Associative array of property name/value
* pairs to insert in new contact.
*
* @return array (reference ) null on success, error message otherwise
*
* @access public
*/
function _civicrm_greeting_format_params(&$params) {
$greetingParams = array('', '_id', '_custom');
foreach (array(
'email', 'postal', 'addressee') as $key) {
$greeting = '_greeting';
if ($key == 'addressee') {
$greeting = '';
}
$formatParams = FALSE;
// unset display value from params.
if (isset($params["{$key}{$greeting}_display"])) {
unset($params["{$key}{$greeting}_display"]);
}
// check if greetings are present in present
foreach ($greetingParams as $greetingValues) {
if (array_key_exists("{$key}{$greeting}{$greetingValues}", $params)) {
$formatParams = TRUE;
break;
}
}
if (!$formatParams) {
continue;
}
// format params
if (CRM_Utils_Array::value('contact_type', $params) == 'Organization' && $key != 'addressee') {
return civicrm_create_error(ts('You cannot use email/postal greetings for contact type %1.',
array(1 => $params['contact_type'])
));
}
$nullValue = FALSE;
$filter = array(
'contact_type' => $params['contact_type'],
'greeting_type' => "{$key}{$greeting}",
);
$greetings = CRM_Core_PseudoConstant::greeting($filter);
$greetingId = CRM_Utils_Array::value("{$key}{$greeting}_id", $params);
$greetingVal = CRM_Utils_Array::value("{$key}{$greeting}", $params);
$customGreeting = CRM_Utils_Array::value("{$key}{$greeting}_custom", $params);
if (!$greetingId && $greetingVal) {
$params["{$key}{$greeting}_id"] = CRM_Utils_Array::key($params["{$key}{$greeting}"], $greetings);
}
if ($customGreeting && $greetingId &&
($greetingId != array_search('Customized', $greetings))
) {
return civicrm_create_error(ts('Provide either %1 greeting id and/or %1 greeting or custom %1 greeting',
array(1 => $key)
));
}
if ($greetingVal && $greetingId &&
($greetingId != CRM_Utils_Array::key($greetingVal, $greetings))
) {
return civicrm_create_error(ts('Mismatch in %1 greeting id and %1 greeting',
array(1 => $key)
));
}
if ($greetingId) {
if (!array_key_exists($greetingId, $greetings)) {
return civicrm_create_error(ts('Invalid %1 greeting Id', array(1 => $key)));
}
if (!$customGreeting && ($greetingId == array_search('Customized', $greetings))) {
return civicrm_create_error(ts('Please provide a custom value for %1 greeting',
array(1 => $key)
));
}
}
elseif ($greetingVal) {
if (!in_array($greetingVal, $greetings)) {
return civicrm_create_error(ts('Invalid %1 greeting', array(1 => $key)));
}
$greetingId = CRM_Utils_Array::key($greetingVal, $greetings);
}
if ($customGreeting) {
$greetingId = CRM_Utils_Array::key('Customized', $greetings);
}
$customValue = $params['contact_id'] ? CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact',
$params['contact_id'],
"{$key}{$greeting}_custom"
) : FALSE;
if (array_key_exists("{$key}{$greeting}_id", $params) && empty($params["{$key}{$greeting}_id"])) {
$nullValue = TRUE;
}
elseif (array_key_exists("{$key}{$greeting}", $params) && empty($params["{$key}{$greeting}"])) {
$nullValue = TRUE;
}
elseif ($customValue && array_key_exists("{$key}{$greeting}_custom", $params)
&& empty($params["{$key}{$greeting}_custom"])
) {
$nullValue = TRUE;
}
$params["{$key}{$greeting}_id"] = $greetingId;
if (!$customValue && !$customGreeting && array_key_exists("{$key}{$greeting}_custom", $params)) {
unset($params["{$key}{$greeting}_custom"]);
}
if ($nullValue) {
$params["{$key}{$greeting}_id"] = '';
$params["{$key}{$greeting}_custom"] = '';
}
if (isset($params["{$key}{$greeting}"])) {
unset($params["{$key}{$greeting}"]);
}
}
}
/**
* Retrieve one or more contacts, given a set of search params
*
* @param mixed[] (reference ) input parameters
* @param bool follow the pre-2.2.3 behavior of this function
*
* @return array (reference ) array of properties, if error an array with an error id and error message
* @static void
* @access public
*/
function civicrm_contact_get(&$params, $deprecated_behavior = FALSE) {
_civicrm_initialize();
if ($deprecated_behavior) {
return _civicrm_contact_get_deprecated($params);
}
// fix for CRM-7384 cater for soft deleted contacts
$params['contact_is_deleted'] = 0;
if (isset($params['showAll'])) {
if (strtolower($params['showAll']) == "active") {
$params['contact_is_deleted'] = 0;
}
if (strtolower($params['showAll']) == "trash") {
$params['contact_is_deleted'] = 1;
}
if (strtolower($params['showAll']) == "all" && isset($params['contact_is_deleted'])) {
unset($params['contact_is_deleted']);
}
}
$inputParams = array();
$returnProperties = array();
$otherVars = array('sort', 'offset', 'rowCount', 'smartGroupCache');
$sort = NULL;
$offset = 0;
$rowCount = 25;
$smartGroupCache = FALSE;
foreach ($params as $n => $v) {
if (substr($n, 0, 6) == 'return') {
$returnProperties[substr($n, 7)] = $v;
}
elseif (in_array($n, $otherVars)) {
$$n = $v;
}
else {
$inputParams[$n] = $v;
}
}
if (empty($returnProperties)) {
$returnProperties = NULL;
}
require_once 'CRM/Contact/BAO/Query.php';
$newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams);
list($contacts, $options) = CRM_Contact_BAO_Query::apiQuery($newParams,
$returnProperties,
NULL,
$sort,
$offset,
$rowCount,
$smartGroupCache
);
return $contacts;
}
/**
* Retrieve a specific contact, given a set of search params
*
* @deprecated deprecated since version 2.2.3
*
* @param array $params (reference ) input parameters
*
* @return array (reference ) array of properties, if error an array with an error id and error message
* @static void
* @access public
*/
function _civicrm_contact_get_deprecated(&$params) {
$values = array();
if (empty($params)) {
return civicrm_create_error(ts('No input parameters present'));
}
if (!is_array($params)) {
return civicrm_create_error(ts('Input parameters is not an array'));
}
$contacts = &civicrm_contact_search($params);
if (civicrm_error($contacts)) {
return $contacts;
}
if (count($contacts) != 1 &&
!CRM_Utils_Array::value('returnFirst', $params)
) {
return civicrm_create_error(ts('%1 contacts matching input params', array(1 => count($contacts))));
}
elseif (count($contacts) == 0) {
return civicrm_create_error(ts('No contacts match the input params'));
}
$contacts = array_values($contacts);
return $contacts[0];
}
/**
* Delete a contact with given contact id
*
* @param array $params (reference ) input parameters, contact_id element required
*
* @return boolean true if success, else false
* @static void
* @access public
*/
function civicrm_contact_delete(&$params) {
require_once 'CRM/Contact/BAO/Contact.php';
$contactID = CRM_Utils_Array::value('contact_id', $params);
if (!$contactID) {
return civicrm_create_error(ts('Could not find contact_id in input parameters'));
}
$session = CRM_Core_Session::singleton();
if ($contactID == $session->get('userID')) {
return civicrm_create_error(ts('This contact record is linked to the currently logged in user account - and cannot be deleted.'));
}
$restore = CRM_Utils_Array::value('restore', $params) ? $params['restore'] : FALSE;
$skipUndelete = CRM_Utils_Array::value('skip_undelete', $params) ? $params['skip_undelete'] : FALSE;
if (CRM_Contact_BAO_Contact::deleteContact($contactID, $restore, $skipUndelete)) {
return civicrm_create_success();
}
else {
return civicrm_create_error(ts('Could not delete contact'));
}
}
/**
* Retrieve a set of contacts, given a set of input params
*
* @deprecated deprecated since version 2.2.3
*
* @param array $params (reference ) input parameters
* @param array $returnProperties Which properties should be included in the
* returned Contact object. If NULL, the default
* set of properties will be included.
*
* @return array (reference ) array of contacts, if error an array with an error id and error message
* @static void
* @access public
*/
function &civicrm_contact_search(&$params) {
_civicrm_initialize();
$inputParams = $returnProperties = array();
$otherVars = array('sort', 'offset', 'rowCount', 'smartGroupCache');
$sort = NULL;
$offset = 0;
$rowCount = 25;
$smartGroupCache = FALSE;
foreach ($params as $n => $v) {
if (substr($n, 0, 6) == 'return') {
$returnProperties[substr($n, 7)] = $v;
}
elseif (in_array($n, $otherVars)) {
$$n = $v;
}
else {
$inputParams[$n] = $v;
}
}
// explicitly suppress all deleted contacts
// this is fixed in api v3
// CRM-8809
$inputParams['contact_is_deleted'] = 0;
if (empty($returnProperties)) {
$returnProperties = NULL;
}
require_once 'CRM/Contact/BAO/Query.php';
$newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams);
list($contacts, $options) = CRM_Contact_BAO_Query::apiQuery($newParams,
$returnProperties,
NULL,
$sort,
$offset,
$rowCount,
$smartGroupCache
);
return $contacts;
}
/**
* Ensure that we have the right input parameters
*
* @todo We also need to make sure we run all the form rules on the params list
* to ensure that the params are valid
*
* @param array $params Associative array of property name/value
* pairs to insert in new contact.
* @param boolean $dupeCheck Should we check for duplicate contacts
* @param boolean $dupeErrorArray Should we return values of error
* object in array foramt
* @param boolean $requiredCheck Should we check if required params
* are present in params array
* @param int $dedupeRuleGroupID - the dedupe rule ID to use if present
*
* @return null on success, error message otherwise
* @access public
*/
function civicrm_contact_check_params(&$params,
$dupeCheck = TRUE,
$dupeErrorArray = FALSE,
$requiredCheck = TRUE,
$dedupeRuleGroupID = NULL
) {
if ($requiredCheck) {
$required = array(
'Individual' => array(
array('first_name', 'last_name'),
'email',
),
'Household' => array(
'household_name',
),
'Organization' => array(
'organization_name',
),
);
// cannot create a contact with empty params
if (empty($params)) {
return civicrm_create_error('Input Parameters empty');
}
if (!array_key_exists('contact_type', $params)) {
return civicrm_create_error('Contact Type not specified');
}
// contact_type has a limited number of valid values
$fields = CRM_Utils_Array::value($params['contact_type'], $required);
if ($fields == NULL) {
return civicrm_create_error("Invalid Contact Type: {$params['contact_type']}");
}
if ($csType = CRM_Utils_Array::value('contact_sub_type', $params)) {
if (!(CRM_Contact_BAO_ContactType::isExtendsContactType($csType, $params['contact_type']))) {
return civicrm_create_error("Invalid or Mismatched Contact SubType: " . implode(', ', (array)$csType));
}
}
if (!CRM_Utils_Array::value('contact_id', $params)) {
$valid = FALSE;
$error = '';
foreach ($fields as $field) {
if (is_array($field)) {
$valid = TRUE;
foreach ($field as $element) {
if (!CRM_Utils_Array::value($element, $params)) {
$valid = FALSE;
$error .= $element;
break;
}
}
}
else {
if (CRM_Utils_Array::value($field, $params)) {
$valid = TRUE;
}
}
if ($valid) {
break;
}
}
if (!$valid) {
return civicrm_create_error("Required fields not found for {$params['contact_type']} : $error");
}
}
}
if ($dupeCheck) {
// check for record already existing
require_once 'CRM/Dedupe/Finder.php';
$dedupeParams = CRM_Dedupe_Finder::formatParams($params, $params['contact_type']);
// CRM-6431
// setting 'check_permission' here means that the dedupe checking will be carried out even if the
// person does not have permission to carry out de-dupes
// this is similar to the front end form
if (isset($params['check_permission'])) {
$dedupeParams['check_permission'] = $fields['check_permission'];
}
$ids = implode(',',
CRM_Dedupe_Finder::dupesByParams($dedupeParams,
$params['contact_type'],
'Strict',
array(),
$dedupeRuleGroupID
)
);
if ($ids != NULL) {
if ($dupeErrorArray) {
$error = CRM_Core_Error::createError("Found matching contacts: $ids",
CRM_Core_Error::DUPLICATE_CONTACT,
'Fatal', $ids
);
return civicrm_create_error($error->pop());
}
return civicrm_create_error("Found matching contacts: $ids", array($ids));
}
}
//check for organisations with same name
if (CRM_Utils_Array::value('current_employer', $params)) {
$organizationParams = array();
$organizationParams['organization_name'] = $params['current_employer'];
require_once 'CRM/Dedupe/Finder.php';
$dedupParams = CRM_Dedupe_Finder::formatParams($organizationParams, 'Organization');
$dedupParams['check_permission'] = FALSE;
$dupeIds = CRM_Dedupe_Finder::dupesByParams($dedupParams, 'Organization', 'Fuzzy');
// check for mismatch employer name and id
if (CRM_Utils_Array::value('employer_id', $params)
&& !in_array($params['employer_id'], $dupeIds)
) {
return civicrm_create_error('Employer name and Employer id Mismatch');
}
// show error if multiple organisation with same name exist
if (!CRM_Utils_Array::value('employer_id', $params)
&& (count($dupeIds) > 1)
) {
return civicrm_create_error('Found more than one Organisation with same Name.');
}
}
return NULL;
}
/**
* @todo What does this do? If it's still useful, figure out where it should live and what it should be named.
*
* @deprecated deprecated since version 2.2.3
*/
function civicrm_replace_contact_formatted($contactId, &$params, &$fields) {
//$contact = civcrm_get_contact(array('contact_id' => $contactId));
$delContact = array('contact_id' => $contactId);
civicrm_contact_delete($delContact);
$cid = CRM_Contact_BAO_Contact::createProfileContact($params, $fields,
NULL, NULL, NULL,
$params['contact_type']
);
return civicrm_create_success($cid);
}
/**
* Takes an associative array and creates a contact object and all the associated
* derived objects (i.e. individual, location, email, phone etc)
*
* @param array $params (reference ) an assoc array of name/value pairs
* @param int $contactID if present the contact with that ID is updated
*
* @return object CRM_Contact_BAO_Contact object
* @access public
* @static
*/
function _civicrm_contact_update(&$params, $contactID = NULL) {
require_once 'CRM/Core/Transaction.php';
$transaction = new CRM_Core_Transaction();
if ($contactID) {
$params['contact_id'] = $contactID;
}
require_once 'CRM/Contact/BAO/Contact.php';
$contact = CRM_Contact_BAO_Contact::create($params);
$transaction->commit();
return $contact;
}
/**
* @todo Move this to ContactFormat.php
* @deprecated
*/
function civicrm_contact_format_create(&$params) {
_civicrm_initialize();
CRM_Core_DAO::freeResult();
// return error if we have no params
if (empty($params)) {
return civicrm_create_error('Input Parameters empty');
}
$error = _civicrm_required_formatted_contact($params);
if (civicrm_error($error)) {
return $error;
}
$error = _civicrm_validate_formatted_contact($params);
if (civicrm_error($error)) {
return $error;
}
//get the prefix id etc if exists
require_once 'CRM/Contact/BAO/Contact.php';
CRM_Contact_BAO_Contact::resolveDefaults($params, TRUE);
require_once 'CRM/Import/Parser.php';
if (CRM_Utils_Array::value('onDuplicate', $params) != CRM_Import_Parser::DUPLICATE_NOCHECK) {
CRM_Core_Error::reset();
$error = _civicrm_duplicate_formatted_contact($params);
if (civicrm_error($error)) {
return $error;
}
}
$contact = CRM_Contact_BAO_Contact::create($params,
CRM_Utils_Array::value('fixAddress', $params)
);
_civicrm_object_to_array($contact, $contactArray);
return $contactArray;
}
/**
* Returns the number of Contact objects which match the search criteria specified in $params.
*
* @deprecated deprecated since version 2.2.3; civicrm_contact_get now returns a record_count value
*
* @param array $params
*
* @return int
* @access public
*/
function civicrm_contact_search_count(&$params) {
// convert the params to new format
require_once 'CRM/Contact/Form/Search.php';
$newP = CRM_Contact_BAO_Query::convertFormValues($params);
$query = new CRM_Contact_BAO_Query($newP);
return $query->searchQuery(0, 0, NULL, TRUE);
}
/**
* Ensure that we have the right input parameters for custom data
*
* @param array $params Associative array of property name/value
* pairs to insert in new contact.
* @param string $csType contact subtype if exists/passed.
*
* @return null on success, error message otherwise
* @access public
*/
function civicrm_contact_check_custom_params($params, $csType = NULL) {
empty($csType) ? $onlyParent = TRUE : $onlyParent = FALSE;
require_once 'CRM/Core/BAO/CustomField.php';
$customFields = CRM_Core_BAO_CustomField::getFields($params['contact_type'], FALSE, FALSE, $csType, NULL, $onlyParent);
foreach ($params as $key => $value) {
if ($customFieldID = CRM_Core_BAO_CustomField::getKeyID($key)) {
/* check if it's a valid custom field id */
if (!array_key_exists($customFieldID, $customFields)) {
$errorMsg = ts("Invalid Custom Field Contact Type: {$params['contact_type']}");
if ($csType) {
$errorMsg .= ts(" or Mismatched SubType: " . implode(', ', (array)$csType));
}
return civicrm_create_error($errorMsg);
}
}
}
}
<?php
// $Id$
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* CRM-6241
* Creating this file to maintain backward compatibility
* We should plan on deprecating this in 4.0
*
* @package CiviCRM_APIv2
* @subpackage API_Contact
* @copyright CiviCRM LLC (c) 2004-2013
* $Id: Contact.php 30415 2010-10-29 12:02:47Z shot $
*/
require_once 'api/v2/Contribution.php';
<?php
// $Id: Contribution.php 45502 2013-02-08 13:32:55Z kurund $
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* File for the CiviCRM APIv2 Contribution functions
*
* @package CiviCRM_APIv2
* @subpackage API_Contribute
*
* @copyright CiviCRM LLC (c) 2004-2013
* @version $Id: Contribution.php 45502 2013-02-08 13:32:55Z kurund $
*
*/
/**
* Include utility functions
*/
require_once 'api/v2/utils.php';
require_once 'CRM/Utils/Rule.php';
require_once 'CRM/Contribute/PseudoConstant.php';
/**
* Add or update a contribution
*
* @param array $params (reference ) input parameters
*
* @return array (reference ) contribution_id of created or updated record
* @static void
* @access public
*/
function &civicrm_contribution_create(&$params) {
_civicrm_initialize();
$error = _civicrm_contribute_check_params($params);
if (civicrm_error($error)) {
return $error;
}
$values = array();
require_once 'CRM/Contribute/BAO/Contribution.php';
$error = _civicrm_contribute_format_params($params, $values);
if (civicrm_error($error)) {
return $error;
}
$values['contact_id'] = $params['contact_id'];
$values['source'] = CRM_Utils_Array::value('source', $params);
$values['skipRecentView'] = TRUE;
$ids = array();
if (CRM_Utils_Array::value('id', $params)) {
$ids['contribution'] = $params['id'];
}
$contribution = CRM_Contribute_BAO_Contribution::create($values, $ids);
if (is_a($contribution, 'CRM_Core_Error')) {
return civicrm_create_error(ts($contribution->_errors[0]['message']));
}
_civicrm_object_to_array($contribution, $contributeArray);
return $contributeArray;
}
/*
* Deprecated wrapper function
* @deprecated
*/
function civicrm_contribution_add(&$params) {
$result = civicrm_contribution_create($params);
return $result;
}
/**
* Retrieve a specific contribution, given a set of input params
* If more than one contribution exists, return an error, unless
* the client has requested to return the first found contact
*
* @param array $params (reference ) input parameters
*
* @return array (reference ) array of properties, if error an array with an error id and error message
* @static void
* @access public
*/
function &civicrm_contribution_get(&$params) {
_civicrm_initialize();
$values = array();
if (empty($params)) {
return civicrm_create_error(ts('No input parameters present'));
}
if (!is_array($params)) {
return civicrm_create_error(ts('Input parameters is not an array'));
}
$contributions = &civicrm_contribution_search($params);
if (civicrm_error($contributions)) {
return $contributions;
}
if (count($contributions) != 1 &&
!$params['returnFirst']
) {
return civicrm_create_error(ts('%1 contributions matching input params', array(1 => count($contributions))),
$contributions
);
}
$contributions = array_values($contributions);
return $contributions[0];
}
/**
* Delete a contribution
*
* @param array $params (reference ) input parameters
*
* @return boolean true if success, else false
* @static void
* @access public
*/
function civicrm_contribution_delete(&$params) {
_civicrm_initialize();
$contributionID = CRM_Utils_Array::value('contribution_id', $params);
if (!$contributionID) {
return civicrm_create_error(ts('Could not find contribution_id in input parameters'));
}
require_once 'CRM/Contribute/BAO/Contribution.php';
if (CRM_Contribute_BAO_Contribution::deleteContribution($contributionID)) {
return civicrm_create_success();
}
else {
return civicrm_create_error(ts('Could not delete contribution'));
}
}
/**
* Retrieve a set of contributions, given a set of input params
*
* @param array $params (reference ) input parameters
* @param array $returnProperties Which properties should be included in the
* returned Contribution object. If NULL, the default
* set of properties will be included.
*
* @return array (reference ) array of contributions, if error an array with an error id and error message
* @static void
* @access public
*/
function &civicrm_contribution_search(&$params) {
_civicrm_initialize();
if (!is_array($params)) {
return civicrm_create_error(ts('Input parameters is not an array'));
}
$inputParams = array();
$returnProperties = array();
$otherVars = array('sort', 'offset', 'rowCount');
$sort = NULL;
$offset = 0;
$rowCount = 25;
foreach ($params as $n => $v) {
if (substr($n, 0, 7) == 'return.') {
$returnProperties[substr($n, 7)] = $v;
}
elseif (in_array($n, $otherVars)) {
$$n = $v;
}
else {
$inputParams[$n] = $v;
}
}
// add is_test to the clause if not present
if (!array_key_exists('contribution_test', $inputParams)) {
$inputParams['contribution_test'] = 0;
}
require_once 'CRM/Contribute/BAO/Query.php';
require_once 'CRM/Contact/BAO/Query.php';
if (empty($returnProperties)) {
$returnProperties = CRM_Contribute_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_CONTRIBUTE);
}
$newParams = CRM_Contact_BAO_Query::convertFormValues($inputParams);
$query = new CRM_Contact_BAO_Query($newParams, $returnProperties, NULL);
list($select, $from, $where, $having) = $query->query();
$sql = "$select $from $where $having";
if (!empty($sort)) {
$sql .= " ORDER BY $sort ";
}
$sql .= " LIMIT $offset, $rowCount ";
$dao = CRM_Core_DAO::executeQuery($sql);
$contribution = array();
while ($dao->fetch()) {
$contribution[$dao->contribution_id] = $query->store($dao);
}
$dao->free();
return $contribution;
}
/**
*
* @param <type> $params
*
* @return <type>
* @deprecated
*/
function &civicrm_contribution_format_create(&$params) {
_civicrm_initialize();
// return error if we have no params
if (empty($params)) {
return civicrm_create_error('Input Parameters empty');
}
$error = _civicrm_contribute_check_params($params);
if (civicrm_error($error)) {
return $error;
}
$values = array();
$error = _civicrm_contribute_format_params($params, $values);
if (civicrm_error($error)) {
return $error;
}
$error = _civicrm_contribute_duplicate_check($params);
if (civicrm_error($error)) {
return $error;
}
$ids = array();
CRM_Contribute_BAO_Contribution::resolveDefaults($params, TRUE);
$contribution = CRM_Contribute_BAO_Contribution::create($params, $ids);
_civicrm_object_to_array($contribution, $contributeArray);
return $contributeArray;
}
/**
* This function ensures that we have the right input contribution parameters
*
* We also need to make sure we run all the form rules on the params list
* to ensure that the params are valid
*
* @param array $params Associative array of property name/value
* pairs to insert in new contribution.
*
* @return bool|CRM_Utils_Error
* @access private
*/
function _civicrm_contribute_check_params(&$params) {
static $required = array('contact_id' => NULL,
'total_amount' => NULL,
'financial_type_id' => 'financial_type',
);
// params should be an array
if (!is_array($params)) {
return civicrm_create_error(ts('Input parameters is not an array'));
}
// cannot create a contribution with empty params
if (empty($params)) {
return civicrm_create_error('Input Parameters empty');
}
$valid = TRUE;
$error = '';
// check params for contribution id during update
if (CRM_Utils_Array::value('id', $params)) {
require_once 'CRM/Contribute/BAO/Contribution.php';
$contributor = new CRM_Contribute_BAO_Contribution();
$contributor->id = $params['id'];
if (!$contributor->find(TRUE)) {
return civicrm_create_error(ts('Contribution id is not valid'));
}
// do not check other field during update
return array();
}
foreach ($required as $field => $eitherField) {
if (!CRM_Utils_Array::value($field, $params)) {
if ($eitherField && CRM_Utils_Array::value($eitherField, $params)) {
continue;
}
$valid = FALSE;
$error .= $field;
break;
}
}
if (!$valid) {
return civicrm_create_error("Required fields not found for contribution $error");
}
return array();
}
/**
* Check if there is a contribution with the same trxn_id or invoice_id
*
* @param array $params Associative array of property name/value
* pairs to insert in new contribution.
*
* @return array|CRM_Error
* @access private
*/
function _civicrm_contribute_duplicate_check(&$params) {
require_once 'CRM/Contribute/BAO/Contribution.php';
$duplicates = array();
$result = CRM_Contribute_BAO_Contribution::checkDuplicate($params, $duplicates);
if ($result) {
$d = implode(', ', $duplicates);
$error = CRM_Core_Error::createError("Duplicate error - existing contribution record(s) have a matching Transaction ID or Invoice ID. Contribution record ID(s) are: $d", CRM_Core_Error::DUPLICATE_CONTRIBUTION, 'Fatal', $d);
return civicrm_create_error($error->pop(),
$d
);
}
else {
return array();
}
}
/**
* take the input parameter list as specified in the data model and
* convert it into the same format that we use in QF and BAO object
*
* @param array $params Associative array of property name/value
* pairs to insert in new contact.
* @param array $values The reformatted properties that we can use internally
* '
*
* @return array|CRM_Error
* @access public
*/
function _civicrm_contribute_format_params(&$params, &$values, $create = FALSE) {
// copy all the contribution fields as is
$fields = CRM_Contribute_DAO_Contribution::fields();
_civicrm_store_values($fields, $params, $values);
foreach ($params as $key => $value) {
// ignore empty values or empty arrays etc
if (CRM_Utils_System::isNull($value)) {
continue;
}
switch ($key) {
case 'contribution_contact_id':
if (!CRM_Utils_Rule::integer($value)) {
return civicrm_create_error("contact_id not valid: $value");
}
$dao = new CRM_Core_DAO();
$qParams = array();
$svq = $dao->singleValueQuery("SELECT id FROM civicrm_contact WHERE id = $value",
$qParams
);
if (!$svq) {
return civicrm_create_error("Invalid Contact ID: There is no contact record with contact_id = $value.");
}
$values['contact_id'] = $values['contribution_contact_id'];
unset($values['contribution_contact_id']);
break;
case 'receive_date':
case 'cancel_date':
case 'receipt_date':
case 'thankyou_date':
if (!CRM_Utils_Rule::dateTime($value)) {
return civicrm_create_error("$key not a valid date: $value");
}
break;
case 'non_deductible_amount':
case 'total_amount':
case 'fee_amount':
case 'net_amount':
if (!CRM_Utils_Rule::money($value)) {
return civicrm_create_error("$key not a valid amount: $value");
}
break;
case 'currency':
if (!CRM_Utils_Rule::currencyCode($value)) {
return civicrm_create_error("currency not a valid code: $value");
}
break;
case 'financial_type_id' :
if (!CRM_Utils_Array::value($value, CRM_Contribute_PseudoConstant::financialType())) {
return civicrm_create_error('Invalid Financial Type Id');
}
break;
case 'financial_type':
$contributionTypeId = CRM_Utils_Array::key(ucfirst($value),
CRM_Contribute_PseudoConstant::financialType()
);
if ($contributionTypeId) {
if (CRM_Utils_Array::value('financial_type_id', $values) &&
$contributionTypeId != $values['financial_type_id']
) {
return civicrm_create_error('Mismatched Financial Type and Financial Type Id');
}
$values['financial_type_id'] = $contributionTypeId;
}
else {
return civicrm_create_error('Invalid Financial Type');
}
break;
case 'payment_instrument':
require_once 'CRM/Core/OptionGroup.php';
$values['payment_instrument_id'] = CRM_Core_OptionGroup::getValue('payment_instrument', $value);
break;
case 'soft_credit_to':
if (!CRM_Utils_Rule::integer($value)) {
return civicrm_create_error("$key not a valid Id: $value");
}
$values['soft_credit_to'] = $value;
break;
default:
break;
}
}
if (array_key_exists('note', $params)) {
$values['note'] = $params['note'];
}
_civicrm_custom_format_params($params, $values, 'Contribution');
if ($create) {
// CRM_Contribute_BAO_Contribution::add() handles contribution_source
// So, if $values contains contribution_source, convert it to source
$changes = array('contribution_source' => 'source');
foreach ($changes as $orgVal => $changeVal) {
if (isset($values[$orgVal])) {
$values[$changeVal] = $values[$orgVal];
unset($values[$orgVal]);
}
}
}
return array();
}
/**
* Process a transaction and record it against the contact.
*
* @param array $params (reference ) input parameters
*
* @return array (reference ) contribution of created or updated record (or a civicrm error)
* @static void
* @access public
*
*/
function civicrm_contribute_transact($params) {
civicrm_initialize();
if (empty($params)) {
return civicrm_create_error(ts('No input parameters present'));
}
if (!is_array($params)) {
return civicrm_create_error(ts('Input parameters is not an array'));
}
$values = array();
require_once 'CRM/Contribute/BAO/Contribution.php';
$error = _civicrm_contribute_format_params($params, $values);
if (civicrm_error($error)) {
return $error;
}
$required = array(
'amount',
);
foreach ($required as $key) {
if (!isset($params[$key])) {
return civicrm_create_error("Missing parameter $key: civicrm_contribute_transact() requires a parameter '$key'.");
}
}
// allow people to omit some values for convenience
$defaults = array(
// 'payment_processor_id' => NULL /* we could retrieve the default processor here, but only if it's missing to avoid an extra lookup */
'payment_processor_mode' => 'live',
);
$params = array_merge($defaults, $params);
// clean up / adjust some values which
if (!isset($params['total_amount'])) {
$params['total_amount'] = $params['amount'];
}
if (!isset($params['net_amount'])) {
$params['net_amount'] = $params['amount'];
}
if (!isset($params['receive_date'])) {
$params['receive_date'] = date('Y-m-d');
}
if (!isset($params['invoiceID']) && isset($params['invoice_id'])) {
$params['invoiceID'] = $params['invoice_id'];
}
require_once 'CRM/Financial/BAO/PaymentProcessor.php';
$paymentProcessor = CRM_Financial_BAO_PaymentProcessor::getPayment($params['payment_processor_id'],
$params['payment_processor_mode']
);
if (civicrm_error($paymentProcessor)) {
return $paymentProcessor;
}
require_once 'CRM/Core/Payment.php';
$payment = CRM_Core_Payment::singleton($params['payment_processor_mode'], $paymentProcessor);
if (civicrm_error($payment)) {
return $payment;
}
$transaction = $payment->doDirectPayment($params);
if (civicrm_error($transaction)) {
return $transaction;
}
// but actually, $payment->doDirectPayment() doesn't return a
// CRM_Core_Error by itself
if (get_class($transaction) == 'CRM_Core_Error') {
$errs = $transaction->getErrors();
if (!empty($errs)) {
$last_error = array_shift($errs);
return CRM_Core_Error::createApiError($last_error['message']);
}
}
$contribution = civicrm_contribution_add($params);
return $contribution;
}
<?php
// $Id: CustomGroup.php 45502 2013-02-08 13:32:55Z kurund $
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* File for the CiviCRM APIv2 custom group functions
*
* @package CiviCRM_APIv2
* @subpackage API_CustomGroup
*
* @copyright CiviCRM LLC (c) 2004-2013
* @version $Id: CustomGroup.php 45502 2013-02-08 13:32:55Z kurund $
*/
/**
* Files required for this package
*/
require_once 'api/v2/utils.php';
/**
* Most API functions take in associative arrays ( name => value pairs
* as parameters. Some of the most commonly used parameters are
* described below
*
* @param array $params an associative array used in construction
* retrieval of the object
*
*
*/
/**
* Use this API to create a new group. See the CRM Data Model for custom_group property definitions
* $params['class_name'] is a required field, class being extended.
*
* @param $params array Associative array of property name/value pairs to insert in group.
*
*
* @return Newly create custom_group object
*
* @access public
*/
function civicrm_custom_group_create($params) {
_civicrm_initialize();
if (!is_array($params)) {
return civicrm_create_error("params is not an array");
}
// Require either param['class_name'] (string) - for backwards compatibility - OR parm['extends'] (array)
// If passing extends array - set class_name (e.g. 'Contact', 'Participant'...) as extends[0]. You may optionally
// pass an extends_entity_column_value as extends[1] (e.g. an Activity Type ID).
if (isset($params['class_name']) && trim($params['class_name'])) {
$params['extends'][0] = trim($params['class_name']);
}
else {
if (!isset($params['extends']) || !is_array($params['extends'])) {
return civicrm_create_error("Params must include either 'class_name' (string) or 'extends' (array).");
}
else {
if (!isset($params['extends'][0]) || !trim($params['extends'][0])) {
return civicrm_create_error("First item in params['extends'] must be a class name (e.g. 'Contact').");
}
}
}
$error = _civicrm_check_required_fields($params, 'CRM_Core_DAO_CustomGroup');
require_once 'CRM/Utils/String.php';
if (!isset($params['title']) ||
!trim($params['title'])
) {
return civicrm_create_error("Title parameter is required.");
}
if (!isset($params['style']) || !trim($params['style'])) {
$params['style'] = 'Inline';
}
if (is_a($error, 'CRM_Core_Error')) {
return civicrm_create_error($error->_errors[0]['message']);
}
require_once 'CRM/Core/BAO/CustomGroup.php';
$customGroup = CRM_Core_BAO_CustomGroup::create($params);
_civicrm_object_to_array($customGroup, $values);
if (is_a($customGroup, 'CRM_Core_Error')) {
return civicrm_create_error($customGroup->_errors[0]['message']);
}
else {
$values['is_error'] = 0;
}
if (CRM_Utils_Array::value('html_type', $params)) {
$params['custom_group_id'] = $customGroup->id;
$fieldValues = civicrm_custom_field_create($params);
$values = array_merge($values, $fieldValues['result']);
}
return $values;
}
/**
* Use this API to delete an existing group.
*
* @param array id of the group to be deleted
*
* @return Null if success
* @access public
**/
function civicrm_custom_group_delete($params) {
_civicrm_initialize();
if (!is_array($params)) {
return civicrm_create_error('Params is not an array');
}
if (!CRM_Utils_Array::value('id', $params)) {
return civicrm_create_error('Invalid or no value for Custom group ID');
}
// convert params array into Object
require_once 'CRM/Core/DAO/CustomGroup.php';
$values = new CRM_Core_DAO_CustomGroup();
$values->id = $params['id'];
$values->find(TRUE);
require_once 'CRM/Core/BAO/CustomGroup.php';
$result = CRM_Core_BAO_CustomGroup::deleteGroup($values);
return $result ? civicrm_create_success() : civicrm_error('Error while deleting custom group');
}
/**
* Defines 'custom field' within a group.
*
*
* @param $params array Associative array of property name/value pairs to create new custom field.
*
* @return Newly created custom_field id array
*
* @access public
*
*/
function civicrm_custom_field_create($params) {
_civicrm_initialize();
if (!is_array($params)) {
return civicrm_create_error("params is not an array ");
}
if (!CRM_Utils_Array::value('custom_group_id', $params)) {
return civicrm_create_error("Missing Required field :custom_group_id");
}
if (!(CRM_Utils_Array::value('label', $params))) {
return civicrm_create_error("Missing Required field :label");
}
if (!(CRM_Utils_Array::value('option_type', $params))) {
if (CRM_Utils_Array::value('id', $params)) {
$params['option_type'] = 2;
}
else {
$params['option_type'] = 1;
}
}
$error = _civicrm_check_required_fields($params, 'CRM_Core_DAO_CustomField');
if (is_a($error, 'CRM_Core_Error')) {
return civicrm_create_error($error->_errors[0]['message']);
}
// Array created for passing options in params
if (isset($params['option_values']) && is_array($params['option_values'])) {
foreach ($params['option_values'] as $key => $value) {
$params['option_label'][$value['weight']] = $value['label'];
$params['option_value'][$value['weight']] = $value['value'];
$params['option_status'][$value['weight']] = $value['is_active'];
$params['option_weight'][$value['weight']] = $value['weight'];
}
}
require_once 'CRM/Core/BAO/CustomField.php';
$customField = CRM_Core_BAO_CustomField::create($params);
$values['customFieldId'] = $customField->id;
if (is_a($customField, 'CRM_Core_Error') && is_a($column, 'CRM_Core_Error')) {
return civicrm_create_error($customField->_errors[0]['message']);
}
else {
return civicrm_create_success($values);
}
}
/**
* Use this API to delete an existing custom group field.
*
* @param $params Array id of the field to be deleted
*
*
* @access public
**/
function civicrm_custom_field_delete($params) {
_civicrm_initialize();
if (!is_array($params)) {
return civicrm_create_error('Params is not an array');
}
if (!CRM_Utils_Array::value('id', $params)) {
return civicrm_create_error('Invalid or no value for Custom Field ID');
}
require_once 'CRM/Core/DAO/CustomField.php';
$field = new CRM_Core_DAO_CustomField();
$field->id = $params['id'];
$field->find(TRUE);
require_once 'CRM/Core/BAO/CustomField.php';
$customFieldDelete = CRM_Core_BAO_CustomField::deleteField($field);
return $customFieldDelete ? civicrm_create_error('Error while deleting custom field') : civicrm_create_success();
}
<?php
// $Id: Domain.php 45502 2013-02-08 13:32:55Z kurund $
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* File for the CiviCRM APIv2 domain functions
*
* @package CiviCRM_APIv2
* @subpackage API_Domain
*
* @copyright CiviCRM LLC (c) 2004-2013
* @version $Id: Domain.php 45502 2013-02-08 13:32:55Z kurund $
*
*/
/**
* Include utility functions
*/
require_once 'api/v2/utils.php';
/**
* Generic file to retrieve all the constants and
* pseudo constants used in CiviCRM
*
*/
function civicrm_domain_get() {
require_once 'CRM/Core/BAO/Domain.php';
$dao = CRM_Core_BAO_Domain::getDomain();
$values = array();
$params = array(
'entity_id' => $dao->id,
'entity_table' => 'civicrm_domain',
);
require_once 'CRM/Core/BAO/Location.php';
$values['location'] = CRM_Core_BAO_Location::getValues($params, TRUE);
$address_array = array(
'street_address', 'supplemental_address_1', 'supplemental_address_2',
'city', 'state_province_id', 'postal_code', 'country_id', 'geo_code_1', 'geo_code_2',
);
require_once 'CRM/Core/OptionGroup.php';
$domain[$dao->id] = array(
'id' => $dao->id,
'domain_name' => $dao->name,
'description' => $dao->description,
'domain_email' => CRM_Utils_Array::value('email', $values['location']['email'][1]),
'domain_phone' => array(
'phone_type' => CRM_Core_OptionGroup::getLabel('phone_type', CRM_Utils_Array::value('phone_type_id', $values['location']['phone'][1])),
'phone' => CRM_Utils_Array::value('phone', $values['location']['phone'][1]),
),
);
foreach ($address_array as $value) {
$domain[$dao->id]['domain_address'][$value] = CRM_Utils_Array::value($value, $values['location']['address'][1]);
}
list($domain[$dao->id]['from_name'], $domain[$dao->id]['from_email']) = CRM_Core_BAO_Domain::getNameAndEmail();
return $domain;
}
/**
* Create a new domain
*
* @param array $params
*
* @return array
*/
function civicrm_domain_create($params) {
require_once 'CRM/Core/BAO/Domain.php';
if (!is_array($params)) {
return civicrm_create_error('Params need to be of type array!');
}
if (empty($params)) {
return civicrm_create_error('Params cannot be empty!');
}
$domain = CRM_Core_BAO_Domain::create($params);
$domain_array = array();
_civicrm_object_to_array($domain, $domain_array);
return $domain_array;
}
<?php
// $Id: EntityTag.php 45502 2013-02-08 13:32:55Z kurund $
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* File for the CiviCRM APIv2 entity tag functions
*
* @package CiviCRM_APIv2
* @subpackage API_EntityTag
*
* @copyright CiviCRM LLC (c) 2004-2013
* @version $Id: EntityTag.php 45502 2013-02-08 13:32:55Z kurund $
*/
/**
* Include utility functions
*/
require_once 'api/v2/utils.php';
/**
*
* @param <type> $params
*
* @return <type>
*/
function civicrm_entity_tag_get(&$params) {
if (!is_array($params)) {
return civicrm_create_error(ts('params should be an array.'));
}
$entityID = NULL;
$entityTable = 'civicrm_contact';
if (!($entityID = CRM_Utils_Array::value('entity_id', $params))) {
$entityID = CRM_Utils_Array::value('contact_id', $params);
}
if (empty($entityID)) {
return civicrm_create_error(ts('entity_id is a required field.'));
}
if (CRM_Utils_Array::value('entity_table', $params)) {
$entityTable = $params['entity_table'];
}
require_once 'CRM/Core/BAO/EntityTag.php';
$values = CRM_Core_BAO_EntityTag::getTag($entityID, $entityTable);
$result = array();
foreach ($values as $v) {
$result[] = array('tag_id' => $v);
}
return $result;
}
/**
*
* @param <type> $params
*
* @return <type>
*/
function civicrm_entity_tag_display(&$params) {
if (!is_array($params)) {
return civicrm_create_error(ts('params should be an array.'));
}
$entityID = NULL;
$entityTable = 'civicrm_contact';
if (!($entityID = CRM_Utils_Array::value('entity_id', $params))) {
$entityID = CRM_Utils_Array::value('contact_id', $params);
}
if (empty($entityID)) {
return civicrm_create_error(ts('entity_id is a required field.'));
}
if (CRM_Utils_Array::value('entity_table', $params)) {
$entityTable = $params['entity_table'];
}
require_once 'CRM/Core/BAO/EntityTag.php';
$values = CRM_Core_BAO_EntityTag::getTag($entityID, $entityTable);
$result = array();
$tags = CRM_Core_PseudoConstant::tag();
foreach ($values as $v) {
$result[] = $tags[$v];
}
return implode(',', $result);
}
/**
* Returns all entities assigned to a specific Tag.
*
* @param $params Array an array valid Tag id
*
* @return $entities Array An array of entity ids.
* @access public
*/
function civicrm_tag_entities_get(&$params) {
require_once 'CRM/Core/BAO/Tag.php';
require_once 'CRM/Core/BAO/EntityTag.php';
$tag = new CRM_Core_BAO_Tag();
$tag->id = CRM_Utils_Array::value('tag_id', $params) ? $params['tag_id'] : NULL;
$entities = CRM_Core_BAO_EntityTag::getEntitiesByTag($tag);
return $entities;
}
/**
*
* @param <type> $params
*
* @return <type>
* @deprecated
*/
function civicrm_entity_tag_add(&$params) {
return civicrm_entity_tag_common($params, 'add');
}
/**
*
* @param <type> $params
*
* @return <type>
*/
function civicrm_entity_tag_create(&$params) {
return civicrm_entity_tag_common($params, 'add');
}
/**
*
* @param <type> $params
*
* @return <type>
* @deprecated
*/
function civicrm_entity_tag_remove(&$params) {
return civicrm_entity_tag_common($params, 'remove');
}
/**
*
* @param array $params
*
* @return <type>
*/
function civicrm_entity_tag_delete(&$params) {
return civicrm_entity_tag_common($params, 'remove');
}
/**
*
* @param <type> $params
* @param <type> $op
*
* @return <type>
*/
function civicrm_entity_tag_common(&$params, $op = 'add') {
$entityIDs = array();
$tagsIDs = array();
$entityTable = 'civicrm_contact';
if (is_array($params)) {
foreach ($params as $n => $v) {
if ((substr($n, 0, 10) == 'contact_id') || (substr($n, 0, 9) == 'entity_id')) {
$entityIDs[] = $v;
}
elseif (substr($n, 0, 6) == 'tag_id') {
$tagIDs[] = $v;
}
elseif (substr($n, 0, 12) == 'entity_table') {
$entityTable = $v;
}
}
}
if (empty($entityIDs)) {
return civicrm_create_error(ts('contact_id is a required field'));
}
if (empty($tagIDs)) {
return civicrm_create_error(ts('tag_id is a required field'));
}
require_once 'CRM/Core/BAO/EntityTag.php';
$values = array('is_error' => 0);
if ($op == 'add') {
$values['total_count'] = $values['added'] = $values['not_added'] = 0;
foreach ($tagIDs as $tagID) {
list($te, $a, $na) = CRM_Core_BAO_EntityTag::addEntitiesToTag($entityIDs, $tagID, $entityTable);
$values['total_count'] += $te;
$values['added'] += $a;
$values['not_added'] += $na;
}
}
else {
$values['total_count'] = $values['removed'] = $values['not_removed'] = 0;
foreach ($tagIDs as $tagID) {
list($te, $r, $nr) = CRM_Core_BAO_EntityTag::removeEntitiesFromTag($entityIDs, $tagID, $entityTable);
$values['total_count'] += $te;
$values['removed'] += $r;
$values['not_removed'] += $nr;
}
}
return $values;
}
<?php
// $Id: Event.php 45502 2013-02-08 13:32:55Z kurund $
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* File for the CiviCRM APIv2 event functions
*
* @package CiviCRM_APIv2
* @subpackage API_Event
*
* @copyright CiviCRM LLC (c) 2004-2013
* @version $Id: Event.php 45502 2013-02-08 13:32:55Z kurund $
*
*/
/**
* Files required for this package
*/
require_once 'api/v2/utils.php';
/**
* Create a Event
*
* This API is used for creating a Event
*
* @param array $params (reference ) input parameters
* Allowed @params array keys are:
* {@schema Event/Event.xml}
*
* @return array of newly created event property values.
* @access public
*/
function civicrm_event_create(&$params) {
_civicrm_initialize();
$errorScope = CRM_Core_TemporaryErrorScope::useException();
try {
civicrm_api_check_permission(__FUNCTION__, $params, TRUE);
civicrm_verify_mandatory($params, 'CRM_Event_DAO_Event', array('start_date', 'event_type_id', 'title'));
// Do we really want $params[id], even if we have
// $params[event_id]? if yes then please uncomment the below line
//$ids['event' ] = $params['id'];
$ids['eventTypeId'] = (int) $params['event_type_id'];
$ids['startDate'] = $params['start_date'];
$ids['event_id'] = CRM_Utils_Array::value('event_id', $params);
require_once 'CRM/Event/BAO/Event.php';
$eventBAO = CRM_Event_BAO_Event::create($params, $ids);
if (is_a($eventBAO, 'CRM_Core_Error')) {
return civicrm_create_error("Event is not created");
}
else {
$event = array();
_civicrm_object_to_array($eventBAO, $event);
$values = array();
$values['event_id'] = $event['id'];
$values['is_error'] = 0;
}
return $values;
}
catch(Exception$e) {
return civicrm_create_error($e->getMessage());
}
}
/**
* Get an Event.
*
* This api is used to retrieve all data for an existing Event.
* Required parameters : id of event
*
* @param array $params an associative array of title/value property values of civicrm_event
*
* @return If successful array of event data; otherwise object of CRM_Core_Error.
* @access public
*/
function civicrm_event_get(&$params) {
_civicrm_initialize();
if (!is_array($params)) {
return civicrm_create_error('Input parameters is not an array.');
}
if (empty($params)) {
return civicrm_create_error('Params cannot be empty.');
}
$event = &civicrm_event_search($params);
if (count($event) != 1 &&
!CRM_Utils_Array::value('returnFirst', $params)
) {
return civicrm_create_error(ts('%1 events matching input params', array(1 => count($event))));
}
if (civicrm_error($event)) {
return $event;
}
$event = array_values($event);
$event[0]['is_error'] = 0;
return $event[0];
}
/**
* Get Event record.
*
*
* @param array $params an associative array of name/value property values of civicrm_event
*
* @return Array of all found event property values.
* @access public
*/
function civicrm_event_search(&$params) {
if (!is_array($params)) {
return civicrm_create_error(ts('Input parameters is not an array.'));
}
$inputParams = array();
$returnProperties = array();
$returnCustomProperties = array();
$otherVars = array('sort', 'offset', 'rowCount', 'isCurrent');
$sort = array_key_exists('return.sort', $params) ? $params['return.sort'] : FALSE;
// don't check if empty, more meaningful error for API user instead of silent defaults
$offset = array_key_exists('return.offset', $params) ? $params['return.offset'] : 0;
$rowCount = array_key_exists('return.max_results', $params) ? $params['return.max_results'] : 25;
$isCurrent = array_key_exists('isCurrent', $params) ? $params['isCurrent'] : 0;
foreach ($params as $n => $v) {
if (substr($n, 0, 7) == 'return.') {
if (substr($n, 0, 14) == 'return.custom_') {
//take custom return properties separate
$returnCustomProperties[] = substr($n, 7);
}
elseif (!in_array(substr($n, 7), array(
'sort', 'offset', 'max_results'))) {
$returnProperties[] = substr($n, 7);
}
}
elseif (in_array($n, $otherVars)) {
$$n = $v;
}
else {
$inputParams[$n] = $v;
}
}
if (!empty($returnProperties)) {
$returnProperties[] = 'id';
$returnProperties[] = 'event_type_id';
}
require_once 'CRM/Core/BAO/CustomGroup.php';
require_once 'CRM/Event/BAO/Event.php';
$eventDAO = new CRM_Event_BAO_Event();
$eventDAO->copyValues($inputParams);
$event = array();
if (!empty($returnProperties)) {
$eventDAO->selectAdd();
$eventDAO->selectAdd(implode(',', $returnProperties));
}
$eventDAO->whereAdd('( is_template IS NULL ) OR ( is_template = 0 )');
if ($isCurrent) {
$eventDAO->whereAdd('(start_date >= CURDATE() || end_date >= CURDATE())');
}
$eventDAO->orderBy($sort);
$eventDAO->limit((int)$offset, (int)$rowCount);
$eventDAO->find();
while ($eventDAO->fetch()) {
$event[$eventDAO->id] = array();
CRM_Core_DAO::storeValues($eventDAO, $event[$eventDAO->id]);
$groupTree = &CRM_Core_BAO_CustomGroup::getTree('Event',
CRM_Core_DAO::$_nullObject,
$eventDAO->id,
FALSE,
$eventDAO->event_type_id
);
$groupTree = CRM_Core_BAO_CustomGroup::formatGroupTree($groupTree, 1, CRM_Core_DAO::$_nullObject);
$defaults = array();
CRM_Core_BAO_CustomGroup::setDefaults($groupTree, $defaults);
if (!empty($defaults)) {
foreach ($defaults as $key => $val) {
if (!empty($returnCustomProperties)) {
$customKey = explode('_', $key);
//show only return properties
if (in_array('custom_' . $customKey['1'], $returnCustomProperties)) {
$event[$eventDAO->id][$key] = $val;
}
}
else {
$event[$eventDAO->id][$key] = $val;
}
}
}
}
//end of the loop
$eventDAO->free();
return $event;
}
/**
* Deletes an existing event
*
* This API is used for deleting a event
*
* @param Array $params array containing event_id to be deleted
*
* @return boolean true if success, error otherwise
* @access public
*/
function civicrm_event_delete(&$params) {
if (empty($params)) {
return civicrm_create_error(ts('No input parameters present'));
}
$eventID = NULL;
$eventID = CRM_Utils_Array::value('event_id', $params);
if (!isset($eventID)) {
return civicrm_create_error(ts('Invalid value for eventID'));
}
require_once 'CRM/Event/BAO/Event.php';
return CRM_Event_BAO_Event::del($eventID) ? civicrm_create_success() : civicrm_create_error(ts('Error while deleting event'));
}
<?php
// $Id$
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* Definition of the Tag of the CRM API.
* More detailed documentation can be found
* {@link http://objectledge.org/confluence/display/CRM/CRM+v1.0+Public+APIs
* here}
*
* @package CiviCRM_APIv2
* @subpackage API_File
* @copyright CiviCRM LLC (c) 2004-2013
* $Id: $
*
*/
/**
* Files required for this package
*/
require_once 'api/v2/utils.php';
/**
* Create a file
*
* This API is used for creating a file
*
* @param array $params an associative array of name/value property values of civicrm_file
*
* @return array of newly created file property values.
* @access public
*/
function civicrm_file_create($params) {
if (!is_array($params)) {
return civicrm_create_error('Params is not an array.');
}
if (!isset($params['file_type_id'])) {
return civicrm_create_error('Required parameter missing.');
}
if (!isset($params['upload_date'])) {
$params['upload_date'] = date("Ymd");
}
require_once 'CRM/Core/DAO/File.php';
$fileDAO = new CRM_Core_DAO_File();
$properties = array('id', 'file_type_id', 'mime_type', 'uri', 'document', 'description', 'upload_date');
foreach ($properties as $name) {
if (array_key_exists($name, $params)) {
$fileDAO->$name = $params[$name];
}
}
$fileDAO->save();
$file = array();
_civicrm_object_to_array($fileDAO, $file);
return $file;
}
/**
* Get a file.
*
* This api is used for finding an existing file.
* Required parameters : id OR file_type_id of a file
*
* @param array $params an associative array of name/value property values of civicrm_file
*
* @return Array of all found file object property values.
* @access public
*/
function civicrm_file_get($params) {
if (!is_array($params)) {
return civicrm_create_error('params is not an array.');
}
if (!isset($params['id']) && !isset($params['file_type_id'])) {
return civicrm_create_error('Required parameters missing.');
}
require_once 'CRM/Core/DAO/File.php';
$fileDAO = new CRM_Core_DAO_File();
$properties = array('id', 'file_type_id', 'mime_type', 'uri', 'document', 'description', 'upload_date');
foreach ($properties as $name) {
if (array_key_exists($name, $params)) {
$fileDAO->$name = $params[$name];
}
}
if ($fileDAO->find()) {
$file = array();
while ($fileDAO->fetch()) {
_civicrm_object_to_array(clone($fileDAO), $file);
$files[$fileDAO->id] = $file;
}
}
else {
return civicrm_create_error('Exact match not found');
}
return $files;
}
/**
* Update an existing file
*
* This api is used for updating an existing file.
* Required parrmeters : id of a file
*
* @param Array $params an associative array of name/value property values of civicrm_file
*
* @return array of updated file object property values
* @access public
*/
function &civicrm_file_update(&$params) {
if (!is_array($params)) {
return civicrm_create_error('Params is not an array');
}
if (!isset($params['id'])) {
return civicrm_create_error('Required parameter missing');
}
require_once 'CRM/Core/DAO/File.php';
$fileDAO = new CRM_Core_DAO_File();
$fileDAO->id = $params['id'];
if ($fileDAO->find(TRUE)) {
$fileDAO->copyValues($params);
if (!$params['upload_date'] && !$fileDAO->upload_date) {
$fileDAO->upload_date = date("Ymd");
}
$fileDAO->save();
}
$file = array();
_civicrm_object_to_array(clone($fileDAO), $file);
return $file;
}
/**
* Deletes an existing file
*
* This API is used for deleting a file
* Required parameters : id of a file
*
* @param Int $fileId Id of the file to be deleted
*
* @return null if successfull, object of CRM_Core_Error otherwise
* @access public
*/
function &civicrm_file_delete($fileId) {
if (empty($fileId)) {
return civicrm_create_error('Required parameter missing');
}
$check = FALSE;
require_once 'CRM/Core/DAO/EntityFile.php';
$entityFileDAO = new CRM_Core_DAO_EntityFile();
$entityFileDAO->file_id = $fileId;
if ($entityFileDAO->find()) {
$check = $entityFileDAO->delete();
}
require_once 'CRM/Core/DAO/File.php';
$fileDAO = new CRM_Core_DAO_File();
$fileDAO->id = $fileId;
if ($fileDAO->find(TRUE)) {
$check = $fileDAO->delete();
}
return $check ? NULL : civicrm_create_error('Error while deleting a file.');
}
/**
* Assigns an entity to a file
*
* @param object $file id of a file
* @param object $entity id of a entity
* @param string $entity_table
*
* @return array of newly created entity-file object properties
* @access public
*/
function civicrm_entity_file_create(&$fileID, &$entityID, $entity_table = 'civicrm_contact') {
require_once 'CRM/Core/DAO/EntityFile.php';
if (!$fileID || !$entityID) {
return civicrm_create_error('Required parameters missing');
}
$params = array(
'entity_id' => $entityID,
'file_id' => $fileID,
'entity_table' => $entity_table,
);
$entityFileDAO = new CRM_Core_DAO_EntityFile();
$entityFileDAO->copyValues($params);
$entityFileDAO->save();
$entityFile = array();
_civicrm_object_to_array($entityFileDAO, $entityFile);
return $entityFile;
}
/**
* Attach a file to a given entity
*
* @param string $name filename
* @param object $entityID id of the supported entity.
* @param string $entity_table
*
* @access public
*/
function civicrm_file_by_entity_add($name, $entityID, $entityTable = 'civicrm_contact', $params) {
require_once 'CRM/Core/BAO/File.php';
CRM_Core_BAO_File::filePostProcess($name, NULL, $entityTable, $entityID, NULL, FALSE, $params);
}
/**
* Returns all files assigned to a single entity instance.
*
* @param object $entityID id of the supported entity.
* @param string $entity_table
*
* @return array nested array of entity-file property values.
* @access public
*/
function civicrm_files_by_entity_get($entityID, $entityTable = 'civicrm_contact', $fileID = NULL) {
if (!$entityID) {
return civicrm_create_error('Required parameters missing');
}
require_once 'CRM/Core/DAO/EntityFile.php';
require_once 'CRM/Core/DAO/File.php';
$entityFileDAO = new CRM_Core_DAO_EntityFile();
$entityFileDAO->entity_table = $entityTable;
$entityFileDAO->entity_id = $entityID;
if ($fileID) {
$entityFileDAO->file_id = $fileID;
}
if ($entityFileDAO->find()) {
$entityFile = array();
while ($entityFileDAO->fetch()) {
_civicrm_object_to_array($entityFileDAO, $entityFile);
$files[$entityFileDAO->file_id] = $entityFile;
if (array_key_exists('file_id', $files[$entityFileDAO->file_id])) {
$fileDAO = new CRM_Core_DAO_File();
$fileDAO->id = $entityFile['file_id'];
$fileDAO->find(TRUE);
_civicrm_object_to_array($fileDAO, $files[$entityFileDAO->file_id]);
}
if (CRM_Utils_Array::value('file_type_id', $files[$entityFileDAO->file_id])) {
$files[$entityFileDAO->file_id]['file_type'] = CRM_Core_OptionGroup::getLabel('file_type',
$files[$entityFileDAO->file_id]['file_type_id']
);
}
}
}
else {
return civicrm_create_error('Exact match not found');
}
return $files;
}
/**
* Deletes an existing entity file assignment.
* Required parameters : 1. id of an entity-file
* 2. entity_id and entity_table of an entity-file
*
* @param array $params an associative array of name/value property values of civicrm_entity_file.
*
* @return null if successfull, object of CRM_Core_Error otherwise
* @access public
*/
function civicrm_entity_file_delete(&$params) {
require_once 'CRM/Core/DAO/EntityFile.php';
//if ( ! isset($params['id']) && ( !isset($params['entity_id']) || !isset($params['entity_file']) ) ) {
if (!isset($params['id']) && (!isset($params['entity_id']) || !isset($params['entity_table']))) {
return civicrm_create_error('Required parameters missing');
}
$entityFileDAO = new CRM_Core_DAO_EntityFile();
$properties = array('id', 'entity_id', 'entity_table', 'file_id');
foreach ($properties as $name) {
if (array_key_exists($name, $params)) {
$entityFileDAO->$name = $params[$name];
}
}
return $entityFileDAO->delete() ? NULL : civicrm_create_error('Error while deleting');
}
<?php
// $Id: Group.php 45502 2013-02-08 13:32:55Z kurund $
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* File for the CiviCRM APIv2 group functions
*
* @package CiviCRM_APIv2
* @subpackage API_Group
* @copyright CiviCRM LLC (c) 2004-2013
* @version $Id: Group.php 45502 2013-02-08 13:32:55Z kurund $
*/
/**
* Include utility functions
*/
require_once 'CRM/Contact/BAO/Group.php';
require_once 'api/v2/utils.php';
/**
* create/update group
*
* This API is used to create new group or update any of the existing
* In case of updating existing group, id of that particular grop must
* be in $params array. Either id or name is required field in the
* $params array
*
* @param array $params (referance) Associative array of property
* name/value pairs to insert in new 'group'
*
* @return array returns id of the group created if success,
* error message otherwise
*
* @access public
*/
function civicrm_group_add(&$params) {
_civicrm_initialize();
if (is_null($params) || !is_array($params) || empty($params)) {
return civicrm_create_error('Required parameter missing');
}
if (!CRM_Utils_Array::value('title', $params)) {
return civicrm_create_error('Required parameter title missing');
}
$group = CRM_Contact_BAO_Group::create($params);
if (is_null($group)) {
return civicrm_create_error('Group not created');
}
else {
return civicrm_create_success($group);
}
}
/*
* Wrapper for civicrm_group_add so function can take new (v3) name
*/
function civicrm_group_create(&$params) {
$result = civicrm_group_add($params);
return $result;
}
/**
* Returns array of groups matching a set of one or more group properties
*
* @param array $params (referance) Array of one or more valid
* property_name=>value pairs. If $params is set
* as null, all groups will be returned
*
* @return array (referance) Array of matching groups
* @access public
*/
function civicrm_group_get(&$params) {
_civicrm_initialize();
if (!is_null($params) && !is_array($params)) {
return civicrm_create_error('Params should be array');
}
$returnProperties = array();
foreach ($params as $n => $v) {
if (substr($n, 0, 7) == 'return.') {
$returnProperties[] = substr($n, 7);
}
}
if (!empty($returnProperties)) {
$returnProperties[] = 'id';
}
$groupObjects = CRM_Contact_BAO_Group::getGroups($params, $returnProperties);
if (count($groupObjects) == 0) {
return civicrm_create_error('No such group exists');
}
$groups = array();
foreach ($groupObjects as $group) {
_civicrm_object_to_array($group, $groups[$group->id]);
}
return $groups;
}
/**
* delete an existing group
*
* This method is used to delete any existing group. id of the group
* to be deleted is required field in $params array
*
* @param array $params (referance) array containing id of the group
* to be deleted
*
* @return array (referance) returns flag true if successfull, error
* message otherwise
*
* @access public
*/
function civicrm_group_delete(&$params) {
_civicrm_initialize();
if (is_null($params) || !is_array($params) || !CRM_Utils_Array::value('id', $params)) {
return civicrm_create_error('Required parameter missing');
}
CRM_Contact_BAO_Group::discard($params['id']);
return civicrm_create_success(TRUE);
}
<?php
// $Id: GroupContact.php 45502 2013-02-08 13:32:55Z kurund $
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* File for the CiviCRM APIv2 group contact functions
*
* @package CiviCRM_APIv2
* @subpackage API_Group
*
* @copyright CiviCRM LLC (c) 2004-2013
* @version $Id: GroupContact.php 45502 2013-02-08 13:32:55Z kurund $
*
*/
/**
* Include utility functions
*/
require_once 'api/v2/utils.php';
/**
* This API will give list of the groups for particular contact
* Particualr status can be sent in params array
* If no status mentioned in params, by default 'added' will be used
* to fetch the records
*
* @param array $params name value pair of contact information
*
* @return array list of groups, given contact subsribed to
*/
function civicrm_group_contact_get(&$params) {
if (!is_array($params)) {
return civicrm_create_error(ts('input parameter should be an array'));
}
if (!array_key_exists('contact_id', $params)) {
return civicrm_create_error(ts('contact_id is a required field'));
}
$status = CRM_Utils_Array::value('status', $params, 'Added');
require_once 'CRM/Contact/BAO/GroupContact.php';
$values = CRM_Contact_BAO_GroupContact::getContactGroup($params['contact_id'], $status, NULL, FALSE, TRUE);
return $values;
}
/**
*
* @param <type> $params
*
* @return <type>
*/
function civicrm_group_contact_add(&$params) {
return civicrm_group_contact_common($params, 'add');
}
/**
*
* @param <type> $params
*
* @return <type>
*/
function civicrm_group_contact_remove(&$params) {
return civicrm_group_contact_common($params, 'remove');
}
/**
*
* @param <type> $params
*
* @return <type>
*/
function civicrm_group_contact_pending(&$params) {
return civicrm_group_contact_common($params, 'pending');
}
/**
*
* @param <type> $params
* @param <type> $op
*
* @return <type>
*/
function civicrm_group_contact_common(&$params, $op = 'add') {
if (!is_array($params)) {
return civicrm_create_error(ts('input parameter should be an array'));
}
$contactIDs = array();
$groupIDs = array();
foreach ($params as $n => $v) {
if (substr($n, 0, 10) == 'contact_id') {
$contactIDs[] = $v;
}
elseif (substr($n, 0, 8) == 'group_id') {
$groupIDs[] = $v;
}
}
if (empty($contactIDs)) {
return civicrm_create_error(ts('contact_id is a required field'));
}
if (empty($groupIDs)) {
return civicrm_create_error(ts('group_id is a required field'));
}
$method = CRM_Utils_Array::value('method', $params, 'API');
if ($op == 'add') {
$status = CRM_Utils_Array::value('status', $params, 'Added');
}
elseif ($op == 'pending') {
$status = CRM_Utils_Array::value('status', $params, 'Pending');
}
else {
$status = CRM_Utils_Array::value('status', $params, 'Removed');
}
$tracking = CRM_Utils_Array::value('tracking', $params);
require_once 'CRM/Contact/BAO/GroupContact.php';
$values = array('is_error' => 0);
if ($op == 'add' || $op == 'pending') {
$values['total_count'] = $values['added'] = $values['not_added'] = 0;
foreach ($groupIDs as $groupID) {
list($tc, $a, $na) = CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIDs, $groupID,
$method, $status, $tracking
);
$values['total_count'] += $tc;
$values['added'] += $a;
$values['not_added'] += $na;
}
}
else {
$values['total_count'] = $values['removed'] = $values['not_removed'] = 0;
foreach ($groupIDs as $groupID) {
list($tc, $r, $nr) = CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contactIDs, $groupID,
$method, $status, $tracking
);
$values['total_count'] += $tc;
$values['removed'] += $r;
$values['not_removed'] += $nr;
}
}
return $values;
}
function civicrm_group_contact_update_status(&$params) {
if (!is_array($params)) {
return civicrm_create_error(ts('input parameter should be an array'));
}
if (empty($params['contact_id'])) {
return civicrm_create_error(ts('contact_id is a required field'));
}
else {
$contactID = $params['contact_id'];
}
if (empty($params['group_id'])) {
return civicrm_create_error(ts('group_id is a required field'));
}
else {
$groupID = $params['group_id'];
}
$method = CRM_Utils_Array::value('method', $params, 'API');
$tracking = CRM_Utils_Array::value('tracking', $params);
require_once 'CRM/Contact/BAO/GroupContact.php';
CRM_Contact_BAO_GroupContact::updateGroupMembershipStatus($contactID, $groupID, $method, $tracking);
return TRUE;
}
<?php
// $Id$
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* File for the CiviCRM APIv2 group nesting functions
*
* @package CiviCRM_APIv2
* @subpackage API_Group
*
* @copyright CiviCRM LLC (c) 2004-2013
* @version $Id: GroupNesting.php 21624 2009-08-07 22:02:55Z wmorgan $
*
*/
/**
* Include utility functions
*/
require_once 'api/v2/utils.php';
/**
* Provides group nesting record(s) given parent and/or child id.
*
* @param array $params an array containing at least child_group_id or parent_group_id
*
* @return array list of group nesting records
*/
function civicrm_group_nesting_get(&$params) {
_civicrm_initialize();
if (!is_array($params)) {
return civicrm_create_error('Params need to be of type array!');
}
if (!array_key_exists('child_group_id', $params) &&
!array_key_exists('parent_group_id', $params)
) {
return civicrm_create_error(ts('At least one of child_group_id or parent_group_id is a required field'));
}
require_once 'CRM/Contact/DAO/GroupNesting.php';
$dao = new CRM_Contact_DAO_GroupNesting();
if (array_key_exists('child_group_id', $params)) {
$dao->child_group_id = $params['child_group_id'];
}
if (array_key_exists('parent_group_id', $params)) {
$dao->parent_group_id = $params['parent_group_id'];
}
$values = array();
if ($dao->find()) {
while ($dao->fetch()) {
$temp = array();
_civicrm_object_to_array($dao, $temp);
$values[$dao->id] = $temp;
}
$values['is_error'] = 0;
}
else {
return civicrm_create_error('No records found.');
}
return $values;
}
/**
* Creates group nesting record for given parent and child id.
* Parent and child groups need to exist.
*
* @param array &$params parameters array - allowed array keys include:
* {@schema Contact/GroupNesting.xml}
*
* @return array TBD
*
* @todo Work out the return value.
*/
function civicrm_group_nesting_create(&$params) {
if (!is_array($params)) {
return civicrm_create_error('Params need to be of type array!');
}
require_once 'CRM/Contact/BAO/GroupNesting.php';
if (!array_key_exists('child_group_id', $params) &&
!array_key_exists('parent_group_id', $params)
) {
return civicrm_create_error(ts('You need to define parent_group_id and child_group_id in params.'));
}
CRM_Contact_BAO_GroupNesting::add($params['parent_group_id'], $params['child_group_id']);
// FIXME: CRM_Contact_BAO_GroupNesting requires some work
$result = array('is_error' => 0);
return $result;
}
/**
* Removes specific nesting records.
*
* @param array &$params parameters array - allowed array keys include:
* {@schema Contact/GroupNesting.xml}
*
* @return array TBD
*
* @todo Work out the return value.
*/
function civicrm_group_nesting_remove(&$params) {
if (!is_array($params)) {
return civicrm_create_error('Params need to be of type array!');
}
if (!array_key_exists('child_group_id', $params) ||
!array_key_exists('parent_group_id', $params)
) {
return civicrm_create_error(ts('You need to define parent_group_id and child_group_id in params.'));
}
require_once 'CRM/Contact/DAO/GroupNesting.php';
$dao = new CRM_Contact_DAO_GroupNesting();
$dao->copyValues($params);
if ($dao->delete()) {
$result = array('is_error' => 0);
}
return $result;
}
<?php
// $Id$
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* File for the CiviCRM APIv2 group contact functions
*
* @package CiviCRM_APIv2
* @subpackage API_Group
*
* @copyright CiviCRM LLC (c) 2004-2013
* @version $Id: GroupContact.php 21624 2009-06-04 22:02:55Z mover $
*
*/
/**
* Include utility functions
*/
require_once 'api/v2/utils.php';
/**
* This API will give list of the groups for particular contact
* Particualr status can be sent in params array
* If no status mentioned in params, by default 'added' will be used
* to fetch the records
*
* @param array $params name value pair of contact information
*
* @return array list of groups, given contact subsribed to
*/
function civicrm_group_organization_get(&$params) {
_civicrm_initialize();
if (!is_array($params)) {
return civicrm_create_error(ts('Input parameter is not an array'));
}
if (empty($params)) {
return civicrm_create_error('No input parameter present');
}
if (!array_key_exists('organization_id', $params) &&
!array_key_exists('group_id', $params)
) {
return civicrm_create_error(ts('at least one of organization_id or group_id is a required field'));
}
require_once 'CRM/Contact/DAO/GroupOrganization.php';
$dao = new CRM_Contact_DAO_GroupOrganization();
if (array_key_exists('organization_id', $params)) {
$dao->organization_id = $params['organization_id'];
}
if (array_key_exists('group_id', $params)) {
$dao->group_id = $params['group_id'];
}
$dao->find();
$values = array();
_civicrm_object_to_array($dao, $values);
return civicrm_create_success($values);
}
/**
*
* @param <type> $params
*
* @return <type>
*/
function civicrm_group_organization_create(&$params) {
if (!is_array($params)) {
return civicrm_create_error(ts('Input parameter is not an array'));
}
if (empty($params)) {
return civicrm_create_error('No input parameter present');
}
if (!array_key_exists('organization_id', $params) ||
!array_key_exists('group_id', $params)
) {
return civicrm_create_error(ts('organization_id and group_id are required field'));
}
require_once 'CRM/Contact/BAO/GroupOrganization.php';
$groupOrgBAO = CRM_Contact_BAO_GroupOrganization::add($params);
if (is_a($groupOrgBAO, 'CRM_Core_Error') || is_null($groupOrgBAO)) {
return civicrm_create_error("Group Organization can not be created");
}
_civicrm_object_to_array($groupOrgBAO, $values);
return civicrm_create_success($values);
}
/**
* Deletes an existing Group Organization
*
* This API is used for deleting a Group Organization
*
* @param Array $params ID of the Group Organization to be deleted
*
* @return null if successfull, array with is_error = 1 otherwise
* @access public
*/
function civicrm_group_organization_remove(&$params) {
_civicrm_initialize();
if (!is_array($params)) {
$error = civicrm_create_error('Input parameter is not an array');
return $error;
}
if (empty($params)) {
return civicrm_create_error('No input parameter present');
}
if (!CRM_Utils_Array::value('id', $params)) {
$error = civicrm_create_error('Invalid or no value for Group Organization ID');
return $error;
}
require_once 'CRM/Contact/BAO/GroupOrganization.php';
$result = CRM_Contact_BAO_GroupOrganization::delete($params['id']);
return $result ? civicrm_create_success(ts('Deleted Group Organization successfully')) : civicrm_create_error(ts('Could not delete Group Organization'));
}
<?php
// $Id: Location.php 45502 2013-02-08 13:32:55Z kurund $
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* File for the CiviCRM APIv2 location functions
*
* @package CiviCRM_APIv2
* @subpackage API_Location
*
* @copyright CiviCRM LLC (c) 2004-2013
* @version $Id: Location.php 45502 2013-02-08 13:32:55Z kurund $
*/
/**
* Include utility functions
*/
require_once 'api/v2/utils.php';
/**
* Create an additional location for an existing contact
*
* @param array $params input properties
*
* @return array the created location's params
*
* @access public
*/
function civicrm_location_add(&$params) {
_civicrm_initialize();
$error = _civicrm_location_check_params($params);
if (civicrm_error($error)) {
return $error;
}
$locationTypeId = CRM_Utils_Array::value('location_type_id', $params);
if (!$locationTypeId &&
'2.0' == CRM_Utils_Array::value('location_format', $params)
) {
require_once 'CRM/Core/DAO/LocationType.php';
$locationTypeDAO = new CRM_Core_DAO_LocationType();
$locationTypeDAO->name = $params['location_type'];
$locationTypeDAO->find(TRUE);
$locationTypeId = $locationTypeDAO->id;
CRM_Core_PseudoConstant::flush('locationType');
if (!isset($locationTypeId)) {
return civicrm_create_error(ts('$location_type is not valid one'));
}
}
$location = &_civicrm_location_add($params, $locationTypeId);
return $location;
}
/*
* Correctly named wrapper for 'add' function
*/
function civicrm_location_create($params) {
$result = civicrm_location_add($params);
return $result;
}
/**
* Update a specified location with the provided property values.
*
* @param object $contact A valid Contact object (passed by reference).
* @param string $location_id Valid (db-level) id for location to be updated.
* @param Array $params Associative array of property name/value pairs to be updated
*
* @return Location object with updated property values
*
* @access public
*
*/
function civicrm_location_update($params) {
_civicrm_initialize();
if (!is_array($params)) {
return civicrm_create_error('Params need to be of type array!');
}
if (!isset($params['contact_id'])) {
return civicrm_create_error(ts('$contact is not valid contact datatype'));
}
$unsetVersion = FALSE;
$locationTypes = array();
$hasLocBlockId = FALSE;
$allLocationTypes = CRM_Core_PseudoConstant::locationType(TRUE);
if ('2.0' == CRM_Utils_Array::value('location_format', $params)) {
//force to use 3.0 location_format for get location api's.
$params['location_format'] = '3.0';
$unsetVersion = TRUE;
if (!($locationTypeId = CRM_Utils_Array::value('location_type_id', $params)) &&
!(CRM_Utils_Rule::integer($locationTypeId))
) {
return civicrm_create_error(ts('missing or invalid location_type_id'));
}
$locationTypes = CRM_Utils_Array::value('location_type', $params);
//if location_type array absent and location_type_id pass build array.
if ((!is_array($locationTypes) || !count($locationTypes)) && $locationTypeId) {
require_once 'CRM/Core/PseudoConstant.php';
if ($locName = CRM_Utils_Array::value($locationTypeId, $allLocationTypes)) {
$locationTypes = array($locName);
}
}
}
else {
$locTypeIds = array();
foreach (array(
'email', 'phone', 'im', 'address', 'openid') as $name) {
if (isset($params[$name]) && is_array($params[$name])) {
foreach ($params[$name] as $count => & $values) {
$locName = CRM_Utils_Array::value('location_type', $values);
$LocTypeId = CRM_Utils_Array::value('location_type_id', $values);
if ($locName && !in_array($locName, $locationTypes)) {
$locationTypes[] = $locName;
}
if ($LocTypeId) {
$locTypeIds[$LocTypeId] = $LocTypeId;
}
elseif (in_array($locName, $allLocationTypes)) {
$values['location_type_id'] = array_search($locName, $allLocationTypes);
}
if (!$hasLocBlockId && CRM_Utils_Array::value('id', $values)) {
$hasLocBlockId = TRUE;
}
}
}
}
//get all location types.
foreach ($locTypeIds as $locId) {
$name = CRM_Utils_Array::value($locId, $allLocationTypes);
if (!$name) {
return civicrm_create_error(ts('Invalid Location Type Id : %1', array(1 => $locId)));
}
if (!in_array($name, $locationTypes)) {
$locationTypes[] = $name;
}
}
}
$invalidTypes = array();
foreach ($locationTypes as $name) {
if (!in_array($name, $allLocationTypes)) {
$invalidTypes[$name] = $name;
}
}
if (!empty($invalidTypes)) {
return civicrm_create_error(ts("Invalid Location Type(s) : %1", array(1 => implode(', ', $invalidTypes))));
}
//allow to swap locations.
if ($hasLocBlockId) {
$locationTypes = $allLocationTypes;
}
if (!empty($locationTypes)) {
$params['location_type'] = $locationTypes;
}
else {
return civicrm_create_error(ts('missing or invalid location_type_id'));
}
//get location filter by loc type.
$locations = &civicrm_location_get($params);
if ($unsetVersion) {
unset($params['location_format']);
}
if (CRM_Utils_System::isNull($locations)) {
return civicrm_create_error(ts("Invalid Location Type(s) : %1",
array(1 => implode(', ', CRM_Utils_Array::value('location_type', $params)))
));
}
$location = &_civicrm_location_update($params, $locations);
return $location;
}
/**
* Deletes a contact location.
*
* @param object $contact A valid Contact object (passed by reference).
* @param string $location_id A valid location ID.
*
* @return null, if successful. CRM error object, if 'contact' or 'location_id' is invalid, permissions are insufficient, etc.
*
* @access public
*
*/
function civicrm_location_delete(&$contact) {
_civicrm_initialize();
if (!is_array($contact)) {
return civicrm_create_error('Params need to be of type array!');
}
if (!isset($contact['contact_id'])) {
return civicrm_create_error(ts('$contact is not valid contact datatype'));
}
require_once 'CRM/Utils/Rule.php';
$locationTypeID = CRM_Utils_Array::value('location_type', $contact);
if (!$locationTypeID ||
!CRM_Utils_Rule::integer($locationTypeID)
) {
return civicrm_create_error(ts('missing or invalid location'));
}
$result = &_civicrm_location_delete($contact);
return $result;
}
/**
* Returns array of location(s) for a contact
*
* @param array $contact a valid array of contact parameters
*
* @return array an array of location parameters arrays
*
* @access public
*/
function civicrm_location_get($contact) {
_civicrm_initialize();
if (!is_array($contact)) {
return civicrm_create_error('Params need to be of type array!');
}
if (!isset($contact['contact_id'])) {
return civicrm_create_error('$contact is not valid contact datatype');
}
$locationTypes = CRM_Utils_Array::value('location_type', $contact);
if (is_array($locationTypes) && !count($locationTypes)) {
return civicrm_create_error('Location type array can not be empty');
}
$location = &_civicrm_location_get($contact, $locationTypes);
return $location;
}
/**
*
* @param <type> $params
* @param <type> $locationTypeId
*
* @return <type>
*/
function _civicrm_location_add(&$params, $locationTypeId = NULL) {
// convert api params to 3.0 format.
if ('2.0' == CRM_Utils_Array::value('location_format', $params)) {
_civicrm_format_params_v2_to_v3($params, $locationTypeId);
}
// Get all existing location blocks.
$blockParams = array(
'contact_id' => $params['contact_id'],
'entity_id' => $params['contact_id'],
);
require_once 'CRM/Core/BAO/Location.php';
$allBlocks = CRM_Core_BAO_Location::getValues($blockParams);
// get all blocks in contact array.
$contact = array_merge(array('contact_id' => $params['contact_id']), $allBlocks);
// copy params value in contact array.
$primary = $billing = array();
foreach (array(
'email', 'phone', 'im', 'openid') as $name) {
if (CRM_Utils_Array::value($name, $params)) {
if (!isset($contact[$name]) ||
!is_array($contact[$name])
) {
$contact[$name] = array();
}
$blockCount = count($contact[$name]);
if (is_array($params[$name])) {
foreach ($params[$name] as $val) {
$contact[$name][++$blockCount] = $val;
// check for primary and billing.
if (CRM_Utils_Array::value('is_primary', $val)) {
$primary[$name][$blockCount] = TRUE;
}
if (CRM_Utils_Array::value('is_billing', $val)) {
$primary[$name][$blockCount] = TRUE;
}
}
}
}
}
// get loc type id from params.
if (!$locationTypeId) {
$locationTypeId = CRM_Utils_Array::value('location_type_id', $params['address'][1]);
}
// address having 1-1 ( loc type - address ) mapping.
$addressCount = 1;
if (array_key_exists('address', $contact) && is_array($contact['address'])) {
foreach ($contact['address'] as $addCount => $values) {
if ($locationTypeId == CRM_Utils_Array::value('location_type_id', $values)) {
$addressCount = $addCount;
break;
}
$addressCount++;
}
}
if (CRM_Utils_Array::value('1', $params['address']) && !empty($params['address'][1])) {
$contact['address'][$addressCount] = $params['address'][1];
// check for primary and billing address.
if (CRM_Utils_Array::value('is_primary', $params['address'][1])) {
$primary['address'][$addressCount] = TRUE;
}
if (CRM_Utils_Array::value('is_billing', $params['address'][1])) {
$billing['address'][$addressCount] = TRUE;
}
// format state and country.
foreach (array(
'state_province', 'country') as $field) {
$fName = ($field == 'state_province') ? 'stateProvinceAbbreviation' : 'countryIsoCode';
if (CRM_Utils_Array::value($field, $contact['address'][$addressCount]) &&
is_numeric($contact['address'][$addressCount][$field])
) {
$fValue = &$contact['address'][$addressCount][$field];
eval('$fValue = CRM_Core_PseudoConstant::' . $fName . '( $fValue );');
//kill the reference.
unset($fValue);
}
}
}
//handle primary and billing reset.
foreach (array(
'email', 'phone', 'im', 'address', 'openid') as $name) {
if (!array_key_exists($name, $contact) || CRM_Utils_System::isNull($contact[$name])) {
continue;
}
$errorMsg = NULL;
$primaryBlockIndex = $billingBlockIndex = 0;
if (array_key_exists($name, $primary)) {
if (count($primary[$name]) > 1) {
$errorMsg .= ts("Multiple primary %1.", array(1 => $name));
}
else {
$primaryBlockIndex = key($primary[$name]);
}
}
if (array_key_exists($name, $billing)) {
if (count($billing[$name]) > 1) {
$errorMsg .= ts("Multiple billing %1.", array(1 => $name));
}
else {
$billingBlockIndex = key($billing[$name]);
}
}
if ($errorMsg) {
return civicrm_create_error($errorMsg);
}
foreach ($contact[$name] as $count => & $values) {
if ($primaryBlockIndex && ($count != $primaryBlockIndex)) {
$values['is_primary'] = FALSE;
}
if ($billingBlockIndex && ($count != $billingBlockIndex)) {
$values['is_billing'] = FALSE;
}
//kill the reference.
unset($values);
}
}
// get all ids if not present.
require_once 'CRM/Contact/BAO/Contact.php';
CRM_Contact_BAO_Contact::resolveDefaults($contact, TRUE);
require_once 'CRM/Core/BAO/Location.php';
$result = CRM_Core_BAO_Location::create($contact);
if (empty($result)) {
return civicrm_create_error(ts("Location not created"));
}
$blocks = array('address', 'phone', 'email', 'im', 'openid');
foreach ($blocks as $block) {
for ($i = 0; $i < count($result[$block]); $i++) {
$locArray[$block][$i] = $result[$block][$i]->id;
}
}
// CRM-4800
if (2.0 == CRM_Utils_Array::value('location_format', $params)) {
$locArray['location_type_id'] = $locationTypeId;
}
return civicrm_create_success($locArray);
}
/**
*
* @param <type> $params
* @param <type> $locationArray
*
* @return <type>
*/
function _civicrm_location_update($params, $locations) {
// convert api params to 3.0 format.
if ('2.0' == CRM_Utils_Array::value('location_format', $params)) {
_civicrm_format_params_v2_to_v3($params);
}
$contact = array('contact_id' => $params['contact_id']);
$primary = $billing = array();
// copy params value in contact array.
foreach (array(
'email', 'phone', 'im', 'openid') as $name) {
if (CRM_Utils_Array::value($name, $params) && is_array($params[$name])) {
$blockCount = 0;
$contact[$name] = array();
foreach ($params[$name] as $val) {
$contact[$name][++$blockCount] = $val;
// check for primary and billing.
if (CRM_Utils_Array::value('is_primary', $val)) {
$primary[$name][$blockCount] = TRUE;
}
if (CRM_Utils_Array::value('is_billing', $val)) {
$primary[$name][$blockCount] = TRUE;
}
}
}
else {
// get values from db blocks so we dont lose them.
if (!CRM_Utils_Array::value($name, $locations) || !is_array($locations[$name])) {
continue;
}
$contact[$name] = $locations[$name];
}
}
$addressCount = 1;
if (CRM_Utils_Array::value(1, $params['address']) && !empty($params['address'][1])) {
$contact['address'][$addressCount] = $params['address'][1];
// check for primary and billing address.
if (CRM_Utils_Array::value('is_primary', $params['address'][1])) {
$primary['address'][$addressCount] = TRUE;
}
if (CRM_Utils_Array::value('is_billing', $params['address'][1])) {
$billing['address'][$addressCount] = TRUE;
}
// format state and country.
foreach (array(
'state_province', 'country') as $field) {
$fName = ($field == 'state_province') ? 'stateProvinceAbbreviation' : 'countryIsoCode';
if (CRM_Utils_Array::value($field, $contact['address'][$addressCount]) &&
is_numeric($contact['address'][$addressCount][$field])
) {
$fValue = &$contact['address'][$addressCount][$field];
eval('$fValue = CRM_Core_PseudoConstant::' . $fName . '( $fValue );');
//kill the reference.
unset($fValue);
}
}
}
//handle primary and billing reset.
foreach (array(
'email', 'phone', 'im', 'address', 'openid') as $name) {
if (!array_key_exists($name, $contact) || CRM_Utils_System::isNull($contact[$name])) {
continue;
}
$errorMsg = NULL;
$primaryBlockIndex = $billingBlockIndex = 0;
if (array_key_exists($name, $primary)) {
if (count($primary[$name]) > 1) {
$errorMsg .= ts("<br />Multiple Primary %1.", array(1 => $name));
}
else {
$primaryBlockIndex = key($primary[$name]);
}
}
if (array_key_exists($name, $billing)) {
if (count($billing[$name]) > 1) {
$errorMsg .= ts("<br />Multiple Billing %1.", array(1 => $name));
}
else {
$billingBlockIndex = key($billing[$name]);
}
}
if ($errorMsg) {
return civicrm_create_error($errorMsg);
}
foreach ($contact[$name] as $count => & $values) {
if ($primaryBlockIndex && ($count != $primaryBlockIndex)) {
$values['is_primary'] = FALSE;
}
if ($billingBlockIndex && ($count != $billingBlockIndex)) {
$values['is_billing'] = FALSE;
}
// kill the reference.
unset($values);
}
}
// get all ids if not present.
require_once 'CRM/Contact/BAO/Contact.php';
CRM_Contact_BAO_Contact::resolveDefaults($contact, TRUE);
$location = CRM_Core_BAO_Location::create($contact);
if (empty($location)) {
return civicrm_create_error(ts("Location not created"));
}
$locArray = array();
$blocks = array('address', 'phone', 'email', 'im', 'openid');
$locationTypeId = NULL;
foreach ($blocks as $block) {
for ($i = 0; $i < count($location[$block]); $i++) {
$locArray[$block][$i] = $location[$block][$i]->id;
$locationTypeId = $location[$block][$i]->location_type_id;
}
}
// CRM-4800
if (2.0 == CRM_Utils_Array::value('location_format', $params)) {
$locArray['location_type_id'] = $locationTypeId;
}
return civicrm_create_success($locArray);
}
/**
*
* @param <type> $contact
*
* @return <type>
*/
function _civicrm_location_delete(&$contact) {
require_once 'CRM/Core/DAO/LocationType.php';
$locationTypeDAO = new CRM_Core_DAO_LocationType();
$locationTypeDAO->id = $contact['location_type'];
if (!$locationTypeDAO->find()) {
return civicrm_create_error(ts('invalid location type'));
}
require_once 'CRM/Core/BAO/Location.php';
CRM_Core_BAO_Location::deleteLocationBlocks($contact['contact_id'], $contact['location_type']);
return NULL;
}
/**
*
* @param <type> $contact
* @param <type> $locationTypes = array(
'Home', 'Work' ) else empty.
*
* @return <type>
*/
function &_civicrm_location_get($contact, $locationTypes = array(
)) {
$params = array(
'contact_id' => $contact['contact_id'],
'entity_id' => $contact['contact_id'],
);
require_once 'CRM/Core/BAO/Location.php';
$locations = CRM_Core_BAO_Location::getValues($params);
$locValues = array();
// filter the blocks return only those from given loc type.
if (is_array($locationTypes) && !empty($locationTypes)) {
foreach ($locationTypes as $locName) {
if (!$locName) {
continue;
}
if ($locTypeId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_LocationType', $locName, 'id', 'name')) {
foreach (array(
'email', 'im', 'phone', 'address', 'openid') as $name) {
if (!array_key_exists($name, $locations) || !is_array($locations[$name])) {
continue;
}
$blkCount = 0;
if (array_key_exists($name, $locValues)) {
$blkCount = count($locValues[$name]);
}
foreach ($locations[$name] as $count => $values) {
if ($locTypeId == $values['location_type_id']) {
$locValues[$name][++$blkCount] = $values;
}
}
}
}
}
}
else {
$locValues = $locations;
}
// CRM-4800
if ('2.0' == CRM_Utils_Array::value('location_format', $contact)) {
_civicrm_location_get_v3_to_v2($locValues);
}
return $locValues;
}
/**
* This function ensures that we have the right input location parameters
*
* We also need to make sure we run all the form rules on the params list
* to ensure that the params are valid
*
* @param array $params Associative array of property name/value
* pairs to insert in new location.
*
* @return bool|CRM_Utils_Error
* @access public
*/
function _civicrm_location_check_params(&$params) {
if (!is_array($params)) {
return civicrm_create_error('Params need to be of type array!');
}
// cannot create a location with empty params
if (empty($params)) {
return civicrm_create_error('Input Parameters empty');
}
$errorField = NULL;
if (!CRM_Utils_Array::value('contact_id', $params)) {
$errorField = 'contact_id';
}
//lets have user option to send location type id or location type.
if (!$errorField &&
'2.0' == CRM_Utils_Array::value('location_format', $params) &&
!CRM_Utils_Array::value('location_type_id', $params) &&
!CRM_Utils_Array::value('location_type', $params)
) {
$errorField = 'location_type';
}
if (!$errorField) {
$blocks = array('address', 'email', 'phone', 'im', 'website');
$emptyAddressBlock = TRUE;
foreach ($blocks as $block) {
if (isset($params[$block]) && !empty($params[$block])) {
$emptyAddressBlock = FALSE;
break;
}
}
if ($emptyAddressBlock) {
return civicrm_create_error('Please set atleast one location block. ( address or email or phone or im or website)');
}
}
if ($errorField) {
return civicrm_create_error("Required fields not found for location $errorField");
}
return array();
}
/**
* This function provide interface between v3.0 => v2.2 location blocks.
*/
function _civicrm_location_get_v3_to_v2(&$locations) {
$locValues = $blockCounts = array();
$primaryLoc = $billingLoc = FALSE;
foreach ($locations as $blockName => $blockValues) {
if (!is_array($blockValues) || empty($blockValues)) {
continue;
}
foreach ($blockValues as $count => $values) {
$locTypeId = $values['location_type_id'];
if (!array_key_exists($locTypeId, $locValues)) {
$locValues[$locTypeId] = array('location_type_id' => $locTypeId);
}
if (!array_key_exists($blockName, $locValues[$locTypeId])) {
$locValues[$locTypeId][$blockName] = array();
}
if ($blockName == 'address') {
$locValues[$locTypeId][$blockName] = $values;
}
else {
if (!array_key_exists($blockName, $blockCounts) ||
!array_key_exists($locTypeId, $blockCounts[$blockName])
) {
$blockCounts[$blockName][$locTypeId] = 1;
}
$blkCount = &$blockCounts[$blockName][$locTypeId];
$locValues[$locTypeId][$blockName][$blkCount++] = $values;
}
if (!$primaryLoc && CRM_Utils_Array::value('is_primary', $values)) {
$primaryLoc = TRUE;
$locValues[$locTypeId]['is_primary'] = TRUE;
}
if (!$billingLoc && CRM_Utils_Array::value('is_billing', $values)) {
$billingLoc = TRUE;
$locValues[$locTypeId]['is_billing'] = TRUE;
}
}
}
foreach (array(
'email', 'phone', 'im', 'address', 'openid') as $field) {
if (array_key_exists($field, $locations))unset($locations[$field]);
}
$locations = $locValues;
return $locValues;
}
/**
* function convert params to v3.0 format before add location.
*/
function _civicrm_format_params_v2_to_v3(&$params, $locationTypeId = NULL) {
// get the loc type id.
if (!$locationTypeId) {
// get location type.
$locationTypeId = CRM_Utils_Array::value('location_type_id', $params);
if (!$locationTypeId && array_key_exists('location_type', $params)) {
require_once 'CRM/Core/PseudoConstant.php';
$locTypes = CRM_Core_PseudoConstant::locationType();
$locType = $params['location_type'];
if (is_array($params['location_type'])) {
$locType = array_pop($params['location_type']);
}
$locationTypeId = CRM_Utils_Array::key($locType, $locTypes);
}
}
// convert params into v3.0 format.
$primary = $billing = array();
$blocks = array('Email', 'Phone', 'IM', 'OpenID');
// format params array.
$firstBlockCount = NULL;
foreach ($blocks as $block) {
require_once (str_replace('_', DIRECTORY_SEPARATOR, "CRM_Core_DAO_" . $block) . ".php");
eval('$fields =& CRM_Core_DAO_' . $block . '::fields( );');
$name = strtolower($block);
$blockCount = 0;
if (CRM_Utils_Array::value($name, $params)) {
if (is_array($params[$name])) {
$values = $params[$name];
$params[$name] = array();
foreach ($values as $val) {
_civicrm_store_values($fields, $val, $params[$name][++$blockCount]);
// check for primary and billing.
if (CRM_Utils_Array::value('is_primary', $val)) {
$primary[$name][$blockCount] = TRUE;
}
if (CRM_Utils_Array::value('is_billing', $val)) {
$primary[$name][$blockCount] = TRUE;
}
if (!$firstBlockCount) {
$firstBlockCount = $blockCount;
}
}
}
else {
//need to get ids.
if (in_array($name, array(
'im', 'phone'))) {
require_once 'CRM/Core/PseudoConstant.php';
if ($name == 'im') {
CRM_Utils_Array::lookupValue($params,
'provider',
CRM_Core_PseudoConstant::IMProvider(), TRUE
);
}
else {
CRM_Utils_Array::lookupValue($params,
'phone_type',
CRM_Core_PseudoConstant::phoneType(), TRUE
);
}
}
$locValues[$name] = array();
_civicrm_store_values($fields, $params, $locValues[$name][++$blockCount]);
$params[$name] = $locValues[$name];
$firstBlockCount = $blockCount;
unset($locValues[$name]);
}
// make first block as default primary when is_primary
// is not set in sub array and set in main params array.
if (!CRM_Utils_Array::value($name, $primary) && CRM_Utils_Array::value('is_primary', $params)) {
$primary[$name][$firstBlockCount] = TRUE;
$params[$name][$firstBlockCount]['is_primary'] = TRUE;
}
if (!CRM_Utils_Array::value($name, $billing) && CRM_Utils_Array::value('is_billing', $params)) {
$billing[$name][$firstBlockCount] = TRUE;
$params[$name][$firstBlockCount]['is_billing'] = TRUE;
}
}
}
//get the address fields.
$addressCount = 1;
$ids = array(
'county', 'country_id', 'country',
'state_province_id', 'state_province',
'supplemental_address_1', 'supplemental_address_2',
'StateProvince.name', 'city', 'street_address',
);
$addressTaken = FALSE;
foreach ($ids as $id) {
if (array_key_exists($id, $params)) {
if (!$addressTaken) {
require_once 'CRM/Core/DAO/Address.php';
$fields = CRM_Core_DAO_Address::fields();
_civicrm_store_values($fields, $params, $params['address'][$addressCount]);
$addressTaken = TRUE;
}
$params['address'][$addressCount][$id] = $params[$id];
unset($params[$id]);
}
}
// format state and country.
foreach (array(
'state_province', 'country') as $field) {
$fName = ($field == 'state_province') ? 'stateProvinceAbbreviation' : 'countryIsoCode';
if (CRM_Utils_Array::value('address', $params) &&
CRM_Utils_Array::value($field, $params['address'][$addressCount]) &&
is_numeric($params['address'][$addressCount][$field])
) {
$fValue = &$params['address'][$addressCount][$field];
eval('$fValue = CRM_Core_PseudoConstant::' . $fName . '( $fValue );');
//kill the reference.
unset($fValue);
}
}
// check for primary address.
if (CRM_Utils_Array::value('is_primary', $params)) {
if ($addressTaken) {
$primary['address'][$addressCount] = TRUE;
$params['address'][$addressCount]['is_primary'] = TRUE;
}
unset($params['is_primary']);
}
if (CRM_Utils_Array::value('is_billing', $params)) {
if ($addressTaken) {
$billing['address'][$addressCount] = TRUE;
$params['address'][$addressCount]['is_billing'] = TRUE;
}
unset($params['is_billing']);
}
// handle primary and billing reset.
foreach (array(
'email', 'phone', 'im', 'address', 'openid') as $name) {
if (!array_key_exists($name, $params) || CRM_Utils_System::isNull($params[$name])) {
continue;
}
$errorMsg = NULL;
$primaryBlockIndex = $billingBlockIndex = 0;
if (array_key_exists($name, $primary)) {
if (count($primary[$name]) > 1) {
$errorMsg .= ts("<br />Multiple Primary %1.", array(1 => $block));
}
else {
$primaryBlockIndex = key($primary[$name]);
}
}
if (array_key_exists($name, $billing)) {
if (count($billing[$name]) > 1) {
$errorMsg .= ts("<br />Multiple Billing %1.", array(1 => $block));
}
else {
$billingBlockIndex = key($billing[$name]);
}
}
if ($errorMsg) {
return civicrm_create_error($errorMsg);
}
foreach ($params[$name] as $count => & $values) {
if ($primaryBlockIndex && ($count != $primaryBlockIndex)) {
$values['is_primary'] = FALSE;
}
if ($billingBlockIndex && ($count != $billingBlockIndex)) {
$values['is_billing'] = FALSE;
}
// get location type if not present in sub array.
if (!CRM_Utils_Array::value('location_type_id', $values)) {
$values['location_type_id'] = $locationTypeId;
}
//kill the reference.
unset($values);
}
}
// finally unset location_type and location type id.
foreach (array(
'location_type', 'location_type_id') as $f) {
if (isset($params[$f]))unset($params[$f]);
}
return $params;
}
<?php
// $Id$
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
*
* APIv2 functions for registering/processing mailer events.
*
* @package CiviCRM_APIv2
* @subpackage API_Mailer
* @copyright CiviCRM LLC (c) 2004-2013
* $Id$
*
*/
/**
* Files required for this package
*/
require_once 'api/v2/utils.php';
require_once 'CRM/Contact/BAO/Group.php';
require_once 'CRM/Mailing/BAO/BouncePattern.php';
require_once 'CRM/Mailing/Event/BAO/Bounce.php';
require_once 'CRM/Mailing/Event/BAO/Confirm.php';
require_once 'CRM/Mailing/Event/BAO/Opened.php';
require_once 'CRM/Mailing/Event/BAO/Queue.php';
require_once 'CRM/Mailing/Event/BAO/Reply.php';
require_once 'CRM/Mailing/Event/BAO/Subscribe.php';
require_once 'CRM/Mailing/Event/BAO/Unsubscribe.php';
require_once 'CRM/Mailing/Event/BAO/Resubscribe.php';
require_once 'CRM/Mailing/Event/BAO/Forward.php';
require_once 'CRM/Mailing/Event/BAO/TrackableURLOpen.php';
/**
* Process a bounce event by passing through to the BAOs.
*
* @param array $params
*
* @return array
*/
function civicrm_mailer_event_bounce($params) {
$errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash', 'body'));
if (!empty($errors)) {
return $errors;
}
$body = $params['body'];
unset($params['body']);
$params += CRM_Mailing_BAO_BouncePattern::match($body);
if (CRM_Mailing_Event_BAO_Bounce::create($params)) {
return civicrm_create_success();
}
return civicrm_create_error(ts('Queue event could not be found'));
}
/**
* Handle an unsubscribe event
*
* @param array $params
*
* @return array
*/
function civicrm_mailer_event_unsubscribe($params) {
$errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash'));
if (!empty($errors)) {
return $errors;
}
$job = $params['job_id'];
$queue = $params['event_queue_id'];
$hash = $params['hash'];
$groups = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_mailing($job, $queue, $hash);
if (count($groups)) {
CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue, $groups, FALSE, $job);
return civicrm_create_success();
}
return civicrm_create_error(ts('Queue event could not be found'));
}
/**
* Handle a site-level unsubscribe event
*
* @param array $params
*
* @return array
*/
function civicrm_mailer_event_domain_unsubscribe($params) {
$errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash'));
if (!empty($errors)) {
return $errors;
}
$job = $params['job_id'];
$queue = $params['event_queue_id'];
$hash = $params['hash'];
$unsubs = CRM_Mailing_Event_BAO_Unsubscribe::unsub_from_domain($job, $queue, $hash);
if (!$unsubs) {
return civicrm_create_error(ts('Queue event could not be found'));
}
CRM_Mailing_Event_BAO_Unsubscribe::send_unsub_response($queue, NULL, TRUE, $job);
return civicrm_create_success();
}
/**
* Handle a resubscription event
*
* @param array $params
*
* @return array
*/
function civicrm_mailer_event_resubscribe($params) {
$errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash'));
if (!empty($errors)) {
return $errors;
}
$job = $params['job_id'];
$queue = $params['event_queue_id'];
$hash = $params['hash'];
$groups = CRM_Mailing_Event_BAO_Resubscribe::resub_to_mailing($job, $queue, $hash);
if (count($groups)) {
CRM_Mailing_Event_BAO_Resubscribe::send_resub_response($queue, $groups, FALSE, $job);
return civicrm_create_success();
}
return civicrm_create_error(ts('Queue event could not be found'));
}
/**
* Handle a subscription event
*
* @param array $params
*
* @return array
*/
function civicrm_mailer_event_subscribe($params) {
$errors = _civicrm_mailer_check_params($params, array('email', 'group_id'));
if (!empty($errors)) {
return $errors;
}
$email = $params['email'];
$group_id = $params['group_id'];
$contact_id = CRM_Utils_Array::value('contact_id', $params);
$group = new CRM_Contact_DAO_Group();
$group->is_active = 1;
$group->id = (int)$group_id;
if (!$group->find(TRUE)) {
return civicrm_create_error(ts('Invalid Group id'));
}
$subscribe = CRM_Mailing_Event_BAO_Subscribe::subscribe($group_id, $email, $contact_id);
if ($subscribe !== NULL) {
/* Ask the contact for confirmation */
$subscribe->send_confirm_request($email);
$values = array();
$values['contact_id'] = $subscribe->contact_id;
$values['subscribe_id'] = $subscribe->id;
$values['hash'] = $subscribe->hash;
$values['is_error'] = 0;
return $values;
}
return civicrm_create_error(ts('Subscription failed'));
}
/**
* Handle a confirm event
*
* @param array $params
*
* @return array
*/
function civicrm_mailer_event_confirm($params) {
$errors = _civicrm_mailer_check_params($params, array('contact_id', 'subscribe_id', 'hash'));
if (!empty($errors)) {
return $errors;
}
$contact_id = $params['contact_id'];
$subscribe_id = $params['subscribe_id'];
$hash = $params['hash'];
$confirm = CRM_Mailing_Event_BAO_Confirm::confirm($contact_id, $subscribe_id, $hash) !== FALSE;
if (!$confirm) {
return civicrm_create_error(ts('Confirmation failed'));
}
return civicrm_create_success();
}
/**
* Handle a reply event
*
* @param array $params
*
* @return array
*/
function civicrm_mailer_event_reply($params) {
$errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash', 'replyTo'));
if (!empty($errors)) {
return $errors;
}
// CRM-7333: we can’t require fullEmail for backwards compatibility, but we should require either it or bodyTxt
if (empty($params['fullEmail']) and empty($params['bodyTxt'])) {
return civicrm_create_error('Required parameter missing: either "fullEmail" or "bodyTxt" is required');
}
$job = $params['job_id'];
$queue = $params['event_queue_id'];
$hash = $params['hash'];
$bodyTxt = $params['bodyTxt'];
$replyto = $params['replyTo'];
$bodyHTML = CRM_Utils_Array::value('bodyHTML', $params);
$fullEmail = CRM_Utils_Array::value('fullEmail', $params);
$mailing = CRM_Mailing_Event_BAO_Reply::reply($job, $queue, $hash, $replyto);
if (empty($mailing)) {
return civicrm_create_error(ts('Queue event could not be found'));
}
CRM_Mailing_Event_BAO_Reply::send($queue, $mailing, $bodyTxt, $replyto, $bodyHTML, $fullEmail);
return civicrm_create_success();
}
/**
* Handle a forward event
*
* @param array $params
*
* @return array
*/
function civicrm_mailer_event_forward($params) {
$errors = _civicrm_mailer_check_params($params, array('job_id', 'event_queue_id', 'hash', 'email'));
if (!empty($errors)) {
return $errors;
}
$job = $params['job_id'];
$queue = $params['event_queue_id'];
$hash = $params['hash'];
$email = $params['email'];
$fromEmail = CRM_Utils_Array::value('fromEmail', $params);
$params = CRM_Utils_Array::value('params', $params);
$forward = CRM_Mailing_Event_BAO_Forward::forward($job, $queue, $hash, $email, $fromEmail, $params);
if ($forward) {
return civicrm_create_success();
}
return civicrm_create_error(ts('Queue event could not be found'));
}
/**
* Handle a click event
*
* @param array $params
*
* @return array
*/
function civicrm_mailer_event_click($params) {
$errors = _civicrm_mailer_check_params($params, array('event_queue_id', 'url_id'));
if (!empty($errors)) {
return $errors;
}
$url_id = $params['url_id'];
$queue = $params['event_queue_id'];
$url = CRM_Mailing_Event_BAO_TrackableURLOpen::track($queue, $url_id);
$values = array();
$values['url'] = $url;
$values['is_error'] = 0;
return $values;
}
/**
* Handle an open event
*
* @param array $params
*
* @return array
*/
function civicrm_mailer_event_open($params) {
$errors = _civicrm_mailer_check_params($params, array('event_queue_id'));
if (!empty($errors)) {
return $errors;
}
$queue = $params['event_queue_id'];
$success = CRM_Mailing_Event_BAO_Opened::open($queue);
if (!$success) {
return civicrm_create_error(ts('mailer open event failed'));
}
return civicrm_create_success();
}
/**
* Helper function to check for required params
*
* @param array $params associated array of fields
* @param array $required array of required fields
*
* @return array $error array with errors, null if none
*/
function _civicrm_mailer_check_params(&$params, $required) {
// return error if we do not get any params
if (empty($params)) {
return civicrm_create_error(ts('Input Parameters empty'));
}
if (!is_array($params)) {
return civicrm_create_error(ts('Input parameter is not an array'));
}
foreach ($required as $name) {
if (!array_key_exists($name, $params) || !$params[$name]) {
return civicrm_create_error(ts('Required parameter missing: "%1"', array(1 => $name)));
}
}
return NULL;
}
<?php
// $Id: Membership.php 45502 2013-02-08 13:32:55Z kurund $
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.3 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2013 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
/**
* File for the CiviCRM APIv2 membership functions
*
* @package CiviCRM_APIv2
* @subpackage API_Membership
*
* @copyright CiviCRM LLC (c) 2004-2013
* @version $Id: Membership.php 45502 2013-02-08 13:32:55Z kurund $
*
*/
/**
* Files required for this package
*/
require_once 'api/v2/utils.php';
require_once 'CRM/Utils/Rule.php';
require_once 'api/v2/MembershipContact.php';
require_once 'api/v2/MembershipType.php';
require_once 'api/v2/MembershipStatus.php';
/**
* Deletes an existing contact membership
*
* This API is used for deleting a contact membership
*
* @param Int $membershipID Id of the contact membership to be deleted
*
* @return null if successfull, object of CRM_Core_Error otherwise
* @access public
*/
function civicrm_membership_delete(&$membershipID) {
_civicrm_initialize();
if (empty($membershipID)) {
return civicrm_create_error('Membership ID cannot be empty.');
}
// membershipID should be numeric
if (!is_numeric($membershipID)) {
return civicrm_create_error('Input parameter should be numeric');
}
require_once 'CRM/Member/BAO/Membership.php';
CRM_Member_BAO_Membership::deleteRelatedMemberships($membershipID);
$membership = new CRM_Member_BAO_Membership();
$result = $membership->deleteMembership($membershipID);
return $result ? civicrm_create_success() : civicrm_create_error('Error while deleting Membership');
}
/**
*
* @param <type> $contactID
*
* @return <type>
* @deprecated compatilibility wrappers
*/
function civicrm_contact_memberships_get(&$contactID) {
return civicrm_membership_contact_get($contactID);
}
/**
*
* @param <type> $params
*
* @return <type>
*/
function civicrm_contact_membership_create(&$params) {
return civicrm_membership_contact_create($params);
}
/**
* wrapper function according to new api standards
*/
function civicrm_membership_create(&$params) {
return civicrm_membership_contact_create($params);
}
/**
*
* @param <type> $params
*
* @return <type>
*/
function civicrm_membership_types_get(&$params) {
return civicrm_membership_type_get($params);
}
/**
*
* @param <type> $params
*
* @return <type>
*/
function civicrm_membership_statuses_get(&$params) {
return civicrm_membership_status_get($params);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment