File: ArtifactExtraField.class

package info (click to toggle)
gforge 4.5.14-22etch13
  • links: PTS
  • area: main
  • in suites: etch
  • size: 13,004 kB
  • ctags: 11,918
  • sloc: php: 36,047; sql: 29,050; sh: 10,538; perl: 6,496; xml: 3,810; makefile: 341; python: 263; ansic: 256
file content (525 lines) | stat: -rw-r--r-- 14,591 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<?php
/**
 * ArtifactExtraField.class - Class to handle user defined artifacts
 *
 * Copyright 2004 (c) Anthony J. Pugliese
 *
 * @version   $Id: ArtifactExtraField.class 5527 2006-06-05 20:10:10Z lo-lan-do $
 *
 * This file is part of GForge.
 *
 * GForge is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * GForge is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GForge; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
 */
require_once('common/include/Error.class');

define('ARTIFACT_EXTRAFIELD_FILTER_INT','1,2,3,5,7');
define('ARTIFACT_EXTRAFIELDTYPE_SELECT',1);
define('ARTIFACT_EXTRAFIELDTYPE_CHECKBOX',2);
define('ARTIFACT_EXTRAFIELDTYPE_RADIO',3);
define('ARTIFACT_EXTRAFIELDTYPE_TEXT',4);
define('ARTIFACT_EXTRAFIELDTYPE_MULTISELECT',5);
define('ARTIFACT_EXTRAFIELDTYPE_TEXTAREA',6);
define('ARTIFACT_EXTRAFIELDTYPE_STATUS',7);
//define('ARTIFACT_EXTRAFIELDTYPE_ASSIGNEE',8);

class ArtifactExtraField extends Error {

	/** 
	 * The artifact type object.
	 *
	 * @var		object	$ArtifactType.
	 */
	var $ArtifactType; //object

	/**
	 * Array of artifact data.
	 *
	 * @var		array	$data_array.
	 */
	var $data_array;

	/**
	 *	ArtifactExtraField - Constructer
	 *
	 *	@param	object	ArtifactType object.
	 *  @param	array	(all fields from artifact_file_user_vw) OR id from database.
	 *  @return	boolean	success.
	 */
	function ArtifactExtraField(&$ArtifactType, $data=false) {
		$this->Error(); 

		//was ArtifactType legit?
		if (!$ArtifactType || !is_object($ArtifactType)) {
			$this->setError('ArtifactExtraField: No Valid ArtifactType');
			return false;
		}
		//did ArtifactType have an error?
		if ($ArtifactType->isError()) {
			$this->setError('ArtifactExtraField: '.$Artifact->getErrorMessage());
			return false;
		}
		$this->ArtifactType =& $ArtifactType;

		if ($data) {
			if (is_array($data)) {
				$this->data_array =& $data;
				return true;
			} else {
				if (!$this->fetchData($data)) {
					return false;
				} else {
					return true;
				}
			}
		}
	}

	/**
	 *	create - create a row in the table that stores box names for a
	 *	a tracker.  This function is only used to create rows for boxes 
	 *	configured by the admin.
	 *
	 *	@param	string	Name of the extra field.
	 *	@param	int	The type of field - radio, select, text, textarea
	 *	@param	int	Attribute1 - for text (size) and textarea (rows)
	 *	@param	int	Attribute2 - for text (maxlength) and textarea (cols)
	 *	@param	int	is_required - true or false whether this is a required field or not.
	 *	@param	string	alias - alias for this extra field (optional)
	 *  @return 	true on success / false on failure.
	 */
	function create($name,$field_type,$attribute1,$attribute2,$is_required=0,$alias='') {
		global $Language;

		//
		//	data validation
		//
		if (!$name) {
			$this->setError($Language->getText('tracker_admin_build_boxes','required_box_name'));
			return false;
		}
		if (!$this->ArtifactType->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}
		if ($is_required) {
			$is_required=1;
		} else {
			$is_required=0;
		}	
		
		if (!($alias = $this->generateAlias($alias,$name))) {
			return false;
		}
		
		$sql="INSERT INTO artifact_extra_field_list (group_artifact_id,field_name,
			field_type,attribute1,attribute2,is_required,alias) 
			VALUES ('".$this->ArtifactType->getID()."','".htmlspecialchars($name)."',
			'$field_type','$attribute1','$attribute2','$is_required','$alias')";

