field_registration = new InvoiceFieldRegistration(); // Call parent constructor (which will call initializeDefaults) parent::__construct(); } /** * Initialize default tabs, fields, and templates * * @since 1.0.0 * @return void */ protected function initializeDefaults(): void { // Register default tabs $this->field_registration->registerDefaultTabs(); // Register default fields $this->field_registration->registerDefaultFields(); // Register default templates $this->field_registration->registerDefaultTemplates(); // Register default item fields $this->field_registration->registerDefaultItemFields(); } /** * Get form templates * * @since 1.0.0 * @return array */ public function getTemplates(): array { return $this->field_registration->getTemplates(); } /** * Get item fields * * @since 1.0.0 * @return array */ public function getItemFields(): array { return $this->field_registration->getItemFields(); } /** * Get fields for a specific tab * * @since 1.0.0 * @param string $tab_id * @return array */ public function getFieldsForTab(string $tab_id): array { return $this->field_registration->getFieldsForTab($tab_id); } /** * Render form field * * @since 1.0.0 * @param array $field_config * @param mixed $value * @return string */ public function renderField(array $field_config, $value = null): string { return $this->field_renderer->renderField($field_config, $value); } /** * Render form tab * * @since 1.0.0 * @param string $tab_id * @param array $tab_config * @param array $fields * @param array $data * @return string */ public function renderTab(string $tab_id, array $tab_config, array $fields, array $data = []): string { return $this->tab_renderer->render($tab_id, $tab_config, $fields, $data); } /** * Process form data * * @since 1.0.0 * @param array $data * @return array */ public function processFormData(array $data): array { $all_fields = $this->getAllFields(); return $this->form_processor->processFormData($data, $all_fields); } /** * Process items data * * @since 1.0.0 * @param array $items_data * @return array */ public function processItemsData(array $items_data): array { return $this->form_processor->processItemsData($items_data, $this->getItemFields()); } /** * Validate form data * * @since 1.0.0 * @param array $data * @return array */ public function validateFormData(array $data): array { return $this->form_validator->validate($data, $this->getAllFields()); } /** * Get form errors * * @since 1.0.0 * @return array */ public function getErrors(): array { return $this->form_validator->getErrors(); } /** * Clear form errors * * @since 1.0.0 * @return void */ public function clearErrors(): void { $this->form_validator->clearErrors(); } /** * Get the form type * * @since 1.0.0 * @return string */ protected function getFormType(): string { return 'invoice'; } /** * Get the form action * * @since 1.0.0 * @return string */ protected function getFormAction(): string { return admin_url('admin-ajax.php'); } /** * Get the form method * * @since 1.0.0 * @return string */ protected function getFormMethod(): string { return 'POST'; } /** * Get the form enctype * * @since 1.0.0 * @return string */ protected function getFormEnctype(): string { return 'multipart/form-data'; } /** * Get the form class * * @since 1.0.0 * @return string */ protected function getFormClass(): string { return 'easy-invoice-form invoice-form'; } /** * Get the form ID * * @since 1.0.0 * @return string */ protected function getFormId(): string { return 'easy-invoice-form'; } /** * Get the form nonce action * * @since 1.0.0 * @return string */ protected function getFormNonceAction(): string { return 'easy_invoice_invoice_form_nonce'; } /** * Get the form nonce field name * * @since 1.0.0 * @return string */ protected function getFormNonceFieldName(): string { return 'easy_invoice_invoice_nonce'; } /** * Get the form submit button text * * @since 1.0.0 * @return string */ protected function getSubmitButtonText(): string { return __('Save Invoice', 'easy-invoice'); } /** * Get the form submit button class * * @since 1.0.0 * @return string */ protected function getSubmitButtonClass(): string { return 'button button-primary easy-invoice-submit'; } /** * Get the form submit button ID * * @since 1.0.0 * @return string */ protected function getSubmitButtonId(): string { return 'easy-invoice-submit'; } /** * Get the form submit button name * * @since 1.0.0 * @return string */ protected function getSubmitButtonName(): string { return 'easy_invoice_invoice_submit'; } /** * Get the form submit button value * * @since 1.0.0 * @return string */ protected function getSubmitButtonValue(): string { return 'save_invoice'; } /** * Get the form action field name * * @since 1.0.0 * @return string */ protected function getActionFieldName(): string { return 'action'; } /** * Get the form action field value * * @since 1.0.0 * @return string */ protected function getActionFieldValue(): string { return 'easy_invoice_save_invoice'; } /** * Get the form invoice ID field name * * @since 1.0.0 * @return string */ protected function getInvoiceIdFieldName(): string { return 'invoice_id'; } /** * Get the form invoice ID field value * * @since 1.0.0 * @param int $invoice_id * @return string */ protected function getInvoiceIdFieldValue(int $invoice_id = 0): string { return (string) $invoice_id; } /** * Get the form invoice ID field type * * @since 1.0.0 * @return string */ protected function getInvoiceIdFieldType(): string { return 'hidden'; } /** * Get the form invoice ID field class * * @since 1.0.0 * @return string */ protected function getInvoiceIdFieldClass(): string { return 'easy-invoice-invoice-id'; } /** * Get the form invoice ID field ID * * @since 1.0.0 * @return string */ protected function getInvoiceIdFieldId(): string { return 'easy-invoice-invoice-id'; } /** * Get field configuration for JavaScript * * @since 1.0.0 * @return array */ public function getFieldConfigForJavaScript(): array { $config = [ 'tabs' => $this->getTabs(), 'fields' => [], 'templates' => $this->getTemplates(), 'itemFields' => $this->getItemFields() ]; // Get fields for each tab $tabs = $this->getTabs(); foreach ($tabs as $tab_id => $tab_config) { $config['fields'][$tab_id] = $this->getFields($tab_id); } // Convert item fields array to object with field names as keys for JavaScript $item_fields_array = $this->getItemFields(); $item_fields_object = []; foreach ($item_fields_array as $field) { $field_name = $field['name'] ?? ''; if (!empty($field_name)) { $item_fields_object[$field_name] = $field; } } $config['itemFields'] = $item_fields_object; return $config; } /** * Generate HTML for existing invoice items * * @param mixed $item The item object or array * @param int $index The item index * @return string */ public function generateExistingItemHtml($item, int $index): string { $html = ''; $fields = $this->getItemFields(); // Wrap fields in a grid container $html .= '