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

Source for file pnadminapi.php

Documentation is available at pnadminapi.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  * @copyright (c) 2001, Zikula Development Team
  5.  * @link http://www.zikula.org
  6.  * @version $Id: pnadminapi.php 24342 2008-06-06 12:03:14Z markwest $
  7.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  8.  * @package Zikula_System_Modules
  9.  * @subpackage Blocks
  10.  */
  11.  
  12. /**
  13.  * update attributes of a block
  14.  * @author Jim McDonald
  15.  * @author Robert Gasch
  16.  * @param int $args ['bid'] the ID of the block to update
  17.  * @param string $args ['title'] the new title of the block
  18.  * @param string $args ['positions'] the new positions of the block
  19.  * @param string $args ['url'] the new URL of the block
  20.  * @param string $args ['language'] the new language of the block
  21.  * @param string $args ['content'] the new content of the block
  22.  * @return bool true on success, false on failure
  23.  */
  24. function blocks_adminapi_update($args)
  25. {
  26.     // Optional arguments
  27.     if (!isset($args['url'])) {
  28.         $args['url''';
  29.     }
  30.     if (!isset($args['content'])) {
  31.         $args['content''';
  32.     }
  33.  
  34.     // Argument check
  35.     if (!isset($args['bid'])          ||
  36.         !is_numeric($args['bid'])     ||
  37.         !isset($args['content'])      ||
  38.         !isset($args['title'])        ||
  39.         !isset($args['language'])     ||
  40.         !isset($args['collapsable'])  ||
  41.         !isset($args['defaultstate'])) {
  42.         return LogUtil::registerError (_MODARGSERROR);
  43.     }
  44.  
  45.     $block DBUtil::selectObjectByID ('blocks'$args['bid']'bid');
  46.  
  47.     // Security check
  48.     // this function is called durung the init process so we have to check in _PNINSTALLVER
  49.     // is set as alternative to the correct permission check
  50.     if (!defined('_PNINSTALLVER'&& !SecurityUtil::checkPermission('Blocks::'"$block[bkey]:$block[title]:$block[bid]"ACCESS_EDIT)) {
  51.         return LogUtil::registerPermissionError();
  52.     }
  53.  
  54.     $item array('bid' => isset($args['bid']$args['bid'$block['bid'],
  55.                   'content' => isset($args['content']$args['content'$block['content'],
  56.                   'title' => isset($args['title']$args['title'$block['title'],
  57.                   'filter' => isset($args['filter']serialize($args['filter']$block['filter'],
  58.                   'url' => isset($args['url']$args['url'$block['url'],
  59.                   'refresh' => isset($args['refresh']$args['refresh'$block['refresh'],
  60.                   'language' => isset($args['language']$args['language'$block['language'],
  61.                   'collapsable' => isset($args['collapsable']$args['collapsable'$block['collapsable'],
  62.                   'defaultstate' => isset($args['defaultstate']$args['defaultstate'$block['defaultstate']);
  63.  
  64.     $res DBUtil::updateObject ($item'blocks''''bid');
  65.     if (!$res{
  66.         return LogUtil::registerError (_UPDATEFAILED);
  67.     }
  68.  
  69.     // leave unchanged positions as is, delete removed positions from placements table 
  70.     // and add placement for new positions
  71.     if (isset($args['positions'])) {
  72.         // Get all existing block positions. We do not use the userapi function here because we need
  73.         // an associative array for the next steps: key = pid (position id)
  74.         $allblockspositions DBUtil::selectObjectArray('block_positions'null'pid'-1-1'pid'null);
  75.         foreach ($allblockspositions as $positionid => $blockposition{
  76.             if(in_array($positionid$args['positions'])) {
  77.                 // position name is present in the array submitted from the user
  78.                 $where "WHERE pn_pid = '" DataUtil::formatForStore($positionid'\'';
  79.                 $blocksinposition DBUtil::selectObjectArray('block_placements'$where'pn_order'-1-1'bid');
  80.                 if(array_key_exists($item['bid']$blocksinposition)) {
  81.                     // block is already in this position, placement did not change, this means we do nothing
  82.                 else {
  83.                     // add the block to the given position as last entry (max(pn_order) +1
  84.                     $newplacement array('pid'   => $blockposition['pid'],
  85.                                           'bid'   => $item['bid'],
  86.                                           'order' => count($blocksinpositions));
  87.                     $res DBUtil::insertObject($newplacement'block_placements''bid'true);
  88.                     if (!$res{
  89.                         return LogUtil::registerError (_INSERTFAILED);
  90.                     }
  91.                 }
  92.             else {
  93.                 // position name is NOT present in the array submitted from the user
  94.                 // delete the block id from the placements table for this position
  95.                 $where '(pn_bid = \'' DataUtil::formatForStore($item['bid']'\' AND pn_pid = \'' DataUtil::formatForStore($blockposition['pid']'\')';
  96.                 $res DBUtil::deleteWhere('block_placements'$where);
  97.                 if (!$res{
  98.                     return LogUtil::registerError (_UPDATEFAILED);
  99.                 }
  100.             }
  101.         }
  102.     }
  103.  
  104.     // call update hooks
  105.     pnModCallHooks('item''update'$args['bid']array('module' => 'Blocks'));
  106.  
  107.     return true;
  108. }
  109.  
  110. /**
  111.  * create a new block
  112.  * @author Jim McDonald
  113.  * @author Robert Gasch
  114.  * @param string $block ['title'] the title of the block
  115.  * @param int $block ['mid'] the module ID of the block
  116.  * @param string $block ['language'] the language of the block
  117.  * @param int $block ['bkey'] the key of the block
  118.  * @return mixed block Id on success, false on failure
  119.  */
  120. function blocks_adminapi_create($args)
  121. {
  122.     // Argument check
  123.     if ((!isset($args['title']))        ||
  124.         (!isset($args['mid']))          ||
  125.         (!isset($args['language']))     ||
  126.         (!isset($args['collapsable']))  ||
  127.         (!isset($args['defaultstate'])) ||
  128.         (!isset($args['bkey']))) {
  129.         return LogUtil::registerError (_MODARGSERROR);
  130.     }
  131.  
  132.     // Security check
  133.     if (!defined('_PNINSTALLVER'&& !SecurityUtil::checkPermission('Blocks::'"$args[bkey]:$args[title]:"ACCESS_ADD)) {
  134.         return LogUtil::registerPermissionError();
  135.     }
  136.  
  137.     // optional arguments
  138.     if (!isset($args['content']|| !is_string($args['content'])) {
  139.         $args['content''';
  140.     }
  141.  
  142.     $block array('title' => $args['title']'language' => $args['language']'collapsable' => $args['collapsable'],
  143.                    'mid' => $args['mid']'defaultstate' => $args['defaultstate']'bkey' => $args['bkey'],
  144.                    'content' => $args['content']);
  145.  
  146.     $block['url']         '';
  147.     $block['filter']      '';
  148.     $block['active']      1;
  149.     $block['refresh']     3600;
  150.     $block['last_update'date('Y-m-d H:i:s');
  151.     $block['active']      1;
  152.     $res DBUtil::insertObject ($block'blocks''bid');
  153.  
  154.     if (!$res{
  155.         return LogUtil::registerError (_CREATEFAILED);
  156.     }
  157.  
  158.     // empty block positions for this block
  159.     if (isset($args['positions'])) {
  160.         // add new block positions
  161.         $blockplacments array();
  162.         foreach ($args['positions'as $position{
  163.             $blockplacments[array('bid' => $block['bid']'pid' => $position);
  164.         }
  165.         $res DBUtil::insertObjectArray($blockplacments'block_placements');
  166.         if (!$res{
  167.             return LogUtil::registerError (_CREATEFAILED);
  168.         }
  169.     }
  170.  
  171.     // Let other modules know we have created an item
  172.     pnModCallHooks('item''create'$block['bid']array('module' => 'Blocks'));
  173.  
  174.     return $block['bid'];
  175. }
  176.  
  177. /**
  178.  * Set a block's active state
  179.  * @author Robert Gasch
  180.  * @param int $args ['bid'] the ID of the block to deactivate
  181.  * @return bool true on success, false on failure
  182.  */
  183. {
  184.     if (!isset($block['bid']|| !is_numeric($block['bid'])) {
  185.         return LogUtil::registerError (_MODARGSERROR);
  186.     }
  187.     if (!isset($block['active']|| !is_numeric($block['active'])) {
  188.         return LogUtil::registerError (_MODARGSERROR);
  189.     }
  190.     $blockinfo pnBlockGetInfo($block['bid']);
  191.     if (!SecurityUtil::checkPermission('Blocks::'"$blockinfo[bkey]:$blockinfo[title]:$block[bid]"ACCESS_EDIT)) {
  192.         return LogUtil::registerPermissionError();
  193.     }
  194.  
  195.     // create a new object to ensure that we only update the 'active' field
  196.     $obj array();
  197.     $obj['bid']    $block['bid'];
  198.     $obj['active'$block['active'];
  199.     $res DBUtil::updateObject ($obj'blocks''''bid');
  200.  
  201.     return $res;
  202. }
  203.  
  204. /**
  205.  * deactivate a block
  206.  * @author Jim McDonald
  207.  * @author Robert Gasch
  208.  * @param int $args ['bid'] the ID of the block to deactivate
  209.  * @return bool true on success, false on failure
  210.  */
  211. {
  212.     $args['active'0;
  213.     $res = (boolean)blocks_adminapi_setActiveState ($args);
  214.  
  215.     if (!$res{
  216.         return LogUtil::registerError (_BLOCKS_DEACTIVATEERROR);
  217.     }
  218.  
  219.     return $res;
  220. }
  221.  
  222. /**
  223.  * activate a block
  224.  * @author Jim McDonald
  225.  * @author Robert Gasch
  226.  * @param int $args ['bid'] the ID of the block to activate
  227.  * @return bool true on success, false on failure
  228.  */
  229. function blocks_adminapi_activate($args)
  230. {
  231.     $args['active'1;
  232.     $res = (boolean)blocks_adminapi_setActiveState ($args);
  233.  
  234.     if (!$res{
  235.         return LogUtil::registerError (_BLOCKS_ACTIVATEERROR);
  236.     }
  237.  
  238.     return $res;
  239. }
  240.  
  241. /**
  242.  * delete a block
  243.  * @author Jim McDonald
  244.  * @param int $args ['bid'] the ID of the block to delete
  245.  * @return bool true on success, false on failure
  246.  */
  247. function blocks_adminapi_delete($args)
  248. {
  249.     // Argument check
  250.     if (!isset($args['bid']|| !is_numeric($args['bid'])) {
  251.         return LogUtil::registerError (_MODARGSERROR);
  252.     }
  253.  
  254.     $block DBUtil::selectObjectByID ('blocks'$args['bid']'bid');
  255.  
  256.     // Security check
  257.     if (!SecurityUtil::checkPermission('Blocks::'"$block[bkey]:$block[title]:$block[bid]"ACCESS_DELETE)) {
  258.         return LogUtil::registerPermissionError();
  259.     }
  260.  
  261.     // delete block placements for this block
  262.     $res DBUtil::deleteObjectByID('block_placements'$args['bid']'bid');
  263.     if (!$res{
  264.         return LogUtil::registerError (_DELETEFAILED);
  265.     }
  266.  
  267.     // delete the block itself
  268.     $res DBUtil::deleteObjectByID ('blocks'$args['bid']'bid');
  269.     if (!$res{
  270.         return LogUtil::registerError (_DELETEFAILED);
  271.     }
  272.  
  273.     // Let other modules know we have deleted an item
  274.     pnModCallHooks('item''delete'$args['bid']array('module' => 'Blocks'));
  275.  
  276.     return true;
  277. }
  278.  
  279. /**
  280.  * create a block position
  281.  * @author Mark West
  282.  * @param string $args['name'] name of the position
  283.  * @param string $args['description'] description of the position
  284.  * @return mixed position ID on success, false on failure
  285.  */
  286. {
  287.     // Argument check
  288.     if (!isset($args['name']||
  289.         !strlen($args['name']||
  290.         !isset($args['description'])) {
  291.         return LogUtil::registerError (_MODARGSERROR);
  292.     }
  293.  
  294.     // Security check
  295.     if (!defined('_PNINSTALLVER'&& !SecurityUtil::checkPermission('Blocks::position'"$args[name]::"ACCESS_ADD)) {
  296.         return LogUtil::registerPermissionError();
  297.     }
  298.  
  299.     $positions pnModAPIFunc('Blocks''user''getallpositions');
  300.     if (isset($positions&& is_array($positions)) {
  301.         foreach ($positions as $position{
  302.             if ($position['name'== $args['name']{
  303.                return LogUtil::registerError (_BLOCKS_POSITIONALREADYEXISTS);
  304.             }
  305.         }
  306.     }
  307.  
  308.     $item array('name' => $args['name']'description' => $args['description']);
  309.  
  310.     if (!DBUtil::insertObject($item'block_positions''pid')) {
  311.         return LogUtil::registerError (_CREATEFAILED);
  312.     }
  313.  
  314.     // Return the id of the newly created item to the calling process
  315.     return $item['pid'];
  316. }
  317.  
  318. /**
  319.  * update a block position item
  320.  * @author Mark West
  321.  * @param int $args['pid'] the ID of the item
  322.  * @param sting $args['name'] name of the block position
  323.  * @param string $args['description'] description of the block position
  324.  * @return bool true if successful, false otherwise
  325.  */
  326. {
  327.     // Argument check
  328.     if (!isset($args['pid'])           ||
  329.         !isset($args['name'])          ||
  330.         !isset($args['description'])) {
  331.         return LogUtil::registerError (_MODARGSERROR);
  332.     }
  333.  
  334.     // Get the existing admin message
  335.     $item pnModAPIFunc('Blocks''user''getposition'array('pid' => $args['pid']));
  336.  
  337.     if ($item == false{
  338.         return LogUtil::registerError (_NOSUCHITEM);
  339.     }
  340.  
  341.     // Security check
  342.     if (!SecurityUtil::checkPermission('Blocks::position'"$item[name]::$item[pid]"ACCESS_EDIT)) {
  343.         return LogUtil::registerPermissionError();
  344.     }
  345.  
  346.     // create the item array
  347.     $item array('pid' => $args['pid']'name' => $args['name']'description' => $args['description']);
  348.  
  349.     if (!DBUtil::updateObject($args'block_positions''''pid')) {
  350.         return LogUtil::registerError (_UPDATEFAILED);
  351.     }
  352.  
  353.     // Let the calling process know that we have finished successfully
  354.     return true;
  355. }
  356.  
  357. /**
  358.  * delete a block position
  359.  * @author Mark West
  360.  * @param int $args['pid'] ID of the position
  361.  * @return bool true on success, false on failure
  362.  */
  363. {
  364.     if (!isset($args['pid']|| !is_numeric($args['pid'])) {
  365.         return LogUtil::registerError (_MODARGSERROR);
  366.     }
  367.  
  368.     $item pnModAPIFunc('Blocks''user''getposition'array('pid' => $args['pid']));
  369.  
  370.     if ($item == false{
  371.         return LogUtil::registerError (_NOSUCHITEM);
  372.     }
  373.  
  374.     if (!SecurityUtil::checkPermission('Blocks::position'"$item[name]::$item[pid]"ACCESS_DELETE)) {
  375.         return LogUtil::registerPermissionError();
  376.     }
  377.  
  378.     // Now actually delete the category
  379.     if (!DBUtil::deleteObjectByID ('block_positions'$args['pid']'pid')) {
  380.         return LogUtil::registerError (_DELETEFAILED);
  381.     }
  382.  
  383.     // Let the calling process know that we have finished successfully
  384.     return true;
  385. }
  386.  
  387. /**
  388.  * get available admin panel links
  389.  *
  390.  * @author Mark West
  391.  * @return array array of admin links
  392.  */
  393. {
  394.     $links array();
  395.  
  396.     pnModLangLoad('Blocks''admin');
  397.  
  398.     if (SecurityUtil::checkPermission('Blocks::''::'ACCESS_EDIT)) {
  399.         $links[array('url' => pnModURL('Blocks''admin''view')'text' => _BLOCKS_VIEWBLOCKS);
  400.     }
  401.     if (SecurityUtil::checkPermission('Blocks::''::'ACCESS_ADD)) {
  402.         $links[array('url' => pnModURL('Blocks''admin''new')'text' => _BLOCKS_CREATEBLOCK);
  403.     }
  404.     if (SecurityUtil::checkPermission('Blocks::''::'ACCESS_ADD)) {
  405.         $links[array('url' => pnModURL('Blocks''admin''newposition')'text' => _BLOCKS_CREATEBLOCKPOSITION);
  406.     }
  407.     if (SecurityUtil::checkPermission('Blocks::''::'ACCESS_EDIT)) {
  408.         if (SessionUtil::getVar('blocks_show_all')) {
  409.             $links[array('url' => pnModURL('Blocks''admin''showactive')'text' => _BLOCKS_SHOWACTIVEBLOCKS);
  410.         else {
  411.             $links[array('url' => pnModURL('Blocks''admin''showall')'text' => _BLOCKS_SHOWALLBLOCKS);
  412.         }
  413.     }
  414.     if (SecurityUtil::checkPermission('Blocks::''::'ACCESS_ADMIN)) {
  415.         $links[array('url' => pnModURL('Blocks''admin''modifyconfig')'text' => _MODIFYCONFIG);
  416.     }
  417.  
  418.     return $links;
  419. }

Documentation generated on Fri, 18 Jul 2008 21:51:43 +0200 by phpDocumentor 1.4.1