/**
  * Return the generated form output.
  * @return Form HTML.
  */
 public static function get_form($args)
 {
     iform_load_helpers(array('map_helper'));
     $readAuth = map_helper::get_read_auth($args['website_id'], $args['password']);
     // setup the map options
     $options = iform_map_get_map_options($args, $readAuth);
     $olOptions = iform_map_get_ol_options($args);
     for ($layer = 1; $layer <= 3; $layer++) {
         $argTitle = $args["layer_title_{$layer}"];
         if (isset($argTitle) && !empty($argTitle)) {
             //if there is no title then ignore the layer
             $meaningId = self::get_meaning_id($layer, $args, $readAuth);
             $taxon = self::get_taxon($meaningId, $readAuth);
             $layerTitle = str_replace('{species}', $taxon, $argTitle);
             $layerTitle = str_replace("'", "\\'", $layerTitle);
             $url = map_helper::$geoserver_url . 'wms';
             $argFeature = $args["wms_feature_type_{$layer}"];
             $layers = "layers: '{$argFeature}'";
             $argStyle = $args["wms_style_{$layer}"];
             $style = $argStyle ? ", styles: '{$argStyle}'" : '';
             $argWebsite = $args["website_id"];
             $filter = ", CQL_FILTER: 'website_id={$argWebsite} AND taxon_meaning_id={$meaningId}";
             $argCql = $args["cql_filter_{$layer}"];
             if ($argCql) {
                 $arg = str_replace("'", "\\'", $argCql);
                 $filter .= " AND({$argCql})'";
             } else {
                 $filter .= "'";
             }
             $script = "  var distLayer{$layer} = new OpenLayers.Layer.WMS(";
             $script .= "'{$layerTitle}', '{$url}',";
             $script .= "{" . "{$layers}, transparent: true {$filter} {$style}},";
             $script .= "{isBaseLayer: false, sphericalMercator: true, singleTile: true}";
             $script .= ");\n";
             map_helper::$onload_javascript .= $script;
             $options['layers'][] = "distLayer{$layer}";
         }
     }
     // This is not a map used for input
     $options['editLayer'] = false;
     // output a legend
     $r .= map_helper::layer_list(array('includeSwitchers' => true, 'includeHiddenLayers' => true));
     // output a map
     $r .= map_helper::map_panel($options, $olOptions);
     // Set up a page refresh for dynamic update of the map at set intervals
     if ($args['refresh_timer'] !== 0 && is_numeric($args['refresh_timer'])) {
         // is_int prevents injection
         if (isset($args['load_on_refresh']) && !empty($args['load_on_refresh'])) {
             map_helper::$javascript .= "setTimeout('window.location=\"" . $args['load_on_refresh'] . "\";', " . $args['refresh_timer'] . "*1000 );\n";
         } else {
             map_helper::$javascript .= "setTimeout('window.location.reload( false );', " . $args['refresh_timer'] . "*1000 );\n";
         }
     }
     return $r;
 }
 /**
  * Return the generated form output.
  * @return Form HTML.
  */
 public static function get_form($args)
 {
     iform_load_helpers(array('map_helper', 'data_entry_helper'));
     global $user;
     $readAuth = map_helper::get_read_auth($args['website_id'], $args['password']);
     // setup the map options
     $options = iform_map_get_map_options($args, $readAuth);
     $olOptions = iform_map_get_ol_options($args);
     if (!$args['show_all_species']) {
         if (isset($args['taxon_identifier']) && !empty($args['taxon_identifier'])) {
             // This page is for a predefined species map
             $taxonIdentifier = $args['taxon_identifier'];
         } else {
             if (isset($_GET['taxon'])) {
                 $taxonIdentifier = $_GET['taxon'];
             } else {
                 return lang::get("The distribution map cannot be displayed without a taxon identifier");
             }
         }
         if ($args['external_key'] == true) {
             if (empty($args['taxon_list_id'])) {
                 return lang::get('This form is configured with the Distribution Layer - External Key option ticked, but no species list has been configured to ' . 'lookup the external keys against.');
             }
             // the taxon identifier is an external key, so we need to translate to a meaning ID.
             $fetchOpts = array('table' => 'taxa_taxon_list', 'extraParams' => $readAuth + array('view' => 'detail', 'external_key' => $taxonIdentifier, 'taxon_list_id' => $args['taxon_list_id'], 'preferred' => true));
             $prefRecords = data_entry_helper::get_population_data($fetchOpts);
             // We might have multiple records back, e.g. if there are several photos, but we should have a unique meaning id.
             $meaningId = 0;
             foreach ($prefRecords as $prefRecord) {
                 if ($meaningId != 0 && $meaningId != $prefRecord['taxon_meaning_id']) {
                     // bomb out, as we  don't know which taxon to display
                     return lang::get("The taxon identifier cannot be used to identify a unique taxon.");
                 }
                 $meaningId = $prefRecord['taxon_meaning_id'];
             }
             if ($meaningId == 0) {
                 return lang::get("The taxon identified by the taxon identifier cannot be found.");
             }
             $meaningId = $prefRecords[0]['taxon_meaning_id'];
         } else {
             // the taxon identifier is the meaning ID.
             $meaningId = $taxonIdentifier;
         }
         // We still need to fetch the species record, to get its common name
         $fetchOpts = array('table' => 'taxa_taxon_list', 'extraParams' => $readAuth + array('view' => 'detail', 'language_iso' => iform_lang_iso_639_2(hostsite_get_user_field('language')), 'taxon_meaning_id' => $meaningId));
         $taxonRecords = data_entry_helper::get_population_data($fetchOpts);
     }
     $url = map_helper::$geoserver_url . 'wms';
     // Get the style if there is one selected
     $style = $args["wms_style"] ? ", styles: '" . $args["wms_style"] . "'" : '';
     map_helper::$onload_javascript .= "\n    var filter='website_id=" . $args['website_id'] . "';";
     if ($args['show_all_species']) {
         $layerTitle = lang::get('All species occurrences');
     } else {
         $layerTitle = str_replace('{species}', $taxonRecords[0]['taxon'], $args['layer_title']);
         map_helper::$onload_javascript .= "\n    filter += ' AND taxon_meaning_id={$meaningId}';\n";
     }
     if (!empty($args['cql_filter'])) {
         map_helper::$onload_javascript .= "\n    filter += ' AND(" . str_replace("'", "\\'", $args['cql_filter']) . ")';\n";
     }
     $layerTitle = str_replace("'", "\\'", $layerTitle);
     map_helper::$onload_javascript .= "\n    var distLayer = new OpenLayers.Layer.WMS(\r\n          '" . $layerTitle . "',\r\n          '{$url}',\r\n          {layers: '" . $args["wms_feature_type"] . "', transparent: true, CQL_FILTER: filter {$style}},\r\n          {isBaseLayer: false, sphericalMercator: true, singleTile: true}\r\n    );\n";
     $options['layers'][] = 'distLayer';
     if (isset($args['click_on_occurrences_mode']) && $args['click_on_occurrences_mode'] != 'none') {
         $options['clickableLayersOutputMode'] = $args['click_on_occurrences_mode'];
         $options['clickableLayersOutputDiv'] = 'getinfo-output';
         $options['clickableLayers'][] = 'distLayer';
         if (!empty($args['click_columns'])) {
             // convert the input column list argument to a structured array to pass to the map
             $inputarr = explode("\r\n", $args['click_columns']);
             $outputarr = array();
             foreach ($inputarr as $coldef) {
                 $coldef = explode('=', $coldef);
                 $outputarr[$coldef[0]] = $coldef[1];
             }
             $options['clickableLayersOutputColumns'] = $outputarr;
         }
     }
     // This is not a map used for input
     $options['editLayer'] = false;
     // if in Drupal, and IForm proxy is installed, then use this path as OpenLayers proxy
     if (defined('DRUPAL_BOOTSTRAP_CONFIGURATION') && module_exists('iform_proxy')) {
         global $base_url;
         $options['proxy'] = $base_url . '?q=' . variable_get('iform_proxy_path', 'proxy') . '&url=';
     }
     // output a legend
     if (isset($args['include_layer_list_types'])) {
         $layerTypes = explode(',', $args['include_layer_list_types']);
     } else {
         $layerTypes = array('base', 'overlay');
     }
     $r = '';
     if (!isset($args['include_layer_list']) || $args['include_layer_list']) {
         $r .= map_helper::layer_list(array('includeSwitchers' => isset($args['include_layer_list_switchers']) ? $args['include_layer_list_switchers'] : true, 'includeHiddenLayers' => true, 'layerTypes' => $layerTypes));
     }
     // output a map
     $r .= map_helper::map_panel($options, $olOptions);
     // add an empty div for the output of getinfo requests
     if (isset($args['click_on_occurrences_mode']) && $args['click_on_occurrences_mode'] == 'div') {
         $r .= '<div id="getinfo-output"></div>';
     }
     // Set up a page refresh for dynamic update of the map at set intervals
     if (!empty($args['refresh_timer']) && $args['refresh_timer'] !== 0 && is_numeric($args['refresh_timer'])) {
         // is_int prevents injection
         if (isset($args['load_on_refresh']) && !empty($args['load_on_refresh'])) {
             map_helper::$javascript .= "setTimeout('window.location=\"" . $args['load_on_refresh'] . "\";', " . $args['refresh_timer'] . "*1000 );\n";
         } else {
             map_helper::$javascript .= "setTimeout('window.location.reload( false );', " . $args['refresh_timer'] . "*1000 );\n";
         }
     }
     return $r;
 }