/
transition_pse.module
441 lines (408 loc) · 14.4 KB
/
transition_pse.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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
<?php
/**
* @file Transition Project Sharing Engine
*
* Needs mild refactoring:
* @todo run through Coder.
* @todo break out user stuff to .user.inc.
* @todo move any text theme() functions (widget thanks, about) to .tpl.php?.
* @todo clean up code paths.
* @todo do a security audit.
*/
// The content type for PSE submissions
define('TRANSITION_PSE_CT', 'project_pse');
// Radius (miles) when searching for nearby projects
define('TRANSITION_PSE_RADIUS', '50');
// Mailchimp ID for Widget Owners mailing list
define('TRANSITION_PSE_MC_LID', '57fc02ca3d');
/**
* Implementation of hook_init().
*
* Switches to the PSE the theme if we're viewing a widget page.
*/
function transition_pse_init() {
global $custom_theme;
// current page is a view or entry widget
if (arg(0) == 'pse' && (arg(1) == 'view' || arg(1) == 'entry')) {
$custom_theme = 'pse';
}
// or a pse callback for quicktabs
if (arg(0) == 'quicktabs' && arg(1) == 'ajax' && arg(3) == 'pse') {
$custom_theme = 'pse';
}
}
/**
* Implementation of hook_perm().
*/
function transition_pse_perm() {
return array(
'get widget code',
'view submissions in moderation',
);
}
/**
* Implementation of hook_menu().
*/
function transition_pse_menu() {
$items = array();
// get widget code page
$items['pse/widget/get'] = array(
'title' => 'Get the Projects Widget!',
'page callback' => 'transition_pse_widget_get',
'access arguments' => array('access content'),
'file' => 'transition_pse.widget.inc',
'type' => MENU_CALLBACK,
);
// unified reg/login first
$items['pse/entry/add-project/%/%'] = array(
// 'title' => 'Add your project',
'page callback' => 'transition_pse_entry_form_steps',
'page arguments' => array(3, 4),
'access arguments' => array('access content'),
'file' => 'transition_pse.pages.inc',
'type' => MENU_CALLBACK,
);
// thanks page -- says thanks, shows their project, promotes TN
$items['pse/entry/add-project/thank-you/%/%'] = array(
// 'title' => 'Add your project',
'page callback' => 'transition_pse_entry_thanks',
'page arguments' => array(4, 5),
'access arguments' => array('access content'),
'file' => 'transition_pse.pages.inc',
'type' => MENU_CALLBACK,
);
// view pages -- SIDEBAR
$items['pse/view/sidebar/%/%'] = array(
// 'title' => 'Add your project',
'page callback' => 'transition_pse_view_sidebar',
'page arguments' => array(3, 4),
'access arguments' => array('access content'),
'file' => 'transition_pse.pages.inc',
'type' => MENU_CALLBACK,
);
// view pages -- FULL
$items['pse/view/full/%/%'] = array(
'title' => 'Transition Projects',
'page callback' => 'transition_pse_view_full',
'page arguments' => array(3, 4),
'access arguments' => array('access content'),
'file' => 'transition_pse.pages.inc',
'type' => MENU_CALLBACK,
);
$items['pse/view/full/search'] = array(
'title' => 'Find projects',
'page callback' => 'transition_pse_view_full_search',
'access arguments' => array('access content'),
'file' => 'transition_pse.pages.inc',
'type' => MENU_CALLBACK,
);
// view pages -- about
$items['pse/view/about'] = array(
'title' => 'About the PSE',
'page callback' => 'transition_pse_view_about',
'access arguments' => array('access content'),
'file' => 'transition_pse.pages.inc',
'type' => MENU_CALLBACK,
);
// accept project submission
$items['pse/submission/%/accept'] = array(
'title' => 'Accept project submission',
'page callback' => 'transition_pse_submission_accept',
'page arguments' => array(2),
'access callback' => 'transition_pse_web_point_of_contact_initiatives_access',
'file' => 'transition_pse.moderation.inc',
'type' => MENU_CALLBACK,
);
// reject project submission
$items['pse/submission/%/reject'] = array(
'title' => 'Reject project submission',
'page callback' => 'transition_pse_submission_reject',
'page arguments' => array(2),
'access callback' => 'transition_pse_web_point_of_contact_initiatives_access',
'file' => 'transition_pse.moderation.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implementation of hook_form_alter()
*
* when _transition_pse_overriding_form_flag is set, it removes all non-mandatory
* items from the User Register form, otherwise just hides the widget owner's
* mailchimp newsletter option.
*/
function transition_pse_form_alter(&$form, &$form_state, $form_id) {
// Only affect user register form
if ($form_id != 'user_register') return;
// Always hide widget owner's newsletter option
// @BUG? see: https://tech.transitionnetwork.org/trac/ticket/447
$form['mailchimp_list_forms']['wrapper' . TRANSITION_PSE_MC_LID]['#access'] = FALSE;
// Are we registering from PSE?
if (_transition_pse_overriding_form_flag() == TRUE) {
// PSE registration, hide all the bits we don't need...
_transition_pse_hide_non_manadatory_elements($form);
// hide username since it will be auto-created from first/last name on user save
// but for now make it unique to get through safely
$form['account']['name']['#type'] = 'hidden';
$form['account']['name']['#default_value'] = 'PSEUSER--' . uniqid();
unset($form['account']['pass']['#type']);
// email tweak message
$form['account']['mail']['#description'] = t('We will not use your email address for anything else, nor give it to anyone else. ') . $form['account']['mail']['#description'];
// stop LoginToboggan doing its stuff
$logintoboggan_index = array_search('logintoboggan_user_register_validate', $form['#validate']);
if ($logintoboggan_user_login_validate !== FALSE) {
unset($form['#validate'][$logintoboggan_index]);
}
// ensure we can build username on submit
array_unshift($form['#submit'], 'transition_pse_user_register_submit');
// remove field_roles_offered, field_initiative and field_themes from
// display/validation
$form['group_user_interest']['field_roles_offered']['#type'] = 'optionwidgets_select';
$form['group_user_interest']['field_roles_offered']['#access'] = FALSE;
unset($form['group_user_interest']['field_initiative']);
unset($form['group_user_interest']['field_themes']);
// and redirect to the thank you page when done
$redirect_to = 'pse/entry/add-project' . '?login=' . uniqid();
$_REQUEST['destination'] = $redirect_to;
}
}
/**
* User registraton submit callback
*
* Builds a unique username based on first & last name, avoiding collisions
*/
function transition_pse_user_register_submit($form, &$form_state) {
// build username from first & last names
$first_name = $form_state['content_profile_registration']['profile']['node']->field_name_first[0]['value'];
$last_name .= $form_state['content_profile_registration']['profile']['node']->field_name_last[0]['value'];
// make alphanumeric only, set proper noun capitalisation
$full_name = ucwords(strtolower($first_name . ' ' . $last_name));
$full_name = trim(preg_replace("/[^a-zA-Z0-9\s]+/", "", $full_name));
// then ensure not too long after making space for 'counter' on end
if (strlen($full_name) >= USERNAME_MAX_LENGTH - 3) $name = substr($name, 0, USERNAME_MAX_LENGTH - 3);
// ensure username is unique
$attempt = 0;
$append = '';
$success = FALSE;
while (!$success && $attempt < 256) {
// add attempt number to end of username if not unique - e.g. "John Smith 2"
if ($attempt > 0) $append = ' ' . dechex($attempt);
$result = db_query("SELECT name FROM {users} u WHERE u.name = '%s'", $full_name . $append);
if (db_fetch_array($result) === FALSE) {
// found a unique name, use it!
$form_state['values']['name'] = $full_name . $append;
break;
}
// otherwise carry on up to 0xff appended to name... or 256 chances.
$attempt++;
}
}
/**
* Implementation of hook_user().
*
* Ensures PSE registration/login submits go back to entry PSE form.
* Also builds a username from first and last name elements for PSE.
*/
function transition_pse_user($op, &$edit, &$account, $category = NULL) {
// only for PSE entry widget
if (arg(0) <> 'pse' || arg(1) <> 'entry') return;
switch ($op) {
case 'login':
case 'register':
// redirect login/form to itself for PSE only
// build url with hex timestamp to minise caching issues
$_REQUEST['destination'] = request_uri() . '?' . $op . '=' . uniqid();
break;
}
}
/**
* Implementation of hook_locationapi().
*
* Location module... A lot of code just to change the title of the 'city' field,
* but hey ho...
*/
function transition_pse_locationapi(&$obj, $op, $a3 = NULL, $a4 = NULL, $a5 = NULL) {
if ($op == 'field_expand' ) {
switch ($a3) {
case 'city':
return array(
'#type' => 'textfield',
'#title' => t('Nearest village/town/city'),
'#default_value' => $obj,
'#size' => 64,
'#maxlength' => 255,
'#description' => NULL,
'#attributes' => NULL,
'#required' => ($settings['collect'] == 2),
);
break;
case 'postal_code':
return array(
'#type' => 'textfield',
'#title' => t('Postal code'),
'#description' => t('Postcode does not have to be exact; the first half is fine.'),
'#default_value' => $obj,
'#size' => 16,
'#maxlength' => 16,
'#required' => ($settings['collect'] == 2),
);
break;
}
}
}
/**
* Implementation of hook_mail()
*/
function transition_pse_mail($key, &$message, $params) {
module_load_include('inc', 'transition_pse', 'transition_pse.moderation');
global $user;
// set language from message
$language = $message['language'];
switch ($key) {
case 'submitter_project_thanks':
transition_pse_submitter_mail_text('thanks', $message, $params);
break;
case 'submitter_project_accepted':
transition_pse_submitter_mail_text('accepted', $message, $params);
break;
case 'submitter_project_rejected':
transition_pse_submitter_mail_text('rejected', $message, $params);
break;
case 'webmaster_submission_new':
transition_pse_webmaster_mail_text('new', $message, $params);
break;
case 'webmaster_submission_reminder':
transition_pse_webmaster_mail_text('reminder', $message, $params);
break;
}
}
/**
* Implementation of hook_theme().
*/
function transition_pse_theme($existing, $type, $theme, $path) {
$path = drupal_get_path('module', 'transition_pse') .'/theme';
return array(
'pse_widget_container' => array(
'arguments' => array('type' => NULL, 'widget_urls' => NULL, 'assets' => NULL, 'options' => NULL),
'file' => 'transition_pse.widget.inc',
),
'pse_widget_page' => array(
'template' => 'pse-page',
'path' => $path,
'arguments' => array('content' => NULL, 'header' => NULL, 'footer' => NULL),
),
'pse_widget_page_header' => array(
'template' => 'pse-page-header',
'path' => $path,
'arguments' => array('title' => NULL, 'tabs' => NULL),
),
'pse_widget_page_tabs' => array(
'template' => 'pse-page-tabs',
'path' => $path,
'arguments' => array('step' => NULL),
),
'pse_widget_page_footer' => array(
'template' => 'pse-page-footer',
'path' => $path,
),
'pse_widget_entry_container' => array(
'arguments' => array('type' => NULL, 'widget_urls' => NULL, 'options' => NULL),
'file' => 'transition_pse.widget.inc',
),
// thanks page
'pse_widget_entry_thanks' => array(
'file' => 'transition_pse.widget.inc',
),
'pse_widget_entry_thanks_content' => array(
'template' => 'pse-page-entry-thanks-content',
'arguments' => array('submit_another_url' => NULL),
'path' => $path,
),
// widget structure
'pse_widget_view_container' => array(
'arguments' => array('type' => NULL, 'widget_urls' => NULL, 'options' => NULL),
'file' => 'transition_pse.widget.inc',
),
'pse_widget_view_inner' => array(
'arguments' => array('content' => NULL),
'file' => 'transition_pse.widget.inc',
),
// about tab
'pse_widget_view_about' => array(
'template' => 'pse-page-view-about',
'path' => $path,
),
);
}
/**
* template_preprocess function for pse_widget_page.tpl.php
*
* Adds $header and $footer theme calls to page
*/
function template_preprocess_pse_widget_page(&$vars) {
// get step from args for tabs
if (arg(3) == 'thank-you') {
$step = 3;
}
elseif (user_is_logged_in()) {
// logged in
$step = 2;
}
else {
$step = 1;
}
// build tabs
$tabs = theme('pse_widget_page_tabs', $step);
$vars['header'] = theme('pse_widget_page_header', drupal_get_title(), $tabs);
$vars['footer'] = theme('pse_widget_page_footer');
}
/**
* Wraps a static flag in a function so we know when we want to override a form
*/
function _transition_pse_overriding_form_flag($set = FALSE) {
static $flag = FALSE;
if ($set) $flag = TRUE;
return $flag;
}
/**
* Access callback for users who are a Web Point of Contact for an initiative
*
* @return TRUE when current $user is a Web PoC for an initiative.
*/
function transition_pse_web_point_of_contact_initiatives_access() {
global $user;
if ($user->uid == 1) return TRUE;
$nids = transition_pse_web_point_of_contact_initiatives($user);
if (is_array($nids) && (count($nids) > 0)) {
return TRUE;
}
return FALSE;
}
/**
* Gets array nids of initiatives where Web Point of Contact is $account
*
* @return array of nids for initiatives where the $account is a web PoC.
*/
function transition_pse_web_point_of_contact_initiatives($account) {
// static cache this to avoid multiple lookups
static $account_web_pocs = NULL;
if ($account->uid > 0 && is_null($account_web_pocs)) {
// look up Web PoC field for published initiatives for this user, store nid
// of result (up to max 10 per user)
$result = db_query(
"SELECT n.nid FROM {content_field_webcontact_user} poc
INNER JOIN node n ON n.nid = poc.nid
WHERE poc.field_webcontact_user_uid = '%d'
AND n.type IN ('initiative_profile', 'initiative_hub_profile')
AND n.status = 1
LIMIT 0, 10",
$account->uid
);
// store ini nids in array for later
$account_web_pocs = array();
while ($row = db_fetch_array($result)) {
$account_web_pocs[$row['nid']] = $row['nid'];
}
}
return $account_web_pocs;
}