Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
19 kB
0
Indexable
Never
<?php

/**
 * Lアラート一覧
 */

namespace App\Http\Controllers;

use App\Consts\SystemConst;
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
use App\Library\LogEx;
use App\Http\Controllers\Core\PageController;
use App\Infrastructure\Lalert\LalertRequest;
use App\Infrastructure\CmsCategory\CmsCategoryAction;
use App\Infrastructure\CmsSettingFunctionData\CmsSettingFDAction;
use App\Infrastructure\CmsUser\CmsUserAction;
use App\Infrastructure\Lalert\LalertAction;
use App\Infrastructure\Lalert\LalertXmlObjectAction;
use App\Infrastructure\Info\InfoAction;
use App\Infrastructure\Info\InfoSetParameterAction;
use App\Infrastructure\LvGroup\LvGroupAction;
use App\Infrastructure\LvInfoAudio\LvInfoAudioAction;
use App\Infrastructure\LvRole\LvRoleAction;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;

class LalertController extends PageController
{
    /**
     * コンストラクタ
     */
    public function __construct()
    {
        parent::__construct();
        $ssmInfo = json_decode(json_encode(Config::get('app.ssm.info')), true) ?? [];
        if (isset($ssmInfo['link_system']) && $ssmInfo['link_system'] == true) {
            $this->loadSsmToConfig(Config('app.ssm.name') . 'link_system');
        }

        if (isset($ssmInfo['language']) && $ssmInfo['language'] == true) {
            $this->loadSsmToConfig(Config('app.ssm.name') . 'language');
        }

        if (isset($ssmInfo['options'])) {
            if (is_array($ssmInfo['options'])) {
                foreach ($ssmInfo['options'] as $value) {
                    $this->loadSsmToConfig(Config('app.ssm.name') . $value);
                }
            } elseif ($ssmInfo['options'] != '') {
                $this->loadSsmToConfig(Config('app.ssm.name') . $ssmInfo['options']);
            }
        }
        $this->loadSsmToConfig(Config('app.ssm.name') . 'toutatsu');
    }

    /**
     * 一覧画面
     *
     * @param Request $request
     * @return View
     */
    public function index(Request $request): View
    {
        LogEx::infolog('Start', ['request' => $request->toArray()]);
        $cmsApisId = $request->get('cms_apis_id');
        // 検索・ソートパラメータを作成して一覧を取得
        $sortColumns = ['xml_type', 'send_flg', 'areanames'];
        $pageParam = $this->getPageParam($request, $sortColumns, ['xml_type', 'desc']);
        $order_link = $this->getDefaultOrderLink($sortColumns, $pageParam);
        $isSorting = $request->has('sort');
        $lalertAction = new LalertAction();
        $lalerts = $lalertAction->list($pageParam['sort'], $pageParam['order'], $pageParam['search']);
        $infoAction = new InfoAction();
        $audios = $infoAction->getAudios();
        foreach ($audios as $k => $v) {
            $audios[$k]['name'] = addslashes($v['name'] ?? '');
        }

        LogEx::infolog('End');
        return view('page.lalert.lalert_list', [
            'lalerts' => $lalerts,
            'pageParam' => $pageParam,
            'sort' => $pageParam['sort'],
            'order' => $pageParam['order'],
            'search' => $pageParam['search'],
            'audios' => $audios,
            'order_link' => $order_link,
            'isSorting' => $isSorting,
            'cmsApisId' => $cmsApisId,
        ]);
    }

    /**
     * 受信リスト
     *
     * @param Request $request
     * @return View
     */
    public function xmlList(Request $request): View
    {
        LogEx::infolog('start', $request->toArray());
        // 検索・ソートパラメータを作成して一覧を取得
        $sortColumns = ['deliver_time', 'xml_type', 'title'];
        $isSorting = $request->has('sort');
        $pageParam = $this->getPageParam($request, $sortColumns, ['deliver_time', 'desc']);
        $order_link = $this->getDefaultOrderLink($sortColumns, $pageParam);
        $LalertXmlObjectAction = new LalertXmlObjectAction();
        $lalerts = $LalertXmlObjectAction->list($pageParam['sort'], $pageParam['order'], $pageParam['search']);
        LogEx::infolog('end');
        return view('page.lalert.lalert_xml_list', [
            'lalerts' => $lalerts,
            'pageParam' => $pageParam,
            'sort' => $pageParam['sort'],
            'order' => $pageParam['order'],
            'search' => $pageParam['search'],
            'order_link' => $order_link,
            'calendar' => false,
            'isSorting' => $isSorting,
        ]);
    }

