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 $gfcommon.'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 $gfcommon.'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) {
100 $this->setMissingParamsError();
103 if (!session_loggedin()) {
104 $this->setError('Must Be Logged In');
108 if ($this->Exist(htmlspecialchars($name))) {
109 $this->setError(_('Query already exists'));
113 $sql="INSERT INTO artifact_query (group_artifact_id,query_name,user_id)
114 VALUES ('".$this->ArtifactType->getID()."','".htmlspecialchars($name)."','".user_getid()."')";
117 $result=db_query($sql);
118 if ($result && db_affected_rows($result) > 0) {
120 $id=db_insertid($result,'artifact_query','artifact_query_id');
122 $this->setError('Error getting id '.db_error());
126 if (!$this->insertElements($id,$status,$assignee,$moddaterange,$sort_col,$sort_ord,$extra_fields,$opendaterange,$closedaterange)) {
132 $this->setError(db_error());
137 // Now set up our internal data structures
139 if ($this->fetchData($id)) {
149 * fetchData - re-fetch the data for this ArtifactQuery from the database.
151 * @param int ID of saved query.
152 * @return boolean success.
154 function fetchData($id) {
155 $res=db_query("SELECT * FROM artifact_query WHERE artifact_query_id='$id'");
157 if (!$res || db_numrows($res) < 1) {
158 $this->setError('ArtifactQuery: Invalid ArtifactQuery ID'.db_error());
161 $this->data_array =& db_fetch_array($res);
162 db_free_result($res);
163 $res=db_query("SELECT * FROM artifact_query_fields WHERE artifact_query_id='$id'");
164 unset($this->element_array);
165 while ($arr = db_fetch_array($res)) {
167 // Some things may have been saved as comma-separated items
169 if (strstr($arr['query_field_values'],',')) {
170 $arr['query_field_values']=explode(',',$arr['query_field_values']);
172 $this->element_array[$arr['query_field_type']][$arr['query_field_id']]=$arr['query_field_values'];
178 * getArtifactType - get the ArtifactType Object this ArtifactExtraField is associated with.
180 * @return object ArtifactType.
182 function &getArtifactType() {
183 return $this->ArtifactType;
190 function insertElements($id,$status,$assignee,$moddaterange,$sort_col,$sort_ord,$extra_fields,$opendaterange,$closedaterange) {
191 $res=db_query("DELETE FROM artifact_query_fields WHERE artifact_query_id='$id'");
193 $this->setError('Deleting Old Elements: '.db_error());
197 $res=db_query("INSERT INTO artifact_query_fields
198 (artifact_query_id,query_field_type,query_field_id,query_field_values)
199 VALUES ('$id','".ARTIFACT_QUERY_STATE."','0','".intval($status)."')");
201 $this->setError('Setting Status: '.db_error());
205 if (is_array($assignee)) {
206 for($e=0; $e<count($assignee); $e++) {
207 $assignee[$e]=intval($assignee[$e]);
209 $assignee=implode(',',$assignee);
211 $assignee = intval($assignee);
214 if (preg_match("/[^[:alnum:]_]/", $string)) {
215 $this->setError('ArtifactQuery: not valid sort_col');
219 if (preg_match("/[^[:alnum:]_]/", $string)) {
220 $this->setError('ArtifactQuery: not valid sort_ord');
224 //CSV LIST OF ASSIGNEES
225 $res=db_query("INSERT INTO artifact_query_fields
226 (artifact_query_id,query_field_type,query_field_id,query_field_values)
227 VALUES ('$id','".ARTIFACT_QUERY_ASSIGNEE."','0','".$assignee."')");
229 $this->setError('Setting Assignee: '.db_error());
233 //MOD DATE RANGE YYYY-MM-DD YYYY-MM-DD format
234 if ($moddaterange && !$this->validateDateRange($moddaterange)) {
235 $this->setError('Invalid Mod Date Range');
238 $res=db_query("INSERT INTO artifact_query_fields
239 (artifact_query_id,query_field_type,query_field_id,query_field_values)
240 VALUES ('$id','".ARTIFACT_QUERY_MODDATE."','0','".$moddaterange."')");
242 $this->setError('Setting Last Modified Date Range: '.db_error());
246 //OPEN DATE RANGE YYYY-MM-DD YYYY-MM-DD format
247 if ($opendaterange && !$this->validateDateRange($opendaterange)) {
248 $this->setError('Invalid Open Date Range');
251 $res=db_query("INSERT INTO artifact_query_fields
252 (artifact_query_id,query_field_type,query_field_id,query_field_values)
253 VALUES ('$id','".ARTIFACT_QUERY_OPENDATE."','0','".$opendaterange."')");
255 $this->setError('Setting Open Date Range: '.db_error());
259 //CLOSE DATE RANGE YYYY-MM-DD YYYY-MM-DD format
260 if ($closedaterange && !$this->validateDateRange($closedaterange)) {
261 $this->setError('Invalid Close Date Range');
264 $res=db_query("INSERT INTO artifact_query_fields
265 (artifact_query_id,query_field_type,query_field_id,query_field_values)
266 VALUES ('$id','".ARTIFACT_QUERY_CLOSEDATE."','0','".$closedaterange."')");
268 $this->setError('Setting Close Date Range: '.db_error());
273 $res=db_query("INSERT INTO artifact_query_fields
274 (artifact_query_id,query_field_type,query_field_id,query_field_values)
275 VALUES ('$id','".ARTIFACT_QUERY_SORTCOL."','0','".$sort_col."')");
277 $this->setError('Setting Sort Col: '.db_error());
280 $res=db_query("INSERT INTO artifact_query_fields
281 (artifact_query_id,query_field_type,query_field_id,query_field_values)
282 VALUES ('$id','".ARTIFACT_QUERY_SORTORD."','0','".$sort_ord."')");
284 $this->setError('Setting Sort Order: '.db_error());
288 if (!$extra_fields) {
289 $extra_fields=array();
292 $keys=array_keys($extra_fields);
293 $vals=array_values($extra_fields);
294 for ($i=0; $i<count($keys); $i++) {
299 // Checkboxes and multi-select may be arrays so store it comma-separated
301 if (is_array($vals[$i])) {
302 for($e=0; $e<count($vals[$i]); $e++) {
303 $vals[$i][$e]=intval($vals[$i][$e]);
305 $vals[$i]=implode(',',$vals[$i]);
307 $vals[$i] = intval($vals[$i]);
309 $res=db_query("INSERT INTO artifact_query_fields
310 (artifact_query_id,query_field_type,query_field_id,query_field_values)
311 VALUES ('$id','".ARTIFACT_QUERY_EXTRAFIELD."','".((int)$keys[$i]) ."','". $vals[$i] ."')");
313 $this->setError('Setting values: '.db_error());
321 * getID - get this ArtifactQuery ID.
323 * @return int The id #.
326 return $this->data_array['artifact_query_id'];
330 * getName - get the name.
332 * @return string The name.
335 return $this->data_array['query_name'];
339 * getSortCol - the column that you're sorting on
341 * @return string The column name.
343 function getSortCol() {
344 return $this->element_array[ARTIFACT_QUERY_SORTCOL][0];
348 * getSortOrd - ASC or DESC
350 * @return string ASC or DESC
352 function getSortOrd() {
353 return $this->element_array[ARTIFACT_QUERY_SORTORD][0];
357 * getModDateRange - get the range of dates to include in a query
359 * @return string mod date range.
361 function getModDateRange() {
362 if ($this->element_array[ARTIFACT_QUERY_MODDATE][0]) {
363 return $this->element_array[ARTIFACT_QUERY_MODDATE][0];
370 * getOpenDateRange - get the range of dates to include in a query
372 * @return string Open date range.
374 function getOpenDateRange() {
375 if ($this->element_array[ARTIFACT_QUERY_OPENDATE][0]) {
376 return $this->element_array[ARTIFACT_QUERY_OPENDATE][0];
383 * getCloseDateRange - get the range of dates to include in a query
385 * @return string Close date range.
387 function getCloseDateRange() {
388 if ($this->element_array[ARTIFACT_QUERY_CLOSEDATE][0]) {
389 return $this->element_array[ARTIFACT_QUERY_CLOSEDATE][0];
398 * @return string Assignee ID
400 function getAssignee() {
401 return $this->element_array[ARTIFACT_QUERY_ASSIGNEE][0];
407 * @return string Status ID
409 function getStatus() {
410 return $this->element_array[ARTIFACT_QUERY_STATE][0];
414 * getExtraFields - complex multi-dimensional array of extra field IDs/Vals
416 * @return array Complex Array
418 function getExtraFields() {
419 return $this->element_array[ARTIFACT_QUERY_EXTRAFIELD];
423 * validateDateRange - validate a date range in this format '1999-05-01 1999-06-01'.
425 * @return boolean true/false.
427 function validateDateRange($daterange) {
428 return preg_match('/[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{4}-[0-9]{2}-[0-9]{2}/',$daterange);
432 * update - update a row in the table used to query names
435 * @param int Id of the saved query
436 * @param string The name of the saved query
437 * @return boolean success.
439 function update($name,$status,$assignee,$moddaterange,$sort_col,$sort_ord,$extra_fields,$opendaterange='',$closedaterange='') {
441 $this->setMissingParamsError();
444 if (!session_loggedin()) {
445 $this->setError('Must Be Logged In');
448 if (!$this->Exist(htmlspecialchars($name))) {
449 $this->setError(_('Query does not exist'));
452 $sql="UPDATE artifact_query
454 query_name='".htmlspecialchars($name)."'
455 WHERE artifact_query_id='".$this->getID()."'
456 AND user_id='".user_getid()."'";
458 $result=db_query($sql);
459 if ($result && db_affected_rows($result) > 0) {
460 if (!$this->insertElements($this->getID(),$status,$assignee,$moddaterange,$sort_col,$sort_ord,$extra_fields,$opendaterange,$closedaterange)) {
465 $this->fetchData($this->getID());
469 $this->setError('Error Updating: '.db_error());
476 * makeDefault - set this as the default query
478 * @return boolean success.
480 function makeDefault() {
481 if (!session_loggedin()) {
482 $this->setError('Must Be Logged In');
485 $usr =& session_get_user();
486 return $usr->setPreference('art_query'.$this->ArtifactType->getID(),$this->getID());
490 $res=db_query("DELETE FROM artifact_query WHERE artifact_query_id='".$this->getID()."'
491 AND user_id='".user_getid()."'");
492 $res=db_query("DELETE FROM user_preferences WHERE preference_value='".$this->getID()."'
493 AND preference_name 'art_query".$this->ArtifactType->getID()."'");
494 unset($this->data_array);
495 unset($this->element_array);
499 * Exist - check if already exist a query with the same name , user_id and artifact_id
501 * @return boolean exist
503 function Exist($name) {
504 $user_id = user_getid();
505 $art_id = $this->ArtifactType->getID();
506 $sql = "SELECT * FROM artifact_query WHERE group_artifact_id = '$art_id' AND query_name = '$name' AND user_id = '$user_id'";
507 $res = db_query($sql);
508 if (db_numrows($res)>0) {
518 // c-file-style: "bsd"