示例#1
0
文件: Group.php 项目: ksecor/civicrm
 /**
  * Create a new group
  *
  * @param array $params     Associative array of parameters
  * @return object|null      The new group BAO (if created)
  * @access public
  * @static
  */
 public static function &create(&$params)
 {
     require_once 'CRM/Utils/Hook.php';
     if (CRM_Utils_Array::value('id', $params)) {
         CRM_Utils_Hook::pre('edit', 'Group', $params['id'], $params);
     } else {
         CRM_Utils_Hook::pre('create', 'Group', null, $params);
     }
     // form the name only if missing: CRM-627
     if (!CRM_Utils_Array::value('name', $params)) {
         require_once 'CRM/Utils/String.php';
         $params['name'] = CRM_Utils_String::titleToVar($params['title']);
     }
     // convert params if array type
     if (isset($params['group_type'])) {
         if (is_array($params['group_type'])) {
             $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($params['group_type'])) . CRM_Core_DAO::VALUE_SEPARATOR;
         }
     } else {
         $params['group_type'] = '';
     }
     $group =& new CRM_Contact_BAO_Group();
     $group->copyValues($params);
     $group->save();
     if (!$group->id) {
         return null;
     }
     $group->buildClause();
     $group->save();
     // add custom field values
     if (CRM_Utils_Array::value('custom', $params)) {
         require_once 'CRM/Core/BAO/CustomValueTable.php';
         CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_group', $group->id);
     }
     // make the group, child of domain/site group by default.
     require_once 'CRM/Contact/BAO/GroupContactCache.php';
     require_once 'CRM/Core/BAO/Domain.php';
     require_once 'CRM/Contact/BAO/GroupNesting.php';
     $domainGroupID = CRM_Core_BAO_Domain::getGroupId();
     if (CRM_Utils_Array::value('no_parent', $params) !== 1) {
         if (defined('CIVICRM_MULTISITE') && CIVICRM_MULTISITE && empty($params['parents']) && $domainGroupID != $group->id && !CRM_Contact_BAO_GroupNesting::hasParentGroups($group->id)) {
             // if no parent present and the group doesn't already have any parents,
             // make sure site group goes as parent
             $params['parents'] = array($domainGroupID => 1);
         } else {
             if (!is_array($params['parents'])) {
                 $params['parents'] = array($params['parents'] => 1);
             }
         }
         foreach ($params['parents'] as $parentId => $dnc) {
             if ($parentId && !CRM_Contact_BAO_GroupNesting::isParentChild($parentId, $group->id)) {
                 CRM_Contact_BAO_GroupNesting::add($parentId, $group->id);
             }
         }
         // clear any descendant groups cache if exists
         require_once 'CRM/Core/BAO/Cache.php';
         $finalGroups =& CRM_Core_BAO_Cache::deleteGroup('descendant groups for an org');
         // this is always required, since we don't know when a
         // parent group is removed
         require_once 'CRM/Contact/BAO/GroupNestingCache.php';
         CRM_Contact_BAO_GroupNestingCache::update();
         // update group contact cache for all parent groups
         $parentIds = CRM_Contact_BAO_GroupNesting::getParentGroupIds($group->id);
         foreach ($parentIds as $parentId) {
             CRM_Contact_BAO_GroupContactCache::add($parentId);
         }
     }
     if (CRM_Utils_Array::value('organization_id', $params)) {
         require_once 'CRM/Contact/BAO/GroupOrganization.php';
         $groupOrg = array();
         $groupOrg = $params;
         $groupOrg['group_id'] = $group->id;
         CRM_Contact_BAO_GroupOrganization::add($groupOrg);
     }
     CRM_Contact_BAO_GroupContactCache::add($group->id);
     if (CRM_Utils_Array::value('id', $params)) {
         CRM_Utils_Hook::post('edit', 'Group', $group->id, $group);
     } else {
         CRM_Utils_Hook::post('create', 'Group', $group->id, $group);
     }
     return $group;
 }
