connection = new Mysql(); } /** Carrega os campos de uma tabela */ public function Describe() { /** Consulta SQL */ $this->sql = "describe documents"; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ $this->field = $this->stmt->fetchAll(\PDO::FETCH_OBJ); /** Declara o objeto */ $resultDescribe = new \stdClass(); $Field = ''; /** Lista os campos da tabela para objetos */ foreach($this->field as $UsersKey => $Result){ /** Pega o nome do Field/Campo */ $Field = $Result->Field; /** Carrega os objetos como null */ $resultDescribe->$Field = null; } /** Retorna os campos declarados como vazios */ return $resultDescribe; } /** Lista os registros do banco de dados com limitação */ public function Get(int $documentsId) { /** Parametros de entrada */ $this->documentsId = $documentsId; /** Consulta SQL */ $this->sql = 'select * from documents where documents_id = :documents_id'; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam(':documents_id', $this->documentsId); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject(); } /** Lista todos os egistros do banco com ou sem paginação*/ public function All(? int $start, ? int $max, ? int $documentsCategorysId, ? string $tag, ? string $label, ? bolean $companyId) { /** Parametros de entrada */ $this->start = $start; $this->max = $max; $this->documentsCategorysId = $documentsCategorysId; $this->companyId = $companyId; $this->tag = $tag; $this->label = $label; $this->and = ""; /** Se houver categoria informada, crio o filtro de consulta*/ if($this->documentsCategorysId > 0){ $this->and = " and d.documents_categorys_id = {$this->documentsCategorysId}"; $this->and .= " and json_search(tag, 'all', '".$this->tag."', null, '$.".$this->label."') is not null"; } /** Verifico se há paginação */ if($this->max){ $this->limit = "limit $this->start, $this->max"; } /** Se houver um cliente informado, crio o filtro de consulta */ if($this->companyId === true){ $this->and = " and company_id is not null"; } elseif ($this->companyId === false) { $this->and = " and company_id is null"; } /** Consulta SQL */ $this->sql = 'select d.documents_id, d.documents_drafts_id, d.documents_categorys_id, d.user_id, d.company_id, d.description, d.date_register, d.archive, d.extension, d.active, d.tag, dc.description as categorys, c.name_fantasy from documents d left join companies c on d.company_id = c.company_id left join documents_categorys dc on d.documents_categorys_id = dc.documents_categorys_id where d.documents_id is not null '; $this->sql .= $this->and; $this->sql .= ' order by d.documents_id desc '; $this->sql .= $this->limit; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchAll(\PDO::FETCH_OBJ); } /** Conta a quantidades de registros */ public function Count(? int $documentsCategorysId, ? string $tag, ? string $label, ? bolean $companyId) { /** Parametros de entraa */ $this->documentsCategorysId = $documentsCategorysId; $this->tag = $tag; $this->label = $label; $this->companyId = $companyId; $this->and = ""; /** Se houver categoria informada, crio o filtro de consulta*/ if($this->documentsCategorysId > 0){ $this->and = " and documents_categorys_id = {$this->documentsCategorysId}"; $this->and .= " and json_search(tag, 'all', '".$this->tag."', null, '$.".$this->label."') is not null"; } /** Se houver um cliente informado, crio o filtro de consulta */ if($this->companyId === true){ $this->and = " and company_id is not null"; } elseif ($this->companyId === false) { $this->and = " and company_id is null"; } /** Consulta SQL */ $this->sql = 'select count(documents_id) as qtde from documents where documents_id is not null '.$this->and; /** Preparo o SQL para execução */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Executo o SQL */ $this->stmt->execute(); /** Retorno o resultado */ return $this->stmt->fetchObject()->qtde; } /** Insere um novo registro no banco */ public function Save(int $documentsId, int $documentsCategorysId, string $description, string $archive, string $extension, object $tag, int $financialMovementsId, ? int $companyId) { /** Parametros */ $this->documentsId = $documentsId; $this->documentsCategorysId = $documentsCategorysId; $this->companyId = $companyId == 0 ? null : $companyId; $this->description = $description; $this->archive = $archive; $this->extension = $extension; $this->tag = json_encode($tag, JSON_PRETTY_PRINT); $this->financialMovementsId = $financialMovementsId; /** Verifica se o ID do registro foi informado */ if($this->documentsId > 0){ /** Consulta SQL */ $this->sql = 'update documents set description = :description, tag = :tag, company_id = :company_id where documents_id = :documents_id'; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('documents_id', $this->documentsId); $this->stmt->bindParam('company_id', $this->companyId); $this->stmt->bindParam('description', $this->description); $this->stmt->bindParam('tag', $this->tag); }elseif($this->financialMovementsId > 0){ /** Consulta SQL */ $this->sql = 'insert into documents(user_id, company_id, description, archive, extension, financial_movements_id, tag ) values (:user_id, :company_id, :description, :archive, :extension, :financial_movements_id, :tag)'; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('user_id', $_SESSION['USERSID']);/** Informa o usuário responsável pelo novo arquivo cadastrado */ $this->stmt->bindParam('company_id', $this->companyId); $this->stmt->bindParam('description', $this->description); $this->stmt->bindParam('archive', $this->archive); $this->stmt->bindParam('extension', $this->extension); $this->stmt->bindParam('financial_movements_id', $this->financialMovementsId); $this->stmt->bindParam('tag', $this->tag); }else{//Se o ID não foi informado, grava-se um novo registro /** Consulta SQL */ $this->sql = 'insert into documents(documents_id, documents_categorys_id, user_id, company_id, description, archive, extension, tag, financial_movements_id ) values (:documents_id, :documents_categorys_id, :user_id, :company_id, :description, :archive, :extension, :tag, :financial_movements_id)'; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('documents_id', $this->documentsId); $this->stmt->bindParam('documents_categorys_id', $this->documentsCategorysId); $this->stmt->bindParam('user_id', $_SESSION['USERSID']);/** Informa o usuário responsável pelo novo arquivo cadastrado */ $this->stmt->bindParam('company_id', $this->companyId); $this->stmt->bindParam('description', $this->description); $this->stmt->bindParam('archive', $this->archive); $this->stmt->bindParam('extension', $this->extension); $this->stmt->bindParam('tag', $this->tag); $this->stmt->bindParam('financial_movements_id', $this->financialMovementsId); } /** Executo o SQL */ return $this->stmt->execute(); } /** Deleta um determinado registro no banco de dados */ function Delete(int $documentsId) { /** Parametros de entrada */ $this->documentsId = $documentsId; /** Consulta SQL */ $this->sql = 'delete from documents where documents_id = :documents_id'; /** Preparo o sql para receber os valores */ $this->stmt = $this->connection->connect()->prepare($this->sql); /** Preencho os parâmetros do SQL */ $this->stmt->bindParam('documents_id', $this->documentsId); /** Executo o SQL */ return $this->stmt->execute(); } /** Fecha uma conexão aberta anteriormente com o banco de dados */ function __destruct() { $this->connection = null; } }