Zikula_Core
[ class tree: Zikula_Core ] [ index: Zikula_Core ] [ all elements ]

Source for file UserUtil.class.php

Documentation is available at UserUtil.class.php

  1. <?php
  2. /**
  3.  * Zikula Application Framework
  4.  *
  5.  * @copyright Robert Gasch
  6.  * @link http://www.zikula.org
  7.  * @version $Id: UserUtil.class.php 24342 2008-06-06 12:03:14Z markwest $
  8.  * @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
  9.  * @author Robert Gasch rgasch@gmail.com
  10.  * @package Zikula_Core
  11.  */
  12.  
  13. /**
  14.  * UserUtil
  15.  *
  16.  * @package Zikula_Core
  17.  * @subpackage UserUtil
  18.  */
  19. class UserUtil
  20. {
  21.     /**
  22.      * Return a user object
  23.      *
  24.      * @param uid         The userID of the user to retrieve
  25.      * @param getVars     whether or not to also get the user-variables (not implemented yet)
  26.      *
  27.      * @return The resulting user object
  28.      */
  29.     function getPNUser ($uid$getVars=false)
  30.     {
  31.         $user DBUtil::selectObjectByID ('users'$uid'uid');
  32.         if($user==false{
  33.             return LogUtil::registerError(_UNKNOWNUSER ' (' DataUtil::formatForDisplay($uid')');
  34.         }
  35.  
  36.         if ($getVars{
  37.             $dud UserUtil::getUserDynamicDataFields ($uid'uda_propid');
  38.             $user['DUD'$dud;
  39.         }
  40.  
  41.         return $user;
  42.     }
  43.  
  44.  
  45.     /**
  46.      * Return a field from a user object
  47.      *
  48.      * @param id         The userID of the user to retrieve
  49.      * @param field      The field from the user object to get
  50.      *
  51.      * @return The requested field
  52.      */
  53.     function getPNUserField ($id$field)
  54.     {
  55.         $user UserUtil::getPNUser ($id);
  56.         return $user[$field];
  57.     }
  58.  
  59.  
  60.     /**
  61.      * Return a hash structure mapping uid to username
  62.      *
  63.      * @param where         The where clause to use (optional)
  64.      * @param orderBy       The order by clause to use (optional)
  65.      * @param limitOffset   The select-limit offset (optional) (default=-1)
  66.      * @param limitNumRows  The number of rows to fetch (optional) (default=-1)
  67.      * @param assocKey      The associative key to apply (optional) (default='gid')
  68.      *
  69.      * @return An array mapping uid to username
  70.      */
  71.     function getPNUsers ($where=''$orderBy=''$limitOffset=-1$limitNumRows=-1$assocKey='uid')
  72.     {
  73.         return DBUtil::selectObjectArray ('users'$where$orderBy$limitOffset$limitNumRows$assocKey);
  74.     }
  75.  
  76.  
  77.     /**
  78.      * Return a group object
  79.      *
  80.      * @param gid         The groupID to retrieve
  81.      *
  82.      * @return The resulting group object
  83.      */
  84.     function getPNGroup ($gid)
  85.     {
  86.         return DBUtil::selectObjectByID ('groups'$gid'gid');
  87.     }
  88.  
  89.  
  90.     /**
  91.      * Return a hash structure mapping gid to groupname
  92.      *
  93.      * @param where          The where clause to use (optional) (default='')
  94.      * @param orderBy        The order by clause to use (optional) (default='')
  95.      * @param limitOffset    The select-limit offset (optional) (default=-1)
  96.      * @param limitNumRows   The number of rows to fetch (optional) (default=-1)
  97.      * @param assocKey       The associative key to apply (optional) (default='gid')
  98.      *
  99.      * @return An array mapping gid to groupname
  100.      */
  101.     function getPNGroups ($where=''$orderBy=''$limitOffset=-1$limitNumRows=-1$assocKey='gid')
  102.     {
  103.         return DBUtil::selectObjectArray ('groups'$where$orderBy$limitOffset$limitNumRows$assocKey);
  104.     }
  105.  
  106.  
  107.     /**
  108.      * Return a (string) list of user-ids which can then be used in a SQL 'IN (...)' clause
  109.      *
  110.      * @param where       The where clause to use (optional)
  111.      * @param orderBy     The order by clause to use (optional)
  112.      * @param separator   The field separator to use (default=",") (optional)
  113.      *
  114.      * @return string list of user ids
  115.      */
  116.     function getPNUserIdList ($where=''$orderBy=''$separator=',')
  117.     {
  118.         $userdata UserUtil::getPNUsers ($where$orderBy);
  119.  
  120.         $keys array_keys ($userdata);
  121.         $size sizeof ($keys);
  122.         $list '';
  123.  
  124.         if ($size == 0{
  125.             return '-1';
  126.         }
  127.  
  128.         for ($i=0$i<$size$i++{
  129.             $list .= $keys[$i$separator;
  130.         }
  131.  
  132.         if (($length strlen($list)) 0{
  133.             $listsubstr($list0$length 1);
  134.         }
  135.  
  136.         return $list;
  137.     }
  138.  
  139.  
  140.     /**
  141.      * Return a (string) list of group-ids which can then be used in a SQL 'IN (...)' clause
  142.      *
  143.      * @param where       The where clause to use (optional)
  144.      * @param orderBy     The order by clause to use (optional)
  145.      * @param separator   The field separator to use (default=",") (optional)
  146.      *
  147.      * @return string list of group ids
  148.      */
  149.     function getPNGroupIdList ($where=''$orderBy=''$separator=',')
  150.     {
  151.         $groupdata UserUtil::getPNGroups ($where$orderBy);
  152.  
  153.         $keys array_keys ($groupdata);
  154.         $numkeys sizeof ($keys);
  155.         $list '';
  156.  
  157.         for ($i=0$i<$numkeys$i++{
  158.             $list .= $keys[$i$separator;
  159.         }
  160.  
  161.         if (($length strlen($list)) 0{
  162.             $listsubstr($list0$length 1);
  163.         }
  164.  
  165.         return $list;
  166.     }
  167.  
  168.  
  169.     /**
  170.      * Return an array group-ids for the specified user
  171.      *
  172.      * @param uid         The user ID for which we want the groups
  173.      *
  174.      * @return An array of group IDs
  175.      */
  176.     function getGroupsForUser ($uid)
  177.     {
  178.         if (empty($uid)) {
  179.             return array();
  180.         }
  181.  
  182.         $where '';
  183.         if ($uid != -1{
  184.             $where "WHERE pn_uid = '" DataUtil::formatForStore($uid"'";
  185.         }
  186.  
  187.         $groups DBUtil::selectFieldArray ('group_membership''gid'$where);
  188.         return $groups;
  189.     }
  190.  
  191.  
  192.     /**
  193.      * Return a string list of group-ids for the specified user
  194.      *
  195.      * @param uid         The user ID for which we want the groups
  196.      * @param separator   The field separator to use (default=",") (optional)
  197.      *
  198.      * @return string list of group ids
  199.      */
  200.     function getGroupListForUser ($uid=0$separator=",")
  201.     {
  202.         if (!$uid{
  203.             $uid pnUserGetVar('uid');
  204.         }
  205.  
  206.         $gidArray UserUtil::getGroupsForUser ($uid);
  207.         $size count ($gidArray);
  208.         $gidlist '';
  209.  
  210.         if ($size == 0{
  211.             return "-1";
  212.         }
  213.  
  214.         for ($i=0$i<$size$i++{
  215.             $gidlist .= $gidArray[$i$separator;
  216.         }
  217.  
  218.         if (($length strlen($gidlist)) 0{
  219.             $gidlist substr($gidlist0$length 1);
  220.         }
  221.  
  222.         return $gidlist;
  223.     }
  224.  
  225.  
  226.     /**
  227.      * Return a string list of user-ids for the specified group
  228.      *
  229.      * @param gid         The group ID for which we want the users
  230.      * @param separator   The field separator to use (default=",") (optional)
  231.      *
  232.      * @return an array of user IDs
  233.      */
  234.     function getUsersForGroup ($gid$separator=",")
  235.     {
  236.         if (!$gid{
  237.             return array ();
  238.         }
  239.  
  240.         $where "WHERE pn_gid = '" DataUtil::formatForStore($gid"'";
  241.         $users DBUtil::selectFieldArray ('group_membership''uid'$where);
  242.         return $users;
  243.     }
  244.  
  245.  
  246.     /**
  247.      * Return the defined dynamic user data fields
  248.      *
  249.      * @return an array of dynamic data field definitions
  250.      */
  251.     function getDynamicDataFields ()
  252.     {
  253.         $dudfields DBUtil::selectObjectArray ('user_property');
  254.         return $dudfields;
  255.     }
  256.  
  257.  
  258.     /**
  259.      * Return a string list of user-ids for the specified group
  260.      *
  261.      * @param uid             The user ID for which we want the users
  262.      * @param assocKey        The associate Key to use
  263.      * @param standardFields  Whether or not to also marshall the standard user properties into the DUD array
  264.      *
  265.      * @return an array of user IDs
  266.      */
  267.     function getUserDynamicDataFields ($uid$assocKey='uda_propid'$standardFields=false)
  268.     {
  269.         if (!$uid{
  270.             return array();
  271.         }
  272.  
  273.         $where "WHERE pn_uda_uid = '" DataUtil::formatForStore($uid"'";
  274.         $dud   DBUtil::selectObjectArray ('user_data'$where''-1-1$assocKey);
  275.  
  276.         if ($standardFields{
  277.             $user UserUtil::getPNUser ($uidfalse);
  278.             foreach ($user as $k=>$v{
  279.                 if (strpos($k'user_')===0{
  280.                     $dud[$k$user[$k];
  281.                 }
  282.             }
  283.         }
  284.  
  285.         return $dud;
  286.     }
  287.  
  288.  
  289.     /**
  290.      * Return a PN array structure for the PN user group selector
  291.      *
  292.      * @param defaultValue    The default value of the selector (default=0) (optional)
  293.      * @param defaultText     The text of the default value (optional)
  294.      * @param ignore          An array of keys to ignore (optional)
  295.      * @param includeAll      whether to include an "All" choice (optional)
  296.      * @param allText         The text to display for the "All" choice (optional)
  297.      *
  298.      * @return The PN array structure for the user group selector
  299.      */
  300.     function getSelectorData_PNGroup ($defaultValue=0$defaultText=''$ignore=array(),
  301.     $includeAll=0$allText='')
  302.     {
  303.         $dropdown array ();
  304.  
  305.         if ($defaultText{
  306.             $dropdown[array ('id'=>$defaultValue'name'=>$defaultText);
  307.         }
  308.  
  309.         $groupdata UserUtil::getPNGroups ('''ORDER BY pn_name');
  310.  
  311.         if (sizeof($groupdata== 0{
  312.             return $dropdown;
  313.         }
  314.  
  315.         $keys array_keys ($groupdata);
  316.         $numkeys sizeof ($keys);
  317.  
  318.         if ($includeAll{
  319.             $dropdown[array ('id'=>$includeAll'name'=>$allText);
  320.         }
  321.  
  322.         for ($i=0$i<$numkeys$i++{
  323.             if (!isset($ignore[$keys[$i]])) {
  324.                 $dropdown[array ('id'=>$keys[$i]'name'=>$groupdata[$keys[$i]]['name']);
  325.             }
  326.         }
  327.  
  328.         return $dropdown;
  329.     }
  330.  
  331.  
  332.     /**
  333.      * Return a PN array strcuture for the PN user dropdown box
  334.      *
  335.      * @param defaultValue    The default value of the selector (optional) (default=0)
  336.      * @param defaultText     The text of the default value (optional) (default='')
  337.      * @param ignore          An array of keys to ignore (optional) (default=array())
  338.      * @param includeAll      whether to include an "All" choice (optional) (default=0)
  339.      * @param allText         The text to display for the "All" choice (optional) (default='')
  340.      * @param exclude         An SQL IN-LIST string to exclude specified uids
  341.      *
  342.      * @return The PN array structure for the user group selector
  343.      */
  344.     function getSelectorData_PNUser ($defaultValue=0$defaultText=''$ignore=array(),
  345.     $includeAll=0$allText=''$exclude='')
  346.     {
  347.         $dropdown array ();
  348.  
  349.         if ($defaultText{
  350.             $dropdown[array ('id'=>$defaultValue'name'=>$defaultText);
  351.         }
  352.  
  353.         $where '';
  354.         if ($exclude{
  355.             $where "WHERE pn_uid NOT IN (" DataUtil::formatForStore($exclude")";
  356.         }
  357.  
  358.         $userdata UserUtil::getPNUsers($where'ORDER BY pn_uname');
  359.  
  360.         if (sizeof ($userdata== 0{
  361.             return $dropdown;
  362.         }
  363.  
  364.         $keys array_keys ($userdata);
  365.         $numkeys sizeof ($keys);
  366.  
  367.         if ($includeAll{
  368.             $dropdown[array ('id'=>$includeAll'name'=>$allText);
  369.         }
  370.  
  371.         for ($i=0$i<$numkeys$i++{
  372.             if (!isset($ignore[$keys[$i]])) {
  373.                 $dropdown[array ('id'=>$keys[$i]'name'=>$userdata[$keys[$i]]['name']);
  374.             }
  375.         }
  376.  
  377.         return $dropdown;
  378.     }
  379. }

Documentation generated on Fri, 18 Jul 2008 21:58:56 +0200 by phpDocumentor 1.4.1