<?php
//----------------------------------------------------------------------
// src/Twig/StyleExtension.php
//----------------------------------------------------------------------
namespace App\Twig;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use App\Entity\Client\Client;
use App\Entity\Client\Zone;
use App\Entity\Common\Attachment;
use App\Entity\Equipment\Vehicle;
use App\Entity\HR\Application\Application;
use App\Entity\HR\HumanResource;
use App\Entity\Ikea\ServiceOrder;
use App\Entity\Ikea\ServiceOrderUpdate;
use App\Entity\Location\Address;
use App\Entity\Mission\Mission;
use App\Entity\Planning\ExternalTask;
use App\Entity\Planning\Task;
use App\Entity\Platform\Command\Command;
use App\Entity\Platform\Cost\Cost;
use App\Entity\Platform\Demand\Demand;
use App\Entity\Platform\Devis\Devis;
use App\Entity\Platform\Devis\ExternalDevis;
use App\Entity\Platform\ExternalMessage\ExternalMessage;
use App\Entity\Platform\Invoice\ExternalInvoice;
use App\Entity\Platform\Invoice\Invoice;
use App\Entity\Platform\Society;
use App\Entity\Platform\SocietyGroup\SocietyGroupCommand;
use App\Entity\Platform\SocietyGroup\SocietyGroupInvoice;
use App\Entity\Platform\Supplier;
use App\Entity\Product\Product;
use App\Entity\Product\Template;
use App\Entity\SocietyGroup;
use App\Entity\Webapp\Document;
use App\Entity\Webapp\ExternalDocument;
use App\Services\LogTools;
class StyleExtension extends AbstractExtension
{
public function __construct(Security $security, UrlGeneratorInterface $router, TranslatorInterface $translator, LogTools $logTools)
{
$this->security = $security;
$this->router = $router;
$this->translator = $translator;
$this->logTools = $logTools;
}
public function getFilters(): array
{
return [
new TwigFilter('style_address', [$this, 'styleAddress']),
new TwigFilter('style_address_gps', [$this, 'styleAddressGps']),
new TwigFilter('style_application', [$this, 'styleApplication']),
new TwigFilter('style_attachment', [$this, 'styleAttachment']),
new TwigFilter('style_client', [$this, 'styleClient']),
new TwigFilter('style_client_list', [$this, 'styleClientList']),
new TwigFilter('style_command', [$this, 'styleCommand']),
new TwigFilter('style_cost', [$this, 'styleCost']),
new TwigFilter('style_demand', [$this, 'styleDemand']),
new TwigFilter('style_devis', [$this, 'styleDevis']),
new TwigFilter('style_document', [$this, 'styleDocument']),
new TwigFilter('style_external_devis', [$this, 'styleExternalDevis']),
new TwigFilter('style_external_document', [$this, 'styleExternalDocument']),
new TwigFilter('style_external_invoice', [$this, 'styleExternalInvoice']),
new TwigFilter('style_external_message', [$this, 'styleExternalMessage']),
new TwigFilter('style_external_task', [$this, 'styleExternalTask']),
new TwigFilter('style_human_resource', [$this, 'styleHumanResource']),
new TwigFilter('style_invoice', [$this, 'styleInvoice']),
new TwigFilter('style_mission', [$this, 'styleMission']),
new TwigFilter('style_society', [$this, 'styleSociety']),
new TwigFilter('style_society_group', [$this, 'styleSocietyGroup']),
new TwigFilter('style_society_group_invoice', [$this, 'styleSocietyGroupInvoice']),
new TwigFilter('style_society_group_command', [$this, 'styleSocietyGroupCommand']),
new TwigFilter('style_supplier', [$this, 'styleSupplier']),
new TwigFilter('style_task', [$this, 'styleTask']),
new TwigFilter('style_template', [$this, 'styleTemplate']),
new TwigFilter('style_product', [$this, 'styleProduct']),
new TwigFilter('style_vehicle', [$this, 'styleVehicle']),
new TwigFilter('style_ikea_service_order', [$this, 'styleIkeaServiceOrder']),
new TwigFilter('style_ikea_service_order_update', [$this, 'styleIkeaServiceOrderUpdate']),
new TwigFilter('style_zone', [$this, 'styleZone']),
];
}
/**
* @param Zone|null $object
* @return string
*/
public function styleZone($object)
{
if (!($object instanceof Zone))
{
return "";
}
// if ($object->isEmptyValue())
// {
// return $object->display();
// }
$url = $this->router->generate('icod_platform_zone_view', array(
'id' => $object->getId(),
));
$print = '<a href="' . $url . '">';
$print .= $object->display();
$print .= '</a>';
return $print;
}
/**
* @param Product|null $object
* @param bool $displayFull = true
* @param bool $isGranted = true : If true, permission to view the object is checked
* If false, link is displayed witout checking permission
* @return string
*/
public function styleProduct($object, bool $displayFull = true, bool $isGranted = true)
{
if (!($object instanceof Product))
{
return "";
}
$print = "";
$display = $object->displayFull();
if (!$displayFull)
{
$display = $object->getRef();
}
// Check Permissions ?
if ($isGranted)
{
if ($this->security->isGranted('view_product', $object))
{
$url = $this->router->generate('icod_platform_product_view', array(
'id' => $object->getId(),
));
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
$url = $this->router->generate('icod_platform_product_view', array(
'id' => $object->getId(),
));
$print = '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
return $print;
}
/**
* @param Template|null $object
* @param bool $isGranted = true : If true, permission to view the object is checked
* If false, link is displayed witout checking permission
* @return string
*/
public function styleTemplate($object, $isGranted = true): string
{
if (!($object instanceof Template))
{
return "";
}
$print = "";
$display = $object->display();
// Check Permissions ?
if ($isGranted)
{
if ($this->security->isGranted('view_template', $object))
{
$url = $this->router->generate('icod_platform_template_view', array(
'id' => $object->getId(),
));
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
$url = $this->router->generate('icod_platform_template_view', array(
'id' => $object->getId(),
));
$print = '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
return $print;
}
public function styleIkeaServiceOrder($object, bool $displayFull = false)
{
if (!($object instanceof ServiceOrder))
{
return "";
}
if ($displayFull)
{
$display = $object->displayOp();
}
else
{
$display = $object->getRef();
}
$print = '';
if ($this->security->isGranted('view_ikea_service_order', $object))
{
$url = $this->router->generate('icod_platform_ikea_service_order_view', array(
'id' => $object->getId(),
));
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleIkeaServiceOrderUpdate($object)
{
if (!($object instanceof ServiceOrderUpdate))
{
return "";
}
// Build display
$display = "";
if ($object->getCreationDate() !== null)
{
$display .= "[" . $object->getCreationDate()->format("d/m/Y H:i:s") . "]";
}
if (!empty($display)) $display .= " ";
$display .= $object->getOrderNumber();
$display .= ", version " . $object->getVersion();
// Build html
$print = '';
if ($this->security->isGranted('view_ikea_service_order', $object->getServiceOrder()))
{
$url = $this->router->generate('icod_platform_ikea_service_order_update_view', array(
'id' => $object->getId(),
));
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleSocietyGroup($object, bool $isAdmin = false): string
{
if (!($object instanceof SocietyGroup))
{
return "";
}
$display = $object->display();
if ($isAdmin)
{
$url = $this->router->generate('icod_admin_society_group_view', array(
'id' => $object->getId(),
));
}
else
{
$url = $this->router->generate('icod_platform_society_group_view', array(
'id' => $object->getId(),
));
}
$print = '';
if ($this->security->isGranted('view_society_group', $object))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleVehicle($object): string
{
if (!($object instanceof Vehicle))
{
return "";
}
$display = $object->display();
$url = $this->router->generate('icod_equipment_vehicle_view', array(
'id' => $object->getId(),
));
$print = '';
if ($this->security->isGranted('view_equipment', $object->getEquipment()))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleHumanResource($object, $withDisplay = false): string
{
if (!($object instanceof HumanResource))
{
return "";
}
if ($withDisplay)
{
$display = $object->display();
}
else
{
$display = $object->getEmail();
}
$url = $this->router->generate('icod_platform_human_resource_view', array(
'id' => $object->getId(),
));
$print = '';
if ($this->security->isGranted('view_human_resource', $object))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleAttachment($object, string $textToDisplay = '', bool $withOwner = false): string
{
if (!($object instanceof Attachment))
{
return "";
}
if (!empty($textToDisplay))
{
$display = $textToDisplay;
}
else
{
$display = $object->display();
}
$url = $this->router->generate('icod_platform_attachment_view', array(
'id' => $object->getId(),
));
// Build the print for twig
$print = '';
if ($this->security->isGranted('view_attachment', $object))
{
$print .= '<a href="' . $url . '" rel="noreferrer" target="_blank">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
if ($withOwner)
{
if ($object->getMission() !== null)
{
$ref = $object->getMission()->getRef();
$print .= " [$ref]";
}
else
{
if ($object->getDevis() !== null)
{
$ref = $object->getDevis()->getRef();
$print .= " [$ref]";
}
}
}
return $print;
}
public function styleTask($object): string
{
if (!($object instanceof Task))
{
return "";
}
$display = $object->display();
$id = $object->getId();
$print = '';
if ($this->security->isGranted('view_task', $object) || $this->security->isGranted('edit_task', $object))
{
$print .= '<a href="javascript:void(0);" onclick="openTask(' . $id . ')">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleSociety($object): string
{
if (!($object instanceof Society))
{
return "";
}
$display = $object->display();
$url = $this->router->generate('icod_platform_society_view', array(
'id' => $object->getId(),
));
$print = '';
if ($this->security->isGranted('view_society', $object))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleClient($object): string
{
if (!($object instanceof Client))
{
return "";
}
$display = $object->display();
$url = $this->router->generate('icod_platform_client_view', array(
'id' => $object->getId(),
));
$print = '';
if ($this->security->isGranted('view_client', $object))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleClientList($object): string
{
if (!($object instanceof Client))
{
return "";
}
$url = "";
$translation = "";
$print = "";
if ($object->getIndividual() !== null)
{
if ($object->isProspect())
{
$url = $this->router->generate('icod_platform_client_prospect_list');
$translation = $this->translator->trans('prospects');
}
else
{
$url = $this->router->generate('icod_platform_client_individual_list');
$translation = $this->translator->trans('clients');
}
}
else
{
if ($object->getStore() !== null)
{
$url = $this->router->generate('icod_platform_client_store_list');
$translation = $this->translator->trans('emitters');
}
}
if (!empty($url) && !empty($translation))
{
$print .= '<a href="' . $url . '">';
$print .= $translation;
$print .= '</a>';
}
return $print;
}
public function styleDocument($object): string
{
if (!($object instanceof Document))
{
return "";
}
$display = $object->display();
$url = $this->router->generate('icod_pdf_document_create_pdf', array(
'id' => $object->getId(),
));
$print = '';
if ($this->security->isGranted('view_pdf_document', $object))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleExternalDevis($object): string
{
if (!($object instanceof ExternalDevis))
{
return "";
}
$display = $object->display();
$url = $this->router->generate('icod_platform_external_devis_view', array(
'id' => $object->getId(),
));
$print = '';
if ($object->getAttachment() !== null && $this->security->isGranted('view_external_devis', $object))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleExternalInvoice($object): string
{
if (!($object instanceof ExternalInvoice))
{
return "";
}
$display = $object->display();
$url = $this->router->generate('icod_platform_external_invoice_view', array(
'id' => $object->getId(),
));
$print = '';
if ($object->getAttachment() !== null && $this->security->isGranted('view_external_invoice', $object))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleExternalDocument($object): string
{
if (!($object instanceof ExternalDocument))
{
return "";
}
$display = $object->display();
$url = $this->router->generate('icod_platform_external_document_view', array(
'id' => $object->getId(),
));
$print = '';
if ($object->getAttachment() !== null && $this->security->isGranted('view_external_document', $object))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleExternalTask($object): string
{
if (!($object instanceof ExternalTask))
{
return "";
}
return $object->display();
}
public function styleDevis($object): string
{
if (!($object instanceof Devis))
{
return "";
}
$display = $object->display();
$url = "";
// Try to find the right route
if ($this->security->isGranted('view_devis', $object))
{
$url = $this->router->generate('icod_platform_devis_view', array(
'id' => $object->getId(),
));
}
else
{
if ($this->security->isGranted('view_pdf_devis_with_price', $object))
{
$url = $this->router->generate('icod_pdf_devis_ht_public_pdf', array(
'id' => $object->getId(),
));
}
else
{
if ($this->security->isGranted('view_pdf_devis_no_price', $object))
{
$url = $this->router->generate('icod_pdf_devis_without_price', array(
'id' => $object->getId(),
));
}
}
}
// Build the returned print
$print = '';
if (!empty($url))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleInvoice($object): string
{
if (!($object instanceof Invoice))
{
return "";
}
$display = $object->display();
$url = "";
// Try to find the right route
if ($this->security->isGranted('view_invoice', $object))
{
$url = $this->router->generate('icod_platform_invoice_view', array(
'id' => $object->getId(),
));
}
else
{
if ($this->security->isGranted('view_pdf_invoice', $object))
{
if ($object->isCredit() or $object->isCreditFree())
{
$url = $this->router->generate('icod_pdf_credit_pdf', array(
'id' => $object->getId(),
));
}
else
{
$url = $this->router->generate('icod_pdf_invoice_pdf', array(
'id' => $object->getId(),
));
}
}
}
// Build the returned print
$print = '';
if (!empty($url))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleSocietyGroupInvoice($object): string
{
if (!($object instanceof SocietyGroupInvoice))
{
return "";
}
$display = $object->display();
$url = "";
// Try to find the right route
if ($this->security->isGranted('view_society_group_invoice', $object))
{
$url = $this->router->generate('icod_platform_society_group_invoice_view', array(
'id' => $object->getId(),
));
}
else
{
if ($this->security->isGranted('view_pdf_society_group_invoice', $object))
{
$url = $this->router->generate('icod_pdf_society_group_invoice_pdf', array(
'id' => $object->getId(),
));
}
}
// Build the returned print
$print = '';
if (!empty($url))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleSocietyGroupCommand($object): string
{
if (!($object instanceof SocietyGroupCommand))
{
return "";
}
$display = $object->display();
$url = "";
// Try to find the right route
if ($this->security->isGranted('view_society_group_command', $object))
{
$url = $this->router->generate('icod_platform_society_group_command_view', array(
'id' => $object->getId(),
));
}
else
{
if ($this->security->isGranted('view_pdf_society_group_command', $object))
{
$url = $this->router->generate('icod_pdf_society_group_command_pdf', array(
'id' => $object->getId(),
));
}
}
// Build the returned print
$print = '';
if (!empty($url))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleMission($object, $withLabel = false): string
{
if (!($object instanceof Mission))
{
return "";
}
if ($withLabel)
{
$display = $object->displayWithTitle();
}
else
{
$display = $object->display();
}
$url = $this->router->generate('icod_platform_mission_view', array(
'id' => $object->getId(),
));
$print = '';
if ($this->security->isGranted('view_mission', $object))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleDemand($object): string
{
if (!($object instanceof Demand))
{
return "";
}
$display = $object->display();
$url = $this->router->generate('icod_platform_demand_view', array(
'id' => $object->getId(),
));
$print = '';
if ($this->security->isGranted('view_demand', $object))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleCommand($object): string
{
if (!($object instanceof Command))
{
return "";
}
$display = $object->display();
$url = $this->router->generate('icod_platform_command_view', array(
'id' => $object->getId(),
));
$print = '';
if ($this->security->isGranted('view_command', $object))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleCost($object): string
{
if (!($object instanceof Cost))
{
return "";
}
$display = $object->display();
$url = $this->router->generate('icod_platform_cost_view', array(
'id' => $object->getId(),
));
$print = '';
if ($this->security->isGranted('view_cost', $object))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleSupplier($object): string
{
if (!($object instanceof Supplier))
{
return "";
}
$display = $object->display();
$url = $this->router->generate('icod_platform_supplier_view', array(
'id' => $object->getId(),
));
$print = '';
if ($this->security->isGranted('view_supplier', $object))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleExternalMessage($object, $emptyText = ''): string
{
if (!($object instanceof ExternalMessage))
{
return "";
}
$display = $object->getRef();
$url = $this->router->generate('icod_platform_external_message_view', array(
'id' => $object->getId(),
));
if (empty($display))
{
$display = $emptyText;
}
$print = '';
if ($this->security->isGranted('view_external_message', $object))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
public function styleApplication($object): string
{
if (!($object instanceof Application))
{
return "";
}
$display = $object->displayName();
$url = $this->router->generate('icod_human_resource_application_view', array(
'id' => $object->getId(),
));
$print = '';
if ($this->security->isGranted('view_application', $object))
{
$print .= '<a href="' . $url . '">';
$print .= $display;
$print .= '</a>';
}
else
{
$print .= $display;
}
return $print;
}
/**
* Format :
* address
* complement
* postal_code, town
* country
*/
public function styleAddress($object, bool $displayEmpty = false, bool $billingAddress = false): string
{
// If $object is null or not an Address Object, return default string ('none_provided' or "")
if (!($object instanceof Address))
{
if ($displayEmpty)
{
$translation = $this->translator->trans('none_provided');
$print = '<span class="text-danger">' . $translation . '</span>';
return $print;
}
return "";
}
$print = "";
if (!empty($object->getName()))
{
$print .= $object->getName();
if (!empty($print))
{
$print .= "<br>";
}
}
if (!empty($object->getAddress()))
{
$print .= $object->getAddress();
}
if (!empty($object->getComplement()))
{
if (!empty($print))
{
$print .= "<br>";
}
$print .= $object->getComplement();
}
if (!empty($print))
{
$print .= "<br>";
}
if (!empty($object->getPostalCode()))
{
$print .= $object->getPostalCode() . ", ";
}
if (!empty($object->getTown()))
{
$print .= $object->getTown();
}
if (!empty($object->getCountry()))
{
if (!empty($print))
{
$print .= "<br>";
}
$print .= $object->getCountry();
}
return $print;
}
/**
* Format :
* address complement postal_code, town country
*/
public function styleAddressGps($object): string
{
// If $object is null or not an Address Object, return default string ('none_provided' or "")
if (!($object instanceof Address))
{
return "";
}
$print = "";
$print = "";
if (!empty($object->getAddress()))
{
$print .= $object->getAddress();
}
if (!empty($object->getComplement()))
{
if (!empty($print))
{
$print .= " ";
}
$print .= $object->getComplement();
}
if (!empty($print))
{
$print .= " ";
}
if (!empty($object->getPostalCode()))
{
$print .= $object->getPostalCode() . ", ";
}
if (!empty($object->getTown()))
{
$print .= $object->getTown();
}
if (!empty($object->getCountry()))
{
if (!empty($print))
{
$print .= " ";
}
$print .= $object->getCountry();
}
return $print;
}
}