PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.4.0
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.4.0
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 / Invoice / InvoiceFormManager.php

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

482 lines 11.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Invoice Form Manager 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\Invoice;
13
14 use EasyInvoice\Forms\BaseFormManager;
15
16 /**
17 * Invoice Form Manager
18 *
19 * Handles invoice-specific form management, rendering, and processing.
20 *
21 * @since 1.0.0
22 */
23 class InvoiceFormManager extends BaseFormManager {
24
25 /**
26 * Constructor
27 *
28 * @since 1.0.0
29 */
30 public function __construct() {
31 // Initialize invoice-specific field registration first
32 $this->field_registration = new InvoiceFieldRegistration();
33
34 // Call parent constructor (which will call initializeDefaults)
35 parent::__construct();
36 }
37
38 /**
39 * Initialize default tabs, fields, and templates
40 *
41 * @since 1.0.0
42 * @return void
43 */
44 protected function initializeDefaults(): void {
45 // Register default tabs
46 $this->field_registration->registerDefaultTabs();
47
48 // Register default fields
49 $this->field_registration->registerDefaultFields();
50
51 // Register default templates
52 $this->field_registration->registerDefaultTemplates();
53
54 // Register default item fields
55 $this->field_registration->registerDefaultItemFields();
56 }
57
58 /**
59 * Get form templates
60 *
61 * @since 1.0.0
62 * @return array
63 */
64 public function getTemplates(): array {
65 return $this->field_registration->getTemplates();
66 }
67
68 /**
69 * Get item fields
70 *
71 * @since 1.0.0
72 * @return array
73 */
74 public function getItemFields(): array {
75 return $this->field_registration->getItemFields();
76 }
77
78 /**
79 * Get fields for a specific tab
80 *
81 * @since 1.0.0
82 * @param string $tab_id
83 * @return array
84 */
85 public function getFieldsForTab(string $tab_id): array {
86 return $this->field_registration->getFieldsForTab($tab_id);
87 }
88
89 /**
90 * Render form field
91 *
92 * @since 1.0.0
93 * @param array $field_config
94 * @param mixed $value
95 * @return string
96 */
97 public function renderField(array $field_config, $value = null): string {
98 return $this->field_renderer->renderField($field_config, $value);
99 }
100
101 /**
102 * Render form tab
103 *
104 * @since 1.0.0
105 * @param string $tab_id
106 * @param array $tab_config
107 * @param array $fields
108 * @param array $data
109 * @return string
110 */
111 public function renderTab(string $tab_id, array $tab_config, array $fields, array $data = []): string {
112 return $this->tab_renderer->render($tab_id, $tab_config, $fields, $data);
113 }
114
115 /**
116 * Process form data
117 *
118 * @since 1.0.0
119 * @param array $data
120 * @return array
121 */
122 public function processFormData(array $data): array {
123 $all_fields = $this->getAllFields();
124 return $this->form_processor->processFormData($data, $all_fields);
125 }
126
127 /**
128 * Process items data
129 *
130 * @since 1.0.0
131 * @param array $items_data
132 * @return array
133 */
134 public function processItemsData(array $items_data): array {
135 return $this->form_processor->processItemsData($items_data, $this->getItemFields());
136 }
137
138 /**
139 * Validate form data
140 *
141 * @since 1.0.0
142 * @param array $data
143 * @return array
144 */
145 public function validateFormData(array $data): array {
146 return $this->form_validator->validate($data, $this->getAllFields());
147 }
148
149 /**
150 * Get form errors
151 *
152 * @since 1.0.0
153 * @return array
154 */
155 public function getErrors(): array {
156 return $this->form_validator->getErrors();
157 }
158
159 /**
160 * Clear form errors
161 *
162 * @since 1.0.0
163 * @return void
164 */
165 public function clearErrors(): void {
166 $this->form_validator->clearErrors();
167 }
168
169 /**
170 * Get the form type
171 *
172 * @since 1.0.0
173 * @return string
174 */
175 protected function getFormType(): string {
176 return 'invoice';
177 }
178
179 /**
180 * Get the form action
181 *
182 * @since 1.0.0
183 * @return string
184 */
185 protected function getFormAction(): string {
186 return admin_url('admin-ajax.php');
187 }
188
189 /**
190 * Get the form method
191 *
192 * @since 1.0.0
193 * @return string
194 */
195 protected function getFormMethod(): string {
196 return 'POST';
197 }
198
199 /**
200 * Get the form enctype
201 *
202 * @since 1.0.0
203 * @return string
204 */
205 protected function getFormEnctype(): string {
206 return 'multipart/form-data';
207 }
208
209 /**
210 * Get the form class
211 *
212 * @since 1.0.0
213 * @return string
214 */
215 protected function getFormClass(): string {
216 return 'easy-invoice-form invoice-form';
217 }
218
219 /**
220 * Get the form ID
221 *
222 * @since 1.0.0
223 * @return string
224 */
225 protected function getFormId(): string {
226 return 'easy-invoice-form';
227 }
228
229 /**
230 * Get the form nonce action
231 *
232 * @since 1.0.0
233 * @return string
234 */
235 protected function getFormNonceAction(): string {
236 return 'easy_invoice_invoice_form_nonce';
237 }
238
239 /**
240 * Get the form nonce field name
241 *
242 * @since 1.0.0
243 * @return string
244 */
245 protected function getFormNonceFieldName(): string {
246 return 'easy_invoice_invoice_nonce';
247 }
248
249 /**
250 * Get the form submit button text
251 *
252 * @since 1.0.0
253 * @return string
254 */
255 protected function getSubmitButtonText(): string {
256 return __('Save Invoice', 'easy-invoice');
257 }
258
259 /**
260 * Get the form submit button class
261 *
262 * @since 1.0.0
263 * @return string
264 */
265 protected function getSubmitButtonClass(): string {
266 return 'button button-primary easy-invoice-submit';
267 }
268
269 /**
270 * Get the form submit button ID
271 *
272 * @since 1.0.0
273 * @return string
274 */
275 protected function getSubmitButtonId(): string {
276 return 'easy-invoice-submit';
277 }
278
279 /**
280 * Get the form submit button name
281 *
282 * @since 1.0.0
283 * @return string
284 */
285 protected function getSubmitButtonName(): string {
286 return 'easy_invoice_invoice_submit';
287 }
288
289 /**
290 * Get the form submit button value
291 *
292 * @since 1.0.0
293 * @return string
294 */
295 protected function getSubmitButtonValue(): string {
296 return 'save_invoice';
297 }
298
299 /**
300 * Get the form action field name
301 *
302 * @since 1.0.0
303 * @return string
304 */
305 protected function getActionFieldName(): string {
306 return 'action';
307 }
308
309 /**
310 * Get the form action field value
311 *
312 * @since 1.0.0
313 * @return string
314 */
315 protected function getActionFieldValue(): string {
316 return 'easy_invoice_save_invoice';
317 }
318
319 /**
320 * Get the form invoice ID field name
321 *
322 * @since 1.0.0
323 * @return string
324 */
325 protected function getInvoiceIdFieldName(): string {
326 return 'invoice_id';
327 }
328
329 /**
330 * Get the form invoice ID field value
331 *
332 * @since 1.0.0
333 * @param int $invoice_id
334 * @return string
335 */
336 protected function getInvoiceIdFieldValue(int $invoice_id = 0): string {
337 return (string) $invoice_id;
338 }
339
340 /**
341 * Get the form invoice ID field type
342 *
343 * @since 1.0.0
344 * @return string
345 */
346 protected function getInvoiceIdFieldType(): string {
347 return 'hidden';
348 }
349
350 /**
351 * Get the form invoice ID field class
352 *
353 * @since 1.0.0
354 * @return string
355 */
356 protected function getInvoiceIdFieldClass(): string {
357 return 'easy-invoice-invoice-id';
358 }
359
360 /**
361 * Get the form invoice ID field ID
362 *
363 * @since 1.0.0
364 * @return string
365 */
366 protected function getInvoiceIdFieldId(): string {
367 return 'easy-invoice-invoice-id';
368 }
369
370 /**
371 * Get field configuration for JavaScript
372 *
373 * @since 1.0.0
374 * @return array
375 */
376 public function getFieldConfigForJavaScript(): array {
377 $config = [
378 'tabs' => $this->getTabs(),
379 'fields' => [],
380 'templates' => $this->getTemplates(),
381 'itemFields' => $this->getItemFields()
382 ];
383
384 // Get fields for each tab
385 $tabs = $this->getTabs();
386 foreach ($tabs as $tab_id => $tab_config) {
387 $config['fields'][$tab_id] = $this->getFields($tab_id);
388 }
389
390 // Convert item fields array to object with field names as keys for JavaScript
391 $item_fields_array = $this->getItemFields();
392 $item_fields_object = [];
393 foreach ($item_fields_array as $field) {
394 $field_name = $field['name'] ?? '';
395 if (!empty($field_name)) {
396 $item_fields_object[$field_name] = $field;
397 }
398 }
399 $config['itemFields'] = $item_fields_object;
400
401 return $config;
402 }
403
404 /**
405 * Generate HTML for existing invoice items
406 *
407 * @param mixed $item The item object or array
408 * @param int $index The item index
409 * @return string
410 */
411 public function generateExistingItemHtml($item, int $index): string {
412 $html = '';
413 $fields = $this->getItemFields();
414
415 // Wrap fields in a grid container
416 $html .= '<div class="grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-4">';
417
418 foreach ($fields as $field) {
419 $field_name = $field['name'] ?? '';
420 $value = '';
421 // Try to get the value from the item object using a getter
422 $getter = 'get' . easy_invoice_str_replace(' ', '', ucwords(easy_invoice_str_replace('_', ' ', $field_name)));
423
424 // Try to call the getter method (including magic methods via __call)
425 try {
426 $value = $item->$getter();
427 } catch (\BadMethodCallException $e) {
428 // If the magic method doesn't exist, try array access
429 if (is_array($item) && isset($item[$field_name])) {
430 $value = $item[$field_name];
431 } else {
432 $value = '';
433 }
434 }
435
436 // A stored boolean must reach the renderer as '1'/'0'; cast to
437 // string, false becomes '' and would be mistaken for "not set".
438 if (is_bool($value)) {
439 $value = $value ? '1' : '0';
440 } elseif ('taxable' === $field_name && is_object($item) && is_callable([$item, 'isTaxable'])) {
441 $value = $item->isTaxable() ? '1' : '0';
442 }
443 $html .= $this->item_field_renderer->renderItemField($field, $index, is_scalar($value) ? (string) $value : '');
444 }
445
446 $html .= '</div>';
447
448 return $html;
449 }
450
451 /**
452 * Render the table header row for invoice items
453 *
454 * @since 1.0.0
455 * @return string
456 */
457 public function renderItemTableHeader(): string {
458 return $this->item_field_renderer->renderTableHeaderRow($this->getItemFields());
459 }
460
461 /**
462 * Render a table row for an existing invoice item
463 *
464 * @since 1.0.0
465 * @param object|array $item
466 * @param int $index
467 * @return string
468 */
469 public function renderItemTableRow($item, int $index): string {
470 return $this->item_field_renderer->renderTableRow($this->getItemFields(), $item, $index);
471 }
472
473 /**
474 * Render a table row for a blank (template) item
475 *
476 * @since 1.0.0
477 * @return string
478 */
479 public function renderTemplateItemTableRow(): string {
480 return $this->item_field_renderer->renderTemplateTableRow($this->getItemFields());
481 }
482 }