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;
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Blade;
|
|
|
|
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
|
2019-12-30 22:59:12 +01:00
|
|
|
*
|
2019-11-20 06:41:49 +01:00
|
|
|
* @return string The invoice string in HTML format
|
|
|
|
*/
|
|
|
|
public function generateInvoiceHtml($design, $invoice) :string
|
|
|
|
{
|
|
|
|
$variables = array_merge($invoice->makeLabels(), $invoice->makeValues());
|
2019-12-18 11:49:28 +01:00
|
|
|
|
2019-12-30 22:59:12 +01:00
|
|
|
// uasort($variables, 'arraySort');
|
2019-12-18 11:49:28 +01:00
|
|
|
|
2019-11-20 06:41:49 +01:00
|
|
|
$design = str_replace(array_keys($variables), array_values($variables), $design);
|
|
|
|
|
|
|
|
$data['invoice'] = $invoice;
|
|
|
|
|
|
|
|
return $this->renderView($design, $data);
|
|
|
|
|
|
|
|
//return view($design, $data)->render();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
*/
|
|
|
|
public function renderView($string, $data) :string
|
|
|
|
{
|
|
|
|
if (!$data) {
|
2019-12-30 22:59:12 +01:00
|
|
|
$data = [];
|
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();
|
|
|
|
}
|
2019-12-30 22:59:12 +01:00
|
|
|
}
|