Skip to content
Snippets Groups Projects
NoteController.php 6.4 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alex ORLUC's avatar
    Alex ORLUC 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 Note Controller
     * @author dev@maarch.org
     * @ingroup core
     */
    
    namespace Note\controllers;
    
    use Note\models\NoteModel;
    
    use Note\models\NoteEntityModel;
    
    use Entity\models\EntityModel;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    use Respect\Validation\Validator;
    
    Damien's avatar
    Damien committed
    use setasign\Fpdi\Tcpdf\Fpdi;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    use Slim\Http\Request;
    use Slim\Http\Response;
    
    Vinciane's avatar
    Vinciane committed
    use History\controllers\HistoryController;
    
    use Resource\controllers\ResController;
    
    Damien's avatar
    Damien committed
    use SrcCore\models\ValidatorModel;
    use User\models\UserModel;
    
    use Template\models\TemplateModel;
    use Resource\models\ResModel;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
    class NoteController
    {
    
    Damien's avatar
    Damien committed
        public function getByResId(Request $request, Response $response, array $aArgs)
    
    Alex ORLUC's avatar
    Alex ORLUC committed
        {
    
            $check = Validator::intVal()->notEmpty()->validate($aArgs['resId']);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            if (!$check) {
    
                return $response->withStatus(400)->withJson(['errors' => 'resId is empty or not an integer']);
            }
    
            if (!ResController::hasRightByResId(['resId' => $aArgs['resId'], 'userId' => $GLOBALS['userId']])) {
                return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
            }
    
    
    Damien's avatar
    Damien committed
            $aNotes = NoteModel::getByResId(['select' => ['notes.id', 'firstname', 'lastname', 'entity_label', 'note_text', 'creation_date'], 'resId' => $aArgs['resId'], 'orderBy' => ['creation_date DESC']]);
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    
            return $response->withJson($aNotes);
        }
    
    Damien's avatar
    Damien committed
        public function create(Request $request, Response $response, array $aArgs)
    
    Vinciane's avatar
    Vinciane committed
        {
            $data = $request->getParams();
    
            $check = Validator::stringType()->notEmpty()->validate($data['note_text']);
    
            if (!$check) {
    
    Damien's avatar
    Damien committed
                return $response->withStatus(400)->withJson(['errors' => 'Data note_text is empty or not a string']);
    
            }
    
            if (!ResController::hasRightByResId(['resId' => $aArgs['resId'], 'userId' => $GLOBALS['userId']])) {
                return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
    
            if (isset($data['entities_chosen'])) {
    
    Damien's avatar
    Damien committed
                if (!Validator::arrayType()->validate($data['entities_chosen'])) {
    
                    return $response->withStatus(400)->withJson(['errors' => 'entities_chosen is not an array']);
    
    Florian Azizian's avatar
    Florian Azizian committed
                foreach ($data['entities_chosen'] as $entityId) {
    
                    if ($entityId == null) {
                        return $response->withStatus(400)->withJson(['errors' => 'Bad Request entities chosen']);
                    }
                    
                    $entity = entitymodel::getByEntityId(['select' => ['id'], 'entityId' => $entityId]);
    
    Damien's avatar
    Damien committed
                    if (empty($entity['id'])) {
                        return $response->withStatus(400)->withJson(['errors' => 'Bad Request entities chosen']);
                    }
    
            $noteId = NoteModel::create([
                'resId'     => $aArgs['resId'],
                'login'     => $GLOBALS['userId'],
                'note_text' => $data['note_text']
            ]);
    
            if (!empty($noteId) && !empty($data['entities_chosen'])) {
    
    Florian Azizian's avatar
    Florian Azizian committed
                foreach ($data['entities_chosen'] as $entity) {
    
                    NoteEntityModel::create(['item_id' => $entity, 'note_id' => $noteId]);
    
    Vinciane's avatar
    Vinciane committed
                }
    
            HistoryController::add([
    
    Vinciane's avatar
    Vinciane committed
                'tableName' => "notes",
    
                'recordId'  => $noteId,
    
    Vinciane's avatar
    Vinciane committed
                'eventType' => "ADD",
    
                'userId'    => $GLOBALS['userId'],
    
                'info'      => _NOTE_ADDED . " (" . $noteId . ")",
    
    Vinciane's avatar
    Vinciane committed
                'moduleId'  => 'notes',
    
                'eventId'   => 'noteadd'
            ]);
    
            return $response->withJson(['noteId' => $noteId]);
    
    Damien's avatar
    Damien committed
    
        public static function getEncodedPdfByIds(array $aArgs)
        {
            ValidatorModel::notEmpty($aArgs, ['ids']);
            ValidatorModel::arrayType($aArgs, ['ids']);
    
    
    Damien's avatar
    Damien committed
            $pdf = new Fpdi('P', 'pt');
    
    Damien's avatar
    Damien committed
            $pdf->setPrintHeader(false);
            $pdf->AddPage();
    
            foreach ($aArgs['ids'] as $noteId) {
    
    Damien's avatar
    Damien committed
                $note = NoteModel::getById(['id' => $noteId, 'select' => ['note_text', 'creation_date', 'user_id']]);
    
    Damien's avatar
    Damien committed
    
                $user = UserModel::getByLogin(['login' => $note['user_id'], 'select' => ['firstname', 'lastname']]);
    
    Damien's avatar
    Damien committed
                $date = new \DateTime($note['creation_date']);
    
    Damien's avatar
    Damien committed
                $date = $date->format('d-m-Y H:i');
    
                $pdf->Cell(0, 20, "{$user['firstname']} {$user['lastname']} : {$date}", 1, 2, 'C', false);
    
    Florian Azizian's avatar
    Florian Azizian committed
                $pdf->MultiCell(0, 20, $note['note_text'], 1, 'L', false);
    
    Damien's avatar
    Damien committed
                $pdf->SetY($pdf->GetY() + 40);
            }
            $fileContent = $pdf->Output('', 'S');
    
            return ['encodedDocument' => base64_encode($fileContent)];
        }
    
        public static function getTemplateListByResId(Request $request, Response $response, array $aArgs)
    
            $check = Validator::intVal()->notEmpty()->validate($aArgs['resId']);
            if (!$check) {
                return $response->withStatus(400)->withJson(['errors' => 'resId is empty or not an integer']);
            }
    
            if (!empty($aArgs['resId']) && !ResController::hasRightByResId(['resId' => $aArgs['resId'], 'userId' => $GLOBALS['userId']])) {
                return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
            }
    
            //get all templates note
            $tmpAllNotes = TemplateModel::getByTarget(['template_target' => 'notes', 'select' => ['template_id', 'template_label', 'template_content']]);
    
            //get entity resource
            $resEntity = ResModel::getById(['resId' => $aArgs['resId'], 'select' => ['destination']]);
    
            if (!empty($resEntity['destination'])) {
                //get retricted templates note
                $aReturn = TemplateModel::getWithAssociation(['select' => ['DISTINCT(templates.template_id), template_label', 'template_content'], 'where' => ['template_target = ?', 'value_field = ?', 'templates.template_id = templates_association.template_id'], 'data' => ['notes', $resEntity['destination']], 'orderBy' => ['template_label']]);
            } else {
                $aReturn = TemplateModel::getByTarget(['template_target' => 'notes', 'select' => ['template_label', 'template_content']]);
            }
    
            return $response->withJson($aReturn);
        }
    
        public static function getAllTemplateList(Request $request, Response $response)
        {
            //get all templates note
            $aReturn = TemplateModel::getByTarget(['template_target' => 'notes', 'select' => ['template_label', 'template_content']]);
    
    
            return $response->withJson($aReturn);
        }
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    }