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 * 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';
28 require_once $gfcommon.'docman/Document.class.php';
30 class DocumentFactory extends Error {
40 * The Documents dictionary.
42 * @var array Contains doc_group_id as key and the array of documents associated to that id.
48 * @var integer Contains the stateid to limit return documents in getDocuments.
53 * The doc_group_id limit
54 * @var integer Contains the doc_group id to limit return documents in getDocuments.
60 * @var string Contains the order to return documents in getDocuments.
61 * Default value is ASC
67 * @var array Contains the order of columns to sort before return documents in getDocuments.
68 * Default value is title order
70 var $order = array('title');
74 * @var integer Contains the limit of documents retrieve by getDocuments.
75 * Default value is 0 which means NO LIMIT
82 * @param object The Group object to which this DocumentFactory is associated.
83 * @return boolean success.
86 function DocumentFactory(&$Group) {
88 if (!$Group || !is_object($Group)) {
89 $this->setError(_('ProjectGroup:: No Valid Group Object'));
93 if ($Group->isError()) {
94 $this->setError('ProjectGroup:: '.$Group->getErrorMessage());
98 $this->Group =& $Group;
103 * getGroup - get the Group object this DocumentFactory is associated with.
105 * @return object the Group object.
107 function &getGroup() {
112 * setStateID - call this before getDocuments() if you want to limit to a specific state.
114 * @param integer The stateid from the doc_states table.
117 function setStateID($stateid) {
118 $this->stateid = $stateid;
122 * setDocGroupID - call this before getDocuments() if you want to limit to a specific doc_group.
124 * @param integer The doc_group from the doc_groups table.
127 function setDocGroupID($docgroupid) {
128 $this->docgroupid = $docgroupid;
132 * setSort - call this before getDocuments() if you want to order the query.
134 * @param string ASC or DESC : default ASC
137 function setSort($sort) {
138 if ( $sort != 'ASC' && $sort != 'DESC') {
146 * setOrder - call this before getDocuments() if you want to sort the query.
148 * @param array Ordered Columns names: default title
151 function setOrder($columns = array('title')) {
152 // validate columns names
153 $localColumns = array();
154 foreach ($columns as $column) {
157 $localColumns[] = "title";
161 $localColumns[] = "createdate";
165 $localColumns[] = "updatedate";
169 $localColumns[] = "user_name";
173 $localColumns[] = "realname";
177 $localColumns[] = "email";
181 $localColumns[] = "group_id";
185 $localColumns[] = "docid";
189 $localColumns[] = "stateid";
193 $localColumns[] = "create_by";
197 $localColumns[] = "doc_group";
200 case "description": {
201 $localColumns[] = "description";
205 $localColumns[] = "filename";
209 $localColumns[] = "filetype";
213 $localColumns[] = "reserved";
216 case "reserved_by": {
217 $localColumns[] = "reserved_by";
221 $localColumns[] = "locked";
225 $localColumns[] = "locked_by";
229 $localColumns[] = "lockdate";
233 $localColumns[] = "state_name";
237 $localColumns[] = "group_name";
241 //unknown column: skip it
246 $this->order = $localColumns;
250 * setLimit - call this before getDocuments() if you want to limit number of documents retrieve.
251 * default value is 0 which means : no limit.
253 * @param int The limit of documents
256 function setLimit($limit) {
257 $this->limit = $limit;
261 * getDocuments - returns an array of Document objects.
263 * @param integer no cache : force reinit $this->Documents : default: cache is used
264 * @return array Document objects.
267 function &getDocuments($nocache = 0) {
268 if (!$this->Documents || $nocache) {
269 $this->__getFromStorage();
274 // limit scope to the doc_group_id if any. Useful when you retrieve all documents
275 if ($this->docgroupid) {
276 $keys = array($this->docgroupid);
278 $keys = array_keys($this->Documents);
281 foreach ($keys as $key) {
282 if (!array_key_exists($key, $this->Documents)) continue; // Should not happen
284 $count = count($this->Documents[$key]);
286 for ($i=0; $i < $count; $i++) {
287 $valid = true; // do we need to return this document?
288 $doc =& $this->Documents[$key][$i];
290 if (!$this->stateid) {
291 $perm =& $this->Group->getPermission();
292 if (!$perm || !is_object($perm)) {
293 if ($doc->getStateID() != 1) {
296 if ($perm->isEditor()) {
301 if ($this->stateid != "ALL" && $doc->getStateID() != $this->stateid) {
312 if (count($return) === 0) {
313 $this->setError(_('No Documents Found'));
322 * __getFromStorage - Retrieve documents from storage API
324 * @return boolean success or not
327 private function __getFromStorage() {
329 switch ($this->Group->getStorageAPI()) {
331 if ($this->__getFromDB())
336 $this->setError(_('No Storage API Found'));
344 * __getFromDB - Retrieve documents from database.
345 * you can limit query to speed up: warning, once $this->documents is retrieve, it's cached.
347 * @return boolean success or not
350 private function __getFromDB() {
351 $this->Documents = array();
352 $qpa = db_construct_qpa();
353 $qpa = db_construct_qpa($qpa, 'SELECT * FROM docdata_vw WHERE group_id = $1 ',
354 array($this->Group->getID()));
356 if ($this->docgroupid) {
357 $qpa = db_construct_qpa($qpa, 'AND doc_group = $1 ', array($this->docgroupid));
360 $qpa = db_construct_qpa($qpa, 'ORDER BY ');
361 for ($i=0; $i<count($this->order); $i++) {
362 $qpa = db_construct_qpa($qpa, $this->order[$i]);
363 if (count($this->order) != $i + 1) {
364 $qpa = db_construct_qpa($qpa, ',');
366 $qpa = db_construct_qpa($qpa, ' ');
370 $qpa = db_construct_qpa($qpa, $this->sort);
372 if ($this->limit !== 0 ) {
373 $qpa = db_construct_qpa($qpa, ' LIMIT $1', array($this->limit));
376 $result = db_query_qpa($qpa);
378 $this->setError('getFromDB::'.db_error());
382 while ($arr = db_fetch_array($result)) {
383 $doc_group_id = $arr['doc_group'];
384 if (!is_array(@$this->Documents[$doc_group_id])) {
385 $this->Documents[$doc_group_id] = array();
387 $this->Documents[$doc_group_id][] = new Document($this->Group, $arr['docid'], $arr);
395 // c-file-style: "bsd"