File: ArtifactType.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 (1237 lines) | stat: -rw-r--r-- 33,022 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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
<?php
/**
 * ArtifactType.class - Class to artifact an type
 *
 * Copyright 1999-2001 (c) VA Linux Systems
 * The rest Copyright 2002-2004 (c) GForge Team
 * http://gforge.org/
 *
 * @version   $Id: ArtifactType.class 5621 2006-07-23 16:27:16Z tperdue $
 *
 * 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  USA
 */
require_once('common/include/Error.class');

	/**
	* Gets an ArtifactType object from the artifact type id
	* 
	* @param artType_id	the ArtifactType id
	* @param res	the DB handle if passed in (optional)
	* @return	the ArtifactType object	
	*/
	function &artifactType_get_object($artType_id,$res=false) {
		global $ARTIFACTTYPE_OBJ;
		if (!isset($ARTIFACTTYPE_OBJ["_".$artType_id."_"])) {
			if ($res) {
				//the db result handle was passed in
			} else {
				$res=db_query("SELECT * FROM artifact_group_list_vw
						WHERE group_artifact_id='$artType_id'");
			}
			if (!$res || db_numrows($res) < 1 ){
				$ARTIFACTTYPE_OBJ["_".$artType_id."_"]=false;
			} else {
				$data =& db_fetch_array($res);
				$Group =& group_get_object($data["group_id"]);
				$ARTIFACTTYPE_OBJ["_".$artType_id."_"]= new ArtifactType($Group,$data["group_artifact_id"],$data);
			}
		}
		return $ARTIFACTTYPE_OBJ["_".$artType_id."_"];
	}	

class ArtifactType extends Error {

	/**
	 * The Group object.
	 *
	 * @var		object	$Group.
	 */
	var $Group; //group object

	/**
	 * extra_fields 3d array - the IDs and Names of the extra fields
	 *
	 * @var		array extra_fields;
	 */
	var $extra_fields = array();

	/**
	 * extra_field[extra_field_id] array - the IDs and Names of elements on the extra fields
	 *
	 * @var		array	extra_field
	 */
	var $extra_field;
	
	/**
	 * Options	db resource ID.
	 *
	 * @var		int		$options_res.
	 */
	var $options_res;

	/**
	 * Choice name	db resource ID.
	 *
	 * @var		int		$choice_name_res.
	 */
	var $choice_name_res;

	/**
	 * Current user permissions.
	 *
	 * @var		int		$current_user_perm.
	 */
	var $current_user_perm;

	/**
	 * Technicians db resource ID.
	 *
	 * @var		int		$technicians_res.
	 */
	var $technicians_res;

	/**
	 * Status db resource ID.
	 *
	 * @var		int		$status_res.
	 */
	var $status_res;

	/**
	 * Canned responses resource ID.
	 *
	 * @var		int		$cannecresponses_res.
	 */
	var $cannedresponses_res;

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

	/**
	 * Array of element names so they only have to be fetched once from db.
	 *
	 * @var		array	$data_array.
	 */
	var	$element_name;

	/**
	 * Array of element status so they only have to be fetched once from db.
	 *
	 * @var		array	$data_array.
	 */
	var	$element_status;

	/**
	 *	ArtifactType - constructor.
	 *
	 *	@param	object	The Group object.
	 *	@param	int		The id # assigned to this artifact type in the db.
	 *  @param	array	The associative array of data.
	 *	@return boolean	success.
	 */
	function ArtifactType(&$Group,$artifact_type_id=false, $arr=false) {
		$this->Error();
		if (!$Group || !is_object($Group)) {
			$this->setError('No Valid Group Object');
			return false;
		}
		if ($Group->isError()) {
			$this->setError('ArtifactType: '.$Group->getErrorMessage());
			return false;
		}
		$this->Group =& $Group;
		if ($artifact_type_id) {
			if (!$arr || !is_array($arr)) {
				if (!$this->fetchData($artifact_type_id)) {
					return false;
				}
			} else {
				$this->data_array =& $arr;
				if ($this->data_array['group_id'] != $this->Group->getID()) {
					$this->setError('Group_id in db result does not match Group Object');
					$this->data_array = null;
					return false;
				}
			}
			//
			//  Make sure they can even access this object
			//
			if (!$this->userCanView()) {
				$this->setPermissionDeniedError();
				$this->data_array = null;
				return false;
			}
		}
	}

	/**
	 *	create - use this to create a new ArtifactType in the database.
	 *
	 *	@param	string	The type name.
	 *	@param	string	The type description.
	 *  @param  bool	(1) true (0) false - viewable by general public.
	 *  @param  bool	(1) true (0) false - whether non-logged-in users can submit.
	 *	@param	bool	(1) true (0) false - whether to email on all updates.
	 *	@param	string	The address to send new entries and updates to.
	 *	@param	int		Days before this item is considered overdue.
	 *	@param	bool	(1) trye (0) false - whether the resolution box should be shown.
	 *	@param	string	Free-form string that project admins can place on the submit page.
	 *	@param	string	Free-form string that project admins can place on the browse page.
	 *	@param	int		(1) bug tracker, (2) Support Tracker, (3) Patch Tracker, (4) features (0) other.
	 *	@return id on success, false on failure.
	 */
	function create($name,$description,$is_public,$allow_anon,$email_all,$email_address,
		$due_period,$use_resolution,$submit_instructions,$browse_instructions,$datatype=0) {

		global $Language;
		$perm =& $this->Group->getPermission( session_get_user() );

		if (!$perm || !is_object($perm) || !$perm->isArtifactAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}

		if (!$name || !$description || !$due_period) {
			$this->setError($Language->getText('tracker_artifacttype','required_fields'));
			return false;
		}
		
		if ($email_address) {
			$invalid_emails = validate_emails($email_address);
			if (count($invalid_emails) > 0) {
				$this->SetError($Language->getText('tracker_artifacttype','invalid_emails').': '.implode(',',$invalid_emails));
				return false;
			}
		}

		$use_resolution = ((!$use_resolution) ? 0 : $use_resolution);
		$is_public = ((!$is_public) ? 0 : $is_public);
		$allow_anon = ((!$allow_anon) ? 0 : $allow_anon);
		$email_all = ((!$email_all) ? 0 : $email_all);


		$sql="INSERT INTO 
			artifact_group_list 
			(group_id,
			name,
			description,
			is_public,
			allow_anon,
			email_all_updates,
			email_address,
			due_period,
			status_timeout,
			submit_instructions,
			browse_instructions,
			datatype) 
			VALUES 
			('". $this->Group->getID() ."',
			'". htmlspecialchars($name) ."',
			'". htmlspecialchars($description) ."',
			'$is_public',
			'$allow_anon',
			'$email_all',
			'$email_address',
			'". ($due_period*(60*60*24)) ."',
			'1209600',
			'".htmlspecialchars($submit_instructions)."',
			'".htmlspecialchars($browse_instructions)."',
			'$datatype')";
		
		db_begin();
		
		$res = db_query($sql);

		$id = db_insertid($res,'artifact_group_list','group_artifact_id');
		
		if (!$res || !$id) {
			$this->setError('ArtifactType: '.db_error());
			db_rollback();
			return false;
		} else {
			if (!$this->fetchData($id)) {
				db_rollback();
				return false;
			} else {
				if (!$this->addAllUsers()) {
					db_rollback();
					return false;
				} else {
					db_commit();
					return $id;
				}
			}
		}
	}

	/**
	 *  fetchData - re-fetch the data for this ArtifactType from the database.
	 *
	 *  @param	int		The artifact type ID.
	 *  @return boolean	success.
	 */
	function fetchData($artifact_type_id) {
		$res=db_query("SELECT * FROM artifact_group_list_vw
			WHERE group_artifact_id='$artifact_type_id' 
			AND group_id='". $this->Group->getID() ."'");
		if (!$res || db_numrows($res) < 1) {
			$this->setError('ArtifactType: Invalid ArtifactTypeID');
			return false;
		}
		$this->data_array =& db_fetch_array($res);
		db_free_result($res);
		return true;
	}

	/**
	 *	  getGroup - get the Group object this ArtifactType is associated with.
	 *
	 *	  @return	Object	The Group object.
	 */
	function &getGroup() {
		return $this->Group;
	}

	/**
	 *	  getID - get this ArtifactTypeID.
	 *
	 *	  @return	int	The group_artifact_id #.
	 */
	function getID() {
		return $this->data_array['group_artifact_id'];
	}

	/**
	 *	  getOpenCount - get the count of open tracker items in this tracker type.
	 *
	 *	  @return	int	The count.
	 */
	function getOpenCount() {
		return $this->data_array['open_count'];
	}

	/**
	 *	  getTotalCount - get the total number of tracker items in this tracker type.
	 *
	 *	  @return	int	The total count.
	 */
	function getTotalCount() {
		return $this->data_array['count'];
	}

	/**
	 *	  allowsAnon - determine if non-logged-in users can post.
	 *
	 *	  @return	boolean	allow_anonymous_submissions.
	 */
	function allowsAnon() {
		return $this->data_array['allow_anon'];
	}

	/**
	 *	  getSubmitInstructions - get the free-form string strings.
	 *
	 *	  @return	string	instructions.
	 */
	function getSubmitInstructions() {
		return $this->data_array['submit_instructions'];
	}

	/**
	 *	  getBrowseInstructions - get the free-form string strings.
	 *
	 *	  @return string instructions.
	 */
	function getBrowseInstructions() {
		return $this->data_array['browse_instructions'];
	}

	/**
	 *	  emailAll - determine if we're supposed to email on every event.
	 *
	 *	  @return	boolean	email_all.
	 */
	function emailAll() {
		return $this->data_array['email_all_updates'];
	}

	/**
	 *	  emailAddress - defined email address to send events to.
	 *
	 *	  @return	string	email.
	 */
	function getEmailAddress() {
		return $this->data_array['email_address'];
	}

	/**
	 *	  isPublic - whether non-group-members can view.
	 *
	 *	  @return boolean	is_public.
	 */
	function isPublic() {
		return $this->data_array['is_public'];
	}

	/**
	 *	  getName - the name of this ArtifactType.
	 *
	 *	  @return	string	name.
	 */
	function getName() {
		return $this->data_array['name'];
	}
	
	/**
	 * getFormattedName - formatted name of this ArtifactType
	 *
	 * @return string formatted name
	 */
	function getFormattedName() {
		$name = preg_replace('/[^[:alnum:]]/','',$this->getName());
		$name = strtolower($name);
		return $name;
	}
	
	/**
	 * getUnixName - returns the name used by email gateway
	 *
	 * @return string unix name
	 */
	function getUnixName() {
		return strtolower($this->Group->getUnixName()).'-'.$this->getFormattedName();
	}
	
	/**
	 * getReturnEmailAddress() - return the return email address for notification emails
	 *
	 * @return string return email address
	 */
	function getReturnEmailAddress() {
		global $sys_default_domain,$sys_use_gateways;
		$address = '';
		if($sys_use_gateways) {
			$address .= strtolower($this->getUnixName());
		} else {
			$address .= 'noreply';
		}
		$address .= '@'.$sys_default_domain;
		return $address;
	}

	/**
	 *	  getDescription - the description of this ArtifactType.
	 *
	 *	  @return	string	description.
	 */
	function getDescription() {
		return $this->data_array['description'];
	}

	/**
	 *	  getDuePeriod - how many seconds until it's considered overdue.
	 *
	 *	  @return int seconds.
	 */
	function getDuePeriod() {
		return $this->data_array['due_period'];
	}

	/**
	 *	getStatusTimeout - how many seconds until an item is stale.
	 *
	 *	@return int seconds.
	 */
	function getStatusTimeout() {
		return $this->data_array['status_timeout'];
	}

	/**
	 *	  getCustomStatusField - return the extra_field_id of the field containing the custom status.
	 *
	 *	  @return	int	extra_field_id.
	 */
	function getCustomStatusField() {
		return $this->data_array['custom_status_field'];
	}

	/**
	 *	setCustomStatusField - set the extra_field_id of the field containing the custom status.
	 *	@param	int	The extra field id.
	 *	@return	boolean	success.
	 */
	function setCustomStatusField($extra_field_id) {
		$res=db_query("UPDATE artifact_group_list SET custom_status_field='$extra_field_id'
			WHERE group_artifact_id='".$this->getID()."'");
		return $res;
	}

	/**
	 *	  usesCustomStatuses - boolean
	 *
	 *	  @return	boolean	use_custom_statues.
	 */
	function usesCustomStatuses() {
		return $this->getCustomStatusField();
	}

	/**
	 *	remap status	- pass the extra_fields array and return the status_id, either open/closed
	 *	@param	int	The status_id
	 *	@param	array	Complex array of extra_field_data
	 *	@return	int	status_id.
	 */
	function remapStatus($status_id,$extra_fields) {
		if ($this->usesCustomStatuses()) {
			//get the selected element for the extra_field_status element
			$csfield = $this->getCustomStatusField();
			if (array_key_exists($csfield, $extra_fields)) {
				$element_id=$extra_fields[$csfield];

				//convert that element_id into the status_id
				$res=db_query("SELECT status_id FROM artifact_extra_field_elements WHERE element_id='$element_id'");
				if (!$res) {
					$this->setError('Error Remapping Status: '.db_error());
					return false;
				}
				$status_id=db_result($res,0,'status_id');
			} else {
				// custom status was not passed... use the first status from the database
				$res = db_query("SELECT status_id FROM artifact_extra_field_elements WHERE extra_field_id='".$csfield."' ORDER BY element_id ASC LIMIT 1 OFFSET 0");
				if (db_numrows($res) == 0) {		// No values available
					$this->setError('Error Remapping Status');
					return false;
				}
				$status_id=db_result($res,0,'status_id');
			}
			
			if ($status_id < 1 || $status_id > 4) {
				echo "INVALID STATUS REMAP: $status_id FROM SELECTED ELEMENT: $element_id";
				return false;
			}
			return $status_id;
		} else {
			return $status_id;
		}
	}

	/**
	 *	  getDataType - flag that is generally unused but can mark the difference between bugs, patches, etc.
	 *
	 *	  @return	int	The type (1) bug (2) support (3) patch (4) feature (0) other.
	 */
	function getDataType() {
		return $this->data_array['datatype'];
	}

	/**
	 *  setMonitor - user can monitor this artifact.
	 *
	 *  @return false - always false - always use the getErrorMessage() for feedback
	 */
	function setMonitor() {
		global $Language;
		if (session_loggedin()) {

			$user_id=user_getid();
			$user =& user_get_object(user_getid());

		} else {

			$this->setError($Language->getText('tracker_artifact','error_valid_email_required'));
			return false;

		}

		$res=db_query("SELECT * FROM artifact_type_monitor
			WHERE group_artifact_id='". $this->getID() ."'
			AND user_id='$user_id'");

		if (!$res || db_numrows($res) < 1) {
			//not yet monitoring
			$res=db_query("INSERT INTO artifact_type_monitor (group_artifact_id,user_id)
				VALUES ('". $this->getID() ."','$user_id')");
			if (!$res) {
				$this->setError(db_error());
				return false;
			} else {
				$this->setError($Language->getText('tracker_artifacttype','monitoring_activated'));
				return false;
			}
		} else {
			//already monitoring - remove their monitor
			db_query("DELETE FROM artifact_type_monitor
				WHERE group_artifact_id='". $this->getID() ."'
				AND user_id='$user_id'");
			$this->setError($Language->getText('tracker_artifacttype','monitoring_deactivated'));
			return false;
		}
	}

	function isMonitoring() {
		if (!session_loggedin()) {
			return false;
		}
		$sql="SELECT count(*) FROM artifact_type_monitor 
			WHERE user_id='".user_getid()."' AND group_artifact_id='".$this->getID()."';";
		$result = db_query($sql);
		$row_count = db_fetch_array($result);
		return $result && $row_count['count'] > 0;
	}

	/**
	 *  getMonitorIds - array of email addresses monitoring this Artifact.
	 *
	 *  @return array of email addresses monitoring this Artifact.
	 */
	function &getMonitorIds() {
		$res=db_query("SELECT user_id
			FROM artifact_type_monitor
			WHERE group_artifact_id='". $this->getID() ."'");
		return util_result_column_to_array($res);
	}

	/**
	 *	getExtraFields - List of possible user built extra fields
	 *	set up for this artifact type.
	 *
	 *	@return arrays of data;
	 */
	function getExtraFields($filter='') {
		if (!isset($this->extra_fields["$filter"])) {
			$this->extra_fields["$filter"] = array();
			if ($filter) {
				$filter_str=" AND field_type IN ($filter) ";
			} else {
				$filter_str="";
			}
			$sql="select *
				FROM artifact_extra_field_list 
				WHERE group_artifact_id='".$this->getID() ."'
				$filter_str
				ORDER BY field_type ASC";
			$res=db_query($sql);
			while($arr = db_fetch_array($res)) {
				$this->extra_fields["$filter"][$arr['extra_field_id']] = $arr;
			}
		}
			
		return $this->extra_fields["$filter"];
	}

	/**
	 *	cloneFieldsFrom - clone all the fields and elements from another tracker
	 *
	 *	@return	boolean	true/false on success
	 */
	function cloneFieldsFrom($clone_tracker_id) {
		global $sys_template_group;
		$g =& group_get_object($sys_template_group);
		if (!$g || !is_object($g)) {
			$this->setError('Could Not Get Template Group');
			return false;
		} elseif ($g->isError()) {
			$this->setError('Template Group Error '.$g->getErrorMessage());
			return false;
		}
		$at =& new ArtifactType($g,$clone_tracker_id);
		if (!$at || !is_object($at)) {
			$this->setError('Could Not Get Tracker To Clone');
			return false;
		} elseif ($at->isError()) {
			$this->setError('Clone Tracker Error '.$at->getErrorMessage());
			return false;
		}
		$efs =& $at->getExtraFields();


		//
		//	Iterate list of extra fields
		//
		db_begin();
		foreach ($efs as $ef) {
			//new field in this tracker
			$nef = new ArtifactExtraField($this);
			if (!$nef->create( addslashes(util_unconvert_htmlspecialchars($ef['field_name'])), $ef['field_type'], $ef['attribute1'], $ef['attribute2'], $ef['is_required'], $ef['alias'])) {
				db_rollback();
				$this->setError('Error Creating New Extra Field: '.$nef->getErrorMessage());
				return false;
			}
			//
			//	Iterate the elements
			//
			$resel=db_query("SELECT * FROM artifact_extra_field_elements WHERE extra_field_id='".$ef['extra_field_id']."'");
			while ($el =& db_fetch_array($resel)) {
				//new element
				$nel = new ArtifactExtraFieldElement($nef);
				if (!$nel->create( addslashes(util_unconvert_htmlspecialchars($el['element_name'])), $el['status_id'] )) {
					db_rollback();
					$this->setError('Error Creating New Extra Field Element: '.$nel->getErrorMessage());
					return false;
				}
			}
		}
		db_commit();
		return true;

	}

	/**
	 *	getExtraFieldName - Get a box name using the box ID 
	 *
	 *	@param  int 	id of an extra field.
	 *	@return string	name of extra field.
	 */
	function getExtraFieldName($extra_field_id) {
		$arr = $this->getExtraFields();
		return $arr[$extra_field_id]['field_name'];
	}

	/**
	 *	getExtraFieldElements - List of possible admin configured 
	 *	extra field elements. This function is used to 
	 *	present the boxes and choices on the main Add/Update page.
	 *
	 *	@param	int	id of the extra field
	 *	@return array of elements for this extra field.
	 */
	function getExtraFieldElements($id) {
//TODO validate $id
		if (!$id) {
			return false;
		}
		if (!isset($this->extra_field[$id])) {
			$this->extra_field[$id] = array();
			$sql="select element_id,element_name,status_id
				FROM artifact_extra_field_elements
				WHERE extra_field_id ='".$id."'  
				ORDER BY element_id ASC";

			$res=db_query($sql);
			$i=0;
			while($arr =& db_fetch_array($res)) {
				$this->extra_field[$id][$i++] = $arr;
			}
//			if (count($this->extra_field[$id]) == 0) {
//				return;
//			}
		}
				
		return $this->extra_field[$id];
	}

	/**
	 *	getElementName - get the name of a particular element.
	 *
	 *	@return	string	The name.
	 */
	function getElementName($choiceid) {
		if (!$choiceid) {
			return '';
		}
		if (is_array($choiceid)) {
			$choiceid=implode(',',$choiceid);
		}
		if ($choiceid == 100) {
			return 'None';
		}
		if (!$this->element_name["$choiceid"]) {
			$sql="select element_id,extra_field_id,element_name
				FROM artifact_extra_field_elements
				WHERE element_id IN ($choiceid)";
			$res=db_query($sql);
			if (db_numrows($res) > 1) {
				$arr=util_result_column_to_array($res,2);
				$this->element_name["$choiceid"]=implode(',',$arr);
			} else {
				$this->element_name["$choiceid"]=db_result($res,0,'element_name');
			}
		}
		return $this->element_name["$choiceid"];
	}

	/**
	 *	getElementStatusID - get the status of a particular element.
	 *
	 *	@return	int		The status
	 */
	function getElementStatusID($choiceid) {
		if (!$choiceid) {
			return 0;
		}
		if (is_array($choiceid)) {
			$choiceid=implode(',',$choiceid);
		}
		if ($choiceid == 100) {
			return 0;
		}
		if (!$this->element_status["$choiceid"]) {
			$sql="select element_id,extra_field_id,status_id
				FROM artifact_extra_field_elements
				WHERE element_id IN ($choiceid)";
			$res=db_query($sql);
			if (db_numrows($res) > 1) {
				$arr=util_result_column_to_array($res,2);
				$this->element_status["$choiceid"]=implode(',',$arr);
			} else {
				$this->element_status["$choiceid"]=db_result($res,0,'status_id');
			}
		}
		return $this->element_status["$choiceid"];
	}


	/**
	 *	delete - delete this tracker and all its related data.
	 *
	 *	@param	bool	I'm Sure.
	 *	@param	bool	I'm REALLY sure.
	 *	@return	bool true/false;
	 */
	function delete($sure, $really_sure) {
		if (!$sure || !$really_sure) {
			$this->setMissingParamsError();
			return false;
		}
		if (!$this->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}
		db_begin();
		db_query("DELETE FROM artifact_extra_field_data
			WHERE EXISTS (SELECT artifact_id FROM artifact 
			WHERE group_artifact_id='".$this->getID()."'
			AND artifact.artifact_id=artifact_extra_field_data.artifact_id)");
//echo '0.1'.db_error();
		db_query("DELETE FROM artifact_extra_field_elements
			WHERE EXISTS (SELECT extra_field_id FROM artifact_extra_field_list 
			WHERE group_artifact_id='".$this->getID()."'
			AND artifact_extra_field_list.extra_field_id = artifact_extra_field_elements.extra_field_id)");
//echo '0.2'.db_error();
		db_query ("DELETE FROM artifact_extra_field_list
			WHERE group_artifact_id='".$this->getID()."'");
//echo '0.3'.db_error();
		db_query("DELETE FROM artifact_canned_responses 
			WHERE group_artifact_id='".$this->getID()."'");
//echo '1'.db_error();
		db_query("DELETE FROM artifact_perm
			WHERE group_artifact_id='".$this->getID()."'");
//echo '3'.db_error();
		db_query("DELETE FROM artifact_counts_agg
			WHERE group_artifact_id='".$this->getID()."'");
//echo '5'.db_error();
		db_query("DELETE FROM artifact_file
			WHERE EXISTS (SELECT artifact_id FROM artifact 
			WHERE group_artifact_id='".$this->getID()."'
			AND artifact.artifact_id=artifact_file.artifact_id)");
//echo '6'.db_error();
		db_query("DELETE FROM artifact_message
			WHERE EXISTS (SELECT artifact_id FROM artifact 
			WHERE group_artifact_id='".$this->getID()."'
			AND artifact.artifact_id=artifact_message.artifact_id)");
//echo '7'.db_error();
		db_query("DELETE FROM artifact_history
			WHERE EXISTS (SELECT artifact_id FROM artifact 
			WHERE group_artifact_id='".$this->getID()."'
			AND artifact.artifact_id=artifact_history.artifact_id)");
//echo '8'.db_error();
		db_query("DELETE FROM artifact_monitor
			WHERE EXISTS (SELECT artifact_id FROM artifact 
			WHERE group_artifact_id='".$this->getID()."'
			AND artifact.artifact_id=artifact_monitor.artifact_id)");
//echo '9'.db_error();
		db_query("DELETE FROM artifact
			WHERE group_artifact_id='".$this->getID()."'");
//echo '4'.db_error();
		db_query("DELETE FROM artifact_group_list
			WHERE group_artifact_id='".$this->getID()."'");
//echo '11'.db_error();
		
		db_commit();
		return true;
	}

	/**
	 *	getTechnicians - returns a result set of technicians.
	 *
	 *	@return database result set.
	 */
	function getTechnicians() {
		if (!isset($this->technicians_res)) {
			$sql="SELECT user_id,realname 
				FROM artifactperm_user_vw
				WHERE group_artifact_id='". $this->getID() ."' 
				AND perm_level in (1,2)
				ORDER BY realname";
			$this->technicians_res = db_query($sql);
		}
		return $this->technicians_res;
	}

	/**
	 *	getTechnicianObjects - Array of User objects set up for this artifact type.
	 *
	 *	@return	array	Of User objects.
	 */
	function &getTechnicianObjects() {
		$res = $this->getTechnicians();
		$arr =& util_result_column_to_array($res,0);
		return user_get_objects($arr);
	}

	/**
	 *	getCannedResponses - returns a result set of canned responses.
	 *
	 *	@return database result set.
	 */
	function getCannedResponses() {
		if (!isset($this->cannedresponses_res)) {
			$sql="SELECT id,title
				FROM artifact_canned_responses 
				WHERE group_artifact_id='". $this->getID() ."'";
			$this->cannedresponses_res = db_query($sql);
		}
		return $this->cannedresponses_res;
	}

	/**
	 *	getStatuses - returns a result set of statuses.
	 *
	 *	These statuses are either the default open/closed or any number of 
	 *	custom statuses that are stored in the extra fields. On insert/update
	 *	to an artifact the status_id is remapped from the extra_field_element_id to
	 *	the standard open/closed id.
	 *
	 *	@param	boolean	Whether to show the real statuses or not.
	 *	@return database result set.
	 */
	function getStatuses() {
		if (!isset($this->status_res)) {
			$sql="select * from artifact_status";
			$this->status_res=db_query($sql);
		}
		return $this->status_res;
	}

	/**
	 * getStatusName - returns the name of this status.
	 *
	 * @param	int		The status ID.
	 * @return	string	name.
	 */
	function getStatusName($id) {
		$sql="select status_name from artifact_status WHERE id='$id'";
		$result=db_query($sql);
		if ($result && db_numrows($result) > 0) {
			return db_result($result,0,'status_name');
		} else {
			return 'Error - Not Found';
		}
	}

	/**
	 *  addAllUsers - add all users to this artifact.
	 *
	 *  @return boolean success.
	 */
	function addAllUsers() {
		if (!$this->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}
		$sql="INSERT INTO artifact_perm (group_artifact_id,user_id,perm_level)
			SELECT '".$this->getID()."',user_id,artifact_flags
			FROM user_group
			WHERE
			group_id='".$this->Group->getID()."'
			AND NOT EXISTS (SELECT user_id FROM artifact_perm
			WHERE group_artifact_id='".$this->getID()."'
			AND user_id=user_group.user_id);";
		$res= db_query($sql);
		if (!$res) {
			$this->setError(db_error());
			return false;
		} else {
			return true;
		}
	}

	/**
	 *	addUser - add a user to this ArtifactType.
	 *
	 *	@param	int		user_id of the new user.
	 *	@return boolean	success.
	 */
	function addUser($id) {
		if (!$this->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}
		if (!$id) {
			$this->setMissingParamsError();
			return false;
		}
		$sql="SELECT * FROM artifact_perm 
			WHERE group_artifact_id='".$this->getID()."' 
			AND user_id='$id'";
		$result=db_query($sql);
		if (db_numrows($result) > 0) {
			return true;
		} else {
			$sql="INSERT INTO artifact_perm (group_artifact_id,user_id,perm_level) 
				VALUES ('".$this->getID()."','$id',0)";
			$result=db_query($sql);
			if ($result && db_affected_rows($result) > 0) {
				return true;
			} else {
				$this->setError(db_error());
				return false;
			}
		}
	}

	/**
	 *	updateUser - update a user's permissions.
	 *
	 *	@param	int		user_id of the user to update.
	 *	@param	int		(0) read only, (1) tech only, (2) admin & tech (3) admin only.
	 *	@return boolean	success.
	 */
	function updateUser($id,$perm_level) {
		if (!$this->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}
		if (!$id) {
			$this->setMissingParamsError();
			return false;
		}
		$sql="UPDATE artifact_perm SET perm_level='$perm_level'
			WHERE user_id='$id' AND group_artifact_id='".$this->getID()."'";
		$result=db_query($sql);
		if (db_affected_rows($result) < 1) {
			//
			//  If not, insert it.
			//
			$sql="INSERT INTO artifact_perm (group_artifact_id,user_id,perm_level) VALUES
				('".$this->getID()."','$id','$perm_level')";
			$result=db_query($sql);
			if (!$result) {
				$this->setError(db_error());
				return false;
			} else {
				return true;
			}
		} else {
			return true;
		}
	}

	/**
	 *	deleteUser - delete a user's permissions.
	 *
	 *	@param	int		user_id of the user who's permissions to delete.
	 *	@return boolean	success.
	 */
	function deleteUser($id) {
		if (!$this->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}
		if (!$id) {
			$this->setMissingParamsError();
			return false;
		}
		$sql="DELETE FROM artifact_perm
			WHERE user_id='$id' AND group_artifact_id='".$this->getID()."'";
		$result=db_query($sql);
		if ($result) {
			return true;
		} else {
			$this->setError(db_error());
			return false;
		}
	}

	/*

		USER PERMISSION FUNCTIONS

	*/

	/**
	 *	  userCanView - determine if the user can view this artifact type.
	 *
	 *	  @return boolean	user_can_view.
	 */
	function userCanView() {
		if ($this->isPublic()) {
			return true;
		} else {
			if (!session_loggedin()) {
				return false;
			} else {
				//
				//	You must have an entry in artifact_perm if this tracker is not public
				//
				if ($this->userIsAdmin() || $this->getCurrentUserPerm() >= 0) {
					return true;
				} else {
					return false;
				}
			}
		}
	}

	/**
	 *	userIsAdmin - see if the logged-in user's perms are >= 2 or Group ArtifactAdmin.
	 *
	 *	@return boolean	user_is_admin.
	 */
	function userIsAdmin() { 
		if (!session_loggedin()) {
			return false;
		} else {
			$perm =& $this->Group->getPermission( session_get_user() );

			if (($this->getCurrentUserPerm() >= 2) || ($perm->isArtifactAdmin())) {
				return true;
			} else {
				return false;
			}
		}
	}

	/**
	 *	userIsTechnician - see if the logged-in user's perms are >= 1 or Group ArtifactAdmin.
	 *
	 *	@return boolean	user_is_technician.
	 */
	function userIsTechnician() { 
		if (!session_loggedin()) {
			return false;
		} else {
			$perm =& $this->Group->getPermission( session_get_user() );

			if (($this->getCurrentUserPerm() >= 1) || ($perm->isArtifactAdmin())) {
				return true;
			} else {
				return false;
			}
		}
	}

	/**
	 *	getCurrentUserPerm - get the logged-in user's perms from artifact_perm.
	 *
	 *	@return int perm level for the logged-in user.
	 */
	function getCurrentUserPerm() {
		if (!session_loggedin()) {
			return 0;
		} else {
			if (!isset($this->current_user_perm)) {
				$sql="select perm_level
				FROM artifact_perm
				WHERE group_artifact_id='". $this->getID() ."'
				AND user_id='".user_getid()."'";
				$this->current_user_perm=db_result(db_query($sql),0,0);
			}
			return $this->current_user_perm;
		}
	}

	/**
	 *  update - use this to update this ArtifactType in the database.
	 *
	 *  @param	string	The item name.
	 *  @param	string	The item description.
	 *  @param	bool	(1) true (0) false - whether to email on all updates.
	 *  @param	string	The address to send new entries and updates to.
	 *  @param	int		Days before this item is considered overdue.
	 *  @param	int		Days before stale items time out.
	 *  @param	bool	(1) true (0) false - whether the resolution box should be shown.
	 *  @param	string	Free-form string that project admins can place on the submit page.
	 *  @param	string	Free-form string that project admins can place on the browse page.
	 *  @return true on success, false on failure.
	 */
	function update($name,$description,$email_all,$email_address,
		$due_period, $status_timeout,$use_resolution,$submit_instructions,$browse_instructions) {

		global $Language;

		if (!$this->userIsAdmin()) {
			$this->setPermissionDeniedError();
			return false;
		}

		if ($this->getDataType()) {
			$name=$this->getName();
			$description=$this->getDescription();
		}
		
		if (!$name || !$description || !$due_period || !$status_timeout) {
			$this->setError($Language->getText('tracker_artifacttype','required_fields'));
			return false;
		}
		
		if ($email_address) {
			$invalid_emails = validate_emails($email_address);
			if (count($invalid_emails) > 0) {
				$this->SetError($Language->getText('tracker_artifacttype','invalid_emails').': '.implode(',',$invalid_emails));
				return false;
			}
		}

		$email_all = ((!$email_all) ? 0 : $email_all); 
		$use_resolution = ((!$use_resolution) ? 0 : $use_resolution); 

		$sql="UPDATE artifact_group_list SET 
			name='". htmlspecialchars($name). "',
			description='". htmlspecialchars($description) ."',
			email_all_updates='$email_all',
			email_address='$email_address',
			due_period='". ($due_period * (60*60*24)) ."',
			status_timeout='". ($status_timeout * (60*60*24)) . "',
			submit_instructions='". htmlspecialchars($submit_instructions)."',
			browse_instructions='" .htmlspecialchars($browse_instructions)."'
			WHERE 
			group_artifact_id='". $this->getID() ."' 
			AND group_id='". $this->Group->getID() ."'";

		$res=db_query($sql);
		if (!$res || db_affected_rows($res) < 1) {
			$this->setError('ArtifactType::Update(): '.db_error());
			return false;
		} else {
			$this->fetchData($this->getID());
			return true;
		}
	}

}

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

?>