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 FFError {
31 * Associative array of data from db.
33 * @var array $dataArray.
38 * Selected Trove category id
40 * @var int $categoryId
52 * @param bool|int $categoryId The trove_cat_id.
53 * @param array $dataArray The associative array of data.
55 function __construct($categoryId = false, $dataArray = array()) {
57 $this->categoryId = $categoryId;
58 if (!$dataArray || !is_array($dataArray)) {
59 if (!$this->fetchData($categoryId)) {
60 $this->setError(_('Invalid Trove Category'),
61 _('That Trove category does not exist').' '.db_error('SYS_DB_TROVE')
65 $this->dataArray =& $dataArray;
68 $this->setError(_('Error'), _('That Trove category does not exist'));
73 * fetchData - re-fetch the data for this category from the database.
75 * @param int $categoryId The category_id.
76 * @return bool success.
78 function fetchData($categoryId) {
79 $res = db_query_params('SELECT * FROM trove_cat WHERE trove_cat_id=$1',
81 if (!$res || db_numrows($res) < 1) {
84 $this->dataArray =& db_fetch_array($res);
89 function update($shortName, $fullName, $description) {
90 $shortName = trim($shortName);
91 $fullName = trim($fullName);
92 $description = trim($description);
93 if(empty($shortName) || empty($fullName)) {
94 $this->setError(_('Error'), _('Empty strings'));
98 $result = db_query_params("UPDATE trove_cat
103 WHERE trove_cat_id=$5",
104 array(htmlspecialchars($shortName), htmlspecialchars($fullName), htmlspecialchars($description), date('Ymd',time())."01", $this->categoryId));
105 if(!$result || db_affected_rows($result) != 1) {
106 $this->setError(_('Error'), _('Cannot update'));
111 $this->fetchData($this->categoryId);
118 return $this->categoryId;
121 // returns a localized label if available
122 function & getLabel($languageId) {
123 if(!isset($this->labels)) {
126 if(isset($this->labels[$languageId])) {
127 return $this->labels[$languageId];
130 return $this->labels;
134 function getLocalizedLabel() {
135 $languageId = choose_language_from_context();
136 $label = $this->getLabel($languageId);
138 return $label->getLabel();
140 return $this->getFullName();
144 function &getLabels() {
145 if(!isset($this->labels)) {
146 $this->labels = array();
147 $res = db_query_params("SELECT trove_category_labels.*, supported_languages.name AS language_name FROM trove_category_labels, supported_languages
148 WHERE category_id=$1 AND supported_languages.language_id=trove_category_labels.language_id",
149 array($this->categoryId));
152 return $this->labels;
154 while($data =& db_fetch_array($res)) {
155 $this->labels[$data['language_id']] = new TroveCategoryLabel($this, $data['label_id'], $data);
157 db_free_result($res);
159 return $this->labels;
162 function &getParents() {
163 return $this->parents;
166 function &getChildren() {
167 if(!isset($this->children)) {
168 $this->children = array();
170 $result = db_query_params("
172 trove_treesums.subprojects AS subprojects
173 FROM trove_cat LEFT JOIN trove_treesums USING (trove_cat_id)
175 trove_treesums.limit_1=0
176 OR trove_treesums.limit_1 IS NULL
178 AND trove_cat.parent=$1
180 array($this->categoryId), -1, 0, 'SYS_DB_TROVE');
186 while ($array = db_fetch_array($result)) {
187 $this->children[] = new TroveCategory($array['trove_cat_id'], $array);
191 return $this->children;
194 function getRootParentId() {
195 return $this->dataArray['root_parent'];
198 function getFullName() {
199 return $this->dataArray['fullname'];
202 function getShortName() {
203 return $this->dataArray['shortname'];
206 function getDescription() {
207 return $this->dataArray['description'];
210 function getSubProjectsCount() {
211 return ($this->dataArray['subprojects'] ? $this->dataArray['subprojects'] : 0);
214 function setFilter($filterArray) {
215 $this->filter = $filterArray;
218 function getProjects($offset) {
219 $qpa = db_construct_qpa () ;
220 $qpa = db_construct_qpa($qpa, 'SELECT * FROM trove_agg') ;
222 for($i = 0, $count = sizeof($this->filter); $i < $count; $i++) {
223 $qpa = db_construct_qpa($qpa,
224 ", trove_agg trove_agg_$i") ;
227 $qpa = db_construct_qpa($qpa,
228 ' WHERE trove_agg.trove_cat_id=$1 ',
229 array ($this->categoryId)) ;
231 for($i = 0, $count = sizeof($this->filter); $i < $count; $i++) {
232 $qpa = db_construct_qpa($qpa,
233 "AND trove_agg_$i.trove_cat_id=$1 AND trove_agg_$i.group_id=trove_agg.group_id ",
234 array ($this->filter[$i])) ;
237 $qpa = db_construct_qpa($qpa,
238 'ORDER BY trove_agg.trove_cat_id ASC, trove_agg.ranking ASC') ;
240 $result = db_query_qpa ($qpa, TROVE__PROJECTS_PER_PAGE, $offset);
248 // c-file-style: "bsd"