You are here

public function AdminConfigureForm::buildForm in IP Geolocation Views & Maps 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/AdminConfigureForm.php, line 65

Class

AdminConfigureForm
Pending doc.

Namespace

Drupal\ip_geoloc\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  global $base_url;
  $config = $this
    ->config('ip_geoloc.settings');
  $form['ip_geoloc_google_auth'] = [
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Authentication of maps and geolocation services'),
    '#description' => $this
      ->t('The <a target="_blank" href="!url1">Google Maps API</a>, which include geolocation services, requires either a Google API Key or a Google Client ID. <br/>If you are using <a target="_blank" href="!url2">Leaflet maps</a> you may still need a Google API Key or Client ID when you use this module\'s geolocation services also.', [
      '!url1' => 'https://developers.google.com/maps/documentation/javascript/get-api-key',
      '!url2' => 'http://leafletjs.com',
    ]),
  ];
  $form['ip_geoloc_google_auth']['ip_geoloc_auth_method'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Select Google API Authentication Method'),
    '#description' => $this
      ->t('Google API Authentication Method'),
    '#default_value' => $config
      ->get('ip_geoloc_auth_method') ? $config
      ->get('ip_geoloc_auth_method') : 1,
    '#options' => [
      1 => $this
        ->t('API Key'),
      2 => $this
        ->t('Client ID'),
    ],
  ];
  $form['ip_geoloc_google_auth']['ip_geoloc_apikey'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Google Maps API Key'),
    '#description' => $this
      ->t('Obtain a Google Maps Javascript API key <a href="!url">here</a>.', [
      '!url' => 'https://developers.google.com/maps/documentation/javascript/get-api-key',
    ]),
    '#default_value' => $config
      ->get('ip_geoloc_apikey') ? $config
      ->get('ip_geoloc_apikey') : '',
    '#required' => FALSE,
    '#states' => [
      'visible' => [
        ':input[name="ip_geoloc_auth_method"]' => [
          'value' => 1,
        ],
      ],
    ],
  ];
  $form['ip_geoloc_google_auth']['ip_geoloc_client_id'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Google Client ID'),
    '#description' => $this
      ->t('For more information, visit <a href="!url">this page</a>.', [
      '!url' => 'https://developers.google.com/maps/documentation/javascript/get-api-key#client-id',
    ]),
    '#default_value' => $config
      ->get('ip_geoloc_client_id') ? $config
      ->get('ip_geoloc_client_id') : '',
    '#required' => FALSE,
    '#states' => [
      'visible' => [
        ':input[name="ip_geoloc_auth_method"]' => [
          'value' => 2,
        ],
      ],
    ],
  ];
  $form['ip_geoloc_google_auth']['ip_geoloc_signature'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Google Signature'),
    '#description' => $this
      ->t('May not be required. Check the Google documentation.'),
    '#default_value' => $config
      ->get('ip_geoloc_signature') ? $config
      ->get('ip_geoloc_signature') : '',
    '#required' => FALSE,
    '#states' => [
      'visible' => [
        ':input[name="ip_geoloc_auth_method"]' => [
          'value' => 2,
        ],
      ],
    ],
  ];
  $form['markers'] = [
    '#type' => 'details',
    '#open' => FALSE,
    '#title' => $this
      ->t('Alternative markers'),
  ];
  $markers_path = drupal_get_path('module', 'ip_geoloc');
  $form['markers']['ip_geoloc_marker_directory'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('<strong>Google Maps, Leaflet</strong>: path to marker images'),
    '#field_prefix' => "{$base_url}/",
    '#default_value' => ip_geoloc_marker_directory(),
    '#description' => $this
      ->t('Marker sets included with the module reside in %set1 and %set2. All marker images must be .png files.', [
      '%set1' => "{$markers_path}/markers",
      '%set2' => "{$markers_path}/amarkers",
    ]),
  ];
  $form['markers']['ip_geoloc_marker_dimensions'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('<strong>Google Maps, Leaflet</strong>: marker image width and height'),
    '#default_value' => ip_geoloc_marker_dimensions(),
    '#field_suffix' => $this
      ->t('px'),
    '#description' => $this
      ->t('These dimensions apply to all markers in the set. The default marker size is 21 x 34 for the <em>/markers</em> directory and 32 x 42 for the <em>/amarkers</em> directory.'),
  ];
  $form['markers']['ip_geoloc_marker_anchor_pos'] = [
    '#title' => $this
      ->t('<strong>Google Maps, Leaflet</strong>: marker image anchor position'),
    '#type' => 'select',
    '#default_value' => $config
      ->get('ip_geoloc_marker_anchor_pos') ? $config
      ->get('ip_geoloc_marker_anchor_pos') : 'bottom',
    '#options' => [
      'top' => $this
        ->t('Center of topline'),
      'middle' => $this
        ->t('Center of image'),
      'bottom' => $this
        ->t('Center of baseline'),
    ],
    '#description' => $this
      ->t('This anchor position is applied to all markers in the set.'),
  ];
  $form['markers']['ip_geoloc_num_location_marker_layers'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('<strong>OpenLayers only</strong>: maximum number of marker layers you may need'),
    '#default_value' => $config
      ->get('ip_geoloc_num_location_marker_layers') ? $config
      ->get('ip_geoloc_num_location_marker_layers') : IP_GEOLOC_DEF_NUM_MARKER_LAYERS,
    '#description' => $this
      ->t('Only relevant when you have selected "differentiator" fields in your view.'),
  ];
  $benefits = $this
    ->t('Some of the benefits of font icons are explained <a target="flink" href="!url_flink">here</a>.', [
    '!url_flink' => 'http://flink.com.au/ramblings/spruce-your-map-markers-font-icons',
  ]);
  $form['font_icons'] = [
    '#type' => 'details',
    '#open' => FALSE,
    '#title' => $this
      ->t('Font icons (Leaflet only)'),
    '#description' => $benefits . '<br/>' . $this
      ->t('If you decide to use them, a good spot to put the font icon libraries you choose to download is in subdirectories of %url_libs.<br/>Each font icon library normally contains a CSS file through which other files in the library are found. Type the filespec of those CSS files below, one per library. After you have saved the configuration, check the <a target="status" href="!url_status">status report</a> page for errors.<br/><strong>Example 1</strong>, <a target="fontawesome" href="!url_fontawesome">Font Awesome</a>: %css_file1 <br/><strong>Example 2</strong>, <a target="flaticon" href="!url_flaticon">flaticon</a>: %css_file2', [
      '%url_libs' => '/sites/all/libraries',
      '!url_fontawesome' => 'http://fortawesome.github.io/Font-Awesome',
      '%css_file1' => 'sites/all/libraries/font-awesome/css/font-awesome.min.css',
      '!url_flaticon' => 'http://flaticon.com',
      '%css_file2' => 'sites/all/libraries/flaticon/food-icons/flaticon.css',
      '!url_status' => 'admin/reports/status',
    ]),
  ];
  $libs = ip_geoloc_get_font_icon_libs();
  $count = max(2, min(count($libs) + 1, IP_GEOLOC_MAX_NUM_FONT_ICON_LIBS));
  for ($i = 1; $i <= $count; $i++) {
    $form['font_icons']["ip_geoloc_font_icon_lib{$i}"] = [
      '#type' => 'textfield',
      '#field_prefix' => "{$base_url}/",
      '#title' => $this
        ->t('Main CSS file belonging to font icon library #@i', [
        '@i' => $i,
      ]),
      '#default_value' => empty($libs[$i]) ? '' : $libs[$i],
    ];
  }
  $form['font_icons']["ip_geoloc_font_icon_lib{$count}"]['#description'] = $this
    ->t('<strong>Note:</strong> as explained in this <a target="issue" href="!url_issue">issue</a>, you cannot have identical leaf names amongst the CSS filespecs. Rename .css files if there are clashes.', [
    '!url_issue' => 'https://www.drupal.org/node/1885838#comment-8996119',
  ]);
  if ($this
    ->ipGeoLocDiagnose() > 0) {

    // Form for sync-ing the geolocation table with the system accesslog.
    $form['ip_geoloc_db_options'] = [
      '#type' => 'details',
      '#open' => FALSE,
      '#title' => $this
        ->t('Update IP geolocation database using past visitor IP addresses from the system access log'),
      '#description' => $this
        ->t('You can update the IP geolocation database in batches by pressing the button below. As a rough guide, count on a 1 minute wait for every 100 IP addresses, when executing a web service like IPInfoDB, as employed by Smart IP. Add another 2 minutes per 100 IP addresses if you ticked the option to employ the Google Maps API to reverse-geocode to street addresses. If your server interrupts the process you can continue from where it stopped by refreshing this page and pressing the button again. You will not lose any data.'),
    ];
    $form['ip_geoloc_db_options']['ip_geoloc_sync_with_accesslog'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Update now'),
      '#submit' => [
        'ip_geoloc_sync_with_accesslog',
      ],
    ];
    $form['ip_geoloc_db_options']['ip_geoloc_sync_batch_size'] = [
      '#type' => 'textfield',
      '#size' => 4,
      '#title' => $this
        ->t('Batch size'),
      '#default_value' => $config
        ->get('ip_geoloc_sync_batch_size') ? $config
        ->get('ip_geoloc_sync_batch_size') : 500,
      '#description' => $this
        ->t('To change the default batch size, press "Save configuration".'),
    ];
  }
  $form['ip_geoloc_data_collection_options'] = [
    '#type' => 'details',
    '#open' => FALSE,
    '#title' => $this
      ->t('Data collection options'),
  ];
  $form['ip_geoloc_data_collection_options']['ip_geoloc_allow_session_storage'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow session storage'),
    '#default_value' => $config
      ->get('ip_geoloc_allow_session_storage'),
    '#description' => $this
      ->t('The current visitor\'s location data is temporarily stored in either the $_SESSION variable or via the module <a target="_project" href="@url_project">Session Cache API</a>, if enabled. <br/>To disable both of these session storage functions, untick this box. However unticking this box means you will lose all functions regarding Wifi/GPS-based visitor geolocation. Location of the visitor based on IP address through either the <strong>Smart IP</strong> or <strong>GeoIP</strong> modules will continue to operate normally, if enabled.', [
      '@url_project' => 'http://drupal.org/project/session_cache',
    ]),
  ];
  $form['ip_geoloc_data_collection_options']['ip_geoloc_store_addresses'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Store reverse-geocoded addresses with their corresponding latitude/longitude on the database'),
    '#default_value' => $config
      ->get('ip_geoloc_store_addresses'),
    '#description' => $this
      ->t('This allows you to create maps of recent visitors to your site.'),
  ];
  $note1 = $this
    ->t('Untick, if you do not need periodic visitor location updates. Consider the <em>Set my location</em> block as an alternative.');
  $note2 = $this
    ->t('You can also use the <a href="@project_context">Context module</a> to conditionally geolocate the visitor based on device type, page visited etc.', [
    '@project_context' => 'http://drupal.org/project/context',
  ]);

  // $this->t('Used in conjunction with the options below.');.
  $note3 = '';
  $form['ip_geoloc_data_collection_options']['ip_geoloc_google_to_reverse_geocode'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Employ a free Google service to <em>periodically</em> auto reverse-geocode visitor locations to street addresses'),
    '#default_value' => $config
      ->get('ip_geoloc_google_to_reverse_geocode'),
    '#description' => "{$note1}<br>{$note2}<br/>" . $this
      ->t('Reverse-geocoding takes place via the Javascript version of the Google Maps API and the HTML5 way of obtaining a visitor\'s location. This involves visitors being prompted to accept sharing of their location.<br/>For the upload of historic visitor location data the server-side version of the Google Maps API is used. The latter is subject to a Google-imposed daily limit on the number of calls coming from the same server.') . '<br/>' . $note3,
    '#states' => [
      'visible' => [
        'input[name="ip_geoloc_allow_session_storage"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['ip_geoloc_data_collection_options']['ip_geoloc_location_check_interval'] = [
    '#type' => 'textfield',
    '#size' => 10,
    '#field_suffix' => $this
      ->t('seconds'),
    '#title' => $this
      ->t('Minimum elapsed time before geolocation data for the same user will be collected again'),
    '#default_value' => $config
      ->get('ip_geoloc_location_check_interval') ? $config
      ->get('ip_geoloc_location_check_interval') : IP_GEOLOC_LOCATION_CHECK_INTERVAL,
    '#description' => $this
      ->t('Operates in combination with the checkbox above. Geolocation information associated with an IP address may change over time, for instance when the visitor is using a mobile device and is moving. Use zero to geolocate the visitor <em>once</em> and then refrain from repeat location collection until their session has expired.'),
    '#states' => [
      'visible' => [
        'input[name="ip_geoloc_allow_session_storage"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['ip_geoloc_data_collection_options']['ip_geoloc_include_pages'] = [
    '#type' => 'textarea',
    '#rows' => 2,
    '#title' => $this
      ->t("Pages on which the visitor's HTML5 location may be sampled and reverse-geocoded to a street address"),
    '#default_value' => $config
      ->get('ip_geoloc_include_pages') ? $config
      ->get('ip_geoloc_include_pages') : '*',
    '#description' => $this
      ->t("Enter relative paths, one per line. Where they exist use the URL aliases rather than the node numbers. <strong>&lt;front&gt;</strong> means the front page.<br/>The asterisk <em>*</em> is the wildcard character, i.e. <em>recipes/mains*</em> denotes all pages that have a path starting with <em>recipes/mains</em><br/>The asterisk by itself means any page on your site."),
    '#states' => [
      'visible' => [
        'input[name="ip_geoloc_allow_session_storage"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['ip_geoloc_data_collection_options']['ip_geoloc_exclude_pages'] = [
    '#type' => 'textarea',
    '#rows' => 3,
    '#title' => $this
      ->t('Exceptions: pages excluded from the set of pages specified above'),
    '#default_value' => $config
      ->get('ip_geoloc_exclude_pages') ? $config
      ->get('ip_geoloc_exclude_pages') : IP_GEOLOC_DEFAULT_PAGE_EXCLUSIONS,
    '#description' => $this
      ->t('As above, one path specification per line.'),
    '#states' => [
      'visible' => [
        'input[name="ip_geoloc_allow_session_storage"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['ip_geoloc_data_collection_options']['ip_geoloc_roles_to_reverse_geocode'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t("User roles for which the HTML5 location may be sampled and reverse-geocoded to a street address"),
    '#default_value' => $config
      ->get('ip_geoloc_roles_to_reverse_geocode') ? $config
      ->get('ip_geoloc_roles_to_reverse_geocode') : [
      DRUPAL_ANONYMOUS_RID,
      DRUPAL_AUTHENTICATED_RID,
    ],
    '#options' => user_role_names(),
    '#description' => $this
      ->t('Selected roles are effective only when the check box on the data collection option above is also ticked.'),
    '#states' => [
      'visible' => [
        'input[name="ip_geoloc_allow_session_storage"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['ip_geoloc_data_collection_options']['ip_geoloc_smart_ip_as_backup'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Employ Smart IP as a backup to the Google Maps JS API as well as declined or failed HTML5 location retrievals in Views'),
    '#default_value' => $config
      ->get('ip_geoloc_smart_ip_as_backup'),
    '#description' => $this
      ->t('This refers to situations where the lat/long coords could not be established. An example is the user declining to share their location. Another is when the Google Maps API reverse-geocode function fails or is not enabled through the tick box at the top of this section.<br/>Smart IP lookups tend to be less detailed than the Google Maps reverse-geocoded results.<br/>If this box is <strong>not</strong> ticked, but the <a href="@geoip">GeoIP API module</a> is enabled, then GeoIP will be used as the Google Maps API fallback and to load historic lat/long coordinates.', [
      '@geoip' => 'http://drupal.org/project/geoip',
    ]),
    '#states' => [
      'visible' => [
        'input[name="ip_geoloc_allow_session_storage"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $form['ip_geoloc_data_collection_options']['ip_geoloc_throbber_text2'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Message displayed while visitor is being geolocated'),
    '#size' => 30,
    '#default_value' => $config
      ->get('ip_geoloc_throbber_text2') ? $config
      ->get('ip_geoloc_throbber_text2') : '',
    '#description' => $this
      ->t('A standard status message to show the visitor that geolocation was initiated and is in progress. </br/>Defaults to %default <br/>You may use most HTML tags. Use <em>&lt;none&gt;</em> to have no message shown, which is probably the preferred choice if the <strong>Auto-refresh</strong> box below is unticked.', [
      '%default' => IP_GEOLOC_THROBBER_DEFAULT_TEXT,
    ]),
  ];
  $form['ip_geoloc_advanced'] = [
    '#type' => 'details',
    '#open' => FALSE,
    '#title' => $this
      ->t('Advanced options'),
  ];
  $form['ip_geoloc_advanced']['ip_geoloc_page_refresh'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Auto-refresh the page as soon as an HTML5 location update has come in'),
    '#default_value' => $config
      ->get('ip_geoloc_page_refresh') ? $config
      ->get('ip_geoloc_page_refresh') : TRUE,
    '#description' => $this
      ->t('The above tick box does not apply to administration pages.'),
  ];
  $form['ip_geoloc_advanced']['ip_geoloc_debug'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Detail execution progress with status messages'),
    '#default_value' => $config
      ->get('ip_geoloc_debug'),
    '#description' => $this
      ->t('Enter a comma-separated list of names of users that should see status messages coming from this module, e.g., for debugging purposes. Use <strong>anon</strong> for the anonymous user.'),
  ];
  $form['ip_geoloc_advanced']['ip_geoloc_erase_session'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Erase geolocation data from session now'),
    '#submit' => [
      '::ipGeoLocEraseSession',
    ],
  ];
  $form['ip_geoloc_advanced']['ip_geoloc_erase_db'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Erase entire IP geolocation database now'),
    '#submit' => [
      '::ipGeoLocEraseDb',
    ],
  ];
  return parent::buildForm($form, $form_state);
}