3 * GForge Doc Mgr Facility
5 * Copyright 2002 GForge, LLC
10 * This file is part of GForge.
12 * GForge is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * GForge 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
23 * along with GForge; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 by Quentin Cregan, SourceForge 06/2000
33 Complete OO rewrite by Tim Perdue 1/2003
36 require_once('common/include/Error.class.php');
37 require_once('common/docman/Document.class.php');
39 class DocumentFactory extends Error {
49 * The Documents dictionary.
51 * @var array Contains doc_group_id as key and the array of documents associated to that id.
57 var $sort='group_name, title';
62 * @param object The Group object to which this DocumentFactory is associated.
63 * @return boolean success.
65 function DocumentFactory(&$Group) {
67 if (!$Group || !is_object($Group)) {
68 $this->setError('ProjectGroup:: No Valid Group Object');
71 if ($Group->isError()) {
72 $this->setError('ProjectGroup:: '.$Group->getErrorMessage());
75 $this->Group =& $Group;
81 * getGroup - get the Group object this DocumentFactory is associated with.
83 * @return object the Group object.
85 function &getGroup() {
90 * setStateID - call this before getDocuments() if you want to limit to a specific state.
92 * @param int The stateid from the doc_states table.
94 function setStateID($stateid) {
95 $this->stateid=$stateid;
99 * setLanguageID - call this before getDocuments() if you want to limit to a specific language.
101 * @param int The language_id from the supported_languages table.
103 function setLanguageID($languageid) {
104 $this->languageid=$languageid;
108 * setDocGroupID - call this before getDocuments() if you want to limit to a specific doc_group.
110 * @param int The doc_group from the doc_groups table.
112 function setDocGroupID($docgroupid) {
113 $this->docgroupid=$docgroupid;
117 * setSort - call this before getDocuments() if you want to control the sorting.
119 * @param string The name of the field to sort on.
121 function setSort($sort) {
126 * getDocuments - returns an array of Document objects.
128 * @return array Document objects.
130 function &getDocuments() {
131 if (!$this->Documents) {
136 // If the document group is specified, we should only check that group in
137 // the Documents array. If not, we should check ALL the groups.
138 if ($this->docgroupid) {
139 $keys = array($this->docgroupid);
141 $keys = array_keys($this->Documents);
144 foreach ($keys as $key) {
145 if (!array_key_exists($key, $this->Documents)) continue; // Should not happen
146 $count = count($this->Documents[$key]);
148 for ($i=0; $i < $count; $i++) {
149 $valid = true; // do we need to return this document?
150 $doc =& $this->Documents[$key][$i];
152 if (!$this->stateid) {
153 if (session_loggedin()) {
154 $perm =& $this->Group->getPermission( session_get_user() );
155 if (!$perm || !is_object($perm) || !$perm->isMember()) {
156 if ($doc->getStateID() != 1) { // non-active document?
160 if ($doc->getStateID() != 1 && /* not active */
161 $doc->getStateID() != 4 && /* not hidden */
162 $doc->getStateID() != 5) { /* not private */
167 if ($doc->getStateID() != 1) { // non-active document?
172 if ($this->stateid != "ALL" && $doc->getStateID() != $this->stateid) {
177 if ($this->languageid && $doc->getLanguageID() != $this->languageid) {
188 if (count($return) == 0) {
189 $this->setError(_('No Documents Found'));
197 if (!$this->stateid) {
198 if (session_loggedin()) {
199 $perm =& $this->Group->getPermission( session_get_user() );
200 if (!$perm || !is_object($perm) || !$perm->isMember()) {
201 $public_flag='AND stateid=1';
203 $public_flag='AND stateid IN (1,4,5)';
206 $public_flag='AND stateid=1';
209 if ($this->stateid =='ALL') {
212 $public_flag='AND stateid =\''.$this->stateid.'\'';
216 if ($this->docgroupid) {
217 $docgroupsql="AND doc_group='".$this->docgroupid."'";
220 if ($this->languageid) {
221 $languagesql="AND language_id='".$this->languageid."'";
226 WHERE group_id='". $this->Group->getID() ."'
230 ORDER BY ".$this->sort;
232 $result = db_query ($sql);
234 $rows = db_numrows($result);
236 if (!$result || $rows < 1) {
237 $this->setError(_('No Documents Found')." ".db_error());
240 while ($arr =& db_fetch_array($result)) {
241 $this->Documents[] = new Document($this->Group, $arr['docid'], $arr);
244 return $this->Documents;
249 * getFromDB - Retrieve documents from database.
251 function getFromDB() {
252 $this->Documents = array();
253 $sql = 'SELECT * FROM docdata_vw ORDER BY title';
254 $result = db_query($sql);
256 exit_error('Error', db_error());
259 while ($arr =& db_fetch_array($result)) {
260 $doc_group_id = $arr['doc_group'];
261 if (!is_array($this->Documents[$doc_group_id])) {
262 $this->Documents[$doc_group_id] = array();
265 $this->Documents[$doc_group_id][] = new Document($this->Group, $arr['docid'], $arr);
270 * getStates - Return an array of states that have documents associated to them
272 function getUsedStates() {
273 $sql = "SELECT DISTINCT doc_states.stateid,doc_states.name
274 FROM doc_states,doc_data
275 WHERE doc_data.stateid=doc_states.stateid
276 ORDER BY doc_states.name ASC";
277 $result = db_query($sql);
279 exit_error('error', db_error());
283 while ($arr = db_fetch_array($result)) {
294 // c-file-style: "bsd"