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 / image.php < prev    next >
Encoding:
PHP Script  |  2007-10-19  |  11.3 KB  |  281 lines

  1. <?php
  2. // $Id: image.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.  * An Image
  38.  *
  39.  * @package        kernel
  40.  * @author        Kazumi Ono     <onokazu@xoops.org>
  41.  * @copyright    (c) 2000-2003 The Xoops Project - www.xoops.org
  42.  */
  43. class XoopsImage extends XoopsObject
  44. {
  45.     /**
  46.      * Constructor
  47.      **/
  48.     function XoopsImage()
  49.     {
  50.         $this->XoopsObject();
  51.         $this->initVar('image_id', XOBJ_DTYPE_INT, null, false);
  52.         $this->initVar('image_name', XOBJ_DTYPE_OTHER, null, false, 30);
  53.         $this->initVar('image_nicename', XOBJ_DTYPE_TXTBOX, null, true, 100);
  54.         $this->initVar('image_mimetype', XOBJ_DTYPE_OTHER, null, false);
  55.         $this->initVar('image_created', XOBJ_DTYPE_INT, null, false);
  56.         $this->initVar('image_display', XOBJ_DTYPE_INT, 1, false);
  57.         $this->initVar('image_weight', XOBJ_DTYPE_INT, 0, false);
  58.         $this->initVar('image_body', XOBJ_DTYPE_SOURCE, null, true);
  59.         $this->initVar('imgcat_id', XOBJ_DTYPE_INT, 0, false);
  60.     }
  61. }
  62.  
  63. /**
  64.  * XOOPS image handler class.
  65.  *
  66.  * This class is responsible for providing data access mechanisms to the data source
  67.  * of XOOPS image class objects.
  68.  *
  69.  * @package        kernel
  70.  *
  71.  * @author        Kazumi Ono     <onokazu@xoops.org>
  72.  * @copyright    (c) 2000-2003 The Xoops Project - www.xoops.org
  73.  */
  74. class XoopsImageHandler extends XoopsObjectHandler
  75. {
  76.  
  77.     /**
  78.      * Create a new {@link XoopsImage}
  79.      *
  80.      * @param   boolean $isNew  Flag the object as "new"
  81.      * @return  object
  82.      **/
  83.     function &create($isNew = true)
  84.     {
  85.         $image = new XoopsImage();
  86.         if ($isNew) {
  87.             $image->setNew();
  88.         }
  89.         return $image;
  90.     }
  91.  
  92.     /**
  93.      * Load a {@link XoopsImage} object from the database
  94.      *
  95.      * @param   int     $id     ID
  96.      * @param   boolean $getbinary
  97.      * @return  object  {@link XoopsImage}, FALSE on fail
  98.      **/
  99.     function &get($id, $getbinary=true)
  100.     {
  101.         $image = false;
  102.         $id = intval($id);
  103.         if ($id > 0) {
  104.             $sql = 'SELECT i.*, b.image_body FROM '.$this->db->prefix('image').' i LEFT JOIN '.$this->db->prefix('imagebody').' b ON b.image_id=i.image_id WHERE i.image_id='.$id;
  105.             if (!$result = $this->db->query($sql)) {
  106.                 return $image;
  107.             }
  108.             $numrows = $this->db->getRowsNum($result);
  109.             if ($numrows == 1) {
  110.                 $image = new XoopsImage();
  111.                 $image->assignVars($this->db->fetchArray($result));
  112.             }
  113.         }
  114.         return $image;
  115.     }
  116.  
  117.     /**
  118.      * Write a {@link XoopsImage} object to the database
  119.      *
  120.      * @param   object  &$image {@link XoopsImage}
  121.      * @return  bool
  122.      **/
  123.     function insert(&$image)
  124.     {
  125.         /**
  126.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  127.         */
  128.         if (!is_a($image, 'xoopsimage')) {
  129.             return false;
  130.         }
  131.  
  132.         if (!$image->isDirty()) {
  133.             return true;
  134.         }
  135.         if (!$image->cleanVars()) {
  136.             return false;
  137.         }
  138.         foreach ($image->cleanVars as $k => $v) {
  139.             ${$k} = $v;
  140.         }
  141.         if ($image->isNew()) {
  142.             $image_id = $this->db->genId('image_image_id_seq');
  143.             $sql = sprintf("INSERT INTO %s (image_id, image_name, image_nicename, image_mimetype, image_created, image_display, image_weight, imgcat_id) VALUES (%u, %s, %s, %s, %u, %u, %u, %u)", $this->db->prefix('image'), $image_id, $this->db->quoteString($image_name), $this->db->quoteString($image_nicename), $this->db->quoteString($image_mimetype), time(), $image_display, $image_weight, $imgcat_id);
  144.             if (!$result = $this->db->query($sql)) {
  145.                 return false;
  146.             }
  147.             if (empty($image_id)) {
  148.                 $image_id = $this->db->getInsertId();
  149.             }
  150.             if (isset($image_body) && $image_body != '') {
  151.                 $sql = sprintf("INSERT INTO %s (image_id, image_body) VALUES (%u, %s)", $this->db->prefix('imagebody'), $image_id, $this->db->quoteString($image_body));
  152.                 if (!$result = $this->db->query($sql)) {
  153.                     $sql = sprintf("DELETE FROM %s WHERE image_id = %u", $this->db->prefix('image'), $image_id);
  154.                     $this->db->query($sql);
  155.                     return false;
  156.                 }
  157.             }
  158.             $image->assignVar('image_id', $image_id);
  159.         } else {
  160.             $sql = sprintf("UPDATE %s SET image_name = %s, image_nicename = %s, image_display = %u, image_weight = %u, imgcat_id = %u WHERE image_id = %u", $this->db->prefix('image'), $this->db->quoteString($image_name), $this->db->quoteString($image_nicename), $image_display, $image_weight, $imgcat_id, $image_id);
  161.             if (!$result = $this->db->query($sql)) {
  162.                 return false;
  163.             }
  164.             if (isset($image_body) && $image_body != '') {
  165.                 $sql = sprintf("UPDATE %s SET image_body = %s WHERE image_id = %u", $this->db->prefix('imagebody'), $this->db->quoteString($image_body), $image_id);
  166.                 if (!$result = $this->db->query($sql)) {
  167.                     $this->db->query(sprintf("DELETE FROM %s WHERE image_id = %u", $this->db->prefix('image'), $image_id));
  168.                     return false;
  169.                 }
  170.             }
  171.         }
  172.         return true;
  173.     }
  174.  
  175.     /**
  176.      * Delete an image from the database
  177.      *
  178.      * @param   object  &$image {@link XoopsImage}
  179.      * @return  bool
  180.      **/
  181.     function delete(&$image)
  182.     {
  183.         /**
  184.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  185.         */
  186.         if (!is_a($image, 'xoopsimage')) {
  187.             return false;
  188.         }
  189.  
  190.         $id = $image->getVar('image_id');
  191.         $sql = sprintf("DELETE FROM %s WHERE image_id = %u", $this->db->prefix('image'), $id);
  192.         if (!$result = $this->db->query($sql)) {
  193.             return false;
  194.         }
  195.         $sql = sprintf("DELETE FROM %s WHERE image_id = %u", $this->db->prefix('imagebody'), $id);
  196.         $this->db->query($sql);
  197.         return true;
  198.     }
  199.  
  200.     /**
  201.      * Load {@link XoopsImage}s from the database
  202.      *
  203.      * @param   object  $criteria   {@link CriteriaElement}
  204.      * @param   boolean $id_as_key  Use the ID as key into the array
  205.      * @param   boolean $getbinary
  206.      * @return  array   Array of {@link XoopsImage} objects
  207.      **/
  208.     function getObjects($criteria = null, $id_as_key = false, $getbinary = false)
  209.     {
  210.         $ret = array();
  211.         $limit = $start = 0;
  212.         if ($getbinary) {
  213.             $sql = 'SELECT i.*, b.image_body FROM '.$this->db->prefix('image').' i LEFT JOIN '.$this->db->prefix('imagebody').' b ON b.image_id=i.image_id';
  214.         } else {
  215.             $sql = 'SELECT * FROM '.$this->db->prefix('image');
  216.         }
  217.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  218.             $sql .= ' '.$criteria->renderWhere();
  219.             $sort = !in_array($criteria->getSort(), array('image_id', 'image_created', 'image_mimetype', 'image_display', 'image_weight')) ? 'image_weight' : $criteria->getSort();
  220.             $sql .= ' ORDER BY '.$sort.' '.$criteria->getOrder();
  221.             $limit = $criteria->getLimit();
  222.             $start = $criteria->getStart();
  223.         }
  224.         $result = $this->db->query($sql, $limit, $start);
  225.         if (!$result) {
  226.             return $ret;
  227.         }
  228.         while ($myrow = $this->db->fetchArray($result)) {
  229.             $image = new XoopsImage();
  230.             $image->assignVars($myrow);
  231.             if (!$id_as_key) {
  232.                 $ret[] =& $image;
  233.             } else {
  234.                 $ret[$myrow['image_id']] =& $image;
  235.             }
  236.             unset($image);
  237.         }
  238.         return $ret;
  239.     }
  240.  
  241.     /**
  242.      * Count some images
  243.      *
  244.      * @param   object  $criteria   {@link CriteriaElement}
  245.      * @return  int
  246.      **/
  247.     function getCount($criteria = null)
  248.     {
  249.         $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('image');
  250.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  251.             $sql .= ' '.$criteria->renderWhere();
  252.         }
  253.         if (!$result =& $this->db->query($sql)) {
  254.             return 0;
  255.         }
  256.         list($count) = $this->db->fetchRow($result);
  257.         return $count;
  258.     }
  259.  
  260.     /**
  261.      * Get a list of images
  262.      *
  263.      * @param   int     $imgcat_id
  264.      * @param   bool    $image_display
  265.      * @return  array   Array of {@link XoopsImage} objects
  266.      **/
  267.     function getList($imgcat_id, $image_display = null)
  268.     {
  269.         $criteria = new CriteriaCompo(new Criteria('imgcat_id', intval($imgcat_id)));
  270.         if (isset($image_display)) {
  271.             $criteria->add(new Criteria('image_display', intval($image_display)));
  272.         }
  273.         $images =& $this->getObjects($criteria, false, true);
  274.         $ret = array();
  275.         foreach (array_keys($images) as $i) {
  276.             $ret[$images[$i]->getVar('image_name')] = $images[$i]->getVar('image_nicename');
  277.         }
  278.         return $ret;
  279.     }
  280. }
  281. ?>