File: ArtifactFactory.class.php

package info (click to toggle)
fusionforge 5.3.2%2B20141104-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 60,472 kB
  • sloc: php: 271,846; sql: 36,817; python: 14,575; perl: 6,406; sh: 5,980; xml: 4,294; pascal: 1,411; makefile: 911; cpp: 52; awk: 27
file content (507 lines) | stat: -rw-r--r-- 15,389 bytes parent folder | download | duplicates (3)
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
<?php
/**
 * FusionForge trackers
 *
 * Copyright 2002, GForge, LLC
 * Copyright (C) 2011 Alain Peyrat - Alcatel-Lucent
 * Copyright 2014, Franck Villaume - TrivialDev
 *
 * This file is part of FusionForge. FusionForge 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 Licence, or (at your option)
 * any later version.
 *
 * FusionForge 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 FusionForge; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

require_once $gfcommon.'include/Error.class.php';
require_once $gfcommon.'tracker/Artifact.class.php';
require_once $gfcommon.'tracker/ArtifactType.class.php';
require_once $gfcommon.'tracker/ArtifactQuery.class.php';

class ArtifactFactory extends Error {

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

	/**
	 * The artifacts array.
	 *
	 * @var	array	artifacts.
	 */
	var $artifacts = array();
	var $order_col;
	var $sort;
	var $status;
	var $changed_from;
	var $last_changed;
	var $submitted_by;
	var $assigned_to;
	var $offset;
	var $max_rows;
	var $fetched_rows;
	var $extra_fields;
	var $defaultquery;
	var $moddaterange;
	var $opendaterange;
	var $closedaterange;
	var $summary;
	var $description;
	var $followups;

	var $query_type;		// query, default, custom
	var $query_id;			// id of the query (when query_type=query)

	/**
	 * __construct - Constructor.
	 *
	 * @param	object	$ArtifactType	The ArtifactType object to which this ArtifactFactory is associated.
	 */
	function __construct(&$ArtifactType) {
		$this->Error();
		if (!$ArtifactType || !is_object($ArtifactType)) {
			$this->setError('ArtifactFactory:: No Valid ArtifactType Object');
			return;
		}
		if ($ArtifactType->isError()) {
			$this->setError('ArtifactFactory:: '.$ArtifactType->getErrorMessage());
			return;
		}
		$this->ArtifactType =& $ArtifactType;
		$this->changed_from = 0x7ffffff; // Any
	}

	/**
	 * setup - sets up limits and sorts before you call getTasks().
	 *
	 * @param	int	$offset		The offset - number of rows to skip.
	 * @param	string	$order_col	The column to sort on.
	 * @param	string	$sort		The way to order - ASC or DESC.
	 * @param	int	$max_rows	The max number of rows to return.
	 * @param	string	$set		Whether to set these prefs into the user_prefs table - use "custom".
	 * @param	int	$_assigned_to	Include this param if you want to limit to a certain assignee.
	 * @param	int	$_status	Include this param if you want to limit to a particular status.
	 * @param	array	$_extra_fields	Array of extra fields & elements to limit the query to.
	 */
	function setup($offset,$order_col,$sort,$max_rows,$set,$_assigned_to,$_status,$_extra_fields=array()) {

		if ((!$offset) || ($offset < 0)) {
			$this->offset=0;
		} else {
			$this->offset=$offset;
		}

		// $max_rows == 0 means we want all the rows
		if (is_null($max_rows) || $max_rows < 0) {
			$this->max_rows = 50 ;
		} else {
			$this->max_rows = $max_rows ;
		}

		if (session_loggedin()) {
			$u =& session_get_user();
		}
		if (!is_array($_extra_fields)) {
			$_extra_fields=array();
		}

		$_changed=0;
		if (!$set) {
			/*
				if no set is passed in, see if a preference was set
				if no preference or not logged in, use open set
			*/
			$this->query_type = '';
			if (session_loggedin()) {
				$query_id=$u->getPreference('art_query'.$this->ArtifactType->getID());
				if ($query_id) {
					$this->query_type = 'query';
					$this->query_id = $query_id;
				} else {
					$custom_pref=$u->getPreference('art_cust'.$this->ArtifactType->getID());
					if ($custom_pref) {
//$_assigned_to.'|'.$_status.'|'.$_order_col.'|'.$_sort_ord.'|'.$_changed.'|'.serialize($_extra_fields);
						$this->query_type = 'custom';
						$pref_arr=explode('|',$custom_pref);
						$_assigned_to=$pref_arr[0];
						$_status=$pref_arr[1];
						$order_col=$pref_arr[2];
						$sort=$pref_arr[3];
						$_changed=$pref_arr[4];
						if ($this->ArtifactType->usesCustomStatuses()) {
							$_extra_fields=unserialize($pref_arr[5]);
						} else {
							$_status=$pref_arr[1];
						}
						$set='custom';
					}
				}
			} elseif (isset($_COOKIE["GFTrackerQuery"])) {
				$gf_tracker = unserialize($_COOKIE["GFTrackerQuery"]);
				$query_id = (int)$gf_tracker[$this->ArtifactType->getID()];
				if ($query_id) {
					$this->query_type = 'query';
					$this->query_id = $query_id;
				}
			}

			if (!$this->query_type) {
				$res = db_query_params ('SELECT artifact_query_id FROM artifact_query
					WHERE group_artifact_id=$1
					AND query_type=2',
							array($this->ArtifactType->getID()));
				if (db_numrows($res)>0) {
					$this->query_type = 'query';
					$this->query_id = db_result($res, 0, 'artifact_query_id');
				}
			}

			if (!$this->query_type) {
				//default to all opened
				$this->query_type = 'default';
				$_assigned_to=0;
				$_status=1;
				$_changed=0;
			}

			if ($this->query_type == 'query') {
				$aq = new ArtifactQuery($this->ArtifactType, $this->query_id);
				$this->submitted_by=$aq->getSubmitter();
				$_assigned_to=$aq->getAssignee();
				$_status=$aq->getStatus();
				$_extra_fields=$aq->getExtraFields();
				$this->moddaterange = $aq->getModDateRange();
				$this->opendaterange = $aq->getOpenDateRange();
				$this->closedaterange = $aq->getCloseDateRange();
				$this->summary = $aq->getSummary();
				$this->description = $aq->getDescription();
				$this->followups = $aq->getFollowups();
				$order_col=$aq->getSortCol();
				$sort=$aq->getSortOrd();
			}
		}

		//
		//  validate the column names and sort order passed in from user
		//  before saving it to prefs
		//
		$allowed_order_col = array ('artifact_id',
					   'summary',
					   'open_date',
					   'close_date',
					   'assigned_to',
					   'submitted_by',
					   'priority',
					   'last_modified_date',
					   '_votes',
					   '_voters',
					   '_votage') ;
		$efarr = $this->ArtifactType->getExtraFields();
		$keys=array_keys($efarr);
		for ($k=0; $k<count($keys); $k++) {
			$i=$keys[$k];
			$allowed_order_col[] = $efarr[$i]['extra_field_id'];
		}

		$_order_col = util_ensure_value_in_set ($order_col,
							$allowed_order_col);
		$_sort_ord = util_ensure_value_in_set ($sort,
						       array ('ASC', 'DESC')) ;
		if ($set=='custom') {
			$this->query_type = 'custom';
			if (session_loggedin()) {
				/*
					if this custom set is different than the stored one, reset preference
				*/
				if (is_array($_assigned_to)) {
					$_assigned_to='';
				}
				$aux_extra_fields = array();
				if (is_array($_extra_fields)){
					//print_r($_extra_fields);
					$keys=array_keys($_extra_fields);

					foreach ($keys as $key) {
						if ($_extra_fields[$key] != 'Array') {
							$aux_extra_fields[$key] = $_extra_fields[$key];
						}
					}
				}

				$extra_pref = '';
				if (count($aux_extra_fields)>0) {
					$extra_pref = '|'.serialize($aux_extra_fields);
				}

				$pref_=$_assigned_to.'|'.$_status.'|'.$_order_col.'|'.$_sort_ord.'|'.$_changed.$extra_pref;
				if ($pref_ != $u->getPreference('art_cust'.$this->ArtifactType->getID())) {
					$u->setPreference('art_cust'.$this->ArtifactType->getID(),$pref_);
				}
				$default_query=$u->getPreference('art_query'.$this->ArtifactType->getID());
				if ($default_query) {
					$u->deletePreference('art_query'.$this->ArtifactType->getID());
				}
			}
			$_changed=0;
		}

		$this->sort=$_sort_ord;
		$this->order_col=$_order_col;
		$this->status=$_status;
		$this->assigned_to=$_assigned_to;
		$this->extra_fields=$_extra_fields;
		$this->setChangedFrom($_changed);
	}

	/**
	 * setChangedFrom - sets up changed-from and last-changed before you call getTasks().
	 *
	 * @param	int	$changed_from	The changed_from - offset time(sec) from now
	 */
	function setChangedFrom($changed_from) {
		$this->changed_from = ($changed_from <= 0) ? 0x7fffffff : $changed_from;
		$this->last_changed = time() - $this->changed_from;
	}

	/**
	 * getDefaultQuery - get the default query
	 *
	 * @return	int
	 */
	function getDefaultQuery() {
		if ($this->query_type == 'query')
			return $this->query_id;
		else
			return '';
	}

	/**
	 * getArtifacts - get an array of Artifact objects.
	 *
	 * @return	Artifact[]	Array of Artifact objects.
	 */
	function getArtifacts() {
		if (!empty($this->artifacts)) {
			return $this->artifacts;
		}

		$params = array() ;
		$paramcount = 1 ;

		$selectsql = 'SELECT DISTINCT ON (group_artifact_id, artifact_id) artifact_vw.* FROM artifact_vw';

		$wheresql = ' WHERE group_artifact_id=$'.$paramcount++ ;
		$params[] = $this->ArtifactType->getID() ;

		if (is_array($this->extra_fields) && !empty($this->extra_fields)) {
			$keys=array_keys($this->extra_fields);
			$vals=array_values($this->extra_fields);
			for ($i=0; $i<count($keys); $i++) {
				if (empty($vals[$i])) {
					continue;
				}
				$selectsql .= ', artifact_extra_field_data aefd'.$i;
				$wheresql .= ' AND aefd'.$i.'.extra_field_id=$'.$paramcount++ ;
				$params[] = $keys[$i] ;

				// Hack: Determine the type of the element to get the right search query.
				$res = db_query_params ('SELECT field_type FROM artifact_extra_field_list WHERE extra_field_id=$1',
							array($keys[$i])) ;
				$type = db_result($res,0,'field_type');
				if ($type == 4 or $type == 6) {
					$wheresql .= ' AND aefd'.$i.'.field_data LIKE $'.$paramcount++ ;
					$params[] = $vals[$i];
				} else {
					if (is_array($vals[$i])) {
						$wheresql .= ' AND aefd'.$i.'.field_data = ANY ($'.$paramcount++ .')' ;
						$params[] = db_string_array_to_any_clause ($vals[$i]) ;
					} else {
						$wheresql .= ' AND aefd'.$i.'.field_data = $'.$paramcount++ ;
						$params[] = $vals[$i];
					}
				}
				$wheresql .= ' AND aefd'.$i.'.artifact_id=artifact_vw.artifact_id' ;
			}
		}

		//if status selected, and more to where clause
		if ($this->status && ($this->status != 100)) {
			//for open tasks, add status=100 to make sure we show all
			$wheresql .= ' AND status_id=$'.$paramcount++ ;
			$params[] = $this->status;
		}

		// Add filter if submitted_by is selected.
		if ($this->submitted_by) {
			if (is_array($this->submitted_by)) {
				$wheresql .= ' AND submitted_by = ANY ($'.$paramcount++ ;
				$params[] = db_int_array_to_any_clause ($this->submitted_by) ;
				$wheresql .= ')' ;
			} else {
				$wheresql .= ' AND submitted_by = $'.$paramcount++ ;
				$params[] = $this->submitted_by ;
			}
		}

		//if assigned to selected, and more to where clause
		if ($this->assigned_to) {
			if (is_array($this->assigned_to)) {
				$wheresql .= ' AND assigned_to = ANY ($'.$paramcount++ ;
				$params[] = db_int_array_to_any_clause ($this->assigned_to) ;
				$wheresql .= ')' ;
			} else {
				$wheresql .= ' AND assigned_to = $'.$paramcount++ ;
				$params[] = $this->assigned_to ;
			}
		}

		if ($this->last_changed > 0) {
			$wheresql .= ' AND last_modified_date > $'.$paramcount++ ;
			$params[] = $this->last_changed ;
		}

		//add constraint of range of modified dates
		if ($this->moddaterange) {
			$range_arr=explode(' ',$this->moddaterange);
			$begin_int = strtotime($range_arr[0]);
			$end_int=strtotime($range_arr[1])+(24*60*60);
			$wheresql .= ' AND (last_modified_date BETWEEN $'.$paramcount++ ;
			$params[] = $begin_int ;
			$wheresql .= ' AND $'.$paramcount++ ;
			$params[] = $end_int ;
			$wheresql .= ')' ;
		}
		//add constraint of range of open dates
		if ($this->opendaterange) {
			$range_arr=explode(' ',$this->opendaterange);
			$begin_int = strtotime($range_arr[0]);
			$end_int=strtotime($range_arr[1])+(24*60*60);
			$wheresql .= ' AND (open_date BETWEEN $'.$paramcount++ ;
			$params[] = $begin_int ;
			$wheresql .= ' AND $'.$paramcount++ ;
			$params[] = $end_int ;
			$wheresql .= ')' ;
		}
		//add constraint of range of close dates
		if ($this->closedaterange) {
			$range_arr=explode(' ',$this->closedaterange);
			$begin_int = strtotime($range_arr[0]);
			$end_int=strtotime($range_arr[1])+(24*60*60);
			$wheresql .= ' AND (close_date BETWEEN $'.$paramcount++ ;
			$params[] = $begin_int ;
			$wheresql .= ' AND $'.$paramcount++ ;
			$params[] = $end_int ;
			$wheresql .= ')' ;
		}

		//add constraint on the summary string.
		if ($this->summary) {
			$wheresql .= ' AND summary LIKE $'.$paramcount++ ;
			$params[] = $this->summary;
		}
		//add constraint on the description string.
		if ($this->description) {
			$wheresql .= ' AND details LIKE $'.$paramcount++ ;
			$params[] = $this->description;
		}
		//add constraint on the followups string.
		if ($this->followups) {
			$selectsql .= ' LEFT OUTER JOIN artifact_message am USING (artifact_id)';
			$wheresql .= ' AND am.body LIKE $'.$paramcount++;
			$params[] = $this->followups;
		}

		$sortorder = util_ensure_value_in_set ($this->sort,
						       array ('ASC', 'DESC')) ;

		$sortcol = util_ensure_value_in_set ($this->order_col,
						     array ('extra',
							    'artifact_id',
							    'summary',
							    'open_date',
							    'close_date',
							    'assigned_to',
							    'submitted_by',
							    'priority',
							    '_votage',
							    '_voters',
							    '_votes'));

		if ($sortcol != 'extra' && $sortcol != '_votes' && $sortcol != '_voters' && $sortcol != '_votage' ) {
			$ordersql = " ORDER BY Artifacts.group_artifact_id $sortorder, Artifacts.$sortcol $sortorder" ;
		} else {
			$ordersql = ''  ;
		}

		$result = db_query_params ('SELECT * FROM (' . $selectsql . $wheresql . ') AS Artifacts' . $ordersql,
					   $params) ;
		$rows = db_numrows($result);
		$this->fetched_rows=$rows;
		if (db_error()) {
			$this->setError('Database Error: '.db_error());
			return false;
		} else {
			while ($arr = db_fetch_array($result)) {
				$this->artifacts[] = new Artifact($this->ArtifactType, $arr);
			}
		}
		if ($sortcol == 'extra' || $sortcol == '_votes' || $sortcol == '_voters' || $sortcol == '_votage') {
			sortArtifactList ($this->artifacts, $this->order_col, $this->sort) ;
		}
		return $this->artifacts;
	}

	/**
	 * getArtifactsByReleases - get an array of Artifact objects.
	 *
	 * @param	$extra_field_id
	 * @param	$releases
	 * @return	array		The array of Artifact objects.
	 */
	function getArtifactsByReleases($extra_field_id, $releases) {
		$artifacts = array();

		$sql = 'SELECT a.*
		FROM artifact_extra_field_data aefd, artifact_extra_field_elements aefe, artifact_vw a
		WHERE aefd.extra_field_id=aefe.extra_field_id
		AND CAST (aefd.field_data AS integer)=aefe.element_id
		AND aefd.artifact_id=a.artifact_id
		AND aefd.extra_field_id=$1';

		$query_params = array($extra_field_id);

		if ($releases) {
			$query_params[] = db_string_array_to_any_clause($releases);
			$sql .= ' AND aefe.element_name = ANY ($2)';
		}

		$sql .= ' ORDER BY a.artifact_id';

		$result = db_query_params($sql, $query_params);
		if ($result && db_numrows($result)) {
			while ($arr = db_fetch_array($result)) {
				$artifacts[] = new Artifact($this->ArtifactType, $arr);
			}
		}

		return $artifacts;
	}

}

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