5 * Copyright 2011, Alcatel-Lucent
7 * This file is part of FusionForge. FusionForge is free software;
8 * you can redistribute it and/or modify it under the terms of the
9 * GNU General Public License as published by the Free Software
10 * Foundation; either version 2 of the Licence, or (at your option)
13 * FusionForge is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * Standard Alcatel-Lucent disclaimer for contributing to open source
26 * "The Roadmap ("Contribution") has not been tested and/or
27 * validated for release as or in products, combinations with products or
28 * other commercial use. Any use of the Contribution is entirely made at
29 * the user's own responsibility and the user can not rely on any features,
30 * functionalities or performances Alcatel-Lucent has attributed to the
33 * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
34 * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
35 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
36 * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
37 * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
38 * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
39 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
40 * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
41 * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
45 require_once $gfcommon.'include/Error.class.php';
47 class Roadmap extends Error {
62 function __construct($group, $roadmap_id=0, $data=false) {
65 if (is_object($group)) {
66 if ($group->isError()) {
67 $this->setError('in Roadmap, '.$group->getErrorMessage());
70 $this->group = $group;
71 $this->group_id = $group->getID();
74 $this->setError('No Valid Group');
78 /*if (! $this->userCanView()) {
79 $this->setPermissionDeniedError();
80 $this->data_array = null;
84 $this->roadmap_id = $roadmap_id;
87 $this->release_order = false;
88 $this->is_default = 0;
90 $this->data_array = array();
91 return $this->_fetchData($data);
97 public function create($name) {
98 if (! $this->_isAdmin()) return false;
100 $result = db_query_params ('SELECT * FROM roadmap WHERE group_id=$1 AND name=$2',
101 array ($this->group_id, $name));
102 if ($result && db_numrows($result)) {
103 $this->setError(sprintf(_('"%s" roadmap already exists'), $name));
107 $result = db_query_params('INSERT INTO roadmap (group_id, name, enable, release_order, is_default) VALUES ($1, $2, $3, $4, $5)',
108 array ($this->group_id,
111 $this->release_order,
114 $this->setError('in create, '.db_error());
117 $this->roadmap_id = db_insertid($result, 'roadmap', 'roadmap_id');
122 public function delete() {
123 if (! $this->_isAdmin()) return false;
125 $result = db_query_params('DELETE FROM roadmap_list WHERE roadmap_id=$1',
126 array ($this->roadmap_id));
128 $this->setError('in delete, '.db_error());
131 $result = db_query_params('DELETE FROM roadmap WHERE roadmap_id=$1',
132 array ($this->roadmap_id));
134 $this->setError('in delete, '.db_error());
137 $this->roadmap_id = 0;
140 $this->release_order = false;
141 $this->is_default = 0;
142 $this->data_array = array();
146 public function getID() {
147 return $this->roadmap_id;
150 public function rename($name) {
151 if (! $this->_isAdmin()) return false;
153 $result = db_query_params('UPDATE roadmap SET name=$1 WHERE roadmap_id=$2',
157 $this->setError('in rename, '.db_error());
164 public function getName() {
168 public function enable() {
169 if (! $this->_isAdmin()) return false;
171 return $this->_setState(1);
174 public function disable() {
175 if (! $this->_isAdmin()) return false;
177 return $this->_setState(0);
180 public function setState($state) {
181 if (! $this->_isAdmin()) return false;
186 $result = $this->disable();
191 $result = $this->enable();
200 $this->setError('in setState, '.db_error());
206 public function getState() {
207 return $this->enable;
210 public function setReleaseOrder($release_order) {
211 if (! $this->_isAdmin()) return false;
213 $result = db_query_params('UPDATE roadmap SET release_order=$1 WHERE roadmap_id=$2',
214 array (serialize($release_order),
217 $this->setError('in setReleaseOrder, '.db_error());
220 $this->release_order = $release_order;
224 public function getReleaseOrder() {
225 return $this->release_order;
228 public function setDefault($default) {
229 if (! $this->_isAdmin()) return false;
231 $result = db_query_params('UPDATE roadmap SET is_default=$1 WHERE roadmap_id=$2',
235 $this->setError('in setDefault, '.db_error());
238 $this->is_default = $default;
242 public function isDefault($default=-1) {
243 if ($default == -1) {
244 return $this->is_default;
247 return $this->setDefault($default);
251 public function setList($arg1, $arg2=false) {
252 if (! $this->_isAdmin()) return false;
254 if (is_array($arg1)) {
256 foreach ($arg1 as $artifact_type_id => $field_id) {
257 $result = $this->_setList($artifact_type_id, $field_id);
261 $this->setError('in setList, '.db_error());
268 $result = $this->_setList($arg1, $arg2);
271 $this->setError('in setList, '.db_error());
279 public function getList($artifact_type_id=false) {
280 if ($artifact_type_id === false) {
281 return $this->data_array;
284 if (array_key_exists($artifact_type_id, $this->data_array)) {
285 return $this->data_array[$artifact_type_id];
292 public function getReleases() {
293 $atf = new ArtifactTypeFactory($this->group);
294 if (!$atf || !is_object($atf) || $atf->isError()) {
295 $this->setError('in getReleases, could not get ArtifactTypeFactory');
298 $at_arr = $atf->getArtifactTypes();
299 if (!$at_arr || count($at_arr) < 1) {
303 $artifact_type_list = $this->getList();
304 $release_order = $this->getReleaseOrder();
308 if (is_array($release_order)) {
309 foreach ($release_order as $release_value) {
310 $releases[$release_value] = false;
314 $releases2add = array();
315 foreach ($at_arr as $artifact_type) {
316 if (!is_object($artifact_type)) {
318 } elseif ($artifact_type->isError()) {
319 $this->setError($artifact_type->getErrorMessage());
323 if (! array_key_exists($artifact_type->getID(), $artifact_type_list) || ! $artifact_type_list[$artifact_type->getID()]) {
324 // This tracker is not used for the roadmap
327 $field_id = $artifact_type_list[$artifact_type->getID()];
329 $ath = new ArtifactTypeHtml($this->group, $artifact_type->getID());
331 if (!forge_check_perm ('tracker', $artifact_type->getID(), 'read')) {
335 $ef_elements = $ath->getExtraFieldElements($field_id);
336 if (is_array($ef_elements)) {
337 foreach ($ef_elements as $ef_element) {
338 $releases2add[] = $ef_element['element_name'];
344 usort($releases2add, 'version_compare');
345 foreach ($releases2add as $release_value) {
346 $releases[$release_value] = true;
349 // Remove release values of the release_order field (from 'roadmap' db table) that are no longer used
350 if (is_array($release_order)) {
351 foreach ($release_order as $release_value) {
352 if ($releases[$release_value] === false) {
353 unset($releases[$release_value]);
358 return array_keys($releases);
364 private function _fetchData($data=false) {
365 if (is_array($data)) {
366 foreach ($data as $key => $value) {
367 switch ((string)$key) {
368 case 'release_order':
370 $this->release_order = unserialize($value);
373 $this->release_order = array();
382 $this->{$key} = $value;
386 $this->data_array = $this->getList();
388 elseif (isset($this->roadmap_id) && $this->roadmap_id) {
389 $result = db_query_params ('SELECT * FROM roadmap WHERE roadmap_id=$1',
390 array ($this->roadmap_id));
392 $this->setError('in _fetchData, '.db_error());
395 if (db_numrows($result)) {
396 $this->name = db_result($result, 0, 'name');
397 $this->enable = db_result($result, 0, 'enable');
398 $tmp = db_result($result, 0, 'release_order');
399 if ($tmp) $this->release_order = unserialize($tmp);
400 $this->is_default = db_result($result, 0, 'is_default');
401 db_free_result($result);
405 if ($this->roadmap_id) {
406 $result = db_query_params ('SELECT * FROM roadmap_list WHERE roadmap_id=$1 ORDER BY artifact_type_id',
407 array ($this->roadmap_id));
409 $this->setError('in _fetchData, '.db_error());
412 while($entry = db_fetch_array($result)) {
413 $this->data_array[$entry['artifact_type_id']] = $entry['field_id'];
415 db_free_result($result);
421 private function _setState($state) {
422 $result = db_query_params('UPDATE roadmap SET enable=$1 WHERE group_id=$2 AND name=$3',
430 $this->enable = $state;
434 private function _insertList($artifact_type_id, $field_id) {
435 $result = db_query_params('INSERT INTO roadmap_list (roadmap_id, artifact_type_id, field_id) VALUES ($1, $2, $3)',
436 array ($this->roadmap_id,
442 $this->data_array[$artifact_type_id] = $field_id;
446 private function _updateList($artifact_type_id, $field_id) {
447 $result = db_query_params('UPDATE roadmap_list SET field_id=$1 WHERE roadmap_id=$2 AND artifact_type_id=$3',
454 $this->data_array[$artifact_type_id] = $field_id;
458 private function _deleteList($artifact_type_id) {
459 $result = db_query_params('DELETE FROM roadmap_list WHERE roadmap_id=$1 AND artifact_type_id=$2',
460 array ($this->roadmap_id,
465 $this->data_array[$artifact_type_id] = 0;
469 private function _setList($artifact_type_id, $field_id) {
471 $result = $this->_deleteList($artifact_type_id);
473 elseif (array_key_exists($artifact_type_id, $this->data_array)) {
474 $result = $this->_updateList($artifact_type_id, $field_id);
477 $result = $this->_insertList($artifact_type_id, $field_id);
483 private function _isAdmin() {
484 if (isset($this->is_admin)) {
485 if ($this->is_admin) {
489 $this->setPermissionDeniedError();
493 $perm =& $this->group->getPermission(session_get_user());
495 if (!$perm || !is_object($perm) || !$perm->isArtifactAdmin()) {
496 $this->setPermissionDeniedError();