entity = $entity; $this->design = $design->design; $this->input_variables = json_decode(json_encode($input_variables), 1); $this->entity_string = $entity_string; } /** * Returns the design * formatted HTML * @return string The HTML design built */ public function build():Designer { $this->setHtml() ->exportVariables() ->setDesign($this->getSection('includes')) ->setDesign($this->getSection('header')) ->setDesign($this->getSection('body')) ->setDesign($this->getSection('footer')); return $this; } public function init() { $this->setHtml() ->exportVariables(); return $this; } public function getIncludes() { return $this->getSection('includes'); } public function getHeader() { return $this->getSection('header'); } public function getFooter() { return $this->getSection('footer'); } public function getBody() { return $this->getSection('body'); } public function getHtml():string { return $this->html; } public function setHtml() { $this->html = ''; return $this; } private function setDesign($section) { $this->html .= $section; return $this; } /** * Returns the template section on with the * stacked variables replaced with single variables. * * @param string $section the method name to be executed ie header/body/table/footer * @return string The HTML of the template section */ public function getSection($section):string { return str_replace(array_keys($this->exported_variables), array_values($this->exported_variables), $this->design->{$section}); } private function exportVariables() { //$s = microtime(true); $company = $this->entity->company; $this->exported_variables['$app_url'] = $this->entity->generateAppUrl(); $this->exported_variables['$client_details'] = $this->processVariables($this->input_variables['client_details'], $this->clientDetails($company)); $this->exported_variables['$company_details'] = $this->processVariables($this->input_variables['company_details'], $this->companyDetails($company)); $this->exported_variables['$company_address'] = $this->processVariables($this->input_variables['company_address'], $this->companyAddress($company)); if ($this->entity_string == 'invoice') { //$this->exported_variables['$entity_labels'] = $this->processLabels($this->input_variables['invoice_details'], $this->invoiceDetails($company)); $this->exported_variables['$entity_details'] = $this->processVariables($this->input_variables['invoice_details'], $this->invoiceDetails($company)); } elseif ($this->entity_string == 'credit') { //$this->exported_variables['$entity_labels'] = $this->processLabels($this->input_variables['credit_details'], $this->creditDetails($company)); $this->exported_variables['$entity_details'] = $this->processVariables($this->input_variables['credit_details'], $this->creditDetails($company)); } elseif ($this->entity_string == 'quote') { //$this->exported_variables['$entity_labels'] = $this->processLabels($this->input_variables['quote_details'], $this->quoteDetails($company)); $this->exported_variables['$entity_details'] = $this->processVariables($this->input_variables['quote_details'], $this->quoteDetails($company)); } else { $this->exported_variables['$entity_details'] = $this->processVariables($this->input_variables['invoice_details'], $this->quoteDetails($company)); } $this->exported_variables['$product_table_header']= $this->entity->buildTableHeader($this->input_variables['product_columns']); $this->exported_variables['$product_table_body'] = $this->entity->buildTableBody($this->input_variables['product_columns'], $this->design->product, '$product'); $this->exported_variables['$task_table_header'] = $this->entity->buildTableHeader($this->input_variables['task_columns']); $this->exported_variables['$task_table_body'] = $this->entity->buildTableBody($this->input_variables['task_columns'], $this->design->task, '$task'); if (strlen($this->exported_variables['$task_table_body']) == 0) { $this->exported_variables['$task_table_header'] = ''; } if (strlen($this->exported_variables['$product_table_body']) == 0) { $this->exported_variables['$product_table_header'] = ''; } return $this; } private function processVariables($input_variables, $variables):string { $output = ''; foreach (array_values($input_variables) as $value) { if (array_key_exists($value, $variables)) { $output .= $variables[$value]; } } return $output; } private function processLabels($input_variables, $variables):string { $output = ''; foreach (array_keys($input_variables) as $value) { if (array_key_exists($value, $variables)) { $tmp = str_replace("", "_label", $variables[$value]); $output .= $tmp; } } return $output; } private function clientDetails(Company $company) { $data = [ '$client.name' => '
$client.name
', '$client.id_number' => '$client.id_number
', '$client.vat_number' => '$client.vat_number
', '$client.address1' => '$client.address1
', '$client.address2' => '$client.address2
', '$client.city_state_postal' => '$client.city_state_postal
', '$client.postal_city_state' => '$client.postal_city_state
', '$client.country' => '$client.country
', '$contact.email' => '$client.email
', '$client.custom1' => '$client.custom1
', '$client.custom2' => '$client.custom2
', '$client.custom3' => '$client.custom3
', '$client.custom4' => '$client.custom4
', '$contact.contact1' => '$contact.custom1
', '$contact.contact2' => '$contact.custom2
', '$contact.contact3' => '$contact.custom3
', '$contact.contact4' => '$contact.custom4
', ]; return $this->processCustomFields($company, $data); } private function companyDetails(Company $company) { $data = [ '$company.name' => '$company.name', '$company.id_number' => '$company.id_number', '$company.vat_number' => '$company.vat_number', '$company.website' => '$company.website', '$company.email' => '$company.email', '$company.phone' => '$company.phone', '$company.company1' => '$company1', '$company.company2' => '$company2', '$company.company3' => '$company3', '$company.company4' => '$company4', ]; return $this->processCustomFields($company, $data); } private function companyAddress(Company $company) { $data = [ '$company.address1' => '$company.address1', '$company.address2' => '$company.address2', '$company.city_state_postal' => '$company.city_state_postal', '$company.postal_city_state' => '$company.postal_city_state', '$company.country' => '$company.country', '$company.company1' => '$company1', '$company.company2' => '$company2', '$company.company3' => '$company3', '$company.company4' => '$company4', ]; return $this->processCustomFields($company, $data); } private function invoiceDetails(Company $company) { $data = [ '$invoice.number' => '$invoice.number_label: $invoice.number', '$invoice.po_number' => '$invoice.po_number_label: $invoice.po_number', '$invoice.date' => '$invoice.date_label: $invoice.date', '$invoice.due_date' => '$invoice.due_date_label: $invoice.due_date', '$invoice.balance_due' => '$invoice.balance_due_label: $invoice.balance_due', '$invoice.total' => '$invoice.total_label: $invoice.total', '$invoice.partial_due' => '$invoice.partial_due_label: $invoice.partial_due', '$invoice.custom1' => '$invoice1_label: $invoice.custom1', '$invoice.custom2' => '$invoice2_label: $invoice.custom2', '$invoice.custom3' => '$invoice3_label: $invoice.custom3', '$invoice.custom4' => '$invoice4_label: $invoice.custom4', '$surcharge1' => '$surcharge1_label: $surcharge1', '$surcharge2' => '$surcharge2_label: $surcharge2', '$surcharge3' => '$surcharge3_label: $surcharge3', '$surcharge4' => '$surcharge4_label: $surcharge4', ]; return $this->processCustomFields($company, $data); } private function quoteDetails(Company $company) { $data = [ '$quote.quote_number' => '$quote.number_label: $quote.number', '$quote.po_number' => '$quote.po_number_label: $quote.po_number', '$quote.quote_date' => '$quote.date_label: $quote.date', '$quote.valid_until' => '$quote.valid_until_label: $quote.valid_until', '$quote.balance_due' => '$quote.balance_due_label: $quote.balance_due', '$quote.quote_total' => '$quote.total_label: $quote.total', '$quote.partial_due' => '$quote.partial_due_label: $quote.partial_due', '$quote.custom1' => '$quote.custom1_label: $quote.custom1', '$quote.custom2' => '$quote.custom2_label: $quote.custom2', '$quote.custom3' => '$quote.custom3_label: $quote.custom3', '$quote.custom4' => '$quote.custom4_label: $quote.custom4', '$quote.surcharge1' => '$surcharge1_label: $surcharge1', '$quote.surcharge2' => '$surcharge2_label: $surcharge2', '$quote.surcharge3' => '$surcharge3_label: $surcharge3', '$quote.surcharge4' => '$surcharge4_label: $surcharge4', ]; return $this->processCustomFields($company, $data); } private function creditDetails(Company $company) { $data = [ '$credit.credit_number' => '$credit.number_label$credit.number', '$credit.po_number' => '$credit.po_number_label$credit.po_number', '$credit.credit_date' => '$credit.date_label$credit.date', '$credit.credit_balance' => '$credit.balance_label$credit.balance', '$credit.credit_amount' => '$credit.amount_label$credit.amount', '$credit.partial_due' => '$credit.partial_due_label$credit.partial_due', '$credit.custom1' => '$credit.custom1_label$credit.custom1', '$credit.custom2' => '$credit.custom2_label$credit.custom2', '$credit.custom3' => '$credit.custom3_label$credit.custom3', '$credit.custom4' => '$credit.custom4_label$credit.custom4', '$credit.surcharge1' => '$surcharge1_label$surcharge1_label: $surcharge1', '$credit.surcharge2' => '$surcharge2_label$surcharge2_label: $surcharge2', '$credit.surcharge3' => '$surcharge3_label$surcharge3_label: $surcharge3', '$credit.surcharge4' => '$surcharge4_label$surcharge4_label: $surcharge4', ]; return $this->processCustomFields($company, $data); } private function processCustomFields(Company $company, $data) { $custom_fields = $company->custom_fields; if (!$custom_fields) { return $data; } foreach (self::$custom_fields as $cf) { if (!property_exists($custom_fields, $cf) || (strlen($custom_fields->{$cf}) == 0)) { unset($data[$cf]); } } return $data; } // private function processInputVariables($company, $variables) // { // if(is_object($variables)) // $variables = json_decode(json_encode($variables),true); // $custom_fields = $company->custom_fields; // $matches = array_intersect(self::$custom_fields, $variables); // foreach ($matches as $match) { // if (!property_exists($custom_fields, $match) || (strlen($custom_fields->{$match}) == 0)) { // foreach ($variables as $key => $value) { // if ($value == $match) { // unset($variables[$key]); // } // } // } // } // return $variables; // } }