WebsiteBaker Community Forum

WebsiteBaker Support (2.8.x) => Droplets & Snippets => Topic started by: WebBird on August 04, 2009, 02:16:00 PM

Title: Snippet: WbRandomSection
Post by: WebBird on August 04, 2009, 02:16:00 PM
Due to a request in the German board, I created a RandomSection Snippet. I am aware of the existence of "RandomSection", but I think it is too restrictive, for it limits the sections to choose from to WYSIWYG.

So, I created a Snippet called "WbRandomSection" (where Wb is for Webbird :-D), which gives you more freedom.

For download, see the AMASP page: http://www.websitebakers.com/pages/modules/listings/section-pages/wbrandomsection.php?lang=EN

Usage instruction can be found in the readme.txt:

Quote
USAGE
=====

1. In a Code section

* Add a code section to the page
* Insert the following code (the two lines between --- and ---):

---
include_once WB_PATH.'/modules/wbrandomsection/include.php';
show_random( array( <Option> ) );
---

2. In a template

* Open the index.php of the template
* Insert the code mentioned above where the random section(s) shall occur


3. As a droplet

* Name: WBRandomSection
* Code:

---
include_once WB_PATH.'/modules/wbrandomsection/include.php';

$options = array();

if ( isset( $page_id ) ) {
    $options['page_id'] = $page_id;
}

if ( isset( $types ) ) {
    $options['types'] = explode( ',', $types );
}

if ( isset( $limit ) ) {
    $options['limit'] = $limit;
}

ob_start();
show_random( $options );
$return = ob_get_clean();

return $return;

---

* Comment:
  [[WBRandomSection?page_id=<ID>;types=<Type(s)>;limit=<Int>]]


Examples:

[[WBRandomSection?page_id=5]]
[[WBRandomSection?types=wysiwyg,imagegallery,code]]
[[WBRandomSection?page_id=5;types=wysiwyg,imagegallery,code]]


OPTIONS
=======

There are several options to control the snippet's behaviour:

page_id => <Number>

    A page number to choose the sections from. By default, the section
    is chosen from all sections of all pages.

types => '<Moduletype>' | array( 'List', 'of', 'Types' )

    Module types to choose from. May be a string or a list. By default,
    the section is chosen from all available module types.
    
limit => <Number>

    Number of sections to show. Default is 1.
  


EXAMPLES
========

* All default; choose a (=one) random section from all available pages

show_random();

* Show 2 random sections from page with ID 5, but only of type
 "wysiwyg" or "imagegallery".

show_random(
    array(
        'page_id' => 5,
        'limit'   => 2,
        'types'   => array( 'wysiwyg', 'imagegallery' )
    )
);


STYLES
======

To style your Random Sections, two CSS classes are provided:

div.randomsection

    Each section is wrapped into a <div> of class "randomsection". To
    add a gray border to each section, for example, add this code to
    your frontend.css (of the template you are using):
    
    div.randomsection { border: 1px solid #ccc; }
    
    To add a margin on the bottom of each section, try this:
    
    div.randomsection { margin-bottom: 15px; }

div.randomsectionwr ap

    Wraps the complete RandomSection part. That is, if you include more
    than one section, the complete list of rendered sections is
    wrapped into a <div> of class "randomsectionwrap".
    
    You can use this to "float" the complete RandomSection block:
    
    div.randomsectionwr ap { width: 100px; float: right; }
    
Title: Re: Snippet: WbRandomSection
Post by: Stefek on August 04, 2009, 02:36:42 PM
Thanks again WebBird.

Thats a very handy Snippet.

Regards,
Stefek
Title: Re: Snippet: WbRandomSection
Post by: WebBird on August 04, 2009, 02:41:09 PM
 :-D
Title: Re: Snippet: WbRandomSection
Post by: crnogorac081 on August 04, 2009, 03:15:36 PM
can you put sections id in array ?
Title: Re: Snippet: WbRandomSection
Post by: Stefek on August 04, 2009, 03:33:20 PM
Hello Ivan,


All you need to do is: name your pageID.
The snippet returns the sections of this pageID in random.

So you do not have to call the sections in a array.
But f you need it that way, there is another module for this at AMASP.

Regards,
Stefek
Title: Re: Snippet: WbRandomSection
Post by: crnogorac081 on August 04, 2009, 03:39:36 PM
I ment if sections are on different pages..

But if there is already exist cool :)

cheers
Title: Re: Snippet: WbRandomSection
Post by: WebBird on August 04, 2009, 04:01:42 PM
Hum? Do you want to give a list of sections to choose from? If so, you don't need the snippet. :-D

By default, a random section will be chosen from ALL active sections on ALL pages.
Title: Re: Snippet: WbRandomSection
Post by: crnogorac081 on August 04, 2009, 04:20:47 PM
I was thinking maybe to have in array section id like (5,10,20,21) and then to chose random between those 4..

Does it metter which module is it, or any module can work ?
Title: Re: Snippet: WbRandomSection
Post by: Stefek on August 04, 2009, 04:42:17 PM
I was thinking maybe to have in array section id like (5,10,20,21) and then to chose random between those 4..

Does it metter which module is it, or any module can work ?

I think this is the module you're looking for.
But notice, that it handles WYSIWYG only.

Regards,
Stefek
Title: Re: Snippet: WbRandomSection
Post by: WebBird on August 05, 2009, 10:03:07 AM
The original RandomSection module only handles WYSIWYG.

WbRandomSection handles any section/module type.

I can add the option to give a list of section id's to choose from, but I think you could do it as a droplet.

Try this code (untested) in a code section:

Code: [Select]
global $database;
$sections = array();

// get random section id
$rand = rand( 0, sizeof($sections) );
$section_id = $sections[$rand];

// get module type from DB
$result = $database->query( "SELECT module FROM ".TABLE_PREFIX."sections WHERE section_id='$section_id'" );

if ( $result ) {

    $data   = $result->fetchRow();
    $module = $data['module'];
   
    $file = WB_PATH.'/modules/'.$module.'/view.php';
    if ( file_exists ($file) ) {
      ob_start(); // Output Buffer start
      require($file);
      $foo = ob_get_contents(); // put Output Buffer in $foo
      ob_end_clean();           // end and clean the Output Buffer
     
      $output .= '<div class="randomsection">'
            .  $foo
            .  '</div>';
           
      return $output;

}

return false;

In
Code: [Select]
$sections = array();, insert the comma seperated section IDs between the (...).
Title: Re: Snippet: WbRandomSection
Post by: crnogorac081 on August 05, 2009, 12:19:26 PM
cool.

It is important that it can use any section :)

cheers
Title: Re: Snippet: WbRandomSection
Post by: WebBird on August 05, 2009, 12:24:14 PM
With the WbRandomSection snippet you can, but you can't give a list of sections to choose from. Maybe in the next version. :-D
Title: Re: Snippet: WbRandomSection
Post by: mr-fan on August 25, 2009, 08:13:34 AM
for WB 2.8 there was a missing index.php file in the zip...

attached file with index.php

regards martin (thanks to doc - he figured this out...)



[gelöscht durch Administrator]
Title: Re: Snippet: WbRandomSection
Post by: WebBird on August 26, 2009, 10:52:01 AM
Updated version 0.3 uploaded to AMASP.
Title: Re: Snippet: WbRandomSection
Post by: mad_nico on December 01, 2013, 06:54:38 PM
Hi,

I use this snippet for different projects. When I use this snippet together with other modules, I get an PHP error:

Type   E_NOTICE
Message   Undefined variable: types
Line:File   48 : .../modules/wbrandomsection/include.php

I know, this is not a real error, just an notice, but even if I set the PHP messages to E_ALL^E_NOTICE, this mesage still apperars at the web-frontend. Has anybody an idea, how to stop this messages?

cheers, Nico