5 * Copyright 2004, Anthony J. Pugliese
6 * Copyright 2009, Roland Mas
8 * This file is part of FusionForge.
10 * FusionForge is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published
12 * by the Free Software Foundation; either version 2 of the License,
13 * or (at your option) any later version.
15 * FusionForge is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with FusionForge; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 require_once $gfcommon.'include/Error.class.php';
28 define('ARTIFACT_EXTRAFIELD_FILTER_INT','1,2,3,5,7');
29 define('ARTIFACT_EXTRAFIELDTYPE_SELECT',1);
30 define('ARTIFACT_EXTRAFIELDTYPE_CHECKBOX',2);
31 define('ARTIFACT_EXTRAFIELDTYPE_RADIO',3);
32 define('ARTIFACT_EXTRAFIELDTYPE_TEXT',4);
33 define('ARTIFACT_EXTRAFIELDTYPE_MULTISELECT',5);
34 define('ARTIFACT_EXTRAFIELDTYPE_TEXTAREA',6);
35 define('ARTIFACT_EXTRAFIELDTYPE_STATUS',7);
36 //define('ARTIFACT_EXTRAFIELDTYPE_ASSIGNEE',8);
37 define('ARTIFACT_EXTRAFIELDTYPE_RELATION',9);
39 class ArtifactExtraField extends Error {
42 * The artifact type object.
44 * @var object $ArtifactType.
46 var $ArtifactType; //object
49 * Array of artifact data.
51 * @var array $data_array.
56 * ArtifactExtraField - Constructer
58 * @param object ArtifactType object.
59 * @param array (all fields from artifact_file_user_vw) OR id from database.
60 * @return boolean success.
62 function ArtifactExtraField(&$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;
82 if (!$this->fetchData($data)) {
92 * create - create a row in the table that stores box names for a
93 * a tracker. This function is only used to create rows for boxes
94 * configured by the admin.
96 * @param string Name of the extra field.
97 * @param int The type of field - radio, select, text, textarea
98 * @param int Attribute1 - for text (size) and textarea (rows)
99 * @param int Attribute2 - for text (maxlength) and textarea (cols)
100 * @param int is_required - true or false whether this is a required field or not.
101 * @param string alias - alias for this extra field (optional)
102 * @return true on success / false on failure.
104 function create($name,$field_type,$attribute1,$attribute2,$is_required=0,$alias='') {
109 $this->setError(_('a field name is required'));
113 $this->setError("Type of custom field not selected");
116 if (!$this->ArtifactType->userIsAdmin()) {
117 $this->setPermissionDeniedError();
121 $res = db_query_params ('SELECT field_name FROM artifact_extra_field_list WHERE field_name=$1 AND group_artifact_id=$2',
123 $this->ArtifactType->getID()));
124 if (db_numrows($res) > 0) {
125 $this->setError(_('Field name already exists'));
128 if ($field_type == ARTIFACT_EXTRAFIELDTYPE_TEXT) {
129 if (!$attribute1 || !$attribute2 || $attribute2 < $attribute1) {
130 $this->setError("Invalid size/maxlength for text field");
134 if ($field_type == ARTIFACT_EXTRAFIELDTYPE_TEXTAREA) {
135 if (!$attribute1 || !$attribute2) {
136 $this->setError("Invalid rows/cols for textarea field");
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 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 function getAvailableTypes() {
323 3=>_('Radio Buttons'),
325 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 (!$this->ArtifactType->userIsAdmin()) {
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();
432 if (!$this->ArtifactType->userIsAdmin()) {
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(
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 (-) and underscores (_) allowed.'));
495 } else if (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 entered by the user
507 * @param string 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 } else if (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'));
585 if ($i == $new_pos) {
586 $data[] = $element_id;
589 if (($row = db_fetch_array($res)) && $row['element_id'] != $element_id) {
590 $data[] = $row['element_id'];
594 for ($i = 0; $i < count($data); $i++) {
595 if (! $this->updateOrder($data[$i], $i + 1))
602 function alphaorderValues($element_id) {
604 $res = db_query_params ('SELECT element_id FROM artifact_extra_field_elements WHERE extra_field_id=$1 ORDER BY element_name ASC',
605 array($this->getID()));
607 while ($row = db_fetch_array($res)) {
608 if (! $this->updateOrder($row['element_id'], $i))
620 // c-file-style: "bsd"