| 1: | <?php |
| 2: | namespace TIC\ListBundle\Controller; |
| 3: | |
| 4: | use TIC\CoreBundle\Base\TICController as BaseController; |
| 5: | use TIC\DormBundle\Traits\ControllerCrudable; |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | use Symfony\Component\Routing\Annotation\Route; |
| 11: | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
| 12: | use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; |
| 13: | use Symfony\Component\HttpFoundation\Request; |
| 14: | use Symfony\Component\HttpFoundation\Response; |
| 15: | use Symfony\Component\HttpFoundation\StreamedResponse; |
| 16: | |
| 17: | use TIC\ListBundle\Entity\ListInfo; |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | class ListInfoController extends BaseController |
| 25: | { |
| 26: | use ControllerCrudable; |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | public function list(Request $request): Response { |
| 34: | return $this->crudList(); |
| 35: | } |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | |
| 43: | public function show(Request $request, $id): Response { |
| 44: | return $this->crudShow($id); |
| 45: | } |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | public function create(Request $request): Response { |
| 54: | $item = new ListInfo(2); |
| 55: | $form = $this->getForm($item, $request, array( |
| 56: | )); |
| 57: | if($form->isSubmitted()) { |
| 58: | if ($form->isValid()) { |
| 59: | $item->generateDefaultItemsData(); |
| 60: | return $this->saveItem($item, true, true); |
| 61: | } |
| 62: | } |
| 63: | return $this->renderAction('form', array( |
| 64: | 'form' => $form->createView(), |
| 65: | 'item' => $item, |
| 66: | )); |
| 67: | } |
| 68: | |
| 69: | |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | public function update(Request $request, $id) { |
| 76: | $item = $this->getItem($id); |
| 77: | $form = $this->getForm($item, $request, array( |
| 78: | 'allow_cfg' => $this->get('security.authorization_checker')->isGranted('ROLE_TICLIST_UPDATE'), |
| 79: | 'allow_mod' => $this->get('security.authorization_checker')->isGranted('ROLE_TICLIST_MOD'), |
| 80: | 'allow_add' => $this->get('security.authorization_checker')->isGranted('ROLE_TICLIST_ADD'), |
| 81: | 'allow_del' => $this->get('security.authorization_checker')->isGranted('ROLE_TICLIST_DEL'), |
| 82: | )); |
| 83: | if($form->isSubmitted()) { |
| 84: | if ($form->isValid()) { |
| 85: | $item->generateDefaultItemsData(); |
| 86: | return $this->saveItem($item, true, true); |
| 87: | } |
| 88: | } |
| 89: | return $this->renderAction('form', array( |
| 90: | 'form' => $form->createView(), |
| 91: | 'item' => $item, |
| 92: | )); |
| 93: | } |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | |
| 101: | public function delete(Request $request, $id) { |
| 102: | return $this->crudDelete($request, $id); |
| 103: | } |
| 104: | |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | public function export(Request $request, $id, $format = 'yaml', $disposition = 'attachment') { |
| 112: | $list = $this->getItem($id); |
| 113: | |
| 114: | |
| 115: | $filename = sprintf('export_list-%s.%s', $list->getRef(), $format); |
| 116: | $headers = array( |
| 117: | 'Content-Disposition' => $disposition . '; filename="' . $filename . '";', |
| 118: | 'Cache-Control' => 'private', |
| 119: | ); |
| 120: | switch ($format) { |
| 121: | case 'json' : $headers['Content-Type'] = 'application/json'; break; |
| 122: | case 'yaml' : $headers['Content-Type'] = 'text/yaml'; break; |
| 123: | default : die("Invalid format"); |
| 124: | } |
| 125: | |
| 126: | |
| 127: | return new StreamedResponse(function() use ($format, $list) { |
| 128: | switch ($format) { |
| 129: | case 'json' : echo json_encode($list->export(true)); break; |
| 130: | case 'yaml' : echo str_replace(": null\n", ": ~\n", \Symfony\Component\Yaml\Yaml::dump($list->export(true), 5)); break; |
| 131: | } |
| 132: | }, 200, $headers); |
| 133: | } |
| 134: | |
| 135: | |
| 136: | } |
| 137: | |