-
Notifications
You must be signed in to change notification settings - Fork 0
/
georedirect.module
117 lines (109 loc) · 3.34 KB
/
georedirect.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* @file
* Redirect based on HTML5 Geolocation
*/
/**
* Implements hook_help.
*
* Displays help and module information.
*
* @param path
* Which path of the site we're using to display help
* @param arg
* Array that holds the current path as returned from arg() function
*/
function georedirect_help($path, $arg) {
switch ($path) {
case "admin/help#georedirect":
return '<p>'. t("Redirect based on HTML5 Geolocation") .'</p>';
break;
}
}
/**
* Implements hook_menu().
*/
function georedirect_menu() {
$items = array();
$items['admin/config/content/georedirect'] = array(
'title' => 'GeoRedirect',
'description' => 'Redirection based on HTML5 geolocation module',
'page callback' => 'drupal_get_form',
'page arguments' => array('georedirect_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
/**
* Implements hook_preprocess_page.
*/
function georedirect_preprocess_page(&$variables) {
if(!empty($variables['node'])){
$node = $variables['node'];
$nodes = variable_get('georedirect_nodes', 1);
$nodes_list = explode(",", $nodes);
foreach($nodes_list as $node_path){
$node_id = explode("/",drupal_get_normal_path(trim($node_path)));
if($node_id[0]=='node' && is_numeric($node_id[1]))
if($node->nid == $node_id[1] && !isset($_COOKIE['georedirect'])){
$time = variable_get('georedirect_time', 60);
if(empty($time))
setcookie("georedirect", "1", "", "/");
else
setcookie("georedirect", "1", time()+($time*60), "/");
drupal_add_js('http://maps.google.com/maps/api/js?sensor=true');
print "adding locjs";
drupal_add_js(drupal_get_path('module', 'georedirect') . '/js/FindLocation.js');
break;
}
}
}
}
/**
* Page callback: GeoRedirect settings
*
* @see georedirect_menu()
*/
function georedirect_form($form, &$form_state) {
$form['georedirect']['georedirect_nodes'] = array(
'#type' => 'textarea',
'#title' => t('Pages to redirect'),
'#default_value' => variable_get('georedirect_nodes', 1),
'#description' => t('List of comma seperated pages for which the redirect needs to happen. Eg. node/about,node/99'),
'#required' => TRUE,
);
$form['georedirect']['georedirect_time'] = array(
'#type' => 'textfield',
'#title' => t('Time interval between consecutive redirects(in minutes)'),
'#default_value' => variable_get('georedirect_time', 60),
'#description' => t('Leaving this empty will reset redirection till the session ends(browser closes)'),
'#size' => '4',
'#maxlength' => '8',
);
return system_settings_form($form);
}
/**
* Implements validation from the Form API.
*
* @param $form
* A structured array containing the elements and properties of the form.
* @param $form_state
* An array that stores information about the form's current state
* during processing.
*/
function georedirect_form_validate($form, &$form_state){
$max_num = $form_state['values']['georedirect_time'];
$success = 1;
if (!is_numeric($max_num)){
$success = 0;
form_set_error('georedirect_time', t('You must enter a number for the time interval field.'));
}
else if ($max_num < 0){
$success = 0;
form_set_error('georedirect_time', t('Time interval must be positive.'));
}
if($success && isset($_COOKIE['georedirect'])){
setcookie("georedirect", "0", time()-60*60*24*100, "/");
}
}