Ojs\JournalBundle\Entity\JournalUser::addRole PHP Метод

addRole() публичный Метод

public addRole ( Role $role )
$role Ojs\UserBundle\Entity\Role
    public function addRole(Role $role)
    {
        if (!$this->roles->contains($role)) {
            $this->roles->add($role);
        }
        return $this;
    }

Usage Example

 /**
  * @param  Request                   $request
  * @param $articleId
  * @return RedirectResponse|Response
  * @throws \Exception
  */
 public function previewAction(Request $request, $articleId)
 {
     if ($this->submissionsNotAllowed()) {
         return $this->respondAsNotAllowed();
     }
     $journal = $this->get('ojs.journal_service')->getSelectedJournal();
     $em = $this->getDoctrine()->getManager();
     $dispatcher = $this->get('event_dispatcher');
     $session = $this->get('session');
     /** @var User $user */
     $user = $this->getUser();
     if (!$journal) {
         $this->throw404IfNotFound($journal);
     }
     /** @var ArticleRepository $articleRepository */
     $articleRepository = $em->getRepository('OjsJournalBundle:Article');
     /** @var Article $article */
     $article = $articleRepository->findOneBy(array('id' => $articleId, 'submitterUser' => $user, 'status' => ArticleStatuses::STATUS_NOT_SUBMITTED));
     $this->throw404IfNotFound($article);
     $form = $this->createForm(new ArticlePreviewType(), $article, array('action' => $this->generateUrl('ojs_journal_submission_preview', array('journalId' => $journal->getId(), 'articleId' => $article->getId())), 'method' => 'POST'))->add('submit', 'submit', array('label' => 'article.submit', 'attr' => ['class' => 'btn-block']));
     $form->handleRequest($request);
     $validator = $this->get('validator');
     $draftErrors = $validator->validate($article, null, ['groups' => 'submission']);
     if ($form->isValid() && count($draftErrors) == 0) {
         if ($session->has('submissionFiles')) {
             $session->remove('submissionFiles');
         }
         $article->setStatus(ArticleStatuses::STATUS_INREVIEW);
         $article->setSubmissionDate(new \DateTime());
         $em->persist($article);
         // Assign user to author journal role
         /** @var Role $role */
         $role = $em->getRepository('OjsUserBundle:Role')->findOneBy(['role' => 'ROLE_AUTHOR']);
         /** @var JournalUser $journalUser */
         $journalUser = $em->getRepository('OjsJournalBundle:JournalUser')->findOneBy(array('user' => $user));
         if (!$journalUser) {
             $journalUser = new JournalUser();
             $journalUser->setJournal($journal)->setUser($user);
         }
         $journalUser->addRole($role);
         $em->persist($journalUser);
         $em->flush();
         $response = $this->redirectToRoute('ojs_journal_submission_me', ['journalId' => $article->getJournal()->getId()]);
         $event = new JournalItemEvent($article);
         $dispatcher->dispatch(ArticleEvents::POST_SUBMIT, $event);
         if (null !== $event->getResponse()) {
             return $event->getResponse();
         }
         return $response;
     }
     return $this->render('OjsJournalBundle:ArticleSubmission:preview.html.twig', array('article' => $article, 'journal' => $journal, 'translations' => $article->getTranslations(), 'fileTypes' => ArticleFileParams::$FILE_TYPES, 'form' => $form->createView(), 'draftErrors' => $draftErrors));
 }