# PLUGIN PREVIEW BY TEXTPATTERN.INFO
// NOTE
/*
adi_list - Use lists like arrays
Written by Adi Gilbert
Released under the GNU General Public License
Version history:
0.2 - TXP 4.6 tag registration
0.1 - initial release
*/
// TXP 4.6 tag registration
if (class_exists('\Textpattern\Tag\Registry')) {
Txp::get('\Textpattern\Tag\Registry')
->register('adi_if_list')
->register('adi_list_count')
->register('adi_list_debug')
->register('adi_list_default')
->register('adi_list_end')
->register('adi_list_first')
->register('adi_list_item')
->register('adi_list_last')
->register('adi_list_next')
->register('adi_list_prev')
->register('adi_list_reset')
->register('adi_list')
;
}
function adi_list_count($atts) {
// return number of items in list
extract(lAtts(array(
'name' => '', // the list name
), $atts));
return adi_list(array('name' => $name,'count' => 1));
}
function adi_list_item($atts) {
// return value of specific (or current) item
extract(lAtts(array(
'name' => '', // the list name
'index' => '', // item index
), $atts));
return adi_list(array('name' => $name,'index' => $index));
}
function adi_list_first($atts) {
// return first value in list
extract(lAtts(array(
'name' => '', // the list name
), $atts));
return adi_list(array('name' => $name,'index' => 0));
}
function adi_list_last($atts) {
// return last value in list
extract(lAtts(array(
'name' => '', // the list name
), $atts));
return adi_list(array('name' => $name,'last' => 1));
}
function adi_list_next($atts) {
// advance pointer
extract(lAtts(array(
'name' => '', // the list name
), $atts));
return adi_list(array('name' => $name,'next' => 1));
}
function adi_list_prev($atts) {
// rewind pointer
extract(lAtts(array(
'name' => '', // the list name
), $atts));
return adi_list(array('name' => $name,'prev' => 1));
}
function adi_list_reset($atts) {
// move pointer to start of list
extract(lAtts(array(
'name' => '', // the list name
), $atts));
return adi_list(array('name' => $name,'reset' => 1));
}
function adi_list_end($atts) {
// move pointer to end of list
extract(lAtts(array(
'name' => '', // the list name
), $atts));
return adi_list(array('name' => $name,'end' => 1));
}
function adi_list_default($atts) {
// return default value
extract(lAtts(array(
'name' => '', // the list name
), $atts));
// adi_list global
$list = '$adi_list_list_'.$name;
global $$list;
if (${$list}['default'] !== NULL)
return ${$list}['default'];
}
function adi_if_list($atts,$thing) {
extract(lAtts(array(
'name' => '', // the list name
'value' => '', // value to test
'index' => '', // index to test
// 'overrun' => '', // test if gone past end of original list
),$atts));
// adi_list global
$list = '$adi_list_list_'.$name;
global $$list;
if ($value !== '')
return parse(EvalElse($thing,($value == current(${$list}['value']))));
if ($index !== '')
return parse(EvalElse($thing,($index == key(${$list}['value']))));
// if ($overrun)
// return parse(EvalElse($thing,(key($$list) > count($$list))));
}
function adi_list($atts,$thing=NULL) {
// set up list or return item values
global $variable,$thisarticle;
extract(lAtts(array(
'name' => '', // the list name
'value' => '', // set value of list
'txpvar' => '', // get list from TXP variable
'field' => '', // get list from custom field (incl. Article image)
'separator' => ',', // list separator
'default_separator' => '|', // default value separator
'index' => '', // return specific item index (default = current)
'default' => NULL, // default value
'last' => '0', // last value
'count' => '0', // number of items in list
'next' => '0', // advance pointer by one
'prev' => '0', // rewind pointer by one
'reset' => '0', // rewind pointer to start
'end' => '0', // advance pointer to end
'repeat' => '0', // repeat list sequence
'random' => '0', // randomise the list
), $atts));
// clean up attribute values
$name = trim($name);
$index = trim($index);
$field = strtolower($field);
// mandatory attribute
if (empty($name)) {
echo "adi_list: list name required".br;
return;
}
// adi_list global
// $adi_list_list_mylist('value' => "this,is,the,list", 'default' => "value", 'repeat' => 0/1, 'separator' => "value")
$list = '$adi_list_list_'.$name;
global $$list;
// if value attribute supplied - set things up
if (array_key_exists('value', $atts) || ($thing !== NULL) || $txpvar || $field) { // list supplied as attribute OR container mode OR TXP variable
$$list = array();
// TXP variable
if ($txpvar)
$value = (isset($variable[$txpvar]) ? $variable[$txpvar] : '');
// article field
if ($field) {
if ($field == 'article image')
$field = 'article_image';
$value = (isset($thisarticle[$field]) ? $thisarticle[$field] : '');
}
// container mode
if ($thing !== NULL)
$value = parse($thing);
// default
$embedded_default = explode($default_separator,$value);
if (isset($embedded_default[1])) { // look for embedded default at end, e.g. a,b,c|z
${$list}['default'] = $embedded_default[1];
$value = $embedded_default[0]; // reassign value from begining
}
else
${$list}['default'] = $default;
// convert value to array
${$list}['value'] = explode($separator,$value);
// randomise?
if ($random)
shuffle(${$list}['value']);
// repeat
${$list}['repeat'] = $repeat;
// list separator
${$list}['separator'] = $separator;
// all done
return;
}
// check list exists
if (isset($$list))
$repeat = ${$list}['repeat'];
else {
echo 'adi_list: list "'.$name.'" ('.$list.') not found'.br;
return;
}
// list info
if ($count) {
return count(${$list}['value']); // number of items in list
}
// pointer stuff
if ($next) {
$res = next(${$list}['value']); // advance pointer by one
if ($repeat && ($res === FALSE)) // fallen off the end
reset(${$list}['value']); // go back to the beginning
return;
}
if ($prev) {
$res = prev(${$list}['value']); // rewind pointer by one
if ($repeat && ($res === FALSE)) // tripped backward over the start
end(${$list}['value']); // go to the end
return;
}
if ($reset) {
reset(${$list}['value']); // rewind pointer to start
return;
}
if ($end) {
end(${$list}['value']); // advance pointer to end
return;
}
// return something
if ($last) { // last item
return implode(array_slice(${$list}['value'],-1,1));
}
else if (array_key_exists('index', $atts)) { // index attribute supplied
if ($index === '') { // empty index supplied, look for current (or default)
if (current(${$list}['value']) === FALSE) {
if (${$list}['default'] !== NULL) // return default
return ${$list}['default'];
else
return ''; // fire a blank
}
else
return current(${$list}['value']); // return current value
}
else if (isset(${$list}['value'][$index])) // index found
return ${$list}['value'][$index];
else {
if ($repeat) { // massage index & repeat
$new_index = $index % count(${$list}['value']);
return ${$list}['value'][$new_index];
}
else if (${$list}['default'] !== NULL) // return default
return ${$list}['default'];
else
return ''; // fire a blank
}
}
else { // return the whole list
return implode(${$list}['separator'],${$list}['value']);
}
}
function adi_list_debug($atts) {
// diagnostics
extract(lAtts(array(
'name' => '', // the list name
), $atts));
// adi_list global
$list = '$adi_list_list_'.$name;
global $$list;
echo 'adi_list_debug: list "'.$name.'" ('.$list.') ';
if (isset($$list)) {
echo '= "'
.implode(${$list}['separator'],${$list}['value'])
.'"; '
.(${$list}['default'] === NULL ? 'no default' : 'default = "'.${$list}['default'].'"')
.'; repeat = '
.${$list}['repeat']
.'; separator = "'
.${$list}['separator']
.'"; pointer = '
.key(${$list}['value'])
;
dmp($$list);
}
else
echo 'not found';
echo br;
}