From 8d36b8012ae0da4e5da5bdb26d082f1aab489104 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Mon, 2 Jun 2014 14:12:07 +0100 Subject: [PATCH] CRM-12872 - Centralize search row selector code --- CRM/Activity/Form/Search.php | 21 +--------- CRM/Campaign/Form/Search.php | 29 +------------ CRM/Case/Form/Search.php | 24 +---------- CRM/Contribute/Form/Search.php | 24 +---------- CRM/Core/Form/Search.php | 18 ++++++++ CRM/Event/Form/Search.php | 27 +----------- CRM/Grant/Form/Search.php | 21 +--------- CRM/Member/Form/Search.php | 19 +-------- CRM/Pledge/Form/Search.php | 19 +-------- js/Common.js | 2 +- js/crm.searchForm.js | 41 +++++++++---------- .../CRM/Contact/Form/Search/ResultTasks.tpl | 8 ---- templates/CRM/Contact/Form/Selector.tpl | 2 +- templates/CRM/common/searchResultTasks.tpl | 10 ----- 14 files changed, 48 insertions(+), 217 deletions(-) diff --git a/CRM/Activity/Form/Search.php b/CRM/Activity/Form/Search.php index b00e600880..0e4b018b35 100644 --- a/CRM/Activity/Form/Search.php +++ b/CRM/Activity/Form/Search.php @@ -180,34 +180,15 @@ class CRM_Activity_Form_Search extends CRM_Core_Form_Search { CRM_Activity_BAO_Query::buildSearchForm($this); - /* - * add form checkboxes for each row. This is needed out here to conform to QF protocol - * of all elements being declared in builQuickForm - */ - $rows = $this->get('rows'); if (is_array($rows)) { if (!$this->_single) { - $this->addElement('checkbox', 'toggleSelect', NULL, NULL, - array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows') - ); - foreach ($rows as $row) { - $this->addElement('checkbox', $row['checkbox'], - NULL, NULL, - array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row') - ); - } + $this->addRowSelectors($rows); } $permission = CRM_Core_Permission::getPermission(); $this->addTaskMenu(CRM_Activity_Task::permissionedTaskTitles($permission)); - - // need to perform tasks on all or selected items ? using radio_ts(task selection) for it - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked')); - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', - array('class' => 'select-rows', 'onchange' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );") - ); } } diff --git a/CRM/Campaign/Form/Search.php b/CRM/Campaign/Form/Search.php index 5765f582da..43ef56d6c7 100755 --- a/CRM/Campaign/Form/Search.php +++ b/CRM/Campaign/Form/Search.php @@ -205,26 +205,12 @@ class CRM_Campaign_Form_Search extends CRM_Core_Form_Search { //build the search form. CRM_Campaign_BAO_Query::buildSearchForm($this); - /* - * add form checkboxes for each row. This is needed out here to conform to QF protocol - * of all elements being declared in builQuickForm - */ - - $rows = $this->get('rows'); if (is_array($rows)) { if (!$this->_single) { - $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows')); - foreach ($rows as $row) { - $this->addElement('checkbox', $row['checkbox'], - NULL, NULL, - array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row') - ); - } + $this->addRowSelectors($rows); } - $total = $cancel = 0; - $permission = CRM_Core_Permission::getPermission(); $allTasks = CRM_Campaign_Task::permissionedTaskTitles($permission); @@ -250,18 +236,7 @@ class CRM_Campaign_Form_Search extends CRM_Core_Form_Search { )); } - $this->add('select', 'task', ts('Actions:') . ' ', array('' => ts('- actions -')) + $taskValue); - - $this->add('submit', $this->_actionButtonName, ts('Go'), - array( - 'class' => 'form-submit', - 'id' => 'Go', - ) - ); - - // need to perform tasks on all or selected items ? using radio_ts(task selection) for it - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked')); - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );")); + $this->addTaskMenu($taskValue); } } diff --git a/CRM/Case/Form/Search.php b/CRM/Case/Form/Search.php index 045a8fc6f0..c2b74f8f1f 100644 --- a/CRM/Case/Form/Search.php +++ b/CRM/Case/Form/Search.php @@ -188,30 +188,12 @@ class CRM_Case_Form_Search extends CRM_Core_Form_Search { CRM_Case_BAO_Query::buildSearchForm($this); - /* - * add form checkboxes for each row. This is needed out here to conform to QF protocol - * of all elements being declared in builQuickForm - */ $rows = $this->get('rows'); if (is_array($rows)) { if (!$this->_single) { - $this->addElement('checkbox', - 'toggleSelect', - NULL, - NULL, - array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows') - ); - - foreach ($rows as $row) { - $this->addElement('checkbox', $row['checkbox'], - NULL, NULL, - array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row') - ); - } + $this->addRowSelectors($rows); } - $total = $cancel = 0; - $permission = CRM_Core_Permission::getPermission(); $tasks = CRM_Case_Task::permissionedTaskTitles($permission); @@ -224,10 +206,6 @@ class CRM_Case_Form_Search extends CRM_Core_Form_Search { } $this->addTaskMenu($tasks); - - // need to perform tasks on all or selected items ? using radio_ts(task selection) for it - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked')); - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows')); } } diff --git a/CRM/Contribute/Form/Search.php b/CRM/Contribute/Form/Search.php index 12d1c078bf..5e18535aee 100644 --- a/CRM/Contribute/Form/Search.php +++ b/CRM/Contribute/Form/Search.php @@ -211,30 +211,12 @@ class CRM_Contribute_Form_Search extends CRM_Core_Form_Search { CRM_Contribute_BAO_Query::buildSearchForm($this); - /* - * add form checkboxes for each row. This is needed out here to conform to QF protocol - * of all elements being declared in builQuickForm - */ - $rows = $this->get('rows'); if (is_array($rows)) { if (!$this->_single) { - $this->addElement('checkbox', - 'toggleSelect', - NULL, - NULL, - array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows') - ); - foreach ($rows as $row) { - $this->addElement('checkbox', $row['checkbox'], - NULL, NULL, - array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row') - ); - } + $this->addRowSelectors($rows); } - $total = $cancel = 0; - $permission = CRM_Core_Permission::getPermission(); $queryParams = $this->get('queryParams'); @@ -244,10 +226,6 @@ class CRM_Contribute_Form_Search extends CRM_Core_Form_Search { } $tasks = CRM_Contribute_Task::permissionedTaskTitles($permission, $softCreditFiltering); $this->addTaskMenu($tasks); - - // need to perform tasks on all or selected items ? using radio_ts(task selection) for it - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked')); - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );")); } } diff --git a/CRM/Core/Form/Search.php b/CRM/Core/Form/Search.php index 7796d1212f..4f298d8c29 100644 --- a/CRM/Core/Form/Search.php +++ b/CRM/Core/Form/Search.php @@ -98,6 +98,18 @@ class CRM_Core_Form_Search extends CRM_Core_Form { 'isDefault' => TRUE, ), )); + + $this->setAttribute('class', 'crm-search-form'); + } + + /** + * Add checkboxes for each row plus a master checkbox + */ + function addRowSelectors($rows) { + $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('class' => 'select-rows')); + foreach ($rows as $row) { + $this->addElement('checkbox', $row['checkbox'], NULL, NULL, array('class' => 'select-row')); + } } /** @@ -109,6 +121,12 @@ class CRM_Core_Form_Search extends CRM_Core_Form { $tasks = array('' => ts('Actions')) + $tasks; $this->add('select', 'task', NULL, $tasks, FALSE, array('class' => 'crm-select2 crm-action-menu huge crm-search-result-actions')); $this->add('submit', $this->_actionButtonName, ts('Go'), array('class' => 'hiddenElement crm-search-go-button')); + + // Radio to choose "All items" or "Selected items only" + $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']); + $this->assign('ts_all_id', $allRowsRadio->_attributes['id']); } } } diff --git a/CRM/Event/Form/Search.php b/CRM/Event/Form/Search.php index 8ed56a7747..1e3aafd0ad 100644 --- a/CRM/Event/Form/Search.php +++ b/CRM/Event/Form/Search.php @@ -181,29 +181,14 @@ class CRM_Event_Form_Search extends CRM_Core_Form_Search { CRM_Event_BAO_Query::buildSearchForm($this); - /* - * add form checkboxes for each row. This is needed out here to conform to QF protocol - * of all elements being declared in builQuickForm - */ $rows = $this->get('rows'); if (is_array($rows)) { $lineItems = $eventIds = array(); if (!$this->_single) { - $this->addElement('checkbox', - 'toggleSelect', - NULL, - NULL, - array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows') - ); + $this->addRowSelectors($rows); } foreach ($rows as $row) { $eventIds[$row['event_id']] = $row['event_id']; - if (!$this->_single) { - $this->addElement('checkbox', $row['checkbox'], - NULL, NULL, - array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row') - ); - } if (CRM_Event_BAO_Event::usesPriceSet($row['event_id'])) { // add line item details if applicable $lineItems[$row['participant_id']] = CRM_Price_BAO_LineItem::getLineItems($row['participant_id']); @@ -237,8 +222,6 @@ class CRM_Event_Form_Search extends CRM_Core_Form_Search { $this->assign('participantCount', $participantCount); $this->assign('lineItems', $lineItems); - $total = $cancel = 0; - $permission = CRM_Core_Permission::getPermission(); $tasks = CRM_Event_Task::permissionedTaskTitles($permission); @@ -256,14 +239,6 @@ class CRM_Event_Form_Search extends CRM_Core_Form_Search { } $this->addTaskMenu($tasks); - - // need to perform tasks on all or selected items ? using radio_ts(task selection) for it - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', - array('checked' => 'checked') - ); - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', - array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );") - ); } } diff --git a/CRM/Grant/Form/Search.php b/CRM/Grant/Form/Search.php index e81ad0cbf3..2cbfa3b428 100644 --- a/CRM/Grant/Form/Search.php +++ b/CRM/Grant/Form/Search.php @@ -173,34 +173,15 @@ class CRM_Grant_Form_Search extends CRM_Core_Form_Search { CRM_Grant_BAO_Query::buildSearchForm($this); - /* - * add form checkboxes for each row. This is needed out here to conform to QF protocol - * of all elements being declared in builQuickForm - */ - - $rows = $this->get('rows'); if (is_array($rows)) { if (!$this->_single) { - $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('onchange' => "toggleTaskAction( true );", 'class' => 'select-rows')); - foreach ($rows as $row) { - $this->addElement('checkbox', CRM_Utils_Array::value('checkbox', $row), - NULL, NULL, - array('onclick' => " toggleTaskAction( true );", 'class' => 'select-row') - ); - $grant_id = $row['grant_id']; - } + $this->addRowSelectors($rows); } - $total = $cancel = 0; - $permission = CRM_Core_Permission::getPermission(); $this->addTaskMenu(CRM_Grant_Task::permissionedTaskTitles($permission)); - - // need to perform tasks on all or selected items ? using radio_ts(task selection) for it - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked')); - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows', 'onchange' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );")); } } diff --git a/CRM/Member/Form/Search.php b/CRM/Member/Form/Search.php index 0084cae0cd..e7ad0667bc 100644 --- a/CRM/Member/Form/Search.php +++ b/CRM/Member/Form/Search.php @@ -170,32 +170,15 @@ class CRM_Member_Form_Search extends CRM_Core_Form_Search { CRM_Member_BAO_Query::buildSearchForm($this); - /* - * add form checkboxes for each row. This is needed out here to conform to QF protocol - * of all elements being declared in builQuickForm - */ - $rows = $this->get('rows'); if (is_array($rows)) { if (!$this->_single) { - $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows')); - foreach ($rows as $row) { - $this->addElement('checkbox', $row['checkbox'], - NULL, NULL, - array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row') - ); - } + $this->addRowSelectors($rows); } - $total = $cancel = 0; - $permission = CRM_Core_Permission::getPermission(); $this->addTaskMenu(CRM_Member_Task::permissionedTaskTitles($permission)); - - // need to perform tasks on all or selected items ? using radio_ts(task selection) for it - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked')); - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );")); } } diff --git a/CRM/Pledge/Form/Search.php b/CRM/Pledge/Form/Search.php index d36dd279af..fbb67488c2 100644 --- a/CRM/Pledge/Form/Search.php +++ b/CRM/Pledge/Form/Search.php @@ -169,32 +169,15 @@ class CRM_Pledge_Form_Search extends CRM_Core_Form_Search { CRM_Pledge_BAO_Query::buildSearchForm($this); - /* - * add form checkboxes for each row. This is needed out here to conform to QF protocol - * of all elements being declared in builQuickForm - */ $rows = $this->get('rows'); if (is_array($rows)) { if (!$this->_single) { - $this->addElement('checkbox', 'toggleSelect', NULL, NULL, array('onclick' => "toggleTaskAction( true );", 'class' => 'select-rows')); - - foreach ($rows as $row) { - $this->addElement('checkbox', $row['checkbox'], - NULL, NULL, - array('onclick' => "toggleTaskAction( true );", 'class' => 'select-row') - ); - } + $this->addRowSelectors($rows); } - $total = $cancel = 0; - $permission = CRM_Core_Permission::getPermission(); $this->addTaskMenu(CRM_Pledge_Task::permissionedTaskTitles($permission)); - - // need to perform tasks on all or selected items ? using radio_ts(task selection) for it - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_sel', array('checked' => 'checked')); - $this->addElement('radio', 'radio_ts', NULL, '', 'ts_all', array('class' => 'select-rows', 'onclick' => $this->getName() . ".toggleSelect.checked = false; toggleTaskAction( true );")); } } diff --git a/js/Common.js b/js/Common.js index 68b06ce38d..ee438daa50 100644 --- a/js/Common.js +++ b/js/Common.js @@ -253,7 +253,7 @@ CRM.validate = CRM.validate || { } }); return isDirty; - } + }; /** * Wrapper for select2 initialization function; supplies defaults diff --git a/js/crm.searchForm.js b/js/crm.searchForm.js index 7f8fc3c310..04c002fc54 100644 --- a/js/crm.searchForm.js +++ b/js/crm.searchForm.js @@ -11,28 +11,25 @@ function countSelectedCheckboxes(fldPrefix, form) { return fieldCount; } -/** - * Function to enable task action select - */ -function toggleTaskAction(status) { - var radio_ts = document.getElementsByName('radio_ts'); - if (!radio_ts[1]) { - radio_ts[0].checked = true; - } - if (radio_ts[0].checked || radio_ts[1].checked) { - status = true; - } +(function($, _) { + "use strict"; + var form = 'form.crm-search-form'; - var formElements = ['task', 'Go', 'Print']; - for (var i = 0; i < formElements.length; i++) { - var element = document.getElementById(formElements[i]); - if (element) { - if (status) { - element.disabled = false; - } - else { - element.disabled = true; - } + function toggleTaskMenu() { + var $menu = $('select#task', form); + $menu.val('').select2('val', ''); + if ($('[name=radio_ts][value=ts_all], .select-row', form).filter(':checked').length) { + $menu.prop('disabled', false).select2('enable'); + } else { + $menu.prop('disabled', true).select2('disable'); } } -} + + $('#crm-container') + .on('change', '[name=radio_ts], .select-row', toggleTaskMenu) + .on('crmLoad', toggleTaskMenu) + .on('change', 'select#task', function() { + $(this).siblings('input[type=submit]').click(); + }); + +})(CRM.$, CRM._); diff --git a/templates/CRM/Contact/Form/Search/ResultTasks.tpl b/templates/CRM/Contact/Form/Search/ResultTasks.tpl index c6f9811868..3db10ead97 100644 --- a/templates/CRM/Contact/Form/Search/ResultTasks.tpl +++ b/templates/CRM/Contact/Form/Search/ResultTasks.tpl @@ -91,11 +91,3 @@ - -{literal} - -{/literal} diff --git a/templates/CRM/Contact/Form/Selector.tpl b/templates/CRM/Contact/Form/Selector.tpl index 393f921071..51e21fbc48 100644 --- a/templates/CRM/Contact/Form/Selector.tpl +++ b/templates/CRM/Contact/Form/Selector.tpl @@ -174,7 +174,7 @@ function countSelections(obj) { else { cj('span', label).html(obj.getCount); } - toggleTaskAction(obj.getCount); + //toggleTaskAction(obj.getCount); } } function toggleContactSelection(name, qfKey, selection) { diff --git a/templates/CRM/common/searchResultTasks.tpl b/templates/CRM/common/searchResultTasks.tpl index c72a13646d..3fd7569153 100644 --- a/templates/CRM/common/searchResultTasks.tpl +++ b/templates/CRM/common/searchResultTasks.tpl @@ -76,13 +76,3 @@ -{literal} - -{/literal} -- 2.25.1