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 (session_get_user());
115 if (!$perm || !$perm->isDocEditor()) {
116 $this->setPermissionDeniedError();
120 $result = db_query_params ('INSERT INTO doc_groups (group_id,groupname,parent_doc_group) VALUES ($1, $2, $3)',
121 array ($this->Group->getID(),
122 htmlspecialchars($name),
123 $parent_doc_group)) ;
124 if ($result && db_affected_rows($result) > 0) {
127 $this->setError('DocumentGroup::create() Error Adding Group: '.db_error());
131 $doc_group = db_insertid($result, 'doc_groups', 'doc_group');
133 // Now set up our internal data structures
134 if (!$this->fetchData($doc_group)) {
143 * fetchData - re-fetch the data for this DocumentGroup from the database.
145 * @param int ID of the doc_group.
148 function fetchData($id) {
149 $res = db_query_params ('SELECT * FROM doc_groups WHERE doc_group=$1',
151 if (!$res || db_numrows($res) < 1) {
152 $this->setError(_('DocumentGroup: Invalid DocumentGroup ID'));
155 $this->data_array =& db_fetch_array($res);
156 db_free_result($res);
161 * getGroup - get the Group Object this DocumentGroup is associated with.
163 * @return Object Group.
165 function &getGroup() {
170 * getID - get this DocumentGroup's ID.
172 * @return int The id #.
175 return $this->data_array['doc_group'];
179 * getID - get parent DocumentGroup's id.
181 * @return int The id #.
183 function getParentID() {
184 return $this->data_array['parent_doc_group'];
188 * getName - get the name.
190 * @return String The name.
193 return $this->data_array['groupname'];
197 * update - update a DocumentGroup.
199 * @param string Name of the category.
202 function update($name,$parent_doc_group) {
203 $perm =& $this->Group->getPermission (session_get_user());
204 if (!$perm || !$perm->isDocEditor()) {
205 $this->setPermissionDeniedError();
209 $this->setMissingParamsError();
213 if ($parent_doc_group) {
214 // check if parent group exists
215 $res = db_query_params ('SELECT * FROM doc_groups WHERE doc_group=$1 AND group_id=$2',
216 array ($parent_doc_group,
217 $this->Group->getID())) ;
218 if (!$res || db_numrows($res) < 1) {
219 $this->setError(_('DocumentGroup: Invalid DocumentGroup parent ID'));
226 $result = db_query_params ('UPDATE doc_groups SET groupname=$1, parent_doc_group=$2 WHERE doc_group=$3 AND group_id=$4',
227 array(htmlspecialchars($name),
230 $this->Group->getID())) ;
231 if ($result && db_affected_rows($result) > 0) {
234 $this->setError(db_error());
240 * delete - delete a DocumentGroup.
241 * delete is recursive and permanent
242 * TODO : use the delete status as document ?
243 * @param integer Document Group Id, integer Project Group Id
246 function delete($doc_groupid,$project_group_id) {
247 $perm =& $this->Group->getPermission (session_get_user());
248 if (!$perm || !$perm->isDocEditor()) {
249 $this->setPermissionDeniedError();
253 /* delete documents in directory */
254 $result = db_query_params ('DELETE FROM doc_data where doc_group = $1 and group_id = $2',
258 /* delete directory */
259 $result = db_query_params ('DELETE FROM doc_groups where doc_group = $1 and group_id = $2',
264 /* is there any subdir ? */
265 $result = db_query_params ('select doc_group from doc_groups where parent_doc_group = $1 and group_id = $2',
268 /* make a recursive call */
269 while ($arr = db_fetch_array($result)) {
270 $this->delete($arr,$project_group_id);
280 * hasDocuments - Recursive function that checks if this group or any of it childs has documents associated to it
282 * A group has associated documents if and only if there are documents associated to this
283 * group or to any of its childs
285 * @param array Array of nested groups information, fetched from DocumentGroupFactory class
286 * @param object The DocumentFactory object
287 * @param int (optional) State of the documents
289 function hasDocuments(&$nested_groups, &$document_factory, $stateid=0) {
290 global $doc_group_id;
291 $doc_group_id = $this->getID();
292 static $result = array(); // this function will probably be called several times so we better store results in order to speed things up
293 if (!array_key_exists($stateid, $result) || !is_array($result[$stateid])) $result[$stateid] = array();
295 if (array_key_exists($doc_group_id, $result[$stateid])) return $result[$stateid][$doc_group_id];
297 // check if it has documents
299 $document_factory->setStateID($stateid);
301 $document_factory->setDocGroupID($doc_group_id);
302 $docs = $document_factory->getDocuments();
303 if (is_array($docs) && count($docs) > 0) { // this group has documents
304 $result[$stateid][$doc_group_id] = true;
308 // this group doesn't have documents... check recursively on the childs
309 if (array_key_exists($doc_group_id,$nested_groups) && is_array($nested_groups[$doc_group_id])) {
310 $count = count($nested_groups[$doc_group_id]);
311 for ($i=0; $i < $count; $i++) {
312 if ($nested_groups[$doc_group_id][$i]->hasDocuments($nested_groups, $document_factory, $stateid)) {
313 // child has documents
314 $result[$stateid][$doc_group_id] = true;
318 // no child has documents, then this group doesn't have associated documents
319 $result[$stateid][$doc_group_id] = false;
321 } else { // this group doesn't have childs
322 $result[$stateid][$doc_group_id] = false;
328 * hasSubgroup - Checks if this group has a specified subgroup associated to it
330 * @param array Array of nested groups information, fetched from DocumentGroupFactory class
331 * @param int ID of the subgroup
333 function hasSubgroup(&$nested_groups, $doc_subgroup_id) {
334 $doc_group_id = $this->getID();
336 if (is_array(@$nested_groups[$doc_group_id])) {
337 $count = count($nested_groups[$doc_group_id]);
338 for ($i=0; $i < $count; $i++) {
340 if ($nested_groups[$doc_group_id][$i]->getID() == $doc_subgroup_id) {
343 // recursively check if this child has this subgroup
344 if ($nested_groups[$doc_group_id][$i]->hasSubgroup($nested_groups, $doc_subgroup_id)) {
357 // c-file-style: "bsd"