Log in | Register | Lost password

Bottom
Random block
  • Posted: 26.09.2006, 21:55
     
    Converted
    rank:
    12
    registered:
     March 2009
    Status:
    offline
    last visit:
    Posts:
    0
    hi to all!
    what about random block function in pagesetter 6.3.x? is it working? (and.. well.. how?)

    thanks!
  • Posted: 27.09.2006, 05:31
     
    Converted
    rank:
    12
    registered:
     March 2009
    Status:
    offline
    last visit:
    Posts:
    0
    I'm using it in one of the 6.3 betas and it works great. I'd like to have some filtering abilities and maybe display a random list of x publications, but as it is, it works fine.
  • Posted: 27.09.2006, 09:21
     
    Converted
    rank:
    12
    registered:
     March 2009
    Status:
    offline
    last visit:
    Posts:
    0
    ok, but... how dows it works? what is the command to use?
    thanks.
  • Posted: 28.09.2006, 01:19
     
    Converted
    rank:
    12
    registered:
     March 2009
    Status:
    offline
    last visit:
    Posts:
    0
    Hi. I've been working with the CVS random pub block for the last couple of days as well. The block as-is gave me some problems, but I'm using it with 6.2 so that might explain part of that.

    First problem is that it was only randomizing the first two publications of the type. The problem was here:

    Code

    /**

    * random publication id

    */

    function pagesetter_randompub_random_index($tid)

    {

    $pubCount = pnModAPIFunc('pagesetter', 'user', 'getPubList',

    array('tid' => $tid,

    'countOnly' => true));

    return mt_rand(0, $pubCount-1);

    }


    The problem with this code is that (at least in 6.2) $pubCount is a two element array, with the 2nd element containing the list of publication arrays. So it would only pick a random number between 1 and 2. Not very effective. So I changed it to

    Code

    /**

    * random publication id

    */

    function pagesetter_randompub_random_index($tid)

    {

    $pubCount = pnModAPIFunc('pagesetter', 'user', 'getPubList',

    array('tid' => $tid,

    'countOnly' => true));

    $adjcount = sizeof($pubCount['publications'])-1;



    return mt_rand(0, $adjcount);

    }


    Second problem is that the list of publications it built to choose randomly from obeyed the "items per page" setting of the publication. So if you had paging set to 10, it would only randomize the first 10 publications. So I modified various parts of the script to be passed the 'noOfItems' variable, and hard coded it at '0', which means "all".

    Third, I needed to be able to use a filter. Not all of the publications were eligible to be randomized. So I modified various parts of the script to be passed the filters as well, allowing filters to be added in the block like the lists block can.

    Here's my complete modified script here. Barely tested, but seems to work well.

    Code

    <?php

    // $Id: randompub.php,v 1.3 2006/05/30 19:52:10 jornlind Exp $

    // based on pub.php 1.3

    // =======================================================================

    // Original Author of file: tjreo

    // Purpose of file: Show a random Pagesetter Publication in a Block

    // ----------------------------------------------------------------------

    // For POST-NUKE Content Management System

    // Copyright (C) 2002 by the PostNuke Development Team.

    // http://www.postnuke.com/

    // ----------------------------------------------------------------------

    // LICENSE

    //

    // This program is free software; you can redistribute it and/or

    // modify it under the terms of the GNU General Public License (GPL)

    // as published by the Free Software Foundation; either version 2

    // of the License, or (at your option) any later version.

    //

    // This program is distributed in the hope that it will be useful,

    // but WIthOUT ANY WARRANTY; without even the implied warranty of

    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

    // GNU General Public License for more details.

    //

    // To read the license please visit http://www.gnu.org/copyleft/gpl.html

    // =======================================================================



    /**

    * initialise block

    */

    function pagesetter_randompubblock_init()

    {

    // Security

    pnSecAddSchema('pagesetter:RandomPubblock:', 'Block title:Block Id:Type Id');

    }



    /**

    * get information on block

    */

    function pagesetter_randompubblock_info()

    {

    // Values

    return array('text_type' => 'pagesetterPublication',

    'module' => 'pagesetter',

    'text_type_long' => 'Display a random pagesetter publication',

    'allow_multiple' => true,

    'form_content' => false,

    'form_refresh' => false,

    'show_preview' => true);

    }



    /**

    * random publication id

    */

    function pagesetter_randompub_random_index($tid, $filters)

    {

    $pubCount = pnModAPIFunc('pagesetter', 'user', 'getPubList',

    array('tid' => $tid,

    'filterSet' => $filters,

    'noOfItems' => 0,

    'countOnly' => true));

    $adjcount = sizeof($pubCount['publications'])-1;

    $index = mt_rand(0, $adjcount);

    return $index;

    }





    /**

    * display block

    */

    function pagesetter_randompubblock_display($blockinfo)

    {



    // Get variables from content block

    $vars = pnBlockVarsFromContent($blockinfo['content']);



    if (!pnModAPILoad('pagesetter', 'user'))

    return pagesetterErrorPage(__FILE__, __LINE__, 'Failed to load Pagesetter user API');



    // Defaults

    $tid = $vars['tid'];

    $tpl = $vars['tpl'];

    $filterStr = $vars['filters'];



    if (empty($filterStr))

    $filters = null;

    else

    $filters = preg_split("/\s*&\s*/", $filterStr);



    // Fetch pub. pid

    $index = pagesetter_randompub_random_index($tid,$filters);

    $pub = pnModAPIFunc('pagesetter', 'user', 'getPubList',

    array('tid' => $tid,

    'filterSet' => $filters,

    'offsetItems' => $index,

    'noOfItems' => 1));

    $pid = $pub['publications'][0]['pid'];



    if ( !isset($tid) || !isset($pid) || !isset($tpl) )

    {

    $blockinfo['content'] = _PGRANPUBBLOCKEDITBLOCK;

    return themesideblock($blockinfo);

    }



    // Security check

    if (!pnSecAuthAction(0, 'pagesetter:RandomPubblock:', "$blockinfo[title]:$blockinfo[bid]:$tid", ACCESS_READ))

    return;



    $url = pnModURL('pagesetter', 'user', 'viewpub', array('tid' => $tid, 'pid' => $pid));

    $url = htmlspecialchars($url);



    // get the formatted publication

    $pubFormatted = pnModAPIFunc( 'pagesetter',

    'user',

    'getPubFormatted',

    array('tid' => $tid,

    'pid' => $pid,

    'format' => $tpl,

    'coreExtra' => array( 'page' => 0,

    'baseURL' => $url,

    'format' => $tpl) ));



    // Populate block info and pass to theme

    $blockinfo['content'] = $pubFormatted;

    return themesideblock($blockinfo);

    }





    /**

    * modify block settings

    * This is the function that is called to display the Admin / Blocks / Pagesetter Publication block

    */

    function pagesetter_randompubblock_modify($blockinfo)

    {

    // Create output object

    $output = new pnHTML();



    // Get current content

    $vars = pnBlockVarsFromContent($blockinfo['content']);



    // Defaults

    if (!isset($vars['tid']))

    $vars['tid'] = pnModGetVar('pagesetter','frontpagePubType');

    /* if (!isset($vars['pid']))

    $vars['pid'] = 1; */

    if (!isset($vars['tpl']))

    $vars['tpl'] = 'randompub';



    $filters = $vars['filters'];



    if (!pnModAPILoad('pagesetter', 'admin'))

    return pagesetterErrorPage(__FILE__, __LINE__, 'Failed to load Pagesetter admin API');



    // Create row for "Publication type"

    // $output->Linebreak();

    $pubTypesData = pnModAPIFunc('pagesetter',

    'admin',

    'getPublicationTypes');



    $pubTypes = array();

    foreach ($pubTypesData as $pubType)

    {

    $pubTypes[] = array( 'name' => $pubType['title'],

    'id' => $pubType['id'] );

    if ($pubType['id'] == $vars['tid'])

    $pubTypes[count($pubTypes)-1]['selected'] = 1;

    }



    $row = array();

    $output->SetOutputMode(_PNH_RETURNOUTPUT);

    $row[] = $output->Text(_PGRANPUBBLOCKPUBTYPE);

    $row[] = $output->FormSelectMultiple('tid', $pubTypes);

    $output->SetOutputMode(_PNH_KEEPOUTPUT);



    // Add row

    $output->SetInputMode(_PNH_VERBATIMINPUT);

    $output->TableAddRow($row, 'left');

    $output->SetInputMode(_PNH_PARSEINPUT);



    // Add filter



    $row = array();

    $output->SetOutputMode(_PNH_RETURNOUTPUT);

    $row[] = $output->Text(_PGBLOCKLISTFILTER);

    $row[] = $output->FormText('filters', $filters);

    $output->SetOutputMode(_PNH_KEEPOUTPUT);



    $output->SetInputMode(_PNH_VERBATIMINPUT);

    $output->TableAddRow($row, 'left');

    $output->SetInputMode(_PNH_PARSEINPUT);



    /* // Create row for Publication

    $row = array();

    $output->SetOutputMode(_PNH_RETURNOUTPUT);

    $row[] = $output->Text(_PGPUBBLOCKPUB);

    $row[] = $output->FormText('pid',$vars['pid']);

    $output->SetOutputMode(_PNH_KEEPOUTPUT);



    // Add row

    $output->SetInputMode(_PNH_VERBATIMINPUT);

    $output->TableAddRow($row, 'left');

    $output->SetInputMode(_PNH_PARSEINPUT);

    */

    // Create row for Template to use

    $row = array();

    $output->SetOutputMode(_PNH_RETURNOUTPUT);

    $row[] = $output->Text(_PGRANPUBBLOCKTEMPLATE);

    $row[] = $output->FormText('tpl',$vars['tpl']);

    $output->SetOutputMode(_PNH_KEEPOUTPUT);



    // Add row

    $output->SetInputMode(_PNH_VERBATIMINPUT);

    $output->TableAddRow($row, 'left');

    $output->SetInputMode(_PNH_PARSEINPUT);



    // Return output

    return $output->GetOutput();

    }



    /**

    * update block settings

    */

    function pagesetter_randompubblock_update($blockinfo)

    {

    $filters = pnVarCleanFromInput('filters');



    $vars = array('tid' => pnVarCleanFromInput('tid'),

    'filters' => $filters,

    //'pid' => pnVarCleanFromInput('pid'),

    'tpl' => pnVarCleanFromInput('tpl'));



    $blockinfo['content'] = pnBlockVarsToContent($vars);



    return $blockinfo;

    }



    ?>
  • Posted: 15.12.2008, 03:26
     
    Converted
    rank:
    12
    registered:
     March 2009
    Status:
    offline
    last visit:
    Posts:
    0
    Thanks bronto... This is exactly what i was looking for...

    One thing though, you need to remember to add this to your language file for randompub...

    Code

    define('_PGBLOCKLISTFILTER', 'List filter as used in URL, separated by "&", but without "filter=" (e.g. "country:eq:DK")');

Template courtesy of Designs By Darren.