Skip to content
Snippets Groups Projects
RegisteredMailController.php 34.23 KiB
<?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 Registered Mail Controller
 * @author dev@maarch.org
 */

namespace RegisteredMail\controllers;

use Com\Tecnick\Barcode\Barcode;
use Contact\controllers\ContactController;
use Contact\models\ContactModel;
use Convert\models\AdrModel;
use Docserver\controllers\DocserverController;
use Group\controllers\PrivilegeController;
use Parameter\models\ParameterModel;
use RegisteredMail\models\IssuingSiteModel;
use RegisteredMail\models\RegisteredMailModel;
use RegisteredMail\models\RegisteredNumberRangeModel;
use Resource\controllers\ResController;
use Resource\models\ResModel;
use Respect\Validation\Validator;
use setasign\Fpdi\Tcpdf\Fpdi;
use Slim\Http\Request;
use Slim\Http\Response;
use SrcCore\models\ValidatorModel;

class RegisteredMailController
{
    public function update(Request $request, Response $response, array $args)
    {
        if (!ResController::hasRightByResId(['resId' => [$args['resId']], 'userId' => $GLOBALS['id']])) {
            return $response->withStatus(400)->withJson(['errors' => 'Resource out of perimeter']);
        }

        $registeredMail = RegisteredMailModel::getByResId(['select' => ['issuing_site', 'type', 'deposit_id'], 'resId' => $args['resId']]);
        if (empty($registeredMail)) {
            return $response->withStatus(400)->withJson(['errors' => 'No registered mail for this resource']);
        } elseif (!empty($registeredMail['deposit_id'])) {
            return $response->withStatus(400)->withJson(['errors' => 'Registered mail can not be modified (deposit list already generated)']);
        }

        $body = $request->getParsedBody();

        if (!Validator::stringType()->notEmpty()->validate($body['type'])) {
            return $response->withStatus(400)->withJson(['errors' => 'Body type is empty or not a string']);
        } elseif (!Validator::stringType()->notEmpty()->validate($body['warranty'])) {
            return $response->withStatus(400)->withJson(['errors' => 'Body warranty is empty or not a string']);
        } elseif (!in_array($body['type'], ['2D', '2C', 'RW'])) {
            return $response->withStatus(400)->withJson(['errors' => 'Body type is not correct']);
        } elseif (!in_array($body['warranty'], ['R1', 'R2', 'R3'])) {
            return $response->withStatus(400)->withJson(['errors' => 'Body warranty is not correct']);
        } elseif ($body['type'] == 'RW' && $body['warranty'] == 'R3') {
            return $response->withStatus(400)->withJson(['errors' => 'Body warranty R3 is not allowed for type RW']);
        } elseif (!Validator::notEmpty()->validate($body['recipient'])) {
            return $response->withStatus(400)->withJson(['errors' => 'Body recipient is empty']);
        }

        $issuingSite = IssuingSiteModel::getById([
            'id'        => $registeredMail['issuing_site'],
            'select'    => ['label', 'address_number', 'address_street', 'address_additional1', 'address_additional2', 'address_postcode', 'address_town', 'address_country']
        ]);
        if (empty($issuingSite)) {
            return $response->withStatus(400)->withJson(['errors' => 'Issuing site does not exist']);