PersonAddressController.php 9.75 KB
Newer Older
1 2 3 4 5
<?php

/*
 * Chill is a software for social workers
 *
6
 * Copyright (C) 2016, Champs Libres Cooperative SCRLFS,
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * <http://www.champs-libres.coop>, <info@champs-libres.coop>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace Chill\PersonBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
26
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
27 28 29 30 31 32 33 34 35 36 37 38 39 40
use Chill\PersonBundle\Entity\Person;
use Chill\MainBundle\Form\Type\AddressType;
use Chill\MainBundle\Entity\Address;
use Doctrine\Common\Collections\Criteria;
use Symfony\Component\HttpFoundation\Request;

/**
 * Controller for addresses associated with person
 *
 * @author Julien Fastré <julien.fastre@champs-libres.coop>
 * @author Champs Libres <info@champs-libres.coop>
 */
class PersonAddressController extends Controller
{
41

42 43 44 45 46
    public function listAction($person_id)
    {
        $person = $this->getDoctrine()->getManager()
              ->getRepository('ChillPersonBundle:Person')
              ->find($person_id);
47

48
        if ($person === null) {
49 50 51
            throw $this->createNotFoundException("Person with id $person_id not"
                    . " found ");
        }
52

53 54 55 56 57
        $this->denyAccessUnlessGranted(
            'CHILL_PERSON_SEE',
            $person,
            "You are not allowed to edit this person."
        );
58

59 60 61 62
        return $this->render('ChillPersonBundle:Address:list.html.twig', array(
            'person' => $person
        ));
    }
63

64 65 66 67 68
    public function newAction($person_id)
    {
        $person = $this->getDoctrine()->getManager()
              ->getRepository('ChillPersonBundle:Person')
              ->find($person_id);
69

70
        if ($person === null) {
71 72 73
            throw $this->createNotFoundException("Person with id $person_id not"
                    . " found ");
        }
74

75 76 77 78 79
        $this->denyAccessUnlessGranted(
            'CHILL_PERSON_UPDATE',
            $person,
            "You are not allowed to edit this person."
        );
80

81
        $address = new Address();
82

83
        $form = $this->createCreateForm($person, $address);
84

85 86 87 88 89
        return $this->render('ChillPersonBundle:Address:new.html.twig', array(
            'person' => $person,
            'form' => $form->createView()
        ));
    }
90

91 92 93 94 95
    public function createAction($person_id, Request $request)
    {
        $person = $this->getDoctrine()->getManager()
              ->getRepository('ChillPersonBundle:Person')
              ->find($person_id);
96

97
        if ($person === null) {
98 99 100
            throw $this->createNotFoundException("Person with id $person_id not"
                    . " found ");
        }
101

102 103 104 105 106
        $this->denyAccessUnlessGranted(
            'CHILL_PERSON_UPDATE',
            $person,
            "You are not allowed to edit this person."
        );
107

108
        $address = new Address();
109

110 111
        $form = $this->createCreateForm($person, $address);
        $form->handleRequest($request);
112

113
        $person->addAddress($address);
114

115 116
        if ($form->isSubmitted()) {
            $validatePersonErrors = $this->validatePerson($person);
117

118 119 120 121 122
            if (count($validatePersonErrors) !== 0) {
                foreach ($validatePersonErrors as $error) {
                    $this->addFlash('error', $error->getMessage());
                }
            } elseif ($form->isValid()) {
123

124 125
                $em = $this->getDoctrine()->getManager();
                $em->flush();
126

127 128
                $this->addFlash(
                    'success',
129
                    $this->get('translator')->trans('The new address was created successfully')
130
                );
131

132 133 134 135 136 137 138
                return $this->redirectToRoute('chill_person_address_list', array(
                    'person_id' => $person->getId()
                ));
            } else {
                $this->addFlash('error', $this->get('translator')
                        ->trans('Error! Address not created!'));
            }
139
        }
140

141 142 143 144 145
        return $this->render('ChillPersonBundle:Address:new.html.twig', array(
            'person' => $person,
            'form' => $form->createView()
        ));
    }
146

147 148 149 150 151
    public function editAction($person_id, $address_id)
    {
        $person = $this->getDoctrine()->getManager()
              ->getRepository('ChillPersonBundle:Person')
              ->find($person_id);
152

153
        if ($person === null) {
154 155 156
            throw $this->createNotFoundException("Person with id $person_id not"
                    . " found ");
        }
157

158 159 160 161 162
        $this->denyAccessUnlessGranted(
            'CHILL_PERSON_UPDATE',
            $person,
            "You are not allowed to edit this person."
        );
163

164
        $address = $this->findAddressById($person, $address_id);
165

166
        $form = $this->createEditForm($person, $address);
167

168 169 170 171 172 173
        return $this->render('ChillPersonBundle:Address:edit.html.twig', array(
           'person' => $person,
           'address' => $address,
           'form' => $form->createView()
        ));
    }
174

175 176 177
    public function updateAction($person_id, $address_id, Request $request)
    {
        $person = $this->getDoctrine()->getManager()
178 179
            ->getRepository('ChillPersonBundle:Person')
            ->find($person_id);
180

181
        if ($person === null) {
182
            throw $this->createNotFoundException("Person with id $person_id not"
183
                . " found ");
184
        }
185

186 187 188 189 190
        $this->denyAccessUnlessGranted(
            'CHILL_PERSON_UPDATE',
            $person,
            "You are not allowed to edit this person."
        );
191

192
        $address = $this->findAddressById($person, $address_id);
193

194 195
        $form = $this->createEditForm($person, $address);
        $form->handleRequest($request);
196 197

        if ($request->getMethod() === 'POST') {
198
            $validatePersonErrors = $this->validatePerson($person);
199

200 201 202 203 204 205 206
            if (count($validatePersonErrors) !== 0) {
                foreach ($validatePersonErrors as $error) {
                    $this->addFlash('error', $error->getMessage());
                }
            } elseif ($form->isValid()) {
                $this->getDoctrine()->getManager()
                      ->flush();
207

208 209 210
                $this->addFlash('success', $this->get('translator')->trans(
                    "The address has been successfully updated"
                ));
211

212 213 214 215 216 217 218
                return $this->redirectToRoute('chill_person_address_list', array(
                   'person_id' => $person->getId()
                ));
            } else {
                $this->addFlash('error', $this->get('translator')
                    ->trans('Error when updating the period'));
            }
219
        }
220

221 222 223 224 225 226
        return $this->render('ChillPersonBundle:Address:edit.html.twig', array(
           'person' => $person,
           'address' => $address,
           'form' => $form->createView()
        ));
    }
227

228 229 230 231 232 233 234 235 236 237 238 239 240 241
    /**
     * @param Person $person
     * @param Address $address
     * @return \Symfony\Component\Form\Form
     */
    protected function createEditForm(Person $person, Address $address)
    {
        $form = $this->createForm(AddressType::class, $address, array(
           'method' => 'POST',
           'action' => $this->generateUrl('chill_person_address_update', array(
              'person_id' => $person->getId(),
              'address_id' => $address->getId()
           ))
        ));
242 243

        $form->add('submit', SubmitType::class, array(
244 245
           'label' => 'Submit'
        ));
246

247 248
        return $form;
    }
249

250
    /**
251
     *
252 253 254 255 256 257 258 259 260 261 262 263
     * @param Person $person
     * @param Address $address
     * @return \Symfony\Component\Form\Form
     */
    protected function createCreateForm(Person $person, Address $address)
    {
        $form = $this->createForm(AddressType::class, $address, array(
           'method' => 'POST',
           'action' => $this->generateUrl('chill_person_address_create', array(
              'person_id' => $person->getId()
           ))
        ));
264 265

        $form->add('submit', SubmitType::class, array(
266 267
           'label' => 'Submit'
        ));
268

269 270
        return $form;
    }
271

272
    /**
273
     *
274 275 276 277 278 279 280 281 282 283 284 285
     * @param Person $person
     * @param int $address_id
     * @return Address
     * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if the address id does not exists or is not associated with given person
     */
    protected function findAddressById(Person $person, $address_id)
    {
        // filtering address
        $criteria = Criteria::create()
              ->where(Criteria::expr()->eq('id', $address_id))
              ->setMaxResults(1);
        $addresses = $person->getAddresses()->matching($criteria);
286

287 288 289 290
        if (count($addresses) === 0) {
            throw $this->createNotFoundException("Address with id $address_id "
                  . "matching person $person_id not found ");
        }
291

292 293
        return $addresses->first();
    }
294

295 296 297 298 299 300 301 302 303 304
    /**
     * @param Chill\PersonBundle\Entity\Person $person
     * @return \Symfony\Component\Validator\ConstraintViolationListInterface
     */
    private function validatePerson(Person $person)
    {
        $errors = $this->get('validator')
            ->validate($person, array('Default'));
        $errors_addresses_consistent = $this->get('validator')
            ->validate($person, array('addresses_consistent'));
305

306 307 308
        foreach($errors_addresses_consistent as $error) {
            $errors->add($error);
        }
309

310 311
        return $errors;
    }
312
}