3 * FusionForge document manager
5 * Copyright 2000, Quentin Cregan/Sourceforge
6 * Copyright 2002, Tim Perdue/GForge, LLC
7 * Copyright 2009, Roland Mas
8 * Copyright 2010, Franck Villaume - Capgemini
9 * http://fusionforge.org
11 * This file is part of FusionForge. FusionForge is free software;
12 * you can redistribute it and/or modify it under the terms of the
13 * GNU General Public License as published by the Free Software
14 * Foundation; either version 2 of the Licence, or (at your option)
17 * FusionForge 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 along
23 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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(_('Document Directory: No Valid Project'));
62 //did Group have an error?
63 if ($Group->isError()) {
64 $this->setError(_('Document Directory:').' '.$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 boolean 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, $this->Group->getID())) ;
105 if (!$res || db_numrows($res) < 1) {
106 $this->setError(_('DocumentGroup: Invalid Document Directory parent ID'));
110 $parent_doc_group = 0;
113 $perm =& $this->Group->getPermission();
114 if (!$perm || !$perm->isDocEditor()) {
115 $this->setPermissionDeniedError();
119 $res=db_query_params('SELECT * FROM doc_groups WHERE groupname=$1 AND parent_doc_group=$2 AND group_id=$3',
122 $this->Group->getID())
124 if ($res && db_numrows($res) > 0) {
125 $this->setError(_('Directory 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),
135 if ($result && db_affected_rows($result) > 0) {
138 $this->setError(_('DocumentGroup::create() Error Adding Directory:').' '.db_error());
142 $doc_group = db_insertid($result, 'doc_groups', 'doc_group');
144 // Now set up our internal data structures
145 if (!$this->fetchData($doc_group)) {
153 * delete - delete a DocumentGroup.
154 * WARNING delete is recursive and permanent
155 * @param int Document Group Id, integer Project Group Id
156 * @return boolean success
158 function delete($doc_groupid,$project_group_id) {
159 $perm =& $this->Group->getPermission ();
160 if (!$perm || !$perm->isDocEditor()) {
161 $this->setPermissionDeniedError();
165 /* delete documents in directory */
166 $result = db_query_params('DELETE FROM doc_data where doc_group = $1 and group_id = $2',
167 array($doc_groupid, $project_group_id));
169 /* delete directory */
170 $result = db_query_params('DELETE FROM doc_groups where doc_group = $1 and group_id = $2',
171 array($doc_groupid, $project_group_id));
175 /* is there any subdir ? */
176 $subdir = db_query_params('select doc_group from doc_groups where parent_doc_group = $1 and group_id = $2',
177 array($doc_groupid, $project_group_id));
178 /* make a recursive call */
179 while ($arr = db_fetch_array($subdir)) {
180 $this->delete($arr['doc_group'], $project_group_id);
190 * fetchData - re-fetch the data for this DocumentGroup from the database.
192 * @param int ID of the doc_group.
193 * @return boolean success
195 function fetchData($id) {
196 $res = db_query_params('SELECT * FROM doc_groups WHERE doc_group=$1',
198 if (!$res || db_numrows($res) < 1) {
199 $this->setError(_('DocumentGroup: Invalid Document Directory ID'));
202 $this->data_array = db_fetch_array($res);
203 db_free_result($res);
208 * getGroup - get the Group Object this DocumentGroup is associated with.
210 * @return Object Group.
212 function &getGroup() {
217 * getID - get this DocumentGroup's ID.
219 * @return int The id #.
222 return $this->data_array['doc_group'];
226 * getID - get parent DocumentGroup's id.
228 * @return int The id #.
230 function getParentID() {
231 return $this->data_array['parent_doc_group'];
235 * getName - get the name.
237 * @return String The name.
240 return $this->data_array['groupname'];
244 * getState - get the state id.
246 * @return Int The state id.
248 function getState() {
249 return $this->data_array['stateid'];
253 * update - update a DocumentGroup.
255 * @param string Name of the category.
258 function update($name, $parent_doc_group) {
259 $perm =& $this->Group->getPermission ();
260 if (!$perm || !$perm->isDocEditor()) {
261 $this->setPermissionDeniedError();
265 $this->setMissingParamsError();
269 if ($parent_doc_group) {
270 // check if parent group exists
271 $res = db_query_params('SELECT * FROM doc_groups WHERE doc_group=$1 AND group_id=$2',
272 array($parent_doc_group,
273 $this->Group->getID())
275 if (!$res || db_numrows($res) < 1) {
276 $this->setError(_('DocumentGroup: Invalid Document Directory parent ID'));
280 $parent_doc_group = 0;
283 $res=db_query_params('SELECT * FROM doc_groups WHERE groupname=$1 AND parent_doc_group=$2 AND group_id=$3',
286 $this->Group->getID())
288 if ($res && db_numrows($res) > 0) {
289 $this->setError(_('Directory 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())
299 if ($result && db_affected_rows($result) > 0) {
300 $this->fetchData($this->getID()) ;
303 $this->setOnUpdateError(_('DocumentGroup:').' '.db_error());
309 * hasDocuments - Recursive function that checks if this group or any of it childs has documents associated to it
311 * A group has associated documents if and only if there are documents associated to this
312 * group or to any of its childs
314 * @param array Array of nested groups information, fetched from DocumentGroupFactory class
315 * @param object The DocumentFactory object
316 * @param int (optional) State of the documents
317 * @return boolean success
319 function hasDocuments(&$nested_groups, &$document_factory, $stateid = 0) {
320 $doc_group_id = $this->getID();
321 static $result = array(); // this function will probably be called several times so we better store results in order to speed things up
322 if (!array_key_exists($stateid, $result) || !is_array($result[$stateid]))
323 $result[$stateid] = array();
325 if (array_key_exists($doc_group_id, $result[$stateid]))
326 return $result[$stateid][$doc_group_id];
328 // check if it has documents
330 $document_factory->setStateID($stateid);
332 $document_factory->setDocGroupID($doc_group_id);
333 $docs = $document_factory->getDocuments();
334 if (is_array($docs) && count($docs) > 0) { // this group has documents
335 $result[$stateid][$doc_group_id] = true;
339 // this group doesn't have documents... check recursively on the childs
340 if (array_key_exists($doc_group_id, $nested_groups) && is_array($nested_groups[$doc_group_id])) {
341 $count = count($nested_groups[$doc_group_id]);
342 for ($i=0; $i < $count; $i++) {
343 if ($nested_groups[$doc_group_id][$i]->hasDocuments($nested_groups, $document_factory, $stateid)) {
344 // child has documents
345 $result[$stateid][$doc_group_id] = true;
349 // no child has documents, then this group doesn't have associated documents
350 $result[$stateid][$doc_group_id] = false;
352 } else { // this group doesn't have childs
353 $result[$stateid][$doc_group_id] = false;
359 * hasSubgroup - Checks if this group has a specified subgroup associated to it
361 * @param array Array of nested groups information, fetched from DocumentGroupFactory class
362 * @param int ID of the subgroup
363 * @return boolean success
365 function hasSubgroup(&$nested_groups, $doc_subgroup_id) {
366 $doc_group_id = $this->getID();
368 if (is_array(@$nested_groups[$doc_group_id])) {
369 $count = count($nested_groups[$doc_group_id]);
370 for ($i=0; $i < $count; $i++) {
372 if ($nested_groups[$doc_group_id][$i]->getID() == $doc_subgroup_id) {
375 // recursively check if this child has this subgroup
376 if ($nested_groups[$doc_group_id][$i]->hasSubgroup($nested_groups, $doc_subgroup_id)) {
386 * setStateID - set the state id of this document group
388 * @param int State ID
389 * @return boolean success
391 function setStateID($stateid) {
392 $res = db_query_params('UPDATE doc_groups SET stateid=$1
395 array ($stateid,$this->getID(),$this->Group->getID())
398 if (!$res || db_affected_rows($res) < 1) {
399 $this->setOnUpdateError(_('DocumentGroup:').' '.db_error());
409 // c-file-style: "bsd"