# PLUGIN PREVIEW BY TEXTPATTERN.INFO
/*
adi_join - Join stuff together
Written by Adi Gilbert
Released under the GNU General Public License
Version history:
0.3 - new attributes: 'delimiter', 'ignore' (for pieman)
- new attribute: 'unique'
- TXP 4.6 tag registration
0.2 - new attributes: 'prefix', 'suffix', 'itemtag' (for jakob)
- new attribute: 'sort' (for milosevic)
- new attributes: 'label', 'labeltag', 'label_class', 'label_id'
0.1 - initial release
*/
// TXP 4.6 tag registration
if (class_exists('\Textpattern\Tag\Registry')) {
Txp::get('\Textpattern\Tag\Registry')
->register('adi_join')
;
}
function adi_join($atts,$thing=NULL) {
extract(lAtts(array(
'delimiter' => '', // delimiter (default = newline)
'ignore' => '0', // ignore delimiters embedded within tag's output (i.e. don't pre-parse)
'separator' => ', ', // default "comma" separator
'last_separator'=> '', // & finally - e.g. " & "
'unique' => '0', // strip duplicates?
'no_widow' => '0', // widow prevention
'prefix' => '', // text to go before each item e.g. "("
'suffix' => '', // text to go after each item e.g. ")"
'itemtag' => '', // tag to put around each prefix-item-suffix
'sort' => '', // allowed: 'sort','rsort','natsort','natcasesort' (standard PHP functions)
'label' => '', // label string to precede list
'labeltag' => '', // tag to wrap around label
'label_class' => '', // CSS class for label's tag
'label_id' => '', // HTML ID for label's tag
'wraptag' => '', // the wraptag
'class' => '', // class for wraptag
'html_id' => '', // HTML id for wraptag
'debug' => 0,
), $atts));
// massage attributes
if ($delimiter == '')
$delimiter = "\n"; // newline
if ($last_separator == '')
$last_separator = $separator;
// HTML
$separator = htmlentities($separator);
$last_separator = htmlentities($last_separator);
// split contents into separate items
if ($ignore) { // post-parse
$new_thing = explode($delimiter,$thing);
$lines = doArray($new_thing,'parse');
}
else // pre-parse
$lines = explode($delimiter,parse($thing));
// get rid of all the trimmings
$trimmed_items = array();
foreach ($lines as $line) {
$line = trim($line);
if ($line)
$trimmed_items[] = $line;
}
// all sorts
if ($sort) {
$allowed_sorts = array('sort','rsort','natsort','natcasesort','asc','desc');
if (array_search($sort,$allowed_sorts) !== FALSE) {
if ($sort == 'asc') $sort = 'sort';
if ($sort == 'desc') $sort = 'rsort';
$sort($trimmed_items);
}
}
// one of a kind
if ($unique)
$trimmed_items = array_unique($trimmed_items);
if ($debug)
dmp($trimmed_items);
// prefix/suffix
if ($prefix || $suffix)
foreach ($trimmed_items as $index => $item)
$trimmed_items[$index] = $prefix.$item.$suffix;
// item tags
if ($itemtag)
foreach ($trimmed_items as $index => $item)
$trimmed_items[$index] = tag($item,$itemtag);
// carve off last item
$last_item = array_pop($trimmed_items);
// "separator" separated items
$out = implode($separator,$trimmed_items);
// last separator
if ($out)
$out .= $last_separator;
// reunification
$out .= $last_item;
// no widows?
if ($no_widow && (strrpos($out,' ') !== FALSE)) // no widows wanted & there's a gap to fill
$out = substr_replace($out,' ',strrpos($out,' '),1); // let's stick together (& let's hope the last gap is only one space wide)
// wrap it all up
if ($out) {
if ($label)
$labeltag ?
$out = doTag($label,$labeltag,$label_class,'',$label_id).$out :
$out = $label.$out;
if ($wraptag)
return doTag($out,$wraptag,$class,'',$html_id);
else
return $out;
}
}