/**
  *	setup - sets up limits and sorts before you call getTasks().
  *
  *	@param	int	The offset - number of rows to skip.
  *	@param	string	The column to sort on.
  *	@param	string	The way to order - ASC or DESC.
  *	@param	int	The max number of rows to return.
  *	@param	string	Whether to set these prefs into the user_prefs table - use "custom".
  *	@param	int	Include this param if you want to limit to a certain assignee.
  *	@param	int	Include this param if you want to limit to a particular status.
  *	@param	array	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())
 {
     //echo "<br />offset: $offset| order: $order|max_rows: $max_rows|_assigned_to: $_assigned_to|_status: $_status";
     if (!$offset || $offset < 0) {
         $this->offset = 0;
     } else {
         $this->offset = $offset;
     }
     if (session_loggedin()) {
         $u =& session_get_user();
     }
     if (!is_array($_extra_fields)) {
         $_extra_fields = array();
     }
     if (!$set) {
         /*
         	if no set is passed in, see if a preference was set
         	if no preference or not logged in, use open set
         */
         if (session_loggedin()) {
             $default_query = $u->getPreference('art_query' . $this->ArtifactType->getID());
             $this->defaultquery = $default_query;
             if ($default_query) {
                 $aq = new ArtifactQuery($this->ArtifactType, $default_query);
                 $_extra_fields = $aq->getExtraFields();
                 $order_col = $aq->getSortCol();
                 $sort = $aq->getSortOrd();
                 $_assigned_to = $aq->getAssignee();
                 $_status = $aq->getStatus();
                 $this->moddaterange = $aq->getModDateRange();
                 $this->opendaterange = $aq->getOpenDateRange();
                 $this->closedaterange = $aq->getCloseDateRange();
             } else {
                 $custom_pref = $u->getPreference('art_cust' . $this->ArtifactType->getID());
                 if ($custom_pref) {
                     //$_assigned_to.'|'.$_status.'|'.$_order_col.'|'.$_sort_ord.'|'.$_changed.'|'.serialize($_extra_fields);
                     $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';
                 } else {
                     //default to open
                     $_assigned_to = 0;
                     $_status = 1;
                     $_changed = 0;
                 }
             }
         } else {
             //default to open
             $_assigned_to = 0;
             $_status = 1;
             $_changed = 0;
         }
     }
     //
     //  validate the column names and sort order passed in from user
     //  before saving it to prefs
     //
     if ($order_col == 'artifact_id' || $order_col == 'summary' || $order_col == 'open_date' || $order_col == 'close_date' || $order_col == 'assigned_to' || $order_col == 'submitted_by' || $order_col == 'priority') {
         $_order_col = $order_col;
         if ($sort == 'ASC' || $sort == 'DESC') {
             $_sort_ord = $sort;
         } else {
             $_sort_ord = 'ASC';
         }
     } else {
         $_order_col = 'artifact_id';
         $_sort_ord = 'ASC';
     }
     if ($set == '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;
     if ($_assigned_to != 'Array') {
         $this->assigned_to = $_assigned_to;
     }
     $this->extra_fields = $_extra_fields;
     $this->setChangedFrom($_changed);
     // if $max_rows == 0 it means we want all the rows
     if (is_null($max_rows) || $max_rows < 0) {
         $max_rows = 50;
     }
     if (isset($default_query)) {
         $this->max_rows = 0;
     } else {
         $this->max_rows = $max_rows;
     }
 }