-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathimagex_contest.module
436 lines (387 loc) · 14.6 KB
/
imagex_contest.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
<?php
/**
* @file
* Code for the Imagex Contest feature.
*/
define('IMAGEX_CONTEST_FINALIST', 1);
define('IMAGEX_CONTEST_WINNER', 2);
include_once 'imagex_contest.features.inc';
/**
* Implements hook_ctools_plugin_directory().
*/
function imagex_contest_ctools_plugin_directory($owner, $plugin_type) {
if ($owner == 'ctools' && $plugin_type == 'access') {
return 'plugins/' . $plugin_type;
}
}
/**
* Implements hook_menu().
*/
function imagex_contest_menu() {
$items['contest/select-entry/%node/%node/%'] = array(
'page callback' => 'drupal_get_form',
'page arguments' => array('imagex_contest_contest_admin_form', 2, 3, 4),
'access callback' => 'imagex_contest_contest_admin_access',
'access arguments' => array(2, 3, 4),
);
return $items;
}
/**
* Implements hook_node_view().
*/
function imagex_contest_node_view($node, $view_mode, $langcode) {
//For users to enter contest if allowed
if ($node->type == 'contest' && $links = imagex_contest_contest_entry_links($node)) {
$node->content['links']['imagex_contest_add_entry'] = array(
'#theme' => 'links__imagex_contest_add_entry',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
);
}
// For admins of contests to quickly select finalists & winners
if ($node->type == 'contest_entry' && $links = imagex_contest_contest_admin_links($node)) {
$node->content['links']['imagex_contest_contest_admin_links'] = array(
'#theme' => 'links__imagex_contest_add_entry',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
);
}
}
/**
* Implements of hook_form_FORM_ID_alter()
*/
function imagex_contest_form_contest_entry_node_form_alter(&$form, &$form_state, $form_id) {
$node = $form_state['node'];
// Attach contest rules/terms checkbox on new entries
if (empty($node->nid)) {
$contest_rules = '';
if (!empty($_GET['field_contest_entry_contest'])) {
if ($contest = node_load($_GET['field_contest_entry_contest'])) {
$contest_wrapper = entity_metadata_wrapper('node', $contest);
$contest_rules = $contest_wrapper->field_contest_rules->value(array('sanitize' => TRUE));
}
}
// Container for rules elements
$form['imagex_contest_rules'] = array(
'#type' => 'container',
'#weight' => 99,
);
// Rules is required field so should be present
if (!empty($contest_rules)) {
// Display rules as markup
$form['imagex_contest_rules']['imagex_contest_rules_view'] = array(
'#type' => 'item',
'#title' => t("Contest Rules"),
'#markup' => '<div class="imagex-contest-contest-rules">' . $contest_rules['safe_value'] . '</div>',
);
}
// Show checkbox regardless
$form['imagex_contest_rules']['imagex_contest_rules_agree'] = array(
'#type' => 'checkbox',
'#title' => t("I agree that my entry complies with the rules of this contest."),
'#required' => TRUE,
);
$form['actions']['#weight'] = 100;
}
}
/**
* Implements HOOK_form_alter()
*/
function imagex_contest_form_alter(&$form, &$form_state, $form_id) {
// Temp fix for https://drupal.org/node/1750950
// Boolean single checkboxes have no labels
switch ($form_id) {
case 'views_content_views_panes_content_type_edit_form':
$checkboxes = array(
'field_contest_entry_open_value',
'field_contest_voting_open_value',
);
if (!empty($form['exposed'])) {
foreach ($checkboxes as $checkbox) {
if (!empty($form['exposed']['filter-' . $checkbox])) {
$form['exposed']['filter-' . $checkbox][$checkbox]['#options'][0] = t('No');
$form['exposed']['filter-' . $checkbox][$checkbox]['#options'][1] = t('Yes');
}
}
}
break;
}
}
/**
* Implements hook_views_bulk_operations_form_alter()
*
* @see: views_bulk_operations.api.php
*/
function imagex_contest_views_bulk_operations_form_alter(&$form, &$form_state, $vbo) {
// Hijack the "no mail" variable for the rules_imagex_contest_entry_as_winner & rules_imagex_contest_entry_as_finalist
// rules and make it look better
$view = $vbo->view;
if ($view->name == 'imagex_contest_entries_admin' && $form_state['step'] == 'views_bulk_operations_config_form' && !empty($form['parameter']['notify_entry_author'])) {
$form['parameter']['notify_entry_author']['settings']['notify_entry_author']['#title'] = t('Notify contest entry author?');
$form['parameter']['notify_entry_author']['settings']['notify_entry_author']['#description'] = t('By default contest entry authors will be notified via email about their entry
being selected as a winner or finalist in the contest. Select "no" to not notify them.');
// rules requires the parameter so have to pass something hence yes & no not opt out
$form['parameter']['notify_entry_author']['settings']['notify_entry_author']['#type'] = 'radios';
$form['parameter']['notify_entry_author']['settings']['notify_entry_author']['#options'] = array(
1 => t('Yes'),
2 => t('No')
);
$form['parameter']['notify_entry_author']['settings']['notify_entry_author']['#default_value'] = 1;
}
}
/**
* Implements HOOK_preprocess_node()
*/
function imagex_contest_preprocess_node(&$vars) {
$node = $vars['node'];
// Add class to contest entries if finalist or winner
if ($node->type == 'contest_entry') {
$node_wrapper = entity_metadata_wrapper('node', $node);
if (!empty($node_wrapper->field_contest_entry_status)) {
foreach ($node_wrapper->field_contest_entry_status->getIterator() as $delta => $value) {
$vars['classes_array'][] = 'node-contest-entry-' . trim(strtolower($value->label()));
}
}
}
}
/**
* Determines if a contest is open to entries & global has access
*
* Returns links array based on access
*/
function imagex_contest_contest_entry_links($node) {
global $user;
$links = array();
// User access
if (!user_access('create contest_entry content')) {
// Logged in but no access
$submit_entry_title = t('You do not have sufficient permissions to submit an entry to this contest.');
$submit_entry_class = array('imagex-contest-add-entry-noaccess');
// Not logged in
if (empty($user->uid)) {
$submit_entry_title = t('<a href="@login">Log in</a> or <a href="@register">register</a> to submit an entry to this contest.', array('@login' => url('user/login', array('query' => drupal_get_destination())), '@register' => url('user/register', array('query' => drupal_get_destination()))));
$submit_entry_class = array('imagex-contest-add-entry-anonymous');
}
// Give user feedback
$links['imagex_contest_add_entry_noaccess'] = array(
'title' => $submit_entry_title,
'html' => TRUE,
'attributes' => array(
'title' => strip_tags($submit_entry_title),
'class' => $submit_entry_class,
),
);
}
// Contest is no longer open to entries
$node_wrapper = entity_metadata_wrapper('node', $node);
if (!empty($node_wrapper->field_contest_entry_open)) {
$content_open_entry = $node_wrapper->field_contest_entry_open->value();
if (empty($content_open_entry)) {
$links['imagex_contest_add_entry_noaccess'] = array(
'title' => t('This contest is no longer open to entries.'),
'html' => TRUE,
'attributes' => array(
'title' => t('This contest is no longer open to entries'),
'class' => array('imagex-contest-add-entry-closed'),
),
);
}
}
// Contest is no longer open for voting
$node_wrapper = entity_metadata_wrapper('node', $node);
if (!empty($node_wrapper->field_contest_voting_open) && imagex_contest_contest_has_entries($node)) {
$content_open_voting = $node_wrapper->field_contest_voting_open->value();
$content_open_entry = $node_wrapper->field_contest_entry_open->value();
if (empty($content_open_voting) && empty($content_open_entry)) {
$links['imagex_contest_add_vote_noaccess'] = array(
'title' => t('This contest is no longer open to voting.'),
'html' => TRUE,
'attributes' => array(
'title' => t('This contest is no longer open to voting.'),
'class' => array('imagex-contest-add-vote-closed'),
),
);
}
}
// Default, contest open and ready
if (empty($links)) {
$links['imagex_contest_add_entry'] = array(
'title' => t('Submit your entry now'),
'href' => 'node/add/contest-entry',
'query' => array('field_contest_entry_contest' => $node->nid) + drupal_get_destination(),
'attributes' => array(
'title' => t('Submit your entry into this contest now!'),
'class' => array('imagex-contest-add-entry'),
),
);
}
return $links;
}
/**
* Provides contest admin links on entries ie. Select As Finalist
*
* Returns links array based on access
*/
function imagex_contest_contest_admin_links($node) {
$links = array();
// Get related contest data
if (!empty($node->field_contest_entry_contest[LANGUAGE_NONE][0]['entity'])) {
// Viewing contest entry comes will full entity loaded
$contest_node = $node->field_contest_entry_contest[LANGUAGE_NONE][0]['entity'];
}
else {
// Otherwise we just get the entity id
$contest_node = entity_load('node', array($node->field_contest_entry_contest[LANGUAGE_NONE][0]['target_id']));
if (!empty($contest_node)) {
$contest_node = array_shift($contest_node);
}
}
$contest_entry_node = $node;
// Both contest & entry nodes are required
if (empty($contest_node) || empty($contest_entry_node)) {
return FALSE;
}
// User access
if (!imagex_contest_contest_admin_access($contest_node, $contest_entry_node)) {
return FALSE;
}
// Load Contest status, metadata_wrapper throws errors without property checking
$contest_node_wrapper = entity_metadata_wrapper('node', $contest_node);
if (empty($contest_node_wrapper->field_contest_entry_open) || empty($contest_node_wrapper->field_contest_voting_open)) {
return FALSE;
}
// Contest no longer open
$contest_entry_open = $contest_node_wrapper->field_contest_entry_open->value();
$contest_voting_open = $contest_node_wrapper->field_contest_voting_open->value();
if (empty($contest_entry_open) && empty($contest_voting_open)) {
return FALSE;
}
// Check entries current status (finalist or winner)
$statuses = array(
1 => 'finalist',
2 => 'winner',
);
$contest_entry_node_wrapper = entity_metadata_wrapper('node', $contest_entry_node);
if (empty($contest_entry_node_wrapper->field_contest_entry_status)) {
return FALSE;
}
$contest_entry_status = $contest_entry_node_wrapper->field_contest_entry_status->value();
if (!empty($contest_entry_status)) {
// Entry already finalist or winner, kill the
foreach ($contest_entry_status as $key) {
unset($statuses[$key]);
}
}
// Default, make links for both statuses
if (!empty($statuses)) {
foreach ($statuses as $key => $status) {
$links['imagex_contest_contest_entry_admin_' . $status] = array(
'title' => t('Select Entry As @status', array('@status' => drupal_ucfirst($status))),
'href' => 'contest/select-entry/' . $contest_node->nid . '/' . $contest_entry_node->nid . '/' . $status,
'query' => drupal_get_destination(),
'attributes' => array(
'title' => t('Select Entry As @status', array('@status' => drupal_ucfirst($status))),
'class' => array('imagex-contest-contest-entry-admin imagex-contest-contest-entry-admin-' . $status),
),
);
}
}
return $links;
}
/**
* Checks a user can admin a contest
*/
function imagex_contest_contest_admin_access($contest_node, $contest_entry_node, $status = '') {
global $user;
if (is_array($contest_node)) {
$contest_node = array_shift($contest_node);
}
if (($user->uid == $contest_node->uid && (user_access('create contest_entry content') ||
user_access('administer nodes'))) || user_access('administer nodes')) {
return TRUE;
}
return FALSE;
}
/**
* Invokes a rules set once user confirms
*
* @param $contest_node
* Related contest node object
* @param $contest_entry_node
* Contest entry node to run workflow for
* @param $statuses
* Array, determines the action to perform, select as finalist or winner
*/
function imagex_contest_contest_admin_form($form, &$form_state, $contest_node, $contest_entry_node, $status) {
$rules_sets = array(
'finalist' => 'rules_imagex_contest_entry_as_finalist',
'winner' => 'rules_imagex_contest_entry_as_winner',
);
$form = array();
$form['#rules_set'] = $rules_sets[$status];
$form['#contest_node'] = $contest_node;
$form['#contest_entry_node'] = $contest_entry_node;
$form['entry_author_notify'] = array(
'#type' => 'checkbox',
'#title' => t('Notify contest entry author?'),
'#default_value' => 1,
);
$form = confirm_form($form,
t('Are you sure you want to select this entry as a %status?', array('%status' => $status)),
$_GET['q']
);
return $form;
}
/**
* Submit callback for imagex_contest_contest_admin_form()
*/
function imagex_contest_contest_admin_form_submit($form, &$form_state) {
$rules_set = $form['#rules_set'];
$contest_node = $form['#contest_node'];
$contest_entry_node = $form['#contest_entry_node'];
$entry_author_notify = empty($form_state['values']['entry_author_notify']) ? 2 : 1;
// Invoke rules
$rule = rules_invoke_component($rules_set, $contest_entry_node, $entry_author_notify);
}
/**
* Checks if given contest has any entries
*
* @param $contest_node
* Related contest node object or string containing nid
* @param $statuses
* Array, status of entries to check against ie. finalist or winner
*/
function imagex_contest_contest_has_entries($contest_node, $statuses = array(IMAGEX_CONTEST_FINALIST)) {
// Check if the View returns results
$view_name = 'imagex_contest_entries_callback'; // lightweight version of imagex_contest_entries
$args = array();
$result = '';
// Add nid as argument
if (is_object($contest_node)) {
if (!empty($contest_node->nid)) {
$args = array($contest_node->nid);
}
}
else {
$args = array($contest_node);
}
// Status of enrties
if (!empty($statuses)) {
$args[] = implode(', ', $statuses);
}
// views_get_view_result give errors so execute view direct
$view = views_get_view($view_name);
if (is_object($view)) {
if (is_array($args)) {
$view->set_arguments($args);
}
$view->init_display();
$view->pre_execute($args);
$view->execute();
$result = $view->result;
}
if (!empty($result)) {
return TRUE;
}
return FALSE;
}