FormA.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace App\Models\Parameter;
  3. use App\Helpers\Utils;
  4. use App\Helpers\ParameterUtils;
  5. class FormA
  6. {
  7. protected $data;
  8. protected $bpa;
  9. public function __construct($bpa, $customParaName = null, $themeDir = null, $popupOptions = false, $mode = 'erp')
  10. {
  11. if (empty($bpa)) {
  12. $this->initBpa();
  13. } else {
  14. $this->bpa = Utils::bpaDecoding($bpa);
  15. }
  16. if (isset($customParaName)) {
  17. $this->bpa['para_name'] = $customParaName;
  18. }
  19. $themeDir = $themeDir ?? $this->bpa['theme_dir'];
  20. if ($themeDir && $themeDir !== 'empty') {
  21. $this->data = Utils::getThemeParamFile($this->bpa['para_name'], '.json', $themeDir, $popupOptions);
  22. } else {
  23. $this->data = Utils::getParamFile($this->bpa['para_name'], '.json', $popupOptions, $mode);
  24. }
  25. $this->data['General'] = array_merge($this->data['General'], ['returnUrl' => $this->bpa['page_uri'].'?bpa='.$bpa, 'CustomVar' => $this->bpa['custom_var'] ?? '']);
  26. $this->converterData();
  27. }
  28. public function initBpa()
  29. {
  30. $this->bpa['theme_dir'] = 'empty';
  31. $this->bpa['page_uri'] = '/';
  32. $this->bpa['permission'] = session()->get('user.MenuPermission');
  33. if (empty($this->bpa['permission'])) {
  34. $this->bpa['permission'] = [
  35. 'is_mymenu' => '0',
  36. 'is_list' => '1',
  37. 'is_read' => '1',
  38. 'is_create' => '1',
  39. 'is_update' => '1',
  40. 'is_delete' => '1',
  41. 'is_newtab' => '0',
  42. ];
  43. }
  44. }
  45. public function getData($customName = null)
  46. {
  47. if (isset($customName)) {
  48. return [$customName => $this->data];
  49. }
  50. return ['formA' => $this->data];
  51. }
  52. protected function converterData()
  53. {
  54. $this->createPermission();
  55. $this->exceptionHandling();
  56. ParameterUtils::separateAlignAndFormat($this->data, 'FormVars');
  57. ParameterUtils::checkDisplayAndCount($this->data, 'FormVars');
  58. ParameterUtils::mappingKeys($this->data, 'FormVars');
  59. }
  60. protected function createPermission()
  61. {
  62. $this->data['SelectButtonOptions'] = collect($this->data['SelectButtonOptions'])->map(function ($selectBtnOption, $index) {
  63. switch ($selectBtnOption['Value']) {
  64. case 'new':
  65. $permission = 'is_create';
  66. break;
  67. case 'del':
  68. case 'multi-delete':
  69. $permission = 'is_delete';
  70. break;
  71. default:
  72. $permission = 'dummy';
  73. }
  74. return array_merge($selectBtnOption, ['Permission' => $permission]);
  75. })->toArray();
  76. }
  77. protected function exceptionHandling()
  78. {
  79. if ($this->bpa['permission']['is_update'] === '0') {
  80. $this->data['FormVars'][0]['SaveButton'] = '';
  81. }
  82. $this->data['SelectButtonOptions'] = collect($this->data['SelectButtonOptions'])->filter(function ($selectBtnOption) {
  83. return $selectBtnOption['Value'] != NULL;
  84. })->filter(function ($selectBtnOption) {
  85. if ($selectBtnOption['Permission'] === 'dummy') {
  86. return true;
  87. }
  88. return $this->bpa['permission'][$selectBtnOption['Permission']] === '1';
  89. })->values()->toArray();
  90. }
  91. }