PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.4.1
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.4.1
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 / Quote / QuoteFormManager.php

QuoteFormManager.php in Easy Invoice – Invoice Generator, PDF Quotes & Payments 2.4.1, at includes/Forms/Quote/QuoteFormManager.php

464 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Quote 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\Quote;
13
14 use EasyInvoice\Forms\BaseFormManager;
15
16 /**
17 * Quote Form Manager
18 *
19 * Handles quote-specific form management, rendering, and processing.
20 *
21 * @since 1.0.0
22 */
23 class QuoteFormManager extends BaseFormManager {
24
25 /**
26 * Constructor
27 *
28 * @since 1.0.0
29 */
30 public function __construct() {
31 // Initialize quote-specific field registration first
32 $this->field_registration = new QuoteFieldRegistration();
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 return $this->form_processor->processFormData($data, $this->getAllFields());
124 }
125
126 /**
127 * Process items data
128 *
129 * @since 1.0.0
130 * @param array $items_data
131 * @return array
132 */
133 public function processItemsData(array $items_data): array {
134 return $this->form_processor->processItemsData($items_data, $this->getItemFields());
135 }
136
137 /**
138 * Validate form data
139 *
140 * @since 1.0.0
141 * @param array $data
142 * @return array
143 */
144 public function validateFormData(array $data): array {
145 return $this->form_validator->validate($data, $this->getAllFields());
146 }
147
148 /**
149 * Get form errors
150 *
151 * @since 1.0.0
152 * @return array
153 */
154 public function getErrors(): array {
155 return $this->form_validator->getErrors();
156 }
157
158 /**
159 * Clear form errors
160 *
161 * @since 1.0.0
162 * @return void
163 */
164 public function clearErrors(): void {
165 $this->form_validator->clearErrors();
166 }
167
168 /**
169 * Get the form type
170 *
171 * @since 1.0.0
172 * @return string
173 */
174 protected function getFormType(): string {
175 return 'quote';
176 }
177
178 /**
179 * Get the form action
180 *
181 * @since 1.0.0
182 * @return string
183 */
184 protected function getFormAction(): string {
185 return admin_url('admin-ajax.php');
186 }
187
188 /**
189 * Get the form method
190 *
191 * @since 1.0.0
192 * @return string
193 */
194 protected function getFormMethod(): string {
195 return 'POST';
196 }
197
198 /**
199 * Get the form enctype
200 *
201 * @since 1.0.0
202 * @return string
203 */
204 protected function getFormEnctype(): string {
205 return 'multipart/form-data';
206 }
207
208 /**
209 * Get the form class
210 *
211 * @since 1.0.0
212 * @return string
213 */
214 protected function getFormClass(): string {
215 return 'easy-invoice-form quote-form';
216 }
217
218 /**
219 * Get the form ID
220 *
221 * @since 1.0.0
222 * @return string
223 */
224 protected function getFormId(): string {
225 return 'easy-invoice-quote-form';
226 }
227
228 /**
229 * Get the form nonce action
230 *
231 * @since 1.0.0
232 * @return string
233 */
234 protected function getFormNonceAction(): string {
235 return 'easy_invoice_quote_form_nonce';
236 }
237
238 /**
239 * Get the form nonce field name
240 *
241 * @since 1.0.0
242 * @return string
243 */
244 protected function getFormNonceFieldName(): string {
245 return 'easy_invoice_quote_nonce';
246 }
247
248 /**
249 * Get the form submit button text
250 *
251 * @since 1.0.0
252 * @return string
253 */
254 protected function getSubmitButtonText(): string {
255 return __('Save Quote', 'easy-invoice');
256 }
257
258 /**
259 * Get the form submit button class
260 *
261 * @since 1.0.0
262 * @return string
263 */
264 protected function getSubmitButtonClass(): string {
265 return 'button button-primary easy-invoice-submit';
266 }
267
268 /**
269 * Get the form submit button ID
270 *
271 * @since 1.0.0
272 * @return string
273 */
274 protected function getSubmitButtonId(): string {
275 return 'easy-invoice-quote-submit';
276 }
277
278 /**
279 * Get the form submit button name
280 *
281 * @since 1.0.0
282 * @return string
283 */
284 protected function getSubmitButtonName(): string {
285 return 'easy_invoice_quote_submit';
286 }
287
288 /**
289 * Get the form submit button value
290 *
291 * @since 1.0.0
292 * @return string
293 */
294 protected function getSubmitButtonValue(): string {
295 return 'save_quote';
296 }
297
298 /**
299 * Get the form action field name
300 *
301 * @since 1.0.0
302 * @return string
303 */
304 protected function getActionFieldName(): string {
305 return 'action';
306 }
307
308 /**
309 * Get the form action field value
310 *
311 * @since 1.0.0
312 * @return string
313 */
314 protected function getActionFieldValue(): string {
315 return 'easy_invoice_save_quote';
316 }
317
318 /**
319 * Get the form quote ID field name
320 *
321 * @since 1.0.0
322 * @return string
323 */
324 protected function getQuoteIdFieldName(): string {
325 return 'quote_id';
326 }
327
328 /**
329 * Get the form quote ID field value
330 *
331 * @since 1.0.0
332 * @param int $quote_id
333 * @return string
334 */
335 protected function getQuoteIdFieldValue(int $quote_id = 0): string {
336 return (string) $quote_id;
337 }
338
339 /**
340 * Get the form quote ID field type
341 *
342 * @since 1.0.0
343 * @return string
344 */
345 protected function getQuoteIdFieldType(): string {
346 return 'hidden';
347 }
348
349 /**
350 * Get the form quote ID field class
351 *
352 * @since 1.0.0
353 * @return string
354 */
355 protected function getQuoteIdFieldClass(): string {
356 return 'easy-invoice-quote-id';
357 }
358
359 /**
360 * Get the form quote ID field ID
361 *
362 * @since 1.0.0
363 * @return string
364 */
365 protected function getQuoteIdFieldId(): string {
366 return 'easy-invoice-quote-id';
367 }
368
369 /**
370 * Get field configuration for JavaScript
371 *
372 * @since 1.0.0
373 * @return array
374 */
375 public function getFieldConfigForJavaScript(): array {
376 $config = [
377 'tabs' => $this->getTabs(),
378 'fields' => [],
379 'templates' => $this->getTemplates(),
380 'itemFields' => $this->getItemFields()
381 ];
382
383 // Get fields for each tab
384 $tabs = $this->getTabs();
385 foreach ($tabs as $tab_id => $tab_config) {
386 $config['fields'][$tab_id] = $this->getFields($tab_id);
387 }
388
389 return $config;
390 }
391
392 /**
393 * Generate HTML for a blank (template) quote item
394 *
395 * @since 1.0.0
396 * @return string
397 */
398 public function generateItemTemplateHtml(): string {
399 return $this->item_field_renderer->generateItemTemplateHtml($this->field_registration);
400 }
401
402 /**
403 * Render all item fields for an existing item
404 *
405 * @since 1.0.0
406 * @param object $item The item object
407 * @param int $index The item index
408 * @return string
409 */
410 public function generateExistingItemHtml($item, int $index): string {
411 $html = '';
412 $fields = $this->getItemFields();
413
414 // Wrap fields in a grid container
415 $html .= '<div class="grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-4">';
416
417 foreach ($fields as $field) {
418 $field_name = $field['name'] ?? '';
419 $value = '';
420
421 // Map field names to the correct getter methods
422 $getter_map = [
423 'title' => 'getName',
424 'name' => 'getName',
425 'description' => 'getDescription',
426 'quantity' => 'getQuantity',
427 'price' => 'getPrice',
428 'total' => 'getAmount',
429 'amount' => 'getAmount',
430 'taxable' => 'getTaxable',
431 'adjust_percentage' => 'getAdjustPercentage'
432 ];
433
434 // Try to get the value from the item object using the mapped getter
435 if (isset($getter_map[$field_name]) && method_exists($item, $getter_map[$field_name])) {
436 $getter = $getter_map[$field_name];
437 $value = $item->$getter();
438 } elseif (is_array($item) && isset($item[$field_name])) {
439 $value = $item[$field_name];
440 } else {
441 // Try dynamic getter for fields not in the map
442 $getter = 'get' . easy_invoice_str_replace(' ', '', ucwords(easy_invoice_str_replace('_', ' ', $field_name)));
443 try {
444 $value = $item->$getter();
445 } catch (\BadMethodCallException $e) {
446 $value = '';
447 }
448 }
449
450 // A stored boolean must reach the renderer as '1'/'0'; cast to
451 // string, false becomes '' and would be mistaken for "not set".
452 if (is_bool($value)) {
453 $value = $value ? '1' : '0';
454 } elseif ('taxable' === $field_name && is_object($item) && is_callable([$item, 'isTaxable'])) {
455 $value = $item->isTaxable() ? '1' : '0';
456 }
457 $html .= $this->item_field_renderer->renderItemField($field, $index, is_scalar($value) ? (string) $value : '');
458 }
459
460 $html .= '</div>';
461
462 return $html;
463 }
464 }