/** * Re-implement browse. * * We need to do slightly different things for groups vs saved search groups, hence we * re-implement browse from Page_Basic. * * @param int $action */ public function browse($action = NULL) { $groupPermission = CRM_Core_Permission::check('edit groups') ? CRM_Core_Permission::EDIT : CRM_Core_Permission::VIEW; $this->assign('groupPermission', $groupPermission); $showOrgInfo = FALSE; // CRM-9936 $reservedPermission = CRM_Core_Permission::check('administer reserved groups') ? CRM_Core_Permission::EDIT : CRM_Core_Permission::VIEW; $this->assign('reservedPermission', $reservedPermission); if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) { $showOrgInfo = TRUE; } $this->assign('showOrgInfo', $showOrgInfo); // Refresh smart group cache if (!empty($_GET['update_smart_groups'])) { CRM_Contact_BAO_GroupContactCache::loadAll(); } else { CRM_Contact_BAO_GroupContactCache::fillIfEmpty(); } $this->search(); }
/** * This function to get list of groups. * * @param array $params * Associated array for params. * * @return array */ public static function getGroupList(&$params) { $config = CRM_Core_Config::singleton(); $whereClause = self::whereClause($params, FALSE); //$this->pagerAToZ( $whereClause, $params ); if (!empty($params['rowCount']) && $params['rowCount'] > 0) { $limit = " LIMIT {$params['offset']}, {$params['rowCount']} "; } $orderBy = ' ORDER BY groups.title asc'; if (!empty($params['sort'])) { $orderBy = ' ORDER BY ' . CRM_Utils_Type::escape($params['sort'], 'String'); // CRM-16905 - Sort by count cannot be done with sql if (strpos($params['sort'], 'count') === 0) { $orderBy = $limit = ''; } } $select = $from = $where = ""; $groupOrg = FALSE; if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) { $select = ", contact.display_name as org_name, contact.id as org_id"; $from = " LEFT JOIN civicrm_group_organization gOrg\n ON gOrg.group_id = groups.id\n LEFT JOIN civicrm_contact contact\n ON contact.id = gOrg.organization_id "; //get the Organization ID $orgID = CRM_Utils_Request::retrieve('oid', 'Positive', CRM_Core_DAO::$_nullObject); if ($orgID) { $where = " AND gOrg.organization_id = {$orgID}"; } $groupOrg = TRUE; } $query = "\n SELECT groups.*, createdBy.sort_name as created_by {$select}\n FROM civicrm_group groups\n LEFT JOIN civicrm_contact createdBy\n ON createdBy.id = groups.created_id\n {$from}\n WHERE {$whereClause} {$where}\n GROUP BY groups.id\n {$orderBy}\n {$limit}"; $object = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Contact_DAO_Group'); //FIXME CRM-4418, now we are handling delete separately //if we introduce 'delete for group' make sure to handle here. $groupPermissions = array(CRM_Core_Permission::VIEW); if (CRM_Core_Permission::check('edit groups')) { $groupPermissions[] = CRM_Core_Permission::EDIT; $groupPermissions[] = CRM_Core_Permission::DELETE; } // CRM-9936 $reservedPermission = CRM_Core_Permission::check('administer reserved groups'); $links = self::actionLinks(); $allTypes = CRM_Core_OptionGroup::values('group_type'); $values = $groupsToCount = array(); $visibility = CRM_Core_SelectValues::ufVisibility(); while ($object->fetch()) { $permission = CRM_Contact_BAO_Group::checkPermission($object->id, $object->title); //@todo CRM-12209 introduced an ACL check in the whereClause function // it may be that this checking is now obsolete - or that what remains // should be removed to the whereClause (which is also accessed by getCount) if ($permission) { $newLinks = $links; $values[$object->id] = array('class' => array(), 'count' => '0'); CRM_Core_DAO::storeValues($object, $values[$object->id]); // Wrap with crm-editable. Not an ideal solution. if (in_array(CRM_Core_Permission::EDIT, $groupPermissions)) { $values[$object->id]['title'] = '<span class="crm-editable crmf-title">' . $values[$object->id]['title'] . '</span>'; } if ($object->saved_search_id) { $values[$object->id]['title'] .= ' (' . ts('Smart Group') . ')'; // check if custom search, if so fix view link $customSearchID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $object->saved_search_id, 'search_custom_id'); if ($customSearchID) { $newLinks[CRM_Core_Action::VIEW]['url'] = 'civicrm/contact/search/custom'; $newLinks[CRM_Core_Action::VIEW]['qs'] = "reset=1&force=1&ssID={$object->saved_search_id}"; } } $action = array_sum(array_keys($newLinks)); // CRM-9936 if (array_key_exists('is_reserved', $object)) { //if group is reserved and I don't have reserved permission, suppress delete/edit if ($object->is_reserved && !$reservedPermission) { $action -= CRM_Core_Action::DELETE; $action -= CRM_Core_Action::UPDATE; $action -= CRM_Core_Action::DISABLE; } } if (array_key_exists('is_active', $object)) { if ($object->is_active) { $action -= CRM_Core_Action::ENABLE; } else { $values[$object->id]['class'][] = 'disabled'; $action -= CRM_Core_Action::VIEW; $action -= CRM_Core_Action::DISABLE; } } $action = $action & CRM_Core_Action::mask($groupPermissions); $values[$object->id]['visibility'] = $visibility[$values[$object->id]['visibility']]; $groupsToCount[$object->saved_search_id ? 'civicrm_group_contact_cache' : 'civicrm_group_contact'][] = $object->id; if (isset($values[$object->id]['group_type'])) { $groupTypes = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($values[$object->id]['group_type'], 1, -1)); $types = array(); foreach ($groupTypes as $type) { $types[] = CRM_Utils_Array::value($type, $allTypes); } $values[$object->id]['group_type'] = implode(', ', $types); } $values[$object->id]['action'] = CRM_Core_Action::formLink($newLinks, $action, array('id' => $object->id, 'ssid' => $object->saved_search_id), ts('more'), FALSE, 'group.selector.row', 'Group', $object->id); // If group has children, add class for link to view children $values[$object->id]['is_parent'] = FALSE; if (array_key_exists('children', $values[$object->id])) { $values[$object->id]['class'][] = "crm-group-parent"; $values[$object->id]['is_parent'] = TRUE; } // If group is a child, add child class if (array_key_exists('parents', $values[$object->id])) { $values[$object->id]['class'][] = "crm-group-child"; } if ($groupOrg) { if ($object->org_id) { $contactUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$object->org_id}"); $values[$object->id]['org_info'] = "<a href='{$contactUrl}'>{$object->org_name}</a>"; } else { $values[$object->id]['org_info'] = ''; // Empty cell } } else { $values[$object->id]['org_info'] = NULL; // Collapsed column if all cells are NULL } if ($object->created_id) { $contactUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$object->created_id}"); $values[$object->id]['created_by'] = "<a href='{$contactUrl}'>{$object->created_by}</a>"; } } } // Get group counts - executes one query for regular groups and another for smart groups foreach ($groupsToCount as $table => $groups) { $where = "g.group_id IN (" . implode(',', $groups) . ")"; if ($table == 'civicrm_group_contact') { $where .= " AND g.status = 'Added'"; } // Exclude deleted contacts $where .= " and c.id = g.contact_id AND c.is_deleted = 0"; $dao = CRM_Core_DAO::executeQuery("SELECT g.group_id, COUNT(g.id) as `count` FROM {$table} g, civicrm_contact c WHERE {$where} GROUP BY g.group_id"); while ($dao->fetch()) { $values[$dao->group_id]['count'] = $dao->count; } } // CRM-16905 - Sort by count cannot be done with sql if (!empty($params['sort']) && strpos($params['sort'], 'count') === 0) { usort($values, function ($a, $b) { return $a['count'] - $b['count']; }); if (strpos($params['sort'], 'desc')) { $values = array_reverse($values, TRUE); } return array_slice($values, $params['offset'], $params['rowCount']); } return $values; }
/** * Build parent groups form elements. * * @param CRM_Core_Form $form * * @return array * parent groups */ public static function buildParentGroups(&$form) { $groupNames = CRM_Core_PseudoConstant::group(); $parentGroups = $parentGroupElements = array(); if (isset($form->_id) && !empty($form->_groupValues['parents'])) { $parentGroupIds = explode(',', $form->_groupValues['parents']); foreach ($parentGroupIds as $parentGroupId) { $parentGroups[$parentGroupId] = $groupNames[$parentGroupId]; if (array_key_exists($parentGroupId, $groupNames)) { $parentGroupElements[$parentGroupId] = $groupNames[$parentGroupId]; $form->addElement('checkbox', "remove_parent_group_{$parentGroupId}", $groupNames[$parentGroupId]); } } } $form->assign_by_ref('parent_groups', $parentGroupElements); if (isset($form->_id)) { $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache::getPotentialCandidates($form->_id, $groupNames); } else { $potentialParentGroupIds = array_keys($groupNames); } $parentGroupSelectValues = array('' => '- ' . ts('select group') . ' -'); foreach ($potentialParentGroupIds as $potentialParentGroupId) { if (array_key_exists($potentialParentGroupId, $groupNames)) { $parentGroupSelectValues[$potentialParentGroupId] = $groupNames[$potentialParentGroupId]; } } if (count($parentGroupSelectValues) > 1) { if (CRM_Core_Permission::isMultisiteEnabled()) { $required = !isset($form->_id) || $form->_id && CRM_Core_BAO_Domain::isDomainGroup($form->_id) ? FALSE : empty($parentGroups); } else { $required = FALSE; } $form->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required, array('class' => 'crm-select2')); } return $parentGroups; }
/** * Function to actually build the form * * @return None * @access public */ public function buildQuickForm() { if ($this->_action == CRM_Core_Action::DELETE) { $this->addButtons(array(array('type' => 'next', 'name' => ts('Delete Group'), 'isDefault' => TRUE), array('type' => 'cancel', 'name' => ts('Cancel')))); return; } $this->applyFilter('__ALL__', 'trim'); $this->add('text', 'title', ts('Name') . ' ', CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'title'), TRUE); $this->add('textarea', 'description', ts('Description') . ' ', CRM_Core_DAO::getAttribute('CRM_Contact_DAO_Group', 'description')); $groupTypes = CRM_Core_OptionGroup::values('group_type', TRUE); $config = CRM_Core_Config::singleton(); if (isset($this->_id) && CRM_Utils_Array::value('saved_search_id', $this->_groupValues)) { unset($groupTypes['Access Control']); } if (!empty($groupTypes)) { $this->addCheckBox('group_type', ts('Group Type'), $groupTypes, NULL, NULL, NULL, NULL, ' '); } $this->add('select', 'visibility', ts('Visibility'), CRM_Core_SelectValues::ufVisibility(TRUE), TRUE); $groupNames = CRM_Core_PseudoConstant::group(); $parentGroups = $parentGroupElements = array(); if (isset($this->_id) && CRM_Utils_Array::value('parents', $this->_groupValues)) { $parentGroupIds = explode(',', $this->_groupValues['parents']); foreach ($parentGroupIds as $parentGroupId) { $parentGroups[$parentGroupId] = $groupNames[$parentGroupId]; if (array_key_exists($parentGroupId, $groupNames)) { $parentGroupElements[$parentGroupId] = $groupNames[$parentGroupId]; $this->addElement('checkbox', "remove_parent_group_{$parentGroupId}", $groupNames[$parentGroupId]); } } } $this->assign_by_ref('parent_groups', $parentGroupElements); if (isset($this->_id)) { $potentialParentGroupIds = CRM_Contact_BAO_GroupNestingCache::getPotentialCandidates($this->_id, $groupNames); } else { $potentialParentGroupIds = array_keys($groupNames); } $parentGroupSelectValues = array('' => '- ' . ts('select') . ' -'); foreach ($potentialParentGroupIds as $potentialParentGroupId) { if (array_key_exists($potentialParentGroupId, $groupNames)) { $parentGroupSelectValues[$potentialParentGroupId] = $groupNames[$potentialParentGroupId]; } } if (count($parentGroupSelectValues) > 1) { if (CRM_Core_Permission::isMultisiteEnabled()) { $required = empty($parentGroups) ? TRUE : FALSE; $required = $this->_id && CRM_Core_BAO_Domain::isDomainGroup($this->_id) || !isset($this->_id) ? FALSE : $required; } else { $required = FALSE; } $this->add('select', 'parents', ts('Add Parent'), $parentGroupSelectValues, $required); } if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) { //group organization Element $groupOrgDataURL = CRM_Utils_System::url('civicrm/ajax/search', 'org=1', FALSE, NULL, FALSE); $this->assign('groupOrgDataURL', $groupOrgDataURL); $this->addElement('text', 'organization', ts('Organization'), ''); $this->addElement('hidden', 'organization_id', '', array('id' => 'organization_id')); } // is_reserved property CRM-9936 $this->addElement('checkbox', 'is_reserved', ts('Reserved Group?')); if (!CRM_Core_Permission::check('administer reserved groups')) { $this->freeze('is_reserved'); } //build custom data CRM_Custom_Form_CustomData::buildQuickForm($this); $this->addButtons(array(array('type' => 'upload', 'name' => $this->_action == CRM_Core_Action::ADD ? ts('Continue') : ts('Save'), 'isDefault' => TRUE), array('type' => 'cancel', 'name' => ts('Cancel')))); $doParentCheck = FALSE; if (CRM_Core_Permission::isMultisiteEnabled()) { $doParentCheck = $this->_id && CRM_Core_BAO_Domain::isDomainGroup($this->_id) ? FALSE : TRUE; } $options = array('selfObj' => $this, 'parentGroups' => $parentGroups, 'doParentCheck' => $doParentCheck); $this->addFormRule(array('CRM_Group_Form_Edit', 'formRule'), $options); }
/** * This function to get list of groups * * @param array $params associated array for params * @access public */ static function getGroupList(&$params) { $config = CRM_Core_Config::singleton(); $whereClause = self::whereClause($params, FALSE); //$this->pagerAToZ( $whereClause, $params ); if (!empty($params['rowCount']) && $params['rowCount'] > 0) { $limit = " LIMIT {$params['offset']}, {$params['rowCount']} "; } $orderBy = ' ORDER BY groups.title asc'; if (CRM_Utils_Array::value('sort', $params)) { $orderBy = ' ORDER BY ' . CRM_Utils_Array::value('sort', $params); } $select = $from = $where = ""; $groupOrg = FALSE; if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) { $select = ", contact.display_name as org_name, contact.id as org_id"; $from = " LEFT JOIN civicrm_group_organization gOrg\n ON gOrg.group_id = groups.id\n LEFT JOIN civicrm_contact contact\n ON contact.id = gOrg.organization_id "; //get the Organization ID $orgID = CRM_Utils_Request::retrieve('oid', 'Positive', CRM_Core_DAO::$_nullObject); if ($orgID) { $where = " AND gOrg.organization_id = {$orgID}"; } $groupOrg = TRUE; } $query = "\n SELECT groups.* {$select}\n FROM civicrm_group groups\n {$from}\n WHERE {$whereClause} {$where}\n {$orderBy}\n {$limit}"; $object = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Contact_DAO_Group'); //FIXME CRM-4418, now we are handling delete separately //if we introduce 'delete for group' make sure to handle here. $groupPermissions = array(CRM_Core_Permission::VIEW); if (CRM_Core_Permission::check('edit groups')) { $groupPermissions[] = CRM_Core_Permission::EDIT; $groupPermissions[] = CRM_Core_Permission::DELETE; } // CRM-9936 $reservedPermission = CRM_Core_Permission::check('administer reserved groups'); $links = self::actionLinks(); $allTypes = CRM_Core_OptionGroup::values('group_type'); $values = array(); while ($object->fetch()) { $permission = CRM_Contact_BAO_Group::checkPermission($object->id, $object->title); if ($permission) { $newLinks = $links; $values[$object->id] = array(); CRM_Core_DAO::storeValues($object, $values[$object->id]); if ($object->saved_search_id) { $values[$object->id]['title'] .= ' (' . ts('Smart Group') . ')'; // check if custom search, if so fix view link $customSearchID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $object->saved_search_id, 'search_custom_id'); if ($customSearchID) { $newLinks[CRM_Core_Action::VIEW]['url'] = 'civicrm/contact/search/custom'; $newLinks[CRM_Core_Action::VIEW]['qs'] = "reset=1&force=1&ssID={$object->saved_search_id}"; } } $action = array_sum(array_keys($newLinks)); // CRM-9936 if (array_key_exists('is_reserved', $object)) { //if group is reserved and I don't have reserved permission, suppress delete/edit if ($object->is_reserved && !$reservedPermission) { $action -= CRM_Core_Action::DELETE; $action -= CRM_Core_Action::UPDATE; $action -= CRM_Core_Action::DISABLE; } } $values[$object->id]['class'] = ''; if (array_key_exists('is_active', $object)) { if ($object->is_active) { $action -= CRM_Core_Action::ENABLE; } else { $values[$object->id]['class'] = 'disabled'; $action -= CRM_Core_Action::VIEW; $action -= CRM_Core_Action::DISABLE; } } $action = $action & CRM_Core_Action::mask($groupPermissions); $values[$object->id]['visibility'] = CRM_Contact_DAO_Group::tsEnum('visibility', $values[$object->id]['visibility']); if (isset($values[$object->id]['group_type'])) { $groupTypes = explode(CRM_Core_DAO::VALUE_SEPARATOR, substr($values[$object->id]['group_type'], 1, -1)); $types = array(); foreach ($groupTypes as $type) { $types[] = CRM_Utils_Array::value($type, $allTypes); } $values[$object->id]['group_type'] = implode(', ', $types); } $values[$object->id]['action'] = CRM_Core_Action::formLink($newLinks, $action, array('id' => $object->id, 'ssid' => $object->saved_search_id)); if ($groupOrg) { if ($object->org_id) { $contactUrl = CRM_Utils_System::url('civicrm/contact/view', "reset=1&cid={$object->org_id}"); $values[$object->id]['org_info'] = "<a href='{$contactUrl}'>{$object->org_name}</a>"; } else { $values[$object->id]['org_info'] = ''; // Empty cell } } else { $values[$object->id]['org_info'] = NULL; // Collapsed column if all cells are NULL } } } return $values; }
/** * We need to do slightly different things for groups vs saved search groups, hence we * reimplement browse from Page_Basic * * @param int $action * * @return void * @access public */ function browse($action = NULL) { $groupPermission = CRM_Core_Permission::check('edit groups') ? CRM_Core_Permission::EDIT : CRM_Core_Permission::VIEW; $this->assign('groupPermission', $groupPermission); $showOrgInfo = FALSE; // CRM-9936 $reservedPermission = CRM_Core_Permission::check('administer reserved groups') ? CRM_Core_Permission::EDIT : CRM_Core_Permission::VIEW; $this->assign('reservedPermission', $reservedPermission); if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) { $showOrgInfo = TRUE; } $this->assign('showOrgInfo', $showOrgInfo); $this->search(); }
/** * Re-implement browse. * * We need to do slightly different things for groups vs saved search groups, hence we * re-implement browse from Page_Basic. * * @param int $action */ public function browse($action = NULL) { $groupPermission = CRM_Core_Permission::check('edit groups') ? CRM_Core_Permission::EDIT : CRM_Core_Permission::VIEW; $this->assign('groupPermission', $groupPermission); $showOrgInfo = FALSE; // CRM-9936 $reservedPermission = CRM_Core_Permission::check('administer reserved groups') ? CRM_Core_Permission::EDIT : CRM_Core_Permission::VIEW; $this->assign('reservedPermission', $reservedPermission); if (CRM_Core_Permission::check('administer Multiple Organizations') && CRM_Core_Permission::isMultisiteEnabled()) { $showOrgInfo = TRUE; } $this->assign('showOrgInfo', $showOrgInfo); // Refresh smart group cache if (!empty($_GET['update_smart_groups'])) { CRM_Contact_BAO_GroupContactCache::loadAll(); } elseif (!CRM_Core_DAO::singleValueQuery("SELECT id FROM civicrm_group_contact_cache LIMIT 1")) { CRM_Core_Session::setStatus(ts('Count data for smart groups is not currently calculated. You may click Update Smart Groups to generate it. Be aware this can cause significant server load')); } $this->search(); }