protected function loadRouteForAction(Admin $admin, Action $action, RouteCollection $routeCollection) { $routingUrlPattern = $admin->getConfiguration()->routingUrlPattern; // routing pattern should contains {admin} and {action} if (strpos($routingUrlPattern, '{admin}') == -1 || strpos($routingUrlPattern, '{action}') == -1) { throw new Exception('Admin routing pattern should contains {admin} and {action} placeholder'); } // route path by entity name and action name $path = str_replace('{admin}', $admin->getEntityPath(), $routingUrlPattern); $path = str_replace('{action}', $action->getName(), $path); // by default, generic controller $defaults = ['_controller' => $admin->getController() . ':' . $action->getName(), '_admin' => $admin->getName(), '_action' => $action->getName()]; // by default, no requirements $requirements = []; // for delete and edit action, an id is required if (in_array($action, ['delete', 'edit'])) { $path .= '/{id}'; $requirements = ['id' => '\\d+']; } // creating new route $route = new Route($path, $defaults, $requirements); $routeName = $admin->generateRouteName($action->getName()); // set route to action $action->setRoute($routeName); // adding route to symfony collection $routeCollection->add($routeName, $route); }
/** * Create an Action from configuration values * * @param $actionName * @param $actionConfiguration * @param Admin $admin * @return Action */ protected function createActionFromConfig($actionName, $actionConfiguration, Admin $admin) { $resolver = new OptionsResolver(); $resolver->setDefaults($this->getDefaultActionConfiguration($actionName)); $actionConfiguration = $resolver->resolve($actionConfiguration); // guess title if not provided if (!$actionConfiguration['title']) { $actionConfiguration['title'] = $this->getDefaultActionTitle($admin->getName(), $actionName); } $action = new Action(); $action->setName($actionName); $action->setTitle($actionConfiguration['title']); $action->setPermissions($actionConfiguration['permissions']); $action->setRoute($admin->generateRouteName($action->getName())); $action->setExport($actionConfiguration['export']); // adding fields items to actions foreach ($actionConfiguration['fields'] as $fieldName => $fieldConfiguration) { $field = new Field(); $field->setName($fieldName); $field->setTitle($this->inflectString($fieldName)); if (array_key_exists('length', $fieldConfiguration)) { $field->setLength($fieldConfiguration['length']); } $action->addField($field); } return $action; }
/** * Export entities according to a type (json, csv, xls...) * * @param Admin $admin * @param $exportType * @return Response * @throws MappingException */ protected function exportEntities(Admin $admin, $exportType) { // check allowed export types $this->forward404Unless(in_array($exportType, ['json', 'html', 'xls', 'csv', 'xml'])); /** @var DataExporter $exporter */ $exporter = $this->get('ee.dataexporter'); /** @var ClassMetadata $metadata */ $metadata = $this->getEntityManager()->getClassMetadata($admin->getRepository()->getClassName()); $exportColumns = []; $fields = $metadata->getFieldNames(); $hooks = []; foreach ($fields as $fieldName) { $exporter->addHook(function ($fieldValue) { // if field is an array if (is_array($fieldValue)) { $value = $this->recursiveImplode(', ', $fieldValue); } else { if ($fieldValue instanceof DateTime) { // format date in string $value = $fieldValue->format('c'); } else { $value = $fieldValue; } } return $value; }, "{$fieldName}"); // add field column to export $exportColumns[$fieldName] = $fieldName; } $exporter->setOptions($exportType, ['fileName' => $admin->getName() . '-export-' . date('Y-m-d')])->setColumns($exportColumns)->setData($admin->getEntities()); foreach ($hooks as $hookName => $hook) { $exporter->addHook($hook, $hookName); } return $exporter->render(); }