/**
  * This method populates the civicrm_group_contact table
  */
 private function addGroup()
 {
     // add the 3 groups first
     foreach ($this->sampleData['group'] as $groupName) {
         $group = new CRM_Contact_BAO_Group();
         $group->name = $group->title = $groupName;
         $group->group_type = "12";
         $group->visibility = 'Public Pages';
         $group->is_active = 1;
         $group->save();
         $group->buildClause();
         $group->save();
     }
     // 60 are for newsletter
     for ($i = 0; $i < 60; $i++) {
         $groupContact = new CRM_Contact_DAO_GroupContact();
         // newsletter subscribers
         $groupContact->group_id = 2;
         $groupContact->contact_id = $this->Individual[$i];
         // always add members
         $groupContact->status = 'Added';
         $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
         $subscriptionHistory->contact_id = $groupContact->contact_id;
         $subscriptionHistory->group_id = $groupContact->group_id;
         $subscriptionHistory->status = $groupContact->status;
         // method
         $subscriptionHistory->method = $this->randomItem($this->subscriptionHistoryMethod);
         $subscriptionHistory->date = $this->randomDate();
         if ($groupContact->status != 'Pending') {
             $this->_insert($groupContact);
         }
         $this->_insert($subscriptionHistory);
     }
     // 15 volunteers
     for ($i = 0; $i < 15; $i++) {
         $groupContact = new CRM_Contact_DAO_GroupContact();
         // Volunteers
         $groupContact->group_id = 3;
         $groupContact->contact_id = $this->Individual[$i + 60];
         // membership status
         $groupContact->status = 'Added';
         $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
         $subscriptionHistory->contact_id = $groupContact->contact_id;
         $subscriptionHistory->group_id = $groupContact->group_id;
         $subscriptionHistory->status = $groupContact->status;
         // method
         $subscriptionHistory->method = $this->randomItem($this->subscriptionHistoryMethod);
         $subscriptionHistory->date = $this->randomDate();
         if ($groupContact->status != 'Pending') {
             $this->_insert($groupContact);
         }
         $this->_insert($subscriptionHistory);
     }
     // 8 advisory board group
     for ($i = 0; $i < 8; $i++) {
         $groupContact = new CRM_Contact_DAO_GroupContact();
         // advisory board group
         $groupContact->group_id = 4;
         $groupContact->contact_id = $this->Individual[$i * 7];
         // membership status
         $groupContact->status = 'Added';
         $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
         $subscriptionHistory->contact_id = $groupContact->contact_id;
         $subscriptionHistory->group_id = $groupContact->group_id;
         $subscriptionHistory->status = $groupContact->status;
         // method
         $subscriptionHistory->method = $this->randomItem($this->subscriptionHistoryMethod);
         $subscriptionHistory->date = $this->randomDate();
         if ($groupContact->status != 'Pending') {
             $this->_insert($groupContact);
         }
         $this->_insert($subscriptionHistory);
     }
     //In this function when we add groups that time we are cache the contact fields
     //But at the end of setup we are appending sample custom data, so for consistency
     //reset the cache.
     CRM_Core_BAO_Cache::deleteGroup('contact fields');
 }
Example #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;
 }
/**
 *
 * @package CRM
 * @copyright CiviCRM LLC (c) 2004-2016
 * $Id$
 *
 */
