You are here

public function yamaps_views_plugin_style_default_map::render in Yandex.Maps 7

Renders the map.

Return value

array Rendered string.

Overrides views_plugin_style::render

File

modules/yamaps_views/handlers/yamaps_views_plugin_style_default_map.inc, line 354
Class yamaps_views_plugin_style_default_map.

Class

yamaps_views_plugin_style_default_map
Class defines Yandex Maps style plugin handler.

Code

public function render() {
  $placemarks = [];
  $prepared_lines = [];
  $prepared_polygons = [];
  $prepared_route = [];
  $this->options['coords'] = $this
    ->getCoordinates();
  $prepared_params = yamaps_format_values_to_js($this->options);
  if (empty($this->options['yandex_map_field'])) {
    return t('Please add "Yandex Maps" field and chose it in views settings form.');
  }
  $yandexmap_field_type = $this->view->field[$this->options['yandex_map_field']]->options['type'];
  if ($yandexmap_field_type == YAMAPS_STATIC_FORMATTER && (!isset($prepared_params['coords']['center']) || isset($prepared_params['coords']['center']) && !is_array($prepared_params['coords']['center']))) {
    return t('The Static Yandex Maps style cannot be used without coordinates of center.');
  }
  $this
    ->render_fields($this->view->result);
  $yandexmap_field_name = 'field_' . $this->options['yandex_map_field'];
  $yandexmap_field_settings = $this->view->field[$this->options['yandex_map_field']]->options['settings'];
  foreach ($this->view->result as $row_index => $row) {

    // Fix yandex cart.
    foreach ($row->{$yandexmap_field_name} as $yandexmap_field_coords) {
      if (isset($yandexmap_field_coords['raw']['placemarks'])) {

        // Preparing placemarks.
        $decoded_placemarks = drupal_json_decode($yandexmap_field_coords['raw']['placemarks']);
        if (is_array($decoded_placemarks)) {
          foreach ($decoded_placemarks as $placemark) {

            // Override placemark title.
            $this
              ->overridePlacemarkTitle($placemark, $row_index, $row);

            // Prepare Baloon title.
            if ($this->options['baloon_title'] && $this->options['baloon_title'] !== self::PLACEMARK_DEFAULT_FIELD) {
              $placemark['params'][self::PLACEMARK_BALLON_HEADER] = $this->rendered_fields[$row_index][$this->options['baloon_title']];
            }

            // Prepare Baloon body.
            if (isset($this->options['baloon_body']) && is_array($this->options['baloon_body'])) {
              $ballon_body = [];
              foreach ($this->options['baloon_body'] as $bval) {
                $ballon_body[] = $this
                  ->get_field($row_index, $bval);
              }
              $placemark['params'][self::PLACEMARK_BALLON_BODY] = $this
                ->prepareBody($ballon_body);
            }
            $this->view->row_index = $row_index;
            unset($ballon_body);
            $placemarks[] = $placemark;
          }
        }
      }

      // Preparing lines.
      if (isset($yandexmap_field_coords['raw']['lines'])) {
        $decoded_lines = drupal_json_decode($yandexmap_field_coords['raw']['lines']);
        if (is_array($decoded_lines)) {
          foreach ($decoded_lines as $lines) {
            $prepared_lines[] = $lines;
          }
        }
      }

      // Preparing polygons.
      if (isset($yandexmap_field_coords['raw']['polygons'])) {
        $decoded_polygons = drupal_json_decode($yandexmap_field_coords['raw']['polygons']);
        if (is_array($decoded_polygons)) {
          foreach ($decoded_polygons as $polygons) {
            $prepared_polygons[] = $polygons;
          }
        }
      }

      // Preparing routes.
      if (isset($yandexmap_field_coords['raw']['routes'])) {
        $decoded_routes = drupal_json_decode($yandexmap_field_coords['raw']['routes']);
        if (is_array($decoded_routes)) {
          foreach ($decoded_routes as $route) {
            $prepared_route[] = $route;
          }
        }
      }
    }
  }
  unset($this->view->row_index);
  $array_of_unique_params = [
    $this->plugin_name,
    $this->view->name,
    $this->view->current_display,
  ];
  if (isset($this->view->dom_id)) {
    $array_of_unique_params[] = $this->view->dom_id;
  }

  // Unique map id.
  $id = drupal_html_id(implode('-', $array_of_unique_params));
  $open_button_id = drupal_html_id(implode('-', [
    $id,
    'open_button',
  ]));
  $close_button_id = drupal_html_id(implode('-', [
    $id,
    'close_button',
  ]));
  switch ($this->options['yamaps_center_options']['map_center_type']) {
    case 'geolocation':
      $prepared_params['coords']['center'] = NULL;
      $parameters = yamaps_geocoding($this->options['yamaps_center_options']['map_center_geolocation']);
      if (isset($parameters) && $parameters !== FALSE) {
        $prepared_params['coords']['center'] = $parameters['map_center'];
      }
      $prepared_params['coords']['zoom'] = ++$this->options['yamaps_center_options']['zoom'];
      $prepared_params['type'] = 'yandex#map';
      break;
    case 'mini_map':

      // Merging placemark.
      if (is_array($prepared_params['placemarks'])) {
        foreach ($prepared_params['placemarks'] as $p) {
          $placemarks[] = $p;
        }
      }

      // Merging lines.
      if (is_array($prepared_params['lines'])) {
        foreach ($prepared_params['lines'] as $lines) {
          $prepared_lines[] = $lines;
        }
      }

      // Merging polygons.
      if (is_array($prepared_params['polygons'])) {
        foreach ($prepared_params['polygons'] as $polygon) {
          $prepared_polygons[] = $polygon;
        }
      }

      // Merging routes.
      if (is_array($prepared_params['routes'])) {
        foreach ($prepared_params['routes'] as $route) {
          $prepared_route[] = $route;
        }
      }
      break;
  }
  $views_output = [];
  switch ($yandexmap_field_type) {
    case YAMAPS_DYNAMIC_FORMATTER:

      // Map initialization parameters.
      $map = [
        'id' => $id,
        'display_options' => [
          'display_type' => $yandexmap_field_settings['yamaps_display_options']['display_type'],
        ],
        'controls' => $yandexmap_field_settings['controls'],
        'traffic' => $yandexmap_field_settings['traffic'],
        'clusterer' => isset($yandexmap_field_settings['clusterer']) ? $yandexmap_field_settings['clusterer'] : 0,
        'auto_zoom' => isset($yandexmap_field_settings['auto_zoom']) ? $yandexmap_field_settings['auto_zoom'] : 0,
        'placemarks' => empty($placemarks) ? NULL : $placemarks,
        'lines' => empty($prepared_lines) ? NULL : $prepared_lines,
        'polygons' => empty($prepared_polygons) ? NULL : $prepared_polygons,
        'routes' => empty($prepared_route) ? NULL : $prepared_route,
        'enable_placemarks' => (int) (!empty($yandexmap_field_settings['enable_placemarks'])),
        'enable_lines' => (int) (!empty($yandexmap_field_settings['enable_lines'])),
        'enable_polygons' => (int) (!empty($yandexmap_field_settings['enable_polygons'])),
        'enable_routes' => (int) (!empty($yandexmap_field_settings['enable_routes'])),
        'edit' => FALSE,
        'init' => [
          'type' => $prepared_params['type'],
          'center' => empty($prepared_params['coords']['center']) ? NULL : $prepared_params['coords']['center'],
          'zoom' => empty($prepared_params['coords']['zoom']) ? NULL : $prepared_params['coords']['zoom'],
          'behaviors' => array_values(array_filter($yandexmap_field_settings['behaviors'])),
        ],
      ];

      // Adding map to js.
      $views_output['#attached']['js'][] = [
        'data' => [
          'yamaps' => [
            $id => $map,
          ],
        ],
        'type' => 'setting',
      ];

      // Load library.
      $views_output['#attached']['library'][] = [
        'yamaps',
        'yamaps.full',
      ];
      $hide_map = isset($yandexmap_field_settings['yamaps_display_options']['display_type']) && $yandexmap_field_settings['yamaps_display_options']['display_type'] == 'map_button';
      $map_class = [
        'yamaps-map-container',
      ];
      $map_class[] = $hide_map ? 'element-invisible' : '';
      if (isset($yandexmap_field_settings['yamaps_display_options']['display_type']) && $yandexmap_field_settings['yamaps_display_options']['display_type'] == 'map_button') {
        $views_output['open_map_button'] = [
          '#type' => 'html_tag',
          '#tag' => 'div',
          '#value' => isset($yandexmap_field_settings['yamaps_display_options']['open_button_text']) ? t($yandexmap_field_settings['yamaps_display_options']['open_button_text']) : t(YAMAPS_DEFAULT_OPEN_MAP_TEXT),
          '#attributes' => [
            'id' => $open_button_id,
            'class' => [
              'open_yamap_button',
            ],
            'mapId' => $id,
          ],
        ];
        $views_output['close_map_button'] = [
          '#type' => 'html_tag',
          '#tag' => 'div',
          '#value' => isset($yandexmap_field_settings['yamaps_display_options']['close_button_text']) ? t($yandexmap_field_settings['yamaps_display_options']['close_button_text']) : t(YAMAPS_DEFAULT_CLOSE_MAP_TEXT),
          '#attributes' => [
            'id' => $close_button_id,
            'class' => [
              'close_yamap_button',
              'element-invisible',
            ],
            'mapId' => $id,
          ],
        ];
      }
      $views_output[] = [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#attributes' => [
          'style' => ' width: ' . $yandexmap_field_settings['width'] . '; height:' . $yandexmap_field_settings['height'] . ';',
          'id' => $id,
          'class' => $map_class,
        ],
        '#value' => '',
      ];
      break;
    case YAMAPS_STATIC_FORMATTER:
      $params = [
        'll' => end($prepared_params['coords']['center']) . ',' . reset($prepared_params['coords']['center']),
        'z' => $prepared_params['coords']['zoom'],
        'size' => $yandexmap_field_settings['s_width'] . ',' . $yandexmap_field_settings['s_height'],
      ];

      // 2.x to 1.x types.
      $map_types = [
        'yandex#map' => 'map',
        'yandex#satellite' => 'sat',
        'yandex#hybrid' => 'sat,skl',
        'yandex#publicMap' => 'pmap',
        'yandex#publicMapHybrid' => 'sat,pskl',
      ];
      $params['l'] = $map_types[$prepared_params['type']];
      if ($yandexmap_field_settings['s_traffic']) {
        $params['l'] .= ',trf';
      }

      // 2.x to 1.x colors.
      $colors21 = [
        'blue' => 'bl',
        'lightblue' => 'lb',
        'night' => 'nt',
        'darkblue' => 'db',
        'green' => 'gn',
        'white' => 'wt',
        'red' => 'rd',
        'orange' => 'or',
        'darkorange' => 'do',
        'yellow' => 'yw',
        'violet' => 'vv',
        'pink' => 'pn',
      ];

      // 2.x to hex colors.
      $colors = [
        'blue' => '006cff',
        'lightblue' => '66c7ff',
        'night' => '004056',
        'darkblue' => '00339a',
        'green' => '33cc00',
        'white' => 'ffffff',
        'red' => 'ff0000',
        'orange' => 'ffb400',
        'darkorange' => 'ff6600',
        'yellow' => 'ffea00',
        'violet' => 'b832fd',
        'pink' => 'fd32fb',
      ];

      // Placemarks.
      if (!empty($placemarks)) {
        $pt = [];
        foreach ($placemarks as $placemark) {
          $pm = end($placemark['coords']) . ',' . reset($placemark['coords']) . ',';
          $pm .= 'pm2';
          $pm .= $colors21[$placemark['params']['color']];
          $pm .= 'm';
          $pt[] = $pm;
        }
        $params['pt'] = implode('~', $pt);
      }

      // Lines and polygons.
      $pl = [];
      if ($prepared_lines) {
        foreach ($prepared_lines as $line) {
          $opts = $line['params'];
          $pm = 'c:' . $colors[$opts['strokeColor']] . dechex(255 * $opts['opacity']) . ',';
          $pm .= 'w:' . $opts['strokeWidth'] . ',';
          $c = [];
          foreach ($line['coords'] as $coords) {
            $c[] = end($coords);
            $c[] = reset($coords);
          }
          $pm .= implode(',', $c);
          $pl[] = $pm;
        }
      }
      if ($prepared_polygons) {
        foreach ($prepared_polygons as $polygon) {
          $opts = $polygon['params'];
          $opa = dechex(255 * $opts['opacity']);
          $pm = 'c:' . $colors[$opts['strokeColor']] . $opa . ',';
          $pm .= 'f:' . $colors[$opts['fillColor']] . $opa . ',';
          $pm .= 'w:' . $opts['strokeWidth'] . ',';
          $c = [];
          foreach ($polygon['coords'] as $coords_array) {
            foreach ($coords_array as $coords) {
              $c[] = end($coords);
              $c[] = reset($coords);
            }
          }
          $pm .= implode(',', $c);
          $pl[] = $pm;
        }
      }
      if (!empty($pl)) {
        $params['pl'] = implode('~', $pl);
      }
      $open_button_id = drupal_html_id(implode('-', [
        $id,
        'open_button',
      ]));
      $hide_map = isset($yandexmap_field_settings['yamaps_display_options_static']['display_type_static']) && $yandexmap_field_settings['yamaps_display_options_static']['display_type_static'] == 'map_button';
      $map_display_style = $hide_map ? '; display: none' : '';

      // Returns map container div with image.
      $views_output['map_container'] = [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#attributes' => [
          'id' => $id,
          'style' => 'width:' . $yandexmap_field_settings['s_width'] . 'px; height:' . $yandexmap_field_settings['s_height'] . 'px' . $map_display_style . ';',
        ],
        '#value' => theme('image', [
          'path' => url(YAMAPS_STATIC_API_URL, [
            'query' => $params,
            'external' => TRUE,
          ]),
          'width' => $yandexmap_field_settings['s_width'],
          'height' => $yandexmap_field_settings['s_height'],
          'title' => t('Yandex Map'),
        ]),
      ];
      if ($hide_map) {
        $views_output['open_button_text'] = [
          '#type' => 'html_tag',
          '#tag' => 'div',
          '#value' => isset($yandexmap_field_settings['yamaps_display_options_static']['open_button_text_static']) ? t($yandexmap_field_settings['yamaps_display_options_static']['open_button_text_static']) : t(YAMAPS_DEFAULT_OPEN_MAP_TEXT),
          '#attributes' => [
            'id' => $open_button_id,
            'class' => [
              'open_yamap_button_static',
            ],
            'mapId' => $id,
          ],
        ];
      }

      // Map initialization parameters.
      $maps[$id] = [
        'display_options' => [
          'display_type' => $yandexmap_field_settings['yamaps_display_options_static']['display_type_static'],
        ],
      ];

      // Adding CSS for button.
      $views_output['#attached']['css'][] = drupal_get_path('module', 'yamaps') . '/misc/yamaps.css';

      // Adding map to js and load library.
      $views_output['#attached']['js'][] = [
        'data' => [
          'yamapsStatic' => $maps,
        ],
        'type' => 'setting',
      ];
      $views_output['#attached']['library'][] = [
        'yamaps',
        'yamaps.full',
      ];
      break;
  }
  return $views_output;
}