-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompro_tour.module
200 lines (169 loc) · 5.64 KB
/
compro_tour.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
/**
* @file
* Code for the Compro Tour feature.
*/
include_once 'compro_tour.features.inc';
/**
* Implements hook_form_alter().
*/
function compro_tour_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'eck__entity__form_edit_tour_tour' || $form_id == 'eck__entity__form_add_tour_tour') {
$form['#validate'][] = 'compro_tour_validate';
}
}
/**
* Implements hook_validate().
*
* I copied most of this from menu_edit_item_validate() in menu.admin.inc.
*/
function compro_tour_validate($form, &$form_state) {
$page = $form_state['values']['field_tour_page'][LANGUAGE_NONE][0]['value'];
$normal_path = drupal_get_normal_path($page);
if ($page != $normal_path) {
drupal_set_message(t('The tour system stores system paths only, but will use the URL alias for display. %link_path has been stored as %normal_path', array(
'%link_path' => $page,
'%normal_path' => $normal_path,
)));
$form_state['values']['field_tour_page'][LANGUAGE_NONE][0]['value'] = $normal_path;
}
if (!url_is_external($page)) {
$parsed_link = parse_url($page);
if (isset($parsed_link['path']) && $page != $parsed_link['path']) {
$form_state['values']['field_tour_page'][LANGUAGE_NONE][0]['value'] = $parsed_link['path'];
}
}
if (url_is_external($page)) {
form_set_error('field_tour_page', t("The path '@link_path' must be an internal link.", array('@link_path' => $page)));
}
if (!trim($page) || !drupal_valid_path($page, TRUE)) {
form_set_error('field_tour_page', t("The path '@link_path' is either invalid or you do not have access to it.", array('@link_path' => $page)));
}
}
/**
* Returns all tour entities.
*/
function compro_tour_get_tours() {
$tours = array();
// Search for any available tours.
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'tour')
->entityCondition('bundle', 'tour');
$result = $query->execute();
if (isset($result['tour'])) {
$tours = entity_load('tour', array_keys($result['tour']));
}
return $tours;
}
/**
* Returns a machine name.
*/
function compro_tour_machine_name($string) {
$string = strtolower($string);
return preg_replace('@[^a-z0-9_]+@', '_', $string);
}
/**
* Implements hook_block_info().
*/
function compro_tour_block_info() {
$blocks = array();
// Create blocks from tours.
$blocks['compro_tour'] = array(
'info' => t('Compro Tours'),
);
// The add tour block on the admin page.
$blocks['add_tour'] = array(
'info' => t('Add Tours'),
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function compro_tour_block_view($delta = '') {
$block = array();
if ($delta == 'compro_tour') {
$block['content'] = compro_tour_block_content();
}
// The add tour block on the admin page.
if ($delta == 'add_tour') {
$block['subject'] = '';
$block['content'] = compro_tour_add_tours_content();
}
return $block;
}
/**
* Tour block content.
*/
function compro_tour_block_content() {
$tours = compro_tour_get_tours();
$output = array();
drupal_add_css(drupal_get_path('module', 'compro_tour') . '/theme/hopscotch/css/hopscotch.min.css');
drupal_add_js(drupal_get_path('module', 'compro_tour') . '/theme/hopscotch/js/hopscotch.min.js');
drupal_add_js(drupal_get_path('module', 'compro_tour') . '/theme/compro_tour.js');
foreach ($tours as $tour) {
// Only render the tours for the correct page.
if ($_GET['q'] != $tour->field_tour_page[LANGUAGE_NONE][0]['value']) {
continue;
}
$step_ids = array();
$steps = array();
// Load steps.
foreach ($tour->field_tour_steps[LANGUAGE_NONE] as $step_id) {
$step_ids[$step_id['target_id']] = $step_id['target_id'];
}
$step_entities = entity_load('tour', $step_ids);
// Structure steps a bit better to allow for less JS code.
foreach ($step_entities as $step) {
$steps[] = array(
'title' => $step->title,
'content' => $step->field_step_body[LANGUAGE_NONE][0]['safe_value'],
'target' => $step->field_step_css_selector[LANGUAGE_NONE][0]['safe_value'],
'placement' => $step->field_step_placement[LANGUAGE_NONE][0]['value'],
);
}
// Send step data to front end to build tour.
$settings = array(
'steps' => $steps,
'tour' => compro_tour_machine_name($tour->title),
'link' => isset($tour->field_tour_link[LANGUAGE_NONE][0]['safe_value']) ? TRUE : FALSE,
);
drupal_add_js(array('compro_tour' => $settings), array('type' => 'setting'));
// Output a link if one was requested.
if (isset($tour->field_tour_link[LANGUAGE_NONE][0]['safe_value'])) {
$output = array(
'#markup' => l($tour->field_tour_link[LANGUAGE_NONE][0]['safe_value'], 'javascript:', array(
'external' => TRUE,
'attributes' => array('onclick' => 'startTour()'),
)),
);
}
}
return $output;
}
/**
* Add tour block content.
*/
function compro_tour_add_tours_content() {
$render = array();
$component = entity_get_info('tour');
// Build out an array of components to add.
if ($component['bundles']) {
foreach ($component['bundles'] as $key => $bundle) {
if ($bundle['label'] == 'Tour') {
$render[$key] = array(
'#attached' => array(
'css' => array(drupal_get_path('module', 'compro_tour') . '/theme/css/compro_tour.css'),
),
'#markup' => l(t('Add New !label', array('!label' => $bundle['label'])), $bundle['crud']['add']['path'], array(
'query' => array('destination' => 'admin/content/tours'),
'attributes' => array(
'class' => array('button'),
),
)),
);
}
}
}
return $render;
}