-
Notifications
You must be signed in to change notification settings - Fork 4
/
simplereach.module
275 lines (204 loc) · 7.44 KB
/
simplereach.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
<?php
/**
* @file
* A Module developed by the Meredith Corportation for a SimpleReach
* Implementation.
*
* This module allows Simple Reach to run and associates the appropriate pub-id
* from simplereach.com. Users can pick which node types (categories) which
* SimpleReach runs against.
*
* Built by Aaron Staker with assistance from Scott Phillips,
* Shankar Srinivasan, and Joshua Stewardson.
*
* Feel free to contact me with any questions or concerns:
* [email protected]; on drupal.org I go by holyfire.
*
* Sandbox project location: https://drupal.org/sandbox/holyfire/2163477
*/
/**
* Implements hook_help().
*/
function simplereach_help($path, $arg) {
if ($path == 'admin/help#simplereach')
return t('Full information on implementation can be found here: !url, 12/2013. Admin settings are located at admin/config/services/simplereach.',
array(
'!url' => l(
'https://drupal.org/sandbox/holyfire/2163477',
'https://drupal.org/sandbox/holyfire/2163477',
array(
'attributes' => array(
'target' => '_blank'
)
)
)
)
);
}
/**
* Implements hook_menu().
*/
function simplereach_menu() {
$items = array();
$items['admin/config/services/simplereach'] = array (
'title' => 'SimpleReach Settings',
'description' => 'Configuration options for the SimpleReach module, developed by Aaron Staker, Meredith Corporation.',
'page callback' => 'simplereach_admin_settings',
//ensures current user has appropriate access
'access callback' => 'user_access',
'access arguments' => array('administer site configuration'),
);
return $items;
}
/**
* Admin settings.
*/
function simplereach_admin_settings() {
return drupal_get_form('simplereach_settings_form');
}
/**
* Implements hook_settings_form().
*/
function simplereach_settings_form() {
global $_simplereach_set;
$settings = $_simplereach_set;
$form = array();
// allows the asignment of the SimpleReach PID
$form['simplereach']['pid'] = array(
'#type' => 'textfield',
'#title' => t('Simple Reach PID'),
'#description' => t('The 24 character publisher ID provided by SimpleReach. <b style="color: red">WARNING there is no validation beyond length.</b>'),
'#default_value' => variable_get('simplereach_pid',
'xxa8879300e48g84f9000009'), // the default value is 24 characters
'#required' => TRUE,
'#maxlength' => 24,
);
$form['simplereach']['domain'] = array(
'#type' => 'textfield',
'#title' => t('Simple Reach Domain'),
'#description' => t('If your site makes use of custom domains, you may need to set this value. See http://simplereach.com/docs/custom-domains/'),
'#default_value' => variable_get('simplereach_domain', ''),
);
$form['simplereach']['include_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Include Content Types'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
//shows the content types in the admin form
$form['simplereach']['include_fieldset']['include_content_types'] = array(
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#options' => array_map('check_plain', node_type_get_names()),
'#title' => t('Include Content Types'),
'#default_value' => variable_get('simplereach_content_type', array()),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save settings'),
);
return $form;
}
/**
* Implements hook_settings_form_validate().
*/
// function simplereach_settings_form_validate(&$form, &$form_state){INSERT VALIDATION CHECK HERE}
// Should be of exactly the correct number of characters, not currently implemented without specific criteria (also key length may change)
/**
* Implements hook_settings_form_submit().
*/
function simplereach_settings_form_submit(&$form, &$form_state){
global $_simplereach_defaults;
$form_values = $form_state['values'];
// Saves variables to the database.
variable_set('simplereach_pid', $form_values['pid']);
variable_set('simplereach_domain', $form_values['domain']);
variable_set('simplereach_content_type',
$form_values['include_content_types']);
// Confirms the PID
drupal_set_message(t('PID is set to @pid.',
array('@pid' => $form_state['values']['pid'])));
}
/**
* Implements hook_preprocess().
*/
function simplereach_preprocess_page(&$variables) {
if ('node' == arg(0) && is_numeric(arg(1))) {
$node = node_load(arg(1));
// Find the content types that this should appear on.
$simplereach_content_type_get = variable_get('simplereach_content_type');
$selected_content_types = array();
// Determine if the content type is true and build an array
// of the selected content types.
foreach ($simplereach_content_type_get as $content_type)
if ($content_type != '0')
$selected_content_types[] = $content_type;
// The magic happens here -- if the content type is checked, the JS is added
if (in_array($node->type, $selected_content_types)) {
$pid = variable_get('simplereach_pid');
$domain = variable_get('simplereach_domain');
$title = $node->title;
$url = 'http://' . $_SERVER['HTTP_HOST'] . '/'
. drupal_get_path_alias('node/' . arg(1));
// @FIXME check for https? -- jwc 11Jun2015
$created = $node->created;
$authors = $node->name;
$channels = simplereach_get_channels($node);
$tags = simplereach_get_tags($node->field_tags);
$formatted_date = date('c', $created);
$configjs = array(
'pid' => $pid,
'title' => $title,
'url' => $url,
'date' => $formatted_date,
'authors' => $authors,
'channels' => $channels,
'tags' => $tags,
'landing_url' => $url,
'iframe' => TRUE,
'ignore_errors' => FALSE,
);
if (!empty($domain)) $configjs['domain'] = $domain;
$script = '__reach_config = ' . str_replace('\/', '/',
json_encode($configjs)) . ';';
$script .= "
(function(){
var s = document.createElement('script');
s.async = true;
s.type = 'text/javascript';
s.src = document.location.protocol + '//d8rk54i4mohrb.cloudfront.net/js/reach.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(s);
})();";
// @TODO: see if this script can be added as a file, with __reach_config
// coming from Drupal.settings. jwc 11Jun2015
// drupal_add_js(array('simplereach' => $configjs), 'setting');
drupal_add_js($script, array('scope' => 'header', 'type' => 'inline'));
} // if selected content type
} // if node
} // simplereach_preprocess_page();
function simplereach_get_tags($field_tags) {
$tags = array();
foreach($field_tags[LANGUAGE_NONE] as $tag)
$tags[] = taxonomy_term_load($tag['tid'])->name;
return $tags;
}
function simplereach_get_channels($node) {
$channels = array();
$terms = field_get_items('node', $node, 'field_category');
if (is_array($terms))
foreach ($terms as $term) {
// get parent terms
$parents = taxonomy_get_parents_all($term['tid']);
// make highest-level categories first in the array
$parents = array_reverse($parents);
}
foreach ($parents as $parent)
$channels[] = $parent->name;
return $channels;
}
function simplereach_get_content_types($simplereach_content_type) {
$tags = array();
foreach ($simplereach_content_type[LANGUAGE_NONE] as $tag)
$tags[] = "'". taxonomy_term_load($tag['tid'])->name . "'";
return implode(',', $tags);
}