    /**
     * Send lalert
     *
     * @param Request $request
     * @param int|null $id;
     *
     * @return void
     */
    public function xmlSend(Request $request, ?int $id = null): void
    {
        LogEx::infolog('start', $request->toArray());
        $lalertXmlObjectAction = new LalertXmlObjectAction();
        $lalertXmlObject = $lalertXmlObjectAction->getById($id);
        $decodedLalertXmlObject = json_decode($lalertXmlObject->value);
        $lalertAction = new LalertAction();
        $lalertAction->sendFromXmlObject($decodedLalertXmlObject);
        LogEx::infolog('end');
        return;
    }

    /**
     * Xml View
     *
     * @param Request $request
     * @param int|null $id;
     *
     * @return Response
     */
    public function xmlView(Request $request, ?int $id = null): Response
    {
        LogEx::infolog('start', $request->toArray());
        $lalertXmlObjectAction = new LalertXmlObjectAction();
        $lalertXmlObject = $lalertXmlObjectAction->getById($id);
        LogEx::infolog('end');
        return response($lalertXmlObject->value, 200);
    }

    /**
     * 配信リスト一覧
     *
     * @param Request $request
     * @return View
     */
    public function infoList(Request $request): View
    {
        LogEx::infolog('start', $request->toArray());
        // 検索・ソートパラメータを作成して一覧を取得
        $sortColumns = ['id', 'status', 'delivery_datetime', 'category_id', 'title', 'deliver_to'];
        $isSorting = $request->has('sort');
        $pageParam = $this->getPageParam($request, $sortColumns, ['delivery_datetime', 'desc']);
        $order_link = $this->getDefaultOrderLink($sortColumns, $pageParam);
        $infoAction = new InfoAction();
        $infos = $infoAction->lalertInfoList($pageParam['sort'], $pageParam['order'], $pageParam['search']);
        // 地域・グループ
        $lvRoleAction = new LvRoleAction();
        $roles = $lvRoleAction->list();
        $lvGroupAction = new LvGroupAction();
        $groups = $lvGroupAction->list();
        // オーディオ情報
        $lvInfoAudioAction = new LvInfoAudioAction();
        $audios = $lvInfoAudioAction->list();
        $infoSetParameterAction = new InfoSetParameterAction();
        $dataInfoParameterStore = $infoSetParameterAction->setDataInfoParameter();
        $cmsSettingFDAction = new CmsSettingFDAction();
        $iconHeader =
            $cmsSettingFDAction->getFunctionDataByFunctionType(SystemConst::APP_COMMON_FUNCTION[0]);
        $functionName = isset($iconHeader->function_name) ? $iconHeader->function_name : '';
        $isRegist = empty($request->regist) ? false : $request->regist;
        LogEx::infolog('end');
        return view('page.lalert.lalert_info_list', [
            'infos' => $infos,
            'pageParam' => $pageParam,
            'sort' => $pageParam['sort'],
            'order' => $pageParam['order'],
            'search' => $pageParam['search'],
            'order_link' => $order_link,
            'calendar' => false,
            'dayInfo' => [],
            'roles' => $roles,
            'groups' => $groups,
            'audios' => $audios,
            'dataInfoParameterStore' => $dataInfoParameterStore,
            'is_regist' => $isRegist,
            'tokenApi' => Auth::user()->token,
            'apiUrl' => Config::get('app.api_info_url'),
            'functionName' => $functionName,
            'isSorting' => $isSorting,
        ]);
    }


    /**
     * カレンダー表示
     *
     * @param Request $request
     * @return View
     */
    public function calendar(Request $request): View
    {
        LogEx::infolog('start', $request->toArray());
        // 検索・ソートパラメータを作成して一覧を取得
        $sortColumns = ['id', 'status', 'delivery_datetime', 'category_id', 'title', 'deliver_to'];
        $pageParam = $this->getPageParam($request, $sortColumns, ['delivery_datetime', 'desc']);
        $order_link = $this->getDefaultOrderLink($sortColumns, $pageParam);
        $lalertAction = new LalertAction();
        $infos = $lalertAction->list($pageParam['sort'], $pageParam['order'], $pageParam['search']);
        LogEx::infolog('end');
        return view('page.lalert.lalert_list', [
            'infos' => $infos,
            'pageParam' => $pageParam,
            'sort' => $pageParam['sort'],
            'order' => $pageParam['order'],
            'search' => $pageParam['search'],
            'order_link' => $order_link,
            'calendar' => true,
        ]);
    }

