/**
  * Function prepareQuery
  *
  * @author David S. Callizaya S. <*****@*****.**>
  * @access public
  * @param string $limitPage
  * @return string
  */
 public function prepareQuery($limitPage = false)
 {
     // process the QuickSearch string and add the fields and expression needed to run the search
     if ($this->searchBy !== '') {
         $aSB = explode('|', $this->searchBy);
         //fields are separated by pipes
         //subfilter
         $subFilter = '';
         foreach ($aSB as $sBy) {
             $subFilter .= $subFilter !== '' ? ' OR ' : '';
             //TODO: Get DATABASE type from Criteria, I think sql delimeter is needed too
             $subFilter .= $sBy . ' LIKE "%' . G::sqlEscape($this->fastSearch) . '%"';
         }
         if ($subFilter !== '') {
             //Get the first defined table in Criteria.
             $aCurrentTables = $this->criteria->getTables();
             if (isset($aCurrentTables[0])) {
                 $this->criteria->add($aCurrentTables[0] . ".*", '(' . $subFilter . ')', Criteria::CUSTOM);
             }
         }
     }
     //Merge sort array defined by USER with the array defined by SQL
     parse_str($this->order, $orderFields);
     parse_str($this->orderBy, $orderFields2);
     //User sort is more important (first in merge).
     $orderFields3 = array_merge($orderFields2, $orderFields);
     //User sort is overwrites XMLs definition.
     $orderFields = array_merge($orderFields3, $orderFields2);
     //Order (BY SQL DEFINITION AND USER'S DEFINITION)
     $this->aOrder = array();
     $order = '';
     foreach ($orderFields as $field => $fieldOrder) {
         $field = G::getUIDName($field, '');
         $fieldOrder = strtoupper($fieldOrder);
         if ($fieldOrder === 'A') {
             $fieldOrder = 'ASC';
         }
         if ($fieldOrder === 'D') {
             $fieldOrder = 'DESC';
         }
         switch ($fieldOrder) {
             case 'ASC':
             case 'DESC':
                 if ($order !== '') {
                     $order .= ', ';
                 }
                 $order .= $field . ' ' . $fieldOrder;
                 $this->aOrder[$field] = $fieldOrder;
         }
     }
     //master detail :O
     if (count($this->masterdetail) > 0) {
         $this->criteria->clearOrderByColumns();
         foreach ($this->masterdetail as $idMasterDetail => $fieldMasterDetail) {
             $this->criteria->addAscendingOrderByColumn($fieldMasterDetail);
         }
     }
     if (!empty($this->aOrder)) {
         if (count($this->masterdetail) <= 0) {
             $this->criteria->clearOrderByColumns();
         }
         foreach ($this->aOrder as $field => $ascending) {
             if ($ascending == 'ASC') {
                 $this->criteria->addAscendingOrderByColumn($field);
             } else {
                 $this->criteria->addDescendingOrderByColumn($field);
             }
         }
     }
     /**
      * Add limits
      */
     $this->criteria->setLimit(0);
     $this->criteria->setOffset(0);
     if ($this->criteria->getDbName() == 'dbarray') {
         $this->totRows = ArrayBasePeer::doCount($this->criteria);
     } else {
         $this->totRows = GulliverBasePeer::doCount($this->criteria);
     }
     $this->totPages = ceil($this->totRows / $this->rowsPerPage);
     if ($limitPage) {
         $this->criteria->setLimit($this->rowsPerPage);
         $this->criteria->setOffset(($this->currentPage - 1) * $this->rowsPerPage);
     }
     return;
 }