5 * GForge Trove facility
7 * Copyright 2004 (c) Guillaume Smet
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
27 require_once('TroveCategoryLabel.class.php');
29 // should extend observable
30 class TroveCategory extends Error {
33 * Associative array of data from db.
35 * @var array $dataArray.
40 * Selected Trove category id
42 * @var int $categoryId
56 * @param int The trove_cat_id.
57 * @param array The associative array of data.
58 * @return boolean success.
60 function TroveCategory($categoryId = false, $dataArray = false) {
62 $this->categoryId = $categoryId;
63 if (!$dataArray || !is_array($dataArray)) {
64 if (!$this->fetchData($categoryId)) {
65 $this->setError(_('Invalid Trove Category'),
66 _('That Trove category does not exist.').' '.db_error()
70 $this->dataArray =& $dataArray;
73 $this->setError(_('ERROR'), _('That Trove category does not exist.'));
78 * fetchData - re-fetch the data for this category from the database.
80 * @param int The category_id.
81 * @return boolean success.
83 function fetchData($categoryId) {
84 $res = db_query_params('SELECT * FROM trove_cat WHERE trove_cat_id=$1',
86 if (!$res || db_numrows($res) < 1) {
89 $this->dataArray =& db_fetch_array($res);
94 function update($shortName, $fullName, $description) {
95 $shortName = trim($shortName);
96 $fullName = trim($fullName);
97 $description = trim($description);
98 if(empty($shortName) || empty($fullName)) {
99 $this->setError(_('ERROR'), _('Empty strings'));
103 $result = db_query_params("UPDATE trove_cat
108 WHERE trove_cat_id=$5",
109 array(htmlspecialchars($shortName), htmlspecialchars($fullName), htmlspecialchars($description), date('Ymd',time())."01", $this->categoryId));
110 if(!$result || db_affected_rows($result) != 1) {
111 $this->setError(_('ERROR'), _('Cannot update'));
116 $this->fetchData($this->categoryId);
123 return $this->categoryId;
126 // returns a localized label if available
127 function & getLabel($languageId) {
128 if(!isset($this->labels)) {
131 if(isset($this->labels[$languageId])) {
132 return $this->labels[$languageId];
135 return $this->labels;
139 function getLocalizedLabel() {
140 $languageId = choose_language_from_context();
141 $label = $this->getLabel($languageId);
143 return $label->getLabel();
145 return $this->getFullName();
149 function &getLabels() {
150 if(!isset($this->labels)) {
151 $this->labels = array();
152 $res = db_query_params("SELECT trove_category_labels.*, supported_languages.name AS language_name FROM trove_category_labels, supported_languages
153 WHERE category_id=$1 AND supported_languages.language_id=trove_category_labels.language_id",
154 array($this->cathergoryId));
157 return $this->labels;
159 while($data =& db_fetch_array($res)) {
160 $this->labels[$data['language_id']] = new TroveCategoryLabel($this, $data['label_id'], $data);
162 db_free_result($res);
164 return $this->labels;
167 function &getParents() {
168 return $this->parents;
171 function &getChildren() {
172 if(!isset($this->children)) {
173 $this->children = array();
175 $result = db_query_params("
177 trove_treesums.subprojects AS subprojects
178 FROM trove_cat LEFT JOIN trove_treesums USING (trove_cat_id)
180 trove_treesums.limit_1=0
181 OR trove_treesums.limit_1 IS NULL
183 AND trove_cat.parent=$1
185 array($this->categoryId), -1, 0, SYS_DB_TROVE);
191 while ($array = db_fetch_array($result)) {
192 $this->children[] = new TroveCategory($array['trove_cat_id'], $array);
196 return $this->children;
199 function getRootParentId() {
200 return $this->dataArray['root_parent'];
203 function getFullName() {
204 return $this->dataArray['fullname'];
207 function getShortName() {
208 return $this->dataArray['shortname'];
211 function getDescription() {
212 return $this->dataArray['description'];
215 function getSubProjectsCount() {
216 return ($this->dataArray['subprojects'] ? $this->dataArray['subprojects'] : 0);
219 function setFilter($filterArray) {
220 $this->filter = $filterArray;
223 function getProjects($offset) {
224 $qpa = db_constract_qpa () ;
225 $qpa = db_construct_qpa ($qpa, 'SELECT * FROM trove_agg') ;
227 for($i = 0, $count = sizeof($this->filter); $i < $count; $i++) {
228 $qpa = db_construct_qpa ($qpa,
229 ", trove_agg trove_agg_$i") ;
232 $qpa = db_construct_qpa ($qpa,
233 ' WHERE trove_agg.trove_cat_id=$1 ',
234 array ($this->categoryId)) ;
236 for($i = 0, $count = sizeof($this->filter); $i < $count; $i++) {
237 $qpa = db_construct_qpa ($qpa,
238 "AND trove_agg_$i.trove_cat_id=$1 AND trove_agg_$i.group_id=trove_agg.group_id ",
239 array ($this->filter[$i])) ;
242 $qpa = db_construct_qpa ($qpa,
243 'ORDER BY trove_agg.trove_cat_id ASC, trove_agg.ranking ASC') ;
245 $result = db_query_qpa ($qpa, TROVE__PROJECTS_PER_PAGE, $offset);
253 // c-file-style: "bsd"