static function details($csID, $ssID = null, $gID = null) { $error = array(null, null, null); if (!$csID && !$ssID && !$gID) { return $error; } $customSearchID = $csID; $formValues = array(); if ($ssID || $gID) { if ($gID) { $ssID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $gID, 'saved_search_id'); } $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($ssID); $customSearchID = CRM_Utils_Array::value('customSearchID', $formValues); } if (!$customSearchID) { return $error; } // check that the csid exists in the db along with the right file // and implements the right interface require_once 'CRM/Core/OptionGroup.php'; $customSearchClass = CRM_Core_OptionGroup::getLabel('custom_search', $customSearchID); if (!$customSearchClass) { return $error; } $customSearchFile = str_replace('_', DIRECTORY_SEPARATOR, $customSearchClass) . '.php'; $error = (include_once $customSearchFile); if ($error == false) { CRM_Core_Error::fatal('Custom search file: ' . $customSearchFile . ' does not exist. Please verify your custom search settings in CiviCRM administrative panel.'); } return array($customSearchID, $customSearchClass, $formValues); }
/** * Test fixValues function. * * @dataProvider getSavedSearches */ public function testGetFormValues($formValues, $expectedResult, $searchDescription) { CRM_Core_DAO::executeQuery("INSERT INTO civicrm_saved_search (form_values) VALUES('" . serialize($formValues) . "')"); $result = CRM_Contact_BAO_SavedSearch::getFormValues(CRM_Core_DAO::singleValueQuery('SELECT LAST_INSERT_ID()')); $this->assertEquals(array('membership_type_id', 'membership_status_id'), array_keys($result)); foreach ($result as $key => $value) { $this->assertEquals($expectedResult, $value, 'failure on set ' . $searchDescription); } }
/** * @param int $csID * @param int $ssID * @param int $gID * * @return array * @throws Exception */ public static function details($csID, $ssID = NULL, $gID = NULL) { $error = array(NULL, NULL, NULL); if (!$csID && !$ssID && !$gID) { return $error; } $customSearchID = $csID; $formValues = array(); if ($ssID || $gID) { if ($gID) { $ssID = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $gID, 'saved_search_id'); } $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($ssID); $customSearchID = CRM_Utils_Array::value('customSearchID', $formValues); } if (!$customSearchID) { return $error; } // check that the csid exists in the db along with the right file // and implements the right interface $customSearchClass = CRM_Core_OptionGroup::getLabel('custom_search', $customSearchID); if (!$customSearchClass) { return $error; } $ext = CRM_Extension_System::singleton()->getMapper(); if (!$ext->isExtensionKey($customSearchClass)) { $customSearchFile = str_replace('_', DIRECTORY_SEPARATOR, $customSearchClass) . '.php'; } else { $customSearchFile = $ext->keyToPath($customSearchClass); $customSearchClass = $ext->keyToClass($customSearchClass); } $error = (include_once $customSearchFile); if ($error == FALSE) { CRM_Core_Error::fatal('Custom search file: ' . $customSearchFile . ' does not exist. Please verify your custom search settings in CiviCRM administrative panel.'); } return array($customSearchID, $customSearchClass, $formValues); }
/** * The post processing of the form gets done here. * * Key things done during post processing are * - check for reset or next request. if present, skip post procesing. * - now check if user requested running a saved search, if so, then * the form values associated with the saved search are used for searching. * - if user has done a submit with new values the regular post submissing is * done. * * The processing consists of using a Selector / Controller framework for getting the * search results. */ public function postProcess() { if ($this->_done) { return; } $this->_done = TRUE; if (!empty($_POST)) { $this->_formValues = $this->controller->exportValues($this->_name); foreach (array('activity_type_id', 'status_id', 'activity_subject') as $element) { $value = CRM_Utils_Array::value($element, $this->_formValues); if ($value) { if (is_array($value)) { if ($element == 'status_id') { unset($this->_formValues[$element]); $this->_formValues['activity_' . $element] = $value; } } else { $this->_formValues[$element] = array('LIKE' => "%{$value}%"); } } } } $this->fixFormValues(); if (isset($this->_ssID) && empty($_POST)) { // if we are editing / running a saved search and the form has not been posted $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); } // We don't show test records in summaries or dashboards if (empty($this->_formValues['activity_test']) && $this->_force) { $this->_formValues["activity_test"] = 0; } CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $this->set('formValues', $this->_formValues); $this->set('queryParams', $this->_queryParams); $buttonName = $this->controller->getButtonName(); if ($buttonName == $this->_actionButtonName) { // check actionName and if next, then do not repeat a search, since we are going to the next page // hack, make sure we reset the task values $stateMachine = $this->controller->getStateMachine(); $formName = $stateMachine->getTaskFormName(); $this->controller->resetPage($formName); return; } $sortID = NULL; if ($this->get(CRM_Utils_Sort::SORT_ID)) { $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), $this->get(CRM_Utils_Sort::SORT_DIRECTION)); } $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $selector = new CRM_Activity_Selector_Search($this->_queryParams, $this->_action, NULL, $this->_single, $this->_limit, $this->_context); $selector->setKey($this->controller->_key); $prefix = NULL; if ($this->_context == 'basic' || $this->_context == 'user') { $prefix = $this->_prefix; } $controller = new CRM_Core_Selector_Controller($selector, $this->get(CRM_Utils_Pager::PAGE_ID), $sortID, CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::SESSION, $prefix); $controller->setEmbedded(TRUE); $query =& $selector->getQuery(); if ($this->_context == 'user') { $query->setSkipPermission(TRUE); } $controller->run(); }
function from() { //define table name $randomNum = md5(uniqid()); $this->_tableName = "civicrm_temp_custom_{$randomNum}"; //block for Group search $smartGroup = array(); if ($this->_groups || $this->_allSearch) { require_once 'CRM/Contact/DAO/Group.php'; $group = new CRM_Contact_DAO_Group(); $group->is_active = 1; $group->find(); while ($group->fetch()) { $allGroups[] = $group->id; if ($group->saved_search_id) { $smartGroup[$group->saved_search_id] = $group->id; } } $includedGroups = implode(',', $allGroups); if (!empty($this->_includeGroups)) { $iGroups = implode(',', $this->_includeGroups); } else { //if no group selected search for all groups $iGroups = null; } if (is_array($this->_excludeGroups)) { $xGroups = implode(',', $this->_excludeGroups); } else { $xGroups = 0; } $sql = "CREATE TEMPORARY TABLE Xg_{$this->_tableName} ( contact_id int primary key) ENGINE=HEAP"; CRM_Core_DAO::executeQuery($sql); //used only when exclude group is selected if ($xGroups != 0) { $excludeGroup = "INSERT INTO Xg_{$this->_tableName} ( contact_id )\n SELECT DISTINCT civicrm_group_contact.contact_id\n FROM civicrm_group_contact, civicrm_contact \n WHERE \n civicrm_contact.id = civicrm_group_contact.contact_id AND \n civicrm_group_contact.status = 'Added' AND\n civicrm_group_contact.group_id IN( {$xGroups})"; CRM_Core_DAO::executeQuery($excludeGroup); //search for smart group contacts foreach ($this->_excludeGroups as $keys => $values) { if (in_array($values, $smartGroup)) { $ssId = CRM_Utils_Array::key($values, $smartGroup); $smartSql = CRM_Contact_BAO_SavedSearch::contactIDsSQL($ssId); $smartSql = $smartSql . " AND contact_a.id NOT IN ( \n SELECT contact_id FROM civicrm_group_contact \n WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')"; $smartGroupQuery = " INSERT IGNORE INTO Xg_{$this->_tableName}(contact_id) {$smartSql}"; CRM_Core_DAO::executeQuery($smartGroupQuery); } } } $sql = "CREATE TEMPORARY TABLE Ig_{$this->_tableName} ( id int PRIMARY KEY AUTO_INCREMENT,\n contact_id int,\n group_names varchar(64)) ENGINE=HEAP"; CRM_Core_DAO::executeQuery($sql); if ($iGroups) { $includeGroup = "INSERT INTO Ig_{$this->_tableName} (contact_id, group_names)\n SELECT civicrm_contact.id as contact_id, civicrm_group.title as group_name\n FROM civicrm_contact\n INNER JOIN civicrm_group_contact\n ON civicrm_group_contact.contact_id = civicrm_contact.id\n LEFT JOIN civicrm_group\n ON civicrm_group_contact.group_id = civicrm_group.id"; } else { $includeGroup = "INSERT INTO Ig_{$this->_tableName} (contact_id, group_names)\n SELECT civicrm_contact.id as contact_id, ''\n FROM civicrm_contact"; } //used only when exclude group is selected if ($xGroups != 0) { $includeGroup .= " LEFT JOIN Xg_{$this->_tableName}\n ON civicrm_contact.id = Xg_{$this->_tableName}.contact_id"; } if ($iGroups) { $includeGroup .= " WHERE \n civicrm_group_contact.status = 'Added' AND\n civicrm_group_contact.group_id IN({$iGroups})"; } else { $includeGroup .= " WHERE ( 1 ) "; } //used only when exclude group is selected if ($xGroups != 0) { $includeGroup .= " AND Xg_{$this->_tableName}.contact_id IS null"; } CRM_Core_DAO::executeQuery($includeGroup); //search for smart group contacts foreach ($this->_includeGroups as $keys => $values) { if (in_array($values, $smartGroup)) { $ssId = CRM_Utils_Array::key($values, $smartGroup); $smartSql = CRM_Contact_BAO_SavedSearch::contactIDsSQL($ssId); $smartSql .= " AND contact_a.id NOT IN ( \n SELECT contact_id FROM civicrm_group_contact\n WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')"; //used only when exclude group is selected if ($xGroups != 0) { $smartSql .= " AND contact_a.id NOT IN (SELECT contact_id FROM Xg_{$this->_tableName})"; } $smartGroupQuery = " INSERT IGNORE INTO Ig_{$this->_tableName}(contact_id) \n {$smartSql}"; CRM_Core_DAO::executeQuery($smartGroupQuery); $insertGroupNameQuery = "UPDATE IGNORE Ig_{$this->_tableName}\n SET group_names = (SELECT title FROM civicrm_group\n WHERE civicrm_group.id = {$values})\n WHERE Ig_{$this->_tableName}.contact_id IS NOT NULL \n AND Ig_{$this->_tableName}.group_names IS NULL"; CRM_Core_DAO::executeQuery($insertGroupNameQuery); } } } //group contact search end here; //block for Tags search if ($this->_tags || $this->_allSearch) { //find all tags require_once 'CRM/Core/DAO/Tag.php'; $tag = new CRM_Core_DAO_Tag(); $tag->is_active = 1; $tag->find(); while ($tag->fetch()) { $allTags[] = $tag->id; } $includedTags = implode(',', $allTags); if (!empty($this->_includeTags)) { $iTags = implode(',', $this->_includeTags); } else { //if no group selected search for all groups $iTags = null; } if (is_array($this->_excludeTags)) { $xTags = implode(',', $this->_excludeTags); } else { $xTags = 0; } $sql = "CREATE TEMPORARY TABLE Xt_{$this->_tableName} ( contact_id int primary key) ENGINE=HEAP"; CRM_Core_DAO::executeQuery($sql); //used only when exclude tag is selected if ($xTags != 0) { $excludeTag = "INSERT INTO Xt_{$this->_tableName} ( contact_id )\n SELECT DISTINCT civicrm_entity_tag.entity_id\n FROM civicrm_entity_tag, civicrm_contact \n WHERE \n civicrm_entity_tag.entity_table = 'civicrm_contact' AND\n civicrm_contact.id = civicrm_entity_tag.entity_id AND \n civicrm_entity_tag.tag_id IN( {$xTags})"; CRM_Core_DAO::executeQuery($excludeTag); } $sql = "CREATE TEMPORARY TABLE It_{$this->_tableName} ( id int PRIMARY KEY AUTO_INCREMENT,\n contact_id int,\n tag_names varchar(64)) ENGINE=HEAP"; CRM_Core_DAO::executeQuery($sql); if ($iTags) { $includeTag = "INSERT INTO It_{$this->_tableName} (contact_id, tag_names)\n SELECT civicrm_contact.id as contact_id, civicrm_tag.name as tag_name\n FROM civicrm_contact\n INNER JOIN civicrm_entity_tag\n ON ( civicrm_entity_tag.entity_table = 'civicrm_contact' AND\n civicrm_entity_tag.entity_id = civicrm_contact.id )\n LEFT JOIN civicrm_tag\n ON civicrm_entity_tag.tag_id = civicrm_tag.id"; } else { $includeTag = "INSERT INTO It_{$this->_tableName} (contact_id, tag_names)\n SELECT civicrm_contact.id as contact_id, ''\n FROM civicrm_contact"; } //used only when exclude tag is selected if ($xTags != 0) { $includeTag .= " LEFT JOIN Xt_{$this->_tableName}\n ON civicrm_contact.id = Xt_{$this->_tableName}.contact_id"; } if ($iTags) { $includeTag .= " WHERE civicrm_entity_tag.tag_id IN({$iTags})"; } else { $includeTag .= " WHERE ( 1 ) "; } //used only when exclude tag is selected if ($xTags != 0) { $includeTag .= " AND Xt_{$this->_tableName}.contact_id IS null"; } CRM_Core_DAO::executeQuery($includeTag); } $from = " FROM civicrm_contact contact_a"; /* * check the situation and set booleans */ if ($iGroups != 0) { $iG = true; } else { $iG = false; } if ($iTags != 0) { $iT = true; } else { $iT = false; } if ($xGroups != 0) { $xG = true; } else { $xG = false; } if ($xTags != 0) { $xT = true; } else { $xT = false; } if (!$this->_groups || !$this->_tags) { $this->_andOr = 1; } /* * Set from statement depending on array sel */ if ($iG && $iT && $xG && $xT) { if ($this->_andOr == 1) { $from .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " INNER JOIN It_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL )\n AND contact_a.id NOT IN(SELECT contact_id FROM Xg_{$this->_tableName})\n AND contact_a.id NOT IN(SELECT contact_id FROM Xt_{$this->_tableName})"; } else { $from .= " LEFT JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " LEFT JOIN It_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $from .= " LEFT JOIN Xg_{$this->_tableName} temptable3 ON (contact_a.id = temptable3.contact_id)"; $from .= " LEFT JOIN Xt_{$this->_tableName} temptable4 ON (contact_a.id = temptable4.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL OR\n temptable3.contact_id IS NOT NULL OR temptable4.contact_id IS NOT NULL)"; } } if ($iG && $iT && $xG && !$xT) { if ($this->_andOr == 1) { $from .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " INNER JOIN It_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL )\n AND contact_a.id NOT IN(SELECT contact_id FROM Xg_{$this->_tableName})"; } else { $from .= " LEFT JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " LEFT JOIN It_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $from .= " LEFT JOIN Xg_{$this->_tableName} temptable3 ON (contact_a.id = temptable3.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL OR\n temptable3.contact_id IS NOT NULL)"; } } if ($iG && $iT && !$xG && $xT) { if ($this->_andOr == 1) { $from .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " INNER JOIN It_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL )\n AND contact_a.id NOT IN(SELECT contact_id FROM Xt_{$this->_tableName})"; } else { $from .= " LEFT JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " LEFT JOIN It_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $from .= " LEFT JOIN Xt_{$this->_tableName} temptable3 ON (contact_a.id = temptable3.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL OR\n temptable3.contact_id IS NOT NULL)"; } } if ($iG && $iT && !$xG && !$xT) { if ($this->_andOr == 1) { $from .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " INNER JOIN It_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL )"; } else { $from .= " LEFT JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " LEFT JOIN It_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL)"; } } if ($iG && !$iT && $xG && $xT) { if ($this->_andOr == 1) { $from .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL) AND contact_a.id NOT IN(\n SELECT contact_id FROM Xg_{$this->_tableName}) AND contact_a.id NOT IN(\n SELECT contact_id FROM Xt_{$this->_tableName})"; } else { $from .= " LEFT JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " LEFT JOIN Xg_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $from .= " LEFT JOIN Xt_{$this->_tableName} temptable3 ON (contact_a.id = temptable3.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL OR\n temptable3.contact_id IS NOT NULL)"; } } if ($iG && !$iT && $xG && !$xT) { if ($this->_andOr == 1) { $from .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL) AND contact_a.id NOT IN(\n SELECT contact_id FROM Xg_{$this->_tableName})"; } else { $from .= " LEFT JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " LEFT JOIN Xg_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL)"; } } if ($iG && !$iT && !$xG && $xT) { if ($this->_andOr == 1) { $from .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL) AND contact_a.id NOT IN(\n SELECT contact_id FROM Xt_{$this->_tableName})"; } else { $from .= " LEFT JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " LEFT JOIN Xt_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL)"; } } if ($iG && !$iT && !$xG && !$xT) { if ($this->_andOr == 1) { $from .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL)"; } else { $from .= " LEFT JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL)"; } } if (!$iG && $iT && $xG && $xT) { if ($this->_andOr == 1) { $from .= " INNER JOIN It_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL) AND contact_a.id NOT IN(\n SELECT contact_id FROM Xg_{$this->_tableName}) AND contact_a.id NOT IN(\n SELECT contact_id FROM Xt_{$this->_tableName})"; } else { $from .= " LEFT JOIN It_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " LEFT JOIN Xg_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $from .= " LEFT JOIN Xt_{$this->_tableName} temptable3 ON (contact_a.id = temptable3.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL OR\n temptable3.contact_id IS NOT NULL)"; } } if (!$iG && $iT && $xG && !$xT) { if ($this->_andOr == 1) { $from .= " INNER JOIN It_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL) AND contact_a.id NOT IN(\n SELECT contact_id FROM Xg_{$this->_tableName})"; } else { $from .= " LEFT JOIN It_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " LEFT JOIN Xg_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL)"; } } if (!$iG && $iT && !$xG && $xT) { if ($this->_andOr == 1) { $from .= " INNER JOIN It_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL) AND contact_a.id NOT IN(\n SELECT contact_id FROM Xt_{$this->_tableName})"; } else { $from .= " LEFT JOIN It_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " LEFT JOIN Xt_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL)"; } } if (!$iG && $iT && !$xG && !$xT) { if ($this->_andOr == 1) { $from .= " INNER JOIN It_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL)"; } else { $from .= " LEFT JOIN It_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL)"; } } if (!$iG && !$iT && $xG && $xT) { if ($this->_andOr == 1) { $this->_where = "contact_a.id NOT IN(SELECT contact_id FROM Xg_{$this->_tableName})\n AND contact_a.id NOT IN(SELECT contact_id FROM Xt_{$this->_tableName})"; } else { $from .= " LEFT JOIN Xg_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " LEFT JOIN Xt_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL)"; } } if (!$iG && !$iT && !$xG && $xT) { if ($this->_andOr == 1) { $from .= " INNER JOIN It_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $this->_where = "contact_a.id NOT IN(SELECT contact_id FROM Xt_{$this->_tableName})"; } else { $from .= " LEFT JOIN Xt_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " LEFT JOIN It_{$this->_tableName} temptable2 ON (contact_a.id = temptable2.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL OR temptable2.contact_id IS NOT NULL)"; } } if (!$iG && !$iT && $xG && !$xT) { if ($this->_andOr == 1) { $from .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $this->_where = "contact_a.id NOT IN(SELECT contact_id FROM Xg_{$this->_tableName})"; } else { $from .= " LEFT JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $from .= " LEFT JOIN Xg_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; $this->_where = "( temptable1.contact_id IS NOT NULL)"; } } $from .= " LEFT JOIN civicrm_email ON ( contact_a.id = civicrm_email.contact_id AND ( civicrm_email.is_primary = 1 OR civicrm_email.is_bulkmail = 1 ) )"; return $from; }
/** * process the form after the input has been submitted and validated * * @access public * * @return void */ public function postProcess() { // saved search form values $formValues = $this->controller->exportValues(); $session = CRM_Core_Session::singleton(); //save the search $savedSearch = new CRM_Contact_BAO_SavedSearch(); $savedSearch->id = $this->_id; $savedSearch->form_values = serialize($this->get('formValues')); $savedSearch->mapping_id = $mappingId; $savedSearch->save(); $this->set('ssID', $savedSearch->id); CRM_Core_Session::setStatus(ts("Your smart group has been saved as '%1'.", array(1 => $formValues['title'])), ts('Saved'), 'success'); // also create a group that is associated with this saved search only if new saved search $params = array(); $params['title'] = $formValues['title']; $params['description'] = $formValues['description']; $params['visibility'] = 'User and User Admin Only'; $params['saved_search_id'] = $savedSearch->id; $params['is_active'] = 1; if ($this->_id) { $params['id'] = CRM_Contact_BAO_SavedSearch::getName($this->_id, 'id'); } $group = CRM_Contact_BAO_Group::create($params); }
/** * Change saved search. * * @param \CRM_Queue_TaskContext $ctx * * @return bool */ public static function changeSavedSearch(CRM_Queue_TaskContext $ctx) { $membershipStatuses = array_flip(CRM_Member_PseudoConstant::membershipStatus()); $dao = new CRM_Contact_DAO_SavedSearch(); $dao->find(); while ($dao->fetch()) { $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($dao->id); if (!empty($formValues['mapper'])) { foreach ($formValues['mapper'] as $key => $value) { foreach ($value as $k => $v) { if ($v[0] == 'Membership' && in_array($v[1], array('membership_status', 'membership_status_id'))) { $value = $formValues['value'][$key][$k]; $op = $formValues['operator'][$key][$k]; if ($op == 'IN') { $value = trim($value); $value = str_replace('(', '', $value); $value = str_replace(')', '', $value); $v = explode(',', $value); $value = array(); foreach ($v as $k1 => $v2) { if (is_numeric($v2)) { break 2; } $value[$k1] = $membershipStatuses[$v2]; } $formValues['value'][$key][$k] = "(" . implode(',', $value) . ")"; } elseif (in_array($op, array('=', '!='))) { if (is_numeric($value)) { break; } $formValues['value'][$key][$k] = $membershipStatuses[$value]; } } } } $dao->form_values = serialize($formValues); $dao->save(); } } return TRUE; }
/** * The post processing of the form gets done here. * * Key things done during post processing are * - check for reset or next request. if present, skip post procesing. * - now check if user requested running a saved search, if so, then * the form values associated with the saved search are used for searching. * - if user has done a submit with new values the regular post submissing is * done. * The processing consists of using a Selector / Controller framework for getting the * search results. * * @param * * @return void * @access public */ function postProcess() { if ($this->_done) { return; } $this->_done = true; $this->_formValues = $this->controller->exportValues($this->_name); $this->fixFormValues(); if (isset($this->_ssID) && empty($_POST)) { // if we are editing / running a saved search and the form has not been posted $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); } require_once 'CRM/Contact/BAO/Query.php'; $this->_queryParams =& CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $this->set('formValues', $this->_formValues); $this->set('queryParams', $this->_queryParams); $buttonName = $this->controller->getButtonName(); if ($buttonName == $this->_actionButtonName || $buttonName == $this->_printButtonName) { // check actionName and if next, then do not repeat a search, since we are going to the next page // hack, make sure we reset the task values $stateMachine =& $this->controller->getStateMachine(); $formName = $stateMachine->getTaskFormName(); $this->controller->resetPage($formName); return; } $sortID = null; if ($this->get(CRM_Utils_Sort::SORT_ID)) { $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), $this->get(CRM_Utils_Sort::SORT_DIRECTION)); } require_once 'CRM/Contact/BAO/Query.php'; $selector =& new CRM_Grant_Selector_Search($this->_queryParams, $this->_action, null, $this->_single, $this->_limit, $this->_context); $prefix = null; if ($this->_context == 'basic' || $this->_context == 'user') { $prefix = $this->_prefix; } $controller =& new CRM_Core_Selector_Controller($selector, $this->get(CRM_Utils_Pager::PAGE_ID), $sortID, CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::SESSION, $prefix); $controller->setEmbedded(true); $query =& $selector->getQuery(); if ($this->_context == 'user') { $query->setSkipPermission(true); } $controller->run(); }
/** * Processing needed for buildForm and later. */ public function preProcess() { // set the various class variables $this->_group = CRM_Core_PseudoConstant::group(); $this->_groupIterator = CRM_Core_PseudoConstant::groupIterator(); $this->_tag = CRM_Core_BAO_Tag::getTags(); $this->_done = FALSE; /* * we allow the controller to set force/reset externally, useful when we are being * driven by the wizard framework */ $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject); $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', CRM_Core_DAO::$_nullObject); $this->_groupID = CRM_Utils_Request::retrieve('gid', 'Positive', $this); $this->_amtgID = CRM_Utils_Request::retrieve('amtgID', 'Positive', $this); $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this); $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this); $this->_ufGroupID = CRM_Utils_Request::retrieve('id', 'Positive', $this); $this->_componentMode = CRM_Utils_Request::retrieve('component_mode', 'Positive', $this, FALSE, 1, $_REQUEST); $this->_operator = CRM_Utils_Request::retrieve('operator', 'String', $this, FALSE, 1, $_REQUEST, 'AND'); /** * set the button names */ $this->_searchButtonName = $this->getButtonName('refresh'); $this->_actionButtonName = $this->getButtonName('next', 'action'); $this->assign('actionButtonName', $this->_actionButtonName); // reset from session, CRM-3526 $session = CRM_Core_Session::singleton(); if ($this->_force && $session->get('selectedSearchContactIds')) { $session->resetScope('selectedSearchContactIds'); } // if we dont get this from the url, use default if one exsts $config = CRM_Core_Config::singleton(); if ($this->_ufGroupID == NULL && $config->defaultSearchProfileID != NULL) { $this->_ufGroupID = $config->defaultSearchProfileID; } // assign context to drive the template display, make sure context is valid $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, FALSE, 'search'); if (!CRM_Utils_Array::value($this->_context, self::validContext())) { $this->_context = 'search'; } $this->set('context', $this->_context); $this->assign('context', $this->_context); $this->_modeValue = self::getModeValue($this->_componentMode); $this->assign($this->_modeValue); $this->set('selectorName', self::$_selectorName); // get user submitted values // get it from controller only if form has been submitted, else preProcess has set this // $this->controller->isModal( ) returns TRUE if page is // valid, i.e all the validations are TRUE if (!empty($_POST) && !$this->controller->isModal()) { $this->_formValues = $this->controller->exportValues($this->_name); $this->normalizeFormValues(); $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $this->_returnProperties =& $this->returnProperties(); // also get the uf group id directly from the post value $this->_ufGroupID = CRM_Utils_Array::value('uf_group_id', $_POST, $this->_ufGroupID); $this->_formValues['uf_group_id'] = $this->_ufGroupID; $this->set('id', $this->_ufGroupID); // also get the object mode directly from the post value $this->_componentMode = CRM_Utils_Array::value('component_mode', $_POST, $this->_componentMode); // also get the operator from the post value if set $this->_operator = CRM_Utils_Array::value('operator', $_POST, $this->_operator); $this->_formValues['operator'] = $this->_operator; $this->set('operator', $this->_operator); } else { $this->_formValues = $this->get('formValues'); $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $this->_returnProperties =& $this->returnProperties(); if (!empty($this->_ufGroupID)) { $this->set('id', $this->_ufGroupID); } } if (empty($this->_formValues)) { //check if group is a smart group (fix for CRM-1255) if ($this->_groupID) { if ($ssId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'saved_search_id')) { $this->_ssID = $ssId; } } // fix for CRM-1907 if (isset($this->_ssID) && $this->_context != 'smog') { // we only retrieve the saved search values if out current values are null $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); //fix for CRM-1505 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) { $this->_params = CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID); } else { $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); } $this->_returnProperties =& $this->returnProperties(); } else { if (isset($this->_ufGroupID)) { // also set the uf group id if not already present $this->_formValues['uf_group_id'] = $this->_ufGroupID; } if (isset($this->_componentMode)) { $this->_formValues['component_mode'] = $this->_componentMode; } if (isset($this->_operator)) { $this->_formValues['operator'] = $this->_operator; } // FIXME: we should generalise in a way that components could inject url-filters // just like they build their own form elements foreach (array('mailing_id', 'mailing_delivery_status', 'mailing_open_status', 'mailing_click_status', 'mailing_reply_status', 'mailing_optout', 'mailing_forward', 'mailing_unsubscribe', 'mailing_date_low', 'mailing_date_high') as $mailingFilter) { $type = 'String'; if ($mailingFilter == 'mailing_id' && ($filterVal = CRM_Utils_Request::retrieve('mailing_id', 'Positive', $this))) { $this->_formValues[$mailingFilter] = array($filterVal); } elseif ($filterVal = CRM_Utils_Request::retrieve($mailingFilter, $type, $this)) { $this->_formValues[$mailingFilter] = $filterVal; } if ($filterVal) { $this->_openedPanes['Mailings'] = 1; $this->_formValues['hidden_CiviMail'] = 1; } } } } $this->assign('id', CRM_Utils_Array::value('uf_group_id', $this->_formValues)); $operator = CRM_Utils_Array::value('operator', $this->_formValues, 'AND'); $this->set('queryOperator', $operator); if ($operator == 'OR') { $this->assign('operator', ts('OR')); } else { $this->assign('operator', ts('AND')); } // show the context menu only when we’re not searching for deleted contacts; CRM-5673 if (empty($this->_formValues['deleted_contacts'])) { $menuItems = CRM_Contact_BAO_Contact::contextMenu(); $primaryActions = CRM_Utils_Array::value('primaryActions', $menuItems, array()); $this->_contextMenu = CRM_Utils_Array::value('moreActions', $menuItems, array()); $this->assign('contextMenu', $primaryActions + $this->_contextMenu); } if (!isset($this->_componentMode)) { $this->_componentMode = CRM_Contact_BAO_Query::MODE_CONTACTS; } $modeValues = self::getModeValue($this->_componentMode); self::$_selectorName = $this->_modeValue['selectorName']; $setDynamic = FALSE; if (strpos(self::$_selectorName, 'CRM_Contact_Selector') !== FALSE) { $selector = new self::$_selectorName($this->_customSearchClass, $this->_formValues, $this->_params, $this->_returnProperties, $this->_action, FALSE, TRUE, $this->_context, $this->_contextMenu); $setDynamic = TRUE; } else { $selector = new self::$_selectorName($this->_params, $this->_action, NULL, FALSE, NULL, "search", "advanced"); } $selector->setKey($this->controller->_key); $controller = new CRM_Contact_Selector_Controller($selector, $this->get(CRM_Utils_Pager::PAGE_ID), $this->get(CRM_Utils_Sort::SORT_ID), CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::TRANSFER); $controller->setEmbedded(TRUE); $controller->setDynamicAction($setDynamic); if ($this->_force) { $this->postProcess(); /* * Note that we repeat this, since the search creates and stores * values that potentially change the controller behavior. i.e. things * like totalCount etc */ $sortID = NULL; if ($this->get(CRM_Utils_Sort::SORT_ID)) { $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), $this->get(CRM_Utils_Sort::SORT_DIRECTION)); } $controller = new CRM_Contact_Selector_Controller($selector, $this->get(CRM_Utils_Pager::PAGE_ID), $sortID, CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::TRANSFER); $controller->setEmbedded(TRUE); $controller->setDynamicAction($setDynamic); } $controller->moveFromSessionToTemplate(); }
/** * This method is called for processing a submitted search form. */ public function postProcess() { $this->set('isAdvanced', '0'); $this->set('isSearchBuilder', '0'); // get user submitted values // get it from controller only if form has been submitted, else preProcess has set this if (!empty($_POST)) { $this->_formValues = $this->controller->exportValues($this->_name); } if (isset($this->_groupID) && empty($this->_formValues['group'])) { $this->_formValues['group'] = $this->_groupID; } elseif (isset($this->_ssID) && empty($_POST)) { // if we are editing / running a saved search and the form has not been posted $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); //fix for CRM-1505 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) { $this->_params = CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID); } } // we dont want to store the sortByCharacter in the formValue, it is more like // a filter on the result set // this filter is reset if we click on the search button if ($this->_sortByCharacter !== NULL && empty($_POST)) { if (strtolower($this->_sortByCharacter) == 'all') { $this->_formValues['sortByCharacter'] = NULL; } else { $this->_formValues['sortByCharacter'] = $this->_sortByCharacter; } } else { $this->_sortByCharacter = NULL; } $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $this->_returnProperties =& $this->returnProperties(); parent::postProcess(); }
/** * load the smart group cache for a saved search */ static function load(&$group) { $groupID = $group->id; $savedSearchID = $group->saved_search_id; $sql = null; $idName = 'id'; $customClass = null; if ($savedSearchID) { require_once 'CRM/Contact/BAO/SavedSearch.php'; $ssParams =& CRM_Contact_BAO_SavedSearch::getSearchParams($savedSearchID); $returnProperties = array(); if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $savedSearchID, 'mapping_id')) { require_once "CRM/Core/BAO/Mapping.php"; $fv =& CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID); $returnProperties = CRM_Core_BAO_Mapping::returnProperties($fv); } if (isset($ssParams['customSearchID'])) { // if custom search require_once 'CRM/Contact/BAO/SearchCustom.php'; // we split it up and store custom class // so temp tables are not destroyed if they are used // hence customClass is defined above at top of function $customClass = CRM_Contact_BAO_SearchCustom::customClass($ssParams['customSearchID'], $savedSearchID); $searchSQL = $customClass->contactIDs(); $idName = 'contact_id'; } else { require_once 'CRM/Contact/BAO/Query.php'; $query = new CRM_Contact_BAO_Query($ssParams, $returnProperties, null, false, false, 1, true, true, false); $query->_useGroupBy = false; $searchSQL =& $query->searchQuery(0, 0, null, false, false, false, true, true, null); } $groupID = CRM_Utils_Type::escape($groupID, 'Integer'); $sql = $searchSQL . " AND contact_a.id NOT IN ( \n SELECT contact_id FROM civicrm_group_contact \n WHERE civicrm_group_contact.status = 'Removed' \n AND civicrm_group_contact.group_id = {$groupID} ) "; } if ($sql) { $sql .= " UNION "; } // lets also store the records that are explicitly added to the group // this allows us to skip the group contact LEFT JOIN $sql .= "\nSELECT contact_id as {$idName}\nFROM civicrm_group_contact\nWHERE civicrm_group_contact.status = 'Added'\n AND civicrm_group_contact.group_id = {$groupID} "; $dao = CRM_Core_DAO::executeQuery($sql); $values = array(); while ($dao->fetch()) { $values[] = "({$groupID},{$dao->{$idName}})"; } $groupIDs = array($groupID); self::remove($groupIDs); self::store($groupIDs, $values); if ($group->children) { require_once 'CRM/Contact/BAO/Group.php'; $childrenIDs = explode(',', $group->children); foreach ($childrenIDs as $childID) { $contactIDs =& CRM_Contact_BAO_Group::getMember($childID, false); $values = array(); foreach ($contactIDs as $contactID => $dontCare) { $values[] = "({$groupID},{$contactID})"; } self::store($groupIDs, $values); } } }
/** * Process the form after the input has been submitted and validated. */ public function postProcess() { // saved search form values // get form values of all the forms in this controller $formValues = $this->controller->exportValues(); $isAdvanced = $this->get('isAdvanced'); $isSearchBuilder = $this->get('isSearchBuilder'); // add mapping record only for search builder saved search $mappingId = NULL; if ($isAdvanced == '2' && $isSearchBuilder == '1') { //save the mapping for search builder if (!$this->_id) { //save record in mapping table $mappingParams = array('mapping_type' => 'Search Builder'); $temp = array(); $mapping = CRM_Core_BAO_Mapping::add($mappingParams, $temp); $mappingId = $mapping->id; } else { //get the mapping id from saved search $savedSearch = new CRM_Contact_BAO_SavedSearch(); $savedSearch->id = $this->_id; $savedSearch->find(TRUE); $mappingId = $savedSearch->mapping_id; } //save mapping fields CRM_Core_BAO_Mapping::saveMappingFields($formValues, $mappingId); } //save the search $savedSearch = new CRM_Contact_BAO_SavedSearch(); $savedSearch->id = $this->_id; $savedSearch->form_values = serialize($this->get('formValues')); $savedSearch->mapping_id = $mappingId; $savedSearch->search_custom_id = $this->get('customSearchID'); $savedSearch->save(); $this->set('ssID', $savedSearch->id); CRM_Core_Session::setStatus(ts("Your smart group has been saved as '%1'.", array(1 => $formValues['title'])), ts('Group Saved'), 'success'); // also create a group that is associated with this saved search only if new saved search $params = array(); $params['title'] = $formValues['title']; $params['description'] = $formValues['description']; if (isset($formValues['group_type']) && is_array($formValues['group_type'])) { $params['group_type'] = CRM_Core_DAO::VALUE_SEPARATOR . implode(CRM_Core_DAO::VALUE_SEPARATOR, array_keys($formValues['group_type'])) . CRM_Core_DAO::VALUE_SEPARATOR; } else { $params['group_type'] = ''; } $params['visibility'] = 'User and User Admin Only'; $params['saved_search_id'] = $savedSearch->id; $params['is_active'] = 1; //CRM-14190 $params['parents'] = $formValues['parents']; if ($this->_id) { $params['id'] = CRM_Contact_BAO_SavedSearch::getName($this->_id, 'id'); } $group = CRM_Contact_BAO_Group::create($params); // CRM-9464 $this->_id = $savedSearch->id; //CRM-14190 if (!empty($formValues['parents'])) { CRM_Contact_BAO_GroupNestingCache::update(); } }
/** * this method is called for processing a submitted search form * * @return void * @access public */ function postProcess() { $session =& CRM_Core_Session::singleton(); $session->set('isAdvanced', '0'); $session->set('isSearchBuilder', '0'); // get user submitted values // get it from controller only if form has been submitted, else preProcess has set this if (!empty($_POST)) { $this->_formValues = $this->controller->exportValues($this->_name); $this->normalizeFormValues(); } if (isset($this->_groupID) && !CRM_Utils_Array::value('group', $this->_formValues)) { $this->_formValues['group'][$this->_groupID] = 1; } else { if (isset($this->_ssID) && empty($_POST)) { // if we are editing / running a saved search and the form has not been posted $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); //fix for CRM-1505 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) { $this->_params =& CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID); } } } // we dont want to store the sortByCharacter in the formValue, it is more like // a filter on the result set // this filter is reset if we click on the search button if ($this->_sortByCharacter && empty($_POST)) { if ($this->_sortByCharacter == 1) { $this->_formValues['sortByCharacter'] = null; } else { $this->_formValues['sortByCharacter'] = $this->_sortByCharacter; } } $this->_params =& CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $this->_returnProperties =& $this->returnProperties(); // CRM_Core_Error::debug( 'f', $this->_formValues ); // CRM_Core_Error::debug( 'p', $this->_params ); parent::postProcess(); }
/** * Create a smart group. * * By default it will be a group of households. * * @param array $smartGroupParams * @param array $groupParams * @return int */ public function smartGroupCreate($smartGroupParams = array(), $groupParams = array()) { $smartGroupParams = array_merge(array('formValues' => array('contact_type' => array('IN' => array('Household')))), $smartGroupParams); $savedSearch = CRM_Contact_BAO_SavedSearch::create($smartGroupParams); $groupParams['saved_search_id'] = $savedSearch->id; return $this->groupCreate($groupParams); }
/** * Get group clause for this user * * @param int $type the type of permission needed * @param array $tables (reference ) add the tables that are needed for the select clause * @param array $whereTables (reference ) add the tables that are needed for the where clause * * @return string the group where clause for this user * @access public */ public static function groupClause($type, &$tables, &$whereTables) { if (!isset(self::$_viewPermissionedGroups)) { self::group(); } if ($type == CRM_Core_Permission::EDIT) { if (self::$_editAdminUser) { $clause = ' ( 1 ) '; } else { if (empty(self::$_editPermissionedGroups)) { $clause = ' ( 0 ) '; } else { $clauses = array(); $groups = implode(', ', self::$_editPermissionedGroups); $clauses[] = ' ( civicrm_group_contact.group_id IN ( ' . implode(', ', array_keys(self::$_editPermissionedGroups)) . " ) AND civicrm_group_contact.status = 'Added' ) "; $tables['civicrm_group_contact'] = 1; $whereTables['civicrm_group_contact'] = 1; // foreach group that is potentially a saved search, add the saved search clause foreach (array_keys(self::$_editPermissionedGroups) as $id) { $group = new CRM_Contact_DAO_Group(); $group->id = $id; if ($group->find(true) && $group->saved_search_id) { require_once 'CRM/Contact/BAO/SavedSearch.php'; $clause = CRM_Contact_BAO_SavedSearch::whereClause($group->saved_search_id, $tables, $whereTables); if (trim($clause)) { $clauses[] = $clause; } } } $clause = ' ( ' . implode(' OR ', $clauses) . ' ) '; } } } else { if (self::$_viewAdminUser) { $clause = ' ( 1 ) '; } else { if (empty(self::$_viewPermissionedGroups)) { $clause = ' ( 0 ) '; } else { $clauses = array(); $groups = implode(', ', self::$_viewPermissionedGroups); $clauses[] = ' ( civicrm_group_contact.group_id IN (' . implode(', ', array_keys(self::$_viewPermissionedGroups)) . " ) AND civicrm_group_contact.status = 'Added' ) "; $tables['civicrm_group_contact'] = 1; $whereTables['civicrm_group_contact'] = 1; // foreach group that is potentially a saved search, add the saved search clause foreach (array_keys(self::$_viewPermissionedGroups) as $id) { $group = new CRM_Contact_DAO_Group(); $group->id = $id; if ($group->find(true) && $group->saved_search_id) { require_once 'CRM/Contact/BAO/SavedSearch.php'; $clause = CRM_Contact_BAO_SavedSearch::whereClause($group->saved_search_id, $tables, $whereTables); if (trim($clause)) { $clauses[] = $clause; } } } $clause = ' ( ' . implode(' OR ', $clauses) . ' ) '; } } } return $clause; }
/** * The post processing of the form gets done here. * * Key things done during post processing are * - check for reset or next request. if present, skip post procesing. * - now check if user requested running a saved search, if so, then * the form values associated with the saved search are used for searching. * - if user has done a submit with new values the regular post submissing is * done. * The processing consists of using a Selector / Controller framework for getting the * search results. * * @param * * @return void * @access public */ function postProcess() { if ($this->_done) { return; } $this->_done = true; if (!empty($_POST)) { $this->_formValues = $this->controller->exportValues($this->_name); } if (empty($this->_formValues)) { $this->_formValues = $this->controller->exportValues($this->_name); } $this->fixFormValues(); if (isset($this->_ssID) && empty($_POST)) { // if we are editing / running a saved search and the form has not been posted $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); } // we don't show test registrations in Contact Summary / User Dashboard // in Search mode by default we hide test registrations if (!CRM_Utils_Array::value('participant_test', $this->_formValues)) { $this->_formValues["participant_test"] = 0; } require_once 'CRM/Core/BAO/CustomValue.php'; CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); require_once 'CRM/Contact/BAO/Query.php'; $this->_queryParams =& CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $this->set('formValues', $this->_formValues); $this->set('queryParams', $this->_queryParams); $buttonName = $this->controller->getButtonName(); if ($buttonName == $this->_actionButtonName || $buttonName == $this->_printButtonName) { // check actionName and if next, then do not repeat a search, since we are going to the next page // hack, make sure we reset the task values $stateMachine =& $this->controller->getStateMachine(); $formName = $stateMachine->getTaskFormName(); $this->controller->resetPage($formName); return; } $sortID = null; if ($this->get(CRM_Utils_Sort::SORT_ID)) { $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), $this->get(CRM_Utils_Sort::SORT_DIRECTION)); } require_once 'CRM/Contact/BAO/Query.php'; $this->_queryParams =& CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $selector =& new CRM_Event_Selector_Search($this->_queryParams, $this->_action, null, $this->_single, $this->_limit, $this->_context); $prefix = null; if ($this->_context == 'user') { $prefix = $this->_prefix; } $this->assign("{$prefix}limit", $this->_limit); $this->assign("{$prefix}single", $this->_single); $controller =& new CRM_Core_Selector_Controller($selector, $this->get(CRM_Utils_Pager::PAGE_ID), $sortID, CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::SESSION, $prefix); $controller->setEmbedded(true); $query =& $selector->getQuery(); if ($this->_context == 'user') { $query->setSkipPermission(true); } $controller->run(); }
/** * processing needed for buildForm and later * * @return void * @access public */ function preProcess() { /** * set the varios class variables */ $this->_group =& CRM_Core_PseudoConstant::group(); $this->_groupIterator =& CRM_Core_PseudoConstant::groupIterator(); $this->_tag =& CRM_Core_PseudoConstant::tag(); $this->_done = false; /** * set the button names */ $this->_searchButtonName = $this->getButtonName('refresh'); $this->_printButtonName = $this->getButtonName('next', 'print'); $this->_actionButtonName = $this->getButtonName('next', 'action'); /* * we allow the controller to set force/reset externally, useful when we are being * driven by the wizard framework */ $this->_reset = CRM_Utils_Request::retrieve('reset', 'Boolean', CRM_Core_DAO::$_nullObject); $this->_force = CRM_Utils_Request::retrieve('force', 'Boolean', CRM_Core_DAO::$_nullObject); $this->_groupID = CRM_Utils_Request::retrieve('gid', 'Positive', $this); $this->_amtgID = CRM_Utils_Request::retrieve('amtgID', 'Positive', $this); $this->_ssID = CRM_Utils_Request::retrieve('ssID', 'Positive', $this); $this->_sortByCharacter = CRM_Utils_Request::retrieve('sortByCharacter', 'String', $this); $this->_ufGroupID = CRM_Utils_Request::retrieve('id', 'Positive', $this); // reset from session, CRM-3526 $session =& CRM_Core_Session::singleton(); if ($this->_force && $session->get('selectedSearchContactIds')) { $session->resetScope('selectedSearchContactIds'); } // if we dont get this from the url, use default if one exsts $config =& CRM_Core_Config::singleton(); if ($this->_ufGroupID == null && $config->defaultSearchProfileID != null) { $this->_ufGroupID = $config->defaultSearchProfileID; } /* * assign context to drive the template display, make sure context is valid */ $this->_context = CRM_Utils_Request::retrieve('context', 'String', $this, false, 'search'); if (!CRM_Utils_Array::value($this->_context, self::validContext())) { $this->_context = 'search'; $this->set('context', $this->_context); } $this->assign('context', $this->_context); $this->set('selectorName', $this->_selectorName); // get user submitted values // get it from controller only if form has been submitted, else preProcess has set this // $this->controller->isModal( ) returns true if page is // valid, i.e all the validations are true if (!empty($_POST) && !$this->controller->isModal()) { $this->_formValues = $this->controller->exportValues($this->_name); $this->normalizeFormValues(); $this->_params =& CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $this->_returnProperties =& $this->returnProperties(); // also get the uf group id directly from the post value $this->_ufGroupID = CRM_Utils_Array::value('uf_group_id', $_POST, $this->_ufGroupID); $this->_formValues['uf_group_id'] = $this->_ufGroupID; $this->set('id', $this->_ufGroupID); } else { $this->_formValues = $this->get('formValues'); $this->_params =& CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $this->_returnProperties =& $this->returnProperties(); } if (empty($this->_formValues)) { //check if group is a smart group (fix for CRM-1255) if ($this->_groupID) { if ($ssId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $this->_groupID, 'saved_search_id')) { $this->_ssID = $ssId; } } // fix for CRM-1907 if (isset($this->_ssID) && $this->_context != 'smog') { // we only retrieve the saved search values if out current values are null $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); //fix for CRM-1505 if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'mapping_id')) { $this->_params =& CRM_Contact_BAO_SavedSearch::getSearchParams($this->_ssID); } else { $this->_params =& CRM_Contact_BAO_Query::convertFormValues($this->_formValues); } $this->_returnProperties =& $this->returnProperties(); } else { if (isset($this->_ufGroupID)) { // also set the uf group id if not already present $this->_formValues['uf_group_id'] = $this->_ufGroupID; } } } $this->assign('id', CRM_Utils_Array::value('uf_group_id', $this->_formValues)); require_once 'CRM/Contact/BAO/Contact.php'; $menuItems = CRM_Contact_BAO_Contact::contextMenu(); $primaryActions = CRM_Utils_Array::value('primaryActions', $menuItems, array()); $this->_contextMenu = CRM_Utils_Array::value('moreActions', $menuItems, array()); $this->assign('contextMenu', $primaryActions + $this->_contextMenu); // CRM_Core_Error::debug( 'f', $this->_formValues ); // CRM_Core_Error::debug( 'p', $this->_params ); eval('$selector =& new ' . $this->_selectorName . '( $this->_customSearchClass, $this->_formValues, $this->_params, $this->_returnProperties, $this->_action, false, true, $this->_context );'); $controller =& new CRM_Contact_Selector_Controller($selector, $this->get(CRM_Utils_Pager::PAGE_ID), $this->get(CRM_Utils_Sort::SORT_ID), CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::TRANSFER); $controller->setEmbedded(true); if ($this->_force) { $this->postProcess(); /* * Note that we repeat this, since the search creates and stores * values that potentially change the controller behavior. i.e. things * like totalCount etc */ $sortID = null; if ($this->get(CRM_Utils_Sort::SORT_ID)) { $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), $this->get(CRM_Utils_Sort::SORT_DIRECTION)); } $controller =& new CRM_Contact_Selector_Controller($selector, $this->get(CRM_Utils_Pager::PAGE_ID), $sortID, CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::TRANSFER); $controller->setEmbedded(true); } $controller->moveFromSessionToTemplate(); }
/** * Given a table and id pair, return the filter clause * * @param string $table - The table owning the object * @param int $id - The ID of the object * @param array ref $tables - Tables that will be needed in the FROM * * @return string|null - WHERE-style clause to filter results, or null if $table or $id is null * @access public * @static */ public static function getClause($table, $id, &$tables) { $table = CRM_Utils_Type::escape($table, 'String'); $id = CRM_Utils_Type::escape($id, 'Integer'); $whereTables = array(); $ssTable = CRM_Contact_BAO_SavedSearch::getTableName(); if (empty($table)) { return NULL; } elseif ($table == $ssTable) { return CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables); } elseif (!empty($id)) { $tables[$table] = TRUE; return "{$table}.id = {$id}"; } return NULL; }
/** * The post processing of the form gets done here. * * Key things done during post processing are * - check for reset or next request. if present, skip post procesing. * - now check if user requested running a saved search, if so, then * the form values associated with the saved search are used for searching. * - if user has done a submit with new values the regular post submissing is * done. * The processing consists of using a Selector / Controller framework for getting the * search results. * * @param * * @return void * @access public */ function postProcess() { $this->set('isAdvanced', '1'); // get user submitted values // get it from controller only if form has been submitted, else preProcess has set this if (!empty($_POST)) { $this->_formValues = $this->controller->exportValues($this->_name); $this->normalizeFormValues(); // FIXME: couldn't figure out a good place to do this, // FIXME: so leaving this as a dependency for now if (array_key_exists('contribution_amount_low', $this->_formValues)) { foreach (array('contribution_amount_low', 'contribution_amount_high') as $f) { $this->_formValues[$f] = CRM_Utils_Rule::cleanMoney($this->_formValues[$f]); } } // set the group if group is submitted if (!empty($this->_formValues['uf_group_id'])) { $this->set('id', $this->_formValues['uf_group_id']); } else { $this->set('id', ''); } } // retrieve ssID values only if formValues is null, i.e. form has never been posted if (empty($this->_formValues) && isset($this->_ssID)) { $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); } if (isset($this->_groupID) && !CRM_Utils_Array::value('group', $this->_formValues)) { $this->_formValues['group'] = array($this->_groupID => 1); } //search for civicase if (is_array($this->_formValues)) { $allCases = FALSE; if (array_key_exists('case_owner', $this->_formValues) && !$this->_formValues['case_owner'] && !$this->_force) { foreach (array('case_type_id', 'case_status_id', 'case_deleted', 'case_tags') as $caseCriteria) { if (CRM_Utils_Array::value($caseCriteria, $this->_formValues)) { $allCases = TRUE; $this->_formValues['case_owner'] = 1; continue; } } if ($allCases) { if (CRM_Core_Permission::check('access all cases and activities')) { $this->_formValues['case_owner'] = 1; } else { $this->_formValues['case_owner'] = 2; } } else { $this->_formValues['case_owner'] = 0; } } } // we dont want to store the sortByCharacter in the formValue, it is more like // a filter on the result set // this filter is reset if we click on the search button if ($this->_sortByCharacter !== NULL && empty($_POST)) { if (strtolower($this->_sortByCharacter) == 'all') { $this->_formValues['sortByCharacter'] = NULL; } else { $this->_formValues['sortByCharacter'] = $this->_sortByCharacter; } } else { $this->_sortByCharacter = NULL; } CRM_Core_BAO_CustomValue::fixFieldValueOfTypeMemo($this->_formValues); $this->_params = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $this->_returnProperties =& $this->returnProperties(); parent::postProcess(); }
/** * Normalize default values for multiselect plugins. * * @param array $defaults * * @return array */ public function normalizeDefaultValues(&$defaults) { if (!is_array($defaults)) { $defaults = array(); } if ($this->_ssID && empty($_POST)) { $defaults = array_merge($defaults, CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID)); } return $defaults; }
/** * The post processing of the form gets done here. * * Key things done during post processing are * - check for reset or next request. if present, skip post procesing. * - now check if user requested running a saved search, if so, then * the form values associated with the saved search are used for searching. * - if user has done a submit with new values the regular post submissing is * done. * The processing consists of using a Selector / Controller framework for getting the * search results. */ public function postProcess() { if ($this->_done) { return; } $this->_done = TRUE; $this->_formValues = $this->controller->exportValues($this->_name); $this->fixFormValues(); if (isset($this->_ssID) && empty($_POST)) { // if we are editing / running a saved search and the form has not been posted $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); } //search for civicase if (!$this->_force) { if (array_key_exists('case_owner', $this->_formValues) && !$this->_formValues['case_owner']) { $this->_formValues['case_owner'] = 0; } } if (empty($this->_formValues['case_deleted'])) { $this->_formValues['case_deleted'] = 0; } CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $this->set('formValues', $this->_formValues); $this->set('queryParams', $this->_queryParams); $buttonName = $this->controller->getButtonName(); if ($buttonName == $this->_actionButtonName) { // check actionName and if next, then do not repeat a search, since we are going to the next page // hack, make sure we reset the task values $stateMachine = $this->controller->getStateMachine(); $formName = $stateMachine->getTaskFormName(); $this->controller->resetPage($formName); return; } $sortID = NULL; if ($this->get(CRM_Utils_Sort::SORT_ID)) { $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), $this->get(CRM_Utils_Sort::SORT_DIRECTION)); } $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues); $selector = new CRM_Case_Selector_Search($this->_queryParams, $this->_action, NULL, $this->_single, $this->_limit, $this->_context); $selector->setKey($this->controller->_key); $prefix = NULL; if ($this->_context == 'user') { $prefix = $this->_prefix; } $this->assign("{$prefix}limit", $this->_limit); $this->assign("{$prefix}single", $this->_single); $controller = new CRM_Core_Selector_Controller($selector, $this->get(CRM_Utils_Pager::PAGE_ID), $sortID, CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::SESSION, $prefix); $controller->setEmbedded(TRUE); $query =& $selector->getQuery(); if ($this->_context == 'user') { $query->setSkipPermission(TRUE); } $controller->run(); }
/** * where / qill clause for smart groups * * @return void * @access public */ function savedSearch() { $config =& CRM_Core_Config::singleton(); $ssWhere = array(); $group =& new CRM_Contact_BAO_Group(); foreach (array_keys($this->_params['group']) as $group_id) { $group->id = $group_id; $group->find(true); if (isset($group->saved_search_id)) { require_once 'CRM/Contact/BAO/SavedSearch.php'; if ($config->mysqlVersion >= 4.1) { $sfv =& CRM_Contact_BAO_SavedSearch::getFormValues($group->saved_search_id); $smarts =& CRM_Contact_BAO_Contact::searchQuery($sfv, 0, 0, null, false, false, false, true, true); $ssWhere[] = " \n (civicrm_contact.id IN ({$smarts}) \n AND civicrm_contact.id NOT IN ( \n SELECT contact_id FROM civicrm_group_contact \n WHERE civicrm_group_contact.group_id = " . CRM_Utils_Type::escape($group_id, 'Integer') . " AND civicrm_group_contact.status = 'Removed'))"; } else { $ssw = CRM_Contact_BAO_SavedSearch::whereClause($group->saved_search_id, $this->_tables, $this->_whereTables); /* FIXME: bug with multiple group searches */ $ssWhere[] = "({$ssw} AND\n (civicrm_group_contact.id is null OR\n (civicrm_group_contact.group_id = " . CRM_Utils_Type::escape($group_id, 'Integer') . " AND\n civicrm_group_contact.status = 'Added')))"; } } $group->reset(); $group->selectAdd('*'); } if (!empty($ssWhere)) { $this->_tables['civicrm_group_contact'] = "civicrm_contact.id = civicrm_group_contact.contact_id AND civicrm_group_contact.group_id IN (" . implode(',', array_keys($this->_params['group'])) . ')'; $this->_whereTables['civicrm_group_contact'] = $this->_tables['civicrm_group_contact']; return implode(' OR ', $ssWhere); } return null; }
function from() { //define table name $randomNum = md5(uniqid()); $this->_tableName = "civicrm_temp_custom_{$randomNum}"; //block for Group search $smartGroup = array(); $group = new CRM_Contact_DAO_Group(); $group->is_active = 1; $group->find(); while ($group->fetch()) { $allGroups[] = $group->id; if ($group->saved_search_id) { $smartGroup[$group->saved_search_id] = $group->id; } } $includedGroups = implode(',', $allGroups); if (!empty($this->_includeGroups)) { $iGroups = implode(',', $this->_includeGroups); } else { //if no group selected search for all groups $iGroups = $includedGroups; } if (is_array($this->_excludeGroups)) { $xGroups = implode(',', $this->_excludeGroups); } else { $xGroups = 0; } $sql = "DROP TEMPORARY TABLE IF EXISTS Xg_{$this->_tableName}"; CRM_Core_DAO::executeQuery($sql); $sql = "CREATE TEMPORARY TABLE Xg_{$this->_tableName} ( contact_id int primary key) ENGINE=HEAP"; CRM_Core_DAO::executeQuery($sql); //used only when exclude group is selected if ($xGroups != 0) { $excludeGroup = "INSERT INTO Xg_{$this->_tableName} ( contact_id )\n SELECT DISTINCT civicrm_group_contact.contact_id\n FROM civicrm_group_contact\n WHERE \n civicrm_group_contact.status = 'Added' AND\n civicrm_group_contact.group_id IN ( {$xGroups} )"; CRM_Core_DAO::executeQuery($excludeGroup); //search for smart group contacts foreach ($this->_excludeGroups as $keys => $values) { if (in_array($values, $smartGroup)) { $ssId = CRM_Utils_Array::key($values, $smartGroup); $smartSql = CRM_Contact_BAO_SavedSearch::contactIDsSQL($ssId); $smartSql = $smartSql . " AND contact_a.id NOT IN ( \n SELECT contact_id FROM civicrm_group_contact \n WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')"; $smartGroupQuery = " INSERT IGNORE INTO Xg_{$this->_tableName}(contact_id) {$smartSql}"; CRM_Core_DAO::executeQuery($smartGroupQuery); } } } $sql = "DROP TEMPORARY TABLE IF EXISTS Ig_{$this->_tableName}"; CRM_Core_DAO::executeQuery($sql); $sql = "CREATE TEMPORARY TABLE Ig_{$this->_tableName}\n ( id int PRIMARY KEY AUTO_INCREMENT,\n contact_id int,\n group_names varchar(64)) ENGINE=HEAP"; if ($this->_debug > 0) { print "-- Include groups query: <pre>"; print "{$sql};"; print "</pre>"; } CRM_Core_DAO::executeQuery($sql); $includeGroup = "INSERT INTO Ig_{$this->_tableName} (contact_id, group_names)\n SELECT civicrm_group_contact.contact_id, civicrm_group.name as group_name\n FROM civicrm_group_contact\n LEFT JOIN civicrm_group\n ON civicrm_group_contact.group_id = civicrm_group.id"; //used only when exclude group is selected if ($xGroups != 0) { $includeGroup .= " LEFT JOIN Xg_{$this->_tableName}\n ON civicrm_group_contact.contact_id = Xg_{$this->_tableName}.contact_id"; } $includeGroup .= " WHERE \n civicrm_group_contact.status = 'Added' AND\n civicrm_group_contact.group_id IN({$iGroups})"; //used only when exclude group is selected if ($xGroups != 0) { $includeGroup .= " AND Xg_{$this->_tableName}.contact_id IS null"; } if ($this->_debug > 0) { print "-- Include groups query: <pre>"; print "{$includeGroup};"; print "</pre>"; } CRM_Core_DAO::executeQuery($includeGroup); //search for smart group contacts foreach ($this->_includeGroups as $keys => $values) { if (in_array($values, $smartGroup)) { $ssId = CRM_Utils_Array::key($values, $smartGroup); $smartSql = CRM_Contact_BAO_SavedSearch::contactIDsSQL($ssId); $smartSql .= " AND contact_a.id NOT IN ( \n SELECT contact_id FROM civicrm_group_contact\n WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')"; //used only when exclude group is selected if ($xGroups != 0) { $smartSql .= " AND contact_a.id NOT IN (SELECT contact_id FROM Xg_{$this->_tableName})"; } $smartGroupQuery = " INSERT IGNORE INTO Ig_{$this->_tableName}(contact_id) \n {$smartSql}"; CRM_Core_DAO::executeQuery($smartGroupQuery); $insertGroupNameQuery = "UPDATE IGNORE Ig_{$this->_tableName}\n SET group_names = (SELECT title FROM civicrm_group\n WHERE civicrm_group.id = {$values})\n WHERE Ig_{$this->_tableName}.contact_id IS NOT NULL \n AND Ig_{$this->_tableName}.group_names IS NULL"; CRM_Core_DAO::executeQuery($insertGroupNameQuery); } } $from = "FROM civicrm_contact contact_a"; $fromTail = "LEFT JOIN civicrm_email ON ( contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1 )"; $fromTail .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; // now create a temp table to store the randomized contacts $sql = "DROP TEMPORARY TABLE IF EXISTS random_{$this->_tableName}"; CRM_Core_DAO::executeQuery($sql); $sql = "CREATE TEMPORARY TABLE random_{$this->_tableName} ( id int primary key ) ENGINE=HEAP"; CRM_Core_DAO::executeQuery($sql); if (substr($this->_segmentSize, -1) == '%') { $countSql = "SELECT DISTINCT contact_a.id {$from} {$fromTail}\n WHERE " . $this->where(); $dao = CRM_Core_DAO::executeQuery($countSql); $totalSize = $dao->N; $multiplier = substr($this->_segmentSize, 0, strlen($this->_segmentSize) - 1); $multiplier /= 100; //CRM_Core_Error::debug( "Total size: $totalSize<br/>Multiplier: $multiplier<br/>"); $this->_segmentSize = round($totalSize * $multiplier); } $sql = "INSERT INTO random_{$this->_tableName} ( id )\n SELECT DISTINCT contact_a.id {$from} {$fromTail}\n WHERE " . $this->where() . "\n ORDER BY RAND()\n LIMIT {$this->_segmentSize}"; CRM_Core_DAO::executeQuery($sql); $from = "FROM random_{$this->_tableName} random"; $from .= " INNER JOIN civicrm_contact contact_a ON random.id = contact_a.id"; $from .= " {$fromTail}"; return $from; }
/** * Get group clause for this user. The group Clause filters the * list of groups that the user is permitted to see in a group listing. * For example it will filter both the list on the 'Manage Groups' page * and on the contact 'Groups' tab * * the aclGroup hook & configured ACLs contribute to this data. * If the contact is allowed to see all contacts the function will return ( 1 ) * * @todo the history of this function is that there was some confusion as to * whether it was filtering contacts or groups & some cruft may remain * * @param int $type * The type of permission needed. * @param array $tables * (reference) add the tables that are needed for the select clause. * @param array $whereTables * (reference) add the tables that are needed for the where clause. * * @return string * the clause to add to the query retrieving viewable groups */ public function groupClause($type, &$tables, &$whereTables) { if (!isset($this->_viewPermissionedGroups)) { $this->group(); } // we basically get all the groups here $groupKey = 'all'; if ($type == CRM_Core_Permission::EDIT) { if ($this->_editAdminUser) { $clause = ' ( 1 ) '; } elseif (empty($this->_editPermissionedGroups[$groupKey])) { $clause = ' ( 0 ) '; } else { $clauses = array(); $groups = implode(', ', $this->_editPermissionedGroups[$groupKey]); $clauses[] = ' ( civicrm_group_contact.group_id IN ( ' . implode(', ', array_keys($this->_editPermissionedGroups[$groupKey])) . " ) AND civicrm_group_contact.status = 'Added' ) "; $tables['civicrm_group_contact'] = 1; $whereTables['civicrm_group_contact'] = 1; // foreach group that is potentially a saved search, add the saved search clause foreach (array_keys($this->_editPermissionedGroups[$groupKey]) as $id) { $group = new CRM_Contact_DAO_Group(); $group->id = $id; if ($group->find(TRUE) && $group->saved_search_id) { $clause = CRM_Contact_BAO_SavedSearch::whereClause($group->saved_search_id, $tables, $whereTables); if (trim($clause)) { $clauses[] = $clause; } } } $clause = ' ( ' . implode(' OR ', $clauses) . ' ) '; } } else { if ($this->_viewAdminUser) { $clause = ' ( 1 ) '; } elseif (empty($this->_viewPermissionedGroups[$groupKey])) { $clause = ' ( 0 ) '; } else { $clauses = array(); $groups = implode(', ', $this->_viewPermissionedGroups[$groupKey]); $clauses[] = ' civicrm_group.id IN (' . implode(', ', array_keys($this->_viewPermissionedGroups[$groupKey])) . " ) "; $tables['civicrm_group'] = 1; $whereTables['civicrm_group'] = 1; $clause = ' ( ' . implode(' OR ', $clauses) . ' ) '; } } return $clause; }
/** * Load the smart group cache for a saved search. * * @param object $group * The smart group that needs to be loaded. * @param bool $force * Should we force a search through. */ public static function load(&$group, $force = FALSE) { $groupID = $group->id; $savedSearchID = $group->saved_search_id; if (array_key_exists($groupID, self::$_alreadyLoaded) && !$force) { return; } // grab a lock so other processes don't compete and do the same query $lock = Civi::lockManager()->acquire("data.core.group.{$groupID}"); if (!$lock->isAcquired()) { // this can cause inconsistent results since we don't know if the other process // will fill up the cache before our calling routine needs it. // however this routine does not return the status either, so basically // its a "lets return and hope for the best" return; } self::$_alreadyLoaded[$groupID] = 1; // we now have the lock, but some other process could have actually done the work // before we got here, so before we do any work, lets ensure that work needs to be // done // we allow hidden groups here since we dont know if the caller wants to evaluate an // hidden group if (!$force && !self::shouldGroupBeRefreshed($groupID, TRUE)) { $lock->release(); return; } $sql = NULL; $idName = 'id'; $customClass = NULL; if ($savedSearchID) { $ssParams = CRM_Contact_BAO_SavedSearch::getSearchParams($savedSearchID); // rectify params to what proximity search expects if there is a value for prox_distance // CRM-7021 if (!empty($ssParams)) { CRM_Contact_BAO_ProximityQuery::fixInputParams($ssParams); } $returnProperties = array(); if (CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $savedSearchID, 'mapping_id')) { $fv = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID); $returnProperties = CRM_Core_BAO_Mapping::returnProperties($fv); } if (isset($ssParams['customSearchID'])) { // if custom search // we split it up and store custom class // so temp tables are not destroyed if they are used // hence customClass is defined above at top of function $customClass = CRM_Contact_BAO_SearchCustom::customClass($ssParams['customSearchID'], $savedSearchID); $searchSQL = $customClass->contactIDs(); $searchSQL = str_replace('ORDER BY contact_a.id ASC', '', $searchSQL); if (!strstr($searchSQL, 'WHERE')) { $searchSQL .= " WHERE ( 1 ) "; } $idName = 'contact_id'; } else { $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($savedSearchID); // CRM-17075 using the formValues in this way imposes extra logic and complexity. // we have the where_clause and where tables stored in the saved_search table // and should use these rather than re-processing the form criteria (which over-works // the link between the form layer & the query layer too). // It's hard to think of when you would want to use anything other than return // properties = array('contact_id' => 1) here as the point would appear to be to // generate the list of contact ids in the group. // @todo review this to use values in saved_search table (preferably for 4.8). $query = new CRM_Contact_BAO_Query($ssParams, $returnProperties, NULL, FALSE, FALSE, 1, TRUE, TRUE, FALSE, CRM_Utils_Array::value('display_relationship_type', $formValues), CRM_Utils_Array::value('operator', $formValues, 'AND')); $query->_useDistinct = FALSE; $query->_useGroupBy = FALSE; $searchSQL = $query->searchQuery(0, 0, NULL, FALSE, FALSE, FALSE, TRUE, TRUE, NULL, NULL, NULL, TRUE); } $groupID = CRM_Utils_Type::escape($groupID, 'Integer'); $sql = $searchSQL . " AND contact_a.id NOT IN (\n SELECT contact_id FROM civicrm_group_contact\n WHERE civicrm_group_contact.status = 'Removed'\n AND civicrm_group_contact.group_id = {$groupID} ) "; } if ($sql) { $sql = preg_replace("/^\\s*SELECT/", "SELECT {$groupID} as group_id, ", $sql); } // lets also store the records that are explicitly added to the group // this allows us to skip the group contact LEFT JOIN $sqlB = "\nSELECT {$groupID} as group_id, contact_id as {$idName}\nFROM civicrm_group_contact\nWHERE civicrm_group_contact.status = 'Added'\n AND civicrm_group_contact.group_id = {$groupID} "; $groupIDs = array($groupID); self::remove($groupIDs); $processed = FALSE; $tempTable = 'civicrm_temp_group_contact_cache' . rand(0, 2000); foreach (array($sql, $sqlB) as $selectSql) { if (!$selectSql) { continue; } $insertSql = "CREATE TEMPORARY TABLE {$tempTable} ({$selectSql});"; $processed = TRUE; CRM_Core_DAO::executeQuery($insertSql); CRM_Core_DAO::executeQuery("INSERT IGNORE INTO civicrm_group_contact_cache (contact_id, group_id)\n SELECT DISTINCT {$idName}, group_id FROM {$tempTable}\n "); CRM_Core_DAO::executeQuery(" DROP TEMPORARY TABLE {$tempTable}"); } self::updateCacheTime($groupIDs, $processed); if ($group->children) { //Store a list of contacts who are removed from the parent group $sql = "\nSELECT contact_id\nFROM civicrm_group_contact\nWHERE civicrm_group_contact.status = 'Removed'\nAND civicrm_group_contact.group_id = {$groupID} "; $dao = CRM_Core_DAO::executeQuery($sql); $removed_contacts = array(); while ($dao->fetch()) { $removed_contacts[] = $dao->contact_id; } $childrenIDs = explode(',', $group->children); foreach ($childrenIDs as $childID) { $contactIDs = CRM_Contact_BAO_Group::getMember($childID, FALSE); //Unset each contact that is removed from the parent group foreach ($removed_contacts as $removed_contact) { unset($contactIDs[$removed_contact]); } $values = array(); foreach ($contactIDs as $contactID => $dontCare) { $values[] = "({$groupID},{$contactID})"; } self::store($groupIDs, $values); } } $lock->release(); }
static function fromWhereEmail($id) { $params =& self::getSearchParams($id); if ($params) { if (CRM_Utils_Array::value('customSearchID', $params)) { require_once 'CRM/Contact/BAO/SearchCustom.php'; return CRM_Contact_BAO_SearchCustom::fromWhereEmail(null, $id); } else { $tables = $whereTables = array('civicrm_contact' => 1, 'civicrm_email' => 1); $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables); $from = CRM_Contact_BAO_Query::fromClause($whereTables); return array($from, $where); } } else { // fix for CRM-7240 $from = "\nFROM civicrm_contact contact_a \nLEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1)\n"; $where = " ( 1 ) "; $tables['civicrm_contact'] = $whereTables['civicrm_contact'] = 1; $tables['civicrm_email'] = $whereTables['civicrm_email'] = 1; return array($from, $where); } }
/** * Get from where email (whatever that means!). * * @param int $id * * @return array */ public static function fromWhereEmail($id) { $params = self::getSearchParams($id); if ($params) { if (!empty($params['customSearchID'])) { return CRM_Contact_BAO_SearchCustom::fromWhereEmail(NULL, $id); } else { $tables = $whereTables = array('civicrm_contact' => 1, 'civicrm_email' => 1); $where = CRM_Contact_BAO_SavedSearch::whereClause($id, $tables, $whereTables); $from = CRM_Contact_BAO_Query::fromClause($whereTables); return array($from, $where); } } else { // fix for CRM-7240 $from = "\nFROM civicrm_contact contact_a\nLEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1)\n"; $where = " ( 1 ) "; $tables['civicrm_contact'] = $whereTables['civicrm_contact'] = 1; $tables['civicrm_email'] = $whereTables['civicrm_email'] = 1; return array($from, $where); } }
/** * The post processing of the form gets done here. * * Key things done during post processing are * - check for reset or next request. if present, skip post procesing. * - now check if user requested running a saved search, if so, then * the form values associated with the saved search are used for searching. * - if user has done a submit with new values the regular post submissing is * done. * The processing consists of using a Selector / Controller framework for getting the * search results. * * @param * * @return void */ public function postProcess() { if ($this->_done) { return; } $this->_done = TRUE; if (!empty($_POST)) { $this->_formValues = $this->controller->exportValues($this->_name); CRM_Contact_BAO_Query::processSpecialFormValue($this->_formValues, array('participant_status_id')); } if (empty($this->_formValues)) { $this->_formValues = $this->controller->exportValues($this->_name); } $this->fixFormValues(); if (isset($this->_ssID) && empty($_POST)) { // if we are editing / running a saved search and the form has not been posted $this->_formValues = CRM_Contact_BAO_SavedSearch::getFormValues($this->_ssID); } // We don't show test records in summaries or dashboards if (empty($this->_formValues['participant_test']) && $this->_force) { $this->_formValues["participant_test"] = 0; } CRM_Core_BAO_CustomValue::fixCustomFieldValue($this->_formValues); $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, array('event_id')); $this->set('formValues', $this->_formValues); $this->set('queryParams', $this->_queryParams); $buttonName = $this->controller->getButtonName(); if ($buttonName == $this->_actionButtonName) { // check actionName and if next, then do not repeat a search, since we are going to the next page // hack, make sure we reset the task values $stateMachine = $this->controller->getStateMachine(); $formName = $stateMachine->getTaskFormName(); $this->controller->resetPage($formName); return; } $sortID = NULL; if ($this->get(CRM_Utils_Sort::SORT_ID)) { $sortID = CRM_Utils_Sort::sortIDValue($this->get(CRM_Utils_Sort::SORT_ID), $this->get(CRM_Utils_Sort::SORT_DIRECTION)); } $this->_queryParams = CRM_Contact_BAO_Query::convertFormValues($this->_formValues, 0, FALSE, NULL, array('event_id')); $selector = new CRM_Event_Selector_Search($this->_queryParams, $this->_action, NULL, $this->_single, $this->_limit, $this->_context); $selector->setKey($this->controller->_key); $prefix = NULL; if ($this->_context == 'user') { $prefix = $this->_prefix; } $this->assign("{$prefix}limit", $this->_limit); $this->assign("{$prefix}single", $this->_single); $controller = new CRM_Core_Selector_Controller($selector, $this->get(CRM_Utils_Pager::PAGE_ID), $sortID, CRM_Core_Action::VIEW, $this, CRM_Core_Selector_Controller::SESSION, $prefix); $controller->setEmbedded(TRUE); $query = $selector->getQuery(); if ($this->_context == 'user') { $query->setSkipPermission(TRUE); } $controller->run(); }
function from() { //define table name $randomNum = md5(uniqid()); $this->_tableName = "civicrm_temp_custom_{$randomNum}"; //grab the contacts added in the date range first $sql = "CREATE TEMPORARY TABLE dates_{$this->_tableName} ( id int primary key, date_added date ) ENGINE=HEAP"; if ($this->_debug > 0) { print "-- Date range query: <pre>"; print "{$sql};"; print "</pre>"; } CRM_Core_DAO::executeQuery($sql); $startDate = CRM_Utils_Date::mysqlToIso(CRM_Utils_Date::processDate($this->_formValues['start_date'])); $endDateFix = NULL; if (!empty($this->_formValues['end_date'])) { $endDate = CRM_Utils_Date::mysqlToIso(CRM_Utils_Date::processDate($this->_formValues['end_date'])); # tack 11:59pm on to make search inclusive of the end date $endDateFix = "AND date_added <= '" . substr($endDate, 0, 10) . " 23:59:00'"; } $dateRange = "INSERT INTO dates_{$this->_tableName} ( id, date_added )\n SELECT\n civicrm_contact.id,\n min(civicrm_log.modified_date) AS date_added\n FROM\n civicrm_contact LEFT JOIN civicrm_log\n ON (civicrm_contact.id = civicrm_log.entity_id AND\n civicrm_log.entity_table = 'civicrm_contact')\n GROUP BY\n civicrm_contact.id\n HAVING\n date_added >= '{$startDate}' \n {$endDateFix}"; if ($this->_debug > 0) { print "-- Date range query: <pre>"; print "{$dateRange};"; print "</pre>"; } CRM_Core_DAO::executeQuery($dateRange, CRM_Core_DAO::$_nullArray); // Only include groups in the search query of one or more Include OR Exclude groups has been selected. // CRM-6356 if ($this->_groups) { //block for Group search $smartGroup = array(); require_once 'CRM/Contact/DAO/Group.php'; $group = new CRM_Contact_DAO_Group(); $group->is_active = 1; $group->find(); while ($group->fetch()) { $allGroups[] = $group->id; if ($group->saved_search_id) { $smartGroup[$group->saved_search_id] = $group->id; } } $includedGroups = implode(',', $allGroups); if (!empty($this->_includeGroups)) { $iGroups = implode(',', $this->_includeGroups); } else { //if no group selected search for all groups $iGroups = $includedGroups; } if (is_array($this->_excludeGroups)) { $xGroups = implode(',', $this->_excludeGroups); } else { $xGroups = 0; } $sql = "DROP TEMPORARY TABLE IF EXISTS Xg_{$this->_tableName}"; CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray); $sql = "CREATE TEMPORARY TABLE Xg_{$this->_tableName} ( contact_id int primary key) ENGINE=HEAP"; CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray); //used only when exclude group is selected if ($xGroups != 0) { $excludeGroup = "INSERT INTO Xg_{$this->_tableName} ( contact_id )\n SELECT DISTINCT civicrm_group_contact.contact_id\n FROM civicrm_group_contact, dates_{$this->_tableName} AS d\n WHERE \n d.id = civicrm_group_contact.contact_id AND \n civicrm_group_contact.status = 'Added' AND\n civicrm_group_contact.group_id IN( {$xGroups})"; CRM_Core_DAO::executeQuery($excludeGroup, CRM_Core_DAO::$_nullArray); //search for smart group contacts foreach ($this->_excludeGroups as $keys => $values) { if (in_array($values, $smartGroup)) { $ssId = CRM_Utils_Array::key($values, $smartGroup); $smartSql = CRM_Contact_BAO_SavedSearch::contactIDsSQL($ssId); $smartSql = $smartSql . " AND contact_a.id NOT IN ( \n SELECT contact_id FROM civicrm_group_contact \n WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')"; $smartGroupQuery = " INSERT IGNORE INTO Xg_{$this->_tableName}(contact_id) {$smartSql}"; CRM_Core_DAO::executeQuery($smartGroupQuery, CRM_Core_DAO::$_nullArray); } } } $sql = "DROP TEMPORARY TABLE IF EXISTS Ig_{$this->_tableName}"; CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray); $sql = "CREATE TEMPORARY TABLE Ig_{$this->_tableName}\n ( id int PRIMARY KEY AUTO_INCREMENT,\n contact_id int,\n group_names varchar(64)) ENGINE=HEAP"; if ($this->_debug > 0) { print "-- Include groups query: <pre>"; print "{$sql};"; print "</pre>"; } CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray); $includeGroup = "INSERT INTO Ig_{$this->_tableName} (contact_id, group_names)\n SELECT d.id as contact_id, civicrm_group.name as group_name\n FROM dates_{$this->_tableName} AS d\n INNER JOIN civicrm_group_contact\n ON civicrm_group_contact.contact_id = d.id\n LEFT JOIN civicrm_group\n ON civicrm_group_contact.group_id = civicrm_group.id"; //used only when exclude group is selected if ($xGroups != 0) { $includeGroup .= " LEFT JOIN Xg_{$this->_tableName}\n ON d.id = Xg_{$this->_tableName}.contact_id"; } $includeGroup .= " WHERE \n civicrm_group_contact.status = 'Added' AND\n civicrm_group_contact.group_id IN({$iGroups})"; //used only when exclude group is selected if ($xGroups != 0) { $includeGroup .= " AND Xg_{$this->_tableName}.contact_id IS null"; } if ($this->_debug > 0) { print "-- Include groups query: <pre>"; print "{$includeGroup};"; print "</pre>"; } CRM_Core_DAO::executeQuery($includeGroup, CRM_Core_DAO::$_nullArray); //search for smart group contacts foreach ($this->_includeGroups as $keys => $values) { if (in_array($values, $smartGroup)) { $ssId = CRM_Utils_Array::key($values, $smartGroup); $smartSql = CRM_Contact_BAO_SavedSearch::contactIDsSQL($ssId); $smartSql .= " AND contact_a.id IN (\n SELECT id AS contact_id\n FROM dates_{$this->_tableName} )"; $smartSql .= " AND contact_a.id NOT IN ( \n SELECT contact_id FROM civicrm_group_contact\n WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')"; //used only when exclude group is selected if ($xGroups != 0) { $smartSql .= " AND contact_a.id NOT IN (SELECT contact_id FROM Xg_{$this->_tableName})"; } $smartGroupQuery = " INSERT IGNORE INTO\n Ig_{$this->_tableName}(contact_id) \n {$smartSql}"; CRM_Core_DAO::executeQuery($smartGroupQuery, CRM_Core_DAO::$_nullArray); if ($this->_debug > 0) { print "-- Smart group query: <pre>"; print "{$smartGroupQuery};"; print "</pre>"; } $insertGroupNameQuery = "UPDATE IGNORE Ig_{$this->_tableName}\n SET group_names = (SELECT title FROM civicrm_group\n WHERE civicrm_group.id = {$values})\n WHERE Ig_{$this->_tableName}.contact_id IS NOT NULL \n AND Ig_{$this->_tableName}.group_names IS NULL"; CRM_Core_DAO::executeQuery($insertGroupNameQuery, CRM_Core_DAO::$_nullArray); if ($this->_debug > 0) { print "-- Smart group query: <pre>"; print "{$insertGroupNameQuery};"; print "</pre>"; } } } } // end if( $this->_groups ) condition $from = "FROM civicrm_contact contact_a"; /* We need to join to this again to get the date_added value */ $from .= " INNER JOIN dates_{$this->_tableName} d ON (contact_a.id = d.id)"; // Only include groups in the search query of one or more Include OR Exclude groups has been selected. // CRM-6356 if ($this->_groups) { $from .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; } //this makes smart groups using this search compatible w/ CiviMail $from .= " LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id)"; return $from; }
/** * This function create the hidden smart group when user perform * contact search and want to send mailing to search contacts. * * @param array $params * ( reference ) an assoc array of name/value pairs. * * @return array * ( smartGroupId, ssId ) smart group id and saved search id */ public static function createHiddenSmartGroup($params) { $ssId = CRM_Utils_Array::value('saved_search_id', $params); //add mapping record only for search builder saved search $mappingId = NULL; if ($params['search_context'] == 'builder') { //save the mapping for search builder if (!$ssId) { //save record in mapping table $temp = array(); $mappingParams = array('mapping_type' => 'Search Builder'); $mapping = CRM_Core_BAO_Mapping::add($mappingParams, $temp); $mappingId = $mapping->id; } else { //get the mapping id from saved search $savedSearch = new CRM_Contact_BAO_SavedSearch(); $savedSearch->id = $ssId; $savedSearch->find(TRUE); $mappingId = $savedSearch->mapping_id; } //save mapping fields CRM_Core_BAO_Mapping::saveMappingFields($params['form_values'], $mappingId); } //create/update saved search record. $savedSearch = new CRM_Contact_BAO_SavedSearch(); $savedSearch->id = $ssId; $savedSearch->form_values = serialize($params['form_values']); $savedSearch->mapping_id = $mappingId; $savedSearch->search_custom_id = CRM_Utils_Array::value('search_custom_id', $params); $savedSearch->save(); $ssId = $savedSearch->id; if (!$ssId) { return NULL; } $smartGroupId = NULL; if (!empty($params['saved_search_id'])) { $smartGroupId = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Group', $ssId, 'id', 'saved_search_id'); } else { //create group only when new saved search. $groupParams = array('title' => "Hidden Smart Group {$ssId}", 'is_active' => CRM_Utils_Array::value('is_active', $params, 1), 'is_hidden' => CRM_Utils_Array::value('is_hidden', $params, 1), 'group_type' => CRM_Utils_Array::value('group_type', $params), 'visibility' => CRM_Utils_Array::value('visibility', $params), 'saved_search_id' => $ssId); $smartGroup = self::create($groupParams); $smartGroupId = $smartGroup->id; } return array($smartGroupId, $ssId); }