require_once '../civicrm.config.php';
require_once 'CRM/Core/Config.php';
require_once 'CRM/Core/Error.php';
require_once 'CRM/Core/I18n.php';
require_once 'CRM/Contact/BAO/Group.php';
$config = CRM_Core_Config::singleton();
$prefix = 'Automated Generated Group: ';
$query = "DELETE FROM civicrm_group where name like '%{$prefix}%'";
CRM_Core_DAO::executeQuery($query, CRM_Core_DAO::$_nullArray);
$numGroups = 100;
$visibility = array('User and User Admin Only', 'Public Pages');
$groupType = array(NULL, '1', '2', '12');
for ($i = 1; $i <= $numGroups; $i++) {
    $group = new CRM_Contact_BAO_Group();
    $cnt = sprintf('%05d', $i);
    $alphabet = mt_rand(97, 122);
    $group->name = $group->title = chr($alphabet) . ": {$prefix} {$cnt}";
    $group->is_active = 1;
    $v = mt_rand(0, 1);
    $group->visibility = $visibility[$v];
    $t = mt_rand(0, 3);
    $group->group_type = $groupType[$t];
    $group->save();
}
 public function addGroup()
 {
     // add the 3 groups first
     $numGroup = count($this->group);
     require_once 'CRM/Contact/BAO/Group.php';
     for ($i = 0; $i < $numGroup; $i++) {
         $group = new CRM_Contact_BAO_Group();
         $group->name = $this->group[$i];
         $group->title = $this->group[$i];
         $group->group_type = "12";
         $group->visibility = 'Public Pages';
         $group->is_active = 1;
         $group->save();
         $group->buildClause();
         $group->save();
     }
     // 60 are for newsletter
     for ($i = 0; $i < 60; $i++) {
         $groupContact = new CRM_Contact_DAO_GroupContact();
         // newsletter subscribers
         $groupContact->group_id = 2;
         $groupContact->contact_id = $this->individual[$i];
         // membership status
         $groupContact->status = $this->_getRandomElement($this->groupMembershipStatus);
         $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
         $subscriptionHistory->contact_id = $groupContact->contact_id;
         $subscriptionHistory->group_id = $groupContact->group_id;
         $subscriptionHistory->status = $groupContact->status;
         // method
         $subscriptionHistory->method = $this->_getRandomElement($this->subscriptionHistoryMethod);
         $subscriptionHistory->date = $this->_getRandomDate();
         if ($groupContact->status != 'Pending') {
             $this->_insert($groupContact);
         }
         $this->_insert($subscriptionHistory);
     }
     // 15 volunteers
     for ($i = 0; $i < 15; $i++) {
         $groupContact = new CRM_Contact_DAO_GroupContact();
         // Volunteers
         $groupContact->group_id = 3;
         $groupContact->contact_id = $this->individual[$i + 60];
         // membership status
         $groupContact->status = $this->_getRandomElement($this->groupMembershipStatus);
         $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
         $subscriptionHistory->contact_id = $groupContact->contact_id;
         $subscriptionHistory->group_id = $groupContact->group_id;
         $subscriptionHistory->status = $groupContact->status;
         // method
         $subscriptionHistory->method = $this->_getRandomElement($this->subscriptionHistoryMethod);
         $subscriptionHistory->date = $this->_getRandomDate();
         if ($groupContact->status != 'Pending') {
             $this->_insert($groupContact);
         }
         $this->_insert($subscriptionHistory);
     }
     // 8 advisory board group
     for ($i = 0; $i < 8; $i++) {
         $groupContact = new CRM_Contact_DAO_GroupContact();
         // advisory board group
         $groupContact->group_id = 4;
         $groupContact->contact_id = $this->individual[$i * 7];
         // membership status
         $groupContact->status = $this->_getRandomElement($this->groupMembershipStatus);
         $subscriptionHistory = new CRM_Contact_DAO_SubscriptionHistory();
         $subscriptionHistory->contact_id = $groupContact->contact_id;
         $subscriptionHistory->group_id = $groupContact->group_id;
         $subscriptionHistory->status = $groupContact->status;
         // method
         $subscriptionHistory->method = $this->_getRandomElement($this->subscriptionHistoryMethod);
         $subscriptionHistory->date = $this->_getRandomDate();
         if ($groupContact->status != 'Pending') {
             $this->_insert($groupContact);
         }
         $this->_insert($subscriptionHistory);
     }
     //In this function when we add groups that time we are cache the contact fields
     //But at the end of setup we are appending sample custom data, so for consistency
     //reset the cache.
     require_once 'CRM/Core/BAO/Cache.php';
     CRM_Core_BAO_Cache::deleteGroup('contact fields');
 }
 /**
  * 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;
 }