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 / configoption.php < prev    next >
Encoding:
PHP Script  |  2007-10-19  |  8.1 KB  |  218 lines

  1. <?php
  2. // $Id: configoption.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.  *
  38.  *
  39.  * @package     kernel
  40.  *
  41.  * @author        Kazumi Ono    <onokazu@xoops.org>
  42.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  43.  */
  44.  
  45. /**
  46.  * A Config-Option
  47.  *
  48.  * @author    Kazumi Ono    <onokazu@xoops.org>
  49.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  50.  *
  51.  * @package     kernel
  52.  */
  53. class XoopsConfigOption extends XoopsObject
  54. {
  55.     /**
  56.      * Constructor
  57.      */
  58.     function XoopsConfigOption()
  59.     {
  60.         $this->XoopsObject();
  61.         $this->initVar('confop_id', XOBJ_DTYPE_INT, null);
  62.         $this->initVar('confop_name', XOBJ_DTYPE_TXTBOX, null, true, 255);
  63.         $this->initVar('confop_value', XOBJ_DTYPE_TXTBOX, null, true, 255);
  64.         $this->initVar('conf_id', XOBJ_DTYPE_INT, 0);
  65.     }
  66. }
  67.  
  68. /**
  69.  * XOOPS configuration option handler class.
  70.  * This class is responsible for providing data access mechanisms to the data source
  71.  * of XOOPS configuration option class objects.
  72.  *
  73.  * @copyright    copyright (c) 2000-2003 XOOPS.org
  74.  * @author  Kazumi Ono <onokazu@xoops.org>
  75.  *
  76.  * @package     kernel
  77.  * @subpackage  config
  78. */
  79. class XoopsConfigOptionHandler extends XoopsObjectHandler
  80. {
  81.  
  82.     /**
  83.      * Create a new option
  84.      *
  85.      * @param    bool    $isNew  Flag the option as "new"?
  86.      *
  87.      * @return    object  {@link XoopsConfigOption}
  88.      */
  89.     function &create($isNew = true)
  90.     {
  91.         $confoption = new XoopsConfigOption();
  92.         if ($isNew) {
  93.             $confoption->setNew();
  94.         }
  95.         return $confoption;
  96.     }
  97.  
  98.     /**
  99.      * Get an option from the database
  100.      *
  101.      * @param    int $id ID of the option
  102.      *
  103.      * @return    object  reference to the {@link XoopsConfigOption}, FALSE on fail
  104.      */
  105.     function &get($id)
  106.     {
  107.         $confoption = false;
  108.         $id = intval($id);
  109.         if ($id > 0) {
  110.             $sql = 'SELECT * FROM '.$this->db->prefix('configoption').' WHERE confop_id='.$id;
  111.             if (!$result = $this->db->query($sql)) {
  112.                 return $confoption;
  113.             }
  114.             $numrows = $this->db->getRowsNum($result);
  115.             if ($numrows == 1) {
  116.                 $confoption = new XoopsConfigOption();
  117.                 $confoption->assignVars($this->db->fetchArray($result));
  118.             }
  119.         }
  120.         return $confoption;
  121.     }
  122.  
  123.     /**
  124.      * Insert a new option in the database
  125.      *
  126.      * @param    object  &$confoption    reference to a {@link XoopsConfigOption}
  127.      * @return    bool    TRUE if successfull.
  128.      */
  129.     function insert(&$confoption)
  130.     {
  131.         /**
  132.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  133.         */
  134.         if (!is_a($confoption, 'xoopsconfigoption')) {
  135.             return false;
  136.         }
  137.         if (!$confoption->isDirty()) {
  138.             return true;
  139.         }
  140.         if (!$confoption->cleanVars()) {
  141.             return false;
  142.         }
  143.         foreach ($confoption->cleanVars as $k => $v) {
  144.             ${$k} = $v;
  145.         }
  146.         if ($confoption->isNew()) {
  147.             $confop_id = $this->db->genId('configoption_confop_id_seq');
  148.             $sql = sprintf("INSERT INTO %s (confop_id, confop_name, confop_value, conf_id) VALUES (%u, %s, %s, %u)", $this->db->prefix('configoption'), $confop_id, $this->db->quoteString($confop_name), $this->db->quoteString($confop_value), $conf_id);
  149.         } else {
  150.             $sql = sprintf("UPDATE %s SET confop_name = %s, confop_value = %s WHERE confop_id = %u", $this->db->prefix('configoption'), $this->db->quoteString($confop_name), $this->db->quoteString($confop_value), $confop_id);
  151.         }
  152.         if (!$result = $this->db->query($sql)) {
  153.             return false;
  154.         }
  155.         if (empty($confop_id)) {
  156.             $confop_id = $this->db->getInsertId();
  157.         }
  158.         $confoption->assignVar('confop_id', $confop_id);
  159.         return $confop_id;
  160.     }
  161.  
  162.     /**
  163.      * Delete an option
  164.      *
  165.      * @param    object  &$confoption    reference to a {@link XoopsConfigOption}
  166.      * @return    bool    TRUE if successful
  167.      */
  168.     function delete(&$confoption)
  169.     {
  170.         /**
  171.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  172.         */
  173.         if (!is_a($confoption, 'xoopsconfigoption')) {
  174.             return false;
  175.         }
  176.         $sql = sprintf("DELETE FROM %s WHERE confop_id = %u", $this->db->prefix('configoption'), $confoption->getVar('confop_id'));
  177.         if (!$result = $this->db->query($sql)) {
  178.             return false;
  179.         }
  180.         return true;
  181.     }
  182.  
  183.     /**
  184.      * Get some {@link XoopsConfigOption}s
  185.      *
  186.      * @param    object  $criteria   {@link CriteriaElement}
  187.      * @param    bool    $id_as_key  Use the IDs as array-keys?
  188.      *
  189.      * @return    array   Array of {@link XoopsConfigOption}s
  190.      */
  191.     function getObjects($criteria = null, $id_as_key = false)
  192.     {
  193.         $ret = array();
  194.         $limit = $start = 0;
  195.         $sql = 'SELECT * FROM '.$this->db->prefix('configoption');
  196.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  197.             $sql .= ' '.$criteria->renderWhere().' ORDER BY confop_id '.$criteria->getOrder();
  198.             $limit = $criteria->getLimit();
  199.             $start = $criteria->getStart();
  200.         }
  201.         $result = $this->db->query($sql, $limit, $start);
  202.         if (!$result) {
  203.             return $ret;
  204.         }
  205.         while ($myrow = $this->db->fetchArray($result)) {
  206.             $confoption = new XoopsConfigOption();
  207.             $confoption->assignVars($myrow);
  208.             if (!$id_as_key) {
  209.                 $ret[] =& $confoption;
  210.             } else {
  211.                 $ret[$myrow['confop_id']] =& $confoption;
  212.             }
  213.             unset($confoption);
  214.         }
  215.         return $ret;
  216.     }
  217. }
  218. ?>