    /**
     * 登録画面
     *
     * @param LalertRequest $request
     * @return View
     */
    public function register(LalertRequest $request): View
    {
        LogEx::infolog('start');
        // お知らせ情報取得(無い場合はデフォルト値)
        $lalert = $request->entity();
        // カテゴリ一覧取得
        $cmsCategoryAction = new CmsCategoryAction();
        $categories = $cmsCategoryAction->getCategories();
        // グループ情報のダミー DB取得が出来たら置換
        $lalert->setup(['deliver_groups' => "[1,2]"]);
        $groups = [
            'テストグループ1',
            'テストグループ2',
            'テストグループ3',
            'テストグループ4',
        ];
        $xml_types = [
            'EvacuationOrder',
            'Shelter',
            'DamageInformation',
            'AntidisasterHeadquarter',
            'UrgentMail',
        ];

        // チャイム音取得
        $infoAction = new InfoAction();
        $audioChimes = $infoAction->getAudios(SystemConst::AUDIO_CHIMES, SystemConst::MESSAGE_TYPE_EMERGENCY);

        $cmsUserAction = new CmsUserAction();
        $cmsUsers = $cmsUserAction->getAdminCmsUser();
        $audioEtc = $infoAction->getAudios(SystemConst::AUDIO_ETC, SystemConst::MESSAGE_TYPE_EMERGENCY);
        $lvGroupAction = new LvGroupAction();
        $user = auth()->user();
        $myGroups = $lvGroupAction->getMyGroup($user->id);
        $lvRoleAction = new LvRoleAction();
        $areas = $lvRoleAction->roles();
        LogEx::infolog('end');
        return view('page.lalert.lalert_modal_register', [
            'lalert' => $lalert,
            'categories' => $categories,
            'groups' => $groups,
            'xmlTypes' => $xml_types,
            'cms_users' => $cmsUsers,
            'audio_chimes' => $audioChimes,
            'audio_etcs' => $audioEtc,
            'my_groups' => $myGroups,
            "areas" => $areas,
        ]);
    }

    /**
     * 編集画面
     *
     * @param LalertRequest $request
     * @param integer|null $id
     * @return View
     */
    public function edit(LalertRequest $request, ?int $id = null): View
    {
        LogEx::infolog('start');
        // カテゴリ取得
        $cmsCategoryAction = new CmsCategoryAction();
        $categories = $cmsCategoryAction->getCategories();
        // $categories =
        // CMS_USER取得
        $cmsUserAction = new CmsUserAction();
        $cmsUsers = $cmsUserAction->getAdminCmsUser();

        // チャイム音取得
        $infoAction = new InfoAction();
        $audioChimes = $infoAction->getAudios(SystemConst::AUDIO_CHIMES, SystemConst::MESSAGE_TYPE_EMERGENCY);
        // チャイム音以外の音取得
        $audioEtc = $infoAction->getAudios(SystemConst::AUDIO_ETC, SystemConst::MESSAGE_TYPE_EMERGENCY);

        $lvRoleAction = new LvRoleAction();
        $areas = $lvRoleAction->roles();
        $lvGroupAction = new LvGroupAction();
        $user = auth()->user();
        $myGroups = $lvGroupAction->getMyGroup($user->id);
        $sectionGroups = $lvGroupAction->getSectionGroups($user);
        // $audio_etcs = $this->cms_model->get_audios('2', $this->CATEGORY_INFO['message_type'], $token);
        $viewParams = [
            "areas" => $areas,
            'my_groups' => $myGroups,
            'groups' => $sectionGroups,
            'xmlTypes' => SystemConst::LALERT_XML_TYPES,
            'audio_chimes' => $audioChimes,
            'audio_etcs' => $audioEtc,
            'categories' => $categories,
            'cms_users' => $cmsUsers,
            's3_url' => config('app.s3_url'),
        ];
        $lalertAction = new LalertAction();
        if ($request->isMethod(Request::METHOD_POST) === false) {
            $lalert = $lalertAction->getById($id);
            if ($lalert) {
                $audioType = (empty($lalert->audio_data2) === false)
                ? SystemConst::RADIO_AUDIO_TYPE['ETC']
                : SystemConst::RADIO_AUDIO_TYPE['CHIME'];
                $viewParams = array_merge(
                    $viewParams,
                    [
                        'xml_type' => $lalert->xml_type,
                        'areanames' => $lalert->areanames,
                        'id' => $lalert->id,
                        'send_flg' => $lalert->send_flg,
                        'category_id' => $lalert->category_id,
                        'category_info' => [],
                        'cms_user_id' => $lalert->cms_user_id,
                        'update_info_deliver_flag' => $lalert->update_info_deliver_flag,
                        "audio_type" => $audioType,
                        "audio_data1" => $lalert->audio_data1,
                        "audio_data2" => $lalert->audio_data2,
                        "audio_data3" => $lalert->audio_data3,
                        "deliver_to" => $lalert->deliver_to,
                        "deliver_roles" => $lalert->deliver_roles,
                        "deliver_groups" => $lalert->deliver_groups,
                    ]
                );
            } else {
                return abort(404, __('message.lalert_setting_not_found'));
            }
        } else {
            $viewParams = array_merge($viewParams, $request->all());
        }
        LogEx::infolog('End');
        return view('page.lalert.lalert_modal_register', $viewParams);
    }

