| @@ 15-225 (lines=211) @@ | ||
| 12 | use Symfony\Component\HttpFoundation\Request; |
|
| 13 | ||
| 14 | ||
| 15 | class CampaignController extends StructureController |
|
| 16 | { |
|
| 17 | /** |
|
| 18 | * @ApiDoc( |
|
| 19 | * resource=true, |
|
| 20 | * description="Show campaigns list.", |
|
| 21 | * section="Campaign", |
|
| 22 | * parameters={ |
|
| 23 | * { |
|
| 24 | * "name"="offset", |
|
| 25 | * "dataType"="integer", |
|
| 26 | * "requirement"="\d+", |
|
| 27 | * "description"="starkerxp_structure.doc.offset.result", |
|
| 28 | * "required"="false" |
|
| 29 | * }, |
|
| 30 | * { |
|
| 31 | * "name"="limit", |
|
| 32 | * "dataType"="integer", |
|
| 33 | * "requirement"="\d+", |
|
| 34 | * "description"="starkerxp_structure.doc.limit.result", |
|
| 35 | * "required"="false" |
|
| 36 | * }, |
|
| 37 | * { |
|
| 38 | * "name"="fields", |
|
| 39 | * "dataType"="string", |
|
| 40 | * "requirement"="\w+", |
|
| 41 | * "description"="starkerxp_structure.doc.list_field.entity", |
|
| 42 | * "required"="false" |
|
| 43 | * }, |
|
| 44 | * { |
|
| 45 | * "name"="sort", |
|
| 46 | * "dataType"="string", |
|
| 47 | * "requirement"="\w+", |
|
| 48 | * "description"="starkerxp_structure.doc.sort.result", |
|
| 49 | * "required"="false" |
|
| 50 | * } |
|
| 51 | * }, |
|
| 52 | * views = { "default" } |
|
| 53 | * ) |
|
| 54 | */ |
|
| 55 | public function cgetAction(Request $request) |
|
| 56 | { |
|
| 57 | $manager = $this->get("starkerxp_campaign.manager.campaign"); |
|
| 58 | try { |
|
| 59 | $options = $this->resolveParams()->resolve($request->query->all()); |
|
| 60 | $orderBy = $this->getOrderBy($options['sort']); |
|
| 61 | $resultSets = $manager->findBy([], $orderBy, $options['limit'], $options['offset']); |
|
| 62 | } catch (\Exception $e) { |
|
| 63 | return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
| 64 | } |
|
| 65 | $retour = array_map( |
|
| 66 | function ($element) use ($manager, $options) { |
|
| 67 | return $manager->toArray($element, $this->getFields($options['fields'])); |
|
| 68 | }, |
|
| 69 | $resultSets |
|
| 70 | ); |
|
| 71 | ||
| 72 | return new JsonResponse($retour); |
|
| 73 | } |
|
| 74 | ||
| 75 | /** |
|
| 76 | * @ApiDoc( |
|
| 77 | * resource=true, |
|
| 78 | * description="Show campaign.", |
|
| 79 | * section="Campaign", |
|
| 80 | * requirements={ |
|
| 81 | * { |
|
| 82 | * "name"="campaign_id", |
|
| 83 | * "dataType"="integer", |
|
| 84 | * "requirement"="\d+", |
|
| 85 | * "description"="Show an element" |
|
| 86 | * } |
|
| 87 | * }, |
|
| 88 | * parameters={ |
|
| 89 | * { |
|
| 90 | * "name"="fields", |
|
| 91 | * "dataType"="string", |
|
| 92 | * "requirement"="\w+", |
|
| 93 | * "description"="starkerxp_structure.doc.list_field.entity", |
|
| 94 | * "required"="false" |
|
| 95 | * } |
|
| 96 | * }, |
|
| 97 | * views = { "default" } |
|
| 98 | * ) |
|
| 99 | */ |
|
| 100 | public function getAction(Request $request) |
|
| 101 | { |
|
| 102 | $manager = $this->get("starkerxp_campaign.manager.campaign"); |
|
| 103 | try { |
|
| 104 | $options = $this->resolveParams()->resolve($request->query->all()); |
|
| 105 | /** @var Campaign $entite */ |
|
| 106 | if (!$entite = $manager->findOneBy(['id' => $request->get('campaign_id')])) { |
|
| 107 | return new JsonResponse(["payload" => $this->translate("entity.not_found", "campaign")], 404); |
|
| 108 | } |
|
| 109 | } catch (\Exception $e) { |
|
| 110 | return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
| 111 | } |
|
| 112 | $retour = $manager->toArray($entite, $this->getFields($options['fields'])); |
|
| 113 | ||
| 114 | return new JsonResponse($retour); |
|
| 115 | } |
|
| 116 | ||
| 117 | /** |
|
| 118 | * @ApiDoc( |
|
| 119 | * resource=true, |
|
| 120 | * description="Add campaign.", |
|
| 121 | * section="Campaign", |
|
| 122 | * views = {"default"} |
|
| 123 | * ) |
|
| 124 | */ |
|
| 125 | public function postAction(Request $request) |
|
| 126 | { |
|
| 127 | $manager = $this->get("starkerxp_campaign.manager.campaign"); |
|
| 128 | try { |
|
| 129 | $entite = new Campaign(); |
|
| 130 | $form = $this->createForm(CampaignType::class, $entite, ['method' => 'POST']); |
|
| 131 | $form->submit($this->getRequestData($request)); |
|
| 132 | if ($form->isValid()) { |
|
| 133 | $entite = $form->getData(); |
|
| 134 | $manager->insert($entite); |
|
| 135 | $this->dispatch(Events::CAMPAIGN_CREATED, new GenericEvent($entite)); |
|
| 136 | ||
| 137 | return new JsonResponse(["payload" => $this->translate("entity.created", "campaign"), "token" => $entite->getId()], 201); |
|
| 138 | } |
|
| 139 | } catch (\Exception $e) { |
|
| 140 | $manager->rollback(); |
|
| 141 | ||
| 142 | return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
| 143 | } |
|
| 144 | ||
| 145 | return new JsonResponse(["payload" => $this->getFormErrors($form)], 400); |
|
| 146 | } |
|
| 147 | ||
| 148 | /** |
|
| 149 | * @ApiDoc( |
|
| 150 | * resource=true, |
|
| 151 | * description="Edit campaign.", |
|
| 152 | * section="Campaign", |
|
| 153 | * requirements={ |
|
| 154 | * { |
|
| 155 | * "name"="campaign_id", |
|
| 156 | * "dataType"="integer", |
|
| 157 | * "requirement"="\d+", |
|
| 158 | * "description"="Edit an element." |
|
| 159 | * } |
|
| 160 | * }, |
|
| 161 | * views = { "default" } |
|
| 162 | * ) |
|
| 163 | */ |
|
| 164 | public function putAction(Request $request) |
|
| 165 | { |
|
| 166 | $manager = $this->get("starkerxp_campaign.manager.campaign"); |
|
| 167 | if (!$entite = $manager->findOneBy(['id' => $request->get('campaign_id')])) { |
|
| 168 | return new JsonResponse(["payload" => $this->translate("entity.not_found", "campaign")], 404); |
|
| 169 | } |
|
| 170 | $manager->beginTransaction(); |
|
| 171 | try { |
|
| 172 | $form = $this->createForm(CampaignType::class, $entite, ['method' => 'PUT']); |
|
| 173 | $form->submit($this->getRequestData($request), false); |
|
| 174 | if ($form->isValid()) { |
|
| 175 | $entite = $form->getData(); |
|
| 176 | $manager->update($entite); |
|
| 177 | $this->dispatch(Events::CAMPAIGN_UPDATED, new GenericEvent($entite)); |
|
| 178 | ||
| 179 | return new JsonResponse(["payload" => $this->translate("entity.updated", "campaign")], 204); |
|
| 180 | } |
|
| 181 | } catch (\Exception $e) { |
|
| 182 | $manager->rollback(); |
|
| 183 | ||
| 184 | return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
| 185 | } |
|
| 186 | ||
| 187 | return new JsonResponse(["payload" => $this->getFormErrors($form)], 400); |
|
| 188 | } |
|
| 189 | ||
| 190 | /** |
|
| 191 | * @ApiDoc( |
|
| 192 | * resource=true, |
|
| 193 | * description="Delete campaign.", |
|
| 194 | * section="Campaign", |
|
| 195 | * requirements={ |
|
| 196 | * { |
|
| 197 | * "name"="campaign_id", |
|
| 198 | * "dataType"="integer", |
|
| 199 | * "requirement"="\d+", |
|
| 200 | * "description"="Delete an element." |
|
| 201 | * } |
|
| 202 | * }, |
|
| 203 | * views = { "default" } |
|
| 204 | * ) |
|
| 205 | */ |
|
| 206 | public function deleteAction(Request $request) |
|
| 207 | { |
|
| 208 | $manager = $this->get("starkerxp_campaign.manager.campaign"); |
|
| 209 | ||
| 210 | if (!$entite = $manager->findOneBy(['id' => $request->get('campaign_id')])) { |
|
| 211 | return new JsonResponse(["payload" => $this->translate("entity.not_found", "campaign")], 404); |
|
| 212 | } |
|
| 213 | try { |
|
| 214 | $manager->delete($entite); |
|
| 215 | } catch (\Exception $e) { |
|
| 216 | $manager->rollback(); |
|
| 217 | ||
| 218 | return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
| 219 | } |
|
| 220 | $this->dispatch(Events::CAMPAIGN_DELETED, new GenericEvent($request->get('campaign_id'))); |
|
| 221 | ||
| 222 | return new JsonResponse(["payload" => $this->translate("entity.deleted", "campaign")], 204); |
|
| 223 | } |
|
| 224 | ||
| 225 | } |
|
| 226 | ||
| @@ 15-229 (lines=215) @@ | ||
| 12 | use Symfony\Component\HttpFoundation\Request; |
|
| 13 | ||
| 14 | ||
| 15 | class TemplateController extends StructureController |
|
| 16 | { |
|
| 17 | /** |
|
| 18 | * @ApiDoc( |
|
| 19 | * resource=true, |
|
| 20 | * description="List templates.", |
|
| 21 | * section="Campaign", |
|
| 22 | * parameters={ |
|
| 23 | * { |
|
| 24 | * "name"="offset", |
|
| 25 | * "dataType"="integer", |
|
| 26 | * "requirement"="\d+", |
|
| 27 | * "description"="starkerxp_structure.doc.offset.result", |
|
| 28 | * "required"="false" |
|
| 29 | * }, |
|
| 30 | * { |
|
| 31 | * "name"="limit", |
|
| 32 | * "dataType"="integer", |
|
| 33 | * "requirement"="\d+", |
|
| 34 | * "description"="starkerxp_structure.doc.limit.result", |
|
| 35 | * "required"="false" |
|
| 36 | * }, |
|
| 37 | * { |
|
| 38 | * "name"="fields", |
|
| 39 | * "dataType"="string", |
|
| 40 | * "requirement"="\w+", |
|
| 41 | * "description"="starkerxp_structure.doc.list_field.entity", |
|
| 42 | * "required"="false" |
|
| 43 | * }, |
|
| 44 | * { |
|
| 45 | * "name"="sort", |
|
| 46 | * "dataType"="string", |
|
| 47 | * "requirement"="\w+", |
|
| 48 | * "description"="starkerxp_structure.doc.sort.result", |
|
| 49 | * "required"="false" |
|
| 50 | * } |
|
| 51 | * }, |
|
| 52 | * views = { "default" } |
|
| 53 | * ) |
|
| 54 | * |
|
| 55 | */ |
|
| 56 | public function cgetAction(Request $request) |
|
| 57 | { |
|
| 58 | $manager = $this->get("starkerxp_campaign.manager.template"); |
|
| 59 | try { |
|
| 60 | $options = $this->resolveParams()->resolve($request->query->all()); |
|
| 61 | $orderBy = $this->getOrderBy($options['sort']); |
|
| 62 | $resultSets = $manager->findBy([], $orderBy, $options['limit'], $options['offset']); |
|
| 63 | } catch (\Exception $e) { |
|
| 64 | return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
| 65 | } |
|
| 66 | if (empty($resultSets)) { |
|
| 67 | return new JsonResponse([]); |
|
| 68 | } |
|
| 69 | $retour = array_map( |
|
| 70 | function ($element) use ($manager, $options) { |
|
| 71 | return $manager->toArray($element, $this->getFields($options['fields'])); |
|
| 72 | }, |
|
| 73 | $resultSets |
|
| 74 | ); |
|
| 75 | ||
| 76 | return new JsonResponse($retour); |
|
| 77 | } |
|
| 78 | ||
| 79 | ||
| 80 | /** |
|
| 81 | * @ApiDoc( |
|
| 82 | * resource=true, |
|
| 83 | * description="Affiche un template.", |
|
| 84 | * section="Campaign", |
|
| 85 | * requirements={ |
|
| 86 | * { |
|
| 87 | * "name"="template_id", |
|
| 88 | * "dataType"="integer", |
|
| 89 | * "requirement"="\d+", |
|
| 90 | * "description"="Show an element" |
|
| 91 | * } |
|
| 92 | * }, |
|
| 93 | * parameters={ |
|
| 94 | * { |
|
| 95 | * "name"="fields", |
|
| 96 | * "dataType"="string", |
|
| 97 | * "requirement"="\w+", |
|
| 98 | * "description"="starkerxp_structure.doc.list_field.entity", |
|
| 99 | * "required"="false" |
|
| 100 | * } |
|
| 101 | * }, |
|
| 102 | * views = { "default" } |
|
| 103 | * ) |
|
| 104 | */ |
|
| 105 | public function getAction(Request $request) |
|
| 106 | { |
|
| 107 | $manager = $this->get("starkerxp_campaign.manager.template"); |
|
| 108 | try { |
|
| 109 | $options = $this->resolveParams()->resolve($request->query->all()); |
|
| 110 | if (!$entite = $manager->findOneBy(['id' => $request->get('template_id')])) { |
|
| 111 | return new JsonResponse(["payload" => $this->translate("entity.not_found", "template")], 404); |
|
| 112 | } |
|
| 113 | } catch (\Exception $e) { |
|
| 114 | return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
| 115 | } |
|
| 116 | ||
| 117 | $retour = $manager->toArray($entite, $this->getFields($options['fields'])); |
|
| 118 | ||
| 119 | return new JsonResponse($retour); |
|
| 120 | } |
|
| 121 | ||
| 122 | /** |
|
| 123 | * @ApiDoc( |
|
| 124 | * resource=true, |
|
| 125 | * description="Ajoute un template.", |
|
| 126 | * section="Campaign", |
|
| 127 | * views = { "default" } |
|
| 128 | * ) |
|
| 129 | */ |
|
| 130 | public function postAction(Request $request) |
|
| 131 | { |
|
| 132 | $manager = $this->get("starkerxp_campaign.manager.template"); |
|
| 133 | try { |
|
| 134 | $entite = new Template(); |
|
| 135 | $form = $this->createForm(TemplateType::class, $entite, ['method' => 'POST']); |
|
| 136 | $form->submit($this->getRequestData($request)); |
|
| 137 | if ($form->isValid()) { |
|
| 138 | $template = $form->getData(); |
|
| 139 | $manager->insert($template); |
|
| 140 | $this->dispatch(Events::TEMPLATE_CREATED, new GenericEvent($entite)); |
|
| 141 | ||
| 142 | return new JsonResponse(["payload" => $this->translate("entity.created", "template")], 201); |
|
| 143 | } |
|
| 144 | } catch (\Exception $e) { |
|
| 145 | $manager->rollback(); |
|
| 146 | ||
| 147 | return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
| 148 | } |
|
| 149 | ||
| 150 | return new JsonResponse(["payload" => $this->getFormErrors($form)], 400); |
|
| 151 | } |
|
| 152 | ||
| 153 | /** |
|
| 154 | * @ApiDoc( |
|
| 155 | * resource=true, |
|
| 156 | * description="Modifie un template.", |
|
| 157 | * section="Campaign", |
|
| 158 | * requirements={ |
|
| 159 | * { |
|
| 160 | * "name"="template_id", |
|
| 161 | * "dataType"="integer", |
|
| 162 | * "requirement"="\d+", |
|
| 163 | * "description"="Edit an element." |
|
| 164 | * } |
|
| 165 | * }, |
|
| 166 | * views = { "default" } |
|
| 167 | * ) |
|
| 168 | */ |
|
| 169 | public function putAction(Request $request) |
|
| 170 | { |
|
| 171 | $manager = $this->get("starkerxp_campaign.manager.template"); |
|
| 172 | if (!$entite = $manager->findOneBy(['id' => $request->get('template_id')])) { |
|
| 173 | return new JsonResponse(["payload" => $this->translate("entity.not_found", "template")], 404); |
|
| 174 | } |
|
| 175 | $manager->beginTransaction(); |
|
| 176 | try { |
|
| 177 | $form = $this->createForm(TemplateType::class, $entite, ['method' => 'PUT']); |
|
| 178 | $form->submit($this->getRequestData($request), false); |
|
| 179 | if ($form->isValid()) { |
|
| 180 | $entite = $form->getData(); |
|
| 181 | $manager->update($entite); |
|
| 182 | $this->dispatch(Events::TEMPLATE_UPDATED, new GenericEvent($entite)); |
|
| 183 | ||
| 184 | return new JsonResponse(["payload" => $this->translate("entity.updated", "template")], 204); |
|
| 185 | } |
|
| 186 | } catch (\Exception $e) { |
|
| 187 | $manager->rollback(); |
|
| 188 | ||
| 189 | return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
| 190 | } |
|
| 191 | ||
| 192 | return new JsonResponse(["payload" => $this->getFormErrors($form)], 400); |
|
| 193 | } |
|
| 194 | ||
| 195 | /** |
|
| 196 | * @ApiDoc( |
|
| 197 | * resource=true, |
|
| 198 | * description="Delete a template.", |
|
| 199 | * section="Campaign", |
|
| 200 | * requirements={ |
|
| 201 | * { |
|
| 202 | * "name"="template_id", |
|
| 203 | * "dataType"="integer", |
|
| 204 | * "requirement"="\d+", |
|
| 205 | * "description"="Delete an element." |
|
| 206 | * } |
|
| 207 | * }, |
|
| 208 | * views = { "default" } |
|
| 209 | * ) |
|
| 210 | */ |
|
| 211 | public function deleteAction(Request $request) |
|
| 212 | { |
|
| 213 | $manager = $this->get("starkerxp_campaign.manager.template"); |
|
| 214 | if (!$entite = $manager->findOneBy(['id' => $request->get('template_id')])) { |
|
| 215 | return new JsonResponse(["payload" => $this->translate("entity.not_found", "template")], 404); |
|
| 216 | } |
|
| 217 | try { |
|
| 218 | $manager->delete($entite); |
|
| 219 | } catch (\Exception $e) { |
|
| 220 | $manager->rollback(); |
|
| 221 | ||
| 222 | return new JsonResponse(["payload" => $e->getMessage()], 400); |
|
| 223 | } |
|
| 224 | $this->dispatch(Events::TEMPLATE_DELETED, new GenericEvent($request->get('template_id'))); |
|
| 225 | ||
| 226 | return new JsonResponse(["payload" => $this->translate("entity.deleted", "template")], 204); |
|
| 227 | } |
|
| 228 | ||
| 229 | } |
|
| 230 | ||