Skip to content
Snippets Groups Projects
HistoryController.php 3.19 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?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 History Controller
    
    * @author dev@maarch.org
    */
    
    
    Damien's avatar
    Damien committed
    namespace History\controllers;
    
    Alex ORLUC's avatar
    Alex ORLUC committed
    use Core\Models\ServiceModel;
    
    use SrcCore\controllers\LogsController;
    
    use SrcCore\models\ValidatorModel;
    
    Damien's avatar
    Damien committed
    use History\models\HistoryModel;
    
    use Notification\controllers\NotificationsEventsController;
    
    Damien's avatar
    Damien committed
    use Slim\Http\Request;
    use Slim\Http\Response;
    
    Damien's avatar
    Damien committed
    use User\models\UserModel;
    
    
    class HistoryController
    {
    
    Damien's avatar
    Damien committed
        public static function add(array $aArgs)
    
            ValidatorModel::notEmpty($aArgs, ['tableName', 'recordId', 'eventType', 'info', 'eventId']);
    
            ValidatorModel::stringType($aArgs, ['tableName', 'eventType', 'info', 'eventId', 'moduleId', 'level']);
    
    Damien's avatar
    Damien committed
    
    
            if (empty($aArgs['isTech'])) {
    
                $aArgs['isTech'] = false;
            }
    
            if (empty($aArgs['moduleId'])) {
    
                $aArgs['moduleId'] = 'admin';
    
            if (empty($aArgs['level'])) {
    
                $aArgs['level'] = 'DEBUG';
    
            LogsController::add($aArgs);
    
            HistoryModel::create([
                'tableName' => $aArgs['tableName'],
                'recordId'  => $aArgs['recordId'],
                'eventType' => $aArgs['eventType'],
    
                'userId'    => $GLOBALS['userId'],
    
                'info'      => $aArgs['info'],
                'moduleId'  => $aArgs['moduleId'],
                'eventId'   => $aArgs['eventId'],
            ]);
    
            NotificationsEventsController::fill_event_stack([
                "eventId"   => $aArgs['eventId'],
                "tableName" => $aArgs['tableName'],
                "recordId"  => $aArgs['recordId'],
                "userId"    => $GLOBALS['userId'],
                "info"      => $aArgs['info'],
    
        public function getByUserId(Request $request, Response $response, array $aArgs)
        {
            $user = UserModel::getById(['id' => $aArgs['userSerialId'], 'select' => ['user_id']]);
            if ($user['user_id'] != $GLOBALS['userId'] && !ServiceModel::hasService(['id' => 'view_history', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) {
                return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
            }
    
            $aHistories = HistoryModel::getByUserId(['userId' => $user['user_id'], 'select' => ['info', 'event_date']]);
    
            return $response->withJson(['histories' => $aHistories]);
        }
    
    
        public function get(Request $request, Response $response, array $aArgs)
    
    Damien's avatar
    Damien committed
            if (!ServiceModel::hasService(['id' => 'view_history', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) {
    
    Alex ORLUC's avatar
    Alex ORLUC committed
                return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
            }
    
    Damien's avatar
    Damien committed
    
    
            $historyList                     = HistoryModel::getHistoryList(['event_date' => $aArgs['date']]);
            $historyListFilters['users']     = HistoryModel::getFilter(['select' => 'user_id', 'event_date' => $aArgs['date']]);
    
    Damien's avatar
    Damien committed
            $historyListFilters['eventType'] = HistoryModel::getFilter(['select' => 'event_type', 'event_date' => $aArgs['date']]);
    
    Damien's avatar
    Damien committed
            return $response->withJson(['filters' => $historyListFilters, 'historyList' => $historyList]);