| 1: | <?php |
| 2: | namespace TIC\CoreBundle\Base; |
| 3: | |
| 4: | use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 5: | |
| 6: | use Symfony\Component\Form\FormInterface; |
| 7: | use Symfony\Component\HttpFoundation\Request; |
| 8: | use Symfony\Component\HttpFoundation\RedirectResponse; |
| 9: | use Symfony\Component\HttpFoundation\StreamedResponse; |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | abstract class TICController extends AbstractController |
| 16: | { |
| 17: | |
| 18: | use \TIC\CoreBundle\Traits\ContextProperties; |
| 19: | use \TIC\CoreBundle\Traits\ControllerRouting; |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | protected function getForm(mixed $data = null, ?Request $request = null, ?array $options = []): FormInterface |
| 32: | { |
| 33: | |
| 34: | |
| 35: | |
| 36: | if (isset($options['form_name'])) { |
| 37: | $name = $options['form_name']; unset($options['form_name']); |
| 38: | $form = $this->container->get('form.factory')->createNamed($name, $this->ctxForm, $data, $options); |
| 39: | } else { |
| 40: | $form = $this->container->get('form.factory')->create($this->ctxForm, $data, $options); |
| 41: | } |
| 42: | |
| 43: | |
| 44: | if ($request !== null) $form->handleRequest($request); |
| 45: | |
| 46: | return $form; |
| 47: | } |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | protected function trans(mixed $message, array $parameters = []): mixed |
| 60: | { |
| 61: | if (\is_array($message)) { |
| 62: | foreach ($message as $key => $val) $message[$key] = $this->get('translator')->trans($val, $parameters); |
| 63: | return $message; |
| 64: | } |
| 65: | return $this->get('translator')->trans($message, $parameters); |
| 66: | } |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | protected function mesg(string $mesg): string |
| 72: | { |
| 73: | return $this->ctxTrans . $mesg; |
| 74: | } |
| 75: | |
| 76: | |
| 77: | |
| 78: | |
| 79: | protected function getItemLabel(bool $pluriel = false): string |
| 80: | { |
| 81: | return $this->trans(\sprintf('%s.names.%s', $this->ctxTrans, ($pluriel) ? 'plur' : 'sing')); |
| 82: | } |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | protected function alert(string $type, mixed $message, mixed $redirect = null): ?RedirectResponse |
| 95: | { |
| 96: | $this->addFlash($type, $message); |
| 97: | return $this->redirectTo($redirect); |
| 98: | } |
| 99: | |
| 100: | |
| 101: | |
| 102: | |
| 103: | |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | protected function fileHeaders(string $filepath, ?string $filename, $mimetype, ?bool $inline): array |
| 112: | { |
| 113: | if ($filepath === null) throw new \Exception('alert.file.download.notexist'); |
| 114: | if (! \file_exists($filepath)) throw new \Exception('alert.file.download.notfound'); |
| 115: | if ($filename === null) $filename = \basename($filepath); |
| 116: | |
| 117: | if ($mimetype === null) $mimetype = ($inline) ? true : "application/octet-stream"; |
| 118: | if ($mimetype === true) { |
| 119: | $fileinst = new \Symfony\Component\HttpFoundation\File\File($filepath); |
| 120: | $mimetype = $fileinst->getMimeType(); |
| 121: | } |
| 122: | $disposition = ($inline) ? "inline" : "attachment"; |
| 123: | |
| 124: | return array( |
| 125: | 'Content-Type' => $mimetype, |
| 126: | 'Content-Disposition' => $disposition . '; filename="'. $filename .'"', |
| 127: | 'Content-Length' => \filesize($filepath), |
| 128: | ); |
| 129: | } |
| 130: | |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | protected function fileDownload(string $filepath, ?string $filename=null, ?bool $inline=false, $mimetype=null): Response |
| 141: | { |
| 142: | $headers = $this->fileHeaders($filepath, $filename, $mimetype, $inline); |
| 143: | return new Response(\file_get_contents($filepath), 200, $headers); |
| 144: | } |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | protected function fileStream(string $filepath, ?string $filename=null, ?bool $inline=false, $mimetype=null, ?bool $delete=false): StreamedResponse |
| 157: | { |
| 158: | $headers = $this->fileHeaders($filepath, $filename, $mimetype, $inline); |
| 159: | return new StreamedResponse(function() use ($filepath, $delete) { |
| 160: | \readfile($filepath); |
| 161: | if ($delete === true) @\unlink($filepath); |
| 162: | }, 200, $headers); |
| 163: | } |
| 164: | |
| 165: | |
| 166: | |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | protected function checkCSRF(string $name, ?Request $request = null): ?bool |
| 175: | { |
| 176: | if (empty($request)) return null; |
| 177: | $token = $request->request->get('_token'); |
| 178: | if ($token === null) $token = $request->query->get('_token'); |
| 179: | return $this->isCsrfTokenValid($name, $token); |
| 180: | } |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | protected function checkRole(mixed $roles, mixed $exception = false): bool |
| 190: | { |
| 191: | if (! \is_array($roles)) $roles = \explode(',', $roles); |
| 192: | foreach ($roles as $role) { |
| 193: | if ($this->isGranted('ROLE_' . $role)) return true; |
| 194: | } |
| 195: | if ($exception === false) return false; |
| 196: | if ($exception === true) $exception = 'error.role.access_denied'; |
| 197: | throw $this->createAccessDeniedException($this->trans($exception)); |
| 198: | } |
| 199: | |
| 200: | } |
| 201: | |