    /**
     * 確認画面
     *
     * @param LalertRequest $request
     * @return View
     */
    public function confirm(LalertRequest $request): View
    {
        // カテゴリ取得
        $cmsCategoryAction = new CmsCategoryAction();
        $categories = $cmsCategoryAction->getCategories();
        // CMS_USER取得
        $cmsUserAction = new CmsUserAction();
        $cmsUsers = $cmsUserAction->getAdminCmsUser();
        // 効果音
        $audioName = $this->getAudioLabel($request);
        // グループ指定の場合、IDをカンマ区切り
        $deliver_groups = $request->get('deliver_groups');
        $data = [];
        $data['deliver_groups'] = '';
        if ($deliver_groups && is_array($deliver_groups)) {
            $data['deliver_groups'] = implode(",", $deliver_groups);
        }
        // 表示用配信先名称取得
        $deliver_disp = '';
        $deliver_to = $request->get('deliver_to');
        $deliver_areas = $request->get('deliver_roles', '');
        $deliver_groups = $request->get('deliver_groups', []);
        $lvRoleAction = new LvRoleAction();
        $lvGroupAction = new LvGroupAction();
        if ($deliver_to == SystemConst::DELIVER_TO_AREAS) {
            $deliver_areas = json_decode($deliver_areas, true);
            $areas = $lvRoleAction->getAreaNames($deliver_areas);
            $deliver_disp = implode(", ", $areas);
        } elseif ($deliver_to == SystemConst::DELIVER_TO_GROUPS) {
            $groups = $lvGroupAction->getGroupNames($deliver_groups);
            $deliver_disp = implode(", ", $groups);
        } elseif ($deliver_to == SystemConst::DELIVER_TO_ALL_USERS) {
            $deliver_disp = SystemConst::DELIVER_TO_NAMES[0];
        } else {
            $deliver_disp = '';
        }
        // 配信先で指定していないデータ削除
        if ($deliver_to == SystemConst::DELIVER_TO_AREAS) {
            $deliver_groups = '';
        } elseif ($deliver_to == SystemConst::DELIVER_TO_GROUPS) {
            $deliver_areas = '';
        } else {
            $deliver_groups = '';
            $deliver_areas = '';
        }
        LogEx::infolog('End');
        return view('page.lalert.lalert_modal_confirm', array_merge(
            $request->all(),
            [
                'audio_name' => $audioName,
                'categories' => $categories,
                'cms_users' => $cmsUsers,
                'deliver_disp' => $deliver_disp,
                'deliver_roles' => $deliver_areas,
                'deliver_groups' => $deliver_groups,
            ]
        ));
    }

    /**
     * 登録処理
     *
     * @param LalertRequest $request
     * @return View
     */
    public function store(LalertRequest $request): View
    {
        LogEx::infolog('Start', ['request' => $request->toArray()]);
        $lalertAction = new LalertAction();
        $lalertAction->update($request);
        LogEx::infolog('End');
        return view('page.result.form', ['formHiddens' => [
            'formResult' => 1,
            'msg' => 2,
        ]]);
    }

    /**
     * 削除処理
     *
     * @param LalertRequest $request
     * @return RedirectResponse
     */
    public function delete(LalertRequest $request): RedirectResponse
    {
        LogEx::infolog('start');
        if (isset($request->id) && $request->id != '') {
            $lalertAction = new LalertAction();
            $lalertAction->delete($request);
        }
        LogEx::infolog('end');
        return redirect()->route('info', ['msg' => 1]);
    }

    /**
     * getAudioLabel
     *
     * @param Request $request
     * @return string
     */
    private function getAudioLabel(Request $request): string
    {
        $lvInfoAudioAction = new LvInfoAudioAction();
        $audioType = $request->get('audio_type');
        $audioName = '';
        if ($audioType == SystemConst::RADIO_AUDIO_TYPE['CHIME']) {
            $audioData1 = $request->get('audio_data1');
            if (empty($audioData1) == false) {
                $audioName = $lvInfoAudioAction->getAudioName($audioData1);
            } else {
                $audioName = 'デフォルト音';
            }
        } elseif ($audioType == SystemConst::RADIO_AUDIO_TYPE['ETC']) {
            $audioData2 = $request->get('audio_data_etc');
            $audioName = $lvInfoAudioAction->getAudioName($audioData2);
        }
        return $audioName;
    }
}
Leave a Comment