From 57c9c21755cf2c1ee5ea838f7b5a0b31f63e2897 Mon Sep 17 00:00:00 2001 From: jitendrapurohit Date: Wed, 27 May 2015 12:46:00 +0530 Subject: [PATCH] CRM-16310 - implement pagination on custom field option list page --- CRM/Core/BAO/CustomOption.php | 121 +++++++++++++++++++++++ CRM/Core/xml/Menu/Custom.xml | 7 +- CRM/Custom/Page/AJAX.php | 86 +++++++++++++++++ CRM/Custom/Page/Option.php | 86 ----------------- templates/CRM/Custom/Page/Option.tpl | 139 ++++++++++++++++++--------- 5 files changed, 308 insertions(+), 131 deletions(-) create mode 100644 CRM/Custom/Page/AJAX.php diff --git a/CRM/Core/BAO/CustomOption.php b/CRM/Core/BAO/CustomOption.php index 88c06b122e..00a2f9c0cf 100644 --- a/CRM/Core/BAO/CustomOption.php +++ b/CRM/Core/BAO/CustomOption.php @@ -105,6 +105,127 @@ class CRM_Core_BAO_CustomOption { return $options; } + /** + * wrapper for ajax option selector. + * + * @param array $params + * Associated array for params record id. + * + * @return array + * associated array of option list + * -rp = rowcount + * -page= offset + */ + static public function getOptionListSelector(&$params) { + + $options = array(); + + //get the default value from custom fields + $customFieldBAO = new CRM_Core_BAO_CustomField(); + $customFieldBAO->id = $params['fid']; + if ($customFieldBAO->find(TRUE)) { + $defaultValue = $customFieldBAO->default_value; + $fieldHtmlType = $customFieldBAO->html_type; + } + else { + CRM_Core_Error::fatal(); + } + $defVal = explode(CRM_Core_DAO::VALUE_SEPARATOR, + substr($defaultValue, 1, -1) + ); + + // format the params + $params['offset'] = ($params['page'] - 1) * $params['rp']; + $params['rowCount'] = $params['rp']; + $params['sort'] = CRM_Utils_Array::value('sortBy', $params); + + $field = CRM_Core_BAO_CustomField::getFieldObject($params['fid']); + + // get the option group id + $optionGroupID = $field->option_group_id; + if (!$optionGroupID) { + return $options; + } + $queryParams = array(1 => array($optionGroupID, 'Integer')); + $total = "SELECT COUNT(*) FROM civicrm_option_value WHERE option_group_id = %1"; + $params['total'] = CRM_Core_DAO::singleValueQuery($total, $queryParams); + + $limit = " LIMIT {$params['offset']}, {$params['rowCount']} "; + + $orderBy = ' ORDER BY options.weight asc'; + if (!empty($params['sort'])) { + $orderBy = ' ORDER BY ' . CRM_Utils_Type::escape($params['sort'], 'String'); + } + + $query = "SELECT * FROM civicrm_option_value as options WHERE option_group_id = %1 {$orderBy} {$limit}"; + $dao = CRM_Core_DAO::executeQuery($query, $queryParams); + $links = CRM_Custom_Page_Option::actionLinks(); + + $fields = array('id', 'label', 'value', 'weight'); + $config = CRM_Core_Config::singleton(); + while ($dao->fetch()) { + $options[$dao->id] = array(); + foreach ($fields as $k) { + $options[$dao->id][$k] = $dao->$k; + } + $action = array_sum(array_keys($links)); + $class = 'crm-entity'; + // update enable/disable links depending on custom_field properties. + if ($dao->is_active) { + $action -= CRM_Core_Action::ENABLE; + } + else { + $class .= ' disabled'; + $action -= CRM_Core_Action::DISABLE; + } + if ($fieldHtmlType == 'CheckBox' || + $fieldHtmlType == 'AdvMulti-Select' || + $fieldHtmlType == 'Multi-Select' + ) { + if (in_array($dao->value, $defVal)) { + $options[$dao->id]['is_default'] = ''; + } + else { + $options[$dao->id]['is_default'] = ''; + } + } + else { + if ($defaultValue == $dao->value) { + $options[$dao->id]['is_default'] = ''; + } + else { + $options[$dao->id]['is_default'] = ''; + } + } + + $options[$dao->id]['class'] = $dao->id . ',' . $class; + $options[$dao->id]['is_active'] = !empty($dao->is_active) ? 'Yes' : 'No'; + $options[$dao->id]['links'] = CRM_Core_Action::formLink($links, + $action, + array( + 'id' => $dao->id, + 'fid' => $params['fid'], + 'gid' => $params['gid'], + ), + ts('more'), + FALSE, + 'customOption.row.actions', + '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; + } + /** * Returns the option label for a custom field with a specific value. Handles all * custom field data and html types diff --git a/CRM/Core/xml/Menu/Custom.xml b/CRM/Core/xml/Menu/Custom.xml index 2ddc99d926..b58c6bcfb5 100644 --- a/CRM/Core/xml/Menu/Custom.xml +++ b/CRM/Core/xml/Menu/Custom.xml @@ -5,4 +5,9 @@ civicrm/custom/add CRM_Custom_Form_CustomData - \ No newline at end of file + + civicrm/ajax/optionlist + CRM_Custom_Page_AJAX::getOptionList + access CiviCRM + + diff --git a/CRM/Custom/Page/AJAX.php b/CRM/Custom/Page/AJAX.php new file mode 100644 index 0000000000..fac96f931d --- /dev/null +++ b/CRM/Custom/Page/AJAX.php @@ -0,0 +1,86 @@ + 'options.label', + 1 => 'options.value', + 2 => '', + 3 => 'options.weight', + 4 => '', + 5 => '', + ); + + $sEcho = CRM_Utils_Type::escape($_REQUEST['sEcho'], 'Integer'); + $offset = isset($_REQUEST['iDisplayStart']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayStart'], 'Integer') : 0; + $rowCount = isset($_REQUEST['iDisplayLength']) ? CRM_Utils_Type::escape($_REQUEST['iDisplayLength'], 'Integer') : 25; + $sort = isset($_REQUEST['iSortCol_0']) ? CRM_Utils_Array::value(CRM_Utils_Type::escape($_REQUEST['iSortCol_0'], 'Integer'), $sortMapper) : NULL; + $sortOrder = isset($_REQUEST['sSortDir_0']) ? CRM_Utils_Type::escape($_REQUEST['sSortDir_0'], 'String') : 'asc'; + + if ($sort && $sortOrder) { + $params['sortBy'] = $sort . ' ' . $sortOrder; + } + + $params['page'] = ($offset / $rowCount) + 1; + $params['rp'] = $rowCount; + + $options = CRM_Core_BAO_CustomOption::getOptionListSelector($params); + + $iFilteredTotal = $iTotal = $params['total']; + $selectorElements = array( + 'label', + 'value', + 'is_default', + 'weight', + 'is_active', + 'links', + 'class', + ); + + header('Content-Type: application/json'); + echo CRM_Utils_JSON::encodeDataTableSelector($options, $sEcho, $iTotal, $iFilteredTotal, $selectorElements); + CRM_Utils_System::civiExit(); + } + +} diff --git a/CRM/Custom/Page/Option.php b/CRM/Custom/Page/Option.php index 47e0fa8c3c..fb73094c7a 100644 --- a/CRM/Custom/Page/Option.php +++ b/CRM/Custom/Page/Option.php @@ -114,19 +114,6 @@ class CRM_Custom_Page_Option extends CRM_Core_Page { * @return void */ public function browse() { - //get the default value from custom fields - $customFieldBAO = new CRM_Core_BAO_CustomField(); - $customFieldBAO->id = $this->_fid; - if ($customFieldBAO->find(TRUE)) { - $defaultValue = $customFieldBAO->default_value; - $fieldHtmlType = $customFieldBAO->html_type; - } - else { - CRM_Core_Error::fatal(); - } - $defVal = explode(CRM_Core_DAO::VALUE_SEPARATOR, - substr($defaultValue, 1, -1) - ); // get the option group id $optionGroupID = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_CustomField', @@ -155,79 +142,6 @@ WHERE option_group_id = %1"; CRM_Utils_System::setTitle($newTitle); $this->assign('reusedNames', $reusedNames); } - - $query = " -SELECT * - FROM civicrm_option_value - WHERE option_group_id = %1 -ORDER BY weight, label -"; - $params = array(1 => array($optionGroupID, 'Integer')); - $dao = CRM_Core_DAO::executeQuery($query, $params); - - $customOption = array(); - $fields = array('label', 'value', 'is_active', 'weight'); - $config = CRM_Core_Config::singleton(); - while ($dao->fetch()) { - $customOption[$dao->id] = array(); - foreach ($fields as $field) { - $customOption[$dao->id][$field] = $dao->$field; - } - - $action = array_sum(array_keys($this->actionLinks())); - - // update enable/disable links depending on custom_field properties. - if ($dao->is_active) { - $action -= CRM_Core_Action::ENABLE; - } - else { - $action -= CRM_Core_Action::DISABLE; - } - - if ($fieldHtmlType == 'CheckBox' || - $fieldHtmlType == 'AdvMulti-Select' || - $fieldHtmlType == 'Multi-Select' - ) { - if (in_array($dao->value, $defVal)) { - $customOption[$dao->id]['default_value'] = ''; - } - else { - $customOption[$dao->id]['default_value'] = ''; - } - } - else { - if ($defaultValue == $dao->value) { - $customOption[$dao->id]['default_value'] = ''; - } - else { - $customOption[$dao->id]['default_value'] = ''; - } - } - - $customOption[$dao->id]['action'] = CRM_Core_Action::formLink(self::actionLinks(), - $action, - array( - 'id' => $dao->id, - 'fid' => $this->_fid, - 'gid' => $this->_gid, - ), - ts('more'), - FALSE, - 'customOption.row.actions', - '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={$this->_gid}&fid={$this->_fid}" - ); - $filter = "option_group_id = {$optionGroupID}"; - CRM_Utils_Weight::addOrder($customOption, 'CRM_Core_DAO_OptionValue', - 'id', $returnURL, $filter - ); - $this->assign('customOption', $customOption); } /** diff --git a/templates/CRM/Custom/Page/Option.tpl b/templates/CRM/Custom/Page/Option.tpl index f02fd8a3de..79694dffb0 100644 --- a/templates/CRM/Custom/Page/Option.tpl +++ b/templates/CRM/Custom/Page/Option.tpl @@ -26,54 +26,105 @@ {if $action eq 1 or $action eq 2 or $action eq 4 or $action eq 8} {include file="CRM/Custom/Form/Option.tpl"} {else} - {if $customOption} - {if $reusedNames} -
-
  {ts 1=$reusedNames}These Multiple Choice Options are shared by the following custom fields: %1{/ts} -
- {/if} + {if $reusedNames} +
+
  {ts 1=$reusedNames}These Multiple Choice Options are shared by the following custom fields: %1{/ts} +
+ {/if} -
-

-
- {strip} - {* handle enable/disable actions*} - {include file="CRM/common/enableDisableApi.tpl"} - +
+

+
+ {* handle enable/disable actions*} + {include file="CRM/common/enableDisableApi.tpl"} +
+ - - - - - - + + + + + + + - {foreach from=$customOption item=row key=id} - - - - - - - - - {/foreach} -
{ts}Label{/ts}{ts}Value{/ts}{ts}Default{/ts}{ts}Order{/ts}{ts}Enabled?{/ts} {ts}Label{/ts}{ts}Value{/ts}{ts}Default{/ts}{ts}Order{/ts}{ts}Enabled?{/ts}  
{$row.label}{$row.value}{$row.default_value}{$row.weight}{if $row.is_active eq 1} {ts}Yes{/ts} {else} {ts}No{/ts} {/if}{$row.action|replace:'xx':$id}
- {/strip} + + + {literal} + + {/literal} + +
- - {else} - {if $action eq 16} -
- {ts}status{/ts} - {ts}None found.{/ts} -
- {/if} - {/if} +
{/if} -- 2.25.1