private function insertUsrLists(EntityManager $em, \Pimple $DI) { $owner1 = new UsrListOwner(); $owner1->setRole(UsrListOwner::ROLE_ADMIN); $owner1->setUser($DI['user']); $owner2 = new UsrListOwner(); $owner2->setRole(UsrListOwner::ROLE_ADMIN); $owner2->setUser($DI['user_alt1']); $list1 = new UsrList(); $list1->setName('new list'); $list1->addOwner($owner1); $owner1->setList($list1); $entry1 = new UsrListEntry(); $entry1->setUser($DI['user']); $entry1->setList($list1); $list1->addEntrie($entry1); $entry2 = new UsrListEntry(); $entry2->setUser($DI['user_alt1']); $entry2->setList($list1); $list1->addEntrie($entry2); $list2 = new UsrList(); $list2->setName('new list'); $list2->addOwner($owner2); $owner2->setList($list2); $entry3 = new UsrListEntry(); $entry3->setUser($DI['user_alt1']); $entry3->setList($list2); $list2->addEntrie($entry3); $entry4 = new UsrListEntry(); $entry4->setUser($DI['user_alt2']); $entry4->setList($list2); $list2->addEntrie($entry4); $em->persist($owner1); $em->persist($owner2); $em->persist($list1); $em->persist($list2); $em->persist($entry1); $em->persist($entry2); $em->persist($entry3); $em->persist($entry4); }
public function addUsers(Application $app, Request $request, $list_id) { try { if (!is_array($request->request->get('usr_ids'))) { throw new ControllerException('Invalid or missing parameter usr_ids'); } $repository = $app['EM']->getRepository('Phraseanet:UsrList'); $list = $repository->findUserListByUserAndId($app['authentication']->getUser(), $list_id); /* @var $list UsrList */ if ($list->getOwner($app['authentication']->getUser())->getRole() < UsrListOwner::ROLE_EDITOR) { throw new ControllerException($app->trans('You are not authorized to do this')); } $inserted_usr_ids = []; foreach ($request->request->get('usr_ids') as $usr_id) { $user_entry = $app['manipulator.user']->getRepository()->find($usr_id); if ($list->has($user_entry)) { continue; } $entry = new UsrListEntry(); $entry->setUser($user_entry); $entry->setList($list); $list->addEntrie($entry); $app['EM']->persist($entry); $inserted_usr_ids[] = $user_entry->getId(); } $app['EM']->flush(); if (count($inserted_usr_ids) > 1) { $datas = ['success' => true, 'message' => $app->trans('%quantity% Users added to list', ['%quantity%' => count($inserted_usr_ids)]), 'result' => $inserted_usr_ids]; } else { $datas = ['success' => true, 'message' => $app->trans('%quantity% User added to list', ['%quantity%' => count($inserted_usr_ids)]), 'result' => $inserted_usr_ids]; } } catch (ControllerException $e) { $datas = ['success' => false, 'message' => $e->getMessage()]; } catch (\Exception $e) { $datas = ['success' => false, 'message' => $app->trans('Unable to add usr to list')]; } return $app->json($datas); }
public function addUsers(Request $request, $list_id) { try { if (!is_array($request->request->get('usr_ids'))) { throw new ControllerException('Invalid or missing parameter usr_ids'); } $repository = $this->getUsrListRepository(); $user = $this->getAuthenticatedUser(); $list = $repository->findUserListByUserAndId($user, $list_id); if ($list->getOwner($user)->getRole() < UsrListOwner::ROLE_EDITOR) { throw new ControllerException($this->app->trans('You are not authorized to do this')); } $inserted_usr_ids = []; $manager = $this->getEntityManager(); $userIds = $request->request->get('usr_ids'); if (!is_array($userIds)) { throw new \InvalidArgumentException('A usr_ids key should be provider'); } $userIds = array_unique($userIds); /** @var User[] $users */ $users = $this->getUserRepository()->findBy(['id' => $userIds]); if (count($userIds) !== count($users)) { throw new NotFoundHttpException('At least one user was not found'); } foreach ($users as $user_entry) { if ($list->has($user_entry)) { continue; } $entry = new UsrListEntry(); $entry->setUser($user_entry); $entry->setList($list); $list->addEntrie($entry); $manager->persist($entry); $inserted_usr_ids[] = $user_entry->getId(); } $manager->flush(); if (count($inserted_usr_ids) > 1) { $data = ['success' => true, 'message' => $this->app->trans('%quantity% Users added to list', ['%quantity%' => count($inserted_usr_ids)]), 'result' => $inserted_usr_ids]; } else { $data = ['success' => true, 'message' => $this->app->trans('%quantity% User added to list', ['%quantity%' => count($inserted_usr_ids)]), 'result' => $inserted_usr_ids]; } } catch (ControllerException $e) { $data = ['success' => false, 'message' => $e->getMessage()]; } catch (\Exception $e) { $data = ['success' => false, 'message' => $this->app->trans('Unable to add usr to list')]; } return $this->app->json($data); }
/** * {@inheritDoc} */ public function getList() { $this->__initializer__ && $this->__initializer__->__invoke($this, 'getList', array()); return parent::getList(); }