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);
37 /* reserved for aljeux extension, for merge into FusionForge */
38 define('ARTIFACT_EXTRAFIELDTYPE_FORMULA',11);
39 /* reserved for Evolvis extension, for merge into FusionForge */
40 define('ARTIFACT_EXTRAFIELDTYPE_DATETIME',12);
42 class ArtifactExtraField extends Error {
45 * The artifact type object.
47 * @var object $ArtifactType.
49 var $ArtifactType; //object
52 * Array of artifact data.
54 * @var array $data_array.
59 * __construct - Constructor
60 * @param $ArtifactType
63 function __construct(&$ArtifactType, $data=false) {
66 //was ArtifactType legit?
67 if (!$ArtifactType || !is_object($ArtifactType)) {
68 $this->setError(_('Invalid Artifact Type'));
71 //did ArtifactType have an error?
72 if ($ArtifactType->isError()) {
73 $this->setError('ArtifactExtraField: '.$ArtifactType->getErrorMessage());
76 $this->ArtifactType =& $ArtifactType;
79 if (is_array($data)) {
80 $this->data_array =& $data;
82 $this->fetchData($data);
88 * create - create a row in the table that stores box names for a
89 * a tracker. This function is only used to create rows for boxes
90 * configured by the admin.
92 * @param string $name Name of the extra field.
93 * @param int $field_type The type of field - radio, select, text, textarea
94 * @param int $attribute1 For text (size) and textarea (rows)
95 * @param int $attribute2 For text (maxlength) and textarea (cols)
96 * @param int $is_required True or false whether this is a required field or not.
97 * @param string $alias Alias for this extra field (optional)
98 * @return bool true on success / false on failure.
100 function create($name,$field_type,$attribute1,$attribute2,$is_required=0,$alias='') {
105 $this->setError(_('a field name is required'));
109 $this->setError(_('Type of custom field not selected'));
112 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
113 $this->setPermissionDeniedError();
117 $res = db_query_params ('SELECT field_name FROM artifact_extra_field_list WHERE field_name=$1 AND group_artifact_id=$2',
119 $this->ArtifactType->getID()));
120 if (db_numrows($res) > 0) {
121 $this->setError(_('Field name already exists'));
124 if ($field_type == ARTIFACT_EXTRAFIELDTYPE_TEXT || $field_type == ARTIFACT_EXTRAFIELDTYPE_INTEGER) {
125 if (!$attribute1 || !$attribute2 || $attribute2 < $attribute1) {
126 $this->setError(_('Invalid size/maxlength for text field'));
130 if ($field_type == ARTIFACT_EXTRAFIELDTYPE_TEXTAREA) {
131 if (!$attribute1 || !$attribute2) {
132 $this->setError(_('Invalid rows/cols for textarea field'));
135 } elseif ($field_type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
136 if ($this->ArtifactType->getCustomStatusField()) {
137 $this->setError(_('This Tracker already uses custom statuses'));
148 if (!($alias = $this->generateAlias($alias,$name))) {
153 $result = db_query_params ('INSERT INTO artifact_extra_field_list (group_artifact_id,field_name,field_type,attribute1,attribute2,is_required,alias)
154 VALUES ($1,$2,$3,$4,$5,$6,$7)',
155 array ($this->ArtifactType->getID(),
156 htmlspecialchars($name),
163 if ($result && db_affected_rows($result) > 0) {
165 $id=db_insertid($result,'artifact_extra_field_list','extra_field_id');
167 // Now set up our internal data structures
169 if (!$this->fetchData($id)) {
173 if ($field_type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
174 if (!$this->ArtifactType->setCustomStatusField($id)) {
179 // Must insert some default statuses for each artifact
181 $ao = new ArtifactExtraFieldElement($this);
182 if (!$ao || !is_object($ao)) {
183 $feedback .= 'Unable to create ArtifactExtraFieldElement Object';
187 if (!$ao->create('Open', '1')) {
188 $feedback .= _('Insert Error')._(': ').$ao->getErrorMessage();
193 if (!$ao->create('Closed', '2')) {
194 $feedback .= _('Insert Error')._(': ').$ao->getErrorMessage();
201 } elseif (strstr(ARTIFACT_EXTRAFIELD_FILTER_INT,$field_type) !== false) {
203 // Must insert some default 100 rows for the data table so None queries will work right
205 $resdefault = db_query_params ('INSERT INTO artifact_extra_field_data(artifact_id,field_data,extra_field_id)
206 SELECT artifact_id,100,$1 FROM artifact WHERE group_artifact_id=$2',
208 $this->ArtifactType->getID())) ;
216 $this->setError(db_error());
223 * fetchData - re-fetch the data for this ArtifactExtraField from the database.
225 * @param int $id ID of the Box.
226 * @return boolean success.
228 function fetchData($id) {
230 $res = db_query_params ('SELECT * FROM artifact_extra_field_list WHERE extra_field_id=$1',
233 if (!$res || db_numrows($res) < 1) {
234 $this->setError(_('Invalid ArtifactExtraField ID'));
237 $this->data_array = db_fetch_array($res);
238 db_free_result($res);
243 * getArtifactType - get the ArtifactType Object this ArtifactExtraField is associated with.
245 * @return object ArtifactType.
247 function &getArtifactType() {
248 return $this->ArtifactType;
252 * getID - get this ArtifactExtraField ID.
254 * @return int The id #.
257 return $this->data_array['extra_field_id'];
261 * getName - get the name.
263 * @return string The name.
266 return $this->data_array['field_name'];
270 * getAttribute1 - get the attribute1 field.
272 * @return int The first attribute.
274 function getAttribute1() {
275 return $this->data_array['attribute1'];
279 * getAttribute2 - get the attribute2 field.
281 * @return int The second attribute.
283 function getAttribute2() {
284 return $this->data_array['attribute2'];
288 * getType - the type of field.
293 return $this->data_array['field_type'];
297 * getTypeName - the name of type of field.
299 * @return string type.
301 function getTypeName() {
302 $arr = $this->getAvailableTypes();
303 return $arr[$this->data_array['field_type']];
307 * isRequired - whether this field is required or not.
309 * @return boolean required.
311 function isRequired() {
312 return $this->data_array['is_required'];
316 * getAvailableTypes - the types of text fields and their names available.
318 * @return array types.
320 static function getAvailableTypes() {
322 ARTIFACT_EXTRAFIELDTYPE_SELECT => _('Select Box'),
323 ARTIFACT_EXTRAFIELDTYPE_CHECKBOX => _('Check Box'),
324 ARTIFACT_EXTRAFIELDTYPE_RADIO => _('Radio Buttons'),
325 ARTIFACT_EXTRAFIELDTYPE_TEXT => _('Text Field'),
326 ARTIFACT_EXTRAFIELDTYPE_MULTISELECT => _('Multi-Select Box'),
327 ARTIFACT_EXTRAFIELDTYPE_TEXTAREA => _('Text Area'),
328 ARTIFACT_EXTRAFIELDTYPE_STATUS => _('Status'),
329 ARTIFACT_EXTRAFIELDTYPE_RELATION => _('Relation between artifacts'),
330 ARTIFACT_EXTRAFIELDTYPE_INTEGER => _('Integer')
335 * getAlias - the alias that is used for this field
337 * @return string alias
339 function getAlias() {
340 return $this->data_array['alias'];
344 * getAvailableValues - Get the list of available values for this extra field
348 function getAvailableValues() {
349 $res = db_query_params ('SELECT * FROM artifact_extra_field_elements WHERE extra_field_id=$1',
350 array ($this->getID()));
352 while ($row = db_fetch_array($res)) {
359 * update - update a row in the table used to store box names
360 * for a tracker. This function is only to update rowsf
361 * for boxes configured by
364 * @param string $name Name of the field.
365 * @param int $attribute1 For text (size) and textarea (rows)
366 * @param int $attribute2 For text (maxlength) and textarea (cols)
367 * @param int $is_required True or false whether this is a required field or not.
368 * @param string $alias Alias for this field
369 * @return bool success.
371 function update($name,$attribute1,$attribute2,$is_required=0,$alias="") {
372 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
373 $this->setPermissionDeniedError();
380 $this->setError(_('a field name is required'));
383 $res = db_query_params ('SELECT field_name FROM artifact_extra_field_list
384 WHERE field_name=$1 AND group_artifact_id=$2 AND extra_field_id !=$3',
386 $this->ArtifactType->getID(),
388 if (db_numrows($res) > 0) {
389 $this->setError(_('Field name already exists'));
398 if (!($alias = $this->generateAlias($alias,$name))) {
402 $result = db_query_params ('UPDATE artifact_extra_field_list
408 WHERE extra_field_id=$6
409 AND group_artifact_id=$7',
410 array (htmlspecialchars($name),
416 $this->ArtifactType->getID())) ;
417 if ($result && db_affected_rows($result) > 0) {
420 $this->setError(db_error());
429 function delete($sure, $really_sure) {
430 if (!$sure || !$really_sure) {
431 $this->setMissingParamsError(_('Please tick all checkboxes.'));
434 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
435 $this->setPermissionDeniedError();
439 $result = db_query_params ('DELETE FROM artifact_extra_field_data WHERE extra_field_id=$1',
440 array ($this->getID())) ;
442 $result = db_query_params ('DELETE FROM artifact_extra_field_elements WHERE extra_field_id=$1',
443 array ($this->getID())) ;
445 $result = db_query_params ('DELETE FROM artifact_extra_field_list WHERE extra_field_id=$1',
446 array ($this->getID())) ;
448 if ($this->getType() == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
449 if (!$this->ArtifactType->setCustomStatusField(0)) {
457 $this->setError(db_error());
462 $this->setError(db_error());
467 $this->setError(db_error());
476 * Note that this function does not check for conflicts.
478 * @param string alias - alias to validate
479 * @return bool true if alias is valid, false otherwise and it sets the corresponding error
481 function validateAlias($alias) {
482 // these are reserved alias names
483 static $reserved_alias = array(
495 if (strlen($alias) == 0) return true; // empty alias
498 if (preg_match("/[^[:alnum:]_@\\-]/", $alias)) {
499 $this->setError(_('The alias contains invalid characters. Only letters, numbers, hypens (-), at sign (@) and underscores (_) allowed.'));
501 } elseif (in_array($alias, $reserved_alias)) { // alias is reserved?
502 $this->setError(sprintf(_('ā%sā is a reserved alias. Please provide another name.'), $alias));
510 * Generate an alias for this field. The alias can be entered by the user or
511 * be generated automatically from the name of the field.
513 * @param string $alias Alias entered by the user
514 * @param string $name Name of the field entered by the user (it'll be used when $alias is empty)
517 function generateAlias($alias, $name) {
518 $alias = strtolower(trim($alias));
519 if (strlen($alias) == 0) { // no alias was entered, generate alias from $name
520 $name = strtolower(trim($name));
521 // Convert the original name to a valid alias (i.e., if the extra field is
522 // called "Quality test", make an alias called "quality_test").
523 // The alias can be seen as a "unix name" for this field
524 $alias = preg_replace("/ /", "_", $name);
525 $alias = preg_replace("/[^[:alnum:]_@]/", "", $alias);
526 $alias = strtolower($alias);
527 } elseif (!$this->validateAlias($alias)) {
528 // alias is invalid...
531 // check if the name conflicts with another alias in the same artifact type
532 // in that case append a serial number to the alias
536 if ($this->data_array['extra_field_id']) {
537 $res = db_query_params ('SELECT * FROM artifact_extra_field_list
538 WHERE LOWER (alias)=$1
539 AND group_artifact_id=$2
540 AND extra_field_id <> $3',
542 $this->ArtifactType->getID(),
543 $this->data_array['extra_field_id'])) ;
545 $res = db_query_params ('SELECT * FROM artifact_extra_field_list WHERE LOWER (alias)=$1 AND group_artifact_id=$2',
547 $this->ArtifactType->getID()));
550 $this->setError(db_error());
552 } elseif (db_numrows($res) > 0) { // found another field with the same alias
555 $alias = $alias.$serial;
561 // at this point, the alias is valid and unique
565 function updateOrder($element_id, $order) {
567 $result=db_query_params ('UPDATE artifact_extra_field_elements
569 WHERE element_id=$2',
572 if ($result && db_affected_rows($result) > 0) {
576 $this->setError(db_error());
581 function reorderValues($element_id, $new_pos) {
583 $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',
584 array($this->getID()));
585 $max = db_numrows($res);
586 if ($new_pos < 1 || $new_pos > $max) {
587 $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"