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

129 lines
2.9KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Definition;
  3. abstract class AbstractSettingDefinition
  4. {
  5. protected $settings = [];
  6. public function addSettingText(array $params): self
  7. {
  8. $params['type'] = 'text';
  9. $params['field'] = 'text';
  10. return $this->addSetting($params);
  11. }
  12. public function addSettingTextarea(array $params): self
  13. {
  14. $params['type'] = 'textarea';
  15. $params['field'] = 'text';
  16. return $this->addSetting($params);
  17. }
  18. public function addSettingTextareaAdvanced(array $params): self
  19. {
  20. $params['type'] = 'textarea_advanced';
  21. $params['field'] = 'text';
  22. return $this->addSetting($params);
  23. }
  24. public function addSettingDate(array $params): self
  25. {
  26. $params['type'] = 'date';
  27. $params['field'] = 'date';
  28. return $this->addSetting($params);
  29. }
  30. public function addSettingTime(array $params): self
  31. {
  32. $params['type'] = 'time';
  33. $params['field'] = 'date';
  34. return $this->addSetting($params);
  35. }
  36. public function addSettingFile(array $params): self
  37. {
  38. $params['type'] = 'file';
  39. $params['field'] = 'file';
  40. return $this->addSetting($params);
  41. }
  42. public function addSettingImage(array $params): self
  43. {
  44. $params['type'] = 'image';
  45. $params['field'] = 'file';
  46. return $this->addSetting($params);
  47. }
  48. public function addSettingSelect(array $params): self
  49. {
  50. $params['type'] = 'select';
  51. $params['field'] = 'text';
  52. return $this->addSetting($params);
  53. }
  54. public function addSettingRadio(array $params): self
  55. {
  56. $params['type'] = 'radio';
  57. $params['field'] = 'text';
  58. return $this->addSetting($params);
  59. }
  60. public function addSetting($params)
  61. {
  62. $name = $params['name'];
  63. $category = $params['category'];
  64. if (!isset($this->settings[$category])) {
  65. $this->settings[$category] = [];
  66. }
  67. $this->settings[$category][$name] = $params;
  68. return $this;
  69. }
  70. public function getSettings(): array
  71. {
  72. return $this->settings;
  73. }
  74. public function getSettingsByCategory($category)
  75. {
  76. $settings = $this->getSettings();
  77. if (isset($settings[$category])) {
  78. return $settings[$category];
  79. }
  80. return [];
  81. }
  82. public function getSettingByName($name): ?array
  83. {
  84. $settings = $this->getSettings();
  85. foreach ($settings as $category => $settingsCategory) {
  86. foreach ($settingsCategory as $nameSetting => $setting) {
  87. if ($nameSetting == $name) {
  88. return $setting;
  89. }
  90. }
  91. }
  92. return null;
  93. }
  94. public function getSettingType($name): ?string
  95. {
  96. $setting = $this->getSettingByName($name);
  97. if ($setting) {
  98. return $setting['type'];
  99. }
  100. return null;
  101. }
  102. }