3 * FusionForge document manager
5 * Copyright 2000, Quentin Cregan/Sourceforge
6 * Copyright 2002-2003, Tim Perdue/GForge, LLC
7 * Copyright 2009, Roland Mas
8 * http://fusionforge.org
10 * This file is part of FusionForge.
12 * FusionForge is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published
14 * by the Free Software Foundation; either version 2 of the License,
15 * or (at your option) any later version.
17 * FusionForge is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with FusionForge; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
28 require_once $gfcommon.'include/Error.class.php';
29 require_once $gfcommon.'docman/Document.class.php';
31 class DocumentFactory extends Error {
41 * The Documents dictionary.
43 * @var array Contains doc_group_id as key and the array of documents associated to that id.
49 var $sort='group_name, title';
54 * @param object The Group object to which this DocumentFactory is associated.
55 * @return boolean success.
57 function DocumentFactory(&$Group) {
59 if (!$Group || !is_object($Group)) {
60 $this->setError(_('ProjectGroup:: No Valid Group Object'));
64 if ($Group->isError()) {
65 $this->setError('ProjectGroup:: '.$Group->getErrorMessage());
69 $this->Group =& $Group;
74 * getGroup - get the Group object this DocumentFactory is associated with.
76 * @return object the Group object.
78 function &getGroup() {
83 * setStateID - call this before getDocuments() if you want to limit to a specific state.
85 * @param int The stateid from the doc_states table.
87 function setStateID($stateid) {
88 $this->stateid=$stateid;
92 * setDocGroupID - call this before getDocuments() if you want to limit to a specific doc_group.
94 * @param int The doc_group from the doc_groups table.
96 function setDocGroupID($docgroupid) {
97 $this->docgroupid=$docgroupid;
101 * setSort - call this before getDocuments() if you want to control the sorting.
103 * @param string The name of the field to sort on.
105 function setSort($sort) {
110 * getDocuments - returns an array of Document objects.
112 * @return array Document objects.
114 function &getDocuments() {
115 if (!$this->Documents) {
120 // If the document group is specified, we should only check that group in
121 // the Documents array. If not, we should check ALL the groups.
122 if ($this->docgroupid) {
123 $keys = array($this->docgroupid);
125 $keys = array_keys($this->Documents);
128 foreach ($keys as $key) {
129 if (!array_key_exists($key, $this->Documents)) continue; // Should not happen
130 $count = count($this->Documents[$key]);
132 for ($i=0; $i < $count; $i++) {
133 $valid = true; // do we need to return this document?
134 $doc =& $this->Documents[$key][$i];
136 if (!$this->stateid) {
137 if (session_loggedin()) {
138 $perm =& $this->Group->getPermission ();
139 if (!$perm || !is_object($perm) || !$perm->isMember()) {
140 if ($doc->getStateID() != 1) { // non-active document?
144 if ($doc->getStateID() != 1 && /* not active */
145 $doc->getStateID() != 4 && /* not hidden */
146 $doc->getStateID() != 5) { /* not private */
151 if ($doc->getStateID() != 1) { // non-active document?
156 if ($this->stateid != "ALL" && $doc->getStateID() != $this->stateid) {
167 if (count($return) == 0) {
168 $this->setError(_('No Documents Found'));
177 * getFromDB - Retrieve documents from database.
179 function getFromDB() {
180 $this->Documents = array();
181 $result = db_query_params ('SELECT * FROM docdata_vw ORDER BY title',
184 exit_error(db_error(),'docman');
187 while ($arr = db_fetch_array($result)) {
188 $doc_group_id = $arr['doc_group'];
189 if (!is_array(@$this->Documents[$doc_group_id])) {
190 $this->Documents[$doc_group_id] = array();
193 $this->Documents[$doc_group_id][] = new Document($this->Group, $arr['docid'], $arr);
198 * getStates - Return an array of states that have documents associated to them
200 function getUsedStates() {
201 $result = db_query_params ('SELECT DISTINCT doc_states.stateid,doc_states.name
202 FROM doc_states,doc_data
203 WHERE doc_data.stateid=doc_states.stateid
204 ORDER BY doc_states.name ASC',
207 exit_error(db_error(),'docman');
211 while ($arr = db_fetch_array($result)) {
222 // c-file-style: "bsd"