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.
13 * FusionForge is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published
15 * by the Free Software Foundation; either version 2 of the License,
16 * or (at your option) any later version.
18 * FusionForge is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with FusionForge; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
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 int Contains the stateid to limit return documents in getDocuments.
55 * The doc_group_id limit
56 * @var int 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 int 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 DocumentFactory(&$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;
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 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 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 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 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 The limit of documents
258 function setLimit($limit) {
259 $this->limit = $limit;
263 * getDocuments - returns an array of Document objects.
265 * @param int no cache : force reinit $this->Documents : default: cache is used
266 * @return array Document objects.
269 function &getDocuments($nocache = 0) {
270 if (!$this->Documents || $nocache) {
271 $this->__getFromStorage();
276 // limit scope to the doc_group_id if any. Useful when you retrieve all documents
277 if ($this->docgroupid) {
278 $keys = array($this->docgroupid);
280 $keys = array_keys($this->Documents);
283 foreach ($keys as $key) {
284 if (!array_key_exists($key, $this->Documents)) continue; // Should not happen
286 $count = count($this->Documents[$key]);
288 for ($i=0; $i < $count; $i++) {
289 $valid = true; // do we need to return this document?
290 $doc =& $this->Documents[$key][$i];
292 if (!$this->stateid) {
293 if (session_loggedin()) {
294 $perm =& $this->Group->getPermission();
295 if (!$perm || !is_object($perm) || !$perm->isMember()) {
296 if ($doc->getStateID() != 1) { // non-active document?
300 if ($doc->getStateID() != 1 && /* not active */
301 $doc->getStateID() != 4 && /* not hidden */
302 $doc->getStateID() != 5) { /* not private */
307 if ($doc->getStateID() != 1) { // non-active document?
312 if ($this->stateid != "ALL" && $doc->getStateID() != $this->stateid) {
323 if (count($return) === 0) {
324 $this->setError(_('No Documents Found'));
333 * __getFromStorage - Retrieve documents from storage API
335 * @return boolean success or not
338 private function __getFromStorage() {
340 switch ($this->Group->getStorageAPI()) {
342 if ($this->__getFromDB())
347 $this->setError(_('No Storage API Found'));
355 * __getFromDB - Retrieve documents from database.
356 * you can limit query to speed up: warning, once $this->documents is retrieve, it's cached.
358 * @return boolean success or not
361 private function __getFromDB() {
362 $this->Documents = array();
363 $qpa = db_construct_qpa();
364 $qpa = db_construct_qpa($qpa, 'SELECT * FROM docdata_vw WHERE group_id = $1 ',
365 array($this->Group->getID()));
367 if ($this->docgroupid) {
368 $qpa = db_construct_qpa($qpa, 'AND doc_group = $1 ', array($this->docgroupid));
371 $qpa = db_construct_qpa($qpa, 'ORDER BY ');
372 for ($i=0; $i<count($this->order); $i++) {
373 $qpa = db_construct_qpa($qpa, $this->order[$i]);
374 if (count($this->order) != $i + 1) {
375 $qpa = db_construct_qpa($qpa, ',');
377 $qpa = db_construct_qpa($qpa, ' ');
381 $qpa = db_construct_qpa($qpa, $this->sort);
383 if ($this->limit !== 0 ) {
384 $qpa = db_construct_qpa($qpa, ' LIMIT $1', array($this->limit));
387 $result = db_query_qpa($qpa);
389 $this->setError('getFromDB::'.db_error());
393 while ($arr = db_fetch_array($result)) {
394 $doc_group_id = $arr['doc_group'];
395 if (!is_array(@$this->Documents[$doc_group_id])) {
396 $this->Documents[$doc_group_id] = array();
398 $this->Documents[$doc_group_id][] = new Document($this->Group, $arr['docid'], $arr);
404 * getStates - Return an array of states that have documents associated to them
406 function getUsedStates() {
407 $result = db_query_params('SELECT DISTINCT doc_states.stateid,doc_states.name
408 FROM doc_states,doc_data
409 WHERE doc_data.stateid=doc_states.stateid
410 ORDER BY doc_states.name ASC',
413 exit_error(db_error(), 'docman');
417 while ($arr = db_fetch_array($result)) {
428 // c-file-style: "bsd"