# easy-invoice/2.3.2/includes/Forms/Invoice/InvoiceFormManager.php

Easy Invoice – Invoice Generator, PDF Quotes &amp; Payments, version 2.3.2. 475 lines.

- Page: https://pluginprobe.com/plugins/easy-invoice/2.3.2/code/includes/Forms/Invoice/InvoiceFormManager.php
- Raw: https://pluginprobe.com/plugins/easy-invoice/2.3.2/raw/includes/Forms/Invoice/InvoiceFormManager.php
- Modified: 2025-08-14T09:51:16+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/easy-invoice/2.3.2/code/includes/Forms/Invoice/InvoiceFormManager.php#L10-L20`.

```php
<?php
/**
 * Invoice Form Manager Class
 *
 * @package     EasyInvoice
 * @author      Your Name
 * @copyright   Copyright (c) 2023, Your Company
 * @license     http://opensource.org/licenses/gpl-2.0.php GNU Public License
 * @since       1.0.0
 */

namespace EasyInvoice\Forms\Invoice;

use EasyInvoice\Forms\BaseFormManager;

/**
 * Invoice Form Manager
 *
 * Handles invoice-specific form management, rendering, and processing.
 *
 * @since 1.0.0
 */
class InvoiceFormManager extends BaseFormManager {
    
    /**
     * Constructor
     *
     * @since 1.0.0
     */
    public function __construct() {
        // Initialize invoice-specific field registration first
        $this->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 .= '<div class="grid grid-cols-1 gap-y-6 gap-x-4 sm:grid-cols-4">';
        
        foreach ($fields as $field) {
            $field_name = $field['name'] ?? '';
            $value = '';
            // Try to get the value from the item object using a getter
            $getter = 'get' . easy_invoice_str_replace(' ', '', ucwords(easy_invoice_str_replace('_', ' ', $field_name)));
            
            // Try to call the getter method (including magic methods via __call)
            try {
                $value = $item->$getter();
            } catch (\BadMethodCallException $e) {
                // If the magic method doesn't exist, try array access
                if (is_array($item) && isset($item[$field_name])) {
                    $value = $item[$field_name];
                } else {
                    $value = '';
                }
            }
            
            $html .= $this->item_field_renderer->renderItemField($field, $index, $value);
        }
        
        $html .= '</div>';
        
        return $html;
    }
    
    /**
     * Render the table header row for invoice items
     *
     * @since 1.0.0
     * @return string
     */
    public function renderItemTableHeader(): string {
        return $this->item_field_renderer->renderTableHeaderRow($this->getItemFields());
    }

    /**
     * Render a table row for an existing invoice item
     *
     * @since 1.0.0
     * @param object|array $item
     * @param int $index
     * @return string
     */
    public function renderItemTableRow($item, int $index): string {
        return $this->item_field_renderer->renderTableRow($this->getItemFields(), $item, $index);
    }

    /**
     * Render a table row for a blank (template) item
     *
     * @since 1.0.0
     * @return string
     */
    public function renderTemplateItemTableRow(): string {
        return $this->item_field_renderer->renderTemplateTableRow($this->getItemFields());
    }
} 
```
