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 * @param $ArtifactType
62 function __construct(&$ArtifactType, $data=false) {
65 //was ArtifactType legit?
66 if (!$ArtifactType || !is_object($ArtifactType)) {
67 $this->setError('ArtifactExtraField: No Valid ArtifactType');
70 //did ArtifactType have an error?
71 if ($ArtifactType->isError()) {
72 $this->setError('ArtifactExtraField: '.$ArtifactType->getErrorMessage());
75 $this->ArtifactType =& $ArtifactType;
78 if (is_array($data)) {
79 $this->data_array =& $data;
81 $this->fetchData($data);
87 * create - create a row in the table that stores box names for a
88 * a tracker. This function is only used to create rows for boxes
89 * configured by the admin.
91 * @param string Name of the extra field.
92 * @param int The type of field - radio, select, text, textarea
93 * @param int Attribute1 - for text (size) and textarea (rows)
94 * @param int Attribute2 - for text (maxlength) and textarea (cols)
95 * @param int is_required - true or false whether this is a required field or not.
96 * @param string alias - alias for this extra field (optional)
97 * @return true on success / false on failure.
99 function create($name,$field_type,$attribute1,$attribute2,$is_required=0,$alias='') {
104 $this->setError(_('a field name is required'));
108 $this->setError(_('Type of custom field not selected'));
111 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
112 $this->setPermissionDeniedError();
116 $res = db_query_params ('SELECT field_name FROM artifact_extra_field_list WHERE field_name=$1 AND group_artifact_id=$2',
118 $this->ArtifactType->getID()));
119 if (db_numrows($res) > 0) {
120 $this->setError(_('Field name already exists'));
123 if ($field_type == ARTIFACT_EXTRAFIELDTYPE_TEXT || $field_type == ARTIFACT_EXTRAFIELDTYPE_INTEGER) {
124 if (!$attribute1 || !$attribute2 || $attribute2 < $attribute1) {
125 $this->setError(_('Invalid size/maxlength for text field'));
129 if ($field_type == ARTIFACT_EXTRAFIELDTYPE_TEXTAREA) {
130 if (!$attribute1 || !$attribute2) {
131 $this->setError(_('Invalid rows/cols for textarea field'));
134 } elseif ($field_type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
135 if ($this->ArtifactType->getCustomStatusField()) {
136 $this->setError(_('This Tracker already uses custom statuses'));
147 if (!($alias = $this->generateAlias($alias,$name))) {
152 $result = db_query_params ('INSERT INTO artifact_extra_field_list (group_artifact_id,field_name,field_type,attribute1,attribute2,is_required,alias)
153 VALUES ($1,$2,$3,$4,$5,$6,$7)',
154 array ($this->ArtifactType->getID(),
155 htmlspecialchars($name),
162 if ($result && db_affected_rows($result) > 0) {
164 $id=db_insertid($result,'artifact_extra_field_list','extra_field_id');
166 // Now set up our internal data structures
168 if (!$this->fetchData($id)) {
172 if ($field_type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
173 if (!$this->ArtifactType->setCustomStatusField($id)) {
178 // Must insert some default statuses for each artifact
180 $ao = new ArtifactExtraFieldElement($this);
181 if (!$ao || !is_object($ao)) {
182 $feedback .= 'Unable to create ArtifactExtraFieldElement Object';
186 if (!$ao->create('Open', '1')) {
187 $feedback .= _('Error inserting an element').': '.$ao->getErrorMessage();
192 if (!$ao->create('Closed', '2')) {
193 $feedback .= _('Error inserting an element').': '.$ao->getErrorMessage();
200 } elseif (strstr(ARTIFACT_EXTRAFIELD_FILTER_INT,$field_type) !== false) {
202 // Must insert some default 100 rows for the data table so None queries will work right
204 $resdefault = db_query_params ('INSERT INTO artifact_extra_field_data(artifact_id,field_data,extra_field_id)
205 SELECT artifact_id,100,$1 FROM artifact WHERE group_artifact_id=$2',
207 $this->ArtifactType->getID())) ;
215 $this->setError(db_error());
222 * fetchData - re-fetch the data for this ArtifactExtraField from the database.
224 * @param int $id ID of the Box.
225 * @return boolean success.
227 function fetchData($id) {
229 $res = db_query_params ('SELECT * FROM artifact_extra_field_list WHERE extra_field_id=$1',
232 if (!$res || db_numrows($res) < 1) {
233 $this->setError('ArtifactExtraField: Invalid ArtifactExtraField ID');
236 $this->data_array = db_fetch_array($res);
237 db_free_result($res);
242 * getArtifactType - get the ArtifactType Object this ArtifactExtraField is associated with.
244 * @return object ArtifactType.
246 function &getArtifactType() {
247 return $this->ArtifactType;
251 * getID - get this ArtifactExtraField ID.
253 * @return int The id #.
256 return $this->data_array['extra_field_id'];
260 * getName - get the name.
262 * @return string The name.
265 return $this->data_array['field_name'];
269 * getAttribute1 - get the attribute1 field.
271 * @return int The first attribute.
273 function getAttribute1() {
274 return $this->data_array['attribute1'];
278 * getAttribute2 - get the attribute2 field.
280 * @return int The second attribute.
282 function getAttribute2() {
283 return $this->data_array['attribute2'];
287 * getType - the type of field.
292 return $this->data_array['field_type'];
296 * getTypeName - the name of type of field.
298 * @return string type.
300 function getTypeName() {
301 $arr = $this->getAvailableTypes();
302 return $arr[$this->data_array['field_type']];
306 * isRequired - whether this field is required or not.
308 * @return boolean required.
310 function isRequired() {
311 return $this->data_array['is_required'];
315 * getAvailableTypes - the types of text fields and their names available.
317 * @return array types.
319 static function getAvailableTypes() {
323 3=>_('Radio Buttons'),
325 5=>_('Multi-Select Box'),
334 * getAlias - the alias that is used for this field
336 * @return string alias
338 function getAlias() {
339 return $this->data_array['alias'];
343 * getAvailableValues - Get the list of available values for this extra field
347 function getAvailableValues() {
348 $res = db_query_params ('SELECT * FROM artifact_extra_field_elements WHERE extra_field_id=$1',
349 array ($this->getID()));
351 while ($row = db_fetch_array($res)) {
358 * update - update a row in the table used to store box names
359 * for a tracker. This function is only to update rowsf
360 * for boxes configured by
363 * @param string Name of the field.
364 * @param int Attribute1 - for text (size) and textarea (rows)
365 * @param int Attribute2 - for text (maxlength) and textarea (cols)
366 * @param int is_required - true or false whether this is a required field or not.
367 * @param string Alias for this field
368 * @return boolean success.
370 function update($name,$attribute1,$attribute2,$is_required=0,$alias="") {
371 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
372 $this->setPermissionDeniedError();
379 $this->setError(_('a field name is required'));
382 $res = db_query_params ('SELECT field_name FROM artifact_extra_field_list
383 WHERE field_name=$1 AND group_artifact_id=$2 AND extra_field_id !=$3',
385 $this->ArtifactType->getID(),
387 if (db_numrows($res) > 0) {
388 $this->setError(_('Field name already exists'));
397 if (!($alias = $this->generateAlias($alias,$name))) {
401 $result = db_query_params ('UPDATE artifact_extra_field_list
407 WHERE extra_field_id=$6
408 AND group_artifact_id=$7',
409 array (htmlspecialchars($name),
415 $this->ArtifactType->getID())) ;
416 if ($result && db_affected_rows($result) > 0) {
419 $this->setError(db_error());
428 function delete($sure, $really_sure) {
429 if (!$sure || !$really_sure) {
430 $this->setMissingParamsError(_('Please tick all checkboxes.'));
433 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
434 $this->setPermissionDeniedError();
438 $result = db_query_params ('DELETE FROM artifact_extra_field_data WHERE extra_field_id=$1',
439 array ($this->getID())) ;
441 $result = db_query_params ('DELETE FROM artifact_extra_field_elements WHERE extra_field_id=$1',
442 array ($this->getID())) ;
444 $result = db_query_params ('DELETE FROM artifact_extra_field_list WHERE extra_field_id=$1',
445 array ($this->getID())) ;
447 if ($this->getType() == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
448 if (!$this->ArtifactType->setCustomStatusField(0)) {
456 $this->setError(db_error());
461 $this->setError(db_error());
466 $this->setError(db_error());
475 * Note that this function does not check for conflicts.
476 * @param string alias - alias to validate
477 * @return bool true if alias is valid, false otherwise and it sets the corresponding error
479 function validateAlias($alias) {
480 // these are reserved alias names
481 static $reserved_alias = array(
493 if (strlen($alias) == 0) return true; // empty alias
496 if (preg_match("/[^[:alnum:]_@\\-]/", $alias)) {
497 $this->setError(_('The alias contains invalid characters. Only letters, numbers, hypens (-), at sign (@) and underscores (_) allowed.'));
499 } elseif (in_array($alias, $reserved_alias)) { // alias is reserved?
500 $this->setError(sprintf(_('\'%1$s\' is a reserved alias. Please provide another name.'), $alias));
508 * Generate an alias for this field. The alias can be entered by the user or
509 * be generated automatically from the name of the field.
510 * @param string $alias Alias entered by the user
511 * @param string $name Name of the field entered by the user (it'll be used when $alias is empty)
514 function generateAlias($alias, $name) {
515 $alias = strtolower(trim($alias));
516 if (strlen($alias) == 0) { // no alias was entered, generate alias from $name
517 $name = strtolower(trim($name));
518 // Convert the original name to a valid alias (i.e., if the extra field is
519 // called "Quality test", make an alias called "quality_test").
520 // The alias can be seen as a "unix name" for this field
521 $alias = preg_replace("/ /", "_", $name);
522 $alias = preg_replace("/[^[:alnum:]_@]/", "", $alias);
523 $alias = strtolower($alias);
524 } elseif (!$this->validateAlias($alias)) {
525 // alias is invalid...
528 // check if the name conflicts with another alias in the same artifact type
529 // in that case append a serial number to the alias
533 if ($this->data_array['extra_field_id']) {
534 $res = db_query_params ('SELECT * FROM artifact_extra_field_list
535 WHERE LOWER (alias)=$1
536 AND group_artifact_id=$2
537 AND extra_field_id <> $3',
539 $this->ArtifactType->getID(),
540 $this->data_array['extra_field_id'])) ;
542 $res = db_query_params ('SELECT * FROM artifact_extra_field_list WHERE LOWER (alias)=$1 AND group_artifact_id=$2',
544 $this->ArtifactType->getID()));
547 $this->setError(db_error());
549 } elseif (db_numrows($res) > 0) { // found another field with the same alias
552 $alias = $alias.$serial;
558 // at this point, the alias is valid and unique
562 function updateOrder($element_id, $order) {
564 $result=db_query_params ('UPDATE artifact_extra_field_elements
566 WHERE element_id=$2',
569 if ($result && db_affected_rows($result) > 0) {
573 $this->setError(db_error());
578 function reorderValues($element_id, $new_pos) {
580 $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',
581 array($this->getID()));
582 $max = db_numrows($res);
583 if ($new_pos < 1 || $new_pos > $max) {
584 $this->setError(_('Out of range value'));
590 if ($i == $new_pos) {
591 $data[] = $element_id;
594 if (($row = db_fetch_array($res)) && $row['element_id'] != $element_id) {
595 $data[] = $row['element_id'];
599 for ($i = 0; $i < count($data); $i++) {
600 if (! $this->updateOrder($data[$i], $i + 1))
607 function alphaorderValues() {
609 $res = db_query_params ('SELECT element_id FROM artifact_extra_field_elements WHERE extra_field_id=$1 ORDER BY element_name ASC',
610 array($this->getID()));
612 while ($row = db_fetch_array($res)) {
613 if (! $this->updateOrder($row['element_id'], $i))
625 // c-file-style: "bsd"