Skip to content

Instantly share code, notes, and snippets.

@williammincy
Created August 11, 2013 01:15
Show Gist options
  • Save williammincy/6202938 to your computer and use it in GitHub Desktop.
Save williammincy/6202938 to your computer and use it in GitHub Desktop.
This is a simple POC for using the code I added to the DMS Dev branch (fork) to enable sorting of items within DMS options panels. This was implemented against 'multi' type options and is initialized by a 'sortable' key within the options array.
<?php
/*
Section: Sortable
Author: William Mincy
Author URI: http://slipperysource.com
Description: Example of sortable options reordering content
Class Name: wmSortable
Filter: Component
Loading: active,templates,main,sb_1,sb_2,sb_wrap, footer, footermore
*/
/**
* Post Author Section
*
* @author SlipperySource
*/
class wmSortable extends PageLinesSection {
const version = '1.0.0';
function _get_by_key($needle, $haystack) {
foreach($haystack as $item) {
if($item['key']==$needle) {
return $item;
}
}
}
// Options Panel
function section_opts(){
$options = array();
$FormTextDefaults = array (
'key' => 'FormTextDefaults',
'type' => 'multi',
'sortable' => true,
'title' => __('Default Form Options','wmSortable'),
'ref' => __('Uncheck boxes to exclude fields from the form. The form has been coded to validate only the fields that are visible within the form.','wmSortable'),
'opts' => array(
array(
'key' => '_firstname',
'type' => 'check',
'title' => __('First Name','wmSortable'),
'default' => true
),
array(
'key' => '_lastname',
'type' => 'check',
'title' => __('Last Name','wmSortable'),
'default' => true
),
array(
'key' => '_telephone',
'type' => 'check',
'title' => __('Telephone Number','wmSortable'),
'default' => true
),
array(
'key' => '_telephone_mobile',
'type' => 'check',
'title' => __('Is this a mobile phone?','wmSortable'),
'default' => true
)
)
);
// check if the sort order needs to be adjusted for the options panels
if( ploption('FormTextDefaults_sortable', $this->oset) ) {
$order = explode(",",ploption('FormTextDefaults_sortable', $this->oset));
$neworder = array();
foreach($order as $item) {
$neworder[] = $this->_get_by_key($item,$FormTextDefaults['opts']);
}
$FormTextDefaults['opts'] = $neworder;
}
// set the options object to the sorted arrays
$options[] = $FormTextDefaults;
// return the final sorted options
return $options;
}
// Content Display
function section_template() {
//
$values = array(
'_firstname' => 'First Name',
'_lastname' => 'Last Name',
'_telephone' => 'Telephone',
'_telephone_mobile' => 'Telephone Mobile'
);
// reorder based on DMS sorting
if( ploption('FormTextDefaults_sortable', $this->oset) ) {
$order = explode(",",ploption('FormTextDefaults_sortable', $this->oset));
foreach($order as $item) {
$neworder[] = $values[$item];
}
$values = $neworder;
}
// render the output
foreach($values as $display) {
printf('<p>%s</p>',$display);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment