3 * ArtifactQuery.class.php - Class to handle user defined artifacts
5 * Copyright 2005 (c) GForge Group, LLC; Anthony J. Pugliese,
9 * This file is part of GForge.
11 * GForge is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * GForge is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with GForge; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US
25 require_once('common/include/Error.class.php');
27 define('ARTIFACT_QUERY_ASSIGNEE',1);
28 define('ARTIFACT_QUERY_STATE',2);
29 define('ARTIFACT_QUERY_MODDATE',3);
30 define('ARTIFACT_QUERY_EXTRAFIELD',4);
31 define('ARTIFACT_QUERY_SORTCOL',5);
32 define('ARTIFACT_QUERY_SORTORD',6);
33 define('ARTIFACT_QUERY_OPENDATE',7);
34 define('ARTIFACT_QUERY_CLOSEDATE',8);
36 require_once('common/tracker/ArtifactType.class.php');
38 class ArtifactQuery extends Error {
40 * The artifact type object.
42 * @var object $ArtifactType.
44 var $ArtifactType; //object
47 * Array of artifact data.
49 * @var array $data_array.
54 * ArtifactQuery - Constructer
56 * @param object ArtifactType object.
58 * @return boolean success.
60 function ArtifactQuery(&$ArtifactType, $data = false) {
63 //was ArtifactType legit?
64 if (!$ArtifactType || !is_object($ArtifactType)) {
65 $this->setError('ArtifactQuery: No Valid ArtifactType');
68 //did ArtifactType have an error?
69 if ($ArtifactType->isError()) {
70 $this->setError('ArtifactQuery: '.$ArtifactType->getErrorMessage());
73 $this->ArtifactType =& $ArtifactType;
76 if (is_array($data)) {
77 $this->data_array =& $data;
80 if (!$this->fetchData($data)) {
90 * create - create a row in the table that stores a saved query for * a tracker.
92 * @param string Name of the saved query.
93 * @return true on success / false on failure.
95 function create($name,$status,$assignee,$moddaterange,$sort_col,$sort_ord,$extra_fields,$opendaterange=0,$closedaterange=0) {
102 $this->setMissingParamsError();
105 if (!session_loggedin()) {
106 $this->setError('Must Be Logged In');
110 if ($this->Exist(htmlspecialchars($name))) {
111 $this->setError(_('Query already exists'));
115 $sql="INSERT INTO artifact_query (group_artifact_id,query_name,user_id)
116 VALUES ('".$this->ArtifactType->getID()."','".htmlspecialchars($name)."','".user_getid()."')";
119 $result=db_query($sql);
120 if ($result && db_affected_rows($result) > 0) {
122 $id=db_insertid($result,'artifact_query','artifact_query_id');
124 $this->setError('Error getting id '.db_error());
128 if (!$this->insertElements($id,$status,$assignee,$moddaterange,$sort_col,$sort_ord,$extra_fields,$opendaterange,$closedaterange)) {
134 $this->setError(db_error());
139 // Now set up our internal data structures
141 if ($this->fetchData($id)) {
151 * fetchData - re-fetch the data for this ArtifactQuery from the database.
153 * @param int ID of saved query.
154 * @return boolean success.
156 function fetchData($id) {
157 $res=db_query("SELECT * FROM artifact_query WHERE artifact_query_id='$id'");
159 if (!$res || db_numrows($res) < 1) {
160 $this->setError('ArtifactQuery: Invalid ArtifactQuery ID'.db_error());
163 $this->data_array =& db_fetch_array($res);
164 db_free_result($res);
165 $res=db_query("SELECT * FROM artifact_query_fields WHERE artifact_query_id='$id'");
166 unset($this->element_array);
167 while ($arr = db_fetch_array($res)) {
169 // Some things may have been saved as comma-separated items
171 if (strstr($arr['query_field_values'],',')) {
172 $arr['query_field_values']=explode(',',$arr['query_field_values']);
174 $this->element_array[$arr['query_field_type']][$arr['query_field_id']]=$arr['query_field_values'];
180 * getArtifactType - get the ArtifactType Object this ArtifactExtraField is associated with.
182 * @return object ArtifactType.
184 function &getArtifactType() {
185 return $this->ArtifactType;
192 function insertElements($id,$status,$assignee,$moddaterange,$sort_col,$sort_ord,$extra_fields,$opendaterange,$closedaterange) {
193 $res=db_query("DELETE FROM artifact_query_fields WHERE artifact_query_id='$id'");
195 $this->setError('Deleting Old Elements: '.db_error());
199 $res=db_query("INSERT INTO artifact_query_fields
200 (artifact_query_id,query_field_type,query_field_id,query_field_values)
201 VALUES ('$id','".ARTIFACT_QUERY_STATE."','0','".intval($status)."')");
203 $this->setError('Setting Status: '.db_error());
207 if (is_array($assignee)) {
208 for($e=0; $e<count($assignee); $e++) {
209 $assignee[$e]=intval($assignee[$e]);
211 $assignee=implode(',',$assignee);
213 $assignee = intval($assignee);
216 if (preg_match("/[^[:alnum:]_]/", $string)) {
217 $this->setError('ArtifactQuery: not valid sort_col');
221 if (preg_match("/[^[:alnum:]_]/", $string)) {
222 $this->setError('ArtifactQuery: not valid sort_ord');
226 //CSV LIST OF ASSIGNEES
227 $res=db_query("INSERT INTO artifact_query_fields
228 (artifact_query_id,query_field_type,query_field_id,query_field_values)
229 VALUES ('$id','".ARTIFACT_QUERY_ASSIGNEE."','0','".$assignee."')");
231 $this->setError('Setting Assignee: '.db_error());
235 //MOD DATE RANGE YYYY-MM-DD YYYY-MM-DD format
236 if ($moddaterange && !$this->validateDateRange($moddaterange)) {
237 $this->setError('Invalid Mod Date Range');
240 $res=db_query("INSERT INTO artifact_query_fields
241 (artifact_query_id,query_field_type,query_field_id,query_field_values)
242 VALUES ('$id','".ARTIFACT_QUERY_MODDATE."','0','".$moddaterange."')");
244 $this->setError('Setting Last Modified Date Range: '.db_error());
248 //OPEN DATE RANGE YYYY-MM-DD YYYY-MM-DD format
249 if ($opendaterange && !$this->validateDateRange($opendaterange)) {
250 $this->setError('Invalid Open Date Range');
253 $res=db_query("INSERT INTO artifact_query_fields
254 (artifact_query_id,query_field_type,query_field_id,query_field_values)
255 VALUES ('$id','".ARTIFACT_QUERY_OPENDATE."','0','".$opendaterange."')");
257 $this->setError('Setting Open Date Range: '.db_error());
261 //CLOSE DATE RANGE YYYY-MM-DD YYYY-MM-DD format
262 if ($closedaterange && !$this->validateDateRange($closedaterange)) {
263 $this->setError('Invalid Close Date Range');
266 $res=db_query("INSERT INTO artifact_query_fields
267 (artifact_query_id,query_field_type,query_field_id,query_field_values)
268 VALUES ('$id','".ARTIFACT_QUERY_CLOSEDATE."','0','".$closedaterange."')");
270 $this->setError('Setting Close Date Range: '.db_error());
275 $res=db_query("INSERT INTO artifact_query_fields
276 (artifact_query_id,query_field_type,query_field_id,query_field_values)
277 VALUES ('$id','".ARTIFACT_QUERY_SORTCOL."','0','".$sort_col."')");
279 $this->setError('Setting Sort Col: '.db_error());
282 $res=db_query("INSERT INTO artifact_query_fields
283 (artifact_query_id,query_field_type,query_field_id,query_field_values)
284 VALUES ('$id','".ARTIFACT_QUERY_SORTORD."','0','".$sort_ord."')");
286 $this->setError('Setting Sort Order: '.db_error());
290 if (!$extra_fields) {
291 $extra_fields=array();
294 $keys=array_keys($extra_fields);
295 $vals=array_values($extra_fields);
296 for ($i=0; $i<count($keys); $i++) {
301 // Checkboxes and multi-select may be arrays so store it comma-separated
303 if (is_array($vals[$i])) {
304 for($e=0; $e<count($vals[$i]); $e++) {
305 $vals[$i][$e]=intval($vals[$i][$e]);
307 $vals[$i]=implode(',',$vals[$i]);
309 $vals[$i] = intval($vals[$i]);
311 $res=db_query("INSERT INTO artifact_query_fields
312 (artifact_query_id,query_field_type,query_field_id,query_field_values)
313 VALUES ('$id','".ARTIFACT_QUERY_EXTRAFIELD."','".((int)$keys[$i]) ."','". $vals[$i] ."')");
315 $this->setError('Setting values: '.db_error());
323 * getID - get this ArtifactQuery ID.
325 * @return int The id #.
328 return $this->data_array['artifact_query_id'];
332 * getName - get the name.
334 * @return string The name.
337 return $this->data_array['query_name'];
341 * getSortCol - the column that you're sorting on
343 * @return string The column name.
345 function getSortCol() {
346 return $this->element_array[ARTIFACT_QUERY_SORTCOL][0];
350 * getSortOrd - ASC or DESC
352 * @return string ASC or DESC
354 function getSortOrd() {
355 return $this->element_array[ARTIFACT_QUERY_SORTORD][0];
359 * getModDateRange - get the range of dates to include in a query
361 * @return string mod date range.
363 function getModDateRange() {
364 if ($this->element_array[ARTIFACT_QUERY_MODDATE][0]) {
365 return $this->element_array[ARTIFACT_QUERY_MODDATE][0];
372 * getOpenDateRange - get the range of dates to include in a query
374 * @return string Open date range.
376 function getOpenDateRange() {
377 if ($this->element_array[ARTIFACT_QUERY_OPENDATE][0]) {
378 return $this->element_array[ARTIFACT_QUERY_OPENDATE][0];
385 * getCloseDateRange - get the range of dates to include in a query
387 * @return string Close date range.
389 function getCloseDateRange() {
390 if ($this->element_array[ARTIFACT_QUERY_CLOSEDATE][0]) {
391 return $this->element_array[ARTIFACT_QUERY_CLOSEDATE][0];
400 * @return string Assignee ID
402 function getAssignee() {
403 return $this->element_array[ARTIFACT_QUERY_ASSIGNEE][0];
409 * @return string Status ID
411 function getStatus() {
412 return $this->element_array[ARTIFACT_QUERY_STATE][0];
416 * getExtraFields - complex multi-dimensional array of extra field IDs/Vals
418 * @return array Complex Array
420 function getExtraFields() {
421 return $this->element_array[ARTIFACT_QUERY_EXTRAFIELD];
425 * validateDateRange - validate a date range in this format '1999-05-01 1999-06-01'.
427 * @return boolean true/false.
429 function validateDateRange($daterange) {
430 return preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{4}-[0-9]{2}-[0-9]{2}/',$daterange);
434 * update - update a row in the table used to query names
437 * @param int Id of the saved query
438 * @param string The name of the saved query
439 * @return boolean success.
441 function update($name,$status,$assignee,$moddaterange,$sort_col,$sort_ord,$extra_fields,$opendaterange='',$closedaterange='') {
444 $this->setMissingParamsError();
447 if (!session_loggedin()) {
448 $this->setError('Must Be Logged In');
451 if (!$this->Exist(htmlspecialchars($name))) {
452 $this->setError(_('Query does not exist'));
455 $sql="UPDATE artifact_query
457 query_name='".htmlspecialchars($name)."'
458 WHERE artifact_query_id='".$this->getID()."'
459 AND user_id='".user_getid()."'";
461 $result=db_query($sql);
462 if ($result && db_affected_rows($result) > 0) {
463 if (!$this->insertElements($this->getID(),$status,$assignee,$moddaterange,$sort_col,$sort_ord,$extra_fields,$opendaterange,$closedaterange)) {
468 $this->fetchData($this->getID());
472 $this->setError('Error Updating: '.db_error());
479 * makeDefault - set this as the default query
481 * @return boolean success.
483 function makeDefault() {
484 if (!session_loggedin()) {
485 $this->setError('Must Be Logged In');
488 $usr =& session_get_user();
489 return $usr->setPreference('art_query'.$this->ArtifactType->getID(),$this->getID());
493 $res=db_query("DELETE FROM artifact_query WHERE artifact_query_id='".$this->getID()."'
494 AND user_id='".user_getid()."'");
495 $res=db_query("DELETE FROM user_preferences WHERE preference_value='".$this->getID()."'
496 AND preference_name 'art_query".$this->ArtifactType->getID()."'");
497 unset($this->data_array);
498 unset($this->element_array);
502 * Exist - check if already exist a query with the same name , user_id and artifact_id
504 * @return boolean exist
506 function Exist($name) {
507 $user_id = user_getid();
508 $art_id = $this->ArtifactType->getID();
509 $sql = "SELECT * FROM artifact_query WHERE group_artifact_id = '$art_id' AND query_name = '$name' AND user_id = '$user_id'";
510 $res = db_query($sql);
511 if (db_numrows($res)>0) {