/** * Check if $user is the $organisation's manager * * @param User $user the user to check if he is $organisation's manager * @param Organisation $organisation the organisation to check if $user is * the manager * @return boolean true if $user is $organisation's manager, false else */ public function isOrganisationManager($user, $organisation) { //If $user in not a User (it may be equals to null for e.g) if (!$user instanceof User) { return false; } elseif (!$organisation instanceof Organisation) { //if $organisation is not an Organisation (it may be equals to null) return false; } elseif (empty($organisation->getManager())) { //If organisation has no manager return false; } return $user->getId() == $organisation->getManager()->getId(); }
/** * {@inheritDoc} */ public function load(ObjectManager $manager) { $users = $manager->getRepository('XaliUserBundle:User')->findAll(); $organisations = array(); $dateOfCreation = new \DateTime(); //Create organisations for ($i = 0; $i < 100; $i++) { $organisation = new Organisation(); //Set organisation $organisation->setDateOfCreation($dateOfCreation); $organisation->setName($i); $organisation->setManager($users[$i]); $users[$i]->addRoles(array('ROLE_ORGANISATION')); $organisations[] = $organisation; $manager->persist($organisation); $manager->persist($users[$i]); } $manager->flush(); }
/** * Update an organisation * * @param \Xali\Bundle\UserBundle\Entity\User $manager the manager we want * to assign * @param \Xali\Bundle\OrganisationBundle\Entity\Organisation $organisation * the organisation to update * @return string */ public function updateOrganisation($manager, $organisation) { if (!empty($manager) && $manager instanceof User) { $result = null; $managerOrganisation = $manager->getCamp() == null ? null : $manager->getCamp()->getOrganisation(); //user belong to an other organisation if ($managerOrganisation != null && $managerOrganisation->getId() != $organisation->getId()) { return "form.error.belong_other_org"; } $sql = "UPDATE organisation\n SET manager_id = ?, name = ?, dateofcreation = ?\n WHERE organisation.id = ?\n "; try { $stmt = $this->_em->getConnection()->prepare($sql); $stmt->execute(array($manager->getId(), $organisation->getName(), $organisation->getDateOfCreation()->format('Y-m-d'), $organisation->getId())); } catch (UniqueConstraintViolationException $e) { $result = "form.error.violation_key"; } catch (\Exception $e) { $result = "form.error.other_error"; } } else { //Manager's email adress is invalid $result = "form.error.invalid_email"; } return $result; }
/** * Insert or update an organisation according to $organisation's id * * @param Xali\Bundle\OrganisationBundle\Entity\Organisation $organisation * the organisation * @param Xali\Bundle\UserBundle\Entity\User $manager * the manager user want to assign * @return string error message if there is * @author Anthony Bocci Anthony Bocci <*****@*****.**> */ public function insertOrUpdate($organisation, $manager) { $em = $this->getDoctrine()->getManager(); $orgRepo = $em->getRepository('XaliOrganisationBundle:Organisation'); //Insert if (empty($organisation->getId())) { return $orgRepo->insertOrganisation($manager, $organisation); } else { //Update return $orgRepo->updateOrganisation($manager, $organisation); } }