You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
4.8 KiB
PHP

<?php
namespace App\Http\Controllers;
use Carbon\Carbon;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Http\JsonResponse as JsonResponseAlias;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
use App\Models\Setting;
class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
protected $breadcrumbs = [];
protected $page = [];
protected $title = '';
public function __construct()
{
$now = Carbon::now()->timestamp;
View::share('now', $now);
// 전역변수 설정
$_settings = Setting::all();
$settings = [];
if ($_settings) {
foreach ($_settings as $row) {
$settings[$row['key']] = $row['value'];
}
}
//logger($settings);
View::share('settings', (object)$settings);
}
protected function baseData() {
// breadcrumb 추가
return [
'breadcrumbs' => $this->breadcrumbs,
'page' => $this->page,
'title' => $this->title,
'pages' => config('pages')
];
}
protected function setView($name, $data = []) {
return view($name, $data, $this->baseData());
}
/**
* 유효성 검사
*
* @param Request $request
* @param array $rules
* @param array $message
* @return bool|string
*/
protected function validation(Request $request, array $rules = [], array $message = [])
{
$validator = Validator::make($request->all(), $rules, $message);
if ($validator->fails()) {
return $validator->errors()->first();
}
return true;
}
/**
* 처리결과
*
* @param string $message
* @param string $code
* @param array $appends
* @return array
*/
protected function sendResult(string $message = '', string $code = 'error', array $appends = [])
{
$result = [
'code' => $code,
'message' => $message
];
if (!empty($appends)) {
$result = array_merge($result, $appends);
}
return $result;
}
/**
* JSON Return
*
* @param string $message
* @param string $code
* @param mixed $data
* @param array $errors
* @param bool $success
* @return JsonResponseAlias
*/
protected function sendJson(string $message = '', string $code = 'success', $data = null, array $errors = [], bool $success = true)
{
$errorCode = $success ? 200 : 422;
$result = [
'code' => $code,
'message' => $message,
'success' => $success,
'data' => $data,
'errors' => $errors
];
try {
logger(json_decode(json_encode($result), true));
} catch (\JsonException $e) {
logger($e);
}
return response()->json($result, $errorCode, [], JSON_UNESCAPED_UNICODE);
}
/**
* JSON Return Aliasing
*
* @param string $message
* @param string $code
* @param bool $status
* @return JsonResponseAlias
*/
protected function sendJsonMessage(string $message = '', string $code = 'success', bool $status = true)
{
return $this->sendJson($message, $code, null, [], $status);
}
/**
* 유효성 검사
*
* @param Request $request
* @param array $rules
* @param array $message
* @return bool|JsonResponseAlias
*/
protected function validationJson(Request $request, array $rules = [], array $message = [])
{
$validator = Validator::make($request->all(), $rules, $message);
if ($validator->fails()) {
return $this->sendJson(
'입력하신 데이터가 유효하지 않습니다.',
'error',
null,
$validator->getMessageBag()->getMessages(),
false
);
}
return true;
}
/**
* 목록에서 순번 뽑아오기
*
* @param $list
* @return mixed
*/
protected function getListNumber($list) {
// 전체 갯수
$total = $list->total();
// 페이지당 갯수
$perPage = $list->perPage();
// 현재 페이지번호
$currentPage = $list->currentPage();
$count = 0;
$list->getCollection()->transform(function($list) use($total, $perPage, $currentPage, &$count) {
$list->number = ($total - ($perPage * ($currentPage - 1))) - $count;
$count++;
return $list;
});
return $list;
}
}