2019-11-20 06:41:49 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2020-01-07 01:13:47 +01:00
|
|
|
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
2019-11-20 06:41:49 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Utils\Traits;
|
|
|
|
|
2020-03-07 07:31:26 +01:00
|
|
|
use App\Designs\Designer;
|
2020-02-10 10:53:02 +01:00
|
|
|
use Illuminate\Support\Facades\App;
|
2019-11-20 06:41:49 +01:00
|
|
|
use Illuminate\Support\Facades\Blade;
|
2020-03-25 03:50:08 +01:00
|
|
|
use Illuminate\Support\Facades\File;
|
2019-11-20 06:41:49 +01:00
|
|
|
use Symfony\Component\Debug\Exception\FatalThrowableError;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class MakesInvoiceHtml.
|
|
|
|
*/
|
|
|
|
trait MakesInvoiceHtml
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2019-12-30 22:59:12 +01:00
|
|
|
* Generate the HTML invoice parsing variables
|
2019-11-20 06:41:49 +01:00
|
|
|
* and generating the final invoice HTML
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-11-20 06:41:49 +01:00
|
|
|
* @param string $design either the path to the design template, OR the full design template string
|
|
|
|
* @param Collection $invoice The invoice object
|
2020-03-21 06:37:30 +01:00
|
|
|
*
|
2020-03-16 11:12:10 +01:00
|
|
|
* @deprecated replaced by generateEntityHtml
|
2020-03-21 06:37:30 +01:00
|
|
|
*
|
2019-11-20 06:41:49 +01:00
|
|
|
* @return string The invoice string in HTML format
|
|
|
|
*/
|
2020-03-07 07:31:26 +01:00
|
|
|
public function generateEntityHtml(Designer $designer, $entity, $contact = null) :string
|
|
|
|
{
|
|
|
|
$entity->load('client');
|
|
|
|
|
|
|
|
$client = $entity->client;
|
|
|
|
|
|
|
|
App::setLocale($client->preferredLocale());
|
|
|
|
|
|
|
|
$labels = $entity->makeLabels();
|
|
|
|
$values = $entity->makeValues($contact);
|
|
|
|
|
|
|
|
$data = [];
|
|
|
|
$data['entity'] = $entity;
|
|
|
|
$data['lang'] = $client->preferredLocale();
|
2020-03-24 14:25:20 +01:00
|
|
|
$data['includes'] = $designer->init()->getIncludes()->getHtml();
|
|
|
|
$data['header'] = $designer->init()->getHeader()->getHtml();
|
|
|
|
$data['body'] = $designer->init()->getBody()->getHtml();
|
|
|
|
$data['footer'] = $designer->init()->getFooter()->getHtml();
|
2020-03-07 07:31:26 +01:00
|
|
|
|
2020-03-16 11:12:10 +01:00
|
|
|
$html = view('pdf.stub', $data)->render();
|
|
|
|
|
2020-03-24 14:25:20 +01:00
|
|
|
$html = $this->parseLabelsAndValues($labels, $values, $html);
|
2020-03-12 11:50:40 +01:00
|
|
|
|
2020-03-16 11:12:10 +01:00
|
|
|
return $html;
|
2020-03-07 07:31:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function parseLabelsAndValues($labels, $values, $section) :string
|
|
|
|
{
|
|
|
|
$section = str_replace(array_keys($labels), array_values($labels), $section);
|
|
|
|
$section = str_replace(array_keys($values), array_values($values), $section);
|
|
|
|
return $section;
|
|
|
|
}
|
|
|
|
|
2019-11-20 06:41:49 +01:00
|
|
|
/**
|
|
|
|
* Parses the blade file string and processes the template variables
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-11-20 06:41:49 +01:00
|
|
|
* @param string $string The Blade file string
|
|
|
|
* @param array $data The array of template variables
|
|
|
|
* @return string The return HTML string
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-11-20 06:41:49 +01:00
|
|
|
*/
|
2020-03-07 07:31:26 +01:00
|
|
|
public function renderView($string, $data = []) :string
|
2019-11-20 06:41:49 +01:00
|
|
|
{
|
|
|
|
$data['__env'] = app(\Illuminate\View\Factory::class);
|
|
|
|
|
|
|
|
$php = Blade::compileString($string);
|
|
|
|
|
|
|
|
$obLevel = ob_get_level();
|
|
|
|
ob_start();
|
|
|
|
extract($data, EXTR_SKIP);
|
|
|
|
|
|
|
|
try {
|
|
|
|
eval('?' . '>' . $php);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
while (ob_get_level() > $obLevel) {
|
|
|
|
ob_end_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
} catch (\Throwable $e) {
|
|
|
|
while (ob_get_level() > $obLevel) {
|
|
|
|
ob_end_clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new FatalThrowableError($e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ob_get_clean();
|
|
|
|
}
|
2020-03-25 03:50:08 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the base template we will be using.
|
|
|
|
*/
|
|
|
|
public function getTemplate(string $template = 'plain')
|
|
|
|
{
|
|
|
|
return File::get(resource_path('views/email/template/' . $template . '.blade.php'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTemplatePath(string $template = 'plain')
|
|
|
|
{
|
|
|
|
return 'email.template.' . $template;
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|