		db_begin();
		$result=db_query($sql);

		if ($result && db_affected_rows($result) > 0) {
			$this->clearError();
			$id=db_insertid($result,'artifact_extra_field_list','extra_field_id');
			//
			//	Now set up our internal data structures
			//
			if (!$this->fetchData($id)) {
				db_rollback();
				return false;
			}
			if ($field_type == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
				if (!$this->ArtifactType->setCustomStatusField($id)) {
					db_rollback();
					return false;
				} else {
//
//	Must insert some default statuses for each artifact
//
					$reso=db_query("INSERT INTO artifact_extra_field_elements(extra_field_id,element_name,status_id) 
						values ('$id','Open','1')");
					if (!$reso) {
						echo db_error();
					} else {
						$resoid=db_insertid($reso,'artifact_extra_field_elements','element_id');
						db_query("INSERT INTO artifact_extra_field_data(artifact_id,field_data,extra_field_id) 
							SELECT artifact_id,$resoid,$id FROM artifact 
							WHERE group_artifact_id='".$this->ArtifactType->getID()."'
							AND status_id=1");
					}
					$resc=db_query("INSERT INTO artifact_extra_field_elements(extra_field_id,element_name,status_id)
						values ('$id','Closed','2')");
					if (!$resc) {
						echo db_error();
					} else {
						$rescid=db_insertid($resc,'artifact_extra_field_elements','element_id');
						db_query("INSERT INTO artifact_extra_field_data(artifact_id,field_data,extra_field_id) 
							SELECT artifact_id,$rescid,$id FROM artifact 
							WHERE group_artifact_id='".$this->ArtifactType->getID()."'
							AND status_id != 1");
					}
				}
			} elseif (strstr(ARTIFACT_EXTRAFIELD_FILTER_INT,$field_type) !== false) {
//
//	Must insert some default 100 rows for the data table so None queries will work right
//
				$resdefault=db_query("INSERT INTO artifact_extra_field_data(artifact_id,field_data,extra_field_id) 
					SELECT artifact_id,100,$id FROM artifact WHERE group_artifact_id='".$this->ArtifactType->getID()."'");
				if (!$resdefault) {
					echo db_error();
				}
			}
			db_commit();
			return $id;
		} else {
			$this->setError(db_error());
			db_rollback();
			return false;
		}
	}

	/**
	 *	fetchData - re-fetch the data for this ArtifactExtraField from the database.
	 *
	 *	@param	int		ID of the Box.
	 *	@return	boolean	success.
	 */
	function fetchData($id) {
		$this->id=$id;
		$res=db_query("SELECT * FROM artifact_extra_field_list WHERE extra_field_id='$id'");
		
		if (!$res || db_numrows($res) < 1) {
			$this->setError('ArtifactExtraField: Invalid ArtifactExtraField ID');
			return false;
		}
		$this->data_array =& db_fetch_array($res);
		db_free_result($res);
		return true;
	}

	/**
	 *	getArtifactType - get the ArtifactType Object this ArtifactExtraField is associated with.
	 *
	 *	@return object	ArtifactType.
	 */
	function &getArtifactType() {
		return $this->ArtifactType;
	}
	
	/**
	 *	getID - get this ArtifactExtraField ID.
	 *
	 *	@return	int	The id #.
	 */
	function getID() {
		return $this->data_array['extra_field_id'];
	}

	/**
	 *	getName - get the name.
	 *
	 *	@return	string	The name.
	 */
	function getName() {
		return $this->data_array['field_name'];
	}

	/**
	 *	getAttribute1 - get the attribute1 field.
	 *
	 *	@return int	The first attribute.
	 */
	function getAttribute1() {
		return $this->data_array['attribute1'];
	}

	/**
	 *	getAttribute2 - get the attribute2 field.
	 *
	 *	@return int	The second attribute.
	 */
	function getAttribute2() {
		return $this->data_array['attribute2'];
	}

	/**
	 *	getType - the type of field.
	 *
	 *	@return	int	type.
	 */
	function getType() {
		return $this->data_array['field_type'];
	}

	/**
	 *	getTypeName - the name of type of field.
	 *
	 *	@return	string	type.
	 */
	function getTypeName() {
		$arr=&$this->getAvailableTypes();
		return $arr[$this->data_array['field_type']];
	}

	/**
	 *	isRequired - whether this field is required or not.
	 *
	 *	@return	boolean required.
	 */
	function isRequired() {
		return $this->data_array['is_required'];
	}

	/**
	 *	getAvailableTypes - the types of text fields and their names available.
	 *
	 *	@return	array	types.
	 */
	function getAvailableTypes() {
		global $Language;
		return array(
			1=>$Language->getText('tracker_admin_build_boxes','box_type_select'),
			2=>$Language->getText('tracker_admin_build_boxes','box_type_checkbox'),
			3=>$Language->getText('tracker_admin_build_boxes','box_type_radio'),
			4=>$Language->getText('tracker_admin_build_boxes','box_type_text'),
			5=>$Language->getText('tracker_admin_build_boxes','box_type_multiselect'),
			6=>$Language->getText('tracker_admin_build_boxes','box_type_textarea'),
			7=>$Language->getText('tracker_admin_build_boxes','box_type_status')
		);
	}
	
	/**
	 *	getAlias - the alias that is used for this field
	 *
	 *	@return	string	alias
	 */
	function getAlias() {
		return $this->data_array['alias'];
	}
	
	/**
	 *	getAvailableValues - Get the list of available values for this extra field
	 *
	 *	@return array
	 */
	function getAvailableValues() {
		$sql = "SELECT * FROM artifact_extra_field_elements WHERE extra_field_id=".$this->getID();
		$res = db_query($sql);
		
		$return = array();
		while ($row = db_fetch_array($res)) {
			$return[] = $row;
		}
		return $return;
	}

	/**
	 *  update - update a row in the table used to store box names 
	 *  for a tracker.  This function is only to update rowsf
	 *  for boxes configured by
	 *  the admin.
	 *
	 *  @param	string	Name of the field.
	 *	@param	int	Attribute1 - for text (size) and textarea (rows)
	 *	@param	int	Attribute2 - for text (maxlength) and textarea (cols)
	 *	@param	int	is_required - true or false whether this is a required field or not.
	 *	@param	string	Alias for this field
	 *  @return	boolean	success.
	 */
	function update($name,$attribute1,$attribute2,$is_required=0,$alias="") {
		global $Language;

		if (!$this->ArtifactType->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}
		//
		//	data validation
		//
		if (!$name) {
			$this->setError($Language->getText('tracker_admin_build_boxes','required_box_name'));
			return false;
		}
		if ($is_required) {
			$is_required=1;
		} else {
			$is_required=0;
		}
		
		if (!($alias = $this->generateAlias($alias,$name))) {
			return false;
		}		

		$sql="UPDATE artifact_extra_field_list 
			SET 
			field_name='".htmlspecialchars($name)."',
			attribute1='$attribute1',
			attribute2='$attribute2',
			is_required='$is_required',
			alias='$alias'
			WHERE extra_field_id='". $this->getID() ."' 
			AND group_artifact_id='".$this->ArtifactType->getID()."'";
		$result=db_query($sql);
		if ($result && db_affected_rows($result) > 0) {
			return true;
		} else {
			$this->setError(db_error());
			return false;
		}
	}

	/**
	 *
	 *
	 */
	function delete($sure, $really_sure) {
		if (!$sure || !$really_sure) {
			$this->setMissingParamsError();
			return false;
		}
		if (!$this->ArtifactType->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}
		db_begin();
		$sql="DELETE FROM artifact_extra_field_data 
			WHERE extra_field_id='".$this->getID()."'";
		$result=db_query($sql);
		if ($result) {
			$sql="DELETE FROM artifact_extra_field_elements
				WHERE extra_field_id='".$this->getID()."'";
			$result=db_query($sql);
			if ($result) {
				$sql="DELETE FROM artifact_extra_field_list
                WHERE extra_field_id='".$this->getID()."'";
				$result=db_query($sql);
				if ($result) {
					if ($this->getType() == ARTIFACT_EXTRAFIELDTYPE_STATUS) {
						if (!$this->ArtifactType->setCustomStatusField(0)) {
							db_rollback();
							return false;
						}
					}
					db_commit();
					return true;
				} else {
					$this->setError(db_error());
					db_rollback();
					return false;
				}
			} else {
				$this->setError(db_error());
				db_rollback();
				return false;
			}
		} else {
			$this->setError(db_error());
			db_rollback();
			return false;
		}

	}
	
	/**
	 * 	Validate an alias.
	 *	Note that this function does not check for conflicts.
	 *	@param	string	alias - alias to validate
	 *	@return	bool	true if alias is valid, false otherwise and it sets the corresponding error
	 */
	function validateAlias($alias) {
		global $Language;

		// these are reserved alias names
		static $reserved_alias = array(
			"project",
			"type",
			"priority",
			"assigned_to",
			"summary",
			"details"
		);

		if (strlen($alias) == 0) return true;		// empty alias

		// invalid chars?
		if (preg_match("/[^[:alnum:]_\\-]/", $alias)) {
			$this->setError($Language->getText('tracker_admin_build_boxes','invalid_alias'));
			return false;
		} else if (in_array($alias, $reserved_alias)) {	// alias is reserved?
			$this->setError($Language->getText('tracker_admin_build_boxes','reserved_alias', array($alias)));
			return false;
		}
		
		return true;
	}
	
	/**
	 *	Generate an alias for this field. The alias can be entered by the user or
	 *	be generated automatically from the name of the field.
	 *	@param	string	Alias entered by the user
	 *	@param	string	Name of the field entered by the user (it'll be used when $alias is empty)
	 *	@return	string
	 */
	function generateAlias($alias, $name) {
		$alias = strtolower(trim($alias));
		if (strlen($alias) == 0) {		// no alias was entered, generate alias from $name
			$name = strtolower(trim($name));
			// Convert the original name to a valid alias (i.e., if the extra field is 
			// called "Quality test", make an alias called "quality_test").
			// The alias can be seen as a "unix name" for this field
			$alias = preg_replace("/ /", "_", $name);
			$alias = preg_replace("/[^[:alnum:]_]/", "", $alias);
			$alias = strtolower($alias);
		} elseif (!$this->validateAlias($alias)) {
			// alias is invalid...
			return false;
		} 
		// check if the name conflicts with another alias in the same artifact type
		// in that case append a serial number to the alias
		$serial = 1;
		$conflict = false;	
		do {
			$sql = "SELECT * FROM artifact_extra_field_list ".
					"WHERE LOWER(alias)='".$alias."' AND ".
					"group_artifact_id=".$this->ArtifactType->getID();
			if ($this->data_array['extra_field_id']) {
				$sql .= " AND extra_field_id <> ".$this->data_array['extra_field_id'];
			}
			$res = db_query($sql);

			if (!$res) {
				$this->setError(db_error());
				return false;
			} else if (db_numrows($res) > 0) {		// found another field with the same alias
				$conflict = true;
				$serial++;
				$alias = $alias.$serial;
			} else {
				$conflict = false;
			}
		} while ($conflict);

		// at this point, the alias is valid and unique
		return $alias;
	}
}

// Local Variables:
// mode: php
// c-file-style: "bsd"
// End:

?>