public function modelAction ($actionData = false)
{
- trigger_error(print_r($_REQUEST,1),E_USER_NOTICE);
-
switch($_REQUEST['option']) {
case 'addNewField':
break;
+ case 'sortOrder': // Save sort order when done with drag and drop of field in list
+
+ // Fields should be in correct order in the submitted array and keys should be sequential from 0
+ foreach ($_REQUEST['sortedIds'] as $sortKey=>$sortVal) {
+
+ // Strip "field_" from field ID number
+ $fieldId = preg_replace("/[^0-9]/", "", $sortVal);
+
+ // Update the field with the key as the order field
+ $this->wpdb->update(
+ GLM_MEMBERS_CUSTOMFIELDS_PLUGIN_DB_PREFIX.'custom_fields',
+ array( 'field_order' => $sortKey ),
+ array( 'id' => $fieldId ),
+ array( '%d' ),
+ array( '%d' )
+ );
+
+ // No need to reply to this
+
+ }
+ break;
+
+
default:
}
</thead>
{if $haveCustomFields}
{foreach $custom_fields as $t}
- <tbody id="field_{$t.id}">
+ <tbody id="field_{$t.id}" class="sort-field-container">
<tr class="FieldRow_{$t.id}">
- <th style="font-weight: bold; text-align: left;" id="DisplayFieldName_{$t.id}">{$t.field_name}</th>
+ <th style="font-weight: bold; text-align: left;" id="DisplayFieldName_{$t.id}" class="sortable-handle">{$t.field_name}</th>
<td style="text-align: left;" id="DisplayFieldTypeDescr_{$t.id}">{$t.field_type_descr}</td>
<td style="text-align: left;" id="DisplayFieldPriority_{$t.id}">{$t.priority_display.name}</td>
<td style="text-align: left;" id="DisplayFieldRequired_{$t.id}">{$t.required.name}</td>
<td colspan="5" style="text-align: left; padding-left: 2rem;">Prompt: <span id="DisplayFieldPrompt_{$t.id}">{$t.field_prompt}</span></td>
</tr>
{if $t.field_type=='picklist'}
- <tr id="PicklistField_{$t.id}">
+ <tr id="PicklistField_{$t.id}" class="picklist-options-container">
<td colspan="5" style="padding-left: 2rem;">
<table id="PicklistOptions_{$t.id}" width="100%" style="margin-top: .5rem;">
<tr class="FieldRow_{$t.id}" style="background-color: #ddd;">
});
};
+
+ /*
+ * Code to handle drag-and-drop sorting of custom fields
+ */
+
+ // Hide any custom field options for the field while mousedown
+ $('.sortable-handle').mousedown(function() {
+ $(this).parent().parent().find(".picklist-options-container").css("display","none");
+ });
+ $('.sortable-handle').mouseup(function() {
+ $(this).parent().parent().find(".picklist-options-container").css("display","table-row");
+ });
+
+ // Make fields tables sortable
+ $('#FieldsTable_{$fid}').sortable({
+ items: '.sort-field-container',
+ cursorAt: { top: -20, left: 10 },
+ handle: ".sortable-handle",
+ start: function ( event, ui ) {
+ },
+ stop: function ( event, ui ) {
+ // Make sure we show any options again when were done dragging
+ $(this).find(".picklist-options-container").css("display","table-row");
+ },
+ update: function(event, ui) {
+
+ // Create an array of the fields in the current order by field ID (field_{ id })
+ var sortedIds = $(this).sortable('toArray');
+
+ // Send new order via AJAX
+ var formData = {
+ 'action': 'glm_members_admin_ajax',
+ 'glm_action': 'customFields',
+ 'option': 'sortOrder',
+ 'sortedIds': sortedIds
+ };
+ $.ajax({
+ type: 'POST',
+ url: '{$ajaxUrl}',
+ data: formData,
+ encode: true,
+ dataType: 'text'
+ })
+ }
+
+ });
// Flash an element for a short time
function flashElement(id) {