5 * Copyright 2004, Anthony J. Pugliese
6 * Copyright 2009, Roland Mas
7 * Copyright 2009, Alcatel-Lucent
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.
26 * Standard Alcatel-Lucent disclaimer for contributing to open source
28 * "The Artifact ("Contribution") has not been tested and/or
29 * validated for release as or in products, combinations with products or
30 * other commercial use. Any use of the Contribution is entirely made at
31 * the user's own responsibility and the user can not rely on any features,
32 * functionalities or performances Alcatel-Lucent has attributed to the
35 * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
36 * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
37 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
38 * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
39 * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
40 * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
41 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
42 * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
43 * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
47 require_once $gfcommon.'include/Error.class.php';
48 require_once $gfcommon.'tracker/ArtifactWorkflow.class.php';
50 class ArtifactExtraFieldElement extends FFError {
53 * The artifact type object.
55 * @var object $ArtifactExtraField.
57 var $ArtifactExtraField; //object
60 * Array of artifact data.
62 * @var array $data_array.
67 * __construct - Constructor
69 * @param object $ArtifactExtraField ArtifactExtraField object.
70 * @param array|bool $data (all fields from artifact_file_user_vw) OR id from database.
72 function __construct(&$ArtifactExtraField,$data=false) {
75 // Was ArtifactExtraField legit?
76 if (!$ArtifactExtraField || !is_object($ArtifactExtraField)) {
77 $this->setError('ArtifactExtraField: No Valid ArtifactExtraField');
80 // Did ArtifactExtraField have an error?
81 if ($ArtifactExtraField->isError()) {
82 $this->setError('ArtifactExtraField: '.$ArtifactExtraField->getErrorMessage());
85 $this->ArtifactExtraField =& $ArtifactExtraField;
87 if (is_array($data)) {
88 //TODO validate that data actually belongs in this ArtifactExtraField
89 $this->data_array =& $data;
91 $this->fetchData($data);
97 * create - create a new row in the table used to store the
98 * choices for selection boxes. This function is only used for
99 * extra fields and boxes configured by the admin
101 * @param string $name Name of the choice
102 * @param int $status_id Id the box that contains the choice (optional).
103 * @return bool true on success / false on failure.
105 function create($name,$status_id=0) {
109 if (trim($name) == '') {
110 $this->setError(_('an element name is required'));
121 if (!forge_check_perm ('tracker_admin', $this->ArtifactExtraField->ArtifactType->Group->getID())) {
122 $this->setPermissionDeniedError();
125 $res = db_query_params ('SELECT element_name FROM artifact_extra_field_elements WHERE element_name=$1 AND extra_field_id=$2',
126 array (htmlspecialchars ($name),
127 $this->ArtifactExtraField->getID())) ;
128 if (db_numrows($res) > 0) {
129 $this->setError(_('Element name already exists'));
133 $result = db_query_params ('INSERT INTO artifact_extra_field_elements (extra_field_id,element_name,status_id) VALUES ($1,$2,$3)',
134 array ($this->ArtifactExtraField->getID(),
135 htmlspecialchars($name),
137 if ($result && db_affected_rows($result) > 0) {
139 $id=db_insertid($result,'artifact_extra_field_elements','element_id');
141 // Now set up our internal data structures
143 if (!$this->fetchData($id)) {
147 // If new element belongs to Status custom field, then register the new element in the workflow.
148 if ($this->ArtifactExtraField->getType() == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
149 $atw = new ArtifactWorkflow($this->ArtifactExtraField->ArtifactType, $this->ArtifactExtraField->getID());
157 $this->setError(db_error());
164 * fetchData - re-fetch the data for this ArtifactExtraFieldElement from the database.
166 * @param int $id ID of the Box.
167 * @return boolean success.
169 function fetchData($id) {
170 $res = db_query_params ('SELECT * FROM artifact_extra_field_elements WHERE element_id=$1',
172 if (!$res || db_numrows($res) < 1) {
173 $this->setError('ArtifactExtraField: Invalid ArtifactExtraFieldElement ID');
176 $this->data_array = db_fetch_array($res);
177 db_free_result($res);
182 * getArtifactExtraField - get the ArtifactExtraField Object this ArtifactExtraField is associated with.
184 * @return object ArtifactExtraField.
186 function &getArtifactExtraField() {
187 return $this->ArtifactExtraField;
191 * getID - get this ArtifactExtraField ID.
193 * @return int The id #.
196 return $this->data_array['element_id'];
200 * getBoxID - get this artifact box id.
202 * @return int The id #.
204 function getBoxID() {
205 return $this->data_array['extra_field_id'];
209 * getName - get the name.
211 * @return string The name.
214 return $this->data_array['element_name'];
218 * getStatus - the status equivalent of this field (open or closed).
220 * @return int status.
222 function getStatusID() {
223 return $this->data_array['status_id'];
227 * update - update rows in the table used to store the choices
228 * for a selection box. This function is used only for extra
229 * boxes and fields configured by the admin
231 * @param string $name Name of the choice in a box.
232 * @param int $status_id optional for status box - maps to either open/closed.
233 * @return bool success.
235 function update($name,$status_id=0) {
236 if (!forge_check_perm ('tracker_admin', $this->ArtifactExtraField->ArtifactType->Group->getID())) {
237 $this->setPermissionDeniedError();
240 if (trim($name) == '') {
241 $this->setMissingParamsError();
244 $res = db_query_params ('SELECT element_name FROM artifact_extra_field_elements WHERE element_name=$1 AND extra_field_id=$2 AND element_id != $3',
246 $this->ArtifactExtraField->getID(),
248 if (db_numrows($res) > 0) {
249 $this->setError(_('Element name already exists'));
260 $result = db_query_params ('UPDATE artifact_extra_field_elements
261 SET element_name=$1, status_id=$2
262 WHERE element_id=$3',
263 array (htmlspecialchars($name),
266 if ($result && db_affected_rows($result) > 0) {
269 $this->setError(db_error());
275 * delete - delete the current value.
277 * @return boolean success.
280 if (!forge_check_perm ('tracker_admin', $this->ArtifactExtraField->ArtifactType->Group->getID())) {
281 $this->setPermissionDeniedError();
284 $res = db_query_params ('SELECT element_id FROM artifact_extra_field_elements WHERE element_id=$1',
285 array ($this->getID()));
286 if (db_numrows($res) != 1) {
287 $this->setError('ArtifactExtraField: Invalid ArtifactExtraFieldElement ID');
291 // Reset all artifacts to 100 before removing the value.
292 $ef=$this->getArtifactExtraField();
293 db_query_params ('UPDATE artifact_extra_field_data SET field_data=100 WHERE field_data=$1 AND extra_field_id=$2',
294 array ($this->getID(),
297 $result = db_query_params ('DELETE FROM artifact_extra_field_elements WHERE element_id=$1',
298 array ($this->getID())) ;
299 if (! $result || ! db_affected_rows($result)) {
300 $this->setError(db_error());
303 db_query_params ('DELETE FROM artifact_workflow_event WHERE from_value_id=$1 OR to_value_id=$1',
304 array ($this->getID())) ;
311 // c-file-style: "bsd"