/**
  * 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);
 }
Beispiel #2
0
 /**
  * @param AdminInterface $admin
  * @param string         $term
  * @param int            $page
  * @param int            $offset
  *
  * @return \Sonata\AdminBundle\Datagrid\PagerInterface
  *
  * @throws \RuntimeException
  */
 public function search(AdminInterface $admin, $term, $page = 0, $offset = 20)
 {
     $datagrid = $admin->getDatagrid();
     $found = false;
     foreach ($datagrid->getFilters() as $name => $filter) {
         /** @var $filter FilterInterface */
         if ($filter->getOption('global_search', false)) {
             $filter->setCondition(FilterInterface::CONDITION_OR);
             $datagrid->setValue($name, null, $term);
             $found = true;
         }
     }
     if (!$found) {
         return false;
     }
     $datagrid->buildPager();
     $pager = $datagrid->getPager();
     $pager->setPage($page);
     $pager->setMaxPerPage($offset);
     return $pager;
 }
    /**
     * 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);
    }
 /**
  * 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);
 }
 /**
  * returns the locale of the current admin user
  *
  * @param \Sonata\AdminBundle\Admin\AdminInterface $admin
  * @return mixed|string
  */
 public function getCurrentAdminLocale(\Sonata\AdminBundle\Admin\AdminInterface $admin)
 {
     $locale = '';
     if (!$admin->hasRequest()) {
         $admin->setRequest($this->getService('request'));
     }
     if ($subject = $admin->getSubject()) {
         return $this->getFieldValue($subject, 'locale');
     } elseif ($filter = $admin->getDatagrid()->getFilter('locale')) {
         /** @var \Sonata\AdminBundle\Filter\Filter $filter */
         $data = $filter->getValue();
         if (!$data || !is_array($data) || !array_key_exists('value', $data)) {
             $locale = $this->getCurrentLocale();
         }
         $data['value'] = trim($data['value']);
         if (strlen($data['value']) > 0) {
             $locale = $data['value'];
         }
         if (!$locale && method_exists($admin, 'getDefaultLocale')) {
             $locale = $admin->getDefaultLocale();
         }
     }
     return $locale;
 }