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'));
138 } elseif ($field_type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
139 if ($this->ArtifactType->getCustomStatusField()) {
140 $this->setError(_('This Tracker already uses custom statuses'));
151 if (!($alias = $this->generateAlias($alias,$name))) {
156 $result = db_query_params ('INSERT INTO artifact_extra_field_list (group_artifact_id,field_name,field_type,attribute1,attribute2,is_required,alias)
157 VALUES ($1,$2,$3,$4,$5,$6,$7)',
158 array ($this->ArtifactType->getID(),
159 htmlspecialchars($name),
166 if ($result && db_affected_rows($result) > 0) {
168 $id=db_insertid($result,'artifact_extra_field_list','extra_field_id');
170 // Now set up our internal data structures
172 if (!$this->fetchData($id)) {
176 if ($field_type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
177 if (!$this->ArtifactType->setCustomStatusField($id)) {
182 // Must insert some default statuses for each artifact
184 $ao = new ArtifactExtraFieldElement($this);
185 if (!$ao || !is_object($ao)) {
186 $feedback .= 'Unable to create ArtifactExtraFieldElement Object';
190 if (!$ao->create('Open', '1')) {
191 $feedback .= _('Error inserting an element').': '.$ao->getErrorMessage();
196 if (!$ao->create('Closed', '2')) {
197 $feedback .= _('Error inserting an element').': '.$ao->getErrorMessage();
204 } elseif (strstr(ARTIFACT_EXTRAFIELD_FILTER_INT,$field_type) !== false) {
206 // Must insert some default 100 rows for the data table so None queries will work right
208 $resdefault = db_query_params ('INSERT INTO artifact_extra_field_data(artifact_id,field_data,extra_field_id)
209 SELECT artifact_id,100,$1 FROM artifact WHERE group_artifact_id=$2',
211 $this->ArtifactType->getID())) ;
219 $this->setError(db_error());
226 * fetchData - re-fetch the data for this ArtifactExtraField from the database.
228 * @param int ID of the Box.
229 * @return boolean success.
231 function fetchData($id) {
233 $res = db_query_params ('SELECT * FROM artifact_extra_field_list WHERE extra_field_id=$1',
236 if (!$res || db_numrows($res) < 1) {
237 $this->setError('ArtifactExtraField: Invalid ArtifactExtraField ID');
240 $this->data_array = db_fetch_array($res);
241 db_free_result($res);
246 * getArtifactType - get the ArtifactType Object this ArtifactExtraField is associated with.
248 * @return object ArtifactType.
250 function &getArtifactType() {
251 return $this->ArtifactType;
255 * getID - get this ArtifactExtraField ID.
257 * @return int The id #.
260 return $this->data_array['extra_field_id'];
264 * getName - get the name.
266 * @return string The name.
269 return $this->data_array['field_name'];
273 * getAttribute1 - get the attribute1 field.
275 * @return int The first attribute.
277 function getAttribute1() {
278 return $this->data_array['attribute1'];
282 * getAttribute2 - get the attribute2 field.
284 * @return int The second attribute.
286 function getAttribute2() {
287 return $this->data_array['attribute2'];
291 * getType - the type of field.
296 return $this->data_array['field_type'];
300 * getTypeName - the name of type of field.
302 * @return string type.
304 function getTypeName() {
305 $arr = $this->getAvailableTypes();
306 return $arr[$this->data_array['field_type']];
310 * isRequired - whether this field is required or not.
312 * @return boolean required.
314 function isRequired() {
315 return $this->data_array['is_required'];
319 * getAvailableTypes - the types of text fields and their names available.
321 * @return array types.
323 static function getAvailableTypes() {
327 3=>_('Radio Buttons'),
329 5=>_('Multi-Select Box'),
338 * getAlias - the alias that is used for this field
340 * @return string alias
342 function getAlias() {
343 return $this->data_array['alias'];
347 * getAvailableValues - Get the list of available values for this extra field
351 function getAvailableValues() {
352 $res = db_query_params ('SELECT * FROM artifact_extra_field_elements WHERE extra_field_id=$1',
353 array ($this->getID()));
355 while ($row = db_fetch_array($res)) {
362 * update - update a row in the table used to store box names
363 * for a tracker. This function is only to update rowsf
364 * for boxes configured by
367 * @param string Name of the field.
368 * @param int Attribute1 - for text (size) and textarea (rows)
369 * @param int Attribute2 - for text (maxlength) and textarea (cols)
370 * @param int is_required - true or false whether this is a required field or not.
371 * @param string Alias for this field
372 * @return boolean success.
374 function update($name,$attribute1,$attribute2,$is_required=0,$alias="") {
375 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
376 $this->setPermissionDeniedError();
383 $this->setError(_('a field name is required'));
386 $res = db_query_params ('SELECT field_name FROM artifact_extra_field_list
387 WHERE field_name=$1 AND group_artifact_id=$2 AND extra_field_id !=$3',
389 $this->ArtifactType->getID(),
391 if (db_numrows($res) > 0) {
392 $this->setError(_('Field name already exists'));
401 if (!($alias = $this->generateAlias($alias,$name))) {
405 $result = db_query_params ('UPDATE artifact_extra_field_list
411 WHERE extra_field_id=$6
412 AND group_artifact_id=$7',
413 array (htmlspecialchars($name),
419 $this->ArtifactType->getID())) ;
420 if ($result && db_affected_rows($result) > 0) {
423 $this->setError(db_error());
432 function delete($sure, $really_sure) {
433 if (!$sure || !$really_sure) {
434 $this->setMissingParamsError(_('Please tick all checkboxes.'));
437 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
438 $this->setPermissionDeniedError();
442 $result = db_query_params ('DELETE FROM artifact_extra_field_data WHERE extra_field_id=$1',
443 array ($this->getID())) ;
445 $result = db_query_params ('DELETE FROM artifact_extra_field_elements WHERE extra_field_id=$1',
446 array ($this->getID())) ;
448 $result = db_query_params ('DELETE FROM artifact_extra_field_list WHERE extra_field_id=$1',
449 array ($this->getID())) ;
451 if ($this->getType() == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
452 if (!$this->ArtifactType->setCustomStatusField(0)) {
460 $this->setError(db_error());
465 $this->setError(db_error());
470 $this->setError(db_error());
479 * Note that this function does not check for conflicts.
480 * @param string alias - alias to validate
481 * @return bool true if alias is valid, false otherwise and it sets the corresponding error
483 function validateAlias($alias) {
484 // these are reserved alias names
485 static $reserved_alias = array(
497 if (strlen($alias) == 0) return true; // empty alias
500 if (preg_match("/[^[:alnum:]_@\\-]/", $alias)) {
501 $this->setError(_('The alias contains invalid characters. Only letters, numbers, hypens (-), at sign (@) and underscores (_) allowed.'));
503 } else if (in_array($alias, $reserved_alias)) { // alias is reserved?
504 $this->setError(sprintf(_('\'%1$s\' is a reserved alias. Please provide another name.'), $alias));
512 * Generate an alias for this field. The alias can be entered by the user or
513 * be generated automatically from the name of the field.
514 * @param string Alias entered by the user
515 * @param string Name of the field entered by the user (it'll be used when $alias is empty)
518 function generateAlias($alias, $name) {
519 $alias = strtolower(trim($alias));
520 if (strlen($alias) == 0) { // no alias was entered, generate alias from $name
521 $name = strtolower(trim($name));
522 // Convert the original name to a valid alias (i.e., if the extra field is
523 // called "Quality test", make an alias called "quality_test").
524 // The alias can be seen as a "unix name" for this field
525 $alias = preg_replace("/ /", "_", $name);
526 $alias = preg_replace("/[^[:alnum:]_@]/", "", $alias);
527 $alias = strtolower($alias);
528 } elseif (!$this->validateAlias($alias)) {
529 // alias is invalid...
532 // check if the name conflicts with another alias in the same artifact type
533 // in that case append a serial number to the alias
537 if ($this->data_array['extra_field_id']) {
538 $res = db_query_params ('SELECT * FROM artifact_extra_field_list
539 WHERE LOWER (alias)=$1
540 AND group_artifact_id=$2
541 AND extra_field_id <> $3',
543 $this->ArtifactType->getID(),
544 $this->data_array['extra_field_id'])) ;
546 $res = db_query_params ('SELECT * FROM artifact_extra_field_list WHERE LOWER (alias)=$1 AND group_artifact_id=$2',
548 $this->ArtifactType->getID()));
551 $this->setError(db_error());
553 } else if (db_numrows($res) > 0) { // found another field with the same alias
556 $alias = $alias.$serial;
562 // at this point, the alias is valid and unique
566 function updateOrder($element_id, $order) {
568 $result=db_query_params ('UPDATE artifact_extra_field_elements
570 WHERE element_id=$2',
573 if ($result && db_affected_rows($result) > 0) {
577 $this->setError(db_error());
582 function reorderValues($element_id, $new_pos) {
584 $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',
585 array($this->getID()));
586 $max = db_numrows($res);
587 if ($new_pos < 1 || $new_pos > $max) {
588 $this->setError(_('Out of range value'));
593 if ($i == $new_pos) {
594 $data[] = $element_id;
597 if (($row = db_fetch_array($res)) && $row['element_id'] != $element_id) {
598 $data[] = $row['element_id'];
602 for ($i = 0; $i < count($data); $i++) {
603 if (! $this->updateOrder($data[$i], $i + 1))
610 function alphaorderValues() {
612 $res = db_query_params ('SELECT element_id FROM artifact_extra_field_elements WHERE extra_field_id=$1 ORDER BY element_name ASC',
613 array($this->getID()));
615 while ($row = db_fetch_array($res)) {
616 if (! $this->updateOrder($row['element_id'], $i))
628 // c-file-style: "bsd"