no image
prevtv image
prevtv 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:

download here

<?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
Gravatar Image
Reply #10 on : Mon December 27, 2010, 09:47:06
Hmm, i really don't get it, it worked but somehow it doesn't anymore, im gona put an empty image into every empty tv.
Marc
Gravatar Image
Reply #9 on : Mon December 27, 2010, 09:22:30
Damn, it's the caching :D , somebody shoot me plz.

[! -> [[

Thanks allot for the pinpoint!
admin
Gravatar Image
Reply #8 on : Fri December 24, 2010, 16:10:44
Hey Marc,

check the source code, i use this as a test for now:
<img src="[ [prevtv? &direction=`1` &tvname=`previmage` &text=`assets/templates/web/images/spacer.gif`] ]" height="140" />

maybe also try different caching options

*edit* yeah, ignore my inline styling :)
Last Edit: December 24, 2010, 16:12:48 by admin  
marc
Gravatar Image
Reply #7 on : Fri December 24, 2010, 15:55:56
yea, i can't use the widget (has it's reasons)
and the &text= isn't working so i get the <img scr=""> empty and IE gives ugly cross :(

can't think of other solutions atm :(

ps. Merry Christmis 2 ya too :D
admin
Gravatar Image
Reply #6 on : Fri December 24, 2010, 14:53:14
well, in your case, when no image is seected, you got the spacer gif, right? so there shouldn't be a red cross in IE. For the example above i used an image output widget btw.
marc
Gravatar Image
Reply #5 on : Fri December 24, 2010, 14:39:24
Argh i don't get it, it just won't work lol.
Can't find any typo's, no widget, it should work but just doesn't. When there is some image then it does work. I hate internet explorer, showing some cross when an image is not found.
admin
Gravatar Image
Reply #4 on : Fri December 24, 2010, 07:03:27
Hi Marc,

i just tested it, and it's working. in your case the input type should be "image" & no output widget selected ... and probably just a typo, but your image tag should be closed />

merry christmas :)
Last Edit: December 24, 2010, 07:30:53 by admin  
admin
Gravatar Image
Reply #3 on : Thu December 23, 2010, 18:23:01
Hi Marc,

i'll look into it :)

j
marc
Gravatar Image
Reply #2 on : Thu December 23, 2010, 17:33:08
whoops:

&text isn't working:
i'd like to have a spacer when there is nothing into tv: tvhere.

<img src="[ ! prevtv? &direction=`1` &text=`/assets/templates/web/images/spacer.gif` &tvname=`tvhere` ! ]" height="140">
marc
Gravatar Image
Reply #1 on : Thu December 23, 2010, 17:32:21
&text isn't working:
i'd like to have a spacer when there is nothing into tv: tvhere.

<img src="" height="140">
  • Required fields are marked with *.

If you have trouble reading the code, click on the code itself to generate a new random code.