Skip to content

Commit

Permalink
Cleanup search classes to use enumerators instead of hardcoded values
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Jan 28, 2018
1 parent 56d3977 commit fb13128
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 102 deletions.
10 changes: 9 additions & 1 deletion CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ class CRM_Contact_BAO_Query {
// There is no 4,
MODE_MEMBER = 8,
MODE_EVENT = 16,
// No 32, no 64.
MODE_CONTACTSRELATED = 32,
// no 64.
MODE_GRANT = 128,
MODE_PLEDGEBANK = 256,
MODE_PLEDGE = 512,
Expand All @@ -67,6 +68,13 @@ class CRM_Contact_BAO_Query {
MODE_MAILING = 16384,
MODE_ALL = 17407;

/**
* Constants for search operators
*/
const
SEARCH_OPERATOR_AND = 'AND',
SEARCH_OPERATOR_OR = 'OR';

/**
* The default set of return properties.
*
Expand Down
98 changes: 43 additions & 55 deletions CRM/Contact/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ public static function isSearchContext($context) {
public static function setModeValues() {
if (!self::$_modeValues) {
self::$_modeValues = array(
1 => array(
CRM_Contact_BAO_Query::MODE_CONTACTS => array(
'selectorName' => self::$_selectorName,
'selectorLabel' => ts('Contacts'),
'taskFile' => 'CRM/Contact/Form/Search/ResultTasks.tpl',
Expand All @@ -210,7 +210,7 @@ public static function setModeValues() {
'resultContext' => NULL,
'taskClassName' => 'CRM_Contact_Task',
),
2 => array(
CRM_Contact_BAO_Query::MODE_CONTRIBUTE => array(
'selectorName' => 'CRM_Contribute_Selector_Search',
'selectorLabel' => ts('Contributions'),
'taskFile' => 'CRM/common/searchResultTasks.tpl',
Expand All @@ -219,7 +219,7 @@ public static function setModeValues() {
'resultContext' => 'Search',
'taskClassName' => 'CRM_Contribute_Task',
),
3 => array(
CRM_Contact_BAO_Query::MODE_EVENT => array(
'selectorName' => 'CRM_Event_Selector_Search',
'selectorLabel' => ts('Event Participants'),
'taskFile' => 'CRM/common/searchResultTasks.tpl',
Expand All @@ -228,7 +228,7 @@ public static function setModeValues() {
'resultContext' => 'Search',
'taskClassName' => 'CRM_Event_Task',
),
4 => array(
CRM_Contact_BAO_Query::MODE_ACTIVITY => array(
'selectorName' => 'CRM_Activity_Selector_Search',
'selectorLabel' => ts('Activities'),
'taskFile' => 'CRM/common/searchResultTasks.tpl',
Expand All @@ -237,7 +237,7 @@ public static function setModeValues() {
'resultContext' => 'Search',
'taskClassName' => 'CRM_Activity_Task',
),
5 => array(
CRM_Contact_BAO_Query::MODE_MEMBER => array(
'selectorName' => 'CRM_Member_Selector_Search',
'selectorLabel' => ts('Memberships'),
'taskFile' => "CRM/common/searchResultTasks.tpl",
Expand All @@ -246,7 +246,7 @@ public static function setModeValues() {
'resultContext' => 'Search',
'taskClassName' => 'CRM_Member_Task',
),
6 => array(
CRM_Contact_BAO_Query::MODE_CASE => array(
'selectorName' => 'CRM_Case_Selector_Search',
'selectorLabel' => ts('Cases'),
'taskFile' => "CRM/common/searchResultTasks.tpl",
Expand All @@ -255,7 +255,7 @@ public static function setModeValues() {
'resultContext' => 'Search',
'taskClassName' => 'CRM_Case_Task',
),
7 => array(
CRM_Contact_BAO_Query::MODE_CONTACTSRELATED => array(
'selectorName' => self::$_selectorName,
'selectorLabel' => ts('Related Contacts'),
'taskFile' => 'CRM/Contact/Form/Search/ResultTasks.tpl',
Expand All @@ -264,7 +264,7 @@ public static function setModeValues() {
'resultContext' => NULL,
'taskClassName' => 'CRM_Contact_Task',
),
8 => array(
CRM_Contact_BAO_Query::MODE_MAILING => array(
'selectorName' => 'CRM_Mailing_Selector_Search',
'selectorLabel' => ts('Mailings'),
'taskFile' => "CRM/common/searchResultTasks.tpl",
Expand All @@ -282,11 +282,11 @@ public static function setModeValues() {
*
* @return mixed
*/
public static function getModeValue($mode = 1) {
public static function getModeValue($mode = CRM_Contact_BAO_Query::MODE_CONTACTS) {
self::setModeValues();

if (!array_key_exists($mode, self::$_modeValues)) {
$mode = 1;
$mode = CRM_Contact_BAO_Query::MODE_CONTACTS;
}

return self::$_modeValues[$mode];
Expand All @@ -298,25 +298,36 @@ public static function getModeValue($mode = 1) {
public static function getModeSelect() {
self::setModeValues();

$select = array();
$componentModes = array();
foreach (self::$_modeValues as $id => & $value) {
$select[$id] = $value['selectorLabel'];
$componentModes[$id] = $value['selectorLabel'];
}

// unset contributions or participants if user does not have
// permission on them
$enabledComponents = CRM_Core_Component::getEnabledComponents();

// unset disabled components
if (!array_key_exists('CiviMail', $enabledComponents)) {
unset($componentModes[CRM_Contact_BAO_Query::MODE_MAILING]);
}

// unset contributions or participants if user does not have permission on them
if (!CRM_Core_Permission::access('CiviContribute')) {
unset($select['2']);
unset($componentModes[CRM_Contact_BAO_Query::MODE_CONTRIBUTE]);
}

if (!CRM_Core_Permission::access('CiviEvent')) {
unset($select['3']);
unset($componentModes[CRM_Contact_BAO_Query::MODE_EVENT]);
}

if (!CRM_Core_Permission::access('CiviMember')) {
unset($componentModes[CRM_Contact_BAO_Query::MODE_MEMBER]);
}

if (!CRM_Core_Permission::check('view all activities')) {
unset($select['4']);
unset($componentModes[CRM_Contact_BAO_Query::MODE_ACTIVITY]);
}
return $select;

return $componentModes;
}

/**
Expand All @@ -326,25 +337,15 @@ public static function getModeSelect() {
*/
public function buildTaskList() {
if ($this->_context !== 'amtg') {
$permission = CRM_Core_Permission::getPermission();

if ($this->_componentMode == 1 || $this->_componentMode == 7) {
$this->_taskList += CRM_Contact_Task::permissionedTaskTitles($permission,
CRM_Utils_Array::value('deleted_contacts', $this->_formValues)
);
}
else {
$className = $this->_modeValue['taskClassName'];
$this->_taskList += $className::permissionedTaskTitles($permission, FALSE);
}

// Only offer the "Update Smart Group" task if a smart group/saved search is already in play
if (isset($this->_ssID) && $permission == CRM_Core_Permission::EDIT) {
$this->_taskList += CRM_Contact_Task::optionalTaskTitle();
$taskParams['deletedContacts'] = FALSE;
if ($this->_componentMode == CRM_Contact_BAO_Query::MODE_CONTACTS || $this->_componentMode == CRM_Contact_BAO_Query::MODE_CONTACTSRELATED) {
$taskParams['deletedContacts'] = CRM_Utils_Array::value('deleted_contacts', $this->_formValues);
}
$className = $this->_modeValue['taskClassName'];
$taskParams['ssID'] = isset($this->_ssID) ? $this->_ssID : NULL;
$this->_taskList += $className::permissionedTaskTitles(CRM_Core_Permission::getPermission(), $taskParams);
}

asort($this->_taskList);
return $this->_taskList;
}

Expand All @@ -357,24 +358,11 @@ public function buildQuickForm() {
// jsTree is needed for tags popup
->addScriptFile('civicrm', 'packages/jquery/plugins/jstree/jquery.jstree.js', 0, 'html-header', FALSE)
->addStyleFile('civicrm', 'packages/jquery/plugins/jstree/themes/default/style.css', 0, 'html-header');
$permission = CRM_Core_Permission::getPermission();

// some tasks.. what do we want to do with the selected contacts ?
$tasks = array();
if ($this->_componentMode == 1 || $this->_componentMode == 7) {
$tasks += CRM_Contact_Task::permissionedTaskTitles($permission,
CRM_Utils_Array::value('deleted_contacts', $this->_formValues)
);
}
else {
$className = $this->_modeValue['taskClassName'];
$tasks += $className::permissionedTaskTitles($permission, FALSE);
}
$this->_taskList = $this->buildTaskList();

if (isset($this->_ssID)) {
if ($permission == CRM_Core_Permission::EDIT) {
$tasks = $tasks + CRM_Contact_Task::optionalTaskTitle();
}

$search_custom_id
= CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_SavedSearch', $this->_ssID, 'search_custom_id');

Expand Down Expand Up @@ -464,7 +452,7 @@ public function buildQuickForm() {
'class' => 'crm-form-submit',
)
);
$this->add('hidden', 'task', CRM_Contact_Task::GROUP_CONTACTS);
$this->add('hidden', 'task', CRM_Contact_Task::GROUP_ADD);
$selectedRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked'));
$allRowsRadio = $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all');
$this->assign('ts_sel_id', $selectedRowsRadio->_attributes['id']);
Expand All @@ -474,7 +462,7 @@ public function buildQuickForm() {
$selectedContactIds = array();
$qfKeyParam = CRM_Utils_Array::value('qfKey', $this->_formValues);
// We use ajax to handle selections only if the search results component_mode is set to "contacts"
if ($qfKeyParam && ($this->get('component_mode') <= 1 || $this->get('component_mode') == 7)) {
if ($qfKeyParam && ($this->get('component_mode') <= CRM_Contact_BAO_Query::MODE_CONTACTS || $this->get('component_mode') == CRM_Contact_BAO_Query::MODE_CONTACTSRELATED)) {
$this->addClass('crm-ajax-selection-form');
$qfKeyParam = "civicrm search {$qfKeyParam}";
$selectedContactIdsArr = CRM_Core_BAO_PrevNextCache::getSelection($qfKeyParam);
Expand Down Expand Up @@ -515,8 +503,8 @@ public function preProcess() {
$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');
$this->_componentMode = CRM_Utils_Request::retrieve('component_mode', 'Positive', $this, FALSE, CRM_Contact_BAO_Query::MODE_CONTACTS, $_REQUEST);
$this->_operator = CRM_Utils_Request::retrieve('operator', 'String', $this, FALSE, CRM_Contact_BAO_Query::SEARCH_OPERATOR_AND, 'REQUEST');

/**
* set the button names
Expand Down Expand Up @@ -648,9 +636,9 @@ public function preProcess() {
$this->assign('id',
CRM_Utils_Array::value('uf_group_id', $this->_formValues)
);
$operator = CRM_Utils_Array::value('operator', $this->_formValues, 'AND');
$operator = CRM_Utils_Array::value('operator', $this->_formValues, CRM_Contact_BAO_Query::SEARCH_OPERATOR_AND);
$this->set('queryOperator', $operator);
if ($operator == 'OR') {
if ($operator == CRM_Contact_BAO_Query::SEARCH_OPERATOR_OR) {
$this->assign('operator', ts('OR'));
}
else {
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/Form/Search/Advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public function setDefaultValues() {
$this->normalizeDefaultValues($defaults);

if ($this->_context === 'amtg') {
$defaults['task'] = CRM_Contact_Task::GROUP_CONTACTS;
$defaults['task'] = CRM_Contact_Task::GROUP_ADD;
}

return $defaults;
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/Form/Search/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public function setDefaultValues() {
}

if ($this->_context === 'amtg') {
$defaults['task'] = CRM_Contact_Task::GROUP_CONTACTS;
$defaults['task'] = CRM_Contact_Task::GROUP_ADD;
}

if ($this->_context === 'smog') {
Expand Down
30 changes: 2 additions & 28 deletions CRM/Contact/Form/Search/Criteria.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,32 +158,6 @@ public static function basic(&$form) {
);

$componentModes = CRM_Contact_Form_Search::getModeSelect();
$enabledComponents = CRM_Core_Component::getEnabledComponents();

// unset disabled components that must should have been enabled
// to the option be viable
if (!array_key_exists('CiviMail', $enabledComponents)) {
unset($componentModes['8']);
}

// unset contributions or participants if user does not have
// permission on them
if (!CRM_Core_Permission::access('CiviContribute')) {
unset($componentModes['2']);
}

if (!CRM_Core_Permission::access('CiviEvent')) {
unset($componentModes['3']);
}

if (!CRM_Core_Permission::access('CiviMember')) {
unset($componentModes['5']);
}

if (!CRM_Core_Permission::check('view all activities')) {
unset($componentModes['4']);
}

if (count($componentModes) > 1) {
$form->add('select',
'component_mode',
Expand All @@ -198,8 +172,8 @@ public static function basic(&$form) {
'operator',
ts('Search Operator'),
array(
'AND' => ts('AND'),
'OR' => ts('OR'),
CRM_Contact_BAO_Query::SEARCH_OPERATOR_AND => ts('AND'),
CRM_Contact_BAO_Query::SEARCH_OPERATOR_OR => ts('OR'),
),
array('allowClear' => FALSE)
);
Expand Down
14 changes: 7 additions & 7 deletions CRM/Contact/Form/Task/SaveSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ public function preProcess() {
$values = $this->controller->exportValues('Basic');
}

// Get Task name
$modeValue = CRM_Contact_Form_Search::getModeValue($values['component_mode']);
$className = $modeValue['taskClassName'];
$taskList = $className::taskTitles();
$this->_task = CRM_Utils_Array::value('task', $values);
$crmContactTaskTasks = CRM_Contact_Task::taskTitles();
$this->assign('taskName', CRM_Utils_Array::value($this->_task, $crmContactTaskTasks));
$this->assign('taskName', CRM_Utils_Array::value($this->_task, $taskList));
}

/**
Expand Down Expand Up @@ -208,12 +211,9 @@ public function postProcess() {
$params = array();
$params['title'] = $formValues['title'];
$params['description'] = $formValues['description'];
if (isset($formValues['group_type']) &&
is_array($formValues['group_type'])
) {
if (isset($formValues['group_type']) && is_array($formValues['group_type']) && count($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;
array_keys($formValues['group_type'])) . CRM_Core_DAO::VALUE_SEPARATOR;
}
else {
$params['group_type'] = '';
Expand Down
13 changes: 4 additions & 9 deletions CRM/Contact/StateMachine/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,10 @@ public function taskName($controller, $formName = 'Search') {
}
$this->_controller->set('task', $value);

if ($value) {
$componentMode = $this->_controller->get('component_mode');
$modeValue = CRM_Contact_Form_Search::getModeValue($componentMode);
$taskClassName = $modeValue['taskClassName'];
return $taskClassName::getTask($value);
}
else {
return CRM_Contact_Task::getTask($value);
}
$componentMode = $this->_controller->get('component_mode');
$modeValue = CRM_Contact_Form_Search::getModeValue($componentMode);
$taskClassName = $modeValue['taskClassName'];
return $taskClassName::getTask($value);
}

/**
Expand Down

0 comments on commit fb13128

Please sign in to comment.