PABX criado para pesquisas
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

299 lines
9.9 KiB

<?php
/**
* Description of ORM
*
* @author Lucas Awade
*/
class ORM {
private $query;
private $table;
private $allTables = [];
private $columns;
private $id;
/**
* $table -> table name
* $id -> identificador ou Primary Key
* @param string $table
* @param string $id
*/
function __construct($table, $id) {
$this->setAllTables($id, $table);
$this->table = $table;
$this->id = $id;
}
/**
* Exemplo:
* CALL -> find('23')
*
* @param int|string $id
* @param array $columns
* @return string
*/
function find($id, $columns = []) {
$this->columns = $columns ? implode(',', $columns) : '*';
return $this->query("SELECT $this->columns FROM {$this->table} WHERE {$this->id} = '$id';");
}
/**
* Exemplo
* $where = [
* ['column', 'operator', 'value']
* ];
*
* CALL -> findWhere([['calldate', '=', '2021-09-09']],['id', 'calldate'])
*
* @param type $where
* @param type $columns
*/
function findWhere($where, $columns = []) {
$this->columns = $columns ? implode(',', $columns) : '*';
return $this->query("SELECT $this->columns FROM {$this->table} WHERE 1=1 {$this->getWhere($where)};");
}
/**
* CALL -> findAll();
* @param array $columns
* @return string
*/
function findAll($columns = []) {
$this->columns = $columns ? implode(',', $columns) : '*';
return $this->query("SELECT $this->columns FROM {$this->table};");
}
function findAndImport() {
}
function findAndExport() {
}
/**
* $relations = [
* ['table1', 'id_table1', 'id_table2']
* ]
*
* $relations = ['table1', 'id_table1', 'id_table2']
*
* CALL -> selectJoin(
* [
* ['inner','pbx_eventos_dacs b', 'b.uid2', 'a.uniqueid'],
* ['left','pbx_classificao c', 'c.uniqueid', 'a.uniqueid'],
* ['rigth','pbx_queue d', 'd.nome', 'b.fila'],
* ['inner','pbx_protocolo_reg h', 'h.uniqueid', 'a.uniqueid']
* ],
* [['calldate', '2021-09-09'], ['dcontext', 'ROTASAINTE'], ['tipo_ligacao' , '33']],
* ['calldate', 'uniqueid', 'dst','src', 'tipo_ligacao']);
*
* @param array $relations
*/
function selectJoin($relations, $where = [], $columns = []) {
$selectColumns = count($columns) > 1 ? implode(',', $this->nickTable(array_values($columns))) : '*';
$tableMain = $this->getAllTables($this->table);
$inner = "SELECT $selectColumns \nFROM {$tableMain['name']} {$tableMain['nick']} \n";
if ($this->isMultidimensional($relations)) {
foreach ($relations as $value) {
$this->setAllTables($value[2], $value[1]);
$table = $this->getAllTables($value[1]);
$onJoin = $value[3] ? $this->nickTable($value[3]) : $tableMain['nick'] . "." . $tableMain['id'];
$inner .= $this->getRelations($value[0]) . " JOIN {$table['name']} {$table['nick']} ON {$table['nick']}.{$table['id']} = $onJoin\n";
}
} else {
$this->setAllTables($relations[2], $relations[1]);
$table = $this->getAllTables($relations[1]);
$onJoin = $relations[3] ? $this->nickTable($relations[3]) : $tableMain['nick'] . "." . $tableMain['id'];
$inner .= $this->getRelations(strtoupper($relations[0])) . " JOIN {$table['name']} {$table['nick']} ON {$table['nick']}.{$table['id']} = $onJoin\n";
}
$inner .= $this->getWhere($where);
return $this->query($inner);
}
/**
* $columns = ['column', 'value']
* @param array $columns
*/
function create($columns) {
return $this->query("INSERT INTO {$this->table} (" . implode(',', array_keys($columns)) . ") VALUES('" . implode("','", array_values($columns)) . "'); ");
}
/**
* $update [
* ['column' => 'value']
* ]
*
* $where = [
* ['column', 'operator', 'value']
* ];
*
* @param type $update
*/
function update($update, $where = []) {
$sets = [];
foreach ($update as $data) {
array_push($sets, sprintf("%s = '%s'", array_keys($data)[0], array_values($data)[0]));
}
return $this->query("UPDATE {$this->table} SET " . implode(',', $sets) . $this->getWhere($where) . ";");
}
/**
* $notwhere -> secure to delete all register without filter
*
* CALL -> delete(['id', '=', 15, 'calldate', '>', '2021-09-01']);
* CALL (SECURE)-> delete([], true);
*
* @param array $where
* @param bool $notwhere
* @return boolean
*/
function delete($where, $notwhere = false) {
if ($where || $notwhere) {
return $this->query("DELETE FROM {$this->table} " . $this->getWhere($where));
}
return false;
}
function limit($limit){
if(!$this->query){
return null;
}
return $this->query($this->query . " LIMIT $limit");
}
function orderby($order, $type = 'ASC'){
if(!$this->query){
return null;
}
if(is_array($order)){
$columns = [];
foreach ($order as $column){
$columns[] = $this->nickTable($column);
}
$order = implode(',', $columns);
}
return $this->query($this->query . " ORDER BY $order $type");
}
########################################################################
function setTable($table) {
$this->table = $table;
}
function setColumns($columns) {
$this->columns = $columns;
}
function setId($id) {
$this->id = $id;
}
function query($query = null){
if($query){
$this->query = $query;
}
return $this->query;
}
########################################################################
private function getRelations($relation) {
$relations = ['INNER', 'LEFT', 'RIGHT', 'FULL OUTER', 'FULL'];
if (in_array($relation, $relations)) {
return strtoupper($relation);
}
return 'INNER';
}
private function getOperators() {
return ['=', '>=', '<=', '>', '<', '<>', '!=', '!<', '>!', 'LIKE', 'ILIKE', 'IN', 'BETWEEN'];
}
private function isMultidimensional($array) {
return count($array) !== count($array, COUNT_RECURSIVE);
}
private function getWhere($where) {
$whereAnd = "";
if ($where) {
$ands = function($condition) {
if (in_array(strtoupper($condition[1]), $this->getOperators())) {
$operator = $condition[1];
$value = $condition[2];
} else {
$operator = '=';
$value = $condition[1];
}
return [$operator, $value];
};
$whereAnd = " WHERE 1=1 ";
if (!$this->isMultidimensional($where)) {
$operador = $ands($where);
$whereAnd .= " AND {$this->nickTable($where[0])} {$operador[0]} '{$operador[1]}'";
} else {
foreach ($where as $condition) {
$operador = $ands($condition);
$whereAnd .= " AND {$this->nickTable($condition[0])} {$operador[0]} '{$operador[1]}'";
}
}
}
return $whereAnd;
}
private function setAllTables($id, $table, $columnRelation = '') {
if ($this->getAllTables($table)) {
return;
}
$count = 1;
while ($count <= 26) {
$nick = strtolower($this->randLetter());
if ($nick && !array_key_exists($nick, $this->allTables)) {
break;
}
$count++;
}
$this->allTables[$nick] = ['id' => $id, 'name' => $table, 'columnRelation' => $columnRelation, 'nick' => $nick];
}
private function nickTable($capture) {
if (is_array($capture)) {
$columns = [];
foreach ($capture as $column){
$columns[] = $this->nickTable($column);
}
return $columns;
} else {
$search = explode('.', $capture);
$tables = $this->getAllTables($search[0]);
if ($tables) {
return "{$tables['nick']}.{$search[1]}";
}
}
return $capture;
}
private function getAllTables($table) {
foreach ($this->allTables as $tables) {
if ($table == $tables['name']) {
return $tables;
}
}
return null;
}
private function randLetter() {
$alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$arrayAlpha = str_split($alphabet);
$rand = rand(1, count($arrayAlpha));
return $arrayAlpha[$rand];
}
}