688 lines
29 KiB
PHP
688 lines
29 KiB
PHP
<?php
|
|
|
|
namespace App\Controllers;
|
|
|
|
use CodeIgniter\Controller;
|
|
use CodeIgniter\HTTP\CLIRequest;
|
|
use CodeIgniter\HTTP\IncomingRequest;
|
|
use CodeIgniter\HTTP\RequestInterface;
|
|
use CodeIgniter\HTTP\ResponseInterface;
|
|
use CodeIgniter\HTTP\Files\UploadedFile;
|
|
use Psr\Log\LoggerInterface;
|
|
use App\Libraries\Log\Log;
|
|
use App\Models\UserModel;
|
|
|
|
/**
|
|
* Class BaseController
|
|
*
|
|
* BaseController provides a convenient place for loading components
|
|
* and performing functions that are needed by all your controllers.
|
|
* Extend this class in any new controllers:
|
|
* class Home extends BaseController
|
|
*
|
|
* For security be sure to declare any new methods as protected or private.
|
|
*/
|
|
abstract class BaseController extends Controller
|
|
{
|
|
/**
|
|
* Instance of the main Request object.
|
|
*
|
|
* @var CLIRequest|IncomingRequest
|
|
*/
|
|
protected $request;
|
|
|
|
/**
|
|
* An array of helpers to be loaded automatically upon
|
|
* class instantiation. These helpers will be available
|
|
* to all other controllers that extend BaseController.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $helpers = ['Common'];
|
|
|
|
/**
|
|
* Be sure to declare properties for any property fetch you initialized.
|
|
* The creation of dynamic property is deprecated in PHP 8.2.
|
|
*/
|
|
// protected $session;
|
|
|
|
/**
|
|
* Constructor.
|
|
*/
|
|
private $_userModel = null;
|
|
private $_user_uids = array();
|
|
protected $_model = null;
|
|
protected $_className = '';
|
|
protected $_defines = array();
|
|
protected $_viewPath = '';
|
|
protected $_viewDatas = array();
|
|
protected $_session = null;
|
|
public function initController(RequestInterface $request, ResponseInterface $response, LoggerInterface $logger)
|
|
{
|
|
// Do Not Edit This Line
|
|
parent::initController($request, $response, $logger);
|
|
|
|
// Preload any models, libraries, etc, here.
|
|
|
|
// E.g.: $this->session = \Config\Services::session();
|
|
$this->_session = \Config\Services::session();
|
|
$this->_viewDatas = [
|
|
'layout' => LAYOUTS['empty'],
|
|
'title' => ''
|
|
];
|
|
|
|
//로그인정보처리
|
|
$this->_viewDatas[SESSION_NAMES['ISLOGIN']] = $this->_session->get(SESSION_NAMES['ISLOGIN']);
|
|
if ($this->_viewDatas[SESSION_NAMES['ISLOGIN']]) {
|
|
$this->_viewDatas[SESSION_NAMES['AUTH']] = $this->_session->get(SESSION_NAMES['AUTH']);
|
|
}
|
|
}
|
|
|
|
//User모델
|
|
final protected function getUserModel(): UserModel
|
|
{
|
|
return is_null($this->_userModel) ? new UserModel() : $this->_userModel;
|
|
}
|
|
|
|
//Field별 Form Option용
|
|
protected function getFieldFormOption(string $field): array
|
|
{
|
|
switch ($field) {
|
|
case 'user_uid':
|
|
$options = $this->_user_uids = $this->_user_uids ?: $this->getUserModel()->getFieldFormOptions(['status' => 'use']);
|
|
break;
|
|
default:
|
|
$options = lang($this->_className . '.' . strtoupper($field));
|
|
break;
|
|
}
|
|
if (!is_array($options)) {
|
|
throw new \Exception(__FUNCTION__ . "에서 {$this->_className}의 Field:{$field}의 FormOptionData가 array가 아닙니다.\n" . var_export($options, true));
|
|
}
|
|
return $options;
|
|
}
|
|
//Field별 Form Option용
|
|
final protected function getFieldFormOptions(array $fields): array
|
|
{
|
|
$fieldFormOptions = array();
|
|
foreach ($fields as $field) {
|
|
if (!is_string($field)) {
|
|
throw new \Exception(__FUNCTION__ . "에서 {$this->_className}의 Field:{$field}가 string 아닙니다.\n" . var_export($fields, true));
|
|
}
|
|
$fieldFormOptions[$field] = $this->getFieldFormOption($field);
|
|
}
|
|
return $fieldFormOptions;
|
|
}
|
|
//Field별 Form Rule용
|
|
// final protected function getFieldRules(array $fields, array $fieldRules): array
|
|
// {
|
|
// $tempRules = $this->_model->getValidationRules(['only' => $fields]);
|
|
// foreach ($fields as $field) {
|
|
// if (!is_string($field)) {
|
|
// throw new \Exception(__FUNCTION__ . "에서 {$this->_className}의 Field:{$field}가 string 아닙니다.\n" . var_export($fieldRules, true));
|
|
// }
|
|
// if (array_key_exists($field, $fieldRules)) {
|
|
// $tempRules[$field] = $fieldRules[$field];
|
|
// }
|
|
// }
|
|
// return $tempRules;
|
|
// }
|
|
|
|
//Field별 Form Datas 처리용
|
|
protected function getFieldFormData(string $field, $entity = null): array
|
|
{
|
|
switch ($field) {
|
|
default:
|
|
$this->_viewDatas['fieldDatas'][$field] = $this->request->getVar($field);
|
|
break;
|
|
}
|
|
return $this->_viewDatas['fieldDatas'];
|
|
}
|
|
|
|
//Upload FIle관련
|
|
protected function upload_file_process(UploadedFile $upfile)
|
|
{
|
|
$fileName = "";
|
|
if ($upfile->isValid() && !$upfile->hasMoved()) {
|
|
$fileName = $upfile->getRandomName();
|
|
$upfile->move(PATHS['UPLOAD'], $fileName);
|
|
//move시 중복된파일명이 있다면 파일명이 바뀌므로 여기서 한번더 파일명 확인 필요
|
|
$fileName = $upfile->getRandomName();
|
|
}
|
|
return $fileName;
|
|
}
|
|
protected function single_upload_procedure(string $field, $entity = null)
|
|
{
|
|
$upfile = $this->request->getFile($field);
|
|
$fileName = $this->upload_file_process($upfile);
|
|
// $fileDatas=array();
|
|
// if ($upfile->isValid() && !$upfile->hasMoved()) {
|
|
// $filepath = PATHS['UPLOAD'] . $upfile->store();
|
|
// $fileDatas = [
|
|
// 'uploaded_fileinfo' => new \CodeIgniter\Files\File($filepath)
|
|
// ];
|
|
// return $fileDatas;
|
|
// }
|
|
return $fileName;
|
|
}
|
|
protected function multiple_upload_procedure(string $field, $entity = null): array
|
|
{
|
|
//Multiple파일의경우 html에서는 필드명[]를 넣어야하며
|
|
//rule에서 "uploaded[필드명.0]|is_image[필드명]~~" 이런식으로 넣어야함
|
|
$fileNames = array();
|
|
if ($upfiles = $this->request->getFiles()) {
|
|
foreach ($upfiles[$field] as $upfile) {
|
|
if ($upfile->isValid() && !$upfile->hasMoved()) {
|
|
$fileName = $this->upload_file_process($upfile);
|
|
array_push(
|
|
$this->_viewDatas['fieldDatas'][$field],
|
|
$fileName
|
|
);
|
|
}
|
|
}
|
|
}
|
|
return $fileNames;
|
|
}
|
|
|
|
//Insert관련
|
|
protected function insert_init()
|
|
{
|
|
$this->_viewDatas['fields'] = $this->_defines['insert']['fields'];;
|
|
$this->_viewDatas['fieldRules'] = $this->_defines['insert']['fieldRules'];
|
|
}
|
|
protected function insert_form_init()
|
|
{
|
|
$this->_viewDatas['fieldFilters'] = $this->_defines['insert']['fieldFilters'];
|
|
$this->_viewDatas['fieldFormOptions'] = $this->getFieldFormOptions($this->_viewDatas['fieldFilters']);
|
|
$this->_viewDatas['forms'] = ['attributes' => ['method' => "post",], 'hiddens' => []];
|
|
helper(['form']);
|
|
}
|
|
protected function insert_form_process()
|
|
{
|
|
}
|
|
final public function insert_form()
|
|
{
|
|
try {
|
|
$this->insert_init();
|
|
$this->insert_form_init();
|
|
$this->insert_form_process();
|
|
return view($this->_viewPath . '/insert', $this->_viewDatas);
|
|
} catch (\Exception $e) {
|
|
return alert_CommonHelper($e->getMessage(), 'back');
|
|
}
|
|
}
|
|
|
|
protected function insert_validate()
|
|
{
|
|
//변경된 값 적용
|
|
$this->_viewDatas['fieldDatas'] = array();
|
|
foreach ($this->_viewDatas['fields'] as $field) {
|
|
$this->_viewDatas['fieldDatas'] = $this->getFieldFormData($field);
|
|
Log::add("info", "{$field} : " . var_export($this->_viewDatas['fieldDatas'][$field], true));
|
|
}
|
|
|
|
// echo var_export($this->_viewDatas['fields'], true);
|
|
// echo "<HR>";
|
|
// echo var_export($this->_viewDatas['fieldDatas'], true);
|
|
// echo "<HR>";
|
|
// echo var_export($this->_viewDatas['fieldRules'], true);
|
|
// exit;
|
|
//변경할 값 확인
|
|
if (!$this->validate($this->_viewDatas['fieldRules'])) {
|
|
throw new \Exception("{$this->_viewDatas['title']}의 검증 오류발생\n" . implode("\n", $this->validator->getErrors()));
|
|
}
|
|
}
|
|
protected function insert_process()
|
|
{
|
|
return $this->_model->create($this->_viewDatas['fieldDatas']);
|
|
}
|
|
protected function insert_procedure()
|
|
{
|
|
$message = "";
|
|
try {
|
|
$this->insert_init();
|
|
$this->insert_validate();
|
|
$this->insert_process();
|
|
$message = __FUNCTION__ . " 완료하였습니다.";
|
|
Log::save("{$this->_viewDatas['title']} {$message}");
|
|
//session->setTempdata의 기능은 3초간 success에 message를 보관후 사라짐
|
|
//$this->_session->setTempdata('success', 'Page updated successfully', 3);
|
|
return alert_CommonHelper($message, $this->_session->get(SESSION_NAMES['RETURN_URL']));
|
|
} catch (\Exception $e) {
|
|
$message = __FUNCTION__ . " 실패하였습니다.";
|
|
Log::add("warning", $e->getMessage());
|
|
Log::add("warning", var_export($this->_viewDatas['fieldDatas'], true));
|
|
Log::save("{$this->_viewDatas['title']} {$message}", 'error');
|
|
return redirect()->back()->withInput()->with("error", $message . "<br>\n{$e->getMessage()}");
|
|
}
|
|
}
|
|
|
|
//Update관련
|
|
protected function update_init()
|
|
{
|
|
$this->_viewDatas['fields'] = $this->_defines['update']['fields'];;
|
|
$this->_viewDatas['fieldRules'] = $this->_defines['update']['fieldRules'];
|
|
}
|
|
protected function update_form_init()
|
|
{
|
|
$this->_viewDatas['fieldFilters'] = $this->_defines['update']['fieldFilters'];
|
|
$this->_viewDatas['fieldFormOptions'] = $this->getFieldFormOptions($this->_viewDatas['fieldFilters']);
|
|
$this->_viewDatas['forms'] = ['attributes' => ['method' => "post",], 'hiddens' => []];
|
|
helper(['form']);
|
|
}
|
|
protected function update_form_process($entity)
|
|
{
|
|
return $entity;
|
|
}
|
|
final public function update_form($uid)
|
|
{
|
|
try {
|
|
$entity = $entity = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]);
|
|
$this->update_init();
|
|
$this->update_form_init();
|
|
$this->_viewDatas['entity'] = $this->update_form_process($entity);
|
|
return view($this->_viewPath . '/update', $this->_viewDatas);
|
|
} catch (\Exception $e) {
|
|
return alert_CommonHelper($e->getMessage(), 'back');
|
|
}
|
|
}
|
|
protected function update_validate($entity)
|
|
{
|
|
//변경된 값 적용
|
|
$this->_viewDatas['fieldDatas'] = array();
|
|
foreach ($this->_viewDatas['fields'] as $field) {
|
|
$this->_viewDatas['fieldDatas'] = $this->getFieldFormData($field, $entity);
|
|
Log::add(
|
|
"info",
|
|
"{$field} : {$entity->$field} => " . var_export($this->_viewDatas['fieldDatas'][$field])
|
|
);
|
|
}
|
|
//변경할 값 확인
|
|
if (!$this->validate($this->_viewDatas['fieldRules'])) {
|
|
throw new \Exception("{$this->_viewDatas['title']}의 검증 오류발생\n" . implode("\n", $this->validator->getErrors()));
|
|
}
|
|
return $entity;
|
|
}
|
|
protected function update_process($entity)
|
|
{
|
|
return $this->_model->modify($entity, $this->_viewDatas['fieldDatas']);
|
|
}
|
|
protected function update_procedure($uid)
|
|
{
|
|
$message = "";
|
|
try {
|
|
$entity = $entity = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]);
|
|
$this->update_init();
|
|
$entity = $this->update_validate($entity);
|
|
$entity = $this->update_process($entity);
|
|
$message = "{$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다.";
|
|
Log::save("{$this->_viewDatas['title']} {$message}");
|
|
return alert_CommonHelper($message, $this->_session->get(SESSION_NAMES['RETURN_URL']));
|
|
} catch (\Exception $e) {
|
|
$message = __FUNCTION__ . " 실패하였습니다.";
|
|
Log::add("warning", $e->getMessage());
|
|
Log::add("warning", var_export($this->_viewDatas['fieldDatas'], true));
|
|
Log::save("{$this->_viewDatas['title']} {$message}", 'error');
|
|
return redirect()->back()->withInput()->with("error", $message . "<br>\n{$e->getMessage()}");
|
|
}
|
|
}
|
|
|
|
//Reply관련
|
|
protected function reply_init()
|
|
{
|
|
return $this->update_init();
|
|
}
|
|
protected function reply_form_init()
|
|
{
|
|
return $this->update_form_init();
|
|
}
|
|
protected function reply_form_process($entity)
|
|
{
|
|
return $this->update_form_process($entity);
|
|
}
|
|
final public function reply_form($uid)
|
|
{
|
|
try {
|
|
$entity = $entity = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]);
|
|
$this->reply_init();
|
|
$this->reply_form_init();
|
|
$this->_viewDatas['entity'] = $this->reply_form_process($entity);
|
|
return view($this->_viewPath . '/reply', $this->_viewDatas);
|
|
} catch (\Exception $e) {
|
|
return alert_CommonHelper($e->getMessage(), 'back');
|
|
}
|
|
}
|
|
protected function reply_validate($entity)
|
|
{
|
|
return $this->update_validate($entity);
|
|
}
|
|
protected function reply_process($entity)
|
|
{
|
|
return $this->_model->reply($entity, $this->_viewDatas['fieldDatas']);
|
|
}
|
|
protected function reply_procedure($uid)
|
|
{
|
|
$message = "";
|
|
try {
|
|
$entity = $entity = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]);
|
|
$this->reply_init();
|
|
$entity = $this->reply_validate($entity);
|
|
$entity = $this->reply_process($entity);
|
|
$message = "{$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다.";
|
|
Log::save("{$this->_viewDatas['title']} {$message}");
|
|
return alert_CommonHelper($message, $this->_session->get(SESSION_NAMES['RETURN_URL']));
|
|
} catch (\Exception $e) {
|
|
$message = __FUNCTION__ . " 실패하였습니다.";
|
|
Log::add("warning", $e->getMessage());
|
|
Log::add("warning", var_export($this->_viewDatas['fieldDatas'], true));
|
|
Log::save("{$this->_viewDatas['title']} {$message}", 'error');
|
|
return redirect()->back()->withInput()->with("error", $message . "<br>\n{$e->getMessage()}");
|
|
}
|
|
}
|
|
|
|
//Toggle 관련
|
|
protected function toggle_init($field)
|
|
{
|
|
$this->_viewDatas['fields'] = [$field];
|
|
$this->_viewDatas['fieldRules'] = $this->_defines['update']['fieldRules'];
|
|
}
|
|
protected function toggle_validate($entity)
|
|
{
|
|
return $this->update_validate($entity);
|
|
}
|
|
protected function toggle_process($entity)
|
|
{
|
|
return $this->update_process($entity);
|
|
}
|
|
protected function toggle_procedure($uid, string $field)
|
|
{
|
|
$message = "";
|
|
try {
|
|
$entity = $entity = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]);
|
|
$this->toggle_init($field);
|
|
$entity = $this->toggle_validate($entity);
|
|
$entity = $this->toggle_process($entity);
|
|
$message = "{$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다.";
|
|
Log::save("{$this->_viewDatas['title']} {$message}");
|
|
return alert_CommonHelper($message, $this->_session->get(SESSION_NAMES['RETURN_URL']));
|
|
} catch (\Exception $e) {
|
|
$message = __FUNCTION__ . " 실패하였습니다.";
|
|
Log::add("warning", $e->getMessage());
|
|
Log::add("warning", var_export($this->_viewDatas['fieldDatas'], true));
|
|
Log::save("{$this->_viewDatas['title']} {$message}", 'error');
|
|
return alert_CommonHelper($message . "<br>\n{$e->getMessage()}", 'back');
|
|
}
|
|
}
|
|
//Batchjob 관련
|
|
protected function batchjob_init()
|
|
{
|
|
//fields 해당하는 field중 선택된 값이 있는경우만 fields로 정의
|
|
$fields = array();
|
|
foreach ($this->_defines['index']['batchjobFilters'] as $field) {
|
|
if ($this->request->getVar($field)) {
|
|
array_push($fields, $field);
|
|
}
|
|
}
|
|
if (!is_array($fields) || count($fields) === 0) {
|
|
throw new \Exception($this->_viewDatas['title'] . '에서 변경할 항목(field)이 선택되지 않았습니다.');
|
|
}
|
|
//Fields,FielRules재정의
|
|
$this->_viewDatas['fields'] = $fields;
|
|
$this->_viewDatas['fieldRules'] = $this->_defines['update']['fieldRules'];
|
|
}
|
|
protected function batchjob_validate($entity)
|
|
{
|
|
return $this->update_validate($entity);
|
|
}
|
|
protected function batchjob_process($entity)
|
|
{
|
|
return $this->update_process($entity);
|
|
}
|
|
protected function batchjob_procedure()
|
|
{
|
|
$uids = $this->request->getVar('batchjob_uids');
|
|
if (is_null($uids) || !is_array($uids) || !count($uids)) {
|
|
return alert_CommonHelper($this->_viewDatas['title'] . '에서 변경할 항목(uid)이 선택되지 않았습니다.', 'back');
|
|
}
|
|
$message = "";
|
|
try {
|
|
$this->batchjob_init();
|
|
$entitys = array();
|
|
foreach ($uids as $uid) {
|
|
$entity = $entity = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]);
|
|
try {
|
|
$entity = $this->batchjob_validate($entity);
|
|
array_push($entitys, $this->batchjob_process($entity));
|
|
} catch (\Exception $e) {
|
|
Log::add("warning", "{$entity->getTitle()}는 다음과 같은 이유로 수정되지 않았습니다.\n<br>" . $e->getMessage());
|
|
}
|
|
}
|
|
$message = "총: " . count($entitys) . "개의 수정(Batchjob)을 완료하였습니다.";
|
|
Log::save("{$this->_viewDatas['title']} {$message}");
|
|
return alert_CommonHelper($message, $this->_session->get(SESSION_NAMES['RETURN_URL']));
|
|
} catch (\Exception $e) {
|
|
$message = "총: " . count($uids) . "개의 수정(Batchjob)을 실패하였습니다.";
|
|
Log::add("warning", $e->getMessage());
|
|
Log::add("warning", var_export($this->_viewDatas['fieldDatas'], true));
|
|
Log::save("{$this->_viewDatas['title']} {$message}", 'error');
|
|
return alert_CommonHelper($message . "<br>\n{$e->getMessage()}", 'back');
|
|
}
|
|
}
|
|
|
|
//Delete 관련
|
|
protected function delete_process($entity)
|
|
{
|
|
if (!$this->_model->delete($entity->getPrimaryKey())) {
|
|
Log::add("error", __FUNCTION__ . "에서 호출:" . $this->_model->getLastQuery());
|
|
Log::add("error", implode("\n", $this->_model->errors()));
|
|
throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . var_export($this->_model->errors(), true));
|
|
}
|
|
return $entity;
|
|
}
|
|
protected function delete_procedure($uid)
|
|
{
|
|
$message = "";
|
|
try {
|
|
$entity = $entity = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]);
|
|
$this->delete_process($entity);
|
|
$message = "{$entity->getTitle()} " . __FUNCTION__ . " 완료하였습니다.";
|
|
Log::save("{$this->_viewDatas['title']} {$message}");
|
|
return alert_CommonHelper($message, $this->_session->get(SESSION_NAMES['RETURN_URL']));
|
|
} catch (\Exception $e) {
|
|
$message = __FUNCTION__ . " 실패하였습니다.";
|
|
Log::add("warning", $e->getMessage());
|
|
Log::save("{$this->_viewDatas['title']} {$message}", 'error');
|
|
return alert_CommonHelper($message . "<br>\n{$e->getMessage()}", 'back');
|
|
}
|
|
}
|
|
|
|
//View 관련
|
|
protected function view_init()
|
|
{
|
|
$this->_viewDatas['fields'] = $this->_defines['view']['fields'];
|
|
$this->_viewDatas['fieldFilters'] = $this->_defines['view']['fieldFilters'];
|
|
$this->_viewDatas['fieldRules'] = $this->_defines['view']['fieldRules'];
|
|
helper(['form']);
|
|
$this->_viewDatas['fieldFormOptions'] = $this->getFieldFormOptions($this->_viewDatas['fieldFilters']);
|
|
$this->_viewDatas['forms'] = ['attributes' => ['method' => "post",], 'hiddens' => []];
|
|
}
|
|
protected function view_process($entity)
|
|
{
|
|
return $entity;
|
|
}
|
|
protected function view_procedure($uid)
|
|
{
|
|
try {
|
|
$entity = $this->_model->getEntity([$this->_model->getPrimaryKey() => $uid]);
|
|
// dd($entity);
|
|
$this->view_init();
|
|
$this->_viewDatas['entity'] = $this->view_process($entity);
|
|
return view($this->_viewPath . '/view', $this->_viewDatas);
|
|
} catch (\Exception $e) {
|
|
return alert_CommonHelper($e->getMessage(), 'back');
|
|
}
|
|
}
|
|
|
|
//Index 관련
|
|
protected function index_init()
|
|
{
|
|
$this->_viewDatas['fields'] = $this->_defines['index']['fields'];
|
|
$this->_viewDatas['fieldFilters'] = $this->_defines['index']['fieldFilters'];
|
|
$this->_viewDatas['batchjobFilters'] = $this->_defines['index']['batchjobFilters'];
|
|
helper(['form']);
|
|
$this->_viewDatas['fieldFormOptions'] = $this->getFieldFormOptions($this->_viewDatas['fieldFilters']);
|
|
$this->_viewDatas['forms'] = ['attributes' => ['method' => "post",], 'hiddens' => []];
|
|
$this->_session->set(SESSION_NAMES['RETURN_URL'], current_url() . '?' . $this->request->getUri()->getQuery());
|
|
foreach ($this->_viewDatas['fieldFilters'] as $field) {
|
|
$this->_viewDatas[$field] = $this->request->getVar($field) ? $this->request->getVar($field) : DEFAULTS['EMPTY'];
|
|
}
|
|
$this->_viewDatas['word'] = $this->request->getVar('word') ? $this->request->getVar('word') : '';
|
|
$this->_viewDatas['start'] = $this->request->getVar('start') ? $this->request->getVar('start') : '';
|
|
$this->_viewDatas['end'] = $this->request->getVar('end') ? $this->request->getVar('end') : '';
|
|
$this->_viewDatas['order_field'] = $this->request->getVar('order_field') ? $this->request->getVar('order_field') : 'uid';
|
|
$this->_viewDatas['order_value'] = $this->request->getVar('order_value') ? $this->request->getVar('order_value') : 'DESC';
|
|
$this->_viewDatas['page'] = $this->request->getVar('page') ? $this->request->getVar('page') : 1;
|
|
$this->_viewDatas['per_page'] = $this->request->getVar('per_page') ? $this->request->getVar('per_page') : DEFAULTS['PERPAGE'];
|
|
$this->_viewDatas['uri'] = $this->request->getUri();
|
|
}
|
|
//index 모델 전처리
|
|
protected function index_setCondition()
|
|
{
|
|
foreach ($this->_viewDatas['fieldFilters'] as $field) {
|
|
$value = $this->request->getVar($field) ? $this->request->getVar($field) : false;
|
|
if ($value) {
|
|
$this->_model->where($field, $value);
|
|
}
|
|
}
|
|
$word = $this->request->getVar('word') ? $this->request->getVar('word') : '';
|
|
if (isset($word) && $word !== '') {
|
|
$this->_model->setIndexWordFilter($word);
|
|
}
|
|
$start = $this->request->getVar('start') ? $this->request->getVar('start') : '';
|
|
$end = $this->request->getVar('end') ? $this->request->getVar('end') : '';
|
|
if (isset($start) && $start !== '' && isset($end) && $end !== '') {
|
|
$this->_model->setIndexDateFilter($start, $end);
|
|
}
|
|
}
|
|
protected function index_getRows(int $page = 0, int $per_page = 0): array
|
|
{
|
|
//Totalcount 처리
|
|
$this->index_setCondition();
|
|
$this->_viewDatas['total_count'] = $this->_model->countAllResults();
|
|
//Rows 처리
|
|
$this->index_setCondition();
|
|
//OrderBy
|
|
$order_field = $this->request->getVar('order_field') ? $this->request->getVar('order_field') : 'uid';
|
|
$order_value = $this->request->getVar('order_value') ? $this->request->getVar('order_value') : 'DESC';
|
|
$this->_model->setIndexOrderBy($order_field, $order_value);
|
|
//Limit
|
|
$rows = $per_page ? $this->_model->findAll($per_page, $page * $per_page - $per_page) : $this->_model->findAll();
|
|
// log_message("debug", __METHOD__ . "에서 호출[{$per_page}:{$page}=>{$page}*{$per_page}-{$per_page}]\n" . $this->_model->getLastQuery());
|
|
return $rows;
|
|
}
|
|
private function index_getPagination($pager_group = 'default', int $segment = 0, $template = 'bootstrap_full'): string
|
|
{
|
|
// 1.Views/Pagers/에 bootstrap_full.php,bootstrap_simple.php 생성
|
|
// 2.app/Config/Pager.php/$templates에 'bootstrap_full => 'Pagers\bootstrap_full',
|
|
// 'bootstrap_simple' => 'Pagers\bootstrap_simple', 추가
|
|
$pager = \Config\Services::pager();
|
|
// $this->_model->paginate($this->_viewDatas['per_page'], $pager_group, $this->_viewDatas['page'], $segment);
|
|
$pager->makeLinks(
|
|
$this->_viewDatas['page'],
|
|
$this->_viewDatas['per_page'],
|
|
$this->_viewDatas['total_count'],
|
|
$template,
|
|
$segment,
|
|
$pager_group
|
|
);
|
|
$this->_viewDatas['page'] = $pager->getCurrentPage($pager_group);
|
|
$this->_viewDatas['total_page'] = $pager->getPageCount($pager_group);
|
|
return $pager->links($pager_group, $template);
|
|
}
|
|
protected function index_process()
|
|
{
|
|
//모델 처리
|
|
$this->_viewDatas['rows'] = $this->index_getRows((int)$this->_viewDatas['page'], (int)$this->_viewDatas['per_page']);
|
|
//줄수 처리용
|
|
$this->_viewDatas['pageOptions'] = array("" => "줄수선택");
|
|
for ($i = 10; $i <= $this->_viewDatas['total_count'] + $this->_viewDatas['per_page']; $i += 10) {
|
|
$this->_viewDatas['pageOptions'][$i] = $i;
|
|
}
|
|
//pagenation 처리
|
|
$this->_viewDatas['pagination'] = $this->index_getPagination();
|
|
}
|
|
protected function index_procedure()
|
|
{
|
|
try {
|
|
$this->index_init();
|
|
$this->index_process();
|
|
return view($this->_viewPath . '/index', $this->_viewDatas);
|
|
} catch (\Exception $e) {
|
|
return alert_CommonHelper($e->getMessage(), 'back');
|
|
}
|
|
}
|
|
|
|
//Excel 관련
|
|
protected function excel_init()
|
|
{
|
|
$this->_viewDatas['fields'] = $this->_defines['excel']['fields'];
|
|
$this->_viewDatas['fieldFilters'] = $this->_defines['excel']['fieldFilters'];
|
|
$this->_viewDatas['fieldFormOptions'] = $this->getFieldFormOptions($this->_viewDatas['fieldFilters']);
|
|
}
|
|
protected function excel_spreadSheet()
|
|
{
|
|
//Excepl 초기화
|
|
$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
|
|
$sheet = $spreadsheet->getActiveSheet();
|
|
//Header용
|
|
$column = 'A';
|
|
foreach ($this->_viewDatas['fields'] as $field) {
|
|
$sheet->setCellValue($column++ . '1', lang($this->_className . '.label.' . $field));
|
|
}
|
|
//본문용
|
|
$line = 2;
|
|
foreach ($this->index_getRows() as $row) {
|
|
$column = 'A';
|
|
foreach ($this->_viewDatas['fields'] as $field) {
|
|
// echo "\n<BR>".var_export($this->_fieldFilters,true)."\n<BR>".var_export($fieldFormOptions,true);exit;
|
|
$value = in_array($field, $this->_viewDatas['fieldFilters']) ? $this->_viewDatas['fieldFormOptions'][$field][$row[$field]] : $row[$field];
|
|
$sheet->setCellValue($column . $line, $value);
|
|
$column++;
|
|
}
|
|
$line++;
|
|
}
|
|
return $spreadsheet;
|
|
}
|
|
protected function excel_process()
|
|
{
|
|
$fileName = date('Y-m-d_Hm') . '.xlsx';
|
|
$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($this->excel_spreadSheet(), 'Xlsx');
|
|
//결과파일저장
|
|
$writer->save(PATHS['EXCEL'] . '/' . $fileName);
|
|
//Download시
|
|
header("Content-Type: application/vnd.ms-excel");
|
|
header(sprintf("Content-Disposition: attachment; filename=%s", urlencode($fileName)));
|
|
header("Expires: 0");
|
|
header("Cache-Control: must-revalidate");
|
|
header("Pragma: public");
|
|
header("Content-Length:" . filesize(PATHS['EXCEL'] . '/' . $fileName));
|
|
// flush();
|
|
// return readfile(PATHS['EXCEL'] . '/' . $fileName);
|
|
return $writer->save('php://output');
|
|
}
|
|
protected function excel_procedure()
|
|
{
|
|
try {
|
|
$this->excel_init();
|
|
return $this->excel_process();
|
|
} catch (\Exception $e) {
|
|
return alert_CommonHelper($e->getMessage(), 'back');
|
|
}
|
|
}
|
|
|
|
//File Download관련
|
|
protected function download_process($path, string $real_filename, string $download_filename = 'download')
|
|
{
|
|
return $this->response->download($path . "/" . $real_filename, null)->setFileName($download_filename);
|
|
}
|
|
}
|