3 * FusionForge document manager
5 * Copyright 2000, Quentin Cregan/Sourceforge
6 * Copyright 2002-2003, Tim Perdue/GForge, LLC
7 * Copyright 2009, Roland Mas
9 * This file is part of FusionForge.
11 * FusionForge is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published
13 * by the Free Software Foundation; either version 2 of the License,
14 * or (at your option) any later version.
16 * FusionForge is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with FusionForge; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
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 $sort='group_name, title';
53 * @param object The Group object to which this DocumentFactory is associated.
54 * @return boolean success.
56 function DocumentFactory(&$Group) {
58 if (!$Group || !is_object($Group)) {
59 $this->setError('ProjectGroup:: No Valid Group Object');
62 if ($Group->isError()) {
63 $this->setError('ProjectGroup:: '.$Group->getErrorMessage());
66 $this->Group =& $Group;
72 * getGroup - get the Group object this DocumentFactory is associated with.
74 * @return object the Group object.
76 function &getGroup() {
81 * setStateID - call this before getDocuments() if you want to limit to a specific state.
83 * @param int The stateid from the doc_states table.
85 function setStateID($stateid) {
86 $this->stateid=$stateid;
90 * setDocGroupID - call this before getDocuments() if you want to limit to a specific doc_group.
92 * @param int The doc_group from the doc_groups table.
94 function setDocGroupID($docgroupid) {
95 $this->docgroupid=$docgroupid;
99 * setSort - call this before getDocuments() if you want to control the sorting.
101 * @param string The name of the field to sort on.
103 function setSort($sort) {
108 * getDocuments - returns an array of Document objects.
110 * @return array Document objects.
112 function &getDocuments() {
113 if (!$this->Documents) {
118 // If the document group is specified, we should only check that group in
119 // the Documents array. If not, we should check ALL the groups.
120 if ($this->docgroupid) {
121 $keys = array($this->docgroupid);
123 $keys = array_keys($this->Documents);
126 foreach ($keys as $key) {
127 if (!array_key_exists($key, $this->Documents)) continue; // Should not happen
128 $count = count($this->Documents[$key]);
130 for ($i=0; $i < $count; $i++) {
131 $valid = true; // do we need to return this document?
132 $doc =& $this->Documents[$key][$i];
134 if (!$this->stateid) {
135 if (session_loggedin()) {
136 $perm =& $this->Group->getPermission ();
137 if (!$perm || !is_object($perm) || !$perm->isMember()) {
138 if ($doc->getStateID() != 1) { // non-active document?
142 if ($doc->getStateID() != 1 && /* not active */
143 $doc->getStateID() != 4 && /* not hidden */
144 $doc->getStateID() != 5) { /* not private */
149 if ($doc->getStateID() != 1) { // non-active document?
154 if ($this->stateid != "ALL" && $doc->getStateID() != $this->stateid) {
159 if ($this->languageid && $doc->getLanguageID() != $this->languageid) {
170 if (count($return) == 0) {
171 $this->setError(_('No Documents Found'));
180 * getFromDB - Retrieve documents from database.
182 function getFromDB() {
183 $this->Documents = array();
184 $result = db_query_params ('SELECT * FROM docdata_vw ORDER BY title',
187 exit_error('Error', db_error());
190 while ($arr =& db_fetch_array($result)) {
191 $doc_group_id = $arr['doc_group'];
192 if (!is_array(@$this->Documents[$doc_group_id])) {
193 $this->Documents[$doc_group_id] = array();
196 $this->Documents[$doc_group_id][] = new Document($this->Group, $arr['docid'], $arr);
201 * getStates - Return an array of states that have documents associated to them
203 function getUsedStates() {
204 $result = db_query_params ('SELECT DISTINCT doc_states.stateid,doc_states.name
205 FROM doc_states,doc_data
206 WHERE doc_data.stateid=doc_states.stateid
207 ORDER BY doc_states.name ASC',
210 exit_error('error', db_error());
214 while ($arr = db_fetch_array($result)) {
225 // c-file-style: "bsd"