/** * */ public function getData() { if (!is_null($this->_page) && !is_null($this->_perpage)) { $this->_select->limit('LIMIT ' . ($this->_page - 1) * $this->_perpage . ', ' . $this->_perpage); } if (!empty($this->_filters)) { $filterStorage = \Core\Grid\Model\Filter\Storage::getInstance('filters'); foreach ($this->_filters as $filter) { list($where, $value) = $filter->getFilter(); if (is_null($value)) { $value = $filterStorage->getFilter($filter->getHtmlName()); } if (!is_null($value)) { $filterStorage->setFilter($filter->getHtmlName(), $value); $this->_select->where($where, $value); } } } $sql = $this->_select->getSql(); $dbh = \Core\Db\Connector::getInstance()->getConnection(); $stm = $dbh->query($sql); $countStm = $dbh->query('SELECT FOUND_ROWS()'); $cTmp = $countStm->fetchAll(); $this->_rowsCount = $cTmp[0][0]; return new \Core\Grid\Model\Result($stm->fetchAll(\PDO::FETCH_ASSOC)); }
/** * Init database */ private static function _initDb() { $config = \Core\Config::getInstance(); $dbConnection = new PDO($config->database['driver'] . ':host=' . $config->database['host'] . ';dbname=' . $config->database['dbname'], $config->database['user'], $config->database['password'], array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); $dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); \Core\Db\Connector::getInstance()->setConnection($dbConnection); }
/** * Authenticate * @param string $login * @param string $pass * @return array */ public function authenticate($login, $pass) { $sql = 'SELECT * FROM `admin_users` a INNER JOIN admin_roles r ON r.id = a.admin_role WHERE login=?'; $dbh = \Core\Db\Connector::getInstance()->getConnection(); $stm = $dbh->prepare($sql); $stm->execute(array($login)); $data = $stm->fetch(\PDO::FETCH_ASSOC); if (empty($data)) { throw new Exception('Incorrect login'); } if ($data['pass'] != md5(md5($pass))) { throw new Exception('Incorrect password'); } return $data; }
/** * Constructor */ private function __construct() { return; $memcache = \Core\Memcache::getInstance(); $this->_acl = $memcache->get('acl_cache'); if (!$this->_acl) { $sql = 'SELECT r.title, CONCAT_WS(\':\', res.module, res.controller, res.action) AS resource, a.allow FROM `admin_roles` r INNER JOIN `admin_users_access` a ON a.admin_role_id=r.id'; $sql .= ' INNER JOIN `acl_resources` res ON res.id = a.acl_resource_id'; $stm = $aclData = \Core\Db\Connector::getInstance()->getConnection()->prepare($sql); $stm->execute(); $aclData = $stm->fetchAll(\PDO::FETCH_ASSOC); foreach ($aclData as $nfo) { $this->_acl[$nfo['title']][$nfo['resource']] = $nfo['allow']; } $memcache->set('acl_cache', $this->_acl); } }
public function validate($value) { if (strlen(trim($value)) == 0) { return true; } $dbh = \Core\Db\Connector::getInstance()->getConnection(); $sql = 'SELECT '; if ($this->getOption('pk')) { $sql .= '`' . $this->getOption('pk') . '`, '; } $sql .= '`' . $this->getOption('field') . '` FROM `' . $this->getOption('table') . '` WHERE `' . $this->getOption('field') . '`=? LIMIT 1'; $stm = $dbh->prepare($sql); $stm->execute(array($value)); $data = $stm->fetchAll(\PDO::FETCH_ASSOC); if (!empty($data) && $this->getOption('pkvalue')) { if ($data[0][$this->getOption('pk')] == $this->getOption('pkvalue')) { return true; } } return empty($data); }
/** * Constructor */ public function __construct() { $this->_connection = \Core\Db\Connector::getInstance()->getConnection(); }
/** * Generate SQL * @return string */ public function getSQL() { $sql = 'SELECT '; if (empty($this->_fields)) { $sql .= '* '; } else { foreach ($this->_fields as $field) { $sql .= $field . ', '; if ($field == 'SQL_CALC_FOUND_ROWS') { $sql = rtrim($sql, ', ') . ' '; } } $sql = rtrim($sql, ', '); } if (!empty($this->_from)) { $sql .= ' FROM '; foreach ($this->_from as $from) { if (strpos($from, 'JOIN')) { $sql .= ' ' . $from; } else { $sql .= ' ' . $from . ','; } } $sql = rtrim($sql, ','); } if (!empty($this->_where)) { $sql .= ' WHERE'; foreach ($this->_where as $key => $where) { $sql .= ' ' . $where . ' AND'; $sql = str_replace('?', \Core\Db\Connector::getInstance()->getConnection()->quote($this->_whereValues[$key]), $sql); } $sql = rtrim($sql, 'AND'); } if (!empty($this->_order)) { $sql .= ' ' . $this->_order; } if (!empty($this->_limit)) { $sql .= ' ' . $this->_limit; } return $sql; }