3 * FusionForge document 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, 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/Error.class.php';
30 require_once $gfcommon.'docman/Document.class.php';
32 class DocumentFactory extends Error {
42 * The Documents dictionary.
44 * @var array Contains doc_group_id as key and the array of documents associated to that id.
50 * @var integer Contains the stateid to limit return documents in getDocuments.
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
84 * @param object The Group object to which this DocumentFactory is associated.
85 * @return boolean success.
88 function __construct(&$Group) {
90 if (!$Group || !is_object($Group)) {
91 $this->setError(_('ProjectGroup:: No Valid Group Object'));
95 if ($Group->isError()) {
96 $this->setError('ProjectGroup:: '.$Group->getErrorMessage());
100 $this->Group =& $Group;
104 * getGroup - get the Group object this DocumentFactory is associated with.
106 * @return object the Group object.
108 function &getGroup() {
113 * setStateID - call this before getDocuments() if you want to limit to a specific state.
115 * @param integer The stateid from the doc_states table.
118 function setStateID($stateid) {
119 $this->stateid = $stateid;
123 * setDocGroupID - call this before getDocuments() if you want to limit to a specific doc_group.
125 * @param integer The doc_group from the doc_groups table.
128 function setDocGroupID($docgroupid) {
129 $this->docgroupid = $docgroupid;
133 * setSort - call this before getDocuments() if you want to order the query.
135 * @param string ASC or DESC : default ASC
138 function setSort($sort) {
139 if ( $sort != 'ASC' && $sort != 'DESC') {
147 * setOrder - call this before getDocuments() if you want to sort the query.
149 * @param array Ordered Columns names: default title
152 function setOrder($columns = array('title')) {
153 // validate columns names
154 $localColumns = array();
155 foreach ($columns as $column) {
158 $localColumns[] = "title";
162 $localColumns[] = "createdate";
166 $localColumns[] = "updatedate";
170 $localColumns[] = "user_name";
174 $localColumns[] = "realname";
178 $localColumns[] = "email";
182 $localColumns[] = "group_id";
186 $localColumns[] = "docid";
190 $localColumns[] = "stateid";
194 $localColumns[] = "create_by";
198 $localColumns[] = "doc_group";
201 case "description": {
202 $localColumns[] = "description";
206 $localColumns[] = "filename";
210 $localColumns[] = "filetype";
214 $localColumns[] = "reserved";
217 case "reserved_by": {
218 $localColumns[] = "reserved_by";
222 $localColumns[] = "locked";
226 $localColumns[] = "locked_by";
230 $localColumns[] = "lockdate";
234 $localColumns[] = "state_name";
238 $localColumns[] = "group_name";
242 //unknown column: skip it
247 $this->order = $localColumns;
251 * setLimit - call this before getDocuments() if you want to limit number of documents retrieve.
252 * default value is 0 which means : no limit.
254 * @param int The limit of documents
257 function setLimit($limit) {
258 $this->limit = $limit;
262 * getDocuments - returns an array of Document objects.
264 * @param integer no cache : force reinit $this->Documents : default: cache is used
265 * @return array Document objects.
268 function &getDocuments($nocache = 0) {
269 if (!$this->Documents || $nocache) {
270 $this->getFromStorage();
275 // limit scope to the doc_group_id if any. Useful when you retrieve all documents
276 if ($this->docgroupid) {
277 $keys = array($this->docgroupid);
279 $keys = array_keys($this->Documents);
282 foreach ($keys as $key) {
283 if (!array_key_exists($key, $this->Documents)) continue; // Should not happen
285 $count = count($this->Documents[$key]);
287 for ($i=0; $i < $count; $i++) {
288 $valid = true; // do we need to return this document?
289 $doc =& $this->Documents[$key][$i];
291 if (!$this->stateid) {
292 $perm =& $this->Group->getPermission();
293 if (!$perm || !is_object($perm)) {
294 if ($doc->getStateID() != 1) {
297 if ($perm->isEditor()) {
302 if ($this->stateid != "ALL" && $doc->getStateID() != $this->stateid) {
313 if (count($return) === 0) {
314 $this->setError(_('No Documents Found'));
323 * getFromStorage - Retrieve documents from storage (database for all informations).
324 * you can limit query to speed up: warning, once $this->documents is retrieve, it's cached.
326 * @return boolean success or not
329 private function getFromStorage() {
330 $this->Documents = array();
331 $qpa = db_construct_qpa();
332 $qpa = db_construct_qpa($qpa, 'SELECT * FROM docdata_vw WHERE group_id = $1 ',
333 array($this->Group->getID()));
335 if ($this->docgroupid) {
336 $qpa = db_construct_qpa($qpa, 'AND doc_group = $1 ', array($this->docgroupid));
339 $qpa = db_construct_qpa($qpa, 'ORDER BY ');
340 for ($i=0; $i<count($this->order); $i++) {
341 $qpa = db_construct_qpa($qpa, $this->order[$i]);
342 if (count($this->order) != $i + 1) {
343 $qpa = db_construct_qpa($qpa, ',');
345 $qpa = db_construct_qpa($qpa, ' ');
349 $qpa = db_construct_qpa($qpa, $this->sort);
351 if ($this->limit !== 0 ) {
352 $qpa = db_construct_qpa($qpa, ' LIMIT $1', array($this->limit));
355 $result = db_query_qpa($qpa);
357 $this->setError('getFromStorage::'.db_error());
361 while ($arr = db_fetch_array($result)) {
362 $doc_group_id = $arr['doc_group'];
363 if (!is_array(@$this->Documents[$doc_group_id])) {
364 $this->Documents[$doc_group_id] = array();
366 $this->Documents[$doc_group_id][] = new Document($this->Group, $arr['docid'], $arr);
374 // c-file-style: "bsd"