From: jitendrapurohit Date: Thu, 4 Jun 2015 12:27:35 +0000 (+0530) Subject: CRM-16310 - fix ordering of options in dataTable X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=46227b8d4a61d6f3b3bc6460e662a84d935ac236;p=civicrm-core.git CRM-16310 - fix ordering of options in dataTable --- diff --git a/CRM/Core/BAO/CustomOption.php b/CRM/Core/BAO/CustomOption.php index 00a2f9c0cf..5e0608edc1 100644 --- a/CRM/Core/BAO/CustomOption.php +++ b/CRM/Core/BAO/CustomOption.php @@ -161,7 +161,7 @@ class CRM_Core_BAO_CustomOption { $dao = CRM_Core_DAO::executeQuery($query, $queryParams); $links = CRM_Custom_Page_Option::actionLinks(); - $fields = array('id', 'label', 'value', 'weight'); + $fields = array('id', 'label', 'value'); $config = CRM_Core_Config::singleton(); while ($dao->fetch()) { $options[$dao->id] = array(); @@ -214,14 +214,6 @@ class CRM_Core_BAO_CustomOption { $dao->id ); } - // Add order changing widget to selector - $returnURL = CRM_Utils_System::url('civicrm/admin/custom/group/field/option', - "reset=1&action=browse&gid={$params['gid']}&fid={$params['fid']}" - ); - $filter = "option_group_id = {$optionGroupID}"; - CRM_Utils_Weight::addOrder($options, 'CRM_Core_DAO_OptionValue', - 'id', $returnURL, $filter - ); return $options; } diff --git a/CRM/Core/xml/Menu/Custom.xml b/CRM/Core/xml/Menu/Custom.xml index b58c6bcfb5..04a38ae614 100644 --- a/CRM/Core/xml/Menu/Custom.xml +++ b/CRM/Core/xml/Menu/Custom.xml @@ -10,4 +10,9 @@ CRM_Custom_Page_AJAX::getOptionList access CiviCRM + + civicrm/ajax/reorder + CRM_Custom_Page_AJAX::fixOrdering + access CiviCRM + diff --git a/CRM/Custom/Page/AJAX.php b/CRM/Custom/Page/AJAX.php index fac96f931d..1571dce983 100644 --- a/CRM/Custom/Page/AJAX.php +++ b/CRM/Custom/Page/AJAX.php @@ -47,9 +47,8 @@ class CRM_Custom_Page_AJAX { 0 => 'options.label', 1 => 'options.value', 2 => '', - 3 => 'options.weight', + 3 => '', 4 => '', - 5 => '', ); $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer'); @@ -72,7 +71,6 @@ class CRM_Custom_Page_AJAX { 'label', 'value', 'is_default', - 'weight', 'is_active', 'links', 'class', @@ -83,4 +81,42 @@ class CRM_Custom_Page_AJAX { CRM_Utils_System::civiExit(); } + /** + * Fix Ordering of options + * + */ + + public static function fixOrdering() { + $params = $_REQUEST; + + $queryParams = array( + 1 => array($params['start'], 'Integer'), + 2 => array($params['end'], 'Integer'), + 3 => array($params['gid'], 'Integer'), + ); + $dao = "SELECT id FROM civicrm_option_value WHERE weight = %1 AND option_group_id = %3"; + $startid = CRM_Core_DAO::singleValueQuery($dao, $queryParams); + + $dao2 = "SELECT id FROM civicrm_option_value WHERE weight = %2 AND option_group_id = %3"; + $endid = CRM_Core_DAO::singleValueQuery($dao2, $queryParams); + + $query = "UPDATE civicrm_option_value SET weight = %2 WHERE id = $startid"; + CRM_Core_DAO::executeQuery($query, $queryParams); + + // increment or decrement the rest by one + if ($params['start'] < $params['end']) { + $updateRows = "UPDATE civicrm_option_value + SET weight = weight - 1 + WHERE weight > %1 AND weight < %2 AND option_group_id = %3 + OR id = $endid"; + } + else { + $updateRows = "UPDATE civicrm_option_value + SET weight = weight + 1 + WHERE weight < %1 AND weight > %2 AND option_group_id = %3 + OR id = $endid"; + } + CRM_Core_DAO::executeQuery($updateRows, $queryParams); + } + } diff --git a/CRM/Custom/Page/Option.php b/CRM/Custom/Page/Option.php index fb73094c7a..7e6801f62f 100644 --- a/CRM/Custom/Page/Option.php +++ b/CRM/Custom/Page/Option.php @@ -142,6 +142,7 @@ WHERE option_group_id = %1"; CRM_Utils_System::setTitle($newTitle); $this->assign('reusedNames', $reusedNames); } + $this->assign('optionGroupID', $optionGroupID); } /** diff --git a/templates/CRM/Custom/Page/Option.tpl b/templates/CRM/Custom/Page/Option.tpl index 79694dffb0..8c1e91dca8 100644 --- a/templates/CRM/Custom/Page/Option.tpl +++ b/templates/CRM/Custom/Page/Option.tpl @@ -43,7 +43,6 @@ {ts}Label{/ts} {ts}Value{/ts} {ts}Default{/ts} - {ts}Order{/ts} {ts}Enabled?{/ts}     @@ -71,7 +70,6 @@ {sClass:'crm-custom_option-label'}, {sClass:'crm-custom_option-value'}, {sClass:'crm-custom_option-default_value', bSortable:false}, - {sClass:'crm-custom_option-weight'}, {sClass:'crm-custom_option-is_active', bSortable:false}, {sClass:'crm-custom_option-links', bSortable:false}, {sClass:'hiddenElement', bSortable:false} @@ -100,10 +98,14 @@ var id = $('td:last', nRow).text().split(',')[0]; var cl = $('td:last', nRow).text().split(',')[1]; $(nRow).addClass(cl).attr({id: 'OptionValue-' + id}); + $('td:eq(0)', nRow).wrapInner(''); $('td:eq(2)', nRow).addClass('crmf-default_value'); - $('td:eq(3)', nRow).addClass('crmf-weight'); return nRow; }, + "fnDrawCallback": function() { + // FIXME: trigger crmLoad and crmEditable would happen automatically + $('.crm-editable').crmEditable(); + }, "fnServerData": function ( sSource, aoData, fnCallback ) { $.ajax( { @@ -116,6 +118,31 @@ } }); } + + var startPosition; + var endPosition; + var gid = {/literal}'{$optionGroupID}'{literal}; + + $("table.crm-option-selector tbody").sortable({ + cursor: "move", + start:function(event, ui) { + var oSettings = $('table.crm-option-selector').dataTable().fnSettings(); + var index = oSettings._iDisplayStart; + startPosition = index + ui.item.prevAll().length + 1; + }, + update: function(event, ui) { + var oSettings = $('table.crm-option-selector').dataTable().fnSettings(); + var index = oSettings._iDisplayStart; + endPosition = index + ui.item.prevAll().length + 1; + + $.getJSON(CRM.url('civicrm/ajax/reorder'), { + returnFormat:'JSON', + start:startPosition, + end: endPosition, + gid: gid + }) + } + }); });