Zikula_Core
[ class tree: Zikula_Core ] [ index: Zikula_Core ] [ all elements ]

Source for file pnBlocks.php

Documentation is available at pnBlocks.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright (c) 2001, Zikula Development Team
  6.  * @link http://www.zikula.org
  7.  * @version $Id: pnBlocks.php 24342 2008-06-06 12:03:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @package Zikula_Core
  10.  * @subpackage pnAPI
  11.  */
  12.  
  13. /**
  14.  * display all blocks in a block position
  15.  * @param $side block position to render
  16.  */
  17. function pnBlockDisplayPosition($side$echo true$implode true)
  18. {
  19.     static $blockplacements array();
  20.     static $positions array();
  21.     static $modname;
  22.     static $currentlang;
  23.     static $func;
  24.     static $type;
  25.  
  26.     if (!isset($side))
  27.         return null;
  28.  
  29.     pnModDBInfoLoad('Blocks''Blocks');
  30.  
  31.     // get the block position
  32.     if (empty($positions)) {
  33.         $positions DBUtil::selectObjectArray('block_positions'nullnull-1-1'name');
  34.     }
  35.     if (!isset($positions[$side])) {
  36.         return;
  37.     }
  38.     if (!isset($modname)) {
  39.         $modname pnModGetName();
  40.     }
  41.  
  42.     // get the blocks in this block position
  43.     if (empty($blockplacements)) {
  44.         $blockplacements DBUtil::selectObjectArray('block_placements'null'pn_order');
  45.     }
  46.  
  47.     // get variables from input
  48.     if (!isset($func)) {
  49.         $func FormUtil::getPassedValue('func''main''GETPOST');
  50.     }
  51.     if (!isset($type)) {
  52.         $type FormUtil::getPassedValue('type''user''GETPOST');
  53.     }
  54.  
  55.     // loop around the blocks display only the ones we need
  56.     if (!isset($currentlang)) {
  57.         $currentlang pnUserGetLang();
  58.     }
  59.     $blockoutput array();
  60.     foreach ($blockplacements as $blockplacement{
  61.         // don't display a block if it's not in this block position
  62.         if ($blockplacement['pid'!= $positions[$side]['pid']continue;
  63.         // get the full block info
  64.         $blockinfo pnBlockGetInfo($blockplacement['bid']);
  65.         // block filtering
  66.         if (!empty($blockinfo['filter']['modules']||
  67.             !empty($blockinfo['filter']['type']||
  68.             !empty($blockinfo['filter']['func']||
  69.             !empty($blockinfo['filter']['customargs'])) {
  70.             // check the module name
  71.             if (!empty($blockinfo['filter']['modules']&& !in_array($modname$blockinfo['filter']['modules'])) continue;
  72.             // check the function type
  73.             if (!empty($blockinfo['filter']['type'])) {
  74.                 $blockinfo['filter']['type'explode(','$blockinfo['filter']['type']);
  75.                 if (!in_array($type$blockinfo['filter']['type'])) continue;
  76.             }
  77.             // check the function name
  78.             if (!empty($blockinfo['filter']['functions'])) {
  79.                 $blockinfo['filter']['functions'explode(','$blockinfo['filter']['functions']);
  80.                 if (!in_array($func$blockinfo['filter']['functions'])) continue;
  81.             }
  82.             if (!empty($blockinfo['filter']['customargs'])) {
  83.                 $blockinfo['filter']['customargs'explode (','$blockinfo['filter']['customargs']);
  84.                 $customargs array();
  85.                 static $filtervars array('module''name''type''func''theme''authid');
  86.                 foreach ($_GET as $var => $value{
  87.                     if (!in_array($var$filtervars)) {
  88.                         $customargs[DataUtil::formatForOS(strip_tags($var)).'='.DataUtil::formatForOS(strip_tags($value));
  89.                     }
  90.                 }
  91.                 if (!array_intersect($customargs$blockinfo['filter']['customargs'])) continue;
  92.             }
  93.         }
  94.         // dont display the block if it's not active or not in matching langauge
  95.         if (!$blockinfo['active'|| (!empty($blockinfo['language']&& $blockinfo['language'!= $currentlang)) continue;
  96.         $blockinfo['position'=  $positions[$side]['name'];
  97.         // get the module info and display the block
  98.         $modinfo pnModGetInfo($blockinfo['mid']);
  99.         if (!$modinfo{
  100.             // Assume core
  101.             $modinfo['name''Legacy';
  102.         }
  103.         if ($echo{
  104.             echo pnBlockShow($modinfo['name']$blockinfo['bkey']$blockinfo);
  105.         else {
  106.             $blockoutput[pnBlockShow($modinfo['name']$blockinfo['bkey']$blockinfo);
  107.         }
  108.     }
  109.  
  110.     if ($echo{
  111.         return;
  112.     else {
  113.         if ($implode{
  114.              return implode("\n"$blockoutput);
  115.         else {
  116.              return $blockoutput;
  117.         }
  118.     }
  119. }
  120.  
  121. /**
  122.  * show a block
  123.  *
  124.  * @param string $modname module name
  125.  * @param string $block name of the block
  126.  * @param array $blockinfo information parameters
  127.  * @return mixed blockinfo array or null
  128.  */
  129. function pnBlockShow($modname$block$blockinfo array())
  130. {
  131.     if (empty($modname|| $modname == 'Core'{
  132.         $modname 'Legacy';
  133.     }
  134.  
  135.     global $blocks_modules;
  136.  
  137.     pnBlockLoad($modname$block);
  138.  
  139.     $displayfunc "{$modname}_{$block}block_display";
  140.  
  141.     if (function_exists($displayfunc)) {
  142.         // New-style blocks
  143.         return $displayfunc($blockinfo);
  144.     else {
  145.         // Old-style blocks
  146.         if (isset($blocks_modules[0][$block]['func_display'])) {
  147.             return $blocks_modules[0][$block]['func_display']($blockinfo);
  148.         else {
  149.             if (SecurityUtil::checkPermission('.*''.*'ACCESS_ADMIN)) {
  150.                 $blockinfo['title'"Block Type $block Not Found";
  151.                 $blockinfo['content'"The block type $block doesn't seem to exist.  Please check corresponding blocks directory.";
  152.                 return pnBlockThemeBlock($blockinfo);
  153.             }
  154.         }
  155.     }
  156. }
  157.  
  158. /**
  159.  * Display a block based on the current theme
  160.  *
  161.  */
  162. function pnBlockThemeBlock($row)
  163. {
  164.     static $themeinfo$themedir$upb$downb;
  165.  
  166.     if (!isset($row['bid'])) {
  167.         $row['bid''';
  168.     }
  169.     if (!isset($row['title'])) {
  170.         $row['title''';
  171.     }
  172.  
  173.     if (!isset($themeinfo)) {
  174.         $themeinfo ThemeUtil::getInfo(ThemeUtil::getIDFromName(pnUserGetTheme()));
  175.         $themedir DataUtil::formatForOS($themeinfo['directory']);
  176.     }
  177.  
  178.     // check for collapsable menus being enabled, and setup the collapsable menu image.
  179.     if (!isset($upb)) {
  180.         if (file_exists('themes/'.$themedir.'/images/upb.gif')) {
  181.             $upb '<img src="themes/'.$themedir.'/images/upb.gif" alt="" />';
  182.         else {
  183.             $upb '<img src="images/global/upb.gif" alt="" />';
  184.         }
  185.     }
  186.     if (!isset($downb)) {
  187.         if (file_exists('themes/'.$themedir.'/images/downb.gif')) {
  188.             $downb '<img src="themes/'.$themedir.'/images/downb.gif" alt="" />';
  189.         else {
  190.             $downb '<img src="images/global/downb.gif" alt="" />';
  191.         }
  192.     }
  193.  
  194.     if (pnUserLoggedIn(&& pnModGetVar('Blocks''collapseable'== && isset($row['collapsable']&& ($row['collapsable'== '1')) {
  195.         if (pnCheckUserBlock($row== '1'{
  196.             if (!empty($row['title'])) {
  197.                 $row['minbox''<a href="' DataUtil::formatForDisplay(pnModURL ('Blocks''user''changestatus'array ('bid' => $row['bid']'authid' => pnSecGenAuthKey()))) '">' $upb '</a>';
  198.             }
  199.         else {
  200.             $row['content'='';
  201.             if (!empty($row['title'])) {
  202.                 $row['minbox''<a href="' DataUtil::formatForDisplay(pnModURL ('Blocks''user''changestatus'array ('bid' => $row['bid']'authid' => pnSecGenAuthKey()))) '">' $downb '</a>';
  203.             }
  204.         }
  205.     else {
  206.         $row['minbox''';
  207.     }
  208.     // end collapseable menu config
  209.  
  210.     if ($themeinfo['type'== 3{
  211.         return $GLOBALS['theme_engine']->themesidebox($row);
  212.     else if (function_exists('themesidebox')) {
  213.         return themesidebox($row);
  214.     }
  215.     return false;
  216. }
  217.  
  218. /**
  219.  * load a block
  220.  *
  221.  * @param string $modname module name
  222.  * @param string $block name of the block
  223.  * @return bool true on successful load, false otherwise
  224.  */
  225. function pnBlockLoad($modname$block)
  226. {
  227.     static $loaded array();
  228.  
  229.     if (empty($modname|| $modname == 'Core'{
  230.         $modname 'Legacy';
  231.     }
  232.  
  233.     if (isset($loaded["$modname/$block"])) {
  234.         return true;
  235.     }
  236.  
  237.     $modinfo  pnModGetInfo(pnModGetIdFromName($modname));
  238.     $moddir   DataUtil::formatForOS($modinfo['directory']);
  239.     $blockdir $moddir '/pnblocks';
  240.     $langdir  $moddir '/pnlang';
  241.     $incfile  DataUtil::formatForOS($block '.php');
  242.  
  243.     $files array();
  244.     if ($modinfo['type'== 3{
  245.         $files['system/' $blockdir '/' $incfile;
  246.         $rc Loader::loadOneFile ($files);
  247.         if (!$rc)
  248.             return false;
  249.     elseif ($modinfo['type'== || $modinfo['type'== 1{
  250.         $files['modules/' $blockdir '/' $incfile;
  251.         $rc Loader::loadOneFile ($files);
  252.         if (!$rc)
  253.             return false;
  254.     else {
  255.         // load any old style block - don't use loader because this will 
  256.         // break the scope of the blocks_modules variable
  257.         include 'config/blocks/' $incfile;
  258.     }
  259.  
  260.     $loaded["$modname/$block"1;
  261.  
  262.     $usrlang DataUtil::formatForOS(pnUserGetLang());
  263.     $syslang DataUtil::formatForOS(pnConfigGetVar('language'));
  264.  
  265.     // Load the block language files
  266.     if ($modinfo['type'== 3{
  267.         $files array();
  268.         // This directory is for easy of use when developing language packs.
  269.         // See http://community.zikula.org/index.php?module=Wiki&tag=LanguagePack
  270.         if (isset($GLOBALS['PNConfig']['System']['development']&& $GLOBALS['PNConfig']['System']['development'])
  271.         {
  272.             $files['config/languages/' $usrlang '/system/' $langdir '/' $usrlang '/' $incfile;
  273.         }
  274.         $files['system/' $langdir '/' $usrlang '/' $incfile;
  275.         $files['system/' $langdir '/' $syslang '/' $incfile;
  276.     elseif ($modinfo['type'== || $modinfo['type'== 1{
  277.         $files array();
  278.         // This directory is for easy of use when developing language packs.
  279.         // See http://community.zikula.org/index.php?module=Wiki&tag=LanguagePack
  280.         if (isset($GLOBALS['PNConfig']['System']['development']&& $GLOBALS['PNConfig']['System']['development'])
  281.         {
  282.             $files['config/languages/' $usrlang '/modules/' $langdir '/' $usrlang '/' $incfile;
  283.         }
  284.         $files['modules/' $langdir '/' $usrlang '/' $incfile;
  285.         $files['modules/' $langdir '/' $syslang '/' $incfile;
  286.     else {
  287.         $files array();
  288.         $files['config/blocks/lang/' $usrlang '/' $incfile;
  289.         $files['config/blocks/lang/' $syslang '/' $incfile;
  290.     }
  291.     Loader::loadOneFile ($files);
  292.  
  293.     // get the block info
  294.     $infofunc "{$modname}_{$block}block_info";
  295.     if (function_exists($infofunc)) {
  296.         $blocks_modules[$block$infofunc();
  297.     }
  298.  
  299.  
  300.     // set the module and keys for the new block
  301.     $blocks_modules[$block]['bkey'$block;
  302.     if (!isset($blocks_modules[$block]['module'])) {
  303.         $blocks_modules[$block]['module''Legacy';
  304.         $blocks_modules[$block]['mid'0;
  305.     else {
  306.         $blocks_modules[$block]['module'$modname;
  307.         $blocks_modules[$block]['mid'pnModGetIDFromName($modname);
  308.     }
  309.  
  310.     // merge the blockinfo in the global list of blocks
  311.     if (!isset($GLOBALS['blocks_modules'])) {
  312.         $GLOBALS['blocks_modules'array();
  313.     }
  314.     $GLOBALS['blocks_modules'][$blocks_modules[$block]['mid']][$block$blocks_modules[$block];
  315.  
  316.     // Initialise block if required (new-style)
  317.     $initfunc "{$modname}_{$block}block_init";
  318.     if (function_exists($initfunc)) {
  319.         $initfunc();
  320.     }
  321.  
  322.     return true;
  323. }
  324.  
  325. /**
  326.  * load all blocks
  327.  * @return array array of blocks
  328.  */
  329. function pnBlockLoadAll()
  330. {
  331.     static $blockdirs array();
  332.  
  333.     // Load new-style blocks from system and modules tree
  334.     $mods pnModGetAllMods();
  335.  
  336.     foreach ($mods as $mod{
  337.         $modname $mod['name'];
  338.         $moddir  DataUtil::formatForOS($mod['directory']);
  339.  
  340.         if (!isset($blockdirs[$modname])) {
  341.             $blockdirs[$modnamearray ();
  342.             $blockdirs[$modname]['system/' $moddir '/pnblocks';
  343.             $blockdirs[$modname]['modules/' $moddir '/pnblocks';
  344.  
  345.             foreach ($blockdirs[$modnameas $dir{
  346.                 if (file_exists($dir&& is_dir($dir&& is_readable($dir)) {
  347.                     $dh opendir($dir);
  348.                     while (($f readdir($dh)) !== false{
  349.                         if (substr($f-4== '.php'{
  350.                             $block substr($f,0,-4);
  351.                             pnBlockLoad($modname$block);
  352.                         }
  353.                     }
  354.                     closedir($dh);
  355.                 }
  356.             }
  357.         }
  358.     }
  359.  
  360.     $dir 'config/blocks';
  361.     if (file_exists($dir&& is_dir($dir&& is_readable($dir)) {
  362.         $dh opendir($dir);
  363.         while (($f readdir($dh)) !== false{
  364.             if (substr($f-4== '.php'{
  365.                 $block substr($f,0,-4);
  366.                 pnBlockLoad('Legacy'$block);
  367.             }
  368.         }
  369.         closedir($dh);
  370.     }
  371.  
  372.     // Return information gathered
  373.     return $GLOBALS['blocks_modules'];
  374. }
  375.  
  376. /**
  377.  * extract an array of config variables out of the content field of a
  378.  * block
  379.  *
  380.  * @param the content from the db
  381.  */
  382. function pnBlockVarsFromContent($content)
  383. {
  384.     // Assume serialized content ends in a ";" followed by some curly-end-braces
  385.     if (preg_match('/;}*$/'$content)) {
  386.         // Serialised content
  387.         $vars unserialize($content);
  388.         return $vars;
  389.     }
  390.  
  391.     // Unserialised content
  392.     $links explode("\n"$content);
  393.     $vars array();
  394.     foreach ($links as $link{
  395.         $link trim($link);
  396.         if ($link{
  397.             $var explode(':='$link);
  398.             if (isset($var[1])) {
  399.                 $vars[$var[0]] $var[1];
  400.             }
  401.         }
  402.     }
  403.  
  404.     return $vars;
  405. }
  406.  
  407. /**
  408.  * put an array of config variables in the content field of a block
  409.  *
  410.  * @param the config vars array, in key->value form
  411.  */
  412. function pnBlockVarsToContent($vars)
  413. {
  414.     return (serialize($vars));
  415. }
  416.  
  417. /**
  418.  * Checks if user controlled block state
  419.  *
  420.  * Checks if the user has a state set for a current block
  421.  * Sets the default state for that block if not present
  422.  *
  423.  * @access private
  424.  */
  425. function pnCheckUserBlock($row)
  426. {
  427.     if (!isset($row['bid'])) {
  428.         $row['bid''';
  429.     }
  430.     if (pnUserLoggedIn()) {
  431.         $uid pnUserGetVar('uid');
  432.         $pntable pnDBGetTables();
  433.         $column $pntable['userblocks_column'];
  434.         $where "WHERE $column[bid] = '".DataUtil::formatForStore($row['bid'])."'
  435.                   AND $column[uid] = '".DataUtil::formatForStore($uid)."'";
  436.  
  437.         $result DBUtil::selectObject('userblocks'$where);
  438.         if ($result === false{
  439.             LogUtil::registerError ('Error: ' $dbconn->ErrorNo(': ' $dbconn->ErrorMsg());
  440.             return true// FIXME: should this really return true (RNG)
  441.         }
  442.         if (!$result{
  443.             $uid DataUtil::formatForStore($uid);
  444.             $obj array('uid' => $uid,
  445.                          'bid' => $row['bid'],
  446.                          'active' => $row['defaultstate']);
  447.             if (!DBUtil::insertObject($obj'userblocks''bid'true)) {
  448.                 LogUtil::registerError ('DB Error ' $dbconn->ErrorNo(': ' $dbconn->ErrorMsg());
  449.                 return true// FIXME: should this really return true (RNG)
  450.             }
  451.             return true// FIXME: should this really return true (RNG)
  452.         else {
  453.             return $result['active'];
  454.         }
  455.     }
  456.  
  457.     return false;
  458. }
  459.  
  460.  
  461. /**
  462.  * get block information
  463.  * @param value the value to search for
  464.  * @param assocKey the field in which we look for the value (optional) (default='bid')
  465.  * @return array array of block information
  466.  */
  467. function pnBlocksGetInfo()
  468. {
  469.     pnModDBInfoLoad('Blocks''Blocks');
  470.     return DBUtil::selectObjectArray('blocks');
  471. }
  472.  
  473.  
  474. /**
  475.  * get block information
  476.  * @param value the value to search for
  477.  * @param assocKey the field in which we look for the value (optional) (default='bid')
  478.  * @return array array of block information
  479.  */
  480. function pnBlockGetInfo($value$assocKey='bid')
  481. {
  482.     static $blockinfo array();
  483.  
  484.     if (!isset($blockinfo[$assocKey]|| empty($blockinfo[$assocKey])) {
  485.         $blockinfo[$assocKeyarray();
  486.         $blocks pnBlocksGetInfo();
  487.         $ak array_keys($blocks);
  488.         foreach ($ak as $k{
  489.             $key $blocks[$k][$assocKey];
  490.             $blocks[$k]['filter'strlen($blocks[$k]['filter']? (array)unserialize($blocks[$k]['filter']array();
  491.             $blockinfo[$assocKey][$key$blocks[$k];
  492.         }
  493.     }
  494.  
  495.     if (isset($blockinfo[$assocKey][$value])) {
  496.         return $blockinfo[$assocKey][$value];
  497.     }
  498.  
  499.     return false;
  500. }
  501.  
  502. /**
  503.  * get block information
  504.  * @param title the block title
  505.  * @return array array of block information
  506.  */
  507. function pnBlockGetInfoByTitle($title)
  508. {
  509.     return pnBlockGetInfo($title'title');
  510. }
  511.  
  512.  
  513. /*
  514.  * alias to pnBlockDisplayPosition
  515.  */
  516. function blocks($side)
  517. {
  518.     return pnBlockDisplayPosition($side);
  519. }
  520.  
  521. /*
  522.  * alias to pnBlockDisplayPosition
  523.  */
  524. function themesideblock($row)
  525. {
  526.     return pnBlockThemeBlock($row);
  527. }

Documentation generated on Fri, 18 Jul 2008 21:52:45 +0200 by phpDocumentor 1.4.1