Ejemplo n.º 1
0
 /**
  * Main method for query processing and fetching rows
  * It can return string with SQL query, array or raw rows or processed HTML chunks
  *
  * @return array|bool|string
  */
 public function run()
 {
     $this->makeQuery();
     $this->addTVFilters();
     $this->addTVs();
     $this->addJoins();
     $this->addGrouping();
     $this->addSelects();
     $this->addWhere();
     $this->addSort();
     $this->prepareQuery();
     $output = '';
     if (strtolower($this->config['return']) == 'sql') {
         $this->addTime('Returning raw sql query');
         $output = $this->query->toSql();
     } else {
         $this->modx->exec('SET SQL_BIG_SELECTS = 1');
         $this->addTime('SQL prepared <small>"' . $this->query->toSql() . '"</small>');
         $tstart = microtime(true);
         if ($this->query->stmt->execute()) {
             $this->modx->queryTime += microtime(true) - $tstart;
             $this->modx->executedQueries++;
             $this->addTime('SQL executed', microtime(true) - $tstart);
             $this->setTotal();
             $rows = $this->query->stmt->fetchAll(PDO::FETCH_ASSOC);
             $this->addTime('Rows fetched');
             $rows = $this->checkPermissions($rows);
             $this->count = count($rows);
             if (strtolower($this->config['return']) == 'ids') {
                 $ids = array();
                 foreach ($rows as $row) {
                     $ids[] = $row[$this->pk];
                 }
                 $output = implode(',', $ids);
             } elseif (strtolower($this->config['return']) == 'data') {
                 $rows = $this->prepareRows($rows);
                 $this->addTime('Returning raw data');
                 $output =& $rows;
             } elseif (strtolower($this->config['return']) == 'json') {
                 $rows = $this->prepareRows($rows);
                 $this->addTime('Returning raw data as JSON string');
                 $output = $this->modx->toJSON($rows);
             } elseif (strtolower($this->config['return']) == 'serialize') {
                 $rows = $this->prepareRows($rows);
                 $this->addTime('Returning raw data as serialized string');
                 $output = serialize($rows);
             } else {
                 $rows = $this->prepareRows($rows);
                 $time = microtime(true);
                 foreach ($rows as $row) {
                     if (!empty($this->config['additionalPlaceholders'])) {
                         $row = array_merge($this->config['additionalPlaceholders'], $row);
                     }
                     $row['idx'] = $this->idx++;
                     // Add placeholder [[+link]] if specified
                     if (!empty($this->config['useWeblinkUrl'])) {
                         if (!isset($row['context_key'])) {
                             $row['context_key'] = '';
                         }
                         if (isset($row['class_key']) && $row['class_key'] == 'modWebLink') {
                             $row['link'] = isset($row['content']) && is_numeric(trim($row['content'], '[]~ ')) ? $this->modx->makeUrl(intval(trim($row['content'], '[]~ ')), '', '', $this->config['scheme']) : (isset($row['content']) ? $row['content'] : '');
                         } else {
                             $row['link'] = $this->modx->makeUrl($row['id'], $row['context_key'], '', $this->config['scheme']);
                         }
                     } else {
                         $row['link'] = '';
                     }
                     $tpl = $this->defineChunk($row);
                     if (empty($tpl)) {
                         $output[] = '<pre>' . $this->getChunk('', $row) . '</pre>';
                     } else {
                         $output[] = $this->getChunk($tpl, $row, $this->config['fastMode']);
                     }
                 }
                 $this->addTime('Returning processed chunks', microtime(true) - $time);
                 if (!empty($this->config['toSeparatePlaceholders'])) {
                     $this->modx->setPlaceholders($output, $this->config['toSeparatePlaceholders']);
                     $output = '';
                 } elseif (!empty($output)) {
                     $output = implode($this->config['outputSeparator'], $output);
                 }
             }
         } else {
             $this->modx->log(modX::LOG_LEVEL_INFO, '[pdoTools] ' . $this->query->toSql());
             $errors = $this->query->stmt->errorInfo();
             $this->modx->log(modX::LOG_LEVEL_ERROR, '[pdoTools] Error ' . $errors[0] . ': ' . $errors[2]);
             $this->addTime('Could not process query, error #' . $errors[1] . ': ' . $errors[2]);
         }
     }
     return $output;
 }
Ejemplo n.º 2
0
 public function prepareQueryAfterCount(xPDOQuery $c)
 {
     $c->prepare();
     $this->modx->log(modX::LOG_LEVEL_ERROR, $c->toSql());
     return $c;
 }