Exemple #1
0
 /**
  * Create contact.
  *
  * 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 bool $fixAddress
  *   If we need to fix address.
  * @param bool $invokeHooks
  *   If we need to invoke hooks.
  *
  * @param bool $skipDelete
  *   Unclear parameter, passed to website create
  *
  * @todo explain this parameter
  *
  * @throws Exception
  * @return CRM_Contact_BAO_Contact|CRM_Core_Error
  *   Created or updated contribution object. We are deprecating returning an error in
  *   favour of exceptions
  */
 public static function &create(&$params, $fixAddress = TRUE, $invokeHooks = TRUE, $skipDelete = FALSE)
 {
     $contact = NULL;
     if (empty($params['contact_type']) && empty($params['contact_id'])) {
         return $contact;
     }
     $isEdit = TRUE;
     if ($invokeHooks) {
         if (!empty($params['contact_id'])) {
             CRM_Utils_Hook::pre('edit', $params['contact_type'], $params['contact_id'], $params);
         } else {
             CRM_Utils_Hook::pre('create', $params['contact_type'], NULL, $params);
             $isEdit = FALSE;
         }
     }
     $config = CRM_Core_Config::singleton();
     // CRM-6942: set preferred language to the current language if it’s unset (and we’re creating a contact).
     if (empty($params['contact_id'])) {
         // A case could be made for checking isset rather than empty but this is more consistent with previous behaviour.
         if (empty($params['preferred_language']) && ($language = CRM_Core_I18n::getContactDefaultLanguage()) != FALSE) {
             $params['preferred_language'] = $language;
         }
         // CRM-9739: set greeting & addressee if unset and we’re creating a contact.
         foreach (self::$_greetingTypes as $greeting) {
             if (empty($params[$greeting . '_id'])) {
                 if ($defaultGreetingTypeId = CRM_Contact_BAO_Contact_Utils::defaultGreeting($params['contact_type'], $greeting)) {
                     $params[$greeting . '_id'] = $defaultGreetingTypeId;
                 }
             }
         }
     }
     $transaction = new CRM_Core_Transaction();
     $contact = self::add($params);
     if (!$contact) {
         // Not dying here is stupid, since we get into weird situation and into a bug that
         // is impossible to figure out for the user or for us
         // CRM-7925
         CRM_Core_Error::fatal();
     }
     $params['contact_id'] = $contact->id;
     if (Civi::settings()->get('is_enabled')) {
         // Enabling multisite causes the contact to be added to the domain group.
         $domainGroupID = CRM_Core_BAO_Domain::getGroupId();
         if (!empty($domainGroupID)) {
             if (!empty($params['group']) && is_array($params['group'])) {
                 $params['group'][$domainGroupID] = 1;
             } else {
                 $params['group'] = array($domainGroupID => 1);
             }
         }
     }
     if (array_key_exists('group', $params)) {
         $contactIds = array($params['contact_id']);
         foreach ($params['group'] as $groupId => $flag) {
             if ($flag == 1) {
                 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
             } elseif ($flag == -1) {
                 CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contactIds, $groupId);
             }
         }
     }
     // Add location Block data.
     $blocks = CRM_Core_BAO_Location::create($params, $fixAddress);
     foreach ($blocks as $name => $value) {
         $contact->{$name} = $value;
     }
     if (!empty($params['updateBlankLocInfo'])) {
         $skipDelete = TRUE;
     }
     //add website
     CRM_Core_BAO_Website::create($params['website'], $contact->id, $skipDelete);
     //get userID from session
     $session = CRM_Core_Session::singleton();
     $userID = $session->get('userID');
     // add notes
     if (!empty($params['note'])) {
         if (is_array($params['note'])) {
             foreach ($params['note'] as $note) {
                 $contactId = $contact->id;
                 if (isset($note['contact_id'])) {
                     $contactId = $note['contact_id'];
                 }
                 //if logged in user, overwrite contactId
                 if ($userID) {
                     $contactId = $userID;
                 }
                 $noteParams = array('entity_id' => $contact->id, 'entity_table' => 'civicrm_contact', 'note' => $note['note'], 'subject' => CRM_Utils_Array::value('subject', $note), 'contact_id' => $contactId);
                 CRM_Core_BAO_Note::add($noteParams, CRM_Core_DAO::$_nullArray);
             }
         } else {
             $contactId = $contact->id;
             if (isset($note['contact_id'])) {
                 $contactId = $note['contact_id'];
             }
             //if logged in user, overwrite contactId
             if ($userID) {
                 $contactId = $userID;
             }
             $noteParams = array('entity_id' => $contact->id, 'entity_table' => 'civicrm_contact', 'note' => $params['note'], 'subject' => CRM_Utils_Array::value('subject', $params), 'contact_id' => $contactId);
             CRM_Core_BAO_Note::add($noteParams, CRM_Core_DAO::$_nullArray);
         }
     }
     // update the UF user_unique_id if that has changed
     CRM_Core_BAO_UFMatch::updateUFName($contact->id);
     if (!empty($params['custom']) && is_array($params['custom'])) {
         CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_contact', $contact->id);
     }
     // make a civicrm_subscription_history entry only on contact create (CRM-777)
     if (empty($params['contact_id'])) {
         $subscriptionParams = array('contact_id' => $contact->id, 'status' => 'Added', 'method' => 'Admin');
         CRM_Contact_BAO_SubscriptionHistory::create($subscriptionParams);
     }
     $transaction->commit();
     // CRM-6367: fetch the right label for contact type’s display
     $contact->contact_type_display = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_ContactType', $contact->contact_type, 'label', 'name');
     if (!$config->doNotResetCache) {
         // Note: doNotResetCache flag is currently set by import contact process and merging,
         // since resetting and
         // rebuilding cache could be expensive (for many contacts). We might come out with better
         // approach in future.
         CRM_Contact_BAO_Contact_Utils::clearContactCaches($contact->id);
     }
     if ($invokeHooks) {
         if ($isEdit) {
             CRM_Utils_Hook::post('edit', $params['contact_type'], $contact->id, $contact);
         } else {
             CRM_Utils_Hook::post('create', $params['contact_type'], $contact->id, $contact);
         }
     }
     // process greetings CRM-4575, cache greetings
     self::processGreetings($contact);
     return $contact;
 }