Conditional if thumbnail is selected
Permalink
July 05, 2012 at 8:26 PM
Hi all,
I am working on a custom pagelist template and have page attributes which sets the size of each pagelist item (3 sizes in all) but I now also need some form of condition if a thmbnail isn't selected at all.
Basically each of the pagelist items shown in the code below need some kind of condition to render a different piece of HTML in the place of the img tags if no thumbnail is selected for that page.
I hope this makes sense... I am cobbling this code together from help within the C5 community so if you are able to help, please respond as if I am an idiot as my PHP skills are weak to say the least.
Thank you in advance for anyone that is able to help out.
<?php
defined ( 'C5_EXECUTE' ) or die ( "Access Denied." ) ;
$rssUrl = $showRss ? $controller -> getRssUrl ( $b ) : '' ;
$th = Loader:: helper ( 'text' ) ;
$ih = Loader:: helper ( 'image' ) ;
//Note that $nh (navigation helper) is already loaded for us by the controller (for legacy reasons)
?>
<div class="ccm-page-list">
<?php foreach ( $pages as $page ) :
// get the event attribute
$size = $page -> getAttribute ( 'size' ) ;
$postedby = $page -> getAttribute ( 'postedby' ) ;
$img = $page -> getAttribute ( 'PageThumbnail_One' ) ;
$cclass = $page -> getAttribute ( 'bodyclass' ) ;
$thumb = $ih -> getThumbnail ( $img , 64 , 50 , true ) ;
<?php
defined ( 'C5_EXECUTE' ) or die ( "Access Denied." ) ;
$rssUrl = $showRss ? $controller -> getRssUrl ( $b ) : '' ;
$th = Loader:: helper ( 'text' ) ;
$ih = Loader:: helper ( 'image' ) ;
//Note that $nh (navigation helper) is already loaded for us by the controller (for legacy reasons)
?>
<div class="ccm-page-list">
<?php foreach ( $pages as $page ) :
// get the event attribute
$size = $page -> getAttribute ( 'size' ) ;
$postedby = $page -> getAttribute ( 'postedby' ) ;
$img = $page -> getAttribute ( 'PageThumbnail_One' ) ;
$cclass = $page -> getAttribute ( 'bodyclass' ) ;
$thumb = $ih -> getThumbnail ( $img , 64 , 50 , true ) ;
$mediumthumb = $ih -> getThumbnail ( $img , 460 , 210 , true ) ;
$smallthumb = $ih -> getThumbnail ( $img , 220 , 200 , true ) ;
// output opening div dependent on its value
// continue with standard code.
// Prepare data for each page being listed...
$title = $th -> entities ( $page -> getCollectionName ( ) ) ;
$url = $nh -> getLinkToCollection ( $page ) ;
$target = ( $page -> getCollectionPointerExternalLink ( ) != '' && $page -> openCollectionPointerExternalLinkInNewWindow ( ) ) ? '_blank' : $page -> getAttribute ( 'nav_target' ) ;
$target = empty ( $target ) ? '_self' : $target ;
$description = $page -> getCollectionDescription ( ) ;
$description = $controller -> truncateSummaries ? $th -> shorten ( $description , $controller -> truncateChars ) : $description ;
$description = $th -> entities ( $description ) ;
//Other useful page data...
//$date = date('F j, Y', strtotime($page->getCollectionDatePublic()));
//$last_edited_by = $page->getVersionObject()->getVersionAuthorUserName();
//$original_author = Page::getByID($page->getCollectionID(), 1)->getVersionObject()->getVersionAuthorUserName();
/* CUSTOM ATTRIBUTE EXAMPLES:
* $example_value = $page->getAttribute('example_attribute_handle');
*
* HOW TO USE IMAGE ATTRIBUTES:
* 1) Uncomment the "$ih = Loader::helper('image');" line up top.
* 2) Put in some code here like the following 2 lines:
* $img = $page->getAttribute('example_image_attribute_handle');
* $thumb = $ih->getThumbnail($img, 64, 9999, false);
* (Replace "64" with max width, "9999" with max height. The "9999" effectively means "no maximum size" for that particular dimension.)
* (Change the last argument from false to true if you want thumbnails cropped.)
* 3) Output the image tag below like this:
* <img src="<?php echo $thumb->src ?>" width="<?php echo $thumb->width ?>" height="<?php echo $thumb->height ?>" alt="" />
*
* ~OR~ IF YOU DO NOT WANT IMAGES TO BE RESIZED:
* 1) Put in some code here like the following 2 lines:
* $img_src = $img->getRelativePath();
* list($img_width, $img_height) = getimagesize($img->getPath());
* 2) Output the image tag below like this:
* <img src="<?php echo $img_src ?>" width="<?php echo $img_width ?>" height="<?php echo $img_height ?>" alt="" />
*/
/* End data preparation. */
/* The HTML from here through "endforeach" is repeated for every item in the list... */ ?>
<!-- THIS SETS THE PAGE TYPE -->
<?php switch ( $size ) {
case 'Small' : ?>
<div class="article_box small events" data-category="noble-gas">
<div class="top"></div>
<div class="article">
<h2><a href="<?php echo $url ?> " target="<?php echo $target ?> "><?php echo $title ?> </a></h2>
<div class="hr_w top"></div>
<img src="<?php echo $smallthumb -> src ?> " width="<?php echo $smallthumb -> width ?> " height="<?php echo $smallthumb -> height ?> " alt="Pagelist thumbnail" />
<div class="hr_w top bot"></div>
<p><?php echo $description ?> </p>
</div>
<a href="<?php echo $url ?> " target="<?php echo $target ?> " class="bbtn btn-small btn-primary readmore ">Read More</a>
<div class="article_footer">
</div>
<div class="article_footer_b"></div>
</div>
<?php
break ;
case 'Medium' : ?>
<div class="article_box medium events" data-category="noble-gas">
<div class="top"></div>
<div class="article">
<h2><a href="<?php echo $url ?> " target="<?php echo $target ?> "><?php echo $title ?> </a></h2>
<div class="hr_w top"></div>
<img src="<?php echo $mediumthumb -> src ?> " width="<?php echo $mediumthumb -> width ?> " height="<?php echo $mediumthumb -> height ?> " alt="Pagelist thumbnail" />
<div class="hr_w top bot"></div>
<p><?php echo $description ?> </p>
</div>
<a href="<?php echo $url ?> " target="<?php echo $target ?> " class="bbtn btn-small btn-primary readmore ">Read More</a>
<div class="article_footer">
</div>
<div class="article_footer_b"></div>
</div>
<?php
break ;
case 'Image Only' : ?>
<div class="article_box image-only support" data-category="noble-gas">
<div class="top"></div>
<div class="article">
<img src="<?php echo $smallthumb -> src ?> " width="<?php echo $smallthumb -> width ?> " height="<?php echo $smallthumb -> height ?> " alt="Pagelist thumbnail" />
</div>
<div class="article_footer_b"></div>
</div>
<?php
break ;
default : ?> <div style="display:none"><?php echo $title ?> test3</div><?php
break ;
} ?>
<!--<div style="color:#fff;height:200px;width:500px;background:#336;position:absolute;z-index:2000"><?php echo $title ?> test2
<img src="<?php echo $thumbsizetwo -> src ?> " width="<?php echo $thumbsizetwo -> width ?> " height="<?php echo $thumbsizetwo -> height ?> " alt="" /></div>
<div class="<?php echo $cclass ?> -gradient" style="color:#fff;height:100px;width:100px;background:#CC0"><?php echo $title ?> test1
<img src="<?php echo $thumb -> src ?> " width="<?php echo $thumb -> width ?> " height="<?php echo $thumb -> height ?> " alt="" />
-->
<?php endforeach ; ?>
<?php if ( $showRss ) : ?>
<div class="ccm-page-list-rss-icon">
<a href="<?php echo $rssUrl ?> " target="_blank"><img src="<?php echo $rssIconSrc ?> " width="14" height="14" alt="<?php echo t( 'RSS Icon' ) ?> " title="<?php echo t( 'RSS Feed' ) ?> " /></a>
</div>
<link href="<?php echo BASE_URL. $rssUrl ?> " rel="alternate" type="application/rss+xml" title="<?php echo $rssTitle ; ?> " />
<?php endif ; ?>
</div><!-- end .ccm-page-list -->
<?php if ( $showPagination ) : ?>
<div id="pagination">
<div class="ccm-spacer"></div>
<div class="ccm-pagination">
<span class="ccm-page-left"><?php echo $paginator -> getPrevious ( '« ' . t( 'Previous' ) ) ?> </span>
<?php echo $paginator -> getPages ( ) ?>
<span class="ccm-page-right"><?php echo $paginator -> getNext ( t( 'Next' ) . ' »' ) ?> </span>
</div>
</div>
<?php endif ; ?>