5 * Sets up database results and preferences for a group and abstracts this info.
7 * Foundry.class.php and Project.class.php call this.
9 * Project.class.php contains all the deprecated API from the old group.php file
11 * DEPENDS on user.php being present and setup properly
13 * GENERALLY YOU SHOULD NEVER INSTANTIATE THIS OBJECT DIRECTLY
14 * USE group_get_object() to instantiate properly
16 * @version $Id: Group.class.php 4707 2005-10-04 11:53:48Z danper $
17 * @author Tim Perdue <tperdue@valinux.com>
20 * This file is part of GForge.
22 * GForge is free software; you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation; either version 2 of the License, or
25 * (at your option) any later version.
27 * GForge is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 * GNU General Public License for more details.
32 * You should have received a copy of the GNU General Public License
33 * along with GForge; if not, write to the Free Software
34 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
37 require_once $gfcommon.'include/gettext.php';
42 * group_get_object() - Get the group object.
44 * group_get_object() is useful so you can pool group objects/save database queries
45 * You should always use this instead of instantiating the object directly.
47 * You can now optionally pass in a db result handle. If you do, it re-uses that query
48 * to instantiate the objects.
50 * IMPORTANT! That db result must contain all fields
51 * from groups table or you will have problems
54 * @param int Result set handle ("SELECT * FROM groups WHERE group_id=xx")
55 * @return a group object or false on failure
57 function &group_get_object($group_id,$res=false) {
58 //create a common set of group objects
59 //saves a little wear on the database
61 //automatically checks group_type and
62 //returns appropriate object
65 if (!isset($GROUP_OBJ["_".$group_id."_"])) {
67 //the db result handle was passed in
69 $res=db_query("SELECT * FROM groups WHERE group_id='$group_id'");
71 if (!$res || db_numrows($res) < 1) {
72 $GROUP_OBJ["_".$group_id."_"]=false;
75 check group type and set up object
77 if (db_result($res,0,'type_id')==1) {
79 $GROUP_OBJ["_".$group_id."_"]= new Group($group_id,$res);
82 $GROUP_OBJ["_".$group_id."_"]=false;
86 return $GROUP_OBJ["_".$group_id."_"];
88 function &group_get_object_by_name($groupname) {
89 $res=db_query("SELECT * FROM groups WHERE unix_group_name='$groupname'");
90 return group_get_object(db_result($res,0,'group_id'),$res);
93 class Group extends Error {
95 * Associative array of data from db.
97 * @var array $data_array.
102 * array of User objects.
104 * @var array $membersArr.
109 * Permissions data row from db.
111 * @var array $perm_data_array.
113 var $perm_data_array;
116 * Whether the use is an admin/super user of this project.
118 * @var bool $is_admin.
123 * Artifact types result handle.
125 * @var int $types_res.
130 * Associative array of data for plugins.
132 * @var array $plugins_array.
137 * Group - Group object constructor - use group_get_object() to instantiate.
139 * @param int Required - group_id of the group you want to instantiate.
140 * @param int Database result from select query OR associative array of all columns.
142 function Group($id=false, $res=false) {
145 //setting up an empty object
146 //probably going to call create()
150 if (!$this->fetchData($id)) {
155 // Assoc array was passed in
157 if (is_array($res)) {
158 $this->data_array =& $res;
160 if (db_numrows($res) < 1) {
161 //function in class we extended
162 $this->setError('Group Not Found');
163 $this->data_array=array();
166 //set up an associative array for use by other functions
167 db_reset_result($res);
168 $this->data_array =& db_fetch_array($res);
173 $systemGroups = array(GROUP_IS_NEWS, GROUP_IS_STATS, GROUP_IS_PEER_RATINGS);
174 if(!$this->isPublic() && !in_array($id, $systemGroups)) {
175 $perm =& $this->getPermission(session_get_user());
177 if (!$perm || !is_object($perm) || !$perm->isMember()) {
178 $this->setError(_('Permission denied'), ERROR__PERMISSION_DENIED_ERROR);
186 * fetchData - May need to refresh database fields if an update occurred.
188 * @param int The group_id.
190 function fetchData($group_id) {
191 $res = db_query("SELECT * FROM groups WHERE group_id='$group_id'");
192 if (!$res || db_numrows($res) < 1) {
193 $this->setError('fetchData():: '.db_error());
196 $this->data_array =& db_fetch_array($res);
201 * getPlugins - get a list of all available group plugins
203 * @return array array containing plugin_id => plugin_name
205 function getPlugins() {
206 if (!isset($this->plugins_data)) {
207 $this->plugins_data = array () ;
208 $sql="SELECT group_plugin.plugin_id, plugins.plugin_name
209 FROM group_plugin, plugins
210 WHERE group_plugin.group_id=".$this->getID()."
211 AND group_plugin.plugin_id = plugins.plugin_id" ;
213 $rows = db_numrows($res);
215 for ($i=0; $i<$rows; $i++) {
216 $plugin_id = db_result($res,$i,'plugin_id');
217 $this->plugins_data[$plugin_id] = db_result($res,$i,'plugin_name');
220 return $this->plugins_data ;
224 * usesPlugin - returns true if the group uses a particular plugin
226 * @param string name of the plugin
227 * @return boolean whether plugin is being used or not
229 function usesPlugin($pluginname) {
230 $plugins_data = $this->getPlugins() ;
231 foreach ($plugins_data as $p_id => $p_name) {
232 if ($p_name == $pluginname) {
240 * setPluginUse - enables/disables plugins for the group
242 * @param string name of the plugin
243 * @param boolean the new state
244 * @return string database result
246 function setPluginUse($pluginname, $val=true) {
247 if ($val == $this->usesPlugin($pluginname)) {
248 // State is already good, returning
251 $sql="SELECT plugin_id
253 WHERE plugin_name = '" . $pluginname . "'" ;
255 $rows = db_numrows($res);
257 // Error: no plugin by that name
260 $plugin_id = db_result($res,0,'plugin_id');
262 unset ($this->plugins_data) ;
264 $sql="INSERT INTO group_plugin (group_id, plugin_id)
265 VALUES (". $this->getID() . ", ". $plugin_id .")" ;
269 $sql="DELETE FROM group_plugin
270 WHERE group_id = ". $this->getID() . "
271 AND plugin_id = ". $plugin_id ;
278 * getPermission - Return a Permission for this Group and the specified User.
280 * @param object The user you wish to get permission for (usually the logged in user).
281 * @return object The Permission.
283 function &getPermission(&$_user) {
284 return permission_get_object($this, $_user);
288 * isPublic - Simply returns the is_public flag from the database.
290 * @return boolean is_public.
292 function isPublic() {
293 return $this->data_array['is_public'];
297 * getID - Simply return the group_id for this object.
299 * @return int group_id.
302 return $this->data_array['group_id'];
306 * usesSCM - whether or not this group has opted to use SCM.
308 * @return boolean uses_scm.
313 return $this->data_array['use_scm'];
320 * usesMail - whether or not this group has opted to use mailing lists.
322 * @return boolean uses_mail.
324 function usesMail() {
325 global $sys_use_mail;
327 return $this->data_array['use_mail'];
334 * usesNews - whether or not this group has opted to use news.
336 * @return boolean uses_news.
338 function usesNews() {
339 global $sys_use_news;
341 return $this->data_array['use_news'];
348 * usesForum - whether or not this group has opted to use discussion forums.
350 * @return boolean uses_forum.
352 function usesForum() {
353 global $sys_use_forum;
354 if ($sys_use_forum) {
355 return $this->data_array['use_forum'];
362 * usesStats - whether or not this group has opted to use stats.
364 * @return boolean uses_stats.
366 function usesStats() {
367 return $this->data_array['use_stats'];
371 * usesFRS - whether or not this group has opted to use file release system.
373 * @return boolean uses_frs.
378 return $this->data_array['use_frs'];
385 * usesTracker - whether or not this group has opted to use tracker.
387 * @return boolean uses_tracker.
389 function usesTracker() {
390 global $sys_use_tracker;
391 if ($sys_use_tracker) {
392 return $this->data_array['use_tracker'];
399 * usesDocman - whether or not this group has opted to use docman.
401 * @return boolean uses_docman.
403 function usesDocman() {
404 global $sys_use_docman;
405 if ($sys_use_docman) {
406 return $this->data_array['use_docman'];
413 * usesFTP - whether or not this group has opted to use FTP.
415 * @return boolean uses_ftp.
420 return $this->data_array['use_ftp'];
427 * usesSurvey - whether or not this group has opted to use surveys.
429 * @return boolean uses_survey.
431 function usesSurvey() {
432 global $sys_use_survey;
433 if ($sys_use_survey) {
434 return $this->data_array['use_survey'];
441 * usesPM - whether or not this group has opted to Project Manager.
443 * @return boolean uses_projman.
448 return $this->data_array['use_pm'];
455 * isProject - Simple boolean test to see if it's a project or not.
457 * @return boolean is_project.
459 function isProject() {
460 if ($this->getType()==1) {
468 * getType() - Foundry, project, etc.
470 * @return int The type flag from the database.
473 return $this->data_array['type_id'];
477 * getUnixName - the unix_name
479 * @return string unix_name.
481 function getUnixName() {
482 return strtolower($this->data_array['unix_group_name']);
486 * getPublicName - the full-length public name.
488 * @return string The group_name.
490 function getPublicName() {
491 return htmlspecialchars($this->data_array['group_name']);
497 // c-file-style: "bsd"