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 / group.php < prev    next >
Encoding:
PHP Script  |  2007-10-19  |  15.5 KB  |  459 lines

  1. <?php
  2. // $Id: group.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.  * a group of users
  38.  *
  39.  * @copyright copyright (c) 2000-2003 XOOPS.org
  40.  * @author Kazumi Ono <onokazu@xoops.org>
  41.  * @package kernel
  42.  */
  43. class XoopsGroup extends XoopsObject
  44. {
  45.     /**
  46.      * constructor
  47.      */
  48.     function XoopsGroup()
  49.     {
  50.         $this->XoopsObject();
  51.         $this->initVar('groupid', XOBJ_DTYPE_INT, null, false);
  52.         $this->initVar('name', XOBJ_DTYPE_TXTBOX, null, true, 100);
  53.         $this->initVar('description', XOBJ_DTYPE_TXTAREA, null, false);
  54.         $this->initVar('group_type', XOBJ_DTYPE_OTHER, null, false);
  55.     }
  56. }
  57.  
  58.  
  59. /**
  60. * XOOPS group handler class.
  61. * This class is responsible for providing data access mechanisms to the data source
  62. * of XOOPS group class objects.
  63. *
  64. * @author Kazumi Ono <onokazu@xoops.org>
  65. * @copyright copyright (c) 2000-2003 XOOPS.org
  66. * @package kernel
  67. * @subpackage member
  68. */
  69. class XoopsGroupHandler extends XoopsObjectHandler
  70. {
  71.  
  72.     /**
  73.      * create a new {@link XoopsGroup} object
  74.      *
  75.      * @param bool $isNew mark the new object as "new"?
  76.      * @return object XoopsGroup reference to the new object
  77.      *
  78.      */
  79.     function &create($isNew = true)
  80.     {
  81.         $group = new XoopsGroup();
  82.         if ($isNew) {
  83.             $group->setNew();
  84.         }
  85.         return $group;
  86.     }
  87.  
  88.     /**
  89.      * retrieve a specific group
  90.      *
  91.      * @param int $id ID of the group to get
  92.      * @return object XoopsGroup reference to the group object, FALSE if failed
  93.      */
  94.     function &get($id)
  95.     {
  96.       $id = intval($id);
  97.       $group = false;
  98.         if ($id > 0) {
  99.             $sql = 'SELECT * FROM '.$this->db->prefix('groups').' WHERE groupid='.$id;
  100.             if (!$result = $this->db->query($sql)) {
  101.                 return $group;
  102.             }
  103.             $numrows = $this->db->getRowsNum($result);
  104.             if ($numrows == 1) {
  105.                 $group = new XoopsGroup();
  106.                 $group->assignVars($this->db->fetchArray($result));
  107.             }
  108.         }
  109.         return $group;
  110.     }
  111.  
  112.     /**
  113.      * insert a group into the database
  114.      *
  115.      * @param object reference to the group object
  116.      * @return mixed ID of the group if inserted, FALSE if failed, TRUE if already present and unchanged.
  117.      */
  118.     function insert(&$group)
  119.     {
  120.         /**
  121.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  122.         */
  123.         if (!is_a($group, 'xoopsgroup')) {
  124.             return false;
  125.         }
  126.         if (!$group->isDirty()) {
  127.             return true;
  128.         }
  129.         if (!$group->cleanVars()) {
  130.             return false;
  131.         }
  132.         foreach ($group->cleanVars as $k => $v) {
  133.             ${$k} = $v;
  134.         }
  135.         if ($group->isNew()) {
  136.             $groupid = $this->db->genId('group_groupid_seq');
  137.             $sql = sprintf("INSERT INTO %s (groupid, name, description, group_type) VALUES (%u, %s, %s, %s)", $this->db->prefix('groups'), $groupid, $this->db->quoteString($name), $this->db->quoteString($description), $this->db->quoteString($group_type));
  138.         } else {
  139.             $sql = sprintf("UPDATE %s SET name = %s, description = %s, group_type = %s WHERE groupid = %u", $this->db->prefix('groups'), $this->db->quoteString($name), $this->db->quoteString($description), $this->db->quoteString($group_type), $groupid);
  140.         }
  141.         if (!$result = $this->db->query($sql)) {
  142.             return false;
  143.         }
  144.         if (empty($groupid)) {
  145.             $groupid = $this->db->getInsertId();
  146.         }
  147.         $group->assignVar('groupid', $groupid);
  148.         return true;
  149.     }
  150.  
  151.     /**
  152.      * remove a group from the database
  153.      *
  154.      * @param object $group reference to the group to be removed
  155.      * @return bool FALSE if failed
  156.      */
  157.     function delete(&$group)
  158.     {
  159.         /**
  160.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  161.         */
  162.         if (!is_a($group, 'xoopsgroup')) {
  163.             return false;
  164.         }
  165.         $sql = sprintf("DELETE FROM %s WHERE groupid = %u", $this->db->prefix('groups'), $group->getVar('groupid'));
  166.         if (!$result = $this->db->query($sql)) {
  167.             return false;
  168.         }
  169.         return true;
  170.     }
  171.  
  172.     /**
  173.      * retrieve groups from the database
  174.      *
  175.      * @param object $criteria {@link CriteriaElement} with conditions for the groups
  176.      * @param bool $id_as_key should the groups' IDs be used as keys for the associative array?
  177.      * @return mixed Array of groups
  178.      */
  179.     function getObjects($criteria = null, $id_as_key = false)
  180.     {
  181.         $ret = array();
  182.         $limit = $start = 0;
  183.         $sql = 'SELECT * FROM '.$this->db->prefix('groups');
  184.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  185.             $sql .= ' '.$criteria->renderWhere();
  186.             $limit = $criteria->getLimit();
  187.             $start = $criteria->getStart();
  188.         }
  189.         $result = $this->db->query($sql, $limit, $start);
  190.         if (!$result) {
  191.             return $ret;
  192.         }
  193.         while ($myrow = $this->db->fetchArray($result)) {
  194.             $group = new XoopsGroup();
  195.             $group->assignVars($myrow);
  196.             if (!$id_as_key) {
  197.                 $ret[] =& $group;
  198.             } else {
  199.                 $ret[$myrow['groupid']] =& $group;
  200.             }
  201.             unset($group);
  202.         }
  203.         return $ret;
  204.     }
  205. }
  206.  
  207. /**
  208.  * membership of a user in a group
  209.  *
  210.  * @author Kazumi Ono <onokazu@xoops.org>
  211.  * @copyright copyright (c) 2000-2003 XOOPS.org
  212.  * @package kernel
  213.  */
  214. class XoopsMembership extends XoopsObject
  215. {
  216.     /**
  217.      * constructor
  218.      */
  219.     function XoopsMembership()
  220.     {
  221.         $this->XoopsObject();
  222.         $this->initVar('linkid', XOBJ_DTYPE_INT, null, false);
  223.         $this->initVar('groupid', XOBJ_DTYPE_INT, null, false);
  224.         $this->initVar('uid', XOBJ_DTYPE_INT, null, false);
  225.     }
  226. }
  227.  
  228. /**
  229. * XOOPS membership handler class. (Singleton)
  230. *
  231. * This class is responsible for providing data access mechanisms to the data source
  232. * of XOOPS group membership class objects.
  233. *
  234. * @author Kazumi Ono <onokazu@xoops.org>
  235. * @copyright copyright (c) 2000-2003 XOOPS.org
  236. * @package kernel
  237. */
  238. class XoopsMembershipHandler extends XoopsObjectHandler
  239. {
  240.  
  241.     /**
  242.      * create a new membership
  243.      *
  244.      * @param bool $isNew should the new object be set to "new"?
  245.      * @return object XoopsMembership
  246.      */
  247.     function &create($isNew = true)
  248.     {
  249.         $mship = new XoopsMembership();
  250.         if ($isNew) {
  251.             $mship->setNew();
  252.         }
  253.         return $mship;
  254.     }
  255.  
  256.     /**
  257.      * retrieve a membership
  258.      *
  259.      * @param int $id ID of the membership to get
  260.      * @return mixed reference to the object if successful, else FALSE
  261.      */
  262.     function &get($id)
  263.     {
  264.       $id = intval($id);
  265.       $mship = false;
  266.         if ($id > 0) {
  267.             $sql = 'SELECT * FROM '.$this->db->prefix('groups_users_link').' WHERE linkid='.$id;
  268.             if (!$result = $this->db->query($sql)) {
  269.                 return $mship;
  270.             }
  271.             $numrows = $this->db->getRowsNum($result);
  272.             if ($numrows == 1) {
  273.                 $mship = new XoopsMembership();
  274.                 $mship->assignVars($this->db->fetchArray($result));
  275.             }
  276.         }
  277.         return $mship;
  278.     }
  279.  
  280.     /**
  281.      * inserts a membership in the database
  282.      *
  283.      * @param object $mship reference to the membership object
  284.      * @return bool TRUE if already in DB or successful, FALSE if failed
  285.      */
  286.     function insert(&$mship)
  287.     {
  288.         /**
  289.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  290.         */
  291.         if (!is_a($mship, 'xoopsmembership')) {
  292.             return false;
  293.         }
  294.         if (!$mship->isDirty()) {
  295.             return true;
  296.         }
  297.         if (!$mship->cleanVars()) {
  298.             return false;
  299.         }
  300.         foreach ($mship->cleanVars as $k => $v) {
  301.             ${$k} = $v;
  302.         }
  303.         if ($mship->isNew()) {
  304.             $linkid = $this->db->genId('groups_users_link_linkid_seq');
  305.             $sql = sprintf("INSERT INTO %s (linkid, groupid, uid) VALUES (%u, %u, %u)", $this->db->prefix('groups_users_link'), $linkid, $groupid, $uid);
  306.         } else {
  307.             $sql = sprintf("UPDATE %s SET groupid = %u, uid = %u WHERE linkid = %u", $this->db->prefix('groups_users_link'), $groupid, $uid, $linkid);
  308.         }
  309.         if (!$result = $this->db->query($sql)) {
  310.             return false;
  311.         }
  312.         if (empty($linkid)) {
  313.             $linkid = $this->db->getInsertId();
  314.         }
  315.         $mship->assignVar('linkid', $linkid);
  316.         return true;
  317.     }
  318.  
  319.     /**
  320.      * delete a membership from the database
  321.      *
  322.      * @param object $mship reference to the membership object
  323.      * @return bool FALSE if failed
  324.      */
  325.     function delete(&$mship)
  326.     {
  327.         /**
  328.         * @TODO: Change to if (!(class_exists($this->className) && $obj instanceof $this->className)) when going fully PHP5
  329.         */
  330.         if (!is_a($mship, 'xoopsmembership')) {
  331.             return false;
  332.         }
  333.  
  334.         $sql = sprintf("DELETE FROM %s WHERE linkid = %u", $this->db->prefix('groups_users_link'), $groupm->getVar('linkid'));
  335.         if (!$result = $this->db->query($sql)) {
  336.             return false;
  337.         }
  338.         return true;
  339.     }
  340.  
  341.     /**
  342.      * retrieve memberships from the database
  343.      *
  344.      * @param object $criteria {@link CriteriaElement} conditions to meet
  345.      * @param bool $id_as_key should the ID be used as the array's key?
  346.      * @return array array of references
  347.      */
  348.     function getObjects($criteria = null, $id_as_key = false)
  349.     {
  350.         $ret = array();
  351.         $limit = $start = 0;
  352.         $sql = 'SELECT * FROM '.$this->db->prefix('groups_users_link');
  353.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  354.             $sql .= ' '.$criteria->renderWhere();
  355.             $limit = $criteria->getLimit();
  356.             $start = $criteria->getStart();
  357.         }
  358.         $result = $this->db->query($sql, $limit, $start);
  359.         if (!$result) {
  360.             return $ret;
  361.         }
  362.         while ($myrow = $this->db->fetchArray($result)) {
  363.             $mship = new XoopsMembership();
  364.             $mship->assignVars($myrow);
  365.             if (!$id_as_key) {
  366.                 $ret[] =& $mship;
  367.             } else {
  368.                 $ret[$myrow['linkid']] =& $mship;
  369.             }
  370.             unset($mship);
  371.         }
  372.         return $ret;
  373.     }
  374.  
  375.     /**
  376.      * count how many memberships meet the conditions
  377.      *
  378.      * @param object $criteria {@link CriteriaElement} conditions to meet
  379.      * @return int
  380.      */
  381.     function getCount($criteria = null)
  382.     {
  383.         $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('groups_users_link');
  384.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  385.             $sql .= ' '.$criteria->renderWhere();
  386.         }
  387.         $result = $this->db->query($sql);
  388.         if (!$result) {
  389.             return 0;
  390.         }
  391.         list($count) = $this->db->fetchRow($result);
  392.         return $count;
  393.     }
  394.  
  395.     /**
  396.      * delete all memberships meeting the conditions
  397.      *
  398.      * @param object $criteria {@link CriteriaElement} with conditions to meet
  399.      * @return bool
  400.      */
  401.     function deleteAll($criteria = null)
  402.     {
  403.         $sql = 'DELETE FROM '.$this->db->prefix('groups_users_link');
  404.         if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
  405.             $sql .= ' '.$criteria->renderWhere();
  406.         }
  407.         if (!$result = $this->db->query($sql)) {
  408.             return false;
  409.         }
  410.         return true;
  411.     }
  412.  
  413.     /**
  414.      * retrieve groups for a user
  415.      *
  416.      * @param int $uid ID of the user
  417.      * @param bool $asobject should the groups be returned as {@link XoopsGroup}
  418.      * objects? FALSE returns associative array.
  419.      * @return array array of groups the user belongs to
  420.      */
  421.     function getGroupsByUser($uid)
  422.     {
  423.         $ret = array();
  424.         $sql = 'SELECT groupid FROM '.$this->db->prefix('groups_users_link').' WHERE uid='.intval($uid);
  425.         $result = $this->db->query($sql);
  426.         if (!$result) {
  427.             return $ret;
  428.         }
  429.         while ($myrow = $this->db->fetchArray($result)) {
  430.             $ret[] = $myrow['groupid'];
  431.         }
  432.         return $ret;
  433.     }
  434.  
  435.     /**
  436.      * retrieve users belonging to a group
  437.      *
  438.      * @param int $groupid ID of the group
  439.      * @param bool $asobject return users as {@link XoopsUser} objects?
  440.      * FALSE will return arrays
  441.      * @param int $limit number of entries to return
  442.      * @param int $start offset of first entry to return
  443.      * @return array array of users belonging to the group
  444.      */
  445.     function getUsersByGroup($groupid, $limit=0, $start=0)
  446.     {
  447.         $ret = array();
  448.         $sql = 'SELECT uid FROM '.$this->db->prefix('groups_users_link').' WHERE groupid='.intval($groupid);
  449.         $result = $this->db->query($sql, $limit, $start);
  450.         if (!$result) {
  451.             return $ret;
  452.         }
  453.         while ($myrow = $this->db->fetchArray($result)) {
  454.             $ret[] = $myrow['uid'];
  455.         }
  456.         return $ret;
  457.     }
  458. }
  459. ?>