示例#2
0
 /**
  * Create a new group.
  *
  * @param array $params
  *
  * @return CRM_Contact_BAO_Group|NULL
  *   The new group BAO (if created)
  */
 public static function &create(&$params)
 {
     if (!empty($params['id'])) {
         CRM_Utils_Hook::pre('edit', 'Group', $params['id'], $params);
     } else {
         CRM_Utils_Hook::pre('create', 'Group', NULL, $params);
     }
     // form the name only if missing: CRM-627
     $nameParam = CRM_Utils_Array::value('name', $params, NULL);
     if (!$nameParam && empty($params['id'])) {
         $params['name'] = CRM_Utils_String::titleToVar($params['title']);
     }
     // convert params if array type
     if (isset($params['group_type'])) {
         if (is_array($params['group_type'])) {
             $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($params['group_type'])) . CRM_Core_DAO::VALUE_SEPARATOR;
         }
     } else {
         $params['group_type'] = '';
     }
     $session = CRM_Core_Session::singleton();
     $cid = $session->get('userID');
     // this action is add
     if ($cid && empty($params['id'])) {
         $params['created_id'] = $cid;
     }
     // this action is update
     if ($cid && !empty($params['id'])) {
         $params['modified_id'] = $cid;
     }
     $group = new CRM_Contact_BAO_Group();
     $group->copyValues($params);
     //@todo very hacky fix for the fact this function wants to receive 'parents' as an array further down but
     // needs it as a separated string for the DB. Preferred approaches are having the copyParams or save fn
     // use metadata to translate the array to the appropriate DB type or altering the param in the api layer,
     // or at least altering the param in same section as 'group_type' rather than repeating here. However, further down
     // we need the $params one to be in it's original form & we are not sure what test coverage we have on that
     if (isset($group->parents) && is_array($group->parents)) {
         $group->parents = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($group->parents)) . CRM_Core_DAO::VALUE_SEPARATOR;
     }
     if (empty($params['id']) && !$nameParam) {
         $group->name .= "_tmp";
     }
     $group->save();
     if (!$group->id) {
         return NULL;
     }
     if (empty($params['id']) && !$nameParam) {
         $group->name = substr($group->name, 0, -4) . "_{$group->id}";
     }
     $group->buildClause();
     $group->save();
     // add custom field values
     if (!empty($params['custom'])) {
         CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_group', $group->id);
     }
     // make the group, child of domain/site group by default.
     $domainGroupID = CRM_Core_BAO_Domain::getGroupId();
     if (CRM_Utils_Array::value('no_parent', $params) !== 1) {
         if (empty($params['parents']) && $domainGroupID != $group->id && CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, 'is_enabled') && !CRM_Contact_BAO_GroupNesting::hasParentGroups($group->id)) {
             // if no parent present and the group doesn't already have any parents,
             // make sure site group goes as parent
             $params['parents'] = array($domainGroupID => 1);
         } elseif (array_key_exists('parents', $params) && !is_array($params['parents'])) {
             $params['parents'] = array($params['parents'] => 1);
         }
         if (!empty($params['parents'])) {
             foreach ($params['parents'] as $parentId => $dnc) {
                 if ($parentId && !CRM_Contact_BAO_GroupNesting::isParentChild($parentId, $group->id)) {
                     CRM_Contact_BAO_GroupNesting::add($parentId, $group->id);
                 }
             }
         }
         // clear any descendant groups cache if exists
         $finalGroups = CRM_Core_BAO_Cache::deleteGroup('descendant groups for an org');
         // this is always required, since we don't know when a
         // parent group is removed
         CRM_Contact_BAO_GroupNestingCache::update();
         // update group contact cache for all parent groups
         $parentIds = CRM_Contact_BAO_GroupNesting::getParentGroupIds($group->id);
         foreach ($parentIds as $parentId) {
             CRM_Contact_BAO_GroupContactCache::add($parentId);
         }
     }
     if (!empty($params['organization_id'])) {
         $groupOrg = array();
         $groupOrg = $params;
         $groupOrg['group_id'] = $group->id;
         CRM_Contact_BAO_GroupOrganization::add($groupOrg);
     }
     CRM_Contact_BAO_GroupContactCache::add($group->id);
     if (!empty($params['id'])) {
         CRM_Utils_Hook::post('edit', 'Group', $group->id, $group);
     } else {
         CRM_Utils_Hook::post('create', 'Group', $group->id, $group);
     }
     $recentOther = array();
     if (CRM_Core_Permission::check('edit groups')) {
         $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/group', 'reset=1&action=update&id=' . $group->id);
         // currently same permission we are using for delete a group
         $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/group', 'reset=1&action=delete&id=' . $group->id);
     }
     // add the recently added group (unless hidden: CRM-6432)
     if (!$group->is_hidden) {
         CRM_Utils_Recent::add($group->title, CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $group->id), $group->id, 'Group', NULL, NULL, $recentOther);
     }
     return $group;
 }
 /**
  * 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']) && empty($params['preferred_language'])) {
         $params['preferred_language'] = $config->lcMessages;
     }
     // CRM-9739: set greeting & addressee if unset and we’re creating a contact.
     if (empty($params['contact_id'])) {
         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 wierd 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 (CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, '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;
     }
     //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;
 }
示例#4
0
/**
 *
 * @param int $permission
 *
 * @return NULL|integer $groupID
 */
