Beispiel #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
  *
  * @param array $params (reference ) an assoc array of name/value pairs
  * @param array $ids    the array that holds all the db ids
  * @param int   $maxLocationBlocks the maximum number of location blocks to process
  *
  * @return object CRM_Contact_BAO_Contact object 
  * @access public
  * @static
  */
 function &create(&$params, &$ids, $maxLocationBlocks)
 {
     require_once 'CRM/Utils/Hook.php';
     if (CRM_Utils_Array::value('contact', $ids)) {
         CRM_Utils_Hook::pre('edit', $params['contact_type'], $ids['contact'], $params);
     } else {
         CRM_Utils_Hook::pre('create', $params['contact_type'], null, $params);
     }
     CRM_Core_DAO::transaction('BEGIN');
     $contact = CRM_Contact_BAO_Contact::add($params, $ids);
     $params['contact_id'] = $contact->id;
     // invoke the add operator on the contact_type class
     require_once str_replace('_', DIRECTORY_SEPARATOR, "CRM_Contact_BAO_" . $params['contact_type']) . ".php";
     eval('$contact->contact_type_object =& CRM_Contact_BAO_' . $params['contact_type'] . '::add($params, $ids);');
     $location = array();
     for ($locationId = 1; $locationId <= $maxLocationBlocks; $locationId++) {
         // start of for loop for location
         $location[$locationId] = CRM_Core_BAO_Location::add($params, $ids, $locationId);
     }
     $contact->location = $location;
     // add notes
     if (CRM_Utils_Array::value('note', $params)) {
         if (is_array($params['note'])) {
             foreach ($params['note'] as $note) {
                 $noteParams = array('entity_id' => $contact->id, 'entity_table' => 'civicrm_contact', 'note' => $note['note']);
                 CRM_Core_BAO_Note::add($noteParams);
             }
         } else {
             $noteParams = array('entity_id' => $contact->id, 'entity_table' => 'civicrm_contact', 'note' => $params['note']);
             CRM_Core_BAO_Note::add($noteParams);
         }
     }
     // update the UF email if that has changed
     require_once 'CRM/Core/BAO/UFMatch.php';
     CRM_Core_BAO_UFMatch::updateUFEmail($contact->id);
     // add custom field values
     if (CRM_Utils_Array::value('custom', $params)) {
         foreach ($params['custom'] as $customValue) {
             $cvParams = array('entity_table' => 'civicrm_contact', 'entity_id' => $contact->id, 'value' => $customValue['value'], 'type' => $customValue['type'], 'custom_field_id' => $customValue['custom_field_id']);
             if ($customValue['id']) {
                 $cvParams['id'] = $customValue['id'];
             }
             CRM_Core_BAO_CustomValue::create($cvParams);
         }
     }
     // make a civicrm_subscription_history entry only on contact create (CRM-777)
     if (!CRM_Utils_Array::value('contact', $ids)) {
         $subscriptionParams = array('contact_id' => $contact->id, 'status' => 'Added', 'method' => 'Admin');
         CRM_Contact_BAO_SubscriptionHistory::create($subscriptionParams);
     }
     CRM_Core_DAO::transaction('COMMIT');
     if (CRM_Utils_Array::value('contact', $ids)) {
         CRM_Utils_Hook::post('edit', $params['contact_type'], $contact->id, $contact);
     } else {
         CRM_Utils_Hook::post('create', $params['contact_type'], $contact->id, $contact);
     }
     $contact->contact_type_display = CRM_Contact_DAO_Contact::tsEnum('contact_type', $contact->contact_type);
     return $contact;
 }