FormPost.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Models\Parameter;
  3. use App\Helpers\Utils;
  4. use App\Helpers\ParameterUtils;
  5. class FormPost
  6. {
  7. private $data;
  8. private $bpa;
  9. private $permissionNameList = ['is_create', 'is_update', 'is_delete', 'is_read', 'is_create'];
  10. public function __construct($bpa, $customParaName = null, $themeDir = null, $popupOptions = false)
  11. {
  12. if (empty($bpa)) {
  13. $this->initBpa();
  14. } else {
  15. $this->bpa = Utils::bpaDecoding($bpa);
  16. }
  17. if (isset($customParaName)) {
  18. $this->bpa['para_name'] = $customParaName;
  19. }
  20. if ($themeDir && $themeDir !== 'empty') {
  21. $this->data = Utils::getThemeParamFile($this->bpa['para_name'], '.json', $themeDir);
  22. } else {
  23. $this->data = Utils::getParamFile($this->bpa['para_name']);
  24. }
  25. $this->data['General'] = array_merge($this->data['General'], ['returnUrl' => $this->bpa['page_uri'].'?bpa='.$bpa]);
  26. $this->converterData();
  27. }
  28. public function initBpa()
  29. {
  30. // Todo: permission 전체 가능하게 변경하기
  31. $this->bpa['theme_dir'] = 'empty';
  32. $this->bpa['page_uri'] = '/';
  33. $this->bpa['permission'] = [
  34. 'is_mymenu' => '0',
  35. 'is_list' => '1',
  36. 'is_read' => '1',
  37. 'is_create' => '1',
  38. 'is_update' => '1',
  39. 'is_delete' => '1',
  40. 'is_newtab' => '0',
  41. ];
  42. }
  43. public function getData($customName = null)
  44. {
  45. if (isset($customName)) {
  46. return [$customName => $this->data];
  47. }
  48. return ['formA' => $this->data];
  49. }
  50. protected function resetDisplayVarsToDefaultValues()
  51. {
  52. if (empty($this->data['DisplayVars']['Chunk']))
  53. $this->data['DisplayVars']['Chunk'] = 999;
  54. }
  55. private function converterData()
  56. {
  57. $this->createPermission();
  58. $this->exceptionHandling();
  59. ParameterUtils::separateAlignAndFormat($this->data, 'FormPostVars');
  60. ParameterUtils::checkDisplayAndCount($this->data, 'FormPostVars');
  61. ParameterUtils::mappingKeys($this->data, 'FormPostVars');
  62. $this->checkSaveBtnPermission();
  63. $this->resetDisplayVarsToDefaultValues();
  64. }
  65. private function createPermission()
  66. {
  67. $this->data['SelectButtonOptions'] = collect($this->data['SelectButtonOptions'])->map(function ($selectBtnOption, $index) {
  68. return array_merge($selectBtnOption, ['Permission' => $this->permissionNameList[$index] ?? 'is_create']);
  69. })->toArray();
  70. }
  71. private function exceptionHandling()
  72. {
  73. $this->data['SelectButtonOptions'] = collect($this->data['SelectButtonOptions'])->filter(function ($selectBtnOption) {
  74. return $selectBtnOption['Value'] != NULL;
  75. })->filter(function ($selectBtnOption) {
  76. return $this->bpa['permission'][$selectBtnOption['Permission']] == 1;
  77. })->values()->toArray();
  78. }
  79. private function checkSaveBtnPermission()
  80. {
  81. if (isset($this->data['FormPostVars']['Title']['SaveButton']) && !$this->bpa['permission']['is_update']) {
  82. $this->data['FormPostVars']['Hidden']['SaveButton'] = 'hidden';
  83. }
  84. }
  85. }