no image


Snippet PrevTV
inspired by the following question from the "prevjumpnext" snippet forums thread
-> http://modxcms.com/forums/index.php/topic,5925.msg249233.html#msg249233
btw. great help for writing my first Snippet was Bogdan's MODx API Cheat Sheet
...sorry for the probably dirty & bad code - ItsMyFirstSnippet and still a "work in progress". It will return the value of an (e.g. image)TV from the previous and/or next ressource:
<?php
/**
* **************************
* Snippet: prevtv
* By: sharkbait / @kunane
* Date: Nov 2009
* inspired & borrowed some parts of: PrevJumpNextXHTML Snippet
* from the version modified by PMS
* **************************
installation:
1. create a new snippet "prevtv" and copy this code into the Snippet code-field
2 in your template call the snippet like this: [!prevtv? &direction=`1` &tvname=`yourtvname` !]
&direction=`1` will return the value of the TV from the previous ressource
&direction=`2` will return the value of the TV from the next ressource
enter the name of the TV you want to retrieve the value
&tvname=`yourtvname`
enter the text you want to be displayed if there's no image (ressource prev. to the first / next to the last) - leave it empty if you want no text to be displayed
&text=`no image`
the order of the TVs currently reflects the defauld order of Wayfinder
sorry for the ugly code - #itsmyfirstsnippet :)
*/
// default Wayfinder
if(!isset($sortBy)){
$sortBy = 'menuindex';
}
// default Wayfinder
if(!isset($sortHow)){
$sortHow = 'ASC';
}
// Get the value of the TV named in the Snippet call
$pntv = $tvname;
// Get the value of
$pntext = $text;
// Get the parent ID
$parentid = $modx->documentObject['parent'];
// (re)set the $id variable to the one of this document.
$id = $modx->documentIdentifier;
// select the other members in this folder
$children = $modx->getActiveChildren($parentid, $sortBy, $sortHow);
// the number of selected documents
$limit = count($children);
// set $y to zero
$y = 1;
// sorting the documents, giving them a sequential and searchable index
foreach ($children as $child)
{
$my_array[$y] = $child;
if ($my_array[$y]['id']==$id){
$current=$y; // The current page has number $y in the array
}
$y++;
}
$prev = $current-1;
$next = $current+1;
// if &direction is set to 1 do this
if ($direction == 1) {
if ($prev == 0) {
// in case a value previous to the first document
return $pntext;
} else {
// get the id of the previous document
$previd = $my_array[$prev]['id'];
// return the output of the TV named in the Snippet call -> tvname ($pntv)
$document_tvs = $modx->getTemplateVarOutput(true, $previd);
$templatevar_output = $document_tvs[$pntv];
$output = $templatevar_output;
return $output;
}
}
// if &direction is set to 2 do this
elseif ($direction == 2) {
if ($current == $limit) {
// in case a value next to the last document
return $pntext;
} else {
// get the id of the next document
$nextid = $my_array[$next]['id'];
// return the output of the TV named in the Snippet call -> tvname ($pntv)
$document_tvs = $modx->getTemplateVarOutput(true, $nextid);
$templatevar_output = $document_tvs[$pntv];
$output = $templatevar_output;
return $output;
}
}
else {
$output = '';
return $output;
}
?>
marc
Marc
marc
marc
marc
marc
- Required fields are marked with *.