src/Controller/AbstractBaseController.php line 115

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Customer;
  4. use App\Entity\User;
  5. use Doctrine\Persistence\ObjectRepository;
  6. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  7. use Symfony\Component\Cache\Adapter\FilesystemAdapter;
  8. use Symfony\Component\HttpFoundation\Request;
  9. use Symfony\Component\HttpFoundation\Response;
  10. use Symfony\Component\HttpKernel\EventListener\AbstractSessionListener;
  11. class AbstractBaseController extends AbstractController
  12. {
  13. const NB_RESULTS_PER_PAGE = 10;
  14. const PATTERN_URL = '^(https?:\/\/[-\w.]+(:\d+)?(\/([\w/\'_%éè+&.()-]*(\?[\w\d=_%éè+&.()-]+)?(#[\w\d]+)?)?)?)$';
  15. const PATTERN_EMAIL = '^([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)$';
  16. const PATTERN_PHONE = '^((\+\d{2})|0)\d ?(\d){2} ?(\d){2} ?(\d){2} ?(\d){2}$';
  17. const PATTERN_IBAN = '^[a-zA-Z]{2}[0-9]{2}[a-zA-Z0-9]{4}[0-9]{7}([a-zA-Z0-9]?){0,16}$';
  18. const PATTERN_BIC = '^[a-zA-Z]{4}[a-zA-Z]{2}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?$';
  19. 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})$';
  20. const SESS_KEY_SEARCH_IDS = 'searchIds';
  21. const SESS_KEY_SEARCH_PARAMS = 'searchParams';
  22. const SESS_KEY_BATCH_IDS = 'batchIds';
  23. const SESS_KEY_HIDE_AGENDA = 'hideAgenda';
  24. /**
  25. * @return User|Customer|object|null
  26. */
  27. protected function getSfUser() {
  28. return parent::getUser();
  29. }
  30. /**
  31. * @param Request $oRequest
  32. *
  33. * @return array
  34. */
  35. protected function getBatchItems(Request $oRequest) : array
  36. {
  37. return $oRequest->getSession()->get(static::SESS_KEY_BATCH_IDS, []);
  38. }
  39. /**
  40. * @param Request $oRequest
  41. * @param int $iId
  42. *
  43. * @return int
  44. */
  45. protected function toggleBatchItem(Request $oRequest, int $iId) : int
  46. {
  47. $aIds = $this->getBatchItems($oRequest);
  48. if (in_array($iId, $aIds)) {
  49. $iIdx = array_search($iId, $aIds);
  50. unset($aIds[$iIdx]);
  51. } else {
  52. $aIds[] = $iId;
  53. }
  54. $this->setBatchItems($oRequest, $aIds);
  55. return count($aIds);
  56. }
  57. /**
  58. * @param Request $oRequest
  59. * @param array $aData
  60. */
  61. protected function setBatchItems(Request $oRequest, array $aData) : void
  62. {
  63. $oRequest->getSession()->set(static::SESS_KEY_BATCH_IDS, $aData);
  64. }
  65. /**
  66. *
  67. * @param mixed $mEntity
  68. */
  69. protected function persistAndFlush($mEntity)
  70. {
  71. $aEntities = is_array($mEntity) ? $mEntity : [$mEntity];
  72. $em = $this->getDoctrine()->getManager();
  73. foreach ($aEntities as $oEntity) {
  74. $em->persist($oEntity);
  75. }
  76. $em->flush();
  77. }
  78. /**
  79. *
  80. * @param mixed $mEntity
  81. */
  82. protected function removeAndFlush($mEntity)
  83. {
  84. $aEntities = is_array($mEntity) ? $mEntity : [$mEntity];
  85. $em = $this->getDoctrine()->getManager();
  86. foreach ($aEntities as $oEntity) {
  87. $em->remove($oEntity);
  88. }
  89. $em->flush();
  90. }
  91. /**
  92. * @param string $sPersistentObject
  93. *
  94. * @return ObjectRepository
  95. */
  96. protected function getDoctrineRepository(string $sPersistentObject): ObjectRepository
  97. {
  98. return $this->getDoctrine()->getRepository($sPersistentObject);
  99. }
  100. /**
  101. * @param array $aOldData
  102. * @param array $aNewData
  103. *
  104. * @return string
  105. */
  106. protected function getArrayDiffLog(array $aOldData, array $aNewData) : string
  107. {
  108. $aOldData = array_map(function ($v) {
  109. return is_array($v) ? json_encode($v) : $v;
  110. }, $aOldData);
  111. $aNewData = array_map(function ($v) {
  112. return is_array($v) ? json_encode($v) : $v;
  113. }, $aNewData);
  114. $aChanges = array_diff_assoc($aNewData, $aOldData);
  115. $sDetail = '';
  116. foreach ($aChanges as $sKey => $sValue) {
  117. $sDetail .= "\n- ". $sKey .' : '. str_replace("\r\n", ' ', $sValue);
  118. }
  119. return $sDetail;
  120. }
  121. /**
  122. * @param int|float $mPrice
  123. *
  124. * @return string
  125. */
  126. protected function formatPrice($mPrice): string
  127. {
  128. return number_format($mPrice, 2, '.', ' '). ' €';
  129. }
  130. /**
  131. * @param mixed $mDate
  132. *
  133. * @return string
  134. */
  135. protected function getDateUS($mDate): ?string
  136. {
  137. if (!$mDate) {
  138. return null;
  139. }
  140. elseif ($mDate instanceof \DateTime) {
  141. return $mDate->format('Y-m-d');
  142. }
  143. elseif (strpos($mDate, '-') !== FALSE) {
  144. return $mDate;
  145. }
  146. elseif (strpos($mDate, '/') !== FALSE) {
  147. $aParts = explode('/', $mDate);
  148. $sYear = date('Y');
  149. $sMonth = $sDay = '01';
  150. if (count($aParts) === 3) {
  151. [$sDay, $sMonth, $sYear] = $aParts;
  152. }
  153. elseif (count($aParts) === 2) {
  154. [$sDay, $sMonth] = $aParts;
  155. }
  156. return implode('-', [$sYear, $sMonth, $sDay]);
  157. }
  158. return $mDate;
  159. }
  160. /**
  161. * @param mixed $mDate
  162. *
  163. * @return \DateTime
  164. * @throws \Exception
  165. */
  166. protected function getDatetime($mDate): ?\DateTime
  167. {
  168. if (!$mDate) {
  169. return null;
  170. }
  171. elseif ($mDate instanceof \DateTime) {
  172. return $mDate;
  173. }
  174. elseif (strpos($mDate, '-') !== FALSE) {
  175. return new \DateTime($mDate);
  176. }
  177. elseif (strpos($mDate, '/') !== FALSE) {
  178. $aParts = explode('/', $mDate);
  179. $sYear = date('Y');
  180. $sMonth = $sDay = '01';
  181. if (count($aParts) === 3) {
  182. [$sDay, $sMonth, $sYear] = $aParts;
  183. }
  184. elseif (count($aParts) === 2) {
  185. [$sDay, $sMonth] = $aParts;
  186. }
  187. return new \DateTime($sYear.'-'.$sMonth.'-'.$sDay);
  188. }
  189. return $mDate;
  190. }
  191. /**
  192. * @return FilesystemAdapter
  193. */
  194. protected function getEnvFilesystemAdapter(): FilesystemAdapter
  195. {
  196. return new FilesystemAdapter($this->getParameter('app_secret'));
  197. }
  198. /**
  199. * @param Response $oResponse
  200. *
  201. * @return Response
  202. */
  203. protected function cacheResponse(Response $oResponse): Response
  204. {
  205. $oResponse->headers->set(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER, 'true');
  206. return $oResponse;
  207. }
  208. /**
  209. * @param array $aObjects
  210. * @param array $aOrderIds
  211. *
  212. * @return array
  213. */
  214. protected function orderObjectsByIds(array $aObjects, array $aOrderIds) : array
  215. {
  216. $aOrderedObjects = [];
  217. foreach ($aOrderIds as $iId) {
  218. foreach ($aObjects as $iIdx => $oObject) {
  219. if ($oObject->getId() === $iId) {
  220. $aOrderedObjects[] = $oObject;
  221. unset($aObjects[$iIdx]);
  222. break;
  223. }
  224. }
  225. }
  226. return $aOrderedObjects;
  227. }
  228. }