5 * Copyright 2011, Alcatel-Lucent
6 * Copyright (C) 2012 Alain Peyrat - Alcatel-Lucent
8 * This file is part of FusionForge. FusionForge is free software;
9 * you can redistribute it and/or modify it under the terms of the
10 * GNU General Public License as published by the Free Software
11 * Foundation; either version 2 of the Licence, or (at your option)
14 * FusionForge is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * Standard Alcatel-Lucent disclaimer for contributing to open source
27 * "The Roadmap ("Contribution") has not been tested and/or
28 * validated for release as or in products, combinations with products or
29 * other commercial use. Any use of the Contribution is entirely made at
30 * the user's own responsibility and the user can not rely on any features,
31 * functionalities or performances Alcatel-Lucent has attributed to the
34 * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
35 * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
36 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
37 * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
38 * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
39 * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
40 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
41 * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
42 * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
46 require_once $gfcommon.'include/Error.class.php';
48 class Roadmap extends Error {
63 function __construct($group, $roadmap_id=0, $data=false) {
66 if (is_object($group)) {
67 if ($group->isError()) {
68 $this->setError('in Roadmap, '.$group->getErrorMessage());
71 $this->group = $group;
72 $this->group_id = $group->getID();
75 $this->setError('No Valid Group');
79 /*if (! $this->userCanView()) {
80 $this->setPermissionDeniedError();
81 $this->data_array = null;
85 $this->roadmap_id = $roadmap_id;
88 $this->release_order = false;
89 $this->is_default = 0;
91 $this->data_array = array();
92 return $this->_fetchData($data);
98 public function create($name) {
99 if (! $this->_isAdmin()) return false;
101 $result = db_query_params ('SELECT * FROM roadmap WHERE group_id=$1 AND name=$2',
102 array ($this->group_id, $name));
103 if ($result && db_numrows($result)) {
104 $this->setError(sprintf(_('"%s" roadmap already exists'), $name));
108 $result = db_query_params('INSERT INTO roadmap (group_id, name, enable, release_order, is_default) VALUES ($1, $2, $3, $4, $5)',
109 array ($this->group_id,
112 $this->release_order,
115 $this->setError('in create, '.db_error());
118 $this->roadmap_id = db_insertid($result, 'roadmap', 'roadmap_id');
123 public function delete() {
124 if (! $this->_isAdmin()) return false;
126 $result = db_query_params('DELETE FROM roadmap_list WHERE roadmap_id=$1',
127 array ($this->roadmap_id));
129 $this->setError('in delete, '.db_error());
132 $result = db_query_params('DELETE FROM roadmap WHERE roadmap_id=$1',
133 array ($this->roadmap_id));
135 $this->setError('in delete, '.db_error());
138 $this->roadmap_id = 0;
141 $this->release_order = false;
142 $this->is_default = 0;
143 $this->data_array = array();
147 public function getID() {
148 return $this->roadmap_id;
151 public function rename($name) {
152 if (! $this->_isAdmin()) return false;
154 $result = db_query_params('UPDATE roadmap SET name=$1 WHERE roadmap_id=$2',
158 $this->setError('in rename, '.db_error());
165 public function getName() {
169 public function enable() {
170 if (! $this->_isAdmin()) return false;
172 return $this->_setState(1);
175 public function disable() {
176 if (! $this->_isAdmin()) return false;
178 return $this->_setState(0);
181 public function setState($state) {
182 if (! $this->_isAdmin()) return false;
187 $result = $this->disable();
192 $result = $this->enable();
201 $this->setError('in setState, '.db_error());
207 public function getState() {
208 return $this->enable;
211 public function setReleaseOrder($release_order) {
212 if (! $this->_isAdmin()) return false;
214 $result = db_query_params('UPDATE roadmap SET release_order=$1 WHERE roadmap_id=$2',
215 array (serialize($release_order),
218 $this->setError('in setReleaseOrder, '.db_error());
221 $this->release_order = $release_order;
225 public function getReleaseOrder() {
226 return $this->release_order;
229 public function setDefault($default) {
230 if (! $this->_isAdmin()) return false;
232 $result = db_query_params('UPDATE roadmap SET is_default=$1 WHERE roadmap_id=$2',
236 $this->setError('in setDefault, '.db_error());
239 $this->is_default = $default;
243 public function isDefault($default=-1) {
244 if ($default == -1) {
245 return $this->is_default;
248 return $this->setDefault($default);
252 public function setList($arg1, $arg2=false) {
253 if (! $this->_isAdmin()) return false;
255 if (is_array($arg1)) {
257 foreach ($arg1 as $artifact_type_id => $field_id) {
258 $result = $this->_setList($artifact_type_id, $field_id);
262 $this->setError('in setList, '.db_error());
269 $result = $this->_setList($arg1, $arg2);
272 $this->setError('in setList, '.db_error());
280 public function getList($artifact_type_id=false) {
281 if ($artifact_type_id === false) {
282 return $this->data_array;
285 if (array_key_exists($artifact_type_id, $this->data_array)) {
286 return $this->data_array[$artifact_type_id];
293 public function getReleases() {
294 $atf = new ArtifactTypeFactory($this->group);
295 if (!$atf || !is_object($atf) || $atf->isError()) {
296 $this->setError('in getReleases, could not get ArtifactTypeFactory');
299 $at_arr = $atf->getArtifactTypes();
300 if (!$at_arr || count($at_arr) < 1) {
304 $artifact_type_list = $this->getList();
305 $release_order = $this->getReleaseOrder();
309 if (is_array($release_order)) {
310 foreach ($release_order as $release_value) {
311 $releases[$release_value] = false;
315 $releases2add = array();
316 foreach ($at_arr as $artifact_type) {
317 if (!is_object($artifact_type)) {
319 } elseif ($artifact_type->isError()) {
320 $this->setError($artifact_type->getErrorMessage());
324 if (! array_key_exists($artifact_type->getID(), $artifact_type_list) || ! $artifact_type_list[$artifact_type->getID()]) {
325 // This tracker is not used for the roadmap
328 $field_id = $artifact_type_list[$artifact_type->getID()];
330 $ath = new ArtifactTypeHtml($this->group, $artifact_type->getID());
332 if (!forge_check_perm ('tracker', $artifact_type->getID(), 'read')) {
336 $ef_elements = $ath->getExtraFieldElements($field_id);
337 if (is_array($ef_elements)) {
338 foreach ($ef_elements as $ef_element) {
339 $releases2add[] = $ef_element['element_name'];
345 usort($releases2add, 'version_compare');
346 foreach ($releases2add as $release_value) {
347 $releases[$release_value] = true;
350 // Remove release values of the release_order field (from 'roadmap' db table) that are no longer used
351 if (is_array($release_order)) {
352 foreach ($release_order as $release_value) {
353 if ($releases[$release_value] === false) {
354 unset($releases[$release_value]);
359 return array_keys($releases);
365 private function _fetchData($data=false) {
366 if (is_array($data)) {
367 foreach ($data as $key => $value) {
368 switch ((string)$key) {
369 case 'release_order':
371 $this->release_order = unserialize($value);
374 $this->release_order = array();
383 $this->{$key} = $value;
387 $this->data_array = $this->getList();
389 elseif (isset($this->roadmap_id) && $this->roadmap_id) {
390 $result = db_query_params ('SELECT * FROM roadmap WHERE roadmap_id=$1',
391 array ($this->roadmap_id));
393 $this->setError('in _fetchData, '.db_error());
396 if (db_numrows($result)) {
397 $this->name = db_result($result, 0, 'name');
398 $this->enable = db_result($result, 0, 'enable');
399 $tmp = db_result($result, 0, 'release_order');
400 if ($tmp) $this->release_order = unserialize($tmp);
401 $this->is_default = db_result($result, 0, 'is_default');
402 db_free_result($result);
406 if ($this->roadmap_id) {
407 $result = db_query_params ('SELECT * FROM roadmap_list WHERE roadmap_id=$1 ORDER BY artifact_type_id',
408 array ($this->roadmap_id));
410 $this->setError('in _fetchData, '.db_error());
413 while($entry = db_fetch_array($result)) {
414 $this->data_array[$entry['artifact_type_id']] = $entry['field_id'];
416 db_free_result($result);
422 private function _setState($state) {
423 $result = db_query_params('UPDATE roadmap SET enable=$1 WHERE group_id=$2 AND name=$3',
431 $this->enable = $state;
435 private function _insertList($artifact_type_id, $field_id) {
436 $result = db_query_params('INSERT INTO roadmap_list (roadmap_id, artifact_type_id, field_id) VALUES ($1, $2, $3)',
437 array ($this->roadmap_id,
443 $this->data_array[$artifact_type_id] = $field_id;
447 private function _updateList($artifact_type_id, $field_id) {
448 $result = db_query_params('UPDATE roadmap_list SET field_id=$1 WHERE roadmap_id=$2 AND artifact_type_id=$3',
455 $this->data_array[$artifact_type_id] = $field_id;
459 private function _deleteList($artifact_type_id) {
460 $result = db_query_params('DELETE FROM roadmap_list WHERE roadmap_id=$1 AND artifact_type_id=$2',
461 array ($this->roadmap_id,
466 $this->data_array[$artifact_type_id] = 0;
470 private function _setList($artifact_type_id, $field_id) {
472 $result = $this->_deleteList($artifact_type_id);
474 elseif (array_key_exists($artifact_type_id, $this->data_array)) {
475 $result = $this->_updateList($artifact_type_id, $field_id);
478 $result = $this->_insertList($artifact_type_id, $field_id);
484 private function _isAdmin() {
485 if (isset($this->is_admin)) {
486 if ($this->is_admin) {
490 $this->setPermissionDeniedError();
494 $perm =& $this->group->getPermission();
496 if (!$perm || !is_object($perm) || !$perm->isArtifactAdmin()) {
497 $this->setPermissionDeniedError();