/**
  * Returns an context choice array.
  *
  * @return string[]
  */
 protected final function getContextChoices()
 {
     $contextChoices = array();
     /* @var ContextInterface $context */
     foreach ($this->contextManager->findAll() as $context) {
         $contextChoices[$context->getId()] = $context->getName();
     }
     return $contextChoices;
 }
 /**
  * {@inheritdoc}
  */
 public function getNewInstance()
 {
     $instance = parent::getNewInstance();
     if ($contextId = $this->getPersistentParameter('context')) {
         $context = $this->contextManager->find($contextId);
         if (!$context) {
             $context = $this->contextManager->create();
             $context->setEnabled(true);
             $context->setId($context);
             $context->setName($context);
             $this->contextManager->save($context);
         }
         $instance->setContext($context);
     }
     return $instance;
 }
 public function testExecuteWithNew()
 {
     $context = array('providers' => array(), 'formats' => array(), 'download' => array());
     $this->pool->expects($this->any())->method('getContexts')->will($this->returnValue(array('foo' => $context)));
     $contextModel = $this->getMock('Sonata\\ClassificationBundle\\Model\\ContextInterface');
     $this->contextManger->expects($this->once())->method('findOneBy')->with($this->equalTo(array('id' => 'foo')))->will($this->returnValue(null));
     $this->contextManger->expects($this->once())->method('create')->will($this->returnValue($contextModel));
     $this->contextManger->expects($this->once())->method('save')->with($this->equalTo($contextModel));
     $category = $this->getMock('Sonata\\ClassificationBundle\\Model\\CategoryInterface');
     $this->categoryManger->expects($this->once())->method('getRootCategory')->with($this->equalTo($contextModel))->will($this->returnValue(null));
     $this->categoryManger->expects($this->once())->method('create')->will($this->returnValue($category));
     $this->categoryManger->expects($this->once())->method('save')->with($this->equalTo($category));
     $output = $this->tester->execute(array());
     $this->assertRegExp('@ > default category for \'foo\' is missing, creating one\\s+Done!@', $this->tester->getDisplay());
     $this->assertSame(0, $output);
 }
Beispiel #4
0
 /**
  * Write a context, this method is used by both POST and PUT action methods.
  *
  * @param Request  $request Symfony request
  * @param int|null $id      A context identifier
  *
  * @return FormInterface
  */
 protected function handleWriteContext($request, $id = null)
 {
     $context = $id ? $this->getContext($id) : null;
     $form = $this->formFactory->createNamed(null, 'sonata_classification_api_form_context', $context, array('csrf_protection' => false));
     FormHelper::removeFields($request->request->all(), $form);
     $form->handleRequest($request);
     if ($form->isValid()) {
         $context = $form->getData();
         $this->contextManager->save($context);
         $view = \FOS\RestBundle\View\View::create($context);
         $serializationContext = SerializationContext::create();
         $serializationContext->setGroups(array('sonata_api_read'));
         $serializationContext->enableMaxDepthChecks();
         $view->setSerializationContext($serializationContext);
         return $view;
     }
     return $form;
 }
Beispiel #5
0
 /**
  * @param $contextCode
  *
  * @return ContextInterface
  */
 private function getContext($contextCode)
 {
     if (empty($contextCode)) {
         $contextCode = ContextInterface::DEFAULT_CONTEXT;
     }
     if ($contextCode instanceof ContextInterface) {
         return $contextCode;
     }
     $context = $this->contextManager->find($contextCode);
     if (!$context instanceof ContextInterface) {
         $context = $this->contextManager->create();
         $context->setId($contextCode);
         $context->setName($contextCode);
         $context->setEnabled(true);
         $this->contextManager->save($context);
     }
     return $context;
 }
 /**
  * Returns list of available contexts
  *
  * @return array
  */
 public function getContextList()
 {
     $criteria = array('site' => $this->sitePool->getCurrentSite($this->getRequest()));
     return $this->contextManager->findBy($criteria, array('name' => 'asc'));
 }