<?php
namespace App\Controller;
use App\Entity\Customer;
use App\Entity\User;
use Doctrine\Persistence\ObjectRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
class AbstractBaseController extends AbstractController
{
const NB_RESULTS_PER_PAGE = 10;
const PATTERN_URL = '^(https?:\/\/[-\w.]+(:\d+)?(\/([\w/\'_%éè+&.()-]*(\?[\w\d=_%éè+&.()-]+)?(#[\w\d]+)?)?)?)$';
const PATTERN_EMAIL = '^([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)$';
const PATTERN_PHONE = '^((\+\d{2})|0)\d ?(\d){2} ?(\d){2} ?(\d){2} ?(\d){2}$';
const PATTERN_IBAN = '^[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}$';
const PATTERN_BIC = '^[a-zA-Z]{4}[a-zA-Z]{2}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?$';
const PATTERN_SOCIAL_INSURANCE = '^([12])([0-9]{2})(0[1-9]|[1][0-2])([0-9]{2}|2A|2B)([0-9]{3})([0-9]{3})([0-9]{2})$';
const SESS_KEY_SEARCH_IDS = 'searchIds';
const SESS_KEY_SEARCH_PARAMS = 'searchParams';
const SESS_KEY_BATCH_IDS = 'batchIds';
const SESS_KEY_HIDE_AGENDA = 'hideAgenda';
/**
* @return User|Customer|object|null
*/
protected function getSfUser() {
return parent::getUser();
}
/**
* @param Request $oRequest
*
* @return array
*/
protected function getBatchItems(Request $oRequest) : array
{
return $oRequest->getSession()->get(static::SESS_KEY_BATCH_IDS, []);
}
/**
* @param Request $oRequest
* @param int $iId
*
* @return int
*/
protected function toggleBatchItem(Request $oRequest, int $iId) : int
{
$aIds = $this->getBatchItems($oRequest);
if (in_array($iId, $aIds)) {
$iIdx = array_search($iId, $aIds);
unset($aIds[$iIdx]);
} else {
$aIds[] = $iId;
}
$this->setBatchItems($oRequest, $aIds);
return count($aIds);
}
/**
* @param Request $oRequest
* @param array $aData
*/
protected function setBatchItems(Request $oRequest, array $aData) : void
{
$oRequest->getSession()->set(static::SESS_KEY_BATCH_IDS, $aData);
}
/**
*
* @param mixed $mEntity
*/
protected function persistAndFlush($mEntity)
{
$aEntities = is_array($mEntity) ? $mEntity : [$mEntity];
$em = $this->getDoctrine()->getManager();
foreach ($aEntities as $oEntity) {
$em->persist($oEntity);
}
$em->flush();
}
/**
*
* @param mixed $mEntity
*/
protected function removeAndFlush($mEntity)
{
$aEntities = is_array($mEntity) ? $mEntity : [$mEntity];
$em = $this->getDoctrine()->getManager();
foreach ($aEntities as $oEntity) {
$em->remove($oEntity);
}
$em->flush();
}
/**
* @param string $sPersistentObject
*
* @return ObjectRepository
*/
protected function getDoctrineRepository(string $sPersistentObject): ObjectRepository
{
return $this->getDoctrine()->getRepository($sPersistentObject);
}
/**
* @param array $aOldData
* @param array $aNewData
*
* @return string
*/
protected function getArrayDiffLog(array $aOldData, array $aNewData) : string
{
$aOldData = array_map(function ($v) {
return is_array($v) ? json_encode($v) : $v;
}, $aOldData);
$aNewData = array_map(function ($v) {
return is_array($v) ? json_encode($v) : $v;
}, $aNewData);
$aChanges = array_diff_assoc($aNewData, $aOldData);
$sDetail = '';
foreach ($aChanges as $sKey => $sValue) {
$sDetail .= "\n- ". $sKey .' : '. str_replace("\r\n", ' ', $sValue);
}
return $sDetail;
}
/**
* @param int|float $mPrice
*
* @return string
*/
protected function formatPrice($mPrice): string
{
return number_format($mPrice, 2, '.', ' '). ' €';
}
/**
* @param mixed $mDate
*
* @return string
*/
protected function getDateUS($mDate): ?string
{
if (!$mDate) {
return null;
}
elseif ($mDate instanceof \DateTime) {
return $mDate->format('Y-m-d');
}
elseif (strpos($mDate, '-') !== FALSE) {
return $mDate;
}
elseif (strpos($mDate, '/') !== FALSE) {
$aParts = explode('/', $mDate);
$sYear = date('Y');
$sMonth = $sDay = '01';
if (count($aParts) === 3) {
[$sDay, $sMonth, $sYear] = $aParts;
}
elseif (count($aParts) === 2) {
[$sDay, $sMonth] = $aParts;
}
return implode('-', [$sYear, $sMonth, $sDay]);
}
return $mDate;
}
/**
* @param mixed $mDate
*
* @return \DateTime
* @throws \Exception
*/
protected function getDatetime($mDate): ?\DateTime
{
if (!$mDate) {
return null;
}
elseif ($mDate instanceof \DateTime) {
return $mDate;
}
elseif (strpos($mDate, '-') !== FALSE) {
return new \DateTime($mDate);
}
elseif (strpos($mDate, '/') !== FALSE) {
$aParts = explode('/', $mDate);
$sYear = date('Y');
$sMonth = $sDay = '01';
if (count($aParts) === 3) {
[$sDay, $sMonth, $sYear] = $aParts;
}
elseif (count($aParts) === 2) {
[$sDay, $sMonth] = $aParts;
}
return new \DateTime($sYear.'-'.$sMonth.'-'.$sDay);
}
return $mDate;
}
/**
* @return FilesystemAdapter
*/
protected function getEnvFilesystemAdapter(): FilesystemAdapter
{
return new FilesystemAdapter($this->getParameter('app_secret'));
}
/**
* @param Response $oResponse
*
* @return Response
*/
protected function cacheResponse(Response $oResponse): Response
{
$oResponse->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');
return $oResponse;
}
/**
* @param array $aObjects
* @param array $aOrderIds
*
* @return array
*/
protected function orderObjectsByIds(array $aObjects, array $aOrderIds) : array
{
$aOrderedObjects = [];
foreach ($aOrderIds as $iId) {
foreach ($aObjects as $iIdx => $oObject) {
if ($oObject->getId() === $iId) {
$aOrderedObjects[] = $oObject;
unset($aObjects[$iIdx]);
break;
}
}
}
return $aOrderedObjects;
}
}