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) VALUES ($1, $2, $3)',
130 array ($this->Group->getID(),
131 htmlspecialchars($name),
132 $parent_doc_group)) ;
133 if ($result && db_affected_rows($result) > 0) {
136 $this->setError('DocumentGroup::create() Error Adding Group: '.db_error());
140 $doc_group = db_insertid($result, 'doc_groups', 'doc_group');
142 // Now set up our internal data structures
143 if (!$this->fetchData($doc_group)) {
151 * delete - delete a DocumentGroup.
152 * delete is recursive and permanent
153 * TODO : use the delete status as document ?
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',
175 /* is there any subdir ? */
176 $result = db_query_params ('select doc_group from doc_groups where parent_doc_group = $1 and group_id = $2',
179 /* make a recursive call */
180 while ($arr = db_fetch_array($result)) {
181 $this->delete($arr,$project_group_id);
191 * fetchData - re-fetch the data for this DocumentGroup from the database.
193 * @param int ID of the doc_group.
196 function fetchData($id) {
197 $res = db_query_params ('SELECT * FROM doc_groups WHERE doc_group=$1',
199 if (!$res || db_numrows($res) < 1) {
200 $this->setError(_('DocumentGroup: Invalid DocumentGroup ID'));
203 $this->data_array =& db_fetch_array($res);
204 db_free_result($res);
209 * getGroup - get the Group Object this DocumentGroup is associated with.
211 * @return Object Group.
213 function &getGroup() {
218 * getID - get this DocumentGroup's ID.
220 * @return int The id #.
223 return $this->data_array['doc_group'];
227 * getID - get parent DocumentGroup's id.
229 * @return int The id #.
231 function getParentID() {
232 return $this->data_array['parent_doc_group'];
236 * getName - get the name.
238 * @return String The name.
241 return $this->data_array['groupname'];
245 * update - update a DocumentGroup.
247 * @param string Name of the category.
250 function update($name,$parent_doc_group) {
251 $perm =& $this->Group->getPermission ();
252 if (!$perm || !$perm->isDocEditor()) {
253 $this->setPermissionDeniedError();
257 $this->setMissingParamsError();
261 if ($parent_doc_group) {
262 // check if parent group exists
263 $res = db_query_params ('SELECT * FROM doc_groups WHERE doc_group=$1 AND group_id=$2',
264 array ($parent_doc_group,
265 $this->Group->getID())) ;
266 if (!$res || db_numrows($res) < 1) {
267 $this->setError(_('DocumentGroup: Invalid DocumentGroup parent ID'));
274 $res=db_query_params ('SELECT * FROM doc_groups WHERE groupname=$1 AND parent_doc_group=$2 AND group_id=$3',
277 $this->Group->getID()));
278 if ($res && db_numrows($res) > 0) {
279 $this->setError(_("Group name already exists"));
283 $result = db_query_params ('UPDATE doc_groups SET groupname=$1, parent_doc_group=$2 WHERE doc_group=$3 AND group_id=$4',
284 array(htmlspecialchars($name),
287 $this->Group->getID())) ;
288 if ($result && db_affected_rows($result) > 0) {
291 $this->setError(db_error());
297 * hasDocuments - Recursive function that checks if this group or any of it childs has documents associated to it
299 * A group has associated documents if and only if there are documents associated to this
300 * group or to any of its childs
302 * @param array Array of nested groups information, fetched from DocumentGroupFactory class
303 * @param object The DocumentFactory object
304 * @param int (optional) State of the documents
306 function hasDocuments(&$nested_groups, &$document_factory, $stateid=0) {
307 $doc_group_id = $this->getID();
308 static $result = array(); // this function will probably be called several times so we better store results in order to speed things up
309 if (!array_key_exists($stateid, $result) || !is_array($result[$stateid])) $result[$stateid] = array();
311 if (array_key_exists($doc_group_id, $result[$stateid])) return $result[$stateid][$doc_group_id];
313 // check if it has documents
315 $document_factory->setStateID($stateid);
317 $document_factory->setDocGroupID($doc_group_id);
318 $docs = $document_factory->getDocuments();
319 if (is_array($docs) && count($docs) > 0) { // this group has documents
320 $result[$stateid][$doc_group_id] = true;
324 // this group doesn't have documents... check recursively on the childs
325 if (array_key_exists($doc_group_id,$nested_groups) && is_array($nested_groups[$doc_group_id])) {
326 $count = count($nested_groups[$doc_group_id]);
327 for ($i=0; $i < $count; $i++) {
328 if ($nested_groups[$doc_group_id][$i]->hasDocuments($nested_groups, $document_factory, $stateid)) {
329 // child has documents
330 $result[$stateid][$doc_group_id] = true;
334 // no child has documents, then this group doesn't have associated documents
335 $result[$stateid][$doc_group_id] = false;
337 } else { // this group doesn't have childs
338 $result[$stateid][$doc_group_id] = false;
344 * hasSubgroup - Checks if this group has a specified subgroup associated to it
346 * @param array Array of nested groups information, fetched from DocumentGroupFactory class
347 * @param int ID of the subgroup
349 function hasSubgroup(&$nested_groups, $doc_subgroup_id) {
350 $doc_group_id = $this->getID();
352 if (is_array(@$nested_groups[$doc_group_id])) {
353 $count = count($nested_groups[$doc_group_id]);
354 for ($i=0; $i < $count; $i++) {
356 if ($nested_groups[$doc_group_id][$i]->getID() == $doc_subgroup_id) {
359 // recursively check if this child has this subgroup
360 if ($nested_groups[$doc_group_id][$i]->hasSubgroup($nested_groups, $doc_subgroup_id)) {
373 // c-file-style: "bsd"