function _multisite_get_domain_group($permission = 1)
{
    $groupID = CRM_Core_BAO_Domain::getGroupId();
    if (empty($groupID) || !is_numeric($groupID)) {
        /* domain group not defined - we could let people know but
         * it is acceptable for some domains not to be in the multisite
         * so should probably check enabled before we spring an error
         */
        return NULL;
    }
    // We will check for the possiblility of the acl_enabled setting being deliberately set to 0
    if ($permission) {
        $aclsEnabled = civicrm_api('setting', 'getvalue', array('version' => 3, 'name' => 'multisite_acl_enabled', 'group' => 'Multi Site Preferences'));
        if (is_numeric($aclsEnabled) && !$aclsEnabled) {
            return NULL;
        }
    }
    return $groupID;
}
示例#5
0
 /**
  * Set default values for the form.
  *
  * @return array
  */
 public function setDefaultValues()
 {
     $defaults = array();
     if (isset($this->_id)) {
         $defaults = $this->_groupValues;
         if (!empty($defaults['group_type'])) {
             $types = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($defaults['group_type'], 1, -1));
             $defaults['group_type'] = array();
             foreach ($types as $type) {
                 $defaults['group_type'][$type] = 1;
             }
         }
         if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) {
             CRM_Contact_BAO_GroupOrganization::retrieve($this->_id, $defaults);
         }
     }
     if (!(CRM_Core_Permission::check('access CiviMail') || CRM_Mailing_Info::workflowEnabled() && CRM_Core_Permission::check('create mailings'))) {
         $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE);
         if ($defaults['group_type'][$groupTypes['Mailing List']] == 1) {
             $this->assign('freezeMailignList', $groupTypes['Mailing List']);
         } else {
             $this->assign('hideMailignList', $groupTypes['Mailing List']);
         }
     }
     if (empty($defaults['parents'])) {
         $defaults['parents'] = CRM_Core_BAO_Domain::getGroupId();
     }
     // custom data set defaults
     $defaults += CRM_Custom_Form_CustomData::setDefaultValues($this);
     return $defaults;
 }
