PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.3.4
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.3.4
2.4.0 2.4.1 2.3.8 2.3.7 2.3.6 2.3.5 2.3.4 2.3.3 2.3.2 2.3.1 2.2.0 2.1.21 2.1.20 2.1.19 2.1.18 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.2 All 57 releases
easy-invoice / includes / Forms / BaseFieldRegistration.php

BaseFieldRegistration.php in Easy Invoice – Invoice Generator, PDF Quotes & Payments 2.3.4, at includes/Forms/BaseFieldRegistration.php

308 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Base Field Registration Class
4 *
5 * @package EasyInvoice
6 * @author Your Name
7 * @copyright Copyright (c) 2023, Your Company
8 * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
9 * @since 1.0.0
10 */
11
12 namespace EasyInvoice\Forms;
13
14 /**
15 * Base Field Registration
16 *
17 * Provides common functionality for field registration.
18 *
19 * @since 1.0.0
20 */
21 abstract class BaseFieldRegistration {
22
23 /**
24 * Registered tabs
25 *
26 * @var array
27 */
28 protected $tabs = [];
29
30 /**
31 * Registered fields
32 *
33 * @var array
34 */
35 protected $fields = [];
36
37 /**
38 * Registered templates
39 *
40 * @var array
41 */
42 protected $templates = [];
43
44 /**
45 * Registered item fields
46 *
47 * @var array
48 */
49 protected $item_fields = [];
50
51 /**
52 * Register a new tab
53 *
54 * @since 1.0.0
55 * @param string $id Tab ID
56 * @param array $config Tab configuration
57 * @return void
58 */
59 public function registerTab(string $id, array $config): void {
60 // Apply filter to allow pro plugins to modify individual tab config
61 $config = apply_filters($this->getTabFilterName(), $config, $id);
62
63 // Skip registration if tab was filtered out (set to null/false)
64 if (empty($config)) {
65 return;
66 }
67
68 $this->tabs[$id] = array_merge([
69 'id' => $id,
70 'label' => '',
71 'icon' => 'fas fa-tab',
72 'active' => false,
73 'order' => 0
74 ], $config);
75 }
76
77 /**
78 * Register a new field
79 *
80 * @since 1.0.0
81 * @param string $tab_id Tab ID to register field for
82 * @param array $config Field configuration
83 * @return void
84 */
85 public function registerField(string $tab_id, array $config): void {
86 // Apply filter to allow pro plugins to modify individual field config
87 $config = apply_filters($this->getFieldFilterName(), $config, $tab_id);
88
89 // Skip registration if field was filtered out (set to null/false)
90 if (empty($config)) {
91 return;
92 }
93
94 // Use field name as ID
95 $field_id = $config['name'] ?? uniqid('field_');
96
97 if (!isset($this->fields[$tab_id])) {
98 $this->fields[$tab_id] = [];
99 }
100
101 $this->fields[$tab_id][$field_id] = array_merge([
102 'id' => $field_id,
103 'type' => 'text',
104 'label' => '',
105 'name' => '',
106 'default_value' => '',
107 'placeholder' => '',
108 'required' => false,
109 'grid_cols' => 'sm:col-span-6',
110 'order' => 0
111 ], $config);
112 }
113
114 /**
115 * Register a new template
116 *
117 * @since 1.0.0
118 * @param string $id Template ID
119 * @param array $config Template configuration
120 * @return void
121 */
122 public function registerTemplate(string $id, array $config): void {
123 // Apply filter to allow pro plugins to modify individual template config
124 $config = apply_filters($this->getTemplateFilterName(), $config, $id);
125
126 // Skip registration if template was filtered out (set to null/false)
127 if (empty($config)) {
128 return;
129 }
130
131 $this->templates[$id] = array_merge([
132 'id' => $id,
133 'name' => '',
134 'description' => '',
135 'icon' => 'fas fa-file',
136 'order' => 0
137 ], $config);
138 }
139
140 /**
141 * Register a new item field
142 *
143 * @since 1.0.0
144 * @param array $config Item field configuration
145 * @return void
146 */
147 public function registerItemField(array $config): void {
148 // Apply filter to allow pro plugins to modify individual item field config
149 $config = apply_filters($this->getItemFieldFilterName(), $config);
150
151 // Skip registration if item field was filtered out (set to null/false)
152 if (empty($config)) {
153 return;
154 }
155
156 // Use field name as ID
157 $field_id = $config['name'] ?? uniqid('item_field_');
158 $this->item_fields[$field_id] = array_merge([
159 'id' => $field_id,
160 'type' => 'text',
161 'label' => '',
162 'name' => '',
163 'default_value' => '',
164 'placeholder' => '',
165 'required' => false,
166 'container_class' => 'sm:col-span-6',
167 'order' => 0
168 ], $config);
169 }
170
171 /**
172 * Get all registered tabs
173 *
174 * @since 1.0.0
175 * @return array
176 */
177 public function getTabs(): array {
178 // Sort tabs by order
179 uasort($this->tabs, function($a, $b) {
180 return $a['order'] <=> $b['order'];
181 });
182
183 return $this->tabs;
184 }
185
186 /**
187 * Get fields for a specific tab
188 *
189 * @since 1.0.0
190 * @param string $tab_id Tab ID
191 * @return array
192 */
193 public function getFields(string $tab_id): array {
194 if (!isset($this->fields[$tab_id])) {
195 return [];
196 }
197
198 // Sort fields by order
199 uasort($this->fields[$tab_id], function($a, $b) {
200 return $a['order'] <=> $b['order'];
201 });
202
203 return $this->fields[$tab_id];
204 }
205
206 /**
207 * Get fields by section within a tab
208 *
209 * @since 1.0.0
210 * @param string $tab_id Tab ID
211 * @param string $section Section name
212 * @return array
213 */
214 public function getFieldsBySection(string $tab_id, string $section): array {
215 $fields = $this->getFields($tab_id);
216 $section_fields = [];
217
218 foreach ($fields as $field) {
219 if (($field['section'] ?? '') === $section) {
220 $section_fields[] = $field;
221 }
222 }
223
224 return $section_fields;
225 }
226
227 /**
228 * Get all registered templates
229 *
230 * @since 1.0.0
231 * @return array
232 */
233 public function getTemplates(): array {
234 // Sort templates by order
235 uasort($this->templates, function($a, $b) {
236 return $a['order'] <=> $b['order'];
237 });
238
239 return $this->templates;
240 }
241
242 /**
243 * Get all registered item fields
244 *
245 * @since 1.0.0
246 * @return array
247 */
248 public function getItemFields(): array {
249 // Sort item fields by order
250 uasort($this->item_fields, function($a, $b) {
251 return $a['order'] <=> $b['order'];
252 });
253
254 return $this->item_fields;
255 }
256
257 /**
258 * Get item fields by container class
259 *
260 * @since 1.0.0
261 * @param string $container_class Container class to filter by
262 * @return array
263 */
264 public function getItemFieldsByContainerClass(string $container_class): array {
265 $fields = $this->getItemFields();
266 $filtered_fields = [];
267
268 foreach ($fields as $field) {
269 if (isset($field['container_class']) && $field['container_class'] === $container_class) {
270 $filtered_fields[] = $field;
271 }
272 }
273
274 return $filtered_fields;
275 }
276
277 /**
278 * Get the filter name for tabs
279 *
280 * @since 1.0.0
281 * @return string
282 */
283 abstract protected function getTabFilterName(): string;
284
285 /**
286 * Get the filter name for fields
287 *
288 * @since 1.0.0
289 * @return string
290 */
291 abstract protected function getFieldFilterName(): string;
292
293 /**
294 * Get the filter name for templates
295 *
296 * @since 1.0.0
297 * @return string
298 */
299 abstract protected function getTemplateFilterName(): string;
300
301 /**
302 * Get the filter name for item fields
303 *
304 * @since 1.0.0
305 * @return string
306 */
307 abstract protected function getItemFieldFilterName(): string;
308 }