6 * Copyright 2004 (c) Guillaume Smet
7 * http://fusionforge.org/
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 require_once('TroveCategoryLabel.class.php');
27 // should extend observable
28 class TroveCategory extends Error {
31 * Associative array of data from db.
33 * @var array $dataArray.
38 * Selected Trove category id
40 * @var int $categoryId
54 * @param int The trove_cat_id.
55 * @param array The associative array of data.
56 * @return boolean success.
58 function TroveCategory($categoryId = false, $dataArray = false) {
60 $this->categoryId = $categoryId;
61 if (!$dataArray || !is_array($dataArray)) {
62 if (!$this->fetchData($categoryId)) {
63 $this->setError(_('Invalid Trove Category'),
64 _('That Trove category does not exist.').' '.db_error()
68 $this->dataArray =& $dataArray;
71 $this->setError(_('ERROR'), _('That Trove category does not exist.'));
76 * fetchData - re-fetch the data for this category from the database.
78 * @param int The category_id.
79 * @return boolean success.
81 function fetchData($categoryId) {
82 $res = db_query_params('SELECT * FROM trove_cat WHERE trove_cat_id=$1',
84 if (!$res || db_numrows($res) < 1) {
87 $this->dataArray =& db_fetch_array($res);
92 function update($shortName, $fullName, $description) {
93 $shortName = trim($shortName);
94 $fullName = trim($fullName);
95 $description = trim($description);
96 if(empty($shortName) || empty($fullName)) {
97 $this->setError(_('ERROR'), _('Empty strings'));
101 $result = db_query_params("UPDATE trove_cat
106 WHERE trove_cat_id=$5",
107 array(htmlspecialchars($shortName), htmlspecialchars($fullName), htmlspecialchars($description), date('Ymd',time())."01", $this->categoryId));
108 if(!$result || db_affected_rows($result) != 1) {
109 $this->setError(_('ERROR'), _('Cannot update'));
114 $this->fetchData($this->categoryId);
121 return $this->categoryId;
124 // returns a localized label if available
125 function & getLabel($languageId) {
126 if(!isset($this->labels)) {
129 if(isset($this->labels[$languageId])) {
130 return $this->labels[$languageId];
133 return $this->labels;
137 function getLocalizedLabel() {
138 $languageId = choose_language_from_context();
139 $label = $this->getLabel($languageId);
141 return $label->getLabel();
143 return $this->getFullName();
147 function &getLabels() {
148 if(!isset($this->labels)) {
149 $this->labels = array();
150 $res = db_query_params("SELECT trove_category_labels.*, supported_languages.name AS language_name FROM trove_category_labels, supported_languages
151 WHERE category_id=$1 AND supported_languages.language_id=trove_category_labels.language_id",
152 array($this->cathergoryId));
155 return $this->labels;
157 while($data =& db_fetch_array($res)) {
158 $this->labels[$data['language_id']] = new TroveCategoryLabel($this, $data['label_id'], $data);
160 db_free_result($res);
162 return $this->labels;
165 function &getParents() {
166 return $this->parents;
169 function &getChildren() {
170 if(!isset($this->children)) {
171 $this->children = array();
173 $result = db_query_params("
175 trove_treesums.subprojects AS subprojects
176 FROM trove_cat LEFT JOIN trove_treesums USING (trove_cat_id)
178 trove_treesums.limit_1=0
179 OR trove_treesums.limit_1 IS NULL
181 AND trove_cat.parent=$1
183 array($this->categoryId), -1, 0, SYS_DB_TROVE);
189 while ($array = db_fetch_array($result)) {
190 $this->children[] = new TroveCategory($array['trove_cat_id'], $array);
194 return $this->children;
197 function getRootParentId() {
198 return $this->dataArray['root_parent'];
201 function getFullName() {
202 return $this->dataArray['fullname'];
205 function getShortName() {
206 return $this->dataArray['shortname'];
209 function getDescription() {
210 return $this->dataArray['description'];
213 function getSubProjectsCount() {
214 return ($this->dataArray['subprojects'] ? $this->dataArray['subprojects'] : 0);
217 function setFilter($filterArray) {
218 $this->filter = $filterArray;
221 function getProjects($offset) {
222 $qpa = db_constract_qpa () ;
223 $qpa = db_construct_qpa ($qpa, 'SELECT * FROM trove_agg') ;
225 for($i = 0, $count = sizeof($this->filter); $i < $count; $i++) {
226 $qpa = db_construct_qpa ($qpa,
227 ", trove_agg trove_agg_$i") ;
230 $qpa = db_construct_qpa ($qpa,
231 ' WHERE trove_agg.trove_cat_id=$1 ',
232 array ($this->categoryId)) ;
234 for($i = 0, $count = sizeof($this->filter); $i < $count; $i++) {
235 $qpa = db_construct_qpa ($qpa,
236 "AND trove_agg_$i.trove_cat_id=$1 AND trove_agg_$i.group_id=trove_agg.group_id ",
237 array ($this->filter[$i])) ;
240 $qpa = db_construct_qpa ($qpa,
241 'ORDER BY trove_agg.trove_cat_id ASC, trove_agg.ranking ASC') ;
243 $result = db_query_qpa ($qpa, TROVE__PROJECTS_PER_PAGE, $offset);
251 // c-file-style: "bsd"