You are here

function yamaps_block_content in Yandex.Maps 7

Returns block content.

See also

yamaps_block_view()

1 call to yamaps_block_content()
yamaps_block_view in inc/yamaps.block.inc
Implements hook_block_view().

File

inc/yamaps.block.inc, line 413
Yandex Maps blocks configuration.

Code

function yamaps_block_content($delta) {
  $block_output = [];
  $display_options = variable_get($delta . '_block_display_options', []);

  // Map information.
  $coords = drupal_json_decode(variable_get($delta . '_block_coords', NULL));
  if (empty($coords)) {
    return $block_output;
  }
  $id = drupal_html_id(implode('-', [
    'ymap',
    'block',
    $delta,
  ]));
  $open_button_id = drupal_html_id(implode('-', [
    $id,
    'open_button',
  ]));
  $close_button_id = drupal_html_id(implode('-', [
    $id,
    'close_button',
  ]));
  $width = variable_get($delta . '_block_width', YAMAPS_DEFAULT_BLOCK_MAP_WIDTH);
  $height = variable_get($delta . '_block_height', YAMAPS_DEFAULT_BLOCK_MAP_HEIGHT);
  $traffic = (int) variable_get($delta . '_block_traffic', FALSE);
  $auto_zoom = (int) variable_get($delta . '_block_auto_zoom', FALSE);
  $clusterer = (int) variable_get($delta . '_block_clusterer', FALSE);
  $controls = (int) variable_get($delta . '_block_controls', TRUE);
  $enable_placemarks = (int) variable_get($delta . '_block_enable_placemarks', TRUE);
  $enable_lines = (int) variable_get($delta . '_block_enable_lines', TRUE);
  $enable_polygons = (int) variable_get($delta . '_block_enable_polygons', TRUE);
  $enable_routes = (int) variable_get($delta . '_block_enable_routes', TRUE);
  $block_type = variable_get($delta . '_block_type', 'yandex#map');
  $map_type = isset($display_options['map_type']) ? $display_options['map_type'] : 'dynamic';
  $display_type = isset($display_options['display_type']) ? $display_options['display_type'] : 'map';
  $placemarks = drupal_json_decode(variable_get($delta . '_block_placemarks', NULL));
  $lines = drupal_json_decode(variable_get($delta . '_block_lines', NULL));
  $polygons = drupal_json_decode(variable_get($delta . '_block_polygons', NULL));
  $routes = drupal_json_decode(variable_get($delta . '_block_routes', NULL));

  // Set default 'Open map' text and 'Close map' text.
  if (!isset($display_options['open_button_text'])) {
    $display_options['open_button_text'] = YAMAPS_DEFAULT_OPEN_MAP_TEXT;
  }
  if (!isset($display_options['close_button_text'])) {
    $display_options['close_button_text'] = YAMAPS_DEFAULT_CLOSE_MAP_TEXT;
  }
  switch ($map_type) {
    case 'dynamic':

      // Map initialization parameters.
      $map = [
        'init' => [
          'center' => $coords['center'],
          'zoom' => $coords['zoom'],
          'type' => $block_type,
          'behaviors' => array_values(array_filter(variable_get($delta . '_block_behaviors', []))),
        ],
        'display_options' => [
          'display_type' => $display_type,
        ],
        'controls' => (int) $controls,
        'traffic' => (int) $traffic,
        'clusterer' => (int) $clusterer,
        'auto_zoom' => (int) $auto_zoom,
        'placemarks' => $placemarks,
        'lines' => $lines,
        'polygons' => $polygons,
        'routes' => $routes,
        'enable_placemarks' => (int) $enable_placemarks,
        'enable_lines' => (int) $enable_lines,
        'enable_polygons' => (int) $enable_polygons,
        'enable_routes' => (int) $enable_routes,
        'edit' => FALSE,
      ];
      $open_map_button = FALSE;
      $close_map_button = FALSE;
      if (isset($display_options['display_type']) && $display_options['display_type'] == 'map_button') {
        $block_output['open_map_button'] = [
          '#type' => 'html_tag',
          '#tag' => 'div',
          '#value' => t($display_options['open_button_text']),
          '#attributes' => [
            'id' => $open_button_id,
            'class' => [
              'open_yamap_button',
            ],
            'mapId' => $id,
          ],
        ];
        $open_map_button = TRUE;
        $close_map_button = TRUE;
      }

      // Return map container div.
      $block_output['map_container'] = [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#attributes' => [
          'id' => $id,
          'style' => 'width:' . $width . '; height:' . $height . ';',
          'class' => $open_map_button && $close_map_button ? [
            'yamaps-map-container',
            'element-invisible',
          ] : [
            'yamaps-map-container',
          ],
        ],
        '#value' => '',
      ];

      // Fix issue with block close button.
      if (isset($display_options['display_type']) && $display_options['display_type'] == 'map_button') {
        $block_output['close_map_button'] = [
          '#type' => 'html_tag',
          '#tag' => 'div',
          '#value' => t($display_options['close_button_text']),
          '#attributes' => [
            'id' => $close_button_id,
            'class' => [
              'close_yamap_button',
              'element-invisible',
            ],
            'mapId' => $id,
          ],
        ];
      }

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

      // Load library.
      $block_output['#attached']['library'][] = [
        'yamaps',
        'yamaps.full',
      ];
      break;
    case 'static':
      $params = [];
      $params['ll'] = end($coords['center']) . ',' . reset($coords['center']);
      $params['z'] = $coords['zoom'];
      $params['size'] = intval($width) . ',' . intval($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[$block_type];
      if (variable_get($delta . '_block_traffic', FALSE)) {
        $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 ($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 ($lines) {
        foreach ($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 ($polygons) {
        foreach ($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_map_button = FALSE;
      $close_map_button = FALSE;
      if ($display_type == 'map_button') {
        $block_output['open_button_text'] = [
          '#type' => 'html_tag',
          '#tag' => 'div',
          '#value' => t($display_options['open_button_text']),
          '#attributes' => [
            'id' => $open_button_id,
            'class' => [
              'open_yamap_button',
            ],
            'mapId' => $id,
          ],
        ];
        $block_output['close_button_text'] = [
          '#type' => 'html_tag',
          '#tag' => 'div',
          '#value' => t($display_options['close_button_text']),
          '#attributes' => [
            'id' => $close_button_id,
            'class' => [
              'close_yamap_button',
              'element-invisible',
            ],
            'mapId' => $id,
          ],
        ];
        $open_map_button = TRUE;
        $close_map_button = TRUE;
      }

      // Return map container div with image.
      $block_output['map_container'] = [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#attributes' => [
          'id' => $id,
          'style' => 'width:' . $width . 'px; height:' . $height . 'px;',
          'class' => $open_map_button && $close_map_button ? [
            'yamaps-map-container',
            'element-invisible',
          ] : [
            'yamaps-map-container',
          ],
        ],
        '#value' => theme('image', [
          'path' => url(YAMAPS_STATIC_API_URL, [
            'query' => $params,
            'external' => TRUE,
          ]),
          'width' => $width,
          'height' => $height,
          'title' => t('Yandex Map'),
        ]),
      ];

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

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