Give Your Clients even more Control of their Sidebar

This is a follow up post on Wayne's  Give Your Clients Control Of Their Sidebar post, we could also call it AdvancedContent module series.

Sometimes you find yourself in need for GCB's in your Template but the problem is you don't know how many will be used at last.
With help of AdvancedContent module and get_gcb_list plugin, both written by Georg Busch (NaN), you can finally controll this part to.

show_gcbs.jpg

As first you will need AdvancedContent module you can find it here (Note: the order in Forge is wrong, so if you use CMSMS 1.9.xx use AC 0.7.2) .

Next step would be creating the plugin.

<?php
#-------------------------------------------------------------------------------
#
# Smarty Plugin : {get_gcb_list}
# Purpose       : This plugin gets a list of global content blocks of CMSms 
#                      showing their names divided by a separator
# Author         : Georg Busch (NaN)
# Copyright     : 2010 Georg Busch (NaN)
# Version       : 1.0
# License       : GPL
#
#-------------------------------------------------------------------------------

function smarty_cms_function_get_gcb_list($params, &$smarty)
{
    $gcbs = cmsms()->GetGlobalContentOperations()->LoadHtmlBlobs();
    
    $delimiter = "<hr />";
    if (isset($params['delimiter']) && trim($params['delimiter']) != '')
        $delimiter = trim($params['delimiter']);
    
    $excl_prefix = array();
    $incl_prefix = array();
    $excl_sufix  = array();
    $incl_sufix  = array();
    
    if (isset($params['excl_prefix']))
        $excl_prefix = smarty_cms_function_get_gcb_list_CleanArray(explode(',', $params['excl_prefix']));
    if (isset($params['excl_sufix']))
        $excl_sufix = smarty_cms_function_get_gcb_list_CleanArray(explode(',', $params['excl_sufix']));
    if (isset($params['incl_prefix']))
        $incl_prefix = smarty_cms_function_get_gcb_list_CleanArray(explode(',', $params['incl_prefix']));
    if (isset($params['incl_sufix']))
        $incl_sufix = smarty_cms_function_get_gcb_list_CleanArray(explode(',', $params['incl_sufix']));
    
    $assign_as = 'string';
    if (isset($params['assign_as']))
        $assign_as = $params['assign_as'];
    
    $output = 'content'; // name, id, owner, modified_date, full_object	
    if (isset($params['output']))
        $output = $params['output'];
    
    $sort_by = 'id';
    if (isset($params['sort_by']))
        $sort_by = $params['sort_by'];
    
    $sort_order = 'asc';
    if (isset($params['sort_order']))
        $sort_order = $params['sort_order'];
    
    $gcb_array = array();
    foreach ($gcbs as $gcb) {
        $skip = false;
        foreach ($excl_prefix as $str) {
            if (startswith($gcb->name, $str)) {
                $skip = true;
                break;
            }
        }
        if ($skip)
            continue;
        
        foreach ($incl_prefix as $str) {
            if (!startswith($gcb->name, $str)) {
                $skip = true;
                break;
            }
        }
        if ($skip)
            continue;
        
        foreach ($excl_sufix as $str) {
            if (endswith($gcb->name, $str)) {
                $skip = true;
                break;
            }
        }
        if ($skip)
            continue;
        
        foreach ($incl_sufix as $str) {
            if (!endswith($gcb->name, $str)) {
                $skip = true;
                break;
            }
        }
        if ($skip)
            continue;
        
        switch ($sort_by) {
            case 'name':
                if ($output == 'full_object') {
                    $gcb_array[$gcb->name] = $gcb;
                    break;
                }
                $gcb_array[$gcb->name] = $gcb->$output;
                break;
            
            case 'modified_date':
                if ($output == 'full_object') {
                    $gcb_array[$gcb->modified_date] = $gcb;
                    break;
                }
                $gcb_array[$gcb->modified_date] = $gcb->$output;
                break;
            
            case 'owner':
            case 'owner+create_date':
            case 'owner+id':
                if ($output == 'full_object') {
                    $gcb_array[$gcb->owner . '_' . $gcb->id] = $gcb;
                    break;
                }
                $gcb_array[$gcb->owner . '_' . $gcb->id] = $gcb->$output;
                break;
            
            case 'owner+name':
                if ($output == 'full_object') {
                    $gcb_array[$gcb->owner . '_' . $gcb->name] = $gcb;
                    break;
                }
                $gcb_array[$gcb->owner . '_' . $gcb->name] = $gcb->$output;
                break;
            
            case 'owner+modified_date':
                if ($output == 'full_object') {
                    $gcb_array[$gcb->owner . '_' . $gcb->modified_date] = $gcb;
                    break;
                }
                $gcb_array[$gcb->owner . '_' . $gcb->modified_date] = $gcb->$output;
                break;
            
            case 'id':
            case 'create_date':
            default:
                if ($output == 'full_object') {
                    $gcb_array[$gcb->id] = $gcb;
                    break;
                }
                $gcb_array[$gcb->id] = $gcb->$output;
                break;
        }
    }
    
    if ($sort_order == 'desc')
        krsort($gcb_array);
    else
        ksort($gcb_array);
    
    if (isset($params['assign'])) {
        if ($assign_as == "array") {
            $smarty->assign($params['assign'], $gcb_array);
            return;
        }
        $smarty->assign($params['assign'], implode($delimiter, $gcb_array));
        return;
    }
    return implode($delimiter, $gcb_array);
}

/** 
 * Checks if a var is empty. <br /> 
 * If $var is an array it recursivley checks all elements. 
 * 
 * @since 1.0 
 * @access public 
 * 
 * @param mixed &$var - the var to check for empty value(s) 
 * @param boolean $trim - true to trim off spaces 
 * @param boolean $unset_empty_indexes - true to delete empty elements from array 
 * @return boolean - true if empty, false if not 
 */

function smarty_cms_function_get_gcb_list_IsVarEmpty(&$var, $trim = true, $unset_empty_indexes = false)
{
    if (is_array($var)) {
        foreach ($var as $k => $v) {
            if (!smarty_cms_function_get_gcb_list_IsVarEmpty($v)) {
                return false;
            }
            
            if ($unset_empty_indexes) {
                unset($var[$k]);
            }
            return true;
        }
    } else if ($trim && trim($var) == '') {
        return true;
    } else if ($var == '') {
        return true;
    }
    return false;
}

/** 
 * Removes empty elements from an array. <br /> 
 * (can be useful when using function explode to create the array from a csv) 
 * 
 * @since 1.0 
 * @access public 
 * 
 * @param array $array - the array to clean up 
 * @return array - an array without empty elements or an empty array 
 */

function smarty_cms_function_get_gcb_list_CleanArray($array)
{
    if (is_array($array)) {
        foreach ($array as $k => $v) {
            if (smarty_cms_function_get_gcb_list_IsVarEmpty($v, true, true)) {
                unset($array[$k]);
            } else {
                if (is_array($v)) {
                    $v = smarty_cms_function_get_gcb_list_CleanArray($v);
                    if (smarty_cms_function_get_gcb_list_IsVarEmpty($v, true, true)) {
                        unset($array[$k]);
                    } else {
                        $array[$k] = $v;
                    }
                }
            }
        }
        return $array;
    }
    return array();
}

/** 
 * Checks if a value is really meant to be "true". <br /> 
 * Can be usefull when checking smarty params for the value true 
 * 
 * @since 1.0 * @access public 
 * 
 * @param mixed $value - the value to check 
 * @return bool 
 */

function smarty_cms_function_get_gcb_list_IsTrue($value)
{
    return (strtolower($value) === 'true' || $value === 1 || $value === '1' || $value === true);
}

/**
 * Checks if a value is really meant to be "false". <br /> 
 * Can be usefull when checking smarty params for the value false 
 * 
 * @since 1.0 
 * @access public 
 * 
 * @param mixed $value - the value to check 
 * @return bool 
 */

function smarty_cms_function_get_gcb_list_IsFalse($value)
{
    return (strtolower($value) === 'false' || $value === '0' || $value === 0 || $value === false || $value === '');
}