示例#6
0
文件: Edit.php 项目: ksecor/civicrm
 function setDefaultValues()
 {
     $defaults = array();
     if (isset($this->_id)) {
         $defaults = $this->_groupValues;
         if (CRM_Utils_Array::value('group_type', $defaults)) {
             $types = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($defaults['group_type'], 1, -1));
             $defaults['group_type'] = array();
             foreach ($types as $type) {
                 $defaults['group_type'][$type] = 1;
             }
         }
         if (defined('CIVICRM_MULTISITE') && CIVICRM_MULTISITE && CRM_Core_Permission::check('administer Multiple Organizations')) {
             require_once 'CRM/Contact/BAO/GroupOrganization.php';
             CRM_Contact_BAO_GroupOrganization::retrieve($this->_id, $defaults);
             if (CRM_Utils_Array::value('group_organization', $defaults)) {
                 //used in edit mode
                 $this->_groupOrganizationID = $defaults['group_organization'];
             }
             $this->assign('organizationID', $defaults['organization_id']);
         }
     }
     if (!CRM_Utils_Array::value('parents', $defaults)) {
         $defaults['parents'] = CRM_Core_BAO_Domain::getGroupId();
     }
     // custom data set defaults
     $defaults += CRM_Custom_Form_Customdata::setDefaultValues($this);
     return $defaults;
 }
 /**
  * Create a new group
  *
  * @param array $params     Associative array of parameters
  *
  * @return object|null      The new group BAO (if created)
  * @access public
  * @static
  */
 public static function &create(&$params)
 {
     if (CRM_Utils_Array::value('id', $params)) {
         CRM_Utils_Hook::pre('edit', 'Group', $params['id'], $params);
     } else {
         CRM_Utils_Hook::pre('create', 'Group', NULL, $params);
     }
     // form the name only if missing: CRM-627
     if (!CRM_Utils_Array::value('name', $params) && !CRM_Utils_Array::value('id', $params)) {
         $params['name'] = CRM_Utils_String::titleToVar($params['title']);
     }
     // convert params if array type
     if (isset($params['group_type'])) {
         if (is_array($params['group_type'])) {
             $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($params['group_type'])) . CRM_Core_DAO::VALUE_SEPARATOR;
         }
     } else {
         $params['group_type'] = '';
     }
     $group = new CRM_Contact_BAO_Group();
     $group->copyValues($params);
     if (!CRM_Utils_Array::value('id', $params)) {
         $group->name .= "_tmp";
     }
     $group->save();
     if (!$group->id) {
         return NULL;
     }
     if (!CRM_Utils_Array::value('id', $params)) {
         $group->name = substr($group->name, 0, -4) . "_{$group->id}";
     }
     $group->buildClause();
     $group->save();
     // add custom field values
     if (CRM_Utils_Array::value('custom', $params)) {
         CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_group', $group->id);
     }
     // make the group, child of domain/site group by default.
     $domainGroupID = CRM_Core_BAO_Domain::getGroupId();
     if (CRM_Utils_Array::value('no_parent', $params) !== 1) {
         if (empty($params['parents']) && $domainGroupID != $group->id && CRM_Core_BAO_Setting::getItem(CRM_Core_BAO_Setting::MULTISITE_PREFERENCES_NAME, 'is_enabled') && !CRM_Contact_BAO_GroupNesting::hasParentGroups($group->id)) {
             // if no parent present and the group doesn't already have any parents,
             // make sure site group goes as parent
             $params['parents'] = array($domainGroupID => 1);
         } elseif (array_key_exists('parents', $params) && !is_array($params['parents'])) {
             $params['parents'] = array($params['parents'] => 1);
         }
         if (!empty($params['parents'])) {
             foreach ($params['parents'] as $parentId => $dnc) {
                 if ($parentId && !CRM_Contact_BAO_GroupNesting::isParentChild($parentId, $group->id)) {
                     CRM_Contact_BAO_GroupNesting::add($parentId, $group->id);
                 }
             }
         }
         // clear any descendant groups cache if exists
         $finalGroups = CRM_Core_BAO_Cache::deleteGroup('descendant groups for an org');
         // this is always required, since we don't know when a
         // parent group is removed
         CRM_Contact_BAO_GroupNestingCache::update();
         // update group contact cache for all parent groups
         $parentIds = CRM_Contact_BAO_GroupNesting::getParentGroupIds($group->id);
         foreach ($parentIds as $parentId) {
             CRM_Contact_BAO_GroupContactCache::add($parentId);
         }
     }
     if (CRM_Utils_Array::value('organization_id', $params)) {
         $groupOrg = array();
         $groupOrg = $params;
         $groupOrg['group_id'] = $group->id;
         CRM_Contact_BAO_GroupOrganization::add($groupOrg);
     }
     CRM_Contact_BAO_GroupContactCache::add($group->id);
     if (CRM_Utils_Array::value('id', $params)) {
         CRM_Utils_Hook::post('edit', 'Group', $group->id, $group);
     } else {
         CRM_Utils_Hook::post('create', 'Group', $group->id, $group);
     }
     $recentOther = array();
     if (CRM_Core_Permission::check('edit groups')) {
         $recentOther['editUrl'] = CRM_Utils_System::url('civicrm/group', 'reset=1&action=update&id=' . $group->id);
         // currently same permission we are using for delete a group
         $recentOther['deleteUrl'] = CRM_Utils_System::url('civicrm/group', 'reset=1&action=delete&id=' . $group->id);
     }
     // add the recently added group (unless hidden: CRM-6432)
     if (!$group->is_hidden) {
         CRM_Utils_Recent::add($group->title, CRM_Utils_System::url('civicrm/group/search', 'reset=1&force=1&context=smog&gid=' . $group->id), $group->id, 'Group', NULL, NULL, $recentOther);
     }
     return $group;
 }
示例#8
0
 /**
  * Function to 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 boolean $fixAddress  if we need to fix address
  * @param boolean $invokeHooks if we need to invoke hooks
  *
  * @return object CRM_Contact_BAO_Contact object 
  * @access public
  * @static
  */
 static function &create(&$params, $fixAddress = true, $invokeHooks = true)
 {
     $contact = null;
     if (!CRM_Utils_Array::value('contact_type', $params) && !CRM_Utils_Array::value('contact_id', $params)) {
         return $contact;
     }
     $isEdit = true;
     if ($invokeHooks) {
         require_once 'CRM/Utils/Hook.php';
         if (CRM_Utils_Array::value('contact_id', $params)) {
             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;
         }
     }
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     $contact = self::add($params);
     $params['contact_id'] = $contact->id;
     if (defined('CIVICRM_MULTISITE') && CIVICRM_MULTISITE) {
         // in order to make sure that every contact must be added to a group (CRM-4613) -
         require_once 'CRM/Core/BAO/Domain.php';
         $domainGroupID = CRM_Core_BAO_Domain::getGroupId();
         if (CRM_Utils_Array::value('group', $params) && is_array($params['group'])) {
             $grpFlp = array_flip($params['group']);
             if (!array_key_exists(1, $grpFlp)) {
                 $params['group'][$domainGroupID] = 1;
             }
         } else {
             $params['group'] = array($domainGroupID => 1);
         }
     }
     if (array_key_exists('group', $params)) {
         require_once 'CRM/Contact/BAO/GroupContact.php';
         $contactIds = array($params['contact_id']);
         foreach ($params['group'] as $groupId => $flag) {
             if ($flag == 1) {
                 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
             } else {
                 if ($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;
     }
     //get userID from session
     $session =& CRM_Core_Session::singleton();
     $userID = $session->get('userID');
     // add notes
     if (CRM_Utils_Array::value('note', $params)) {
         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' => $note['subject'], '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
     require_once 'CRM/Core/BAO/UFMatch.php';
     CRM_Core_BAO_UFMatch::updateUFName($contact->id);
     if (CRM_Utils_Array::value('custom', $params) && is_array($params['custom'])) {
         require_once 'CRM/Core/BAO/CustomValueTable.php';
         CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_contact', $contact->id);
     }
     // make a civicrm_subscription_history entry only on contact create (CRM-777)
     if (!CRM_Utils_Array::value('contact_id', $params)) {
         $subscriptionParams = array('contact_id' => $contact->id, 'status' => 'Added', 'method' => 'Admin');
         CRM_Contact_BAO_SubscriptionHistory::create($subscriptionParams);
     }
     $transaction->commit();
     $contact->contact_type_display = CRM_Contact_DAO_Contact::tsEnum('contact_type', $contact->contact_type);
     // reset the group contact cache for this group
     require_once 'CRM/Contact/BAO/GroupContactCache.php';
     CRM_Contact_BAO_GroupContactCache::remove();
     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);
         }
     }
     return $contact;
 }
示例#9
0
 /**
  * Function to 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 boolean $fixAddress  if we need to fix address
  * @param boolean $invokeHooks if we need to invoke hooks
  *
  * @return object CRM_Contact_BAO_Contact object 
  * @access public
  * @static
  */
 static function &create(&$params, $fixAddress = true, $invokeHooks = true)
 {
     $contact = null;
     if (!CRM_Utils_Array::value('contact_type', $params) && !CRM_Utils_Array::value('contact_id', $params)) {
         return $contact;
     }
     $isEdit = true;
     if ($invokeHooks) {
         require_once 'CRM/Utils/Hook.php';
         if (CRM_Utils_Array::value('contact_id', $params)) {
             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;
         }
     }
     require_once 'CRM/Core/Transaction.php';
     $transaction = new CRM_Core_Transaction();
     $contact = self::add($params);
     if (!$contact) {
         // CRM_Core_Error::fatal( ts( 'THe contact was not created, not set up to handle error' ) );
     }
     $params['contact_id'] = $contact->id;
     if (defined('CIVICRM_MULTISITE') && CIVICRM_MULTISITE) {
         // in order to make sure that every contact must be added to a group (CRM-4613) -
         require_once 'CRM/Core/BAO/Domain.php';
         $domainGroupID = CRM_Core_BAO_Domain::getGroupId();
         if (CRM_Utils_Array::value('group', $params) && is_array($params['group'])) {
             $grpFlp = array_flip($params['group']);
             if (!array_key_exists(1, $grpFlp)) {
                 $params['group'][$domainGroupID] = 1;
             }
         } else {
             $params['group'] = array($domainGroupID => 1);
         }
     }
     if (array_key_exists('group', $params)) {
         require_once 'CRM/Contact/BAO/GroupContact.php';
         $contactIds = array($params['contact_id']);
         foreach ($params['group'] as $groupId => $flag) {
             if ($flag == 1) {
                 CRM_Contact_BAO_GroupContact::addContactsToGroup($contactIds, $groupId);
             } else {
                 if ($flag == -1) {
                     CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contactIds, $groupId);
                 }
             }
         }
     }
     $config = CRM_Core_Config::singleton();
     if (!$config->doNotResetCache) {
         // Note: doNotResetCache flag is currently set by import contact process, since resetting and
         // rebuilding cache could be expensive (for many contacts). We might come out with better
         // approach in future.
         // clear acl cache if any.
         require_once 'CRM/ACL/BAO/Cache.php';
         CRM_ACL_BAO_Cache::resetCache();
     }
     //add location Block data
     $blocks = CRM_Core_BAO_Location::create($params, $fixAddress);
     foreach ($blocks as $name => $value) {
         $contact->{$name} = $value;
     }
     //get userID from session
     $session =& CRM_Core_Session::singleton();
     $userID = $session->get('userID');
     // add notes
     if (CRM_Utils_Array::value('note', $params)) {
         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' => $note['subject'], '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
     require_once 'CRM/Core/BAO/UFMatch.php';
     CRM_Core_BAO_UFMatch::updateUFName($contact->id);
     if (CRM_Utils_Array::value('custom', $params) && is_array($params['custom'])) {
         require_once 'CRM/Core/BAO/CustomValueTable.php';
         CRM_Core_BAO_CustomValueTable::store($params['custom'], 'civicrm_contact', $contact->id);
     }
     // make a civicrm_subscription_history entry only on contact create (CRM-777)
     if (!CRM_Utils_Array::value('contact_id', $params)) {
         $subscriptionParams = array('contact_id' => $contact->id, 'status' => 'Added', 'method' => 'Admin');
         CRM_Contact_BAO_SubscriptionHistory::create($subscriptionParams);
     }
     $transaction->commit();
     $contact->contact_type_display = $contact->contact_type;
     // reset the group contact cache for this group
     require_once 'CRM/Contact/BAO/GroupContactCache.php';
     CRM_Contact_BAO_GroupContactCache::remove();
     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;
 }
示例#10
0
文件: Edit.php 项目: agloa/tournament
 /**
  * Set default values for the form. LocationType that in edit/view mode
  * the default values are retrieved from the database
  *
  * @return array
  */
 public function setDefaultValues()
 {
     $defaults = array();
     if (empty($defaults['parents'])) {
         $defaults['parents'] = CRM_Core_BAO_Domain::getGroupId();
     }
     // custom data set defaults
     $defaults += CRM_Custom_Form_CustomData::setDefaultValues($this);
     return $defaults;
 }