Display an area or a random scrapbook block

2011-04-04

I wrote this little snippet of php code today to either show an area to throw a banner image in, or grab from a scrapbook filled with banner images and display a random one.  It's a little more advanced than the block->getByName() functions that I usually use, I thought I'd share.  I know other people could probably use this code...

<?php
     $_blocks = $c->getBlocks("Banner");
     if ((count($_blocks) > 0) || $c->isEditMode()) {
          $a = new Area("Banner");
          $a->setBlockLimit(1);
          $a->display($c);
     } else {
          $scrapbookHelper = Loader::helper('concrete/scrapbook');
          $scrapbookPage = $scrapbookHelper->getGlobalScrapbookPage();
// area name is what the scrapbook is called, scrapbooks are really just areas when you get right down to it.
          $scrapbookArea = new Area("Random Inside Banners");
          $bannerBlocks = $scrapbookArea->getAreaBlocksArray($scrapbookPage);
          $totalBlocks = count($bannerBlocks) - 1;
          $i = rand(0, $totalBlocks);
          $bannerBlock = $bannerBlocks[$i];
          $bannerBlock->display();
     }?>