Ejemplo n.º 1
0
 /**
  * get grid object filled
  *
  * @param  \Kitpages\DataGridBundle\Grid\GridConfig $gridConfig
  * @param  \Symfony\Component\HttpFoundation\Request $request
  * @return \Kitpages\DataGridBundle\Grid\Grid
  */
 public function getGrid(GridConfig $gridConfig, Request $request)
 {
     $queryBuilder = $gridConfig->getQueryBuilder();
     // create grid objet
     $grid = new Grid();
     $grid->setGridConfig($gridConfig);
     $grid->setUrlTool(new UrlTool());
     $grid->setRequestUri($request->getRequestUri());
     $grid->setRequestCurrentRoute($request->attributes->get("_route"));
     $grid->setRequestCurrentRouteParams($request->attributes->get("_route_params"));
     $grid->setDispatcher($this->dispatcher);
     // create base request
     $gridQueryBuilder = clone $queryBuilder;
     // Apply filters
     $filter = $request->query->get($grid->getFilterFormName(), "");
     $this->applyFilter($gridQueryBuilder, $grid, $filter);
     // Apply selector
     $selectorField = $request->query->get($grid->getSelectorFieldFormName(), "");
     $selectorValue = $request->query->get($grid->getSelectorValueFormName(), "");
     $this->applySelector($gridQueryBuilder, $grid, $selectorField, $selectorValue);
     // Apply sorting
     $sortField = $request->query->get($grid->getSortFieldFormName(), "");
     $sortOrder = $request->query->get($grid->getSortOrderFormName(), "");
     $this->applySort($gridQueryBuilder, $grid, $sortField, $sortOrder);
     // build paginator
     $paginatorConfig = $gridConfig->getPaginatorConfig();
     if ($paginatorConfig === null) {
         $paginatorConfig = new PaginatorConfig();
         $paginatorConfig->setCountFieldName($gridConfig->getCountFieldName());
         $paginatorConfig->setName($gridConfig->getName());
         $paginatorConfig->setQueryBuilder($gridQueryBuilder);
     }
     if (is_null($paginatorConfig->getQueryBuilder())) {
         $paginatorConfig->setQueryBuilder($gridQueryBuilder);
     }
     $paginator = $this->paginatorManager->getPaginator($paginatorConfig, $request);
     $grid->setPaginator($paginator);
     // calculate limits
     $gridQueryBuilder->setMaxResults($paginator->getPaginatorConfig()->getItemCountInPage());
     $gridQueryBuilder->setFirstResult(($paginator->getCurrentPage() - 1) * $paginator->getPaginatorConfig()->getItemCountInPage());
     // send event for changing grid query builder
     $event = new DataGridEvent();
     $event->set("grid", $grid);
     $event->set("gridQueryBuilder", $gridQueryBuilder);
     $event->set("request", $request);
     $this->dispatcher->dispatch(KitpagesDataGridEvents::ON_GET_GRID_QUERY, $event);
     if (!$event->isDefaultPrevented()) {
         // execute request
         $query = $gridQueryBuilder->getQuery();
         $event->set("query", $query);
     }
     $this->dispatcher->dispatch(KitpagesDataGridEvents::AFTER_GET_GRID_QUERY, $event);
     // hack : recover query from the event so the developper can build a new grid
     // from the gridQueryBuilder in the listener and reinject it in the event.
     $normalizedItemList = $this->itemListNormalizer->normalize($event->get("query"), $event->get("gridQueryBuilder"), $this->hydratorClass);
     // end normalization
     $grid->setItemList($normalizedItemList);
     return $grid;
 }
 public function testAddFieldWrongArgumentType()
 {
     $arguments = array(true, 1, 2.2, array(), new \stdClass(), null, function () {
     });
     foreach ($arguments as $argument) {
         try {
             $this->gridConfig->addField($argument);
             $this->fail();
         } catch (\InvalidArgumentException $e) {
             $this->assertTrue(true);
         }
     }
 }
 public function testGridBasic()
 {
     // create Request mock (ok this is not a mock....)
     $_SERVER["REQUEST_URI"] = "/foo";
     $request = new \Symfony\Component\HttpFoundation\Request();
     // create gridManager instance
     $gridManager = $this->getGridManager();
     // create queryBuilder
     $em = $this->getEntityManager();
     $repository = $em->getRepository('Kitpages\\DataGridBundle\\Tests\\TestEntities\\Node');
     $queryBuilder = $repository->createQueryBuilder("node");
     $queryBuilder->select("node");
     $gridConfig = new GridConfig();
     $gridConfig->setCountFieldName("node.id");
     $gridConfig->addField(new Field("node.createdAt", array("sortable" => true, "formatValueCallback" => function ($value) {
         return $value->format("Y/m/d");
     })));
     $gridConfig->addField(new Field("node.content", array("formatValueCallback" => function ($value, $row) {
         return $value . ":" . $row["node.createdAt"]->format("Y");
     })));
     // get paginator
     $gridConfig->setQueryBuilder($queryBuilder);
     $grid = $gridManager->getGrid($gridConfig, $request);
     $paginator = $grid->getPaginator();
     // tests paginator
     $this->assertEquals(11, $paginator->getTotalItemCount());
     // grid test
     $itemList = $grid->getItemList();
     $this->assertEquals(11, count($itemList));
     $this->assertEquals(1, $itemList[0]["node.id"]);
     // simple callback
     $this->assertEquals("2010/04/24", $grid->displayGridValue($itemList[0], $gridConfig->getFieldByName("node.createdAt")));
     $this->assertEquals("foobar:2010", $grid->displayGridValue($itemList[0], $gridConfig->getFieldByName("node.content")));
 }
 /**
  * @return Grid
  */
 protected function getGrid()
 {
     $queryBuilder = $this->getDoctrine()->getRepository('AppBundle:RegistrationEvent')->getGridQuery();
     $gridConfig = new GridConfig();
     $gridConfig->setQueryBuilder($queryBuilder)->setCountFieldName('rE.id')->addField(new Field('id', ['label' => 'admin.registration_event.field.id', 'sortable' => true, 'filterable' => true, 'entityFields' => ['rE.id']]))->addField(new Field('domain', ['label' => 'admin.registration_event.field.domain', 'sortable' => true, 'filterable' => true, 'entityFields' => ['rE.domain']]))->addField(new Field('venue', ['label' => 'admin.registration_event.field.venue', 'sortable' => true, 'filterable' => true, 'entityFields' => ['rE.venue']]))->addField(new Field('address', ['label' => 'admin.registration_event.field.address', 'sortable' => true, 'filterable' => true, 'entityFields' => ['rE.address']]))->addField(new Field('city.name', ['label' => 'admin.registration_event.field.city', 'sortable' => true, 'filterable' => true, 'entityFields' => ['cT.name']]))->addField(new Field('state.name', ['label' => 'admin.registration_event.field.state', 'sortable' => true, 'filterable' => true, 'entityFields' => ['s.name']]))->addField(new Field('country.name', ['label' => 'admin.registration_event.field.country', 'sortable' => true, 'filterable' => true, 'entityFields' => ['c.name']]))->addField(new Field('postalCode', ['label' => 'admin.registration_event.field.postal_code', 'sortable' => true, 'filterable' => true, 'entityFields' => ['rE.postalCode']]))->addField(new Field('date', ['label' => 'admin.registration_event.field.date', 'sortable' => true, 'filterable' => true, 'entityFields' => ['rE.date'], 'formatValueCallback' => function ($value) {
         return $value->format('Y-m-d');
     }]));
     return $this->get('kitpages_data_grid.grid_manager')->getGrid($gridConfig, $this->getRequest());
 }