Merge pull request #3209 from eileenmcnaughton/comments
[civicrm-core.git] / api / v3 / Contact.php
index 81c8662a34ff506d77c6ea9450a010b7ce53541d..438544a5aeb9fd7999ec8de0c38ab234040d8e2f 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
   +--------------------------------------------------------------------+
-  | CiviCRM version 4.4                                                |
+  | CiviCRM version 4.5                                                |
   +--------------------------------------------------------------------+
-  | Copyright CiviCRM LLC (c) 2004-2013                                |
+  | Copyright CiviCRM LLC (c) 2004-2014                                |
   +--------------------------------------------------------------------+
   | This file is a part of CiviCRM.                                    |
   |                                                                    |
@@ -32,7 +32,7 @@
  *
  * @package CiviCRM_APIv3
  * @subpackage API_Contact
- * @copyright CiviCRM LLC (c) 2004-2013
+ * @copyright CiviCRM LLC (c) 2004-2014
  * $Id: Contact.php 30879 2010-11-22 15:45:55Z shot $
  *
  */
 /**
  * Create or update a contact (note you should always call this via civicrm_api() & never directly)
  *
- * @param  array   $params   input parameters
+ * @param  array $params input parameters
  *
  * Allowed @params array keys are:
  * {@getfields contact_create}
  *
  *
+ * @throws API_Exception
  * @example ContactCreate.php Example of Create Call
  *
  * @return array  API Result Array
@@ -201,6 +202,10 @@ function _civicrm_api3_contact_get_spec(&$params) {
   $params['group_id']['title'] = 'Group Memberships (filter)';
   $params['group']['title'] = 'Group Memberships (filter, array)';
   $params['tag']['title'] = 'Assigned tags (filter, array)';
+  $params['birth_date_low'] = array('name' => 'birth_date_low', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Birthdate is equal to or greater than'));
+  $params['birth_date_high'] = array('name' => 'birth_date_high', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Birthdate is equal to or less than'));
+  $params['deceased_date_low'] = array('name' => 'deceased_date_low','type' => CRM_Utils_Type::T_DATE, 'title' => ts('Deceased Date is equal to or greater than'));
+  $params['deceased_date_high'] = array('name' => 'deceased_date_high', 'type' => CRM_Utils_Type::T_DATE, 'title' => ts('Deceased Date is equal to or less than'));
 }
 
 /**
@@ -325,17 +330,18 @@ function _civicrm_api3_contact_check_params( &$params, $dupeCheck = true, $dupeE
       $dedupeParams['check_permission'] = $params['check_permission'];
     }
 
-    $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Strict', array());
+    $ids = CRM_Dedupe_Finder::dupesByParams($dedupeParams, $params['contact_type'], 'Unsupervised', array());
 
     if (count($ids) >0) {
       throw new API_Exception("Found matching contacts: ". implode(',',$ids),"duplicate",array("ids"=>$ids));
     }
   }
 
-  //check for organisations with same name
+  // The BAO no longer supports the legacy param "current_employer" so here is a shim for api backward-compatability
   if (!empty($params['current_employer'])) {
-    $organizationParams = array();
-    $organizationParams['organization_name'] = $params['current_employer'];
+    $organizationParams = array(
+      'organization_name' => $params['current_employer'],
+    );
 
     $dedupParams = CRM_Dedupe_Finder::formatParams($organizationParams, 'Organization');
 
@@ -351,6 +357,17 @@ function _civicrm_api3_contact_check_params( &$params, $dupeCheck = true, $dupeE
     if (empty($params['employer_id']) && (count($dupeIds) > 1)) {
       throw new API_Exception('Found more than one Organisation with same Name.');
     }
+
+    if ($dupeIds) {
+      $params['employer_id'] = $dupeIds[0];
+    }
+    else {
+      $result = civicrm_api3('contact', 'create', array(
+        'organization_name' => $params['current_employer'],
+        'contact_type' => 'Organization'
+      ));
+      $params['employer_id'] = $result['id'];
+    }
   }
 
   return NULL;
@@ -382,6 +399,7 @@ function _civicrm_api3_contact_update($params, $contactID = NULL) {
  * @param  $params                   Associative array of property name/value
  *                                   pairs to insert in new contact.
  *
+ * @throws API_Exception
  * @return array (reference )        null on success, error message otherwise
  *
  * @access public
@@ -650,6 +668,11 @@ function civicrm_api3_contact_getquick($params) {
     $where .= " AND cc.contact_sub_type = '{$contactSubType}'";
   }
 
+  if (!empty($params['contact_type'])) {
+    $contactType = CRM_Utils_Type::escape($params['contact_type'], 'String');
+    $where .= " AND cc.contact_type LIKE '{$contactType}'";
+  }
+
   //set default for current_employer or return contact with particular id
   if (!empty($params['id'])) {
     $where .= " AND cc.id = " . (int) $params['id'];
@@ -886,7 +909,7 @@ WHERE     $whereClause
 
 
 /**
- * Overrides _civicrm_api3_generic_getlist_params.
+ * @see _civicrm_api3_generic_getlist_params
  *
  * @param $request array
  */
@@ -918,15 +941,18 @@ function _civicrm_api3_contact_getlist_params(&$request) {
   if(!in_array($searchField, $list)) {
     $list[] = $searchField;
   }
+  $request['description_field'] = $list;
   $list[] = 'contact_type';
-  $request['params']['return'] = $list;
+  $request['params']['return'] = array_unique(array_merge($list, $request['extra']));
   $request['params']['options']['sort'] = 'sort_name';
   // Contact api doesn't support array(LIKE => 'foo') syntax
-  $request['params'][$request['search_field']] = $request['input'];
+  if (!empty($request['input'])) {
+    $request['params'][$request['search_field']] = $request['input'];
+  }
 }
 
 /**
- * Overrides _civicrm_api3_generic_getlist_output
+ * @see _civicrm_api3_generic_getlist_output
  *
  * @param $result array
  * @param $request array
@@ -936,24 +962,36 @@ function _civicrm_api3_contact_getlist_params(&$request) {
 function _civicrm_api3_contact_getlist_output($result, $request) {
   $output = array();
   if (!empty($result['values'])) {
+    $addressFields = array_intersect(array('street_address', 'city', 'state_province', 'country'), $request['params']['return']);
     foreach ($result['values'] as $row) {
       $data = array(
         'id' => $row[$request['id_field']],
         'label' => $row[$request['label_field']],
+        'description' => array(),
       );
-      $description = array();
-      foreach ($request['params']['return'] as $item) {
-        if (!strpos($item, '_name') && $item != 'contact_type' && !empty($row[$item])) {
-          $description[] = $row[$item];
+      foreach ($request['description_field'] as $item) {
+        if (!strpos($item, '_name') && !in_array($item, $addressFields) && !empty($row[$item])) {
+          $data['description'][] = $row[$item];
+        }
+      }
+      $address = array();
+      foreach($addressFields as $item) {
+        if (!empty($row[$item])) {
+          $address[] = $row[$item];
         }
       }
-      $data['description'] = implode(' :: ', $description);
+      if ($address) {
+        $data['description'][] = implode(' ', $address);
+      }
       if (!empty($request['image_field'])) {
         $data['image'] = isset($row[$request['image_field']]) ? $row[$request['image_field']] : '';
       }
       else {
         $data['icon_class'] = $row['contact_type'];
       }
+      foreach ($request['extra'] as $field) {
+        $data['extra'][$field] = isset($row[$field]) ? $row[$field] : NULL;
+      }
       $output[] = $data;
     }
   }