Example #1
0
 public function getData()
 {
     $this->queryLimit = $this->pagination;
     $this->queryOffset = $this->currentPage * $this->pagination - $this->pagination;
     $bFilteredOrderBy = false;
     foreach ($this->queryColumns as $column => $options) {
         if ($this->queryOrderBy == $column || $this->queryOrderBy == 'random' || $this->queryOrderBy == 'default' || $this->queryOrderBy == 'related') {
             $bFilteredOrderBy = true;
         }
         if (isset($options['encrypted']) && $options['encrypted'] && $this->encryptionKey != '') {
             $columns[] = "AES_DECRYPT({$options['source']}, :encryptionkey) AS {$column}";
         } else {
             $columns[] = "{$options['source']} AS {$column}";
         }
     }
     if ($bFilteredOrderBy == false) {
         throw new \Exception('Column not found: ' . $this->queryOrderBy);
     }
     $columns[0] = 'SQL_CALC_FOUND_ROWS ' . $columns[0];
     $sqlColumns = implode(",\n", $columns);
     $sqlFrom = $this->queryFrom;
     $sqlGroupBy = $this->queryGroupBy;
     $selectString = "SELECT {$sqlColumns}";
     $fromString = " FROM {$sqlFrom}";
     $whereString = "";
     $havingString = "";
     if ($this->queryAdditionalWhere != '') {
         $whereString = ' WHERE ' . $this->queryAdditionalWhere;
     }
     $groupString = " GROUP BY {$sqlGroupBy}";
     $orderString = ' ';
     if ($this->queryHaving != '') {
         $havingString = ' HAVING ' . $this->queryHaving;
     }
     $limitString = ' ';
     if ($this->queryOrderBy == 'random') {
         $orderString .= 'ORDER BY RAND()';
         $limitString .= 'LIMIT ' . $this->pagination;
     } elseif ($this->queryOrderBy == 'default') {
         $orderString .= 'ORDER BY hierarchy DESC, discountprice DESC, new DESC';
         $limitString .= 'LIMIT ' . $this->queryOffset . ',' . $this->pagination;
     } elseif ($this->queryOrderBy == 'related') {
         $orderString .= 'ORDER BY hierarchy ' . $this->queryOrderDir;
         $limitString .= 'LIMIT ' . $this->queryOffset . ',' . $this->pagination;
     } else {
         $orderString .= 'ORDER BY ' . $this->queryOrderBy . ' ' . $this->queryOrderDir;
         if ($this->pagination > 0) {
             $limitString .= 'LIMIT ' . $this->queryOffset . ',' . $this->pagination;
         }
     }
     $sql = $selectString . $fromString . $whereString . $groupString . $havingString . $orderString . $limitString;
     $stmt = Db::getInstance()->prepare($sql);
     if (preg_match('/:languageid/', $sql)) {
         $stmt->bindValue('languageid', $this->languageId);
     }
     if (preg_match('/:encryptionkey/', $sql)) {
         $stmt->bindValue('encryptionkey', $this->encryptionKey);
     }
     foreach ($this->sqlParams as $key => $val) {
         if (is_array($val)) {
             if (isset($val[0]) && is_numeric($val[0])) {
                 $stmt->bindValue($key, implode(',', $val));
             } elseif (isset($val[0]) && is_string($val[0])) {
                 $stmt->bindValue($key, implode(',', $val));
             } else {
                 $stmt->bindValue($key, 0);
             }
         } else {
             if (is_int($val)) {
                 $stmt->bindValue($key, $val);
             } elseif (is_null($val)) {
                 $stmt->bindValue($key, NULL);
             } elseif (is_float($val)) {
                 $stmt->bindValue($key, $val);
             } elseif (is_string($val)) {
                 $stmt->bindValue($key, $val);
             } else {
                 $stmt->bindValue($key, $val);
             }
         }
     }
     if (preg_match('/:viewid/', $sql)) {
         $stmt->bindValue('viewid', $this->viewId);
     }
     if (preg_match('/:clientgroupid/', $sql)) {
         $stmt->bindValue('clientgroupid', Session::getActiveClientGroupid());
     }
     if (preg_match('/:today/', $sql)) {
         $stmt->bindValue('today', date("Y-m-d"));
     }
     if (preg_match('/:currencyto/', $sql)) {
         $stmt->bindValue('currencyto', Session::getActiveCurrencyId());
     }
     try {
         $stmt->execute();
         $rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
     } catch (Exception $e) {
         throw new FrontendException('ERR_DATASET_GET_DATA', 12, $e->getMessage());
     }
     $this->getTotalRecords();
     $this->processRows($rows);
     if ($this->pagination > 0) {
         $pages = ceil($this->DataSet['total'] / $this->pagination);
         if ($pages == 0) {
             $this->DataSet['totalPages'] = range(1, 1, 1);
             $this->DataSet['activePage'] = 1;
             $this->DataSet['lastPage'] = 1;
             $this->DataSet['previousPage'] = 1;
             $this->DataSet['nextPage'] = 1;
         } else {
             $this->DataSet['totalPages'] = range(1, $pages, 1);
             $this->DataSet['activePage'] = $this->currentPage;
             $this->DataSet['lastPage'] = $pages;
             $this->DataSet['previousPage'] = $this->currentPage - 1;
             $this->DataSet['nextPage'] = $this->currentPage + 1;
         }
     } else {
         $this->DataSet['totalPages'] = range(1, 1, 1);
         $this->DataSet['activePage'] = 1;
         $this->DataSet['lastPage'] = 1;
         $this->DataSet['previousPage'] = 1;
         $this->DataSet['nextPage'] = 1;
     }
 }