function smarty_cms_help_function_get_gcb_list()
{
    
?>
<p>
    Prints out certain properties of global content blocks
</p>
<p>
    Usage: {get_gcb_list <em>[params]</em>}
</p>
<p>
    Params:
</p>
<ul>
    <li>
        <tt>delimiter</tt><em>(string)</em>
        <ul>
            <li>
                The delimiter that separates the global content blocks.
                <br />
                Possible value can be any sign or text.
                <br />
                Default is a <hr />
            </li>
        </ul>
        <br />
    </li>
    <li>
        <tt>excl_prefix</tt><em>(string)</em>
        <ul>
            <li>
                A prefix of the names of the global content block that will not be shown.
                <br />
                Possible value can be any sign or text.
            </li>
        </ul>
        <br />
    </li>
    <li>
        <tt>excl_sufix</tt><em>(string)</em>
        <ul>
            <li>
                A sufix of the names of the global content block that will not be shown.
                <br />
                Possible value can be any sign or text.
            </li>
        </ul>
        <br />
    </li>
    <li>
        <tt>incl_prefix</tt><em>(string)</em>
        <ul>
            <li>
                A prefix of the names of the global content block that will be shown.
                <br />
                Possible value can be any sign or text.
            </li>
        </ul>
        <br />
    </li>
    <li>
        <tt>incl_sufix</tt><em>(string)</em>
        <ul>
            <li>
                A sufix of the names of the global content block that will be shown.
                <br />
                Possible value can be any sign or text.
            </li>
        </ul>
        <br />
    </li>
    <li>
        <tt>output</tt><em>(string)</em>
        <ul>
            <li>
                A property of the global content blocks that will be printed out.
                <br />
                Possible values are <tt>content, name, id, owner, modified_date, full_object</tt>
                <br />
                Default is <tt>content</tt>
            </li>
        </ul>
        <br />
    </li>
    <li>
        <tt>sort_by</tt><em>(string)</em>
        <ul>
            <li>
                Specify by what property the global content blocks will be sorted.
                <br />
                Possible values are <tt>content, name, id, owner, modified_date</tt>.
                <br />
                Default is <tt>id</tt>
            </li>
        </ul>
        <br />
    </li>
    <li>
        <tt>sort_order</tt><em>(string)</em>
        <ul>
            <li>
                Specify if sorted ascending or descending.
                <br />
                Possible values are <tt>asc, desc</tt>.
                <br />
                Default is <tt>asc</tt>
            </li>
        </ul>
        <br />
    </li>
    <li>
        <tt>assign</tt><em>(string)</em>
        <ul>
            <li>
                Use this to assign the ouptut to a smarty variable.
                <br />
                Possible value can be any text (alpha numeric characters only).
                <br />
                Be sure not to override existing varnames.
            </li>
        </ul>
        <br />
    </li>
    <li>
        <tt>assign_as</tt><em>(string)</em>
        <ul>
            <li>
                Use this to specify if all content blocks will be returned as text or as array.
                <br />
                Possible values can be <tt>string, array</tt>.
                <br />
                If array is used you should also use the param assign.
            </li>
        </ul>
        <br />
    </li>
</ul>
<?php
}
function smarty_cms_about_function_get_gcb_list()
{
?>
<p>
    Author: Georg Busch (NaN) <georg.busch@gmx.net>
</p>
<p>
    Version: 1.0
</p>
<?php
}
?>

Copy the code above and save it as function.get_gcb_list.php and upload to your /plugins folder.

Now open up your page Template and add following code where you want your GCB's to show.
Note that {ldelim}get_gcb_list{rdelim} plugin is called inside Content Block tag with ::: as smarty delimiter, for more information about that you should read AdvancedContent module Help.

{* define content block and assign the content to a variable *}
{content block="global_content_blocks" page_tab="Sidebar" block_type="select_multiple" sortable="true" items=":::get_gcb_list delimiter='|' output='name':::" assign="global_content_blocks" smarty=true}
{* content will be divided on the basis of the delimiter into an array *}
{assign var="global_content_blocks_array" value="|"|explode:$global_content_blocks}

{* pass array *}
{foreach from=$global_content_blocks_array item="block_name"}
    {if $block_name|strip != ''}
    {* call the GCB and assign an array (current_gcb) *}
    {global_content name="$block_name" assign="current_gcb"}
        {if $current_gcb|strip != ''}
            <div class="your_gcb_wrapper_class">
                {$current_gcb} {* show gcb *}
            </div>
        {/if}
    {/if}
{/foreach} 

With plugin parameter incl_prefix you can show only GCB's with specific prefix for example sidebar_

Thats it, after switching your Content pages to type:Advanced Content you will have nice sortable list of GCB's available when editing you pages.

ALL-INKL.COM - Webhosting Server Hosting Domain Provider

Comments