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 Error {
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.
106 function create($name,$status_id=0) {
110 if (trim($name) == '') {
111 $this->setError(_('an element name is required'));
122 if (!forge_check_perm ('tracker_admin', $this->ArtifactExtraField->ArtifactType->Group->getID())) {
123 $this->setPermissionDeniedError();
126 $res = db_query_params ('SELECT element_name FROM artifact_extra_field_elements WHERE element_name=$1 AND extra_field_id=$2',
127 array (htmlspecialchars ($name),
128 $this->ArtifactExtraField->getID())) ;
129 if (db_numrows($res) > 0) {
130 $this->setError(_('Element name already exists'));
134 $result = db_query_params ('INSERT INTO artifact_extra_field_elements (extra_field_id,element_name,status_id) VALUES ($1,$2,$3)',
135 array ($this->ArtifactExtraField->getID(),
136 htmlspecialchars($name),
138 if ($result && db_affected_rows($result) > 0) {
140 $id=db_insertid($result,'artifact_extra_field_elements','element_id');
142 // Now set up our internal data structures
144 if (!$this->fetchData($id)) {
148 // If new element belongs to Status custom field, then register the new element in the workflow.
149 if ($this->ArtifactExtraField->getType() == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
150 $atw = new ArtifactWorkflow($this->ArtifactExtraField->ArtifactType, $this->ArtifactExtraField->getID());
158 $this->setError(db_error());
166 * fetchData - re-fetch the data for this ArtifactExtraFieldElement from the database.
168 * @param int $id ID of the Box.
169 * @return boolean success.
171 function fetchData($id) {
172 $res = db_query_params ('SELECT * FROM artifact_extra_field_elements WHERE element_id=$1',
174 if (!$res || db_numrows($res) < 1) {
175 $this->setError('ArtifactExtraField: Invalid ArtifactExtraFieldElement ID');
178 $this->data_array = db_fetch_array($res);
179 db_free_result($res);
184 * getArtifactExtraField - get the ArtifactExtraField Object this ArtifactExtraField is associated with.
186 * @return object ArtifactExtraField.
188 function &getArtifactExtraField() {
189 return $this->ArtifactExtraField;
193 * getID - get this ArtifactExtraField ID.
195 * @return int The id #.
198 return $this->data_array['element_id'];
202 * getBoxID - get this artifact box id.
204 * @return int The id #.
206 function getBoxID() {
207 return $this->data_array['extra_field_id'];
211 * getName - get the name.
213 * @return string The name.
216 return $this->data_array['element_name'];
220 * getStatus - the status equivalent of this field (open or closed).
222 * @return int status.
224 function getStatusID() {
225 return $this->data_array['status_id'];
229 * update - update rows in the table used to store the choices
230 * for a selection box. This function is used only for extra
231 * boxes and fields configured by the admin
233 * @param string $name Name of the choice in a box.
234 * @param int $status_id optional for status box - maps to either open/closed.
235 * @return bool success.
237 function update($name,$status_id=0) {
238 if (!forge_check_perm ('tracker_admin', $this->ArtifactExtraField->ArtifactType->Group->getID())) {
239 $this->setPermissionDeniedError();
242 if (trim($name) == '') {
243 $this->setMissingParamsError();
246 $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',
248 $this->ArtifactExtraField->getID(),
250 if (db_numrows($res) > 0) {
251 $this->setError(_('Element name already exists'));
262 $result = db_query_params ('UPDATE artifact_extra_field_elements
263 SET element_name=$1, status_id=$2
264 WHERE element_id=$3',
265 array (htmlspecialchars($name),
268 if ($result && db_affected_rows($result) > 0) {
271 $this->setError(db_error());
277 * delete - delete the current value.
279 * @return boolean success.
282 if (!forge_check_perm ('tracker_admin', $this->ArtifactExtraField->ArtifactType->Group->getID())) {
283 $this->setPermissionDeniedError();
286 $res = db_query_params ('SELECT element_id FROM artifact_extra_field_elements WHERE element_id=$1',
287 array ($this->getID()));
288 if (db_numrows($res) != 1) {
289 $this->setError('ArtifactExtraField: Invalid ArtifactExtraFieldElement ID');
293 // Reset all artifacts to 100 before removing the value.
294 $ef=$this->getArtifactExtraField();
295 db_query_params ('UPDATE artifact_extra_field_data SET field_data=100 WHERE field_data=$1 AND extra_field_id=$2',
296 array ($this->getID(),
299 $result = db_query_params ('DELETE FROM artifact_extra_field_elements WHERE element_id=$1',
300 array ($this->getID())) ;
301 if (! $result || ! db_affected_rows($result)) {
302 $this->setError(db_error());
305 db_query_params ('DELETE FROM artifact_workflow_event WHERE from_value_id=$1 OR to_value_id=$1',
306 array ($this->getID())) ;
313 // c-file-style: "bsd"