| 1 |
<?php |
| 2 |
/** |
| 3 |
* Base 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; |
| 13 |
|
| 14 |
/** |
| 15 |
* Base Form Manager |
| 16 |
* |
| 17 |
* Provides common functionality for form managers. |
| 18 |
* |
| 19 |
* @since 1.0.0 |
| 20 |
*/ |
| 21 |
abstract class BaseFormManager { |
| 22 |
|
| 23 |
/** |
| 24 |
* Field registration component |
| 25 |
* |
| 26 |
* @var BaseFieldRegistration |
| 27 |
*/ |
| 28 |
protected $field_registration; |
| 29 |
|
| 30 |
/** |
| 31 |
* Field renderer component |
| 32 |
* |
| 33 |
* @var FieldRenderer |
| 34 |
*/ |
| 35 |
protected $field_renderer; |
| 36 |
|
| 37 |
/** |
| 38 |
* Item field renderer component |
| 39 |
* |
| 40 |
* @var ItemFieldRenderer |
| 41 |
*/ |
| 42 |
protected $item_field_renderer; |
| 43 |
|
| 44 |
/** |
| 45 |
* Form processor component |
| 46 |
* |
| 47 |
* @var FormProcessor |
| 48 |
*/ |
| 49 |
protected $form_processor; |
| 50 |
|
| 51 |
/** |
| 52 |
* Constructor |
| 53 |
* |
| 54 |
* @since 1.0.0 |
| 55 |
*/ |
| 56 |
public function __construct() { |
| 57 |
$this->field_renderer = new FieldRenderer(); |
| 58 |
$this->item_field_renderer = new ItemFieldRenderer(); |
| 59 |
$this->form_processor = new FormProcessor(); |
| 60 |
|
| 61 |
$this->initializeDefaults(); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Initialize default tabs, fields, and templates |
| 66 |
* |
| 67 |
* @since 1.0.0 |
| 68 |
* @return void |
| 69 |
*/ |
| 70 |
abstract protected function initializeDefaults(): void; |
| 71 |
|
| 72 |
/** |
| 73 |
* Get all registered tabs |
| 74 |
* |
| 75 |
* @since 1.0.0 |
| 76 |
* @return array |
| 77 |
*/ |
| 78 |
public function getTabs(): array { |
| 79 |
return $this->field_registration->getTabs(); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Get fields for a specific tab |
| 84 |
* |
| 85 |
* @since 1.0.0 |
| 86 |
* @param string $tab_id Tab ID |
| 87 |
* @return array |
| 88 |
*/ |
| 89 |
public function getFields(string $tab_id): array { |
| 90 |
return $this->field_registration->getFields($tab_id); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get fields by section within a tab |
| 95 |
* |
| 96 |
* @since 1.0.0 |
| 97 |
* @param string $tab_id Tab ID |
| 98 |
* @param string $section Section name |
| 99 |
* @return array |
| 100 |
*/ |
| 101 |
public function getFieldsBySection(string $tab_id, string $section): array { |
| 102 |
return $this->field_registration->getFieldsBySection($tab_id, $section); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Get all registered templates |
| 107 |
* |
| 108 |
* @since 1.0.0 |
| 109 |
* @return array |
| 110 |
*/ |
| 111 |
public function getTemplates(): array { |
| 112 |
return $this->field_registration->getTemplates(); |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* Get all registered item fields |
| 117 |
* |
| 118 |
* @since 1.0.0 |
| 119 |
* @return array |
| 120 |
*/ |
| 121 |
public function getItemFields(): array { |
| 122 |
return $this->field_registration->getItemFields(); |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Get item fields by container class |
| 127 |
* |
| 128 |
* @since 1.0.0 |
| 129 |
* @param string $container_class Container class to filter by |
| 130 |
* @return array |
| 131 |
*/ |
| 132 |
public function getItemFieldsByContainerClass(string $container_class): array { |
| 133 |
return $this->field_registration->getItemFieldsByContainerClass($container_class); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Render a field based on its type |
| 138 |
* |
| 139 |
* @since 1.0.0 |
| 140 |
* @param array $field Field configuration |
| 141 |
* @param mixed $object Object or null |
| 142 |
* @return string Rendered HTML |
| 143 |
*/ |
| 144 |
public function renderField(array $field, $object = null): string { |
| 145 |
return $this->field_renderer->renderField($field, $object); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Generate item template HTML |
| 150 |
* |
| 151 |
* @since 1.0.0 |
| 152 |
* @return string |
| 153 |
*/ |
| 154 |
public function generateItemTemplateHtml(): string { |
| 155 |
return $this->item_field_renderer->generateItemTemplateHtml($this->field_registration); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* Process form data |
| 160 |
* |
| 161 |
* @since 1.0.0 |
| 162 |
* @param array $form_data Form data |
| 163 |
* @return array Processed data |
| 164 |
*/ |
| 165 |
public function processFormData(array $form_data): array { |
| 166 |
return $this->form_processor->process($form_data); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Get all fields from all tabs |
| 171 |
* |
| 172 |
* @since 1.0.0 |
| 173 |
* @return array All fields |
| 174 |
*/ |
| 175 |
public function getAllFields(): array { |
| 176 |
$all_fields = []; |
| 177 |
$tabs = $this->getTabs(); |
| 178 |
|
| 179 |
foreach ($tabs as $tab) { |
| 180 |
$tab_fields = $this->getFields($tab['id']); |
| 181 |
$all_fields = array_merge($all_fields, $tab_fields); |
| 182 |
} |
| 183 |
|
| 184 |
return $all_fields; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Get field by name |
| 189 |
* |
| 190 |
* @since 1.0.0 |
| 191 |
* @param string $field_name Field name |
| 192 |
* @return array|null Field configuration or null |
| 193 |
*/ |
| 194 |
public function getFieldByName(string $field_name): ?array { |
| 195 |
$all_fields = $this->getAllFields(); |
| 196 |
|
| 197 |
foreach ($all_fields as $field) { |
| 198 |
if (($field['name'] ?? '') === $field_name) { |
| 199 |
return $field; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
return null; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Get item field by name |
| 208 |
* |
| 209 |
* @since 1.0.0 |
| 210 |
* @param string $field_name Field name |
| 211 |
* @return array|null Field configuration or null |
| 212 |
*/ |
| 213 |
public function getItemFieldByName(string $field_name): ?array { |
| 214 |
$item_fields = $this->getItemFields(); |
| 215 |
|
| 216 |
foreach ($item_fields as $field) { |
| 217 |
if (($field['name'] ?? '') === $field_name) { |
| 218 |
return $field; |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
return null; |
| 223 |
} |
| 224 |
} |