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 / avatar.php next >
Encoding:
PHP Script  |  2007-10-19  |  10.1 KB  |  253 lines

  1. <?php
  2. // $Id: avatar.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. class XoopsAvatar extends XoopsObject
  37. {
  38.     var $_userCount;
  39.  
  40.     function XoopsAvatar()
  41.     {
  42.         $this->XoopsObject();
  43.         $this->initVar('avatar_id', XOBJ_DTYPE_INT, null, false);
  44.         $this->initVar('avatar_file', XOBJ_DTYPE_OTHER, null, false, 30);
  45.         $this->initVar('avatar_name', XOBJ_DTYPE_TXTBOX, null, true, 100);
  46.         $this->initVar('avatar_mimetype', XOBJ_DTYPE_OTHER, null, false);
  47.         $this->initVar('avatar_created', XOBJ_DTYPE_INT, null, false);
  48.         $this->initVar('avatar_display', XOBJ_DTYPE_INT, 1, false);
  49.         $this->initVar('avatar_weight', XOBJ_DTYPE_INT, 0, false);
  50.         $this->initVar('avatar_type', XOBJ_DTYPE_OTHER, 0, false);
  51.     }
  52.  
  53.     function setUserCount($value)
  54.     {
  55.         $this->_userCount = intval($value);
  56.     }
  57.  
  58.     function getUserCount()
  59.     {
  60.         return $this->_userCount;
  61.     }
  62. }
  63.  
  64.  
  65. /**
  66. * XOOPS avatar handler class.
  67. * This class is responsible for providing data access mechanisms to the data source
  68. * of XOOPS avatar class objects.
  69. *
  70. *
  71. * @author  Kazumi Ono <onokazu@xoops.org>
  72. */
  73.  
  74. class XoopsAvatarHandler extends XoopsObjectHandler
  75. {
  76.  
  77.     function &create($isNew = true)
  78.     {
  79.         $avatar = new XoopsAvatar();
  80.         if ($isNew) {
  81.             $avatar->setNew();
  82.         }
  83.         return $avatar;
  84.     }
  85.  
  86.     function &get($id)
  87.     {
  88.         $avatar = false;
  89.         $id = intval($id);
  90.         if ($id > 0) {
  91.             $sql = 'SELECT * FROM '.$this->db->prefix('avatar').' WHERE avatar_id='.$id;
  92.             if (!$result = $this->db->query($sql)) {
  93.                 return false;
  94.             }
  95.             $numrows = $this->db->getRowsNum($result);
  96.             if ($numrows == 1) {
  97.                 $avatar = new XoopsAvatar();
  98.                 $avatar->assignVars($this->db->fetchArray($result));
  99.                 return $avatar;
  100.             }
  101.         }
  102.         return $avatar;
  103.     }
  104.  
  105.     function insert(&$avatar)
  106.     {
  107.         /**
  108.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  109.         */
  110.         if (!is_a($avatar, 'xoopsavatar')) {
  111.             return false;
  112.         }
  113.         if (!$avatar->isDirty()) {
  114.             return true;
  115.         }
  116.         if (!$avatar->cleanVars()) {
  117.             return false;
  118.         }
  119.         foreach ($avatar->cleanVars as $k => $v) {
  120.             ${$k} = $v;
  121.         }
  122.         if ($avatar->isNew()) {
  123.             $avatar_id = $this->db->genId('avatar_avatar_id_seq');
  124.             $sql = sprintf("INSERT INTO %s (avatar_id, avatar_file, avatar_name, avatar_created, avatar_mimetype, avatar_display, avatar_weight, avatar_type) VALUES (%u, %s, %s, %u, %s, %u, %u, %s)", $this->db->prefix('avatar'), $avatar_id, $this->db->quoteString($avatar_file), $this->db->quoteString($avatar_name), time(), $this->db->quoteString($avatar_mimetype), $avatar_display, $avatar_weight, $this->db->quoteString($avatar_type));
  125.         } else {
  126.             $sql = sprintf("UPDATE %s SET avatar_file = %s, avatar_name = %s, avatar_created = %u, avatar_mimetype= %s, avatar_display = %u, avatar_weight = %u, avatar_type = %s WHERE avatar_id = %u", $this->db->prefix('avatar'), $this->db->quoteString($avatar_file), $this->db->quoteString($avatar_name), $avatar_created, $this->db->quoteString($avatar_mimetype), $avatar_display, $avatar_weight, $this->db->quoteString($avatar_type), $avatar_id);
  127.         }
  128.         if (!$result = $this->db->query($sql)) {
  129.             return false;
  130.         }
  131.         if (empty($avatar_id)) {
  132.             $avatar_id = $this->db->getInsertId();
  133.         }
  134.         $avatar->assignVar('avatar_id', $avatar_id);
  135.         return true;
  136.     }
  137.  
  138.     function delete(&$avatar)
  139.     {
  140.         /**
  141.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  142.         */
  143.         if (!is_a($avatar, 'xoopsavatar')) {
  144.             return false;
  145.         }
  146.  
  147.  
  148.         $id = $avatar->getVar('avatar_id');
  149.         $sql = sprintf("DELETE FROM %s WHERE avatar_id = %u", $this->db->prefix('avatar'), $id);
  150.         if (!$result = $this->db->query($sql)) {
  151.             return false;
  152.         }
  153.         $sql = sprintf("DELETE FROM %s WHERE avatar_id = %u", $this->db->prefix('avatar_user_link'), $id);
  154.         $result = $this->db->query($sql);
  155.         return true;
  156.     }
  157.  
  158.     function &getObjects($criteria = null, $id_as_key = false)
  159.     {
  160.         $ret = array();
  161.         $limit = $start = 0;
  162.         $sql = 'SELECT a.*, COUNT(u.user_id) AS count FROM '.$this->db->prefix('avatar').' a LEFT JOIN '.$this->db->prefix('avatar_user_link').' u ON u.avatar_id=a.avatar_id';
  163.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  164.             $sql .= ' '.$criteria->renderWhere();
  165.             $sql .= ' GROUP BY a.avatar_id ORDER BY avatar_weight, avatar_id';
  166.             $limit = $criteria->getLimit();
  167.             $start = $criteria->getStart();
  168.         }
  169.         $result = $this->db->query($sql, $limit, $start);
  170.         if (!$result) {
  171.             return $ret;
  172.         }
  173.         while ($myrow = $this->db->fetchArray($result)) {
  174.             $avatar = new XoopsAvatar();
  175.             $avatar->assignVars($myrow);
  176.             $avatar->setUserCount($myrow['count']);
  177.             if (!$id_as_key) {
  178.                 $ret[] =& $avatar;
  179.             } else {
  180.                 $ret[$myrow['avatar_id']] =& $avatar;
  181.             }
  182.             unset($avatar);
  183.         }
  184.         return $ret;
  185.     }
  186.  
  187.     function getCount($criteria = null)
  188.     {
  189.         $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('avatar');
  190.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  191.             $sql .= ' '.$criteria->renderWhere();
  192.         }
  193.         if (!$result =& $this->db->query($sql)) {
  194.             return 0;
  195.         }
  196.         list($count) = $this->db->fetchRow($result);
  197.         return $count;
  198.     }
  199.  
  200.     function addUser($avatar_id, $user_id){
  201.         $avatar_id = intval($avatar_id);
  202.         $user_id = intval($user_id);
  203.         if ($avatar_id < 1 || $user_id < 1) {
  204.             return false;
  205.         }
  206.         $sql = sprintf("DELETE FROM %s WHERE user_id = %u", $this->db->prefix('avatar_user_link'), $user_id);
  207.         $this->db->query($sql);
  208.         $sql = sprintf("INSERT INTO %s (avatar_id, user_id) VALUES (%u, %u)", $this->db->prefix('avatar_user_link'), $avatar_id, $user_id);
  209.         if (!$result =& $this->db->query($sql)) {
  210.             return false;
  211.         }
  212.         return true;
  213.     }
  214.  
  215.     function getUser(&$avatar){
  216.         $ret = array();
  217.  
  218.         /**
  219.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  220.         */
  221.         if (!is_a($avatar, 'xoopsavatar')) {
  222.             return false;
  223.         }
  224.  
  225.         $sql = 'SELECT user_id FROM '.$this->db->prefix('avatar_user_link').' WHERE avatar_id='.$avatar->getVar('avatar_id');
  226.         if (!$result = $this->db->query($sql)) {
  227.             return $ret;
  228.         }
  229.         while ($myrow = $this->db->fetchArray($result)) {
  230.             $ret[] =& $myrow['user_id'];
  231.         }
  232.         return $ret;
  233.     }
  234.  
  235.     function getList($avatar_type = null, $avatar_display = null)
  236.     {
  237.         $criteria = new CriteriaCompo();
  238.         if (isset($avatar_type)) {
  239.             $avatar_type = ($avatar_type == 'C') ? 'C' : 'S';
  240.             $criteria->add(new Criteria('avatar_type', $avatar_type));
  241.         }
  242.         if (isset($avatar_display)) {
  243.             $criteria->add(new Criteria('avatar_display', intval($avatar_display)));
  244.         }
  245.         $avatars =& $this->getObjects($criteria, true);
  246.         $ret = array('blank.gif' => _NONE);
  247.         foreach (array_keys($avatars) as $i) {
  248.             $ret[$avatars[$i]->getVar('avatar_file')] = $avatars[$i]->getVar('avatar_name');
  249.         }
  250.         return $ret;
  251.     }
  252. }
  253. ?>