/**
  * Camelize a string.
  *
  * @static
  *
  * @param string $property
  *
  * @return string
  */
 public function camelize($property)
 {
     return BaseFieldDescription::camelize($property);
 }
 /**
  * Apply the default values required by the AdminInterface to the Admin service definition
  *
  * @param  \Symfony\Component\DependencyInjection\ContainerBuilder $container
  * @param  string                                                  $serviceId
  * @param  array                                                   $attributes
  *
  * @return \Symfony\Component\DependencyInjection\Definition
  */
 public function applyDefaults(ContainerBuilder $container, $serviceId, array $attributes = array())
 {
     $definition = $container->getDefinition($serviceId);
     $settings = $container->getParameter('sonata.admin.configuration.admin_services');
     $definition->setScope(ContainerInterface::SCOPE_PROTOTYPE);
     $manager_type = $attributes['manager_type'];
     $addServices = isset($settings[$serviceId]) ? $settings[$serviceId] : array();
     $defaultAddServices = array('model_manager' => sprintf('sonata.admin.manager.%s', $manager_type), 'form_contractor' => sprintf('sonata.admin.builder.%s_form', $manager_type), 'show_builder' => sprintf('sonata.admin.builder.%s_show', $manager_type), 'list_builder' => sprintf('sonata.admin.builder.%s_list', $manager_type), 'datagrid_builder' => sprintf('sonata.admin.builder.%s_datagrid', $manager_type), 'translator' => 'translator', 'configuration_pool' => 'sonata.admin.pool', 'route_generator' => 'sonata.admin.route.default_generator', 'validator' => 'validator', 'security_handler' => 'sonata.admin.security.handler', 'menu_factory' => 'knp_menu.factory', 'route_builder' => 'sonata.admin.route.path_info' . ($manager_type == 'doctrine_phpcr' ? '_slashes' : ''), 'label_translator_strategy' => 'sonata.admin.label.strategy.native');
     $definition->addMethodCall('setManagerType', array($manager_type));
     foreach ($defaultAddServices as $attr => $addServiceId) {
         $method = 'set' . BaseFieldDescription::camelize($attr);
         if (isset($addServices[$attr]) || !$definition->hasMethodCall($method)) {
             $definition->addMethodCall($method, array(new Reference(isset($addServices[$attr]) ? $addServices[$attr] : $addServiceId)));
         }
     }
     if (isset($service['label'])) {
         $label = $service['label'];
     } elseif (isset($attributes['label'])) {
         $label = $attributes['label'];
     } else {
         $label = '-';
     }
     $definition->addMethodCall('setLabel', array($label));
     if (isset($attributes['persist_filters'])) {
         $persistFilters = (bool) $attributes['persist_filters'];
     } else {
         $persistFilters = (bool) $container->getParameter('sonata.admin.configuration.filters.persist');
     }
     $definition->addMethodCall('setPersistFilters', array($persistFilters));
     $this->fixTemplates($container, $definition);
     if ($container->hasParameter('sonata.admin.configuration.security.information') && !$definition->hasMethodCall('setSecurityInformation')) {
         $definition->addMethodCall('setSecurityInformation', array('%sonata.admin.configuration.security.information%'));
     }
     $definition->addMethodCall('initialize');
     return $definition;
 }
