3 * FusionForge Documentation Manager
5 * Copyright 2000, Quentin Cregan/Sourceforge
6 * Copyright 2002-2003, Tim Perdue/GForge, LLC
7 * Copyright 2009, Roland Mas
8 * Copyright 2010-2011, Franck Villaume - Capgemini
9 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
10 * Copyright 2012-2016, Franck Villaume - TrivialDev
11 * http://fusionforge.org
13 * This file is part of FusionForge. FusionForge is free software;
14 * you can redistribute it and/or modify it under the terms of the
15 * GNU General Public License as published by the Free Software
16 * Foundation; either version 2 of the Licence, or (at your option)
19 * FusionForge is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
26 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 require_once $gfcommon.'include/FFError.class.php';
30 require_once $gfcommon.'docman/Document.class.php';
32 class DocumentFactory extends FFError {
42 * The Documents dictionary.
44 * @var array Contains doc_group_id as key and the array of documents associated to that id.
49 * The stateid Array limit
50 * @var array Contains the different stateid to limit return documents in getDocuments.
52 var $stateidArr = array();
55 * The doc_group_id limit
56 * @var integer Contains the doc_group id to limit return documents in getDocuments.
62 * @var string Contains the order to return documents in getDocuments.
63 * Default value is ASC
69 * @var array Contains the order of columns to sort before return documents in getDocuments.
70 * Default value is title order
72 var $order = array('title');
76 * @var integer Contains the limit of documents retrieve by getDocuments.
77 * Default value is 0 which means NO LIMIT
83 * @var int Contains the valid state of documentgroups to retrieve documents using getDocuments.
84 * Default value is 1 which means public any
86 var $docgroupstate = 1;
90 * @var integer Contains the offset of the query used to retrive documents using getDocuments.
91 * Default value is 0 which means NO OFFSET
95 var $validdocumentgroups = array();
101 * @internal param \The $object Group object to which this DocumentFactory is associated.
102 * @return \DocumentFactory
105 function __construct(&$Group) {
106 parent::__construct();
107 if (!$Group || !is_object($Group)) {
108 $this->setError(_('No Valid Group Object'));
112 if ($Group->isError()) {
113 $this->setError('ProjectGroup: '.$Group->getErrorMessage());
117 $this->Group =& $Group;
121 * getGroup - get the Group object this DocumentFactory is associated with.
123 * @return object the Group object.
125 function &getGroup() {
130 * setStateID - call this before getDocuments() if you want to limit to some specific states.
132 * @param array $stateidArr Array of stateid from the doc_states table.
135 function setStateID($stateidArr) {
136 $this->stateidArr = $stateidArr;
140 * setDocGroupID - call this before getDocuments() if you want to limit to a specific doc_group.
142 * @param int $docgroupid The doc_group from the doc_groups table.
145 function setDocGroupID($docgroupid) {
146 $this->docgroupid = $docgroupid;
150 * setSort - call this before getDocuments() if you want to order the query.
152 * @param string $sort ASC or DESC : default ASC
155 function setSort($sort) {
156 if ( $sort != 'ASC' && $sort != 'DESC') {
164 * setOrder - call this before getDocuments() if you want to sort the query.
166 * @param array $columns Ordered Columns names: default title
169 function setOrder($columns = array('title')) {
170 // validate columns names
171 $localColumns = array();
172 foreach ($columns as $column) {
175 $localColumns[] = "title";
179 $localColumns[] = "createdate";
183 $localColumns[] = "updatedate";
187 $localColumns[] = "user_name";
191 $localColumns[] = "realname";
195 $localColumns[] = "email";
199 $localColumns[] = "group_id";
203 $localColumns[] = "docid";
207 $localColumns[] = "stateid";
211 $localColumns[] = "create_by";
215 $localColumns[] = "doc_group";
218 case "description": {
219 $localColumns[] = "description";
223 $localColumns[] = "filename";
227 $localColumns[] = "filetype";
231 $localColumns[] = "reserved";
234 case "reserved_by": {
235 $localColumns[] = "reserved_by";
239 $localColumns[] = "locked";
243 $localColumns[] = "locked_by";
247 $localColumns[] = "lockdate";
251 $localColumns[] = "state_name";
255 $localColumns[] = "group_name";
259 //unknown column: skip it
264 $this->order = $localColumns;
268 * setLimit - call this before getDocuments() if you want to limit number of documents retrieve.
269 * default value is 0 which means : no limit.
271 * @param int $limit The limit of documents
274 function setLimit($limit) {
275 $this->limit = $limit;
279 * setDocGroupState - call this before getDocuments() to setup correct permission settings for retrieve authorized documents.
280 * default value is 1 which means : no limit.
282 * @param array $state The array of valid state of documentgroups
285 function setDocGroupState($state) {
286 $this->docgroupstate = $state;
290 * setOffset - call this before getDocuments() if you want to move to the offset in the query used to retrieve documents.
291 * default value is 0 which means : no offset.
293 * @param int $offset The offset to use
296 function setOffset($offset) {
297 $this->offset = $offset;
301 * getDocuments - returns an array of Document objects.
303 * @param int $nocache Force to reset the cached data if any available.
304 * @internal param \no $integer cache : force reinit $this->Documents : default: cache is used
305 * @return array Document objects.
308 function &getDocuments($nocache = 0) {
309 if (!$this->Documents || $nocache) {
310 $this->getFromStorage();
315 // limit scope to the doc_group_id if any. Useful when you retrieve all documents in cache then filter
316 if ($this->docgroupid) {
317 $keys = array($this->docgroupid);
319 $keys = array_keys($this->Documents);
322 foreach ($keys as $key) {
323 if (!array_key_exists($key, $this->Documents)) continue; // Should not happen
325 $count = count($this->Documents[$key]);
326 for ($i=0; $i < $count; $i++) {
327 $doc =& $this->Documents[$key][$i];
328 if (in_array($doc->getStateID(), $this->stateidArr)) {
334 if (count($return) === 0) {
335 $this->setError(_('No Documents Found'));
343 private function ValidDocumentGroups() {
344 $this->validdocumentgroups = array();
345 // recursive query to find if documentgroups are visible thru the tree of documentgroups
346 $qpa = db_construct_qpa(false, 'WITH RECURSIVE doc_groups_parent(parent_doc_group, doc_group, stateid, group_id) AS
347 ( (SELECT parent_doc_group as anc, doc_group as desc, stateid as desc_stateid, group_id FROM doc_groups)
349 (select doc_groups_parent.parent_doc_group as anc, doc_groups.doc_group as desc, doc_groups_parent.stateid as desc_stateid, doc_groups.group_id
350 FROM doc_groups_parent, doc_groups
351 WHERE doc_groups_parent.doc_group = doc_groups.parent_doc_group ))
352 select max(stateid), doc_group from doc_groups_parent where group_id = $1 group by doc_group having max(stateid) <= $2',
353 array($this->Group->getID(), $this->docgroupstate));
354 $result = db_query_qpa($qpa);
356 $this->setError('getFromStorage:'.db_error());
359 while ($arr = db_fetch_array($result)) {
360 $this->validdocumentgroups[] = $arr['doc_group'];
365 * getFromStorage - Retrieve documents from storage (database for all informations).
366 * you can limit query to speed up: warning, once $this->documents is retrieve, it's cached.
368 * @return boolean success or not
371 private function getFromStorage() {
372 $this->Documents = array();
374 $qpa = db_construct_qpa(false, 'SELECT docdata_vw.* from docdata_vw, doc_groups
375 WHERE docdata_vw.doc_group = doc_groups.doc_group
376 and docdata_vw.group_id = $1 ', array($this->Group->getID()));
378 if ($this->docgroupid) {
379 $qpa = db_construct_qpa($qpa, 'AND docdata_vw.doc_group = $1 ', array($this->docgroupid));
381 $this->ValidDocumentGroups();
382 $qpa = db_construct_qpa($qpa, 'AND docdata_vw.doc_group = ANY ($1) ',array(db_int_array_to_any_clause($this->validdocumentgroups)));
385 $qpa = db_construct_qpa($qpa, 'AND docdata_vw.stateid = ANY ($1) ', array(db_int_array_to_any_clause($this->stateidArr)));
387 $qpa = db_construct_qpa($qpa, 'ORDER BY ');
388 for ($i=0; $i<count($this->order); $i++) {
389 $qpa = db_construct_qpa($qpa, $this->order[$i]);
390 if (count($this->order) != $i + 1) {
391 $qpa = db_construct_qpa($qpa, ',');
393 $qpa = db_construct_qpa($qpa, ' ');
397 $qpa = db_construct_qpa($qpa, $this->sort);
399 $result = db_query_qpa($qpa, $this->limit, $this->offset);
401 $this->setError('getFromStorage:'.db_error());
405 while ($arr = db_fetch_array($result)) {
406 $doc_group_id = $arr['doc_group'];
407 if (!is_array(@$this->Documents[$doc_group_id])) {
408 $this->Documents[$doc_group_id] = array();
410 $this->Documents[$doc_group_id][] = new Document($this->Group, $arr['docid'], $arr);
418 // c-file-style: "bsd"