Skip to content
Snippets Groups Projects
DocumentModel.php 3.42 KiB
Newer Older
  • Learn to ignore specific revisions
  • Damien's avatar
    Damien committed
    <?php
    
    /**
    * Copyright Maarch since 2008 under licence GPLv3.
    * See LICENCE.txt file at the root folder for more details.
    * This file is part of Maarch software.
    *
    */
    
    /**
    * @brief Document Model
    * @author dev@maarch.org
    */
    
    namespace Document\models;
    
    use SrcCore\models\ValidatorModel;
    use SrcCore\models\DatabaseModel;
    
    
    class DocumentModel
    
    Damien's avatar
    Damien committed
    {
    
        public static function get(array $aArgs)
        {
            ValidatorModel::notEmpty($aArgs, ['select']);
    
    Damien's avatar
    Damien committed
            ValidatorModel::arrayType($aArgs, ['select', 'where', 'data', 'orderBy']);
    
            ValidatorModel::intType($aArgs, ['limit', 'offset']);
    
            $aDocuments = DatabaseModel::select([
                'select'    => $aArgs['select'],
                'table'     => ['main_documents'],
                'where'     => empty($aArgs['where']) ? [] : $aArgs['where'],
                'data'      => empty($aArgs['data']) ? [] : $aArgs['data'],
    
    Damien's avatar
    Damien committed
                'orderBy'   => empty($aArgs['orderBy']) ? [] : $aArgs['orderBy'],
    
                'offset'    => empty($aArgs['offset']) ? 0 : $aArgs['offset'],
    
                'limit'     => empty($aArgs['limit']) ? 0 : $aArgs['limit'],
    
            ]);
    
            return $aDocuments;
        }
    
    
        public static function getById(array $args)
    
            ValidatorModel::notEmpty($args, ['select', 'id']);
            ValidatorModel::arrayType($args, ['select']);
    
    
            $document = DatabaseModel::select([
    
                'select'    => $args['select'],
    
                'table'     => ['main_documents'],
                'where'     => ['id = ?'],
    
                'data'      => [$args['id']]
    
            ]);
    
            if (empty($document[0])) {
                return [];
            }
    
            return $document[0];
        }
    
    
        public static function create(array $args)
    
    Damien's avatar
    Damien committed
        {
    
            ValidatorModel::notEmpty($args, ['title', 'sender', 'metadata']);
    
            ValidatorModel::stringType($args, ['title', 'reference', 'description', 'sender', 'deadline', 'notes', 'link_id', 'metadata']);
    
    Damien's avatar
    Damien committed
    
            $nextSequenceId = DatabaseModel::getNextSequenceValue(['sequenceId' => 'main_documents_id_seq']);
    
            DatabaseModel::insert([
                'table'         => 'main_documents',
                'columnsValues' => [
                    'id'                => $nextSequenceId,
    
                    'title'             => $args['title'],
                    'reference'         => $args['reference'],
                    'description'       => $args['description'],
                    'sender'            => $args['sender'],
                    'deadline'          => $args['deadline'],
    
                    'notes'             => $args['notes'],
    
                    'link_id'           => $args['link_id'],
    
                    'metadata'          => $args['metadata']
    
    Damien's avatar
    Damien committed
                ]
            ]);
    
            return $nextSequenceId;
        }
    
    
        public static function update(array $args)
    
    Florian Azizian's avatar
    Florian Azizian committed
        {
    
            ValidatorModel::notEmpty($args, ['set', 'where', 'data']);
            ValidatorModel::arrayType($args, ['set', 'where', 'data']);
    
    Florian Azizian's avatar
    Florian Azizian committed
    
            DatabaseModel::update([
                'table' => 'main_documents',
    
                'set'   => $args['set'],
                'where' => $args['where'],
                'data'  => $args['data']
    
    Florian Azizian's avatar
    Florian Azizian committed
            ]);
    
            return true;
        }
    
    
        public static function delete(array $args)
    
            ValidatorModel::notEmpty($args, ['where', 'data']);
            ValidatorModel::arrayType($args, ['where', 'data']);
    
    
            DatabaseModel::delete([
                'table' => 'main_documents',
    
                'where' => $args['where'],
                'data'  => $args['data']
    
    Damien's avatar
    Damien committed
    }