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-2014, 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 FFError {
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
85 * @internal param \The $object Group object to which this DocumentFactory is associated.
86 * @return \DocumentFactory
89 function __construct(&$Group) {
91 if (!$Group || !is_object($Group)) {
92 $this->setError(_('No Valid Group Object'));
96 if ($Group->isError()) {
97 $this->setError('ProjectGroup: '.$Group->getErrorMessage());
101 $this->Group =& $Group;
105 * getGroup - get the Group object this DocumentFactory is associated with.
107 * @return object the Group object.
109 function &getGroup() {
114 * setStateID - call this before getDocuments() if you want to limit to a specific state.
116 * @param int $stateid The stateid from the doc_states table.
119 function setStateID($stateid) {
120 $this->stateid = $stateid;
124 * setDocGroupID - call this before getDocuments() if you want to limit to a specific doc_group.
126 * @param int $docgroupid The doc_group from the doc_groups table.
129 function setDocGroupID($docgroupid) {
130 $this->docgroupid = $docgroupid;
134 * setSort - call this before getDocuments() if you want to order the query.
136 * @param string $sort ASC or DESC : default ASC
139 function setSort($sort) {
140 if ( $sort != 'ASC' && $sort != 'DESC') {
148 * setOrder - call this before getDocuments() if you want to sort the query.
150 * @param array $columns Ordered Columns names: default title
153 function setOrder($columns = array('title')) {
154 // validate columns names
155 $localColumns = array();
156 foreach ($columns as $column) {
159 $localColumns[] = "title";
163 $localColumns[] = "createdate";
167 $localColumns[] = "updatedate";
171 $localColumns[] = "user_name";
175 $localColumns[] = "realname";
179 $localColumns[] = "email";
183 $localColumns[] = "group_id";
187 $localColumns[] = "docid";
191 $localColumns[] = "stateid";
195 $localColumns[] = "create_by";
199 $localColumns[] = "doc_group";
202 case "description": {
203 $localColumns[] = "description";
207 $localColumns[] = "filename";
211 $localColumns[] = "filetype";
215 $localColumns[] = "reserved";
218 case "reserved_by": {
219 $localColumns[] = "reserved_by";
223 $localColumns[] = "locked";
227 $localColumns[] = "locked_by";
231 $localColumns[] = "lockdate";
235 $localColumns[] = "state_name";
239 $localColumns[] = "group_name";
243 //unknown column: skip it
248 $this->order = $localColumns;
252 * setLimit - call this before getDocuments() if you want to limit number of documents retrieve.
253 * default value is 0 which means : no limit.
255 * @param int $limit The limit of documents
258 function setLimit($limit) {
259 $this->limit = $limit;
263 * getDocuments - returns an array of Document objects.
265 * @param int $nocache Force to reset the cached data if any available.
266 * @internal param \no $integer cache : force reinit $this->Documents : default: cache is used
267 * @return array Document objects.
270 function &getDocuments($nocache = 0) {
271 if (!$this->Documents || $nocache) {
272 $this->getFromStorage();
277 // limit scope to the doc_group_id if any. Useful when you retrieve all documents
278 if ($this->docgroupid) {
279 $keys = array($this->docgroupid);
281 $keys = array_keys($this->Documents);
284 foreach ($keys as $key) {
285 if (!array_key_exists($key, $this->Documents)) continue; // Should not happen
287 $count = count($this->Documents[$key]);
289 for ($i=0; $i < $count; $i++) {
290 $valid = true; // do we need to return this document?
291 $doc =& $this->Documents[$key][$i];
293 if (!$this->stateid) {
294 $perm =& $this->Group->getPermission();
295 if (!$perm || !is_object($perm)) {
296 if ($doc->getStateID() != 1) {
299 if ($perm->isDocEditor()) {
304 if ($this->stateid != "ALL" && $doc->getStateID() != $this->stateid) {
315 if (count($return) === 0) {
316 $this->setError(_('No Documents Found'));
325 * getFromStorage - Retrieve documents from storage (database for all informations).
326 * you can limit query to speed up: warning, once $this->documents is retrieve, it's cached.
328 * @return boolean success or not
331 private function getFromStorage() {
332 $this->Documents = array();
333 $qpa = db_construct_qpa();
334 $qpa = db_construct_qpa($qpa, 'SELECT * FROM docdata_vw WHERE group_id = $1 ',
335 array($this->Group->getID()));
337 if ($this->docgroupid) {
338 $qpa = db_construct_qpa($qpa, 'AND doc_group = $1 ', array($this->docgroupid));
341 $qpa = db_construct_qpa($qpa, 'ORDER BY ');
342 for ($i=0; $i<count($this->order); $i++) {
343 $qpa = db_construct_qpa($qpa, $this->order[$i]);
344 if (count($this->order) != $i + 1) {
345 $qpa = db_construct_qpa($qpa, ',');
347 $qpa = db_construct_qpa($qpa, ' ');
351 $qpa = db_construct_qpa($qpa, $this->sort);
353 if ($this->limit !== 0 ) {
354 $qpa = db_construct_qpa($qpa, ' LIMIT $1', array($this->limit));
357 $result = db_query_qpa($qpa);
359 $this->setError('getFromStorage:'.db_error());
363 while ($arr = db_fetch_array($result)) {
364 $doc_group_id = $arr['doc_group'];
365 if (!is_array(@$this->Documents[$doc_group_id])) {
366 $this->Documents[$doc_group_id] = array();
368 $this->Documents[$doc_group_id][] = new Document($this->Group, $arr['docid'], $arr);
376 // c-file-style: "bsd"