| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Drupal\df_tools_map\Plugin\GeofieldProximitySource; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Drupal\Core\Ajax\AjaxResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Drupal\Core\Ajax\ReplaceCommand; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Drupal\Core\Form\FormStateInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Drupal\Core\Logger\LoggerChannelTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Drupal\df_tools_map\Plugin\views\filter\SimpleProximity; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Drupal\geofield\Plugin\GeofieldProximitySourceBase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use Symfony\Component\DependencyInjection\ContainerInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * Defines 'Simple Proximity Source' plugin. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @package Drupal\geofield\Plugin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @GeofieldProximitySource( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  *   id = "simple_proximity_source", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  *   label = @Translation("Simple Proximity Source"), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  *   description = @Translation("A source plugin that calculates proximity based on a simple proximity filter."), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  *   exposedDescription = @Translation("The origin is calculated from a simple proximity filter."), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  *   context = { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  *     "sort", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  *     "field", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  *   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * ) | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  |  */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | class SimpleProximitySource extends GeofieldProximitySourceBase implements ContainerFactoryPluginInterface { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |   use LoggerChannelTrait; | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |   /** | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |    * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |    */ | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |     return new static( | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |       $configuration, | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |       $plugin_id, | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |       $plugin_definition | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     ); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |   } | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |   /** | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |    * Returns the list of available proximity filters. | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |    * | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |    * @return array | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |    *   The list of available proximity filters. | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |    */ | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |   protected function getAvailableProximityFilters() { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     $proximity_filters = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     /** @var \Drupal\views\Plugin\views\filter\FilterPluginBase $filter */ | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |     foreach ($this->viewHandler->displayHandler->getHandlers('filter') as $delta => $filter) { | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |       if ($filter instanceof SimpleProximity) { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $proximity_filters[$delta] = $filter->adminLabel(); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |       } | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     return $proximity_filters; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |   } | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |   /** | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |    * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |    */ | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |   public function buildOptionsForm(array &$form, FormStateInterface $form_state, array $options_parents, $is_exposed = FALSE) { | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     $user_input = $form_state->getUserInput(); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     $proximity_filters_sources = $this->getAvailableProximityFilters(); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     $user_input_proximity_filter = $user_input['options']['source_configuration']['source_proximity_filter'] ?? current(array_keys($proximity_filters_sources)); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     $source_proximity_filter = $this->configuration['source_proximity_filter'] ?? $user_input_proximity_filter; | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |     if (!empty($proximity_filters_sources)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |       $form['source_proximity_filter'] = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |         '#type' => 'select', | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |         '#title' => $this->t('Source Proximity Filter'), | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         '#description' => $this->t('Select the proximity filter to use as the starting point for calculating proximity.'), | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         '#options' => $this->getAvailableProximityFilters(), | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |         '#default_value' => $source_proximity_filter, | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         '#ajax' => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |           'callback' => [static::class, 'sourceProximityFilterUpdate'], | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |           'effect' => 'fade', | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |         ], | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |       ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     else { | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |       $form['source_proximity_filter_warning'] = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         '#type' => 'html_tag', | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |         '#tag' => 'div', | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |         '#value' => $this->t('No Simple Proximity Filter found. At least one should be set for this Proximity Field to work.'), | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |         "#attributes" => [ | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |           'class' => ['geofield-warning', 'red'], | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |         ], | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |       ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |       $form_state->setError($form['source_proximity_filter_warning'], $this->t('This Proximity Field cannot work. Dismiss this and add & setup a Simple Proximity Filter before.')); | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 97 |  |  |   } | 
            
                                                                        
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 99 |  |  |   /** | 
            
                                                                        
                            
            
                                    
            
            
                | 100 |  |  |    * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 101 |  |  |    */ | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |   public function validateOptionsForm(array &$form, FormStateInterface $form_state, array $options_parents) { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |     $values = $form_state->getValues(); | 
            
                                                                        
                            
            
                                    
            
            
                | 104 |  |  |     if (!isset($values['options']['source_configuration']['source_proximity_filter'])) { | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |       $form_state->setError($form['source_proximity_filter_warning'], $this->t('This Proximity Field cannot work. Dismiss this and add and setup a Proximity Filter before.')); | 
            
                                                                        
                            
            
                                    
            
            
                | 106 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |   } | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 |  |  |   /** | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |    * Ajax callback triggered on Proximity Filter Selection. | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |    * | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |    * @param array $form | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  |    *   The build form. | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |    * @param \Drupal\Core\Form\FormStateInterface $form_state | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |    *   The form state. | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |    * | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |    * @return \Drupal\Core\Ajax\AjaxResponse | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |    *   Ajax response with updated form element. | 
            
                                                                        
                            
            
                                    
            
            
                | 119 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |   public static function sourceProximityFilterUpdate(array $form, FormStateInterface $form_state) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     $response = new AjaxResponse(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     $response->addCommand(new ReplaceCommand( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |       '#proximity-source-configuration', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |       $form['options']['source_configuration'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     )); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  |     return $response; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 127 |  |  |   } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |   /** | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |    * {@inheritdoc} | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |   public function getOrigin() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     $origin = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     if ( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |       isset($this->viewHandler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |       && isset($this->viewHandler->view->filter[$this->viewHandler->options['source_configuration']['source_proximity_filter']]) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |       && $source_proximity_filter = $this->viewHandler->options['source_configuration']['source_proximity_filter'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |       /** @var \Drupal\df_tools_map\Plugin\views\filter\SimpleProximity $simple_proximity_filter */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |       $simple_proximity_filter = $this->viewHandler->view->filter[$source_proximity_filter]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |       $provider = \Drupal::entityTypeManager()->getStorage('geocoder_provider')->load('googlemaps'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |       try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $location = $simple_proximity_filter->value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         if ($collection = \Drupal::service('geocoder')->geocode($location[0], [$provider])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |           $coordinates = $collection->first()->getCoordinates(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |           $origin = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             "lat" => $coordinates->getLatitude(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             "lon" => $coordinates->getLongitude(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |           ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |       catch (\Exception $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $this->getLogger('df_tools_map')->error($e->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |       } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 157 |  |  |     return $origin; | 
            
                                                                        
                                                                
            
                                    
            
            
                | 158 |  |  |   } | 
            
                                                                        
                                                                
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 160 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 161 |  |  |  | 
            
                        
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.