File "db.class.php"
Full path: /home/kosmetik/public_html/wp-content/plugins/unlimited-elements-for-elementor/inc_php/framework/db.class.php
File
size: 12.52 B
MIME-type: text/x-php
Charset: utf-8
Download Open Edit Advanced Editor Back
<?php
defined('UNLIMITED_ELEMENTS_INC') or die('Restricted access');
class UniteCreatorDB
{
const ISNULL = "dbisnull";
private $pdb;
private $lastRowID;
public static $arrTableTitles;
public function __construct()
{
$this->pdb = new UniteProviderDBUC();
}
private function throwError($message, $code = -1)
{
UniteFunctionsUC::throwError($message, $code);
}
public function getPDB()
{
return $this->pdb->getDBObject();
}
private function checkForErrors($prefix = "")
{
$message = $this->pdb->getErrorMsg();
if (!$message) {
return false;
}
if (!empty($prefix)) {
$message = $prefix . " " . $message;
}
$errorNum = $this->pdb->getErrorNum();
$this->throwError($message, $errorNum);
}
public function isTableExists($table)
{
try {
$this->fetchSql("select * from {$table} limit 1", true);
} catch (Exception $e) {
return false;
}
return true;
}
public function insert($tableName, $arrItems)
{
$strFields = "";
$strValues = "";
foreach ($arrItems as $field => $value) {
$value = "'" . $this->escape($value) . "'";
if ($field == "id") {
continue;
}
if ($strFields != "") {
$strFields .= ",";
}
if ($strValues != "") {
$strValues .= ",";
}
$strFields .= $field;
$strValues .= $value;
}
$insertQuery = "insert into {$tableName}({$strFields}) values({$strValues})";
$this->runSql($insertQuery, "insert");
$this->lastRowID = $this->pdb->insertid();
return $this->lastRowID;
}
public function getLastInsertID()
{
$this->lastRowID = $this->pdb->insertid();
return $this->lastRowID;
}
public function delete($table, $where)
{
UniteFunctionsUC::validateNotEmpty($table, "table name");
UniteFunctionsUC::validateNotEmpty($where, "where");
if (is_array($where)) {
$where = $this->getWhereString($where);
}
$query = "delete from {$table} where {$where}";
$success = $this->runSql($query, "delete error");
return $success;
}
public function deleteMultipleByID($table, $arrItems)
{
foreach ($arrItems as $key => $itemID) {
$arrItems[$key] = (int) $itemID;
}
$strItemsIDs = implode(",", $arrItems);
$this->delete($table, "id in({$strItemsIDs})");
}
public function getWhereCatIDWithAll($catID)
{
$arrWhere = array();
if (is_numeric($catID)) {
$catID = (int) $catID;
}
if ($catID === null) {
$catID = "all";
}
if ($catID === "all") {
$arrWhere = array();
} else {
if (is_numeric($catID)) {
$catID = (int) $catID;
$arrWhere[] = "catid={$catID}";
} else {
if (is_array($catID) == false) {
UniteFunctionsUC::throwError("catIDs could be array or number");
}
$strCats = implode(",", $catID);
$strCats = $this->escape($strCats);
$arrWhere[] = "catid in({$strCats})";
}
}
return $arrWhere;
}
private function getWhereString($where)
{
$where_format = null;
foreach ($where as $key => $value) {
if ($value == self::ISNULL) {
$wheres[] = "({$key} = '' or {$key} is null)";
continue;
}
if ($key == self::ISNULL || is_numeric($key)) {
$wheres[] = $value;
continue;
}
$sign = "=";
$isEscape = true;
if (is_array($value)) {
$sign = $value[0];
$value = $value[1];
}
if (is_numeric($value) == false) {
$value = $this->escape($value);
$value = "'{$value}'";
}
$wheres[] = "{$key} {$sign} {$value}";
}
$strWhere = implode(' AND ', $wheres);
return $strWhere;
}
public function update($tableName, $arrData, $where)
{
UniteFunctionsUC::validateNotEmpty($tableName, "table cannot be empty");
UniteFunctionsUC::validateNotEmpty($where, "where cannot be empty");
UniteFunctionsUC::validateNotEmpty($arrData, "data cannot be empty");
if (is_array($where)) {
$where = $this->getWhereString($where);
}
$strFields = "";
foreach ($arrData as $field => $value) {
$value = "'" . $this->escape($value) . "'";
if ($strFields != "") {
$strFields .= ",";
}
$strFields .= "{$field}={$value}";
}
$updateQuery = "update {$tableName} set {$strFields} where {$where}";
$numRows = $this->runSql($updateQuery, "update error");
return $numRows;
}
public function runSql($query)
{
$response = $this->pdb->query($query);
$this->checkForErrors("Regular query error");
return $response;
}
public function fetchSql($query, $supressErrors = false)
{
$rows = $this->pdb->fetchSql($query, $supressErrors);
$this->checkForErrors("fetch");
$rows = UniteFunctionsUC::convertStdClassToArray($rows);
return $rows;
}
public function get_row($query = null)
{
$rows = $this->pdb->fetchSql($query);
$this->checkForErrors("get_row");
if (count($rows) == 1) {
$result = $rows[0];
} else {
$result = $rows;
}
return $result;
}
private function getQueryPart_where($where = "")
{
if ($where) {
if (is_array($where)) {
$where = $this->getWhereString($where);
}
$where = " where {$where}";
}
return $where;
}
private function createFetchQuery($tableName, $fields = null, $where = "", $orderField = "", $groupByField = "", $sqlAddon = "")
{
if (empty($fields)) {
$fields = "*";
} else {
if (is_array($fields)) {
$fields = implode(",", $fields);
}
}
$query = "select {$fields} from {$tableName}";
$where = $this->getQueryPart_where($where);
if (!empty($where)) {
$query .= $where;
}
if ($orderField) {
$orderField = $this->escape($orderField);
$query .= " order by {$orderField}";
}
if ($groupByField) {
$groupByField = $this->escape($groupByField);
$query .= " group by {$groupByField}";
}
if ($sqlAddon) {
$query .= " " . $sqlAddon;
}
return $query;
}
public function fetch($tableName, $where = "", $orderField = "", $groupByField = "", $sqlAddon = "")
{
$query = $this->createFetchQuery($tableName, null, $where, $orderField, $groupByField, $sqlAddon);
$rows = $this->fetchSql($query);
return $rows;
}
public function getTotalRows($tableName, $where = "")
{
$where = $this->getQueryPart_where($where);
$query = "select count(*) as numrows from {$tableName}" . $where;
$response = $this->fetchSql($query);
$totalRows = $response[0]["numrows"];
return $totalRows;
}
public function fetchByIDs($table, $arrIDs)
{
if (is_string($arrIDs)) {
$strIDs = $arrIDs;
} else {
$strIDs = implode(",", $arrIDs);
}
$sql = "select * from {$table} where id in({$strIDs})";
$arrRecords = $this->fetchSql($sql);
return $arrRecords;
}
public function updateRecordsOrdering($table, $arrIDs)
{
$arrRecords = $this->fetchByIDs($table, $arrIDs);
$arrRecords = UniteFunctionsUC::arrayToAssoc($arrRecords, "id");
$order = 0;
foreach ($arrIDs as $recordID) {
$order++;
$arrRecord = UniteFunctionsUC::getVal($arrRecords, $recordID);
if (!empty($arrRecord) && $arrRecord["ordering"] == $order) {
continue;
}
$arrUpdate = array();
$arrUpdate["ordering"] = $order;
$this->update($table, $arrUpdate, array("id" => $recordID));
}
}
public function fetchPage($tableName, $pagingOptions, $where = "", $orderField = "", $groupByField = "", $sqlAddon = "")
{
$page = UniteFunctionsUC::getVal($pagingOptions, "page");
$rowsInPage = UniteFunctionsUC::getVal($pagingOptions, "inpage");
UniteFunctionsUC::validateNumeric($page);
UniteFunctionsUC::validateNumeric($rowsInPage);
UniteFunctionsUC::validateNotEmpty($rowsInPage);
if ($page < 1) {
$page = 1;
}
$totalRows = $this->getTotalRows($tableName, $where);
$numPages = $pages = ceil($totalRows / $rowsInPage);
$offset = ($page - 1) * $rowsInPage;
$query = $this->createFetchQuery($tableName, null, $where, $orderField, $groupByField, $sqlAddon);
$query .= " limit {$rowsInPage} offset {$offset}";
$rows = $this->fetchSql($query);
$response = array();
$response["total"] = $totalRows;
$response["page"] = $page;
$response["num_pages"] = $numPages;
$response["inpage"] = $rowsInPage;
$response["rows"] = $rows;
return $response;
}
public function fetchFields($tableName, $fields, $where = "", $orderField = "", $groupByField = "", $sqlAddon = "")
{
$query = $this->createFetchQuery($tableName, $fields, $where, $orderField, $groupByField, $sqlAddon);
$rows = $this->fetchSql($query);
return $rows;
}
public function fetchSingle($tableName, $where = "", $orderField = "", $groupByField = "", $sqlAddon = "")
{
$errorEmpty = "";
if (is_array($tableName)) {
$arguments = $tableName;
$tableName = UniteFunctionsUC::getVal($arguments, "tableName");
$where = UniteFunctionsUC::getVal($arguments, "where");
$orderField = UniteFunctionsUC::getVal($arguments, "orderField");
$groupByField = UniteFunctionsUC::getVal($arguments, "groupByField");
$sqlAddon = UniteFunctionsUC::getVal($arguments, "sqlAddon");
$errorEmpty = UniteFunctionsUC::getVal($arguments, "errorEmpty");
}
if (empty($errorEmpty)) {
$tableTitle = UniteFunctionsUC::getVal(self::$arrTableTitles, $tableName, __("Record", "unlimited-elements-for-elementor"));
$errorEmpty = $tableTitle . " " . __("not found", "unlimited-elements-for-elementor");
}
$response = $this->fetch($tableName, $where, $orderField, $groupByField, $sqlAddon);
if (empty($response)) {
$this->throwError($errorEmpty);
}
$record = $response[0];
return $record;
}
public function getMaxOrder($table, $field = "ordering")
{
$query = "select MAX({$field}) as maxorder from {$table}";
$rows = $this->fetchSql($query);
$maxOrder = 0;
if (count($rows) > 0) {
$maxOrder = $rows[0]["maxorder"];
}
if (!is_numeric($maxOrder)) {
$maxOrder = 0;
}
return $maxOrder;
}
public function createObjectInDB($table, $arrInsert)
{
$maxOrder = $this->getMaxOrder($table);
$arrInsert["ordering"] = $maxOrder + 1;
$id = $this->insert($table, $arrInsert);
return $id;
}
public function escape($string)
{
$newString = $this->pdb->escape($string);
return $newString;
}
public function getSqlAddonType($addonType, $dbfield = "addontype")
{
if (is_array($addonType)) {
$arrSql = array();
foreach ($addonType as $singleType) {
$arrSql[] = $this->getSqlAddonType($singleType, $dbfield);
}
$sql = "(" . implode(" or ", $arrSql) . ")";
return $sql;
}
if ($addonType == GlobalsUC::ADDON_TYPE_REGULAR_ADDON || $addonType == GlobalsUC::ADDON_TYPE_LAYOUT_GENERAL) {
$addonType = null;
}
$addonType = $this->escape($addonType);
if (empty($addonType)) {
$where = "({$dbfield}='' or {$dbfield} is null)";
} else {
$where = "{$dbfield}='{$addonType}'";
}
return $where;
}
}