3 * GForge Doc Mgr Facility
5 * Copyright 2002 GForge, LLC
10 * This file is part of GForge.
12 * GForge is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * GForge is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with GForge; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 by Quentin Cregan, SourceForge 06/2000
33 Complete OO rewrite by Tim Perdue 1/2003
36 require_once('common/include/Error.class.php');
38 class DocumentGroup extends Error {
50 * @var array $data_array.
55 * DocumentGroup - constructor.
57 * Use this constructor if you are modifying an existing doc_group.
59 * @param object Group object.
60 * @param array (all fields from doc_groups) OR doc_group from database.
61 * @return boolean success.
63 function DocumentGroup(&$Group, $data=false) {
67 if (!$Group || !is_object($Group)) {
68 $this->setError('DocumentGroup: No Valid Group');
71 //did Group have an error?
72 if ($Group->isError()) {
73 $this->setError('DocumentGroup: '.$Group->getErrorMessage());
76 $this->Group =& $Group;
79 if (is_array($data)) {
80 $this->data_array =& $data;
82 // should verify group_id
86 if (!$this->fetchData($data)) {
96 * create - create a new item in the database.
98 * @param string Item name.
99 * @return id on success / false on failure.
101 function create($name,$parent_doc_group=0) {
106 $this->setError(_('DocumentGroup: name is Required'));
110 if ($parent_doc_group) {
111 // check if parent group exists
112 $res=db_query("SELECT * FROM doc_groups WHERE doc_group='$parent_doc_group' AND group_id=".$this->Group->getID());
113 if (!$res || db_numrows($res) < 1) {
114 $this->setError(_('DocumentGroup: Invalid DocumentGroup parent ID'));
118 $parent_doc_group = 0;
121 $perm =& $this->Group->getPermission (session_get_user());
122 if (!$perm || !$perm->isDocEditor()) {
123 $this->setPermissionDeniedError();
127 $sql="INSERT INTO doc_groups (group_id,groupname,parent_doc_group)
128 VALUES ('".$this->Group->getID()."','".htmlspecialchars($name)."','".$parent_doc_group."')";
129 $result=db_query($sql);
130 if ($result && db_affected_rows($result) > 0) {
133 $this->setError('DocumentGroup::create() Error Adding Group: '.db_error());
137 $doc_group = db_insertid($result, 'doc_groups', 'doc_group');
139 // Now set up our internal data structures
140 if (!$this->fetchData($doc_group)) {
149 * fetchData - re-fetch the data for this DocumentGroup from the database.
151 * @param int ID of the doc_group.
154 function fetchData($id) {
155 $res=db_query("SELECT * FROM doc_groups WHERE doc_group='$id'");
156 if (!$res || db_numrows($res) < 1) {
157 $this->setError(_('DocumentGroup: Invalid DocumentGroup ID'));
160 $this->data_array =& db_fetch_array($res);
161 db_free_result($res);
166 * getGroup - get the Group Object this DocumentGroup is associated with.
168 * @return Object Group.
170 function &getGroup() {
175 * getID - get this DocumentGroup's ID.
177 * @return int The id #.
180 return $this->data_array['doc_group'];
184 * getID - get parent DocumentGroup's id.
186 * @return int The id #.
188 function getParentID() {
189 return $this->data_array['parent_doc_group'];
193 * getName - get the name.
195 * @return String The name.
198 return $this->data_array['groupname'];
202 * update - update a DocumentGroup.
204 * @param string Name of the category.
207 function update($name,$parent_doc_group) {
208 $perm =& $this->Group->getPermission (session_get_user());
209 if (!$perm || !$perm->isDocEditor()) {
210 $this->setPermissionDeniedError();
214 $this->setMissingParamsError();
218 if ($parent_doc_group) {
219 // check if parent group exists
220 $res=db_query("SELECT * FROM doc_groups WHERE doc_group='$parent_doc_group' AND group_id=".$this->Group->getID());
221 if (!$res || db_numrows($res) < 1) {
222 $this->setError(_('DocumentGroup: Invalid DocumentGroup parent ID'));
229 $sql="UPDATE doc_groups
230 SET groupname='".htmlspecialchars($name)."',
231 parent_doc_group='".$parent_doc_group."'
232 WHERE doc_group='". $this->getID() ."'
233 AND group_id='".$this->Group->getID()."'";
234 $result=db_query($sql);
235 if ($result && db_affected_rows($result) > 0) {
238 $this->setError(db_error());
244 * hasDocuments - Recursive function that checks if this group or any of it childs has documents associated to it
246 * A group has associated documents if and only if there are documents associated to this
247 * group or to any of its childs
249 * @param array Array of nested groups information, fetched from DocumentGroupFactory class
250 * @param object The DocumentFactory object
251 * @param int (optional) State of the documents
253 function hasDocuments(&$nested_groups, &$document_factory, $stateid=0) {
254 static $result = array(); // this function will probably be called several times so we better store results in order to speed things up
255 if (!is_array($result[$stateid])) $result[$stateid] = array();
256 if (array_key_exists($doc_group_id, $result[$stateid])) return $result[$stateid][$doc_group_id];
258 $doc_group_id = $this->getID();
260 // check if it has documents
262 $document_factory->setStateID($stateid);
264 $document_factory->setDocGroupID($doc_group_id);
265 $docs = $document_factory->getDocuments();
266 if (is_array($docs) && count($docs) > 0) { // this group has documents
267 $result[$stateid][$doc_group_id] = true;
271 // this group doesn't have documents... check recursively on the childs
272 if (is_array($nested_groups["$doc_group_id"])) {
273 $count = count($nested_groups["$doc_group_id"]);
274 for ($i=0; $i < $count; $i++) {
275 if ($nested_groups["$doc_group_id"][$i]->hasDocuments($nested_groups, $document_factory, $stateid)) {
276 // child has documents
277 $result[$stateid][$doc_group_id] = true;
281 // no child has documents, then this group doesn't have associated documents
282 $result[$stateid][$doc_group_id] = false;
284 } else { // this group doesn't have childs
285 $result[$stateid][$doc_group_id] = false;
291 * hasSubgroup - Checks if this group has a specified subgroup associated to it
293 * @param array Array of nested groups information, fetched from DocumentGroupFactory class
294 * @param int ID of the subgroup
296 function hasSubgroup(&$nested_groups, $doc_subgroup_id) {
297 $doc_group_id = $this->getID();
299 if (is_array($nested_groups["$doc_group_id"])) {
300 $count = count($nested_groups["$doc_group_id"]);
301 for ($i=0; $i < $count; $i++) {
303 if ($nested_groups["$doc_group_id"][$i]->getID() == $doc_subgroup_id) {
306 // recursively check if this child has this subgroup
307 if ($nested_groups["$doc_group_id"][$i]->hasSubgroup($nested_groups, $doc_subgroup_id)) {
320 // c-file-style: "bsd"