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 * @param $ArtifactType
58 function __construct(&$ArtifactType, $data=false) {
61 //was ArtifactType legit?
62 if (!$ArtifactType || !is_object($ArtifactType)) {
63 $this->setError('ArtifactExtraField: No Valid ArtifactType');
66 //did ArtifactType have an error?
67 if ($ArtifactType->isError()) {
68 $this->setError('ArtifactExtraField: '.$ArtifactType->getErrorMessage());
71 $this->ArtifactType =& $ArtifactType;
74 if (is_array($data)) {
75 $this->data_array =& $data;
77 $this->fetchData($data);
83 * create - create a row in the table that stores box names for a
84 * a tracker. This function is only used to create rows for boxes
85 * configured by the admin.
87 * @param string Name of the extra field.
88 * @param int The type of field - radio, select, text, textarea
89 * @param int Attribute1 - for text (size) and textarea (rows)
90 * @param int Attribute2 - for text (maxlength) and textarea (cols)
91 * @param int is_required - true or false whether this is a required field or not.
92 * @param string alias - alias for this extra field (optional)
93 * @return true on success / false on failure.
95 function create($name,$field_type,$attribute1,$attribute2,$is_required=0,$alias='') {
100 $this->setError(_('a field name is required'));
104 $this->setError(_('Type of custom field not selected'));
107 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
108 $this->setPermissionDeniedError();
112 $res = db_query_params ('SELECT field_name FROM artifact_extra_field_list WHERE field_name=$1 AND group_artifact_id=$2',
114 $this->ArtifactType->getID()));
115 if (db_numrows($res) > 0) {
116 $this->setError(_('Field name already exists'));
119 if ($field_type == ARTIFACT_EXTRAFIELDTYPE_TEXT || $field_type == ARTIFACT_EXTRAFIELDTYPE_INTEGER) {
120 if (!$attribute1 || !$attribute2 || $attribute2 < $attribute1) {
121 $this->setError(_('Invalid size/maxlength for text field'));
125 if ($field_type == ARTIFACT_EXTRAFIELDTYPE_TEXTAREA) {
126 if (!$attribute1 || !$attribute2) {
127 $this->setError(_('Invalid rows/cols for textarea field'));
130 } elseif ($field_type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
131 if ($this->ArtifactType->getCustomStatusField()) {
132 $this->setError(_('This Tracker already uses custom statuses'));
143 if (!($alias = $this->generateAlias($alias,$name))) {
148 $result = db_query_params ('INSERT INTO artifact_extra_field_list (group_artifact_id,field_name,field_type,attribute1,attribute2,is_required,alias)
149 VALUES ($1,$2,$3,$4,$5,$6,$7)',
150 array ($this->ArtifactType->getID(),
151 htmlspecialchars($name),
158 if ($result && db_affected_rows($result) > 0) {
160 $id=db_insertid($result,'artifact_extra_field_list','extra_field_id');
162 // Now set up our internal data structures
164 if (!$this->fetchData($id)) {
168 if ($field_type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
169 if (!$this->ArtifactType->setCustomStatusField($id)) {
174 // Must insert some default statuses for each artifact
176 $ao = new ArtifactExtraFieldElement($this);
177 if (!$ao || !is_object($ao)) {
178 $feedback .= 'Unable to create ArtifactExtraFieldElement Object';
182 if (!$ao->create('Open', '1')) {
183 $feedback .= _('Error inserting an element').': '.$ao->getErrorMessage();
188 if (!$ao->create('Closed', '2')) {
189 $feedback .= _('Error inserting an element').': '.$ao->getErrorMessage();
196 } elseif (strstr(ARTIFACT_EXTRAFIELD_FILTER_INT,$field_type) !== false) {
198 // Must insert some default 100 rows for the data table so None queries will work right
200 $resdefault = db_query_params ('INSERT INTO artifact_extra_field_data(artifact_id,field_data,extra_field_id)
201 SELECT artifact_id,100,$1 FROM artifact WHERE group_artifact_id=$2',
203 $this->ArtifactType->getID())) ;
211 $this->setError(db_error());
218 * fetchData - re-fetch the data for this ArtifactExtraField from the database.
220 * @param int $id ID of the Box.
221 * @return boolean success.
223 function fetchData($id) {
225 $res = db_query_params ('SELECT * FROM artifact_extra_field_list WHERE extra_field_id=$1',
228 if (!$res || db_numrows($res) < 1) {
229 $this->setError('ArtifactExtraField: Invalid ArtifactExtraField ID');
232 $this->data_array = db_fetch_array($res);
233 db_free_result($res);
238 * getArtifactType - get the ArtifactType Object this ArtifactExtraField is associated with.
240 * @return object ArtifactType.
242 function &getArtifactType() {
243 return $this->ArtifactType;
247 * getID - get this ArtifactExtraField ID.
249 * @return int The id #.
252 return $this->data_array['extra_field_id'];
256 * getName - get the name.
258 * @return string The name.
261 return $this->data_array['field_name'];
265 * getAttribute1 - get the attribute1 field.
267 * @return int The first attribute.
269 function getAttribute1() {
270 return $this->data_array['attribute1'];
274 * getAttribute2 - get the attribute2 field.
276 * @return int The second attribute.
278 function getAttribute2() {
279 return $this->data_array['attribute2'];
283 * getType - the type of field.
288 return $this->data_array['field_type'];
292 * getTypeName - the name of type of field.
294 * @return string type.
296 function getTypeName() {
297 $arr = $this->getAvailableTypes();
298 return $arr[$this->data_array['field_type']];
302 * isRequired - whether this field is required or not.
304 * @return boolean required.
306 function isRequired() {
307 return $this->data_array['is_required'];
311 * getAvailableTypes - the types of text fields and their names available.
313 * @return array types.
315 static function getAvailableTypes() {
319 3=>_('Radio Buttons'),
321 5=>_('Multi-Select Box'),
330 * getAlias - the alias that is used for this field
332 * @return string alias
334 function getAlias() {
335 return $this->data_array['alias'];
339 * getAvailableValues - Get the list of available values for this extra field
343 function getAvailableValues() {
344 $res = db_query_params ('SELECT * FROM artifact_extra_field_elements WHERE extra_field_id=$1',
345 array ($this->getID()));
347 while ($row = db_fetch_array($res)) {
354 * update - update a row in the table used to store box names
355 * for a tracker. This function is only to update rowsf
356 * for boxes configured by
359 * @param string Name of the field.
360 * @param int Attribute1 - for text (size) and textarea (rows)
361 * @param int Attribute2 - for text (maxlength) and textarea (cols)
362 * @param int is_required - true or false whether this is a required field or not.
363 * @param string Alias for this field
364 * @return boolean success.
366 function update($name,$attribute1,$attribute2,$is_required=0,$alias="") {
367 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
368 $this->setPermissionDeniedError();
375 $this->setError(_('a field name is required'));
378 $res = db_query_params ('SELECT field_name FROM artifact_extra_field_list
379 WHERE field_name=$1 AND group_artifact_id=$2 AND extra_field_id !=$3',
381 $this->ArtifactType->getID(),
383 if (db_numrows($res) > 0) {
384 $this->setError(_('Field name already exists'));
393 if (!($alias = $this->generateAlias($alias,$name))) {
397 $result = db_query_params ('UPDATE artifact_extra_field_list
403 WHERE extra_field_id=$6
404 AND group_artifact_id=$7',
405 array (htmlspecialchars($name),
411 $this->ArtifactType->getID())) ;
412 if ($result && db_affected_rows($result) > 0) {
415 $this->setError(db_error());
424 function delete($sure, $really_sure) {
425 if (!$sure || !$really_sure) {
426 $this->setMissingParamsError(_('Please tick all checkboxes.'));
429 if (!forge_check_perm ('tracker_admin', $this->ArtifactType->Group->getID())) {
430 $this->setPermissionDeniedError();
434 $result = db_query_params ('DELETE FROM artifact_extra_field_data WHERE extra_field_id=$1',
435 array ($this->getID())) ;
437 $result = db_query_params ('DELETE FROM artifact_extra_field_elements WHERE extra_field_id=$1',
438 array ($this->getID())) ;
440 $result = db_query_params ('DELETE FROM artifact_extra_field_list WHERE extra_field_id=$1',
441 array ($this->getID())) ;
443 if ($this->getType() == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
444 if (!$this->ArtifactType->setCustomStatusField(0)) {
452 $this->setError(db_error());
457 $this->setError(db_error());
462 $this->setError(db_error());
471 * Note that this function does not check for conflicts.
472 * @param string alias - alias to validate
473 * @return bool true if alias is valid, false otherwise and it sets the corresponding error
475 function validateAlias($alias) {
476 // these are reserved alias names
477 static $reserved_alias = array(
489 if (strlen($alias) == 0) return true; // empty alias
492 if (preg_match("/[^[:alnum:]_@\\-]/", $alias)) {
493 $this->setError(_('The alias contains invalid characters. Only letters, numbers, hypens (-), at sign (@) and underscores (_) allowed.'));
495 } elseif (in_array($alias, $reserved_alias)) { // alias is reserved?
496 $this->setError(sprintf(_('\'%1$s\' is a reserved alias. Please provide another name.'), $alias));
504 * Generate an alias for this field. The alias can be entered by the user or
505 * be generated automatically from the name of the field.
506 * @param string $alias Alias entered by the user
507 * @param string $name Name of the field entered by the user (it'll be used when $alias is empty)
510 function generateAlias($alias, $name) {
511 $alias = strtolower(trim($alias));
512 if (strlen($alias) == 0) { // no alias was entered, generate alias from $name
513 $name = strtolower(trim($name));
514 // Convert the original name to a valid alias (i.e., if the extra field is
515 // called "Quality test", make an alias called "quality_test").
516 // The alias can be seen as a "unix name" for this field
517 $alias = preg_replace("/ /", "_", $name);
518 $alias = preg_replace("/[^[:alnum:]_@]/", "", $alias);
519 $alias = strtolower($alias);
520 } elseif (!$this->validateAlias($alias)) {
521 // alias is invalid...
524 // check if the name conflicts with another alias in the same artifact type
525 // in that case append a serial number to the alias
529 if ($this->data_array['extra_field_id']) {
530 $res = db_query_params ('SELECT * FROM artifact_extra_field_list
531 WHERE LOWER (alias)=$1
532 AND group_artifact_id=$2
533 AND extra_field_id <> $3',
535 $this->ArtifactType->getID(),
536 $this->data_array['extra_field_id'])) ;
538 $res = db_query_params ('SELECT * FROM artifact_extra_field_list WHERE LOWER (alias)=$1 AND group_artifact_id=$2',
540 $this->ArtifactType->getID()));
543 $this->setError(db_error());
545 } elseif (db_numrows($res) > 0) { // found another field with the same alias
548 $alias = $alias.$serial;
554 // at this point, the alias is valid and unique
558 function updateOrder($element_id, $order) {
560 $result=db_query_params ('UPDATE artifact_extra_field_elements
562 WHERE element_id=$2',
565 if ($result && db_affected_rows($result) > 0) {
569 $this->setError(db_error());
574 function reorderValues($element_id, $new_pos) {
576 $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',
577 array($this->getID()));
578 $max = db_numrows($res);
579 if ($new_pos < 1 || $new_pos > $max) {
580 $this->setError(_('Out of range value'));
586 if ($i == $new_pos) {
587 $data[] = $element_id;
590 if (($row = db_fetch_array($res)) && $row['element_id'] != $element_id) {
591 $data[] = $row['element_id'];
595 for ($i = 0; $i < count($data); $i++) {
596 if (! $this->updateOrder($data[$i], $i + 1))
603 function alphaorderValues() {
605 $res = db_query_params ('SELECT element_id FROM artifact_extra_field_elements WHERE extra_field_id=$1 ORDER BY element_name ASC',
606 array($this->getID()));
608 while ($row = db_fetch_array($res)) {
609 if (! $this->updateOrder($row['element_id'], $i))
621 // c-file-style: "bsd"