Display an area or a random scrapbook block

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...

  1. <?php
  2. $_blocks = $c->getBlocks("Banner");
  3. if ((count($_blocks) > 0) || $c->isEditMode()) {
  4. $a = new Area("Banner");
  5. $a->setBlockLimit(1);
  6. $a->display($c);
  7. } else {
  8. $scrapbookHelper = Loader::helper('concrete/scrapbook');
  9. $scrapbookPage = $scrapbookHelper->getGlobalScrapbookPage();
  10. // area name is what the scrapbook is called, scrapbooks are really just areas when you get right down to it.
  11. $scrapbookArea = new Area("Random Inside Banners");
  12. $bannerBlocks = $scrapbookArea->getAreaBlocksArray($scrapbookPage);
  13. $totalBlocks = count($bannerBlocks) - 1;
  14. $i = rand(0, $totalBlocks);
  15. $bannerBlock = $bannerBlocks[$i];
  16. $bannerBlock->display();
  17. }?>
blog comments powered by Disqus