12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793 |
- <?php
- /*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Admin;
- use Symfony\Component\Form\Form;
- use Symfony\Component\Routing\RouterInterface;
- use Symfony\Component\Translation\TranslatorInterface;
- use Symfony\Component\HttpFoundation\Request;
-
- use Sonata\AdminBundle\Form\FormMapper;
- use Sonata\AdminBundle\Datagrid\ListMapper;
- use Sonata\AdminBundle\Datagrid\DatagridMapper;
- use Sonata\AdminBundle\Datagrid\Datagrid;
- use Sonata\AdminBundle\Admin\Pool;
- use Sonata\AdminBundle\Builder\FormBuilderInterface;
- use Sonata\AdminBundle\Builder\ListBuilderInterface;
- use Sonata\AdminBundle\Builder\DatagridBuilderInterface;
- use Knplabs\MenuBundle\Menu;
- use Knplabs\MenuBundle\MenuItem;
- abstract class Admin implements AdminInterface
- {
- /**
- * The class name managed by the admin class
- *
- * @var string
- */
- protected $class;
- /**
- * The list field definitions (quick property definition)
- *
- * @var array
- */
- protected $list = array();
- /**
- * The list FieldDescription constructed from the $list property
- * and the the configureListField method
- *
- * @var array
- */
- protected $listFieldDescriptions = array();
- /**
- * The form field definition (quick property definition)
- *
- * @var array
- */
- protected $form = array();
- /**
- * The list FieldDescription constructed from the $list property
- * and the the configureFormField method
- *
- * @var array
- */
- protected $formFieldDescriptions = array();
- /**
- * The filter field definition (quick property definition)
- *
- * @var array
- */
- protected $filter = array();
- /**
- * The filter FieldDescription constructed from the $list property
- * and the the configureFilterField method
- *
- * @var array
- */
- protected $filterFieldDescriptions = array();
- /**
- * The number of result to display in the list
- *
- * @var integer
- */
- protected $maxPerPage = 25;
- /**
- * The base route name used to generate the routing information
- *
- * @var string
- */
- protected $baseRouteName;
- /**
- * The base route pattern used to generate the routing information
- *
- * @var string
- */
- protected $baseRoutePattern;
- /**
- * The base name controller used to generate the routing information
- *
- * @var string
- */
- protected $baseControllerName;
- /**
- * The form group disposition
- *
- * @var array|boolean
- */
- protected $formGroups = false;
- /**
- * The label class name (used in the title/breadcrumb ...)
- *
- * @var string
- */
- protected $classnameLabel;
- /**
- * The translation domain to be used to translate messages
- *
- * @var string
- */
- protected $translationDomain = 'AdminBundle';
- /**
- * options to set to the form (ie, validation_groups)
- *
- * @var array
- */
- protected $formOptions = array();
- /**
- * The code related to the admin
- *
- * @var string
- */
- protected $code;
- /**
- * The label
- *
- * @var string
- */
- protected $label;
- /**
- * Array of urls related to this admin
- *
- * @var array
- */
- protected $urls = array();
- /**
- * The subject only set in edit/update/create mode
- *
- * @var object
- */
- protected $subject;
- /**
- * Define a Collection of child admin, ie /admin/order/{id}/order-element/{childId}
- *
- * @var array
- */
- protected $children = array();
- /**
- * Reference the parent collection
- *
- * @var Admin
- */
- protected $parent = null;
- /**
- * The base code route refer to the prefix used to generate the route name
- *
- * @var string
- */
- protected $baseCodeRoute = '';
- /**
- * The related field reflection, ie if OrderElement is linked to Order,
- * then the $parentReflectionProperty must be the ReflectionProperty of
- * the order (OrderElement::$order)
- *
- * @var \ReflectionProperty $parentReflectionProperty
- */
- protected $parentAssociationMapping = null;
- /**
- * Reference the parent FieldDescription related to this admin
- * only set for FieldDescription which is associated to an Sub Admin instance
- *
- * @var FieldDescription
- */
- protected $parentFieldDescription;
- /**
- * If true then the current admin is part of the nested admin set (from the url)
- *
- * @var boolean
- */
- protected $currentChild = false;
- /**
- * The uniqid is used to avoid clashing with 2 admin related to the code
- * ie: a Block linked to a Block
- *
- * @var string
- */
- protected $uniqid;
- /**
- * The Entity or Document manager
- *
- * @var object
- */
- protected $modelManager;
- /**
- * The current request object
- *
- * @var Symfony\Component\HttpFoundation\Request
- */
- protected $request;
- /**
- * The translator component
- *
- * @var Symfony\Component\Translation\TranslatorInterface
- */
- protected $translator;
- /**
- * The related form builder
- *
- * @var Sonata\AdminBundle\Builder\FormBuilderInterface
- */
- protected $formBuilder;
- /**
- * The related list builder
- *
- * @var Sonata\AdminBundle\Builder\ListBuilderInterface
- */
- protected $listBuilder;
- /**
- * The related datagrid builder
- *
- * @var Sonata\AdminBundle\Builder\DatagridBuilderInterface
- */
- protected $datagridBuilder;
- /**
- * The router intance
- *
- * @var Symfony\Component\Routing\RouterInterface
- */
- protected $router;
- /**
- * The configuration pool
- *
- * @var Pool
- */
- protected $configurationPool;
- protected $loaded = array(
- 'form_fields' => false,
- 'form_groups' => false,
- 'list_fields' => false,
- 'filter_fields' => false,
- 'urls' => false,
- );
- /**
- * return the doctrine class metadata handled by the Admin instance
- *
- * @return ClassMetadataInfo the doctrine class metadata handled by the Admin instance
- */
- public function getClassMetaData()
- {
- return $this->getModelManager()
- ->getClassMetaData($this->getClass());
- }
- /**
- * This method can be overwritten to tweak the form construction, by default the form
- * is built by reading the FieldDescription
- *
- * @return void
- */
- protected function configureFormFields(FormMapper $form)
- {
- }
- /**
- * overwrite this method to configure the list FormField definition
- *
- * @param ListMapper $list
- */
- protected function configureListFields(ListMapper $list)
- {
- }
- protected function configureDatagridFilters(DatagridMapper $filter)
- {
- }
- /**
- * @param string $class
- * @param string $baseControllerName
- */
- public function __construct($class, $baseControllerName)
- {
- $this->class = $class;
- $this->baseControllerName = $baseControllerName;
- }
- public function configure()
- {
- $this->uniqid = uniqid();
-
- if ($this->parentAssociationMapping) {
- if (!isset($this->getClassMetaData()->associationMappings[$this->parentAssociationMapping])) {
- throw new \RuntimeException(sprintf('The value set to `parentAssociationMapping` refer to a non existent association', $this->parentAssociationMapping));
- }
- $this->parentAssociationMapping = $this->getClassMetaData()->associationMappings[$this->parentAssociationMapping];
- }
- if (!$this->classnameLabel) {
- $this->classnameLabel = $this->urlize(substr($this->class, strrpos($this->class, '\\') + 1), '_');
- }
- $this->baseCodeRoute = $this->getCode();
- }
- public function configureUrls()
- {
- }
- public function preUpdate($object)
- {
- }
- public function postUpdate($object)
- {
- }
- public function prePersist($object)
- {
- }
- public function postPersist($object)
- {
- }
- /**
- * build the list FieldDescription array
- *
- * @return void
- */
- protected function buildListFieldDescriptions()
- {
- if ($this->loaded['list_fields']) {
- return;
- }
- $this->loaded['list_fields'] = true;
- $this->listFieldDescriptions = self::getBaseFields($this->list);
- // normalize field
- foreach ($this->listFieldDescriptions as $fieldDescription) {
- $this->getListBuilder()->fixFieldDescription($this, $fieldDescription);
- }
- if (!isset($this->listFieldDescriptions['_batch'])) {
- $fieldDescription = new FieldDescription();
- $fieldDescription->setOptions(array(
- 'label' => 'batch',
- 'code' => '_batch',
- 'type' => 'batch',
- ));
- $fieldDescription->setTemplate('SonataAdminBundle:CRUD:list__batch.html.twig');
- $this->listFieldDescriptions = array( '_batch' => $fieldDescription ) + $this->listFieldDescriptions;
- }
- return $this->listFieldDescriptions;
- }
- /**
- * build the filter FieldDescription array
- *
- * @return void
- */
- public function buildFilterFieldDescriptions()
- {
- if ($this->loaded['filter_fields']) {
- return;
- }
- $this->loaded['filter_fields'] = true;
- $this->filterFieldDescriptions = self::getBaseFields($this->filter);
- // ok, try to limit to add parent filter
- $parentAssociationMapping = $this->getParentAssociationMapping();
- if ($parentAssociationMapping) {
-
- $fieldName = $parentAssociationMapping['fieldName'];
- $this->filterFieldDescriptions[$fieldName] = new FieldDescription;
- $this->filterFieldDescriptions[$fieldName]->setName($parentAssociationMapping['fieldName']);
- $this->filterFieldDescriptions[$fieldName]->setAssociationMapping($parentAssociationMapping);
- }
- foreach ($this->filterFieldDescriptions as $fieldDescription) {
- $this->getDatagridBuilder()->fixFieldDescription($this, $fieldDescription);
- }
- }
- /**
- * return the name of the parent related field, so the field can be use to set the default
- * value (ie the parent object) or to filter the object
- *
- * @return string the name of the parent related field
- */
- public function getParentAssociationMapping()
- {
- return $this->parentAssociationMapping;
- }
- /**
- * Build the form FieldDescription collection
- *
- * @return void
- */
- protected function buildFormFieldDescriptions()
- {
- if ($this->loaded['form_fields']) {
- return;
- }
- $this->loaded['form_fields'] = true;
- $this->formFieldDescriptions = self::getBaseFields($this->form);
- foreach ($this->formFieldDescriptions as $name => &$fieldDescription) {
- $this->getFormBuilder()->fixFieldDescription($this, $fieldDescription);
- // unset the identifier field as it is not required to update an object
- if ($fieldDescription->isIdentifier()) {
- unset($this->formFieldDescriptions[$name]);
- }
- }
- }
- /**
- * make sure the base fields are set in the correct format
- *
- * @param array $selectedFields
- * @return array
- */
- static public function getBaseFields($selectedFields)
- {
- $fields = array();
- // make sure we works with array
- foreach ($selectedFields as $name => $options) {
- $description = new FieldDescription;
-
- if (!is_array($options)) {
- $name = $options;
- $options = array();
- }
- $description->setName($name);
- $description->setOptions($options);
- $fields[$name] = $description;
- }
- return $fields;
- }
- /**
- * return the baseRoutePattern used to generate the routing information
- *
- * @throws RuntimeException
- * @return string the baseRoutePattern used to generate the routing information
- */
- public function getBaseRoutePattern()
- {
- if (!$this->baseRoutePattern) {
- preg_match('@([A-Za-z]*)\\\([A-Za-z]*)Bundle\\\(Entity|Document)\\\(.*)@', $this->getClass(), $matches);
- if (!$matches) {
- throw new \RuntimeException(sprintf('Please define a default `baseRoutePattern` value for the admin class `%s`', get_class($this)));
- }
-
- if ($this->isChild()) { // the admin class is a child, prefix it with the parent route name
- $this->baseRoutePattern = sprintf('%s/{id}/%s',
- $this->getParent()->getBaseRoutePattern(),
- $this->urlize($matches[4], '-')
- );
- } else {
- $this->baseRoutePattern = sprintf('/%s/%s/%s',
- $this->urlize($matches[1], '-'),
- $this->urlize($matches[2], '-'),
- $this->urlize($matches[4], '-')
- );
- }
- }
- return $this->baseRoutePattern;
- }
- /**
- * return the baseRouteName used to generate the routing information
- *
- * @throws RuntimeException
- * @return string the baseRouteName used to generate the routing information
- */
- public function getBaseRouteName()
- {
- if (!$this->baseRouteName) {
- preg_match('@([A-Za-z]*)\\\([A-Za-z]*)Bundle\\\(Entity|Document)\\\(.*)@', $this->getClass(), $matches);
- if (!$matches) {
- throw new \RuntimeException(sprintf('Please define a default `baseRouteName` value for the admin class `%s`', get_class($this)));
- }
-
- if ($this->isChild()) { // the admin class is a child, prefix it with the parent route name
- $this->baseRouteName = sprintf('%s_%s',
- $this->getParent()->getBaseRouteName(),
- $this->urlize($matches[4])
- );
- } else {
- $this->baseRouteName = sprintf('admin_%s_%s_%s',
- $this->urlize($matches[1]),
- $this->urlize($matches[2]),
- $this->urlize($matches[4])
- );
- }
- }
- return $this->baseRouteName;
- }
- /**
- * urlize the given word
- *
- * @param string $word
- * @param string $sep the separator
- */
- public function urlize($word, $sep = '_')
- {
- return strtolower(preg_replace('/[^a-z0-9_]/i', $sep.'$1', $word));
- }
- /**
- * return the class name handled by the Admin instance
- *
- * @return string the class name handled by the Admin instance
- */
- public function getClass()
- {
- return $this->class;
- }
- /**
- * return the list of batchs actions
- *
- * @return array the list of batchs actions
- */
- public function getBatchActions()
- {
- return array(
- 'delete' => $this->trans('action_delete')
- );
- }
- /**
- * return the list of available urls
- *
- * @return array the list of available urls
- */
- public function getUrls($baseCode = '')
- {
- $this->buildUrls($baseCode);
- return $this->urls;
- }
- /**
- * return the parameter representing router id, ie: {id} or {childId}
- *
- * @return string
- */
- public function getRouterIdParameter()
- {
- return $this->isChild() ? '{childId}' : '{id}';
- }
- /**
- * return the parameter representing request id, ie: id or childId
- *
- * @return string
- */
- public function getIdParameter()
- {
- return $this->isChild() ? 'childId' : 'id';
- }
- /**
- * Build all the related urls to the current admin
- *
- * @return void
- */
- public function buildUrls()
- {
- if ($this->loaded['urls']) {
- return;
- }
- $this->loaded['urls'] = true;
-
- $this->urls = array(
- $this->baseCodeRoute . '.list' => array(
- 'name' => $this->getBaseRouteName().'_list',
- 'pattern' => $this->getBaseRoutePattern().'/list',
- 'defaults' => array(
- '_controller' => $this->getBaseControllerName().':list',
- '_sonata_admin' => $this->baseCodeRoute
- ),
- 'requirements' => array(),
- 'options' => array(),
- 'params' => array(),
- ),
- $this->baseCodeRoute . '.create' => array(
- 'name' => $this->getBaseRouteName().'_create',
- 'pattern' => $this->getBaseRoutePattern().'/create',
- 'defaults' => array(
- '_controller' => $this->getBaseControllerName().':create',
- '_sonata_admin' => $this->baseCodeRoute
- ),
- 'requirements' => array(),
- 'options' => array(),
- 'params' => array(),
- ),
- $this->baseCodeRoute . '.edit' => array(
- 'name' => $this->getBaseRouteName().'_edit',
- 'pattern' => $this->getBaseRoutePattern().'/'.$this->getRouterIdParameter().'/edit',
- 'defaults' => array(
- '_controller' => $this->getBaseControllerName().':edit',
- '_sonata_admin' => $this->baseCodeRoute
- ),
- 'requirements' => array(),
- 'options' => array(),
- 'params' => array(),
- ),
- $this->baseCodeRoute . '.update' => array(
- 'name' => $this->getBaseRouteName().'_update',
- 'pattern' => $this->getBaseRoutePattern().'/update',
- 'defaults' => array(
- '_controller' => $this->getBaseControllerName().':update',
- '_sonata_admin' => $this->baseCodeRoute
- ),
- 'requirements' => array(),
- 'options' => array(),
- 'params' => array(),
- ),
- $this->baseCodeRoute . '.delete' => array(
- 'name' => $this->getBaseRouteName().'_delete',
- 'pattern' => $this->getBaseRoutePattern().'/'.$this->getRouterIdParameter().'/delete',
- 'defaults' => array(
- '_controller' => $this->getBaseControllerName().':delete',
- '_sonata_admin' => $this->baseCodeRoute
- ),
- 'requirements' => array(),
- 'options' => array(),
- 'params' => array(),
- ),
- $this->baseCodeRoute . '.batch' => array(
- 'name' => $this->getBaseRouteName().'_batch',
- 'pattern' => $this->getBaseRoutePattern().'/batch',
- 'defaults' => array(
- '_controller' => $this->getBaseControllerName().':batch',
- '_sonata_admin' => $this->baseCodeRoute
- ),
- 'requirements' => array(),
- 'options' => array(),
- 'params' => array(),
- )
- );
-
- // add children urls
- foreach ($this->getChildren() as $children) {
- $this->urls = array_merge($this->urls, $children->getUrls());
- }
- $this->configureUrls();
- }
- /**
- * return the url defined by the $name
- *
- * @param $name
- * @return bool
- */
- public function getUrl($name)
- {
- $urls = $this->getUrls();
- if (!isset($urls[$name])) {
- return false;
- }
- return $urls[$name];
- }
- /**
- * generate the url with the given $name
- *
- * @throws RuntimeException
- * @param $name
- * @param array $parameters
- *
- * @return return a complete url
- */
- public function generateUrl($name, array $parameters = array())
- {
- if (!$this->isChild()) {
- if (strpos($name, '.')) {
- $name = $this->getCode().'|'.$name;
- } else {
- $name = $this->getCode().'.'.$name;
- }
- }
- // if the admin is a child we automatically append the parent's id
- else if ($this->isChild()) {
- $name = $this->baseCodeRoute.'.'.$name;
- // twig template does not accept variable hash key ... so cannot use admin.idparameter ...
- // switch value
- if (isset($parameters['id'])) {
- $parameters[$this->getIdParameter()] = $parameters['id'];
- unset($parameters['id']);
- }
- $parameters[$this->getParent()->getIdParameter()] = $this->request->get($this->getParent()->getIdParameter());
- }
- // if the admin is linked to a parent FieldDescription (ie, embedded widget)
- if ($this->hasParentFieldDescription()) {
- // merge link parameter if any provided by the parent field
- $parameters = array_merge($parameters, $this->getParentFieldDescription()->getOption('link_parameters', array()));
-
- $parameters['uniqid'] = $this->getUniqid();
- $parameters['code'] = $this->getCode();
- $parameters['pcode'] = $this->getParentFieldDescription()->getAdmin()->getCode();
- $parameters['puniqid'] = $this->getParentFieldDescription()->getAdmin()->getUniqid();
- }
- if ($name == 'update' || substr($name, -7) == '|update') {
- $parameters['uniqid'] = $this->getUniqid();
- $parameters['code'] = $this->getCode();
- }
- // allows to define persistent parameters
- $parameters = array_merge($this->getPersistentParameters(), $parameters);
- $url = $this->getUrl($name);
- if (!$url) {
- throw new \RuntimeException(sprintf('unable to find the url `%s`', $name));
- }
- return $this->router->generate($url['name'], $parameters);
- }
- /**
- * return the list template
- *
- * @return string the list template
- */
- public function getListTemplate()
- {
- return 'SonataAdminBundle:CRUD:list.html.twig';
- }
- /**
- * return the edit template
- *
- * @return string the edit template
- */
- public function getEditTemplate()
- {
- return 'SonataAdminBundle:CRUD:edit.html.twig';
- }
- /**
- * return the reflection fields related to the classname
- *
- * @return array The reflection fields related to the classname
- */
- public function getReflectionFields()
- {
- return $this->getClassMetaData()->reflFields;
- }
- /**
- * return an instance of the related classname
- *
- * @return Object An instance of the related classname
- */
- public function getNewInstance()
- {
- $class = $this->getClass();
- return new $class;
- }
- /**
- *
- * @return Form the base form
- */
- public function getBaseForm($object, $options = array())
- {
- return $this->getFormBuilder()->getBaseForm(
- $this->getUniqid(),
- $object,
- array_merge($this->formOptions, $options)
- );
- }
- /**
- *
- * @return Form the base form
- */
- public function getBaseDatagrid($values = array())
- {
- return new Datagrid(
- $this->getClass(),
- $this->getModelManager(),
- $values
- );
- }
- /**
- * attach an admin instance to the given FieldDescription
- *
- * @param FieldDescription $fieldDescription
- */
- public function attachAdminClass(FieldDescription $fieldDescription)
- {
- $pool = $this->getConfigurationPool();
- $admin = $pool->getAdminByClass($fieldDescription->getTargetEntity());
- if (!$admin) {
- throw new \RuntimeException(sprintf('You must define an Admin class for the `%s` field (targetEntity=%s)', $fieldDescription->getFieldName(), $fieldDescription->getTargetEntity()));
- }
- $fieldDescription->setAssociationAdmin($admin);
- }
- /**
- * return the target object
- *
- * @param integer $id
- * @return
- */
- public function getObject($id)
- {
- return $this->getModelManager()
- ->find($this->getClass(), $id);
- }
- /**
- * build the form group array
- *
- * @return void
- */
- public function buildFormGroups()
- {
- if ($this->loaded['form_groups']) {
- return;
- }
- $this->loaded['form_groups'] = true;
- if (!$this->formGroups) {
- $this->formGroups = array(
- false => array('fields' => array_keys($this->getFormFieldDescriptions()))
- );
- }
- // normalize array
- foreach ($this->formGroups as $name => $group) {
- if (!isset($this->formGroups[$name]['collapsed'])) {
- $this->formGroups[$name]['collapsed'] = false;
- }
- }
- }
- /**
- * return a form depend on the given $object
- *
- * @param object $object
- * @param array $options the form options
- * @return Symfony\Component\Form\Form
- */
- public function getForm($object, array $options = array())
- {
- // append parent object if any
- // todo : clean the way the Admin class can retrieve set the object
- if ($this->isChild() && $this->getParentAssociationMapping()) {
- $mapping = $this->getParentAssociationMapping();
- $parent = $this->getParent()->getObject($this->request->get($this->getParent()->getIdParameter()));
- $propertyPath = new \Symfony\Component\Form\PropertyPath($mapping['fieldName']);
- $propertyPath->setValue($object, $parent);
- }
- $form = $this->getBaseForm($object, $options);
- $mapper = new FormMapper($this->getFormBuilder(), $form, $this);
- $this->buildFormFieldDescriptions();
- $this->configureFormFields($mapper);
- foreach ($this->getFormFieldDescriptions() as $fieldDescription) {
- // do not add field already set in the configureFormField method
- if ($mapper->has($fieldDescription->getFieldName())) {
- continue;
- }
- $mapper->add($fieldDescription);
- }
-
- return $form;
- }
- /**
- * return a list depend on the given $object
- *
- * @param $object
- * @return Symfony\Component\Datagrid\ListCollection
- */
- public function getList(array $options = array())
- {
- $list = $this->getListBuilder()->getBaseList($options);
- $mapper = new ListMapper($this->getListBuilder(), $list, $this);
- $this->buildListFieldDescriptions();
-
- $this->configureListFields($mapper);
- foreach ($this->getListFieldDescriptions() as $fieldDescription) {
- // do not add field already set in the configureFormField method
- if ($mapper->has($fieldDescription->getFieldName())) {
- continue;
- }
- $mapper->add($fieldDescription);
- }
- return $list;
- }
- /**
- * return a list depend on the given $object
- *
- * @param $object
- * @return Symfony\Component\Datagrid\Datagrid
- */
- public function getDatagrid()
- {
- $parameters = $this->request->query->all();
- $datagrid = $this->getBaseDatagrid($parameters);
- $datagrid->setMaxPerPage($this->maxPerPage);
- if ($this->isChild() && $this->getParentAssociationMapping()) {
- $mapping = $this->getParentAssociationMapping();
- $parameters[$mapping['fieldName']] = $this->request->get($this->getParent()->getIdParameter());
- }
- $datagrid->setValues($parameters);
- $mapper = new DatagridMapper($this->getDatagridBuilder(), $datagrid, $this);
- $this->buildFilterFieldDescriptions();
- $this->configureDatagridFilters($mapper);
- foreach ($this->getFilterFieldDescriptions() as $fieldDescription) {
- $mapper->add($fieldDescription);
- }
- return $datagrid;
- }
- /**
- * Build the side menu related to the current action
- *
- * @return MenuItem|false
- */
- public function getSideMenu($action)
- {
- return false;
- }
- /**
- * return the root code
- *
- * @return string the root code
- */
- public function getRootCode()
- {
- return $this->getRoot()->getCode();
- }
- /**
- * return the master admin
- *
- * @return Admin the root admin class
- */
- public function getRoot()
- {
- $parentFieldDescription = $this->getParentFieldDescription();
- if (!$parentFieldDescription) {
- return $this;
- }
- return $parentFieldDescription->getAdmin()->getRoot();
- }
- public function setBaseControllerName($baseControllerName)
- {
- $this->baseControllerName = $baseControllerName;
- }
- public function getBaseControllerName()
- {
- return $this->baseControllerName;
- }
- public function setLabel($label)
- {
- $this->label = $label;
- }
- public function getLabel()
- {
- return $this->label;
- }
- public function setMaxPerPage($maxPerPage)
- {
- $this->maxPerPage = $maxPerPage;
- }
- public function getMaxPerPage()
- {
- return $this->maxPerPage;
- }
- public function setFormGroups($formGroups)
- {
- $this->formGroups = $formGroups;
- }
- public function getFormGroups()
- {
- $this->buildFormGroups();
- return $this->formGroups;
- }
- /**
- * set the parent FieldDescription
- *
- * @param FieldDescription $parentFieldDescription
- * @return void
- */
- public function setParentFieldDescription(FieldDescription $parentFieldDescription)
- {
- $this->parentFieldDescription = $parentFieldDescription;
- }
- /**
- *
- * @return FieldDescription the parent field description
- */
- public function getParentFieldDescription()
- {
- return $this->parentFieldDescription;
- }
- /**
- * return true if the Admin is linked to a parent FieldDescription
- *
- * @return bool
- */
- public function hasParentFieldDescription()
- {
- return $this->parentFieldDescription instanceof FieldDescription;
- }
- /**
- * set the subject linked to the admin, the subject is the related model
- *
- * @param object $subject
- * @return void
- */
- public function setSubject($subject)
- {
- $this->subject = $subject;
- }
- /**
- * return the subject, if none is set try to load one from the request
- *
- * @return $object the subject
- */
- public function getSubject()
- {
- if ($this->subject === null) {
- $id = $this->request->get($this->getIdParameter());
- if (!is_numeric($id)) {
- $this->subject = false;
- } else {
- $this->subject = $this->getModelManager()->find(
- $this->getClass(),
- $id
- );
- }
- }
- return $this->subject;
- }
- /**
- * build and return the collection of form FieldDescription
- *
- * @return array collection of form FieldDescription
- */
- public function getFormFieldDescriptions()
- {
- $this->buildFormFieldDescriptions();
-
- return $this->formFieldDescriptions;
- }
- /**
- * return the form FieldDescription with the given $name
- *
- * @param string $name
- * @return FieldDescription
- */
- public function getFormFieldDescription($name)
- {
- return $this->hasFormFieldDescription($name) ? $this->formFieldDescriptions[$name] : null;
- }
- /**
- * return true if the admin has a FieldDescription with the given $name
- *
- * @param string $name
- * @return bool
- */
- public function hasFormFieldDescription($name)
- {
- $this->buildFormFieldDescriptions();
- return array_key_exists($name, $this->formFieldDescriptions) ? true : false;
- }
- /**
- * add a FieldDescription
- *
- * @param string $name
- * @param FieldDescription $fieldDescription
- * @return void
- */
- public function addFormFieldDescription($name, FieldDescription $fieldDescription)
- {
- $this->formFieldDescriptions[$name] = $fieldDescription;
- }
- /**
- * remove a FieldDescription
- *
- * @param string $name
- * @return void
- */
- public function removeFormFieldDescription($name)
- {
- unset($this->formFieldDescriptions[$name]);
- }
- /**
- * return the collection of list FieldDescriptions
- *
- * @return array
- */
- public function getListFieldDescriptions()
- {
- $this->buildListFieldDescriptions();
-
- return $this->listFieldDescriptions;
- }
- /**
- * return a list FieldDescription
- *
- * @param string $name
- * @return FieldDescription
- */
- public function getListFieldDescription($name) {
- return $this->hasListFieldDescription($name) ? $this->listFieldDescriptions[$name] : null;
- }
- /**
- * return true if the list FieldDescription exists
- *
- * @param string $name
- * @return bool
- */
- public function hasListFieldDescription($name)
- {
- $this->buildListFieldDescriptions();
- return array_key_exists($name, $this->listFieldDescriptions) ? true : false;
- }
- /**
- * add a list FieldDescription
- *
- * @param string $name
- * @param FieldDescription $fieldDescription
- * @return void
- */
- public function addListFieldDescription($name, FieldDescription $fieldDescription)
- {
- $this->listFieldDescriptions[$name] = $fieldDescription;
- }
- /**
- * remove a list FieldDescription
- *
- * @param string $name
- * @return void
- */
- public function removeListFieldDescription($name)
- {
- unset($this->listFieldDescriptions[$name]);
- }
- /**
- * return a filter FieldDescription
- *
- * @param string $name
- * @return array|null
- */
- public function getFilterFieldDescription($name) {
- return $this->hasFilterFieldDescription($name) ? $this->filterFieldDescriptions[$name] : null;
- }
- /**
- * return true if the filter FieldDescription exists
- *
- * @param string $name
- * @return bool
- */
- public function hasFilterFieldDescription($name)
- {
- $this->buildFilterFieldDescriptions();
- return array_key_exists($name, $this->filterFieldDescriptions) ? true : false;
- }
- /**
- * add a filter FieldDescription
- *
- * @param string $name
- * @param FieldDescription $fieldDescription
- * @return void
- */
- public function addFilterFieldDescription($name, FieldDescription $fieldDescription)
- {
- $this->filterFieldDescriptions[$name] = $fieldDescription;
- }
- /**
- * remove a filter FieldDescription
- *
- * @param string $name
- */
- public function removeFilterFieldDescription($name)
- {
- unset($this->filterFieldDescriptions[$name]);
- }
- /**
- * return the filter FieldDescription collection
- *
- * @param array filter FieldDescription collection
- */
- public function getFilterFieldDescriptions()
- {
- $this->buildFilterFieldDescriptions();
-
- return $this->filterFieldDescriptions;
- }
- /**
- * add an Admin child to the current one
- *
- * @param string $code
- * @param Admin $child
- * @return void
- */
- public function addChild($code, AdminInterface $child)
- {
- $this->children[$code] = $child;
-
- $child->setCode($code);
- $child->setBaseCodeRoute($this->getCode().'|'.$code);
- $child->setParent($this);
- }
- /**
- * Returns true or false if an Admin child exists for the given $code
- *
- * @param string $code Admin code
- * @return bool True if child exist, false otherwise
- */
- public function hasChild($code)
- {
- return isset($this->children[$code]);
- }
- /**
- * return an collection of admin children
- *
- * @return array list of Admin children
- */
- public function getChildren()
- {
- return $this->children;
- }
- /**
- * return an admin child with the given $code
- *
- * @param string $code
- * @return array|null
- */
- public function getChild($code)
- {
- return $this->hasChild($code) ? $this->children[$code] : null;
- }
- /**
- * set the Parent Admin
- *
- * @param Admin $parent
- * @return void
- */
- public function setParent(AdminInterface $parent)
- {
- $this->parent = $parent;
- }
- /**
- * get the Parent Admin
- *
- * @return Admin|null
- */
- public function getParent()
- {
- return $this->parent;
- }
- /**
- * return true if the Admin class has an Parent Admin defined
- *
- * @return boolean
- */
- public function isChild()
- {
- return $this->parent instanceof AdminInterface;
- }
- /**
- * return true if the admin has children, false otherwise
- *
- * @return bool if the admin has children
- */
- public function hasChildren()
- {
- return count($this->children) > 0;
- }
- /**
- * set the uniqid
- *
- * @param $uniqid
- * @return void
- */
- public function setUniqid($uniqid)
- {
- $this->uniqid = $uniqid;
- }
- /**
- * return the uniqid
- *
- * @return integer
- */
- public function getUniqid()
- {
- return $this->uniqid;
- }
- /**
- * return the classname label
- *
- * @return string the classname label
- */
- public function getClassnameLabel()
- {
- return $this->classnameLabel;
- }
- /**
- * return an array of persistent parameters
- *
- * @return array
- */
- public function getPersistentParameters()
- {
- return array();
- }
- /**
- * generate the breadcrumbs array
- *
- * @param $action
- * @param \Knplabs\MenuBundle\MenuItem|null $menu
- * @return array the breadcrumbs
- */
- public function getBreadcrumbs($action, MenuItem $menu = null)
- {
- $menu = $menu ?: new Menu;
- $child = $menu->addChild(
- $this->trans(sprintf('link_%s_list', $this->getClassnameLabel())),
- $this->generateUrl('list')
- );
-
- $childAdmin = $this->getCurrentChildAdmin();
- if ($childAdmin) {
- $id = $this->request->get($this->getIdParameter());
- $child = $child->addChild(
- (string) $this->getSubject(),
- $this->generateUrl('edit', array('id' => $id))
- );
- return $childAdmin->getBreadcrumbs($action, $child);
-
- } elseif ($this->isChild()) {
- if ($action != 'list') {
- $menu = $menu->addChild(
- $this->trans(sprintf('link_%s_list', $this->getClassnameLabel())),
- $this->generateUrl('list')
- );
- }
- $breadcrumbs = $menu->getBreadcrumbsArray(
- $this->trans(sprintf('link_%s_%s', $this->getClassnameLabel(), $action))
- );
- } else if ($action != 'list') {
- $breadcrumbs = $child->getBreadcrumbsArray(
- $this->trans(sprintf('link_%s_%s', $this->getClassnameLabel(), $action))
- );
- } else {
- $breadcrumbs = $child->getBreadcrumbsArray();
- }
- // the generated $breadcrumbs contains an empty element
- array_shift($breadcrumbs);
- return $breadcrumbs;
- }
- /**
- * set the current child status
- *
- * @param boolean $currentChild
- * @return void
- */
- public function setCurrentChild($currentChild)
- {
- $this->currentChild = $currentChild;
- }
- /**
- * return the current child status
- *
- * @return bool
- */
- public function getCurrentChild()
- {
- return $this->currentChild;
- }
- /**
- * return the current child admin instance
- *
- * @return Admin|null the current child admin instance
- */
- public function getCurrentChildAdmin()
- {
- foreach ($this->children as $children) {
- if ($children->getCurrentChild()) {
- return $children;
- }
- }
- return null;
- }
- /**
- * translate a message id
- *
- * @param string $id
- * @param array $parameters
- * @param null $domain
- * @param null $locale
- * @return string the translated string
- */
- public function trans($id, array $parameters = array(), $domain = null, $locale = null)
- {
- $domain = $domain ?: $this->translationDomain;
- if (!$this->translator) {
- return $id;
- }
- return $this->translator->trans($id, $parameters, $domain, $locale);
- }
- /**
- * set the translation domain
- *
- * @param string $translationDomain the translation domain
- * @return void
- */
- public function setTranslationDomain($translationDomain)
- {
- $this->translationDomain = $translationDomain;
- }
- /**
- * return the translation domain
- *
- * @return string the translation domain
- */
- public function getTranslationDomain()
- {
- return $this->translationDomain;
- }
- /**
- *
- */
- public function setTranslator(TranslatorInterface $translator)
- {
- $this->translator = $translator;
- }
- /**
- *
- */
- public function getTranslator()
- {
- return $this->translator;
- }
- /**
- * @param \Symfony\Component\HttpFoundation\Request $request
- * @return void
- */
- public function setRequest(Request $request)
- {
- $this->request = $request;
- if ($request->get('uniqid')) {
- $this->setUniqid($request->get('uniqid'));
- }
- }
- /**
- * @return Symfony\Component\HttpFoundation\Request
- */
- public function getRequest()
- {
- return $this->request;
- }
- /**
- * @param \Sonata\AdminBundle\Builder\FormBuilderInterface $formBuilder
- * @return void
- */
- public function setFormBuilder(FormBuilderInterface $formBuilder)
- {
- $this->formBuilder = $formBuilder;
- }
- /**
- * @return Sonata\AdminBundle\Builder\FormBuilderInterface
- */
- public function getFormBuilder()
- {
- return $this->formBuilder;
- }
- /**
- * @param \Sonata\AdminBundle\Builder\DatagridBuilderInterface $datagridBuilder
- * @return void
- */
- public function setDatagridBuilder(DatagridBuilderInterface $datagridBuilder)
- {
- $this->datagridBuilder = $datagridBuilder;
- }
- /**
- * @return Sonata\AdminBundle\Builder\DatagridBuilderInterface
- */
- public function getDatagridBuilder()
- {
- return $this->datagridBuilder;
- }
- /**
- * @param \Sonata\AdminBundle\Builder\ListBuilderInterface $listBuilder
- * @return void
- */
- public function setListBuilder(ListBuilderInterface $listBuilder)
- {
- $this->listBuilder = $listBuilder;
- }
- /**
- * @return Sonata\AdminBundle\Builder\ListBuilderInterface
- */
- public function getListBuilder()
- {
- return $this->listBuilder;
- }
- /**
- * @param Pool $configurationPool
- * @return void
- */
- public function setConfigurationPool(Pool $configurationPool)
- {
- $this->configurationPool = $configurationPool;
- }
- /**
- * @return Pool
- */
- public function getConfigurationPool()
- {
- return $this->configurationPool;
- }
- /**
- * @param \Symfony\Component\Routing\RouterInterface $router
- * @return void
- */
- public function setRouter(RouterInterface $router)
- {
- $this->router = $router;
- }
- /**
- * @return Symfony\Component\Routing\RouterInterface
- */
- public function getRouter()
- {
- return $this->router;
- }
- /**
- * @param $modelManager
- * @return void
- */
- public function setModelManager($modelManager)
- {
- $this->modelManager = $modelManager;
- }
- /**
- * @return object
- */
- public function getModelManager()
- {
- return $this->modelManager;
- }
- public function setCode($code)
- {
- $this->code = $code;
- }
- public function getCode()
- {
- return $this->code;
- }
- public function setBaseCodeRoute($baseCodeRoute)
- {
- $this->baseCodeRoute = $baseCodeRoute;
- }
- public function getBaseCodeRoute()
- {
- return $this->baseCodeRoute;
- }
- }
|