5 * Copyright 2004, Anthony J. Pugliese
6 * Copyright 2009, Roland Mas
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.
24 require_once $gfcommon.'include/Error.class.php';
26 define('ARTIFACT_EXTRAFIELD_FILTER_INT','1,2,3,5,7');
27 define('ARTIFACT_EXTRAFIELDTYPE_SELECT',1);
28 define('ARTIFACT_EXTRAFIELDTYPE_CHECKBOX',2);
29 define('ARTIFACT_EXTRAFIELDTYPE_RADIO',3);
30 define('ARTIFACT_EXTRAFIELDTYPE_TEXT',4);
31 define('ARTIFACT_EXTRAFIELDTYPE_MULTISELECT',5);
32 define('ARTIFACT_EXTRAFIELDTYPE_TEXTAREA',6);
33 define('ARTIFACT_EXTRAFIELDTYPE_STATUS',7);
34 //define('ARTIFACT_EXTRAFIELDTYPE_ASSIGNEE',8);
35 define('ARTIFACT_EXTRAFIELDTYPE_RELATION',9);
36 define('ARTIFACT_EXTRAFIELDTYPE_INTEGER',10);
38 class ArtifactExtraField extends Error {
41 * The artifact type object.
43 * @var object $ArtifactType.
45 var $ArtifactType; //object
48 * Array of artifact data.
50 * @var array $data_array.
55 * ArtifactExtraField - Constructer
57 * @param object ArtifactType object.
58 * @param array (all fields from artifact_file_user_vw) OR id from database.
59 * @return boolean success.
61 function __construct(&$ArtifactType, $data=false) {
64 //was ArtifactType legit?
65 if (!$ArtifactType || !is_object($ArtifactType)) {
66 $this->setError('ArtifactExtraField: No Valid ArtifactType');
69 //did ArtifactType have an error?
70 if ($ArtifactType->isError()) {
71 $this->setError('ArtifactExtraField: '.$ArtifactType->getErrorMessage());
74 $this->ArtifactType =& $ArtifactType;
77 if (is_array($data)) {
78 $this->data_array =& $data;
81 if (!$this->fetchData($data)) {
91 * create - create a row in the table that stores box names for a
92 * a tracker. This function is only used to create rows for boxes
93 * configured by the admin.
95 * @param string Name of the extra field.
96 * @param int The type of field - radio, select, text, textarea
97 * @param int Attribute1 - for text (size) and textarea (rows)
98 * @param int Attribute2 - for text (maxlength) and textarea (cols)
99 * @param int is_required - true or false whether this is a required field or not.
100 * @param string alias - alias for this extra field (optional)
101 * @return true on success / false on failure.
103 function create($name,$field_type,$attribute1,$attribute2,$is_required=0,$alias='') {
108 $this->setError(_('a field name is required'));
112 $this->setError("Type of custom field not selected");
115 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
116 $this->setPermissionDeniedError();
120 $res = db_query_params ('SELECT field_name FROM artifact_extra_field_list WHERE field_name=$1 AND group_artifact_id=$2',
122 $this->ArtifactType->getID()));
123 if (db_numrows($res) > 0) {
124 $this->setError(_('Field name already exists'));
127 if ($field_type == ARTIFACT_EXTRAFIELDTYPE_TEXT || $field_type == ARTIFACT_EXTRAFIELDTYPE_INTEGER) {
128 if (!$attribute1 || !$attribute2 || $attribute2 < $attribute1) {
129 $this->setError("Invalid size/maxlength for text field");
133 if ($field_type == ARTIFACT_EXTRAFIELDTYPE_TEXTAREA) {
134 if (!$attribute1 || !$attribute2) {
135 $this->setError("Invalid rows/cols for textarea field");
146 if (!($alias = $this->generateAlias($alias,$name))) {
151 $result = db_query_params ('INSERT INTO artifact_extra_field_list (group_artifact_id,field_name,field_type,attribute1,attribute2,is_required,alias)
152 VALUES ($1,$2,$3,$4,$5,$6,$7)',
153 array ($this->ArtifactType->getID(),
154 htmlspecialchars($name),
161 if ($result && db_affected_rows($result) > 0) {
163 $id=db_insertid($result,'artifact_extra_field_list','extra_field_id');
165 // Now set up our internal data structures
167 if (!$this->fetchData($id)) {
171 if ($field_type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
172 if (!$this->ArtifactType->setCustomStatusField($id)) {
177 // Must insert some default statuses for each artifact
179 $ao = new ArtifactExtraFieldElement($this);
180 if (!$ao || !is_object($ao)) {
181 $feedback .= 'Unable to create ArtifactExtraFieldElement Object';
185 if (!$ao->create('Open', '1')) {
186 $feedback .= _('Error inserting an element').': '.$ao->getErrorMessage();
191 if (!$ao->create('Closed', '2')) {
192 $feedback .= _('Error inserting an element').': '.$ao->getErrorMessage();
199 } elseif (strstr(ARTIFACT_EXTRAFIELD_FILTER_INT,$field_type) !== false) {
201 // Must insert some default 100 rows for the data table so None queries will work right
203 $resdefault = db_query_params ('INSERT INTO artifact_extra_field_data(artifact_id,field_data,extra_field_id)
204 SELECT artifact_id,100,$1 FROM artifact WHERE group_artifact_id=$2',
206 $this->ArtifactType->getID())) ;
214 $this->setError(db_error());
221 * fetchData - re-fetch the data for this ArtifactExtraField from the database.
223 * @param int ID of the Box.
224 * @return boolean success.
226 function fetchData($id) {
228 $res = db_query_params ('SELECT * FROM artifact_extra_field_list WHERE extra_field_id=$1',
231 if (!$res || db_numrows($res) < 1) {
232 $this->setError('ArtifactExtraField: Invalid ArtifactExtraField ID');
235 $this->data_array = db_fetch_array($res);
236 db_free_result($res);
241 * getArtifactType - get the ArtifactType Object this ArtifactExtraField is associated with.
243 * @return object ArtifactType.
245 function &getArtifactType() {
246 return $this->ArtifactType;
250 * getID - get this ArtifactExtraField ID.
252 * @return int The id #.
255 return $this->data_array['extra_field_id'];
259 * getName - get the name.
261 * @return string The name.
264 return $this->data_array['field_name'];
268 * getAttribute1 - get the attribute1 field.
270 * @return int The first attribute.
272 function getAttribute1() {
273 return $this->data_array['attribute1'];
277 * getAttribute2 - get the attribute2 field.
279 * @return int The second attribute.
281 function getAttribute2() {
282 return $this->data_array['attribute2'];
286 * getType - the type of field.
291 return $this->data_array['field_type'];
295 * getTypeName - the name of type of field.
297 * @return string type.
299 function getTypeName() {
300 $arr = $this->getAvailableTypes();
301 return $arr[$this->data_array['field_type']];
305 * isRequired - whether this field is required or not.
307 * @return boolean required.
309 function isRequired() {
310 return $this->data_array['is_required'];
314 * getAvailableTypes - the types of text fields and their names available.
316 * @return array types.
318 static function getAvailableTypes() {
322 3=>_('Radio Buttons'),
324 5=>_('Multi-Select Box'),
333 * getAlias - the alias that is used for this field
335 * @return string alias
337 function getAlias() {
338 return $this->data_array['alias'];
342 * getAvailableValues - Get the list of available values for this extra field
346 function getAvailableValues() {
347 $res = db_query_params ('SELECT * FROM artifact_extra_field_elements WHERE extra_field_id=$1',
348 array ($this->getID()));
350 while ($row = db_fetch_array($res)) {
357 * update - update a row in the table used to store box names
358 * for a tracker. This function is only to update rowsf
359 * for boxes configured by
362 * @param string Name of the field.
363 * @param int Attribute1 - for text (size) and textarea (rows)
364 * @param int Attribute2 - for text (maxlength) and textarea (cols)
365 * @param int is_required - true or false whether this is a required field or not.
366 * @param string Alias for this field
367 * @return boolean success.
369 function update($name,$attribute1,$attribute2,$is_required=0,$alias="") {
370 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
371 $this->setPermissionDeniedError();
378 $this->setError(_('a field name is required'));
381 $res = db_query_params ('SELECT field_name FROM artifact_extra_field_list
382 WHERE field_name=$1 AND group_artifact_id=$2 AND extra_field_id !=$3',
384 $this->ArtifactType->getID(),
386 if (db_numrows($res) > 0) {
387 $this->setError(_('Field name already exists'));
396 if (!($alias = $this->generateAlias($alias,$name))) {
400 $result = db_query_params ('UPDATE artifact_extra_field_list
406 WHERE extra_field_id=$6
407 AND group_artifact_id=$7',
408 array (htmlspecialchars($name),
414 $this->ArtifactType->getID())) ;
415 if ($result && db_affected_rows($result) > 0) {
418 $this->setError(db_error());
427 function delete($sure, $really_sure) {
428 if (!$sure || !$really_sure) {
429 $this->setMissingParamsError(_('Please tick all checkboxes.'));
432 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
433 $this->setPermissionDeniedError();
437 $result = db_query_params ('DELETE FROM artifact_extra_field_data WHERE extra_field_id=$1',
438 array ($this->getID())) ;
440 $result = db_query_params ('DELETE FROM artifact_extra_field_elements WHERE extra_field_id=$1',
441 array ($this->getID())) ;
443 $result = db_query_params ('DELETE FROM artifact_extra_field_list WHERE extra_field_id=$1',
444 array ($this->getID())) ;
446 if ($this->getType() == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
447 if (!$this->ArtifactType->setCustomStatusField(0)) {
455 $this->setError(db_error());
460 $this->setError(db_error());
465 $this->setError(db_error());
474 * Note that this function does not check for conflicts.
475 * @param string alias - alias to validate
476 * @return bool true if alias is valid, false otherwise and it sets the corresponding error
478 function validateAlias($alias) {
479 // these are reserved alias names
480 static $reserved_alias = array(
492 if (strlen($alias) == 0) return true; // empty alias
495 if (preg_match("/[^[:alnum:]_@\\-]/", $alias)) {
496 $this->setError(_('The alias contains invalid characters. Only letters, numbers, hypens (-), at sign (@) and underscores (_) allowed.'));
498 } else if (in_array($alias, $reserved_alias)) { // alias is reserved?
499 $this->setError(sprintf(_('\'%1$s\' is a reserved alias. Please provide another name.'), $alias));
507 * Generate an alias for this field. The alias can be entered by the user or
508 * be generated automatically from the name of the field.
509 * @param string Alias entered by the user
510 * @param string Name of the field entered by the user (it'll be used when $alias is empty)
513 function generateAlias($alias, $name) {
514 $alias = strtolower(trim($alias));
515 if (strlen($alias) == 0) { // no alias was entered, generate alias from $name
516 $name = strtolower(trim($name));
517 // Convert the original name to a valid alias (i.e., if the extra field is
518 // called "Quality test", make an alias called "quality_test").
519 // The alias can be seen as a "unix name" for this field
520 $alias = preg_replace("/ /", "_", $name);
521 $alias = preg_replace("/[^[:alnum:]_@]/", "", $alias);
522 $alias = strtolower($alias);
523 } elseif (!$this->validateAlias($alias)) {
524 // alias is invalid...
527 // check if the name conflicts with another alias in the same artifact type
528 // in that case append a serial number to the alias
532 if ($this->data_array['extra_field_id']) {
533 $res = db_query_params ('SELECT * FROM artifact_extra_field_list
534 WHERE LOWER (alias)=$1
535 AND group_artifact_id=$2
536 AND extra_field_id <> $3',
538 $this->ArtifactType->getID(),
539 $this->data_array['extra_field_id'])) ;
541 $res = db_query_params ('SELECT * FROM artifact_extra_field_list WHERE LOWER (alias)=$1 AND group_artifact_id=$2',
543 $this->ArtifactType->getID()));
546 $this->setError(db_error());
548 } else if (db_numrows($res) > 0) { // found another field with the same alias
551 $alias = $alias.$serial;
557 // at this point, the alias is valid and unique
561 function updateOrder($element_id, $order) {
563 $result=db_query_params ('UPDATE artifact_extra_field_elements
565 WHERE element_id=$2',
568 if ($result && db_affected_rows($result) > 0) {
572 $this->setError(db_error());
577 function reorderValues($element_id, $new_pos) {
579 $res = db_query_params ('SELECT element_id FROM artifact_extra_field_elements WHERE extra_field_id=$1 ORDER BY element_pos ASC, element_id ASC',
580 array($this->getID()));
581 $max = db_numrows($res);
582 if ($new_pos < 1 || $new_pos > $max) {
583 $this->setError(_('Out of range value'));
588 if ($i == $new_pos) {
589 $data[] = $element_id;
592 if (($row = db_fetch_array($res)) && $row['element_id'] != $element_id) {
593 $data[] = $row['element_id'];
597 for ($i = 0; $i < count($data); $i++) {
598 if (! $this->updateOrder($data[$i], $i + 1))
605 function alphaorderValues() {
607 $res = db_query_params ('SELECT element_id FROM artifact_extra_field_elements WHERE extra_field_id=$1 ORDER BY element_name ASC',
608 array($this->getID()));
610 while ($row = db_fetch_array($res)) {
611 if (! $this->updateOrder($row['element_id'], $i))
623 // c-file-style: "bsd"