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 / configitem.php < prev    next >
Encoding:
PHP Script  |  2007-12-04  |  11.9 KB  |  340 lines

  1. <?php
  2. // $Id: configitem.php 1145 2007-12-04 09:43:26Z phppp $
  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.  * @package     kernel
  38.  *
  39.  * @author        Kazumi Ono    <onokazu@xoops.org>
  40.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  41.  */
  42.  
  43. /**#@+
  44.  * Config type
  45.  */
  46. define('XOOPS_CONF', 1);
  47. define('XOOPS_CONF_USER', 2);
  48. define('XOOPS_CONF_METAFOOTER', 3);
  49. define('XOOPS_CONF_CENSOR', 4);
  50. define('XOOPS_CONF_SEARCH', 5);
  51. define('XOOPS_CONF_MAILER', 6);
  52. define('XOOPS_CONF_AUTH', 7);
  53. /**#@-*/
  54.  
  55. /**
  56.  *
  57.  *
  58.  * @author        Kazumi Ono    <onokazu@xoops.org>
  59.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  60.  */
  61. class XoopsConfigItem extends XoopsObject
  62. {
  63.  
  64.     /**
  65.      * Config options
  66.      *
  67.      * @var    array
  68.      * @access    private
  69.      */
  70.     var $_confOptions = array();
  71.  
  72.     /**
  73.      * Constructor
  74.      */
  75.     function XoopsConfigItem()
  76.     {
  77.         $this->initVar('conf_id', XOBJ_DTYPE_INT, null, false);
  78.         $this->initVar('conf_modid', XOBJ_DTYPE_INT, null, false);
  79.         $this->initVar('conf_catid', XOBJ_DTYPE_INT, null, false);
  80.         $this->initVar('conf_name', XOBJ_DTYPE_OTHER);
  81.         $this->initVar('conf_title', XOBJ_DTYPE_TXTBOX);
  82.         $this->initVar('conf_value', XOBJ_DTYPE_TXTAREA);
  83.         $this->initVar('conf_desc', XOBJ_DTYPE_OTHER);
  84.         $this->initVar('conf_formtype', XOBJ_DTYPE_OTHER);
  85.         $this->initVar('conf_valuetype', XOBJ_DTYPE_OTHER);
  86.         $this->initVar('conf_order', XOBJ_DTYPE_INT);
  87.     }
  88.  
  89.     /**
  90.      * Get a config value in a format ready for output
  91.      *
  92.      * @return    string
  93.      */
  94.     function getConfValueForOutput()
  95.     {
  96.         switch ($this->getVar('conf_valuetype')) {
  97.         case 'int':
  98.             return intval($this->getVar('conf_value', 'N'));
  99.             break;
  100.         case 'array':
  101.             return unserialize($this->getVar('conf_value', 'N'));
  102.         case 'float':
  103.             $value = $this->getVar('conf_value', 'N');
  104.             return (float)$value;
  105.             break;
  106.         case 'textarea':
  107.             return $this->getVar('conf_value');
  108.         default:
  109.             return $this->getVar('conf_value', 'N');
  110.             break;
  111.         }
  112.     }
  113.  
  114.     /**
  115.      * Set a config value
  116.      *
  117.      * @param    mixed   &$value Value
  118.      * @param    bool    $force_slash
  119.      */
  120.     function setConfValueForInput(&$value, $force_slash = false)
  121.     {
  122.         switch($this->getVar('conf_valuetype')) {
  123.         case 'array':
  124.             if (!is_array($value)) {
  125.                 $value = explode('|', trim($value));
  126.             }
  127.             $this->setVar('conf_value', serialize($value), $force_slash);
  128.             break;
  129.         case 'text':
  130.             $this->setVar('conf_value', trim($value), $force_slash);
  131.             break;
  132.         default:
  133.             $this->setVar('conf_value', $value, $force_slash);
  134.             break;
  135.         }
  136.     }
  137.  
  138.     /**
  139.      * Assign one or more {@link XoopsConfigItemOption}s
  140.      *
  141.      * @param    mixed   $option either a {@link XoopsConfigItemOption} object or an array of them
  142.      */
  143.     function setConfOptions($option)
  144.     {
  145.         if (is_array($option)) {
  146.             $count = count($option);
  147.             for ($i = 0; $i < $count; $i++) {
  148.                 $this->setConfOptions($option[$i]);
  149.             }
  150.         } else {
  151.             if(is_object($option)) {
  152.                 $this->_confOptions[] =& $option;
  153.             }
  154.         }
  155.     }
  156.  
  157.     /**
  158.      * Get the {@link XoopsConfigItemOption}s of this Config
  159.      *
  160.      * @return    array   array of {@link XoopsConfigItemOption}
  161.      */
  162.     function &getConfOptions()
  163.     {
  164.         return $this->_confOptions;
  165.     }
  166. }
  167.  
  168.  
  169. /**
  170. * XOOPS configuration handler class.
  171. *
  172. * This class is responsible for providing data access mechanisms to the data source
  173. * of XOOPS configuration class objects.
  174. *
  175. * @author       Kazumi Ono <onokazu@xoops.org>
  176. * @copyright    copyright (c) 2000-2003 XOOPS.org
  177. */
  178. class XoopsConfigItemHandler extends XoopsObjectHandler
  179. {
  180.  
  181.     /**
  182.      * Create a new {@link XoopsConfigItem}
  183.      *
  184.      * @see     XoopsConfigItem
  185.      * @param    bool    $isNew  Flag the config as "new"?
  186.      * @return    object  reference to the new config
  187.      */
  188.     function &create($isNew = true)
  189.     {
  190.         $config = new XoopsConfigItem();
  191.         if ($isNew) {
  192.             $config->setNew();
  193.         }
  194.         return $config;
  195.     }
  196.  
  197.     /**
  198.      * Load a config from the database
  199.      *
  200.      * @param    int $id ID of the config
  201.      * @return    object  reference to the config, FALSE on fail
  202.      */
  203.     function &get($id)
  204.     {
  205.         $config = false;
  206.         $id = intval($id);
  207.         if ($id > 0) {
  208.             $sql = 'SELECT * FROM '.$this->db->prefix('config').' WHERE conf_id='.$id;
  209.             if (!$result = $this->db->query($sql)) {
  210.                 return $config;
  211.             }
  212.             $numrows = $this->db->getRowsNum($result);
  213.             if ($numrows == 1) {
  214.                 $myrow = $this->db->fetchArray($result);
  215.                 $config = new XoopsConfigItem();
  216.                 $config->assignVars($myrow);
  217.             }
  218.         }
  219.         return $config;
  220.     }
  221.  
  222.     /**
  223.      * Write a config to the database
  224.      *
  225.      * @param    object  &$config    {@link XoopsConfigItem} object
  226.      * @return  mixed   FALSE on fail.
  227.      */
  228.     function insert(&$config)
  229.     {
  230.         /**
  231.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  232.         */
  233.         if (!is_a($config, 'xoopsconfigitem')) {
  234.             return false;
  235.         }
  236.         if (!$config->isDirty()) {
  237.             return true;
  238.         }
  239.         if (!$config->cleanVars()) {
  240.             return false;
  241.         }
  242.         foreach ($config->cleanVars as $k => $v) {
  243.             ${$k} = $v;
  244.         }
  245.         if ($config->isNew()) {
  246.             $conf_id = $this->db->genId('config_conf_id_seq');
  247.             $sql = sprintf("INSERT INTO %s (conf_id, conf_modid, conf_catid, conf_name, conf_title, conf_value, conf_desc, conf_formtype, conf_valuetype, conf_order) VALUES (%u, %u, %u, %s, %s, %s, %s, %s, %s, %u)", $this->db->prefix('config'), $conf_id, $conf_modid, $conf_catid, $this->db->quoteString($conf_name), $this->db->quoteString($conf_title), $this->db->quoteString($conf_value), $this->db->quoteString($conf_desc), $this->db->quoteString($conf_formtype), $this->db->quoteString($conf_valuetype), $conf_order);
  248.         } else {
  249.             $sql = sprintf("UPDATE %s SET conf_modid = %u, conf_catid = %u, conf_name = %s, conf_title = %s, conf_value = %s, conf_desc = %s, conf_formtype = %s, conf_valuetype = %s, conf_order = %u WHERE conf_id = %u", $this->db->prefix('config'), $conf_modid, $conf_catid, $this->db->quoteString($conf_name), $this->db->quoteString($conf_title), $this->db->quoteString($conf_value), $this->db->quoteString($conf_desc), $this->db->quoteString($conf_formtype), $this->db->quoteString($conf_valuetype), $conf_order, $conf_id);
  250.         }
  251.         if (!$result = $this->db->query($sql)) {
  252.             return false;
  253.         }
  254.         if (empty($conf_id)) {
  255.             $conf_id = $this->db->getInsertId();
  256.         }
  257.         $config->assignVar('conf_id', $conf_id);
  258.         return true;
  259.     }
  260.  
  261.     /**
  262.      * Delete a config from the database
  263.      *
  264.      * @param    object  &$config    Config to delete
  265.      * @return    bool    Successful?
  266.      */
  267.     function delete(&$config)
  268.     {
  269.         /**
  270.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  271.         */
  272.         if (!is_a($config, 'xoopsconfigitem')) {
  273.             return false;
  274.         }
  275.         $sql = sprintf("DELETE FROM %s WHERE conf_id = %u", $this->db->prefix('config'), $config->getVar('conf_id'));
  276.         if (!$result = $this->db->query($sql)) {
  277.             return false;
  278.         }
  279.         return true;
  280.     }
  281.  
  282.     /**
  283.      * Get configs from the database
  284.      *
  285.      * @param    object  $criteria   {@link CriteriaElement}
  286.      * @param    bool    $id_as_key  return the config's id as key?
  287.      * @return    array   Array of {@link XoopsConfigItem} objects
  288.      */
  289.     function getObjects($criteria = null, $id_as_key = false)
  290.     {
  291.         $ret = array();
  292.         $limit = $start = 0;
  293.         $sql = 'SELECT * FROM '.$this->db->prefix('config');
  294.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  295.             $sql .= ' '.$criteria->renderWhere();
  296.             $sql .= ' ORDER BY conf_order ASC';
  297.             $limit = $criteria->getLimit();
  298.             $start = $criteria->getStart();
  299.         }
  300.         $result = $this->db->query($sql, $limit, $start);
  301.         if (!$result) {
  302.             return false;
  303.         }
  304.         while ($myrow = $this->db->fetchArray($result)) {
  305.             $config = new XoopsConfigItem();
  306.             $config->assignVars($myrow);
  307.             if (!$id_as_key) {
  308.                 $ret[] =& $config;
  309.             } else {
  310.                 $ret[$myrow['conf_id']] =& $config;
  311.             }
  312.             unset($config);
  313.         }
  314.         return $ret;
  315.     }
  316.  
  317.     /**
  318.      * Count configs
  319.      *
  320.      * @param    object  $criteria   {@link CriteriaElement}
  321.      * @return    int     Count of configs matching $criteria
  322.      */
  323.     function getCount($criteria = null)
  324.     {
  325.         $ret = array();
  326.         $limit = $start = 0;
  327.         $sql = 'SELECT * FROM '.$this->db->prefix('config');
  328.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  329.             $sql .= ' '.$criteria->renderWhere();
  330.         }
  331.         $result =& $this->db->query($sql);
  332.         if (!$result) {
  333.             return false;
  334.         }
  335.         list($count) = $this->db->fetchRow($result);
  336.         return $count;
  337.     }
  338. }
  339.  
  340. ?>