Page 1 of 1

output a random GCB

Posted: Thu Nov 08, 2012 10:41 am
by stevegos
Hi

I wish to output a random global content block to the screen. I searched and found a few posts but cannot get anything to work.

Ideally I don't want all the GCB to output just those that I append with random_ for example {global_content name='random_offer1'}

I found this post http://forum.cmsmadesimple.org/viewtopi ... random_gcb who was using a tag called {random_gcb} but it appears to be a user defined tag and I cannot find anything about the tag.

Any ideas?
All help greatly appreciated.

Re: output a random GCB

Posted: Thu Nov 08, 2012 11:29 am
by mrenigma
Did you check the forge?

http://dev.cmsmadesimple.org/projects/randomblob

Although this is marked as stale, it is probably because there is no need for additions. I had a look at the code and it looks like exactly what you need.

You might need to update the tag slightly to work with the latest CMSMS but it's definitely a step in the right direction.

Just place it in your plugins folder and see if it appears fine in your tags list (Extensions > Tags), if so it should be working and ready to use.

Cheers
MrEnigma

Re: output a random GCB

Posted: Thu Nov 08, 2012 11:38 am
by stevegos
Hi and thanks for that.

I did check the forge and there are a few random type functions in there. I did installed the random_blob function but I cannot get it to work - it breaks the template.

I'm not good enough with the code to make it work with the latest CMSMS 1.11 series.

Re: output a random GCB

Posted: Thu Nov 08, 2012 11:47 am
by stevegos
just in case this is a quick job, here the code from the function. Is there a quick way to get this to work with the lastest CMSMS 1.11.2.1?

This is now 6 years old so its not surprising it doesn't work.

Code: Select all

function smarty_cms_function_random_blob($params, &$smarty) {
	$blobs = HtmlBlobOperations::LoadHtmlBlobs();	
  
  if (!isset($params['prefix'])) {
    return "Random Blob Error: no prefix defined";
  } else {

    $random_blobs = array();
    
    foreach ($blobs as $one) {
      if (preg_match("/^".$params['prefix']."/", $one->name)) {
        $random_blobs[] = $one;
      }
    }
    
    srand((float)microtime() * 1000000);
    shuffle($random_blobs);

    return $random_blobs[0]->content;
  }

}

Re: output a random GCB

Posted: Thu Nov 08, 2012 11:59 am
by mrenigma
Try replacing:

Code: Select all

$blobs = HtmlBlobOperations::LoadHtmlBlobs();
With:

Code: Select all

$blobs =& cmsms()->GetGlobalContentOperations()->LoadHtmlBlobs(); 
The rest looks up-to-date...

Cheers
MrEnigma

Re: output a random GCB

Posted: Thu Nov 08, 2012 12:08 pm
by stevegos
Excellent. That has worked.

Thank you very much.