public function createAction($groupId = 0)
 {
     $entity = new User();
     $route = $this->router->getCurrentRoute();
     $formProvider = new FormProvider();
     $this->response->prepareForJson();
     $em = $this->dataProvider->getEntityManager();
     if ($route == 'admin_user_create') {
         $url = $this->router->getUrlByRoute('admin_user_create', array(), false);
         $returnedValue = $this->createReturnedArray('create');
     }
     if ($route == 'admin_user_create_in_group') {
         $url = $this->router->getUrlByRoute('admin_user_create_in_group', array('groupId' => $groupId), false);
         $returnedValue = $this->createReturnedArray('create_in_group');
     }
     $form = $formProvider->getUserEditForm($url, $em->getRepository(get_class($entity)));
     $form->writeFromRequest($this->request);
     if ($form->isValid()) {
         $form->writeToEntity($entity);
         $entity->setWasChecked(true);
         $em->persist($entity);
         $em->flush();
         if ($route == 'admin_user_create_in_group') {
             $manager = new Manager($em, $this->router);
             $branch = $manager->buildGroupTree($groupId);
             $users = $branch->getUsers();
             $users->add($entity->getId());
             $manager->addUsersToGroup($groupId, $users->toArray());
             $returnedValue['view'] = json_encode($manager->prepareGroupTreeForJson($branch)[0]);
         } else {
             $returnedValue['view'] = $this->viewer->render('user_manager::user::newRow.html.twig', array('entity' => $entity));
         }
     } else {
         $returnedValue['status'] = 'failed';
         $returnedValue['view'] = $this->viewer->render('user_manager::user::edit.html.twig', array('entity' => $entity, 'edit_form' => $form));
     }
     return $this->acceptSolutionFlushToResponse(json_encode($returnedValue));
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /**
      * @var EntityManager $em
      */
     $em = (new OrmProvider())->getEntityManager();
     /**
      * @var GroupRepository $groupRepository
      */
     $groupRepository = $em->getRepository('LW\\Model\\Entity\\Group');
     $dict = new Dictionary();
     $users['director'] = $this->generateUser('ivanov_vu', 'ivanov_vu@test', 'ivanov_vu', $dict->userRoles()->ROLE_BOSS[0], 'Иванов', 'Василий', 'Юрьевич', 'Директор');
     $users['department_chief'] = $this->generateUser('vasiliev_va', 'vasiliev_va@test', 'vasiliev_va', $dict->userRoles()->ROLE_BOSS[0], 'Васильев', 'Владимир', 'Алексеевич', 'Начальник отдела рекламы');
     $users['designer'] = $this->generateUser('rasstrelnikov_sv', 'rasstrelnikov_sv@test', 'rasstrelnikov_sv', $dict->userRoles()->ROLE_WORKER[0], 'Расстрельников', 'Сергей', 'Викторович', 'Дизайнер');
     $users['copywriter'] = $this->generateUser('tverskaya_aa', 'tverskaya_aa@test', 'tverskaya_aa', $dict->userRoles()->ROLE_WORKER[0], 'Тверская', 'Алена', 'Александровна', 'Копирайтер');
     $users['system_administrator'] = $this->generateUser('admin', 'kolelov_in@test', '_admin_', $dict->userRoles()->ROLE_WORKER[0], 'Колелов', 'Иван', 'Николаевич', 'Системный администратор');
     $groups['organisation'] = $this->generateGroup('ООО Наша фирма');
     $groups['chief'] = $this->generateGroup('Директор');
     $groups['department_advertising'] = $this->generateGroup('Отдел рекламы');
     $groups['chief_department'] = $this->generateGroup('Начальник отдела');
     $groups['worker_department'] = $this->generateGroup('Работники');
     foreach ($users as $user) {
         $em->persist($user);
     }
     foreach ($groups as $group) {
         $group->setInnerOrder($groupRepository->getNextNumberByOwner(0));
         $em->persist($group);
     }
     $em->flush();
     $request = new Request();
     $router = new Router($request);
     $manager = new Manager($em, $router);
     $manager->addUsersToGroup($groups['chief']->getId(), array($users['director']->getId()));
     $manager->addUsersToGroup($groups['chief_department']->getId(), array($users['department_chief']->getId()));
     $manager->addUsersToGroup($groups['worker_department']->getId(), array($users['copywriter']->getId(), $users['designer']->getId()));
     $manager->moveNode($groups['chief']->getId(), $groups['organisation']->getId(), 1);
     $manager->moveNode($groups['department_advertising']->getId(), $groups['chief']->getId(), 1);
     $manager->moveNode($groups['chief_department']->getId(), $groups['department_advertising']->getId(), 1);
     $manager->moveNode($groups['worker_department']->getId(), $groups['chief_department']->getId(), 1);
     $output->writeln('Successful!');
 }
 public function updateUsersListAction($id)
 {
     $em = $this->dataProvider->getEntityManager();
     $entity = $em->getRepository('LW\\Model\\Entity\\Group')->find($id);
     if (!$entity) {
         throw new \Exception('Unable to find Group entity.');
     }
     $formProvider = new FormProvider();
     $url = $this->router->getUrlByRoute('admin_group_update_users_list', array('id' => $id), false);
     $form = $formProvider->getGroupUsersListForm($url);
     $form->writeFromRequest($this->request);
     $this->response->prepareForJson();
     $returnedValue = $this->createReturnedArray('update_users_list');
     $manager = new Manager($em, $this->router);
     if ($form->isValid()) {
         $users = json_decode($form->getField('users')->getValue());
         $manager->addUsersToGroup($id, $users);
         $branch = $manager->buildGroupTree($id);
         $returnedValue['view'] = json_encode($manager->prepareGroupTreeForJson($branch)[0]);
     } else {
         $entityMoved = $manager->getGroup($form->get('movedId')->getData());
         $returnedValue['status'] = 'failed';
         $returnedValue['view'] = sprintf('Редактирование списка подльзователей для группы <b>%s</b> не было сохранено', $entityMoved->getName());
     }
     return $this->acceptSolutionFlushToResponse(json_encode($returnedValue));
 }
 private function prepareUserListByBoss()
 {
     $manager = new Manager($this->dataProvider->getEntityManager(), $this->router);
     /**
      * @var $user User
      */
     $user = $this->authentication->getCurrentUser();
     $users = array();
     $links = $user->getLinks();
     if ($links->count() != 1) {
         return $users;
     }
     $tree = $manager->buildGroupTree($links[0]->getGroupId());
     $users = $tree->getUsersGlobal();
     $selectedFields = array('id as id', 'firstName as fName', 'name as name', 'secondName as sName', 'workPosition as wPost');
     $users = $this->dataProvider->getEntityManager()->getRepository('Lw\\Model\\Entity\\User')->getAllUsers(false, true, $users, $selectedFields);
     return $users;
 }