Example #3
0
 /**
  * Batch action.
  *
  * @return Response|RedirectResponse
  *
  * @throws NotFoundHttpException If the HTTP method is not POST
  * @throws \RuntimeException     If the batch action is not defined
  */
 public function batchAction()
 {
     $restMethod = $this->getRestMethod();
     if ('POST' !== $restMethod) {
         throw $this->createNotFoundException(sprintf('Invalid request type "%s", POST expected', $restMethod));
     }
     // check the csrf token
     $this->validateCsrfToken('sonata.batch');
     $confirmation = $this->get('request')->get('confirmation', false);
     if ($data = json_decode($this->get('request')->get('data'), true)) {
         $action = $data['action'];
         $idx = $data['idx'];
         $allElements = $data['all_elements'];
         $this->get('request')->request->replace($data);
     } else {
         $this->get('request')->request->set('idx', $this->get('request')->get('idx', array()));
         $this->get('request')->request->set('all_elements', $this->get('request')->get('all_elements', false));
         $action = $this->get('request')->get('action');
         $idx = $this->get('request')->get('idx');
         $allElements = $this->get('request')->get('all_elements');
         $data = $this->get('request')->request->all();
         unset($data['_sonata_csrf_token']);
     }
     $batchActions = $this->admin->getBatchActions();
     if (!array_key_exists($action, $batchActions)) {
         throw new \RuntimeException(sprintf('The `%s` batch action is not defined', $action));
     }
     $camelizedAction = BaseFieldDescription::camelize($action);
     $isRelevantAction = sprintf('batchAction%sIsRelevant', ucfirst($camelizedAction));
     if (method_exists($this, $isRelevantAction)) {
         $nonRelevantMessage = call_user_func(array($this, $isRelevantAction), $idx, $allElements);
     } else {
         $nonRelevantMessage = count($idx) != 0 || $allElements;
         // at least one item is selected
     }
     if (!$nonRelevantMessage) {
         // default non relevant message (if false of null)
         $nonRelevantMessage = 'flash_batch_empty';
     }
     $datagrid = $this->admin->getDatagrid();
     $datagrid->buildPager();
     if (true !== $nonRelevantMessage) {
         $this->addFlash('sonata_flash_info', $nonRelevantMessage);
         return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters())));
     }
     $askConfirmation = isset($batchActions[$action]['ask_confirmation']) ? $batchActions[$action]['ask_confirmation'] : true;
     if ($askConfirmation && $confirmation != 'ok') {
         $actionLabel = $batchActions[$action]['label'];
         $formView = $datagrid->getForm()->createView();
         return $this->render($this->admin->getTemplate('batch_confirmation'), array('action' => 'list', 'action_label' => $actionLabel, 'datagrid' => $datagrid, 'form' => $formView, 'data' => $data, 'csrf_token' => $this->getCsrfToken('sonata.batch')));
     }
     // execute the action, batchActionXxxxx
     $finalAction = sprintf('batchAction%s', ucfirst($camelizedAction));
     if (!method_exists($this, $finalAction)) {
         throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $finalAction));
     }
     $query = $datagrid->getQuery();
     $query->setFirstResult(null);
     $query->setMaxResults(null);
     $this->admin->preBatchAction($action, $query, $idx, $allElements);
     if (count($idx) > 0) {
         $this->admin->getModelManager()->addIdentifiersToQuery($this->admin->getClass(), $query, $idx);
     } elseif (!$allElements) {
         $query = null;
     }
     return call_user_func(array($this, $finalAction), $query);
 }
 public function testCamelize()
 {
     $this->assertEquals('FooBar', BaseFieldDescription::camelize('foo_bar'));
     $this->assertEquals('FooBar', BaseFieldDescription::camelize('foo bar'));
     $this->assertEquals('FOoBar', BaseFieldDescription::camelize('fOo bar'));
 }
 /**
  * return the Response object associated to the batch action
  *
  * @throws \RuntimeException
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function batchAction()
 {
     if ($this->get('request')->getMethod() != 'POST') {
         throw new \RuntimeException('invalid request type, POST expected');
     }
     if ($data = json_decode($this->get('request')->get('data'), true)) {
         $action = $data['action'];
         $idx = $data['idx'];
         $all_elements = $data['all_elements'];
     } else {
         $action = $this->get('request')->get('action');
         $idx = $this->get('request')->get('idx');
         $all_elements = $this->get('request')->get('all_elements', false);
     }
     $batchActions = $this->admin->getBatchActions();
     if (!array_key_exists($action, $batchActions)) {
         throw new \RuntimeException(sprintf('The `%s` batch action is not defined', $action));
     }
     if (count($idx) == 0 && !$all_elements) {
         // no item selected
         $this->get('session')->setFlash('sonata_flash_info', 'flash_batch_empty');
         return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
     }
     $askConfirmation = isset($batchActions[$action]['ask_confirmation']) ? $batchActions[$action]['ask_confirmation'] : true;
     if ($askConfirmation && $this->get('request')->get('confirmation') != 'ok') {
         $data = json_encode(array('action' => $action, 'idx' => $idx, 'all_elements' => $all_elements));
         $datagrid = $this->admin->getDatagrid();
         $formView = $datagrid->getForm()->createView();
         return $this->render('SonataAdminBundle:CRUD:batch_confirmation.html.twig', array('action' => 'list', 'datagrid' => $datagrid, 'form' => $formView, 'data' => $data));
     }
     // execute the action, batchActionXxxxx
     $action = \Sonata\AdminBundle\Admin\BaseFieldDescription::camelize($action);
     $final_action = sprintf('batchAction%s', ucfirst($action));
     if (!method_exists($this, $final_action)) {
         throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $final_action));
     }
     $datagrid = $this->admin->getDatagrid();
     $datagrid->buildPager();
     $query = $datagrid->getQuery();
     $query->setFirstResult(null);
     $query->setMaxResults(null);
     if (count($idx) > 0) {
         $this->admin->getModelManager()->addIdentifiersToQuery($this->admin->getClass(), $query, $idx);
     }
     return call_user_func(array($this, $final_action), $query);
 }
    /**
     * return the Response object associated to the batch action
     *
     * @throws \RuntimeException
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function batchAction()
    {
        if ($this->get('request')->getMethod() != 'POST') {
           throw new \RuntimeException('invalid request type, POST expected');
        }

        $action       = $this->get('request')->get('action');
        $idx          = $this->get('request')->get('idx');
        $all_elements = $this->get('request')->get('all_elements', false);

        if (count($idx) == 0 && !$all_elements) { // no item selected
            $this->get('session')->setFlash('sonata_flash_notice', 'flash_batch_empty');

            return new RedirectResponse($this->admin->generateUrl('list', $this->admin->getFilterParameters()));
        }

        if (!array_key_exists($action, $this->admin->getBatchActions())) {
            throw new \RuntimeException(sprintf('The `%s` batch action is not defined', $action));
        }

        // execute the action, batchActionXxxxx
        $action = \Sonata\AdminBundle\Admin\BaseFieldDescription::camelize($action);

        $final_action = sprintf('batchAction%s', ucfirst($action));
        if (!method_exists($this, $final_action)) {
            throw new \RuntimeException(sprintf('A `%s::%s` method must be created', get_class($this), $final_action));
        }

        $datagrid = $this->admin->getDatagrid();
        $datagrid->buildPager();
        $query = $datagrid->getQuery();

        $query->setFirstResult(null);
        $query->setMaxResults(null);

        if (count($idx) > 0) {
            $this->admin->getModelManager()->addIdentifiersToQuery($this->admin->getClass(), $query, $idx);
        }
        return call_user_func(array($this, $final_action), $query);
    }