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-2017,2021, 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';
31 require_once $gfcommon.'docman/DocumentVersionFactory.class.php';
33 class DocumentFactory extends FFError {
43 * The Documents dictionary.
45 * @var array Contains doc_group_id as key and the array of documents associated to that id.
50 * The stateid Array limit
51 * @var array Contains the different stateid to limit return documents in getDocuments.
53 var $stateidArr = array();
56 * The doc_group_id limit
57 * @var int Contains the doc_group id to limit return documents in getDocuments.
63 * @var string Contains the order to return documents in getDocuments.
64 * Default value is ASC
70 * @var array Contains the order of columns to sort before return documents in getDocuments.
71 * Default value is title order
73 var $order = array('title');
77 * @var int Contains the limit of documents retrieve by getDocuments.
78 * Default value is 0 which means NO LIMIT
84 * @var int Contains the valid state of documentgroups to retrieve documents using getDocuments.
85 * Default value is 1 which means public
87 var $docgroupstate = 1;
91 * @var int Contains the offset of the query used to retrive documents using getDocuments.
92 * Default value is 0 which means NO OFFSET
96 var $validdocumentgroups = array();
101 function __construct(&$Group) {
102 parent::__construct();
103 if (!$Group || !is_object($Group)) {
104 $this->setError(_('Invalid Project'));
108 if ($Group->isError()) {
109 $this->setError('ProjectGroup: '.$Group->getErrorMessage());
113 $this->Group =& $Group;
117 * getGroup - get the Group object this DocumentFactory is associated with.
119 * @return object the Group object.
121 function &getGroup() {
126 * setStateID - call this before getDocuments() if you want to limit to some specific states.
128 * @param array $stateidArr Array of stateid from the doc_states table.
131 function setStateID($stateidArr) {
132 $this->stateidArr = $stateidArr;
136 * setDocGroupID - call this before getDocuments() if you want to limit to a specific doc_group.
138 * @param int $docgroupid The doc_group from the doc_groups table.
141 function setDocGroupID($docgroupid) {
142 $this->docgroupid = $docgroupid;
146 * setSort - call this before getDocuments() if you want to order the query.
148 * @param string $sort ASC or DESC : default ASC
151 function setSort($sort) {
152 if ( $sort != 'ASC' && $sort != 'DESC') {
160 * setOrder - call this before getDocuments() if you want to sort the query.
162 * @param array $columns Ordered Columns names: default title
165 function setOrder($columns = array('title')) {
166 // validate columns names
167 $localColumns = array();
168 foreach ($columns as $column) {
171 $localColumns[] = "title";
175 $localColumns[] = "createdate";
179 $localColumns[] = "updatedate";
183 $localColumns[] = "user_name";
187 $localColumns[] = "realname";
191 $localColumns[] = "email";
195 $localColumns[] = "group_id";
199 $localColumns[] = "docid";
203 $localColumns[] = "stateid";
207 $localColumns[] = "create_by";
211 $localColumns[] = "doc_group";
214 case "description": {
215 $localColumns[] = "description";
219 $localColumns[] = "filename";
223 $localColumns[] = "filetype";
227 $localColumns[] = "reserved";
230 case "reserved_by": {
231 $localColumns[] = "reserved_by";
235 $localColumns[] = "locked";
239 $localColumns[] = "locked_by";
243 $localColumns[] = "lockdate";
247 $localColumns[] = "state_name";
251 $localColumns[] = "group_name";
255 //unknown column: skip it
260 $this->order = $localColumns;
264 * setLimit - call this before getDocuments() if you want to limit number of documents retrieve.
265 * default value is 0 which means : no limit.
267 * @param int $limit The limit of documents
270 function setLimit($limit) {
271 $this->limit = $limit;
275 * setDocGroupState - call this before getDocuments() to setup correct permission settings for retrieve authorized documents.
276 * default value is 1 which means : public.
278 * @param array $state The array of valid state of documentgroups
281 function setDocGroupState($state) {
282 $this->docgroupstate = $state;
286 * setOffset - call this before getDocuments() if you want to move to the offset in the query used to retrieve documents.
287 * default value is 0 which means : no offset.
289 * @param int $offset The offset to use
292 function setOffset($offset) {
293 $this->offset = $offset;
297 * getDocuments - returns an array of Document objects.
299 * @param int $nocache Force to reset the cached data if any available.
300 * @return array Document objects.
303 function getDocuments($nocache = 0) {
304 if (!$this->Documents || $nocache) {
305 $this->getFromStorage();
310 // limit scope to the doc_group_id if any. Useful when you retrieve all documents in cache then filter
311 if ($this->docgroupid) {
312 $keys = array($this->docgroupid);
314 $keys = array_keys($this->Documents);
317 foreach ($keys as $key) {
318 if (!array_key_exists($key, $this->Documents)) {
319 continue; // Should not happen
321 $count = count($this->Documents[$key]);
322 for ($i=0; $i < $count; $i++) {
323 $doc =& $this->Documents[$key][$i];
324 if (in_array($doc->getStateID(), $this->stateidArr)) {
330 if (empty($return)) {
331 $this->setError(_('No Documents Found'));
338 function getDocumentsWithVersions() {
340 $docs = $this->getDocuments(1);
341 if (is_array($docs)) {
342 foreach ($docs as $doc) {
343 $dvf = new DocumentVersionFactory($doc);
345 $docArr = (array)$doc;
346 unset($docArr['Group']);
347 $docArr['versions'] = $dvf->getVersions();
348 $docArr['monitor'] = $doc->getMonitorIds();
349 $serialIDs = $dvf->getSerialIDs();
350 if (forge_get_config('use_docman_review')) {
351 $drf = new DocumentReviewFactory($doc);
352 $docArr['reviews'] = $drf->getReviews($serialIDs);
361 private function ValidDocumentGroups() {
362 $this->validdocumentgroups = array();
363 // recursive query to find if documentgroups are visible thru the tree of documentgroups
364 $qpa = db_construct_qpa(false, 'WITH RECURSIVE doc_groups_parent(parent_doc_group, doc_group, stateid, group_id) AS
365 ( (SELECT parent_doc_group as anc, doc_group as desc, stateid as desc_stateid, group_id FROM doc_groups)
367 (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
368 FROM doc_groups_parent, doc_groups
369 WHERE doc_groups_parent.doc_group = doc_groups.parent_doc_group ))
370 select max(stateid), doc_group from doc_groups_parent where group_id = $1 group by doc_group having max(stateid) <= $2',
371 array($this->Group->getID(), $this->docgroupstate));
372 $result = db_query_qpa($qpa);
374 $this->setError('ValidDocumentGroups:'.db_error());
377 while ($arr = db_fetch_array($result)) {
378 $this->validdocumentgroups[] = $arr['doc_group'];
383 * getFromStorage - Retrieve documents from storage (database for all informations).
384 * you can limit query to speed up: warning, once $this->documents is retrieve, it's cached.
386 * @return bool success or not
388 private function getFromStorage() {
389 $this->Documents = array();
391 $qpa = db_construct_qpa(false, 'SELECT docdata_vw.* from docdata_vw, doc_groups
392 WHERE docdata_vw.doc_group = doc_groups.doc_group
393 and docdata_vw.group_id = $1 ', array($this->Group->getID()));
395 if ($this->docgroupid) {
396 $qpa = db_construct_qpa($qpa, 'AND docdata_vw.doc_group = $1 ', array($this->docgroupid));
398 $this->ValidDocumentGroups();
399 $qpa = db_construct_qpa($qpa, 'AND docdata_vw.doc_group = ANY ($1) ',array(db_int_array_to_any_clause($this->validdocumentgroups)));
402 $qpa = db_construct_qpa($qpa, 'AND docdata_vw.stateid = ANY ($1) ', array(db_int_array_to_any_clause($this->stateidArr)));
404 $qpa = db_construct_qpa($qpa, 'ORDER BY ');
405 for ($i=0; $i<count($this->order); $i++) {
406 $qpa = db_construct_qpa($qpa, $this->order[$i]);
407 if (count($this->order) != $i + 1) {
408 $qpa = db_construct_qpa($qpa, ',');
410 $qpa = db_construct_qpa($qpa, ' ');
414 $qpa = db_construct_qpa($qpa, $this->sort);
416 $result = db_query_qpa($qpa, $this->limit, $this->offset);
418 $this->setError('getFromStorage:'.db_error());
422 while ($arr = db_fetch_array($result)) {
423 $doc_group_id = $arr['doc_group'];
424 if (isset($this->Documents[$doc_group_id]) && !is_array($this->Documents[$doc_group_id])) {
425 $this->Documents[$doc_group_id] = array();
427 $this->Documents[$doc_group_id][] = new Document($this->Group, $arr['docid'], $arr);
435 // c-file-style: "bsd"