| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace LAG\AdminBundle\Field\Field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use LAG\AdminBundle\Field\EntityFieldInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use LAG\AdminBundle\Field\Field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use LAG\AdminBundle\Field\TwigFieldInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Symfony\Component\OptionsResolver\Options; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Symfony\Component\OptionsResolver\OptionsResolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Symfony\Component\PropertyAccess\PropertyAccess; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class Link extends StringField implements EntityFieldInterface, TwigFieldInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var Object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $entity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Render link template filled with configured options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @param mixed $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     public function render($value) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         $text = $this->options->get('text') ?: parent::render($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $parameters = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $accessor = PropertyAccess::createPropertyAccessor(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         foreach ($this->options->get('parameters') as $parameterName => $fieldName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |             if (!$fieldName) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                 $fieldName = $parameterName; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             $parameters[$parameterName] = $accessor->getValue($this->entity, $fieldName); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $render = $this->twig->render($this->options->get('template'), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |             'text' => $text, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             'route' => $this->options->get('route'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             'parameters' => $parameters, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             'target' => $this->options->get('target'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             'url' => $this->options->get('url'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             'title' => $this->options->get('title'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             'icon' => $this->options->get('icon'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return $render; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Configure options resolver. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param OptionsResolver $resolver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function configureOptions(OptionsResolver $resolver) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         // inherit parent's option | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         parent::configureOptions($resolver); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $resolver->setDefaults([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             'length' => $this->applicationConfiguration->getParameter('string_length'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             'replace' => $this->applicationConfiguration->getParameter('string_length_truncate'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             'template' => $this->applicationConfiguration->getParameter('fields_template_mapping')[Field::TYPE_LINK], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             'title' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             'icon' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             'target' => '_self', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             'route' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             'parameters' => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             'url' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             'text' => '', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $resolver->setAllowedTypes('route', 'string'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $resolver->setAllowedTypes('parameters', 'array'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $resolver->setAllowedTypes('length', 'integer'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $resolver->setAllowedTypes('url', 'string'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $resolver->setAllowedValues('target', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             '_self', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             '_blank', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         $resolver->setNormalizer('route', function(Options $options, $value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             // route or url should be defined | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             if (!$value && !$options->offsetGet('url')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 throw new InvalidOptionsException('You must set either an url or a route'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             return $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * Define field type. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function getType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return 'link'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * Define entity. It will be use to fill parameters with properties values. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param $entity | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 108 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |     public function setEntity($entity) | 
            
                                                        
            
                                    
            
            
                | 110 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 111 |  |  |         $this->entity = $entity; | 
            
                                                        
            
                                    
            
            
                | 112 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 114 |  |  |  |