output a random GCB

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
stevegos

output a random GCB

Post 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.
mrenigma
Forum Members
Forum Members
Posts: 48
Joined: Fri Aug 05, 2011 3:43 pm
Location: London, United Kingdom

Re: output a random GCB

Post 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
~ Life is like water, every movement you make changes the way it flows ~
stevegos

Re: output a random GCB

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

Re: output a random GCB

Post 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;
  }

}
mrenigma
Forum Members
Forum Members
Posts: 48
Joined: Fri Aug 05, 2011 3:43 pm
Location: London, United Kingdom

Re: output a random GCB

Post 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
~ Life is like water, every movement you make changes the way it flows ~
stevegos

Re: output a random GCB

Post by stevegos »

Excellent. That has worked.

Thank you very much.
Post Reply

Return to “The Lounge”