home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / CMS / xoops-2.0.18.1.exe / xoops-2.0.18.1 / htdocs / kernel / block.php < prev    next >
Encoding:
PHP Script  |  2007-10-19  |  13.8 KB  |  340 lines

  1. <?php
  2. // $Id: block.php 1102 2007-10-19 02:55:52Z dugris $
  3. //  ------------------------------------------------------------------------ //
  4. //                XOOPS - PHP Content Management System                      //
  5. //                    Copyright (c) 2000 XOOPS.org                           //
  6. //                       <http://www.xoops.org/>                             //
  7. //  ------------------------------------------------------------------------ //
  8. //  This program is free software; you can redistribute it and/or modify     //
  9. //  it under the terms of the GNU General Public License as published by     //
  10. //  the Free Software Foundation; either version 2 of the License, or        //
  11. //  (at your option) any later version.                                      //
  12. //                                                                           //
  13. //  You may not change or alter any portion of this comment or credits       //
  14. //  of supporting developers from this source code or any supporting         //
  15. //  source code which is considered copyrighted (c) material of the          //
  16. //  original comment or credit authors.                                      //
  17. //                                                                           //
  18. //  This program is distributed in the hope that it will be useful,          //
  19. //  but WITHOUT ANY WARRANTY; without even the implied warranty of           //
  20. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            //
  21. //  GNU General Public License for more details.                             //
  22. //                                                                           //
  23. //  You should have received a copy of the GNU General Public License        //
  24. //  along with this program; if not, write to the Free Software              //
  25. //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA //
  26. //  ------------------------------------------------------------------------ //
  27. // Author: Kazumi Ono (AKA onokazu)                                          //
  28. // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
  29. // Project: The XOOPS Project                                                //
  30. // ------------------------------------------------------------------------- //
  31.  
  32. if (!defined('XOOPS_ROOT_PATH')) {
  33.     exit();
  34. }
  35.  
  36. /**
  37.  * @author  Kazumi Ono <onokazu@xoops.org>
  38.  * @copyright copyright (c) 2000 XOOPS.org
  39.  **/
  40.  
  41. /**
  42.  * A block
  43.  *
  44.  * @author Kazumi Ono <onokazu@xoops.org>
  45.  * @copyright copyright (c) 2000 XOOPS.org
  46.  *
  47.  * @package kernel
  48.  **/
  49. class XoopsBlock extends XoopsObject
  50. {
  51.  
  52.     /**
  53.      * constructor
  54.      *
  55.      * @param mixed $id
  56.      **/
  57.     function XoopsBlock($id = null)
  58.     {
  59.         $this->initVar('bid', XOBJ_DTYPE_INT, null, false);
  60.         $this->initVar('mid', XOBJ_DTYPE_INT, 0, false);
  61.         $this->initVar('func_num', XOBJ_DTYPE_INT, 0, false);
  62.         $this->initVar('options', XOBJ_DTYPE_TXTBOX, null, false, 255);
  63.         $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 150);
  64.         //$this->initVar('position', XOBJ_DTYPE_INT, 0, false);
  65.         $this->initVar('title', XOBJ_DTYPE_TXTBOX, null, false, 150);
  66.         $this->initVar('content', XOBJ_DTYPE_TXTAREA, null, false);
  67.         $this->initVar('side', XOBJ_DTYPE_INT, 0, false);
  68.         $this->initVar('weight', XOBJ_DTYPE_INT, 0, false);
  69.         $this->initVar('visible', XOBJ_DTYPE_INT, 0, false);
  70.         $this->initVar('block_type', XOBJ_DTYPE_OTHER, null, false);
  71.         $this->initVar('c_type', XOBJ_DTYPE_OTHER, null, false);
  72.         $this->initVar('isactive', XOBJ_DTYPE_INT, null, false);
  73.         $this->initVar('dirname', XOBJ_DTYPE_TXTBOX, null, false, 50);
  74.         $this->initVar('func_file', XOBJ_DTYPE_TXTBOX, null, false, 50);
  75.         $this->initVar('show_func', XOBJ_DTYPE_TXTBOX, null, false, 50);
  76.         $this->initVar('edit_func', XOBJ_DTYPE_TXTBOX, null, false, 50);
  77.         $this->initVar('template', XOBJ_DTYPE_OTHER, null, false);
  78.         $this->initVar('bcachetime', XOBJ_DTYPE_INT, 0, false);
  79.         $this->initVar('last_modified', XOBJ_DTYPE_INT, 0, false);
  80.  
  81.         // for backward compatibility
  82.         if (isset($id)) {
  83.             if (is_array($id)) {
  84.                 $this->assignVars($id);
  85.             } else {
  86.                 $blkhandler =& xoops_gethandler('block');
  87.                 $obj =& $blkhandler->get($id);
  88.                 foreach (array_keys($obj->getVars() ) as $i) {
  89.                     $this->assignVar($obj->getVar($i, 'n') );
  90.                 }
  91.             }
  92.         }
  93.     }
  94.  
  95.     /**
  96.      * return the content of the block for output
  97.      *
  98.      * @param string $format
  99.      * @param string $c_type type of content<br>
  100.      * Legal value for the type of content<br>
  101.      * <ul><li>H : custom HTML block
  102.      * <li>P : custom PHP block
  103.      * <li>S : use text sanitizater (smilies enabled)
  104.      * <li>T : use text sanitizater (smilies disabled)</ul>
  105.      * @return string content for output
  106.      **/
  107.     function getContent($format = 'S', $c_type = 'T')
  108.     {
  109.         switch ( $format ) {
  110.         case 'S':
  111.             if ( $c_type == 'H' ) {
  112.                 return str_replace('{X_SITEURL}', XOOPS_URL.'/', $this->getVar('content', 'N'));
  113.             } elseif ( $c_type == 'P' ) {
  114.                 ob_start();
  115.                 echo eval($this->getVar('content', 'N'));
  116.                 $content = ob_get_contents();
  117.                 ob_end_clean();
  118.                 return str_replace('{X_SITEURL}', XOOPS_URL.'/', $content);
  119.             } elseif ( $c_type == 'S' ) {
  120.                 $myts =& MyTextSanitizer::getInstance();
  121.                 $content = str_replace('{X_SITEURL}', XOOPS_URL.'/', $this->getVar('content', 'N'));
  122.                 return $myts->displayTarea($content, 0, 1);
  123.             } else {
  124.                 $myts =& MyTextSanitizer::getInstance();
  125.                 $content = str_replace('{X_SITEURL}', XOOPS_URL.'/', $this->getVar('content', 'N'));
  126.                 return $myts->displayTarea($content, 0, 0);
  127.             }
  128.             break;
  129.         case 'E':
  130.             return $this->getVar('content', 'E');
  131.             break;
  132.         default:
  133.             return $this->getVar('content', 'N');
  134.             break;
  135.         }
  136.     }
  137.  
  138.     /**
  139.      * (HTML-) form for setting the options of the block
  140.      *
  141.      * @return string HTML for the form, FALSE if not defined for this block
  142.      **/
  143.     function getOptions()
  144.     {
  145.         if ($this->getVar('block_type') != 'C') {
  146.             $edit_func = $this->getVar('edit_func');
  147.             if (!$edit_func) {
  148.                 return false;
  149.             }
  150.             if (file_exists(XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/blocks/'.$this->getVar('func_file'))) {
  151.                 if (file_exists(XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/language/'.$GLOBALS['xoopsConfig']['language'].'/blocks.php')) {
  152.                     include_once XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/language/'.$GLOBALS['xoopsConfig']['language'].'/blocks.php';
  153.                 } elseif (file_exists(XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/language/english/blocks.php')) {
  154.                     include_once XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/language/english/blocks.php';
  155.                 }
  156.                 include_once XOOPS_ROOT_PATH.'/modules/'.$this->getVar('dirname').'/blocks/'.$this->getVar('func_file');
  157.                 $options = explode('|', $this->getVar('options'));
  158.                 $edit_form = $edit_func($options);
  159.                 if (!$edit_form) {
  160.                     return false;
  161.                 }
  162.                 return $edit_form;
  163.             } else {
  164.                 return false;
  165.             }
  166.         } else {
  167.             return false;
  168.         }
  169.     }
  170. }
  171.  
  172.  
  173. /**
  174.  * XOOPS block handler class. (Singelton)
  175.  *
  176.  * This class is responsible for providing data access mechanisms to the data source
  177.  * of XOOPS block class objects.
  178.  *
  179.  * @author  Kazumi Ono <onokazu@xoops.org>
  180.  * @copyright copyright (c) 2000 XOOPS.org
  181.  * @package kernel
  182.  * @subpackage block
  183. */
  184. class XoopsBlockHandler extends XoopsObjectHandler
  185. {
  186.  
  187.     /**
  188.      * create a new block
  189.      *
  190.      * @see XoopsBlock
  191.      * @param bool $isNew is the new block new??
  192.      * @return object XoopsBlock reference to the new block
  193.      **/
  194.     function &create($isNew = true)
  195.     {
  196.         $block = new XoopsBlock();
  197.         if ($isNew) {
  198.             $block->setNew();
  199.         }
  200.         return $block;
  201.     }
  202.  
  203.     /**
  204.      * retrieve a specific {@link XoopsBlock}
  205.      *
  206.      * @see XoopsBlock
  207.      * @param int $id bid of the block to retrieve
  208.      * @return object XoopsBlock reference to the block
  209.      **/
  210.     function &get($id)
  211.     {
  212.         $block = false;
  213.         $id = intval($id);
  214.         if ($id > 0) {
  215.             $sql = 'SELECT * FROM '.$this->db->prefix('newblocks').' WHERE bid='.$id;
  216.             if ( $result = $this->db->query($sql) ) {
  217.                 $numrows = $this->db->getRowsNum($result);
  218.                 if ($numrows == 1) {
  219.                     $block = new XoopsBlock();
  220.                     $block->assignVars($this->db->fetchArray($result));
  221.                 }
  222.             }
  223.         }
  224.         return $block;
  225.     }
  226.  
  227.     /**
  228.      * write a new block into the database
  229.      *
  230.      * @param object XoopsBlock $block reference to the block to insert
  231.      * @return bool TRUE if succesful
  232.      **/
  233.     function insert(&$block)
  234.     {
  235.         /**
  236.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  237.         */
  238.         if (!is_a($block, 'xoopsblock')) {
  239.             return false;
  240.         }
  241.         if (!$block->isDirty()) {
  242.             return true;
  243.         }
  244.         if (!$block->cleanVars()) {
  245.             return false;
  246.         }
  247.         foreach ($block->cleanVars as $k => $v) {
  248.             ${$k} = $v;
  249.         }
  250.         if ($block->isNew()) {
  251.             $bid = $this->db->genId('newblocks_bid_seq');
  252.             $sql = sprintf("INSERT INTO %s (bid, mid, func_num, options, name, title, content, side, weight, visible, block_type, c_type, isactive, dirname, func_file, show_func, edit_func, template, bcachetime, last_modified) VALUES (%u, %u, %u, '%s', '%s', '%s', '%s', %u, %u, %u, '%s', '%s', %u, '%s', '%s', '%s', '%s', '%s', %u, %u)", $this->db->prefix('newblocks'), $bid, $mid, $func_num, $options, $name, $title, $content, $side, $weight, $visible, $block_type, $c_type, 1, $dirname, $func_file, $show_func, $edit_func, $template, $bcachetime, time());
  253.         } else {
  254.             $sql = sprintf("UPDATE %s SET func_num = %u, options = '%s', name = '%s', title = '%s', content = '%s', side = %u, weight = %u, visible = %u, c_type = '%s', isactive = %u, func_file = '%s', show_func = '%s', edit_func = '%s', template = '%s', bcachetime = %u, last_modified = %u WHERE bid = %u", $this->db->prefix('newblocks'), $func_num, $options, $name, $title, $content, $side, $weight, $visible, $c_type, $isactive, $func_file, $show_func, $edit_func, $template, $bcachetime, time(), $bid);
  255.         }
  256.         if (!$result = $this->db->query($sql)) {
  257.             return false;
  258.         }
  259.         if (empty($bid)) {
  260.             $bid = $this->db->getInsertId();
  261.         }
  262.         $block->assignVar('bid', $bid);
  263.         return true;
  264.     }
  265.  
  266.     /**
  267.      * delete a block from the database
  268.      *
  269.      * @param object XoopsBlock $block reference to the block to delete
  270.      * @return bool TRUE if succesful
  271.      **/
  272.     function delete(&$block)
  273.     {
  274.         /**
  275.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  276.         */
  277.         if (!is_a($block, 'xoopsblock')) {
  278.             return false;
  279.         }
  280.         $id = $block->getVar('bid');
  281.         $sql = sprintf("DELETE FROM %s WHERE bid = %u", $this->db->prefix('newblocks'), $id);
  282.         if (!$result = $this->db->query($sql)) {
  283.             return false;
  284.         }
  285.         $sql = sprintf("DELETE FROM %s WHERE block_id = %u", $this->db->prefix('block_module_link'), $id);
  286.         $this->db->query($sql);
  287.         return true;
  288.     }
  289.  
  290.     /**
  291.      * retrieve array of {@link XoopsBlock}s meeting certain conditions
  292.      * @param object $criteria {@link CriteriaElement} with conditions for the blocks
  293.      * @param bool $id_as_key should the blocks' bid be the key for the returned array?
  294.      * @return array {@link XoopsBlock}s matching the conditions
  295.      **/
  296.     function getObjects($criteria = null, $id_as_key = false)
  297.     {
  298.         $ret = array();
  299.         $limit = $start = 0;
  300.         $sql = 'SELECT DISTINCT(b.*) FROM '.$this->db->prefix('newblocks').' b LEFT JOIN '.$this->db->prefix('block_module_link').' l ON b.bid=l.block_id';
  301.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  302.             $sql .= ' '.$criteria->renderWhere();
  303.             $limit = $criteria->getLimit();
  304.             $start = $criteria->getStart();
  305.         }
  306.         $result = $this->db->query($sql, $limit, $start);
  307.         if (!$result) {
  308.             return $ret;
  309.         }
  310.         while ($myrow = $this->db->fetchArray($result)) {
  311.             $block = new XoopsBlock();
  312.             $block->assignVars($myrow);
  313.             if (!$id_as_key) {
  314.                 $ret[] =& $block;
  315.             } else {
  316.                 $ret[$myrow['bid']] =& $block;
  317.             }
  318.             unset($block);
  319.         }
  320.         return $ret;
  321.     }
  322.  
  323.     /**
  324.      * get a list of blocks matchich certain conditions
  325.      *
  326.      * @param string $criteria conditions to match
  327.      * @return array array of blocks matching the conditions
  328.      **/
  329.     function getList($criteria = null)
  330.     {
  331.         $blocks =& $this->getObjects($criteria, true);
  332.         $ret = array();
  333.         foreach (array_keys($blocks) as $i) {
  334.             $name = ($blocks[$i]->getVar('block_type') != 'C') ? $blocks[$i]->getVar('name') : $blocks[$i]->getVar('title');
  335.             $ret[$i] = $name;
  336.         }
  337.         return $ret;
  338.     }
  339. }
  340. ?>