This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loyalist.module
115 lines (102 loc) · 2.84 KB
/
loyalist.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
<?php
/**
* @file
* Primary hooks and functions for the Loyalist module.
*/
/**
* Implements hook_permission().
*
* @see hook_permission()
*/
function loyalist_permission() {
return array(
'administer loyalist' => array(
'title' => t('Administer Loyalist settings'),
),
);
}
/**
* Implements hook_menu().
*
* @see hook_menu()
*/
function loyalist_menu() {
$items = array();
$items['admin/config/people/loyalist'] = array(
'title' => 'Loyalist',
'description' => 'Define parameters for loyalist visitors.',
'page callback' => 'drupal_get_form',
'page arguments' => array('loyalist_settings'),
'access arguments' => array('administer loyalist'),
'file' => 'loyalist.admin.inc',
);
$items['loyalist/init/%'] = array(
'title' => 'Initialize Loyalist Session',
'description' => 'Adds loyalist visitor data to the session.',
'page callback' => 'loyalist_init_session',
'page arguments' => array(2),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_page_build().
*
* @see hook_page_build()
*/
function loyalist_page_build(&$page) {
if (!path_is_admin(current_path())) {
drupal_add_js(drupal_get_path('module', 'loyalist') . '/js/loyalist.js');
drupal_add_js(array(
'loyalist' => array(
'cooldown' => variable_get('loyalist_cooldown', 0),
'interval' => variable_get('loyalist_interval', 0),
'visits' => variable_get('loyalist_visits', 0),
),
), 'setting');
// Invoke a pending Rules event.
if (isset($_SESSION['loyalist'])
&& isset($_SESSION['loyalist']['invoke'])) {
if (module_exists('rules')) {
rules_invoke_event($_SESSION['loyalist']['invoke']);
}
unset($_SESSION['loyalist']['invoke']);
}
}
}
/**
* Loyalist session initialization.
*
* Because the visits log is stored local to the user and accessed only via
* Javascript, this information is controlled in the session so this function
* can be called as an AJAX request.
*
* For rules integration, an extra session variable is set the Rules event is
* invoked in loyalist_page_build().
*
* @param string $op
* The operation to run. One of:
* - "new": This session is for a new loyalist.
* - "returning": This session is for a returning loyalist.
* - "non": This session is for a non-loyalist.
*
* @see loyalist_menu()
* @see loyalist_page_build()
*/
function loyalist_init_session($op) {
$_SESSION['loyalist'] = array('loyalist' => FALSE);
switch ($op) {
case 'new':
$_SESSION['loyalist']['loyalist'] = TRUE;
$rule = 'loyalist_new';
break;
case 'returning':
$_SESSION['loyalist']['loyalist'] = TRUE;
$rule = 'loyalist_visit';
break;
}
if (isset($rule) && module_exists('rules')) {
$_SESSION['loyalist']['invoke'] = $rule;
}
}