CRM-16310 - fix ordering of options in dataTable
authorjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Thu, 4 Jun 2015 12:27:35 +0000 (17:57 +0530)
committerjitendrapurohit <jitendra.purohit@webaccessglobal.com>
Fri, 12 Jun 2015 12:40:31 +0000 (18:10 +0530)
CRM/Core/BAO/CustomOption.php
CRM/Core/xml/Menu/Custom.xml
CRM/Custom/Page/AJAX.php
CRM/Custom/Page/Option.php
templates/CRM/Custom/Page/Option.tpl

index 00a2f9c0cf9551c1b521d4f8861bb5dbeafe2686..5e0608edc1ed4dd8025c49edc201b8fabdcf6ab5 100644 (file)
@@ -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;
   }
index b58c6bcfb5041c238639ac7931caa0246da58b79..04a38ae614c7e6dff2b054c7267b31c50959364f 100644 (file)
@@ -10,4 +10,9 @@
      <page_callback>CRM_Custom_Page_AJAX::getOptionList</page_callback>
      <access_arguments>access CiviCRM</access_arguments>
   </item>
+  <item>
+     <path>civicrm/ajax/reorder</path>
+     <page_callback>CRM_Custom_Page_AJAX::fixOrdering</page_callback>
+     <access_arguments>access CiviCRM</access_arguments>
+  </item>
 </menu>
index fac96f931d72323a65d2eabec4dd99731aa758cd..1571dce983dd0955a336308b6b1563565e687b79 100644 (file)
@@ -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);
+  }
+
 }
index fb73094c7afafb04b27e0508f8be7ef5efd385b2..7e6801f62f43f9274683a7df7f8b9bc0ac08578b 100644 (file)
@@ -142,6 +142,7 @@ WHERE  option_group_id = %1";
       CRM_Utils_System::setTitle($newTitle);
       $this->assign('reusedNames', $reusedNames);
     }
+    $this->assign('optionGroupID', $optionGroupID);
   }
 
   /**
index 79694dffb05daf989984507bd8ff2a6b56df7b83..8c1e91dca8cf014a2efe86626f368ff7753d6831 100644 (file)
@@ -43,7 +43,6 @@
             <th class='crm-custom_option-label'>{ts}Label{/ts}</th>
             <th class='crm-custom_option-value'>{ts}Value{/ts}</th>
             <th class='crm-custom_option-default_value'>{ts}Default{/ts}</th>
-            <th class='nowrap crm-custom_option-weight'>{ts}Order{/ts}</th>
             <th class='crm-custom_option-is_active  nosort'>{ts}Enabled?{/ts}</th>
             <th class='crm-custom_option-links'>&nbsp;</th>
             <th class='hiddenElement'>&nbsp;</th>
@@ -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}
                 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('<span class="crm-editable crmf-label" />');
                 $('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( {
               }
           });
         }
+
+        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
+            })
+          }
+        });
       });
 
       </script>