3 * FusionForge document manager
5 * Copyright 2000, Quentin Cregan/Sourceforge
6 * Copyright 2002, Tim Perdue/GForge, LLC
7 * Copyright 2009, Roland Mas
9 * This file is part of FusionForge.
11 * FusionForge is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published
13 * by the Free Software Foundation; either version 2 of the License,
14 * or (at your option) any later version.
16 * FusionForge is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with FusionForge; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 require_once $gfcommon.'include/Error.class.php';
29 class DocumentGroup extends Error {
41 * @var array $data_array.
46 * DocumentGroup - constructor.
48 * Use this constructor if you are modifying an existing doc_group.
50 * @param object Group object.
51 * @param array (all fields from doc_groups) OR doc_group from database.
52 * @return boolean success.
54 function DocumentGroup(&$Group, $data=false) {
58 if (!$Group || !is_object($Group)) {
59 $this->setError(_('DocumentGroup: No Valid Group'));
62 //did Group have an error?
63 if ($Group->isError()) {
64 $this->setError('DocumentGroup: '.$Group->getErrorMessage());
67 $this->Group =& $Group;
70 if (is_array($data)) {
71 $this->data_array =& $data;
73 // should verify group_id
77 if (!$this->fetchData($data)) {
87 * create - create a new item in the database.
89 * @param string Item name.
90 * @return id on success / false on failure.
92 function create($name,$parent_doc_group=0) {
97 $this->setError(_('DocumentGroup: name is Required'));
101 if ($parent_doc_group) {
102 // check if parent group exists
103 $res = db_query_params ('SELECT * FROM doc_groups WHERE doc_group=$1 AND group_id=$2',
104 array ($parent_doc_group,
105 $this->Group->getID())) ;
106 if (!$res || db_numrows($res) < 1) {
107 $this->setError(_('DocumentGroup: Invalid DocumentGroup parent ID'));
111 $parent_doc_group = 0;
114 $perm =& $this->Group->getPermission ();
115 if (!$perm || !$perm->isDocEditor()) {
116 $this->setPermissionDeniedError();
120 $res=db_query_params('SELECT * FROM doc_groups WHERE groupname=$1 AND parent_doc_group=$2 AND group_id=$3',
123 $this->Group->getID()));
124 if ($res && db_numrows($res) > 0) {
125 $this->setError(_('Group name already exists'));
129 $result = db_query_params ('INSERT INTO doc_groups (group_id,groupname,parent_doc_group,stateid) VALUES ($1, $2, $3, $4)',
130 array ($this->Group->getID(),
131 htmlspecialchars($name),
134 if ($result && db_affected_rows($result) > 0) {
137 $this->setError(_('DocumentGroup::create() Error Adding Group: ').db_error());
141 $doc_group = db_insertid($result, 'doc_groups', 'doc_group');
143 // Now set up our internal data structures
144 if (!$this->fetchData($doc_group)) {
152 * delete - delete a DocumentGroup.
153 * delete is recursive and permanent
154 * @param integer Document Group Id, integer Project Group Id
157 function delete($doc_groupid,$project_group_id) {
158 $perm =& $this->Group->getPermission ();
159 if (!$perm || !$perm->isDocEditor()) {
160 $this->setPermissionDeniedError();
164 /* delete documents in directory */
165 $result = db_query_params ('DELETE FROM doc_data where doc_group = $1 and group_id = $2',
169 /* delete directory */
170 $result = db_query_params ('DELETE FROM doc_groups where doc_group = $1 and group_id = $2',
176 /* is there any subdir ? */
177 $subdir = db_query_params ('select doc_group from doc_groups where parent_doc_group = $1 and group_id = $2',
180 /* make a recursive call */
181 while ($arr = db_fetch_array($subdir)) {
182 $this->delete($arr['doc_group'],$project_group_id);
192 * fetchData - re-fetch the data for this DocumentGroup from the database.
194 * @param int ID of the doc_group.
197 function fetchData($id) {
198 $res = db_query_params ('SELECT * FROM doc_groups WHERE doc_group=$1',
200 if (!$res || db_numrows($res) < 1) {
201 $this->setError(_('DocumentGroup: Invalid DocumentGroup ID'));
204 $this->data_array = db_fetch_array($res);
205 db_free_result($res);
210 * getGroup - get the Group Object this DocumentGroup is associated with.
212 * @return Object Group.
214 function &getGroup() {
219 * getID - get this DocumentGroup's ID.
221 * @return int The id #.
224 return $this->data_array['doc_group'];
228 * getID - get parent DocumentGroup's id.
230 * @return int The id #.
232 function getParentID() {
233 return $this->data_array['parent_doc_group'];
237 * getName - get the name.
239 * @return String The name.
242 return $this->data_array['groupname'];
246 * getState - get the state id.
248 * @return Int The state id.
250 function getState() {
251 return $this->data_array['stateid'];
255 * update - update a DocumentGroup.
257 * @param string Name of the category.
260 function update($name,$parent_doc_group) {
261 $perm =& $this->Group->getPermission ();
262 if (!$perm || !$perm->isDocEditor()) {
263 $this->setPermissionDeniedError();
267 $this->setMissingParamsError();
271 if ($parent_doc_group) {
272 // check if parent group exists
273 $res = db_query_params ('SELECT * FROM doc_groups WHERE doc_group=$1 AND group_id=$2',
274 array ($parent_doc_group,
275 $this->Group->getID())) ;
276 if (!$res || db_numrows($res) < 1) {
277 $this->setError(_('DocumentGroup: Invalid DocumentGroup parent ID'));
284 $res=db_query_params ('SELECT * FROM doc_groups WHERE groupname=$1 AND parent_doc_group=$2 AND group_id=$3',
287 $this->Group->getID()));
288 if ($res && db_numrows($res) > 0) {
289 $this->setError(_("Group name already exists"));
293 $result = db_query_params ('UPDATE doc_groups SET groupname=$1, parent_doc_group=$2 WHERE doc_group=$3 AND group_id=$4',
294 array(htmlspecialchars($name),
297 $this->Group->getID())) ;
298 if ($result && db_affected_rows($result) > 0) {
301 $this->setOnUpdateError(_('DocumentGroup: ').db_error());
307 * hasDocuments - Recursive function that checks if this group or any of it childs has documents associated to it
309 * A group has associated documents if and only if there are documents associated to this
310 * group or to any of its childs
312 * @param array Array of nested groups information, fetched from DocumentGroupFactory class
313 * @param object The DocumentFactory object
314 * @param int (optional) State of the documents
316 function hasDocuments(&$nested_groups, &$document_factory, $stateid=0) {
317 $doc_group_id = $this->getID();
318 static $result = array(); // this function will probably be called several times so we better store results in order to speed things up
319 if (!array_key_exists($stateid, $result) || !is_array($result[$stateid])) $result[$stateid] = array();
321 if (array_key_exists($doc_group_id, $result[$stateid])) return $result[$stateid][$doc_group_id];
323 // check if it has documents
325 $document_factory->setStateID($stateid);
327 $document_factory->setDocGroupID($doc_group_id);
328 $docs = $document_factory->getDocuments();
329 if (is_array($docs) && count($docs) > 0) { // this group has documents
330 $result[$stateid][$doc_group_id] = true;
334 // this group doesn't have documents... check recursively on the childs
335 if (array_key_exists($doc_group_id,$nested_groups) && is_array($nested_groups[$doc_group_id])) {
336 $count = count($nested_groups[$doc_group_id]);
337 for ($i=0; $i < $count; $i++) {
338 if ($nested_groups[$doc_group_id][$i]->hasDocuments($nested_groups, $document_factory, $stateid)) {
339 // child has documents
340 $result[$stateid][$doc_group_id] = true;
344 // no child has documents, then this group doesn't have associated documents
345 $result[$stateid][$doc_group_id] = false;
347 } else { // this group doesn't have childs
348 $result[$stateid][$doc_group_id] = false;
354 * hasSubgroup - Checks if this group has a specified subgroup associated to it
356 * @param array Array of nested groups information, fetched from DocumentGroupFactory class
357 * @param int ID of the subgroup
359 function hasSubgroup(&$nested_groups, $doc_subgroup_id) {
360 $doc_group_id = $this->getID();
362 if (is_array(@$nested_groups[$doc_group_id])) {
363 $count = count($nested_groups[$doc_group_id]);
364 for ($i=0; $i < $count; $i++) {
366 if ($nested_groups[$doc_group_id][$i]->getID() == $doc_subgroup_id) {
369 // recursively check if this child has this subgroup
370 if ($nested_groups[$doc_group_id][$i]->hasSubgroup($nested_groups, $doc_subgroup_id)) {
381 * setStateID - set the state id of this document group
383 * @param int State ID
385 function setStateID($stateid) {
386 $res = db_query_params ('UPDATE doc_groups SET stateid=$1
389 array ($stateid,$this->getID(),$this->Group->getID()));
391 if (!$res || db_affected_rows($res) < 1) {
392 $this->setOnUpdateError(_('DocumentGroup :').db_error());
402 // c-file-style: "bsd"