コード例 #1
0
 /** 
  * takes an associative array and creates a contact object and all the associated 
  * derived objects (i.e. individual, location, email, phone etc) 
  * 
  * This function is invoked from within the web form layer and also from the api layer
  * primarily from the profile / contribute forms where we dont have a nice hierarchy
  * and are too lazy to create one. This function should be obsoleted at some time
  * 
  * @param array $params (reference ) an assoc array of name/value pairs 
  * @param array $ids    the array that holds all the db ids 
  * 
  * @return object CRM_Contact_BAO_Contact object  
  * @access public 
  * @static 
  */
 function &createFlat(&$params, &$ids)
 {
     require_once 'CRM/Utils/Hook.php';
     if (CRM_Utils_Array::value('contact', $ids)) {
         CRM_Utils_Hook::pre('edit', 'Individual', $ids['contact'], $params);
     } else {
         CRM_Utils_Hook::pre('create', 'Individual', null, $params);
     }
     CRM_Core_DAO::transaction('BEGIN');
     $params['contact_type'] = 'Individual';
     $contact = CRM_Contact_BAO_Contact::add($params, $ids);
     $params['contact_id'] = $contact->id;
     require_once 'CRM/Contact/BAO/Individual.php';
     CRM_Contact_BAO_Individual::add($params, $ids);
     require_once 'CRM/Core/BAO/LocationType.php';
     $locationType =& CRM_Core_BAO_LocationType::getDefault();
     $locationTypeId = $locationType->id;
     $locationIds = CRM_Utils_Array::value('location', $ids);
     // extract the first location id
     if ($locationIds) {
         foreach ($locationIds as $dontCare => $locationId) {
             $locationIds = $locationId;
             break;
         }
     }
     $location =& new CRM_Core_DAO_Location();
     $location->location_type_id = $locationTypeId;
     $location->entity_table = 'civicrm_contact';
     $location->entity_id = $contact->id;
     $location->id = CRM_Utils_Array::value('id', $locationIds);
     if ($location->find(true)) {
         if (!$location->is_primary) {
             $location->is_primary = true;
         }
     } else {
         $location->is_primary = true;
     }
     $location->save();
     $address =& new CRM_Core_BAO_Address();
     CRM_Core_BAO_Address::fixAddress($params);
     if (!$address->copyValues($params)) {
         $address->id = CRM_Utils_Array::value('address', $locationIds);
         $address->location_id = $location->id;
         $address->save();
     }
     $phone =& new CRM_Core_BAO_Phone();
     if (!$phone->copyValues($params)) {
         $blockIds = CRM_Utils_Array::value('phone', $locationIds);
         $phone->id = CRM_Utils_Array::value(1, $blockIds);
         $phone->location_id = $location->id;
         $phone->is_primary = true;
         $phone->save();
     }
     $email =& new CRM_Core_BAO_Email();
     if (!$email->copyValues($params)) {
         $blockIds = CRM_Utils_Array::value('email', $locationIds);
         $email->id = CRM_Utils_Array::value(1, $blockIds);
         $email->location_id = $location->id;
         $email->is_primary = true;
         $email->save();
     }
     /* Process custom field values and other values */
     foreach ($params as $key => $value) {
         if ($key == 'group') {
             CRM_Contact_BAO_GroupContact::create($params['group'], $contact->id);
         } else {
             if ($key == 'tag') {
                 require_once 'CRM/Core/BAO/EntityTag.php';
                 CRM_Core_BAO_EntityTag::create($params['tag'], $contact->id);
             } else {
                 if ($cfID = CRM_Core_BAO_CustomField::getKeyID($key)) {
                     $custom_field_id = $cfID;
                     $cf =& new CRM_Core_BAO_CustomField();
                     $cf->id = $custom_field_id;
                     if ($cf->find(true)) {
                         switch ($cf->html_type) {
                             case 'Select Date':
                                 $date = CRM_Utils_Date::format($value);
                                 if (!$date) {
                                     $date = '';
                                 }
                                 $customValue = $date;
                                 break;
                             case 'CheckBox':
                                 $customValue = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, array_keys($value));
                                 break;
                                 //added a case for Multi-Select
                             //added a case for Multi-Select
                             case 'Multi-Select':
                                 $customValue = implode(CRM_CORE_BAO_CUSTOMOPTION_VALUE_SEPERATOR, array_keys($value));
                                 break;
                             default:
                                 $customValue = $value;
                         }
                     }
                     CRM_Core_BAO_CustomValue::updateValue($contact->id, $custom_field_id, $customValue);
                 }
             }
         }
     }
     CRM_Core_DAO::transaction('COMMIT');
     if (CRM_Utils_Array::value('contact', $ids)) {
         CRM_Utils_Hook::post('edit', 'Individual', $contact->id, $contact);
     } else {
         CRM_Utils_Hook::post('create', 'Individual', $contact->id, $contact);
     }
     return $contact;
 }