public function setCurrentPage($currentPage) { if (Tools::isInteger($currentPage)) { if ($currentPage < 0) { $this->currentPage = 1; } else { $this->currentPage = $currentPage; } } else { $this->currentPage = 1; } }
public function __construct($tpl) { if (!empty($tpl)) { if (is_string($tpl)) { $this->_tpl = $tpl; } elseif (Tools::isInstanceOf($tpl, new Route())) { $this->_route = $tpl; $this->_tpl = $this->_route->controller . "/" . $this->_route->controller . "_" . $this->_route->action; } else { throw new \Exception("Invalid view template"); } } else { throw new \Exception("Empty view template"); } }
/** * Validate if field value is integer. * * @param string $fieldName Name of the field. * @return bool */ public static function validateInteger($fieldName) { $fieldValue = trim(Request::getFieldValue($fieldName)); if (!self::validateNotEmpty($fieldName)) { Errors::saveErrorFor($fieldName, \__ERRORS::FIELD_CANT_BE_EMPTY); return false; } elseif (!Tools::isInteger($fieldValue)) { Errors::saveErrorFor($fieldName, \__ERRORS::INVALID_INTEGER); return false; } return true; }
/** * Checks if user is logged in. * * @global User $_USER Current user object. * * @return boolean */ public static function checkLoggedIn() { global $_USER; if (Tools::isInstanceOf($_USER, new User())) { return true; } return false; }
/** * Returns value of the filter field. * * @param string $filterName Name of the filter field. * @param mixed $defaultValue Default value. * * @return mixed */ public static function _filter($filterName, $defaultValue) { return Tools::getFilterValue($filterName, $defaultValue); }
/** * Generates SQL formatted condition string. * * @param mixed $queryCondition List of DBQueryCondition objects or object * itself. * @param string $operator Initial logical OR or AND operator. * * @return string SQL query condition string. */ public static function getSQLCondition($queryCondition, $operator = "") { $operator = strtoupper(trim($operator)); if ($operator === "OR" || $operator === "AND") { if (is_array($queryCondition)) { if ($operator === "AND") { $cond = " (1"; } else { $cond = " (0"; } foreach ($queryCondition as $operation => $conditions) { $cond .= " " . $operator . self::getSQLCondition($conditions, $operation); } $cond .= ")"; return $cond; } } else { if (is_array($queryCondition)) { foreach ($queryCondition as $operation => $conditions) { return trim(str_replace(["(1 AND ", "(0 OR "], "(", self::getSQLCondition($conditions, $operation))); } } elseif (Tools::isInstanceOf($queryCondition, "\\Asymptix\\db\\DBQueryCondition")) { return " " . $queryCondition->sqlCondition; } return ""; } }
/** * Returns type of the parameter by value. * * @param mixed $fieldValue * * @return string Types of the parameter ("idsb"). * @throws DBFieldTypeException If can't detect field type by value. */ public static function getType($fieldValue) { if (Tools::isInteger($fieldValue)) { return "i"; } elseif (Tools::isDouble($fieldValue)) { return "d"; } elseif (Tools::isBoolean($fieldValue)) { return "b"; } elseif (Tools::isString($fieldValue)) { return "s"; } else { throw new DBFieldTypeException("Invalid field value type"); } }
/** * Selects DB record(s) for current DBObject table according to params. * * @param boolean $debug Debug mode flag. * * @return mixed DBObject, array of DBObject or null. * @throws DBCoreException If some DB or query syntax errors occurred. */ public function go($debug = false) { switch ($this->dbQuery->getType()) { case DBQueryType::SELECT: $this->dbQuery->query = "SELECT * FROM " . static::TABLE_NAME; break; case DBQueryType::UPDATE: $this->dbQuery->query = "UPDATE " . static::TABLE_NAME . " SET "; $this->dbQuery->sqlPushValues($this->dbQuery->fields); break; } /** * Conditions */ if ($this->isNewRecord()) { if (!empty($this->dbQuery->conditions)) { $this->dbQuery->query .= " WHERE "; $this->dbQuery->sqlPushValues($this->dbQuery->conditions, " AND "); } } else { $this->dbQuery->query .= " WHERE "; $this->dbQuery->sqlPushValues(array(static::ID_FIELD_NAME => $this->id)); } /** * Order */ if ($this->isNewRecord()) { if (!empty($this->dbQuery->order)) { $this->dbQuery->query .= " ORDER BY"; if (is_array($this->dbQuery->order)) { foreach ($this->dbQuery->order as $fieldName => $ord) { $this->dbQuery->query .= " " . $fieldName . " " . $ord . ","; } $this->dbQuery->query = substr($this->dbQuery->query, 0, strlen($this->dbQuery->query) - 1); } elseif (is_string($this->dbQuery->order)) { $this->dbQuery->query .= " " . $this->dbQuery->order; } } } /** * Limit */ if ($this->isNewRecord()) { if (!is_null($this->dbQuery->limit)) { if (Tools::isInteger($this->dbQuery->limit)) { $this->dbQuery->query .= " LIMIT " . $this->dbQuery->limit; } elseif (is_array($this->dbQuery->limit) && count($this->dbQuery->limit) == 2) { $offset = $this->dbQuery->limit[0]; $count = $this->dbQuery->limit[1]; if (Tools::isInteger($offset) && Tools::isInteger($count)) { $this->dbQuery->query .= " LIMIT " . $offset . ", " . $count; } else { throw new DBCoreException("Invalid LIMIT param in select() method."); } } else { throw new DBCoreException("Invalid LIMIT param in select() method."); } } } else { $this->dbQuery->query .= " LIMIT 1"; } if ($debug) { DBQuery::showQueryDebugInfo($this->dbQuery->query, $this->dbQuery->types, $this->dbQuery->params); } else { if ($this->dbQuery->isSelect()) { $stmt = $this->dbQuery->go(); if ($stmt !== false) { $data = null; if ($stmt->num_rows > 1) { $data = DBCore::selectDBObjectsFromStatement($stmt, $this); } elseif ($stmt->num_rows == 1) { $data = DBCore::selectDBObjectFromStatement($stmt, $this); } $stmt->close(); return $data; } } else { return $this->dbQuery->go(); } } return null; }
/** * Returns SQL types string. * Type specification chars: * i - corresponding variable has type integer * d - corresponding variable has type double * s - corresponding variable has type string * b - corresponding variable is a blob and will be sent in packets * * @param array<mixed> $fieldsList List of the table fields (syntax: array[fieldName] = fieldValue) * @param string $idFieldName Name of the primary key field. * @return string */ public static function sqlTypesString($fieldsList, $idFieldName = "") { $typesString = ""; foreach ($fieldsList as $fieldName => $fieldValue) { if ($fieldName != $idFieldName) { if (Tools::isDouble($fieldValue)) { $typesString .= "d"; } elseif (Tools::isInteger($fieldValue)) { $typesString .= "i"; } else { $typesString .= "s"; } } } return $typesString; }
/** * Saves DBObject to the database. If this is a new object - INSERT SQL * instruction executes, if existed one - UPDATE. * * @param bool $debug Debug mode flag. * * @return mixed Primary key value. * @throws DBCoreException If some database error occurred. */ public function save($debug = false) { if ($this->isNewRecord()) { $insertionId = DBCore::insertDBObject($this, false, $debug); if (Tools::isInteger($insertionId) && $insertionId > 0) { $this->setId($insertionId); return $insertionId; } throw new DBCoreException("Save database object error"); } DBCore::updateDBObject($this, $debug); return $this->id; }
/** * Executes SQL DELETE query to the database. * * @param DBObject $dbObject DBObject to delete. * * @return integer Returns the number of affected rows on success, and -1 if * the last query failed. */ public static function deleteDBObject($dbObject) { if (!empty($dbObject) && is_object($dbObject)) { $query = "DELETE FROM " . $dbObject->getTableName() . " WHERE " . $dbObject->getIdFieldName() . " = ?"; if (Tools::isInteger($dbObject->getId())) { $typesString = "i"; } else { $typesString = "s"; } self::doUpdateQuery($query, $typesString, array($dbObject->getId())); return self::connection()->affected_rows; } else { return false; } }
/** * Executes SQL query with single record and value result and return this value. * * @param mixed $query SQL query template string or DBPreparedQuery object * if single parameter. * @param string $types Types string (ex: "isdb"). * @param array $params Parameters in the same order like types string. * * @return mixed * @throws DBCoreException If no one or more than one records selected. */ public static function selectSingleValue($query, $types = "", $params = []) { if (!Tools::isInstanceOf($query, new DBPreparedQuery())) { $dbQuery = new DBPreparedQuery($query, $types, $params); } else { $dbQuery = $query; } $stmt = $dbQuery->go(); if ($stmt !== false) { $value = null; $numRows = $stmt->num_rows; if ($numRows === 1) { $stmt->bind_result($value); $stmt->fetch(); } $stmt->close(); if ($numRows !== 1) { throw new DBCoreException("No one or more than one records selected."); } return $value; } return null; }
/** * Returns type of the parameter by value. * * @param mixed $fieldValue * * @return string Types of the parameter ("idsb"). * @throws DBFieldTypeException If can't detect field type by value. */ public static function getType($fieldValue) { if (is_null($fieldValue)) { // Type is not principled for NULL return "i"; } elseif (Tools::isInteger($fieldValue)) { return "i"; } elseif (Tools::isDouble($fieldValue)) { return "d"; } elseif (Tools::isBoolean($fieldValue)) { return "b"; } elseif (Tools::isString($fieldValue)) { return "s"; } else { throw new DBFieldTypeException("Can't detect field value type for value '" . (string) $fieldValue . "'"); } }