forked from artesis/ding_campaign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ding_campaign.module
654 lines (556 loc) · 18.9 KB
/
ding_campaign.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
<?php
/**
* @file
* Provides campaign feature to various node types in Ding.
*/
define('DING_CAMPAIGN_PATH', drupal_get_path('module', 'ding_campaign'));
define('DING_CAMPAIGN_DEFAULT_COUNT', 3);
/**
* Implements hook_menu().
*/
function ding_campaign_menu() {
$items = array();
$items['admin/config/ding/campaign'] = array(
'title' => 'Ding! Campaign',
'description' => 'Configure the Ding! campaign system.',
'page callback' => 'drupal_get_form',
'page arguments' => array('ding_campaign_configure'),
'access arguments' => array('access administration pages'),
'file' => 'ding_campaign.admin.inc',
);
$items['node/%node/campaign_rules'] = array(
'title' => 'Campaign rules',
'description' => 'Edit campaign rules',
'page callback' => 'ding_campaign_rules_admin',
'page arguments' => array(1),
'access arguments' => array('access campaign rules'),
'file' => 'ding_campaign.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
$items['ding_campaign/autocomplete/%'] = array(
'title' => 'Campaign rule autocomplete',
'description' => 'Autocomplete field for campaign rule values.',
'page callback' => 'ding_campaign_admin_autocomplete',
'page arguments' => array(2),
'access arguments' => array('access campaign rules'),
'file' => 'ding_campaign.admin.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_perimission().
*/
function ding_campaign_permission() {
$perm = array();
$perm['access campaign rules'] = array(
'title' => t('Access campaign rules'),
'description' => t('Enable access to campaign nodes display rules'),
);
return $perm;
}
/**
* Implements hook_admin_paths().
*/
function ding_campaign_admin_paths() {
$paths = array(
'node/*/campaign_rules' => TRUE,
);
return $paths;
}
/**
* Implements hook_theme().
*/
function ding_campaign_theme($existing, $type, $theme, $path) {
$hooks = array();
$hooks['ding_campaign'] = array(
'variables' => array('content' => NULL),
'template' => 'ding_campaign',
'path' => DING_CAMPAIGN_PATH . '/templates/',
);
return $hooks;
}
/**
* Displays certain campaigns.
*
* @param $context
* Display context used.
* @param $count
* Count of campaigns to display.
* @param $offset
* Offset of the first campaign in the database.
*
* @return
* HTML formatted string.
*/
function ding_campaign_display($context, $count, $offset, $style = 'medium') {
$campaigns = ding_campaign_get_campaigns($context, $count, $offset);
// There is at least one result.
if (is_array($campaigns) && count($campaigns) > 0) {
$block = '';
// Loop through campaigns.
foreach ($campaigns as $k => $v) {
$node = node_load($v);
$content = '';
$camp_settings = field_get_items('node', $node, 'field_camp_settings');
$base_class = 'ding-campaign-item';
// Select the display for campaign type.
switch ($camp_settings[0]['value']) {
case 'plain':
$camp_link = field_get_items('node', $node, 'field_camp_link');
$camp_text_plain = field_get_items('node', $node, 'field_camp_text_plain');
$content = '<div class="' . $base_class . ' ' . $base_class . '-id-' . $v . ' ding-campaign-item-plain"><a href="' . $camp_link[0]['value'] . '">' . $camp_text_plain[0]['value'] . '</a></div>';
break;
case 'image':
$camp_link = field_get_items('node', $node, 'field_camp_link');
$camp_image = field_get_items('node', $node, 'field_camp_image');
$image_title = field_get_items('file', (object) ($camp_image[0]), 'field_file_image_title_text');
$image_title = $image_title[0]['safe_value'];
$image_alt = field_get_items('file', (object) ($camp_image[0]), 'field_file_image_alt_text');
$image_alt = $image_alt[0]['safe_value'];
if (empty($image_alt)) {
$image_alt = $image_title;
}
$image_tag = theme('image_style', array(
'style_name' => $style,
'path' => $camp_image[0]['uri'],
'alt' => $image_alt,
'title' => $image_title,
));
$link_tag = l($image_tag, $camp_link[0]['value'], array('html' => TRUE, 'absolute' => TRUE, 'attributes' => array('target' => '_blank')));
$content = '<div class="' . $base_class . ' ' . $base_class . '-id-' . $v . ' ding-campaign-item-image">' . $link_tag . '</div>';
break;
case 'full':
$camp_text_full = field_get_items('node', $node, 'field_camp_text_full');
$content = '<div class="' . $base_class . ' ' . $base_class . '-id-' . $v . ' ding-campaign-item-full">' . $camp_text_full[0]['safe_value'] . '</div>';
break;
}
$theme = 'ding_campaign';
$block .= theme($theme, array('content' => $content));
}
return $block;
}
}
/**
* Fetch campaign from database according to context.
*
* @param $context
* @param $limit
* @param $offset
*
* @return
*/
function ding_campaign_get_campaigns($context, $limit, $offset) {
$campaigns = array();
// Process event campaigns.
if ($context['page'] != NULL && isset($context['page']->nid) && $context['page']->type == 'ding_event') {
$nid = $context['page']->nid;
$result = db_query('SELECT r.cid, d.weight
FROM {ding_campaign_rules} r, {ding_campaign} d, {node} n
WHERE r.nid = :nid
AND r.type = \'rule_event\'
AND n.status = 1
AND r.cid = d.cid
AND r.cid = n.nid
ORDER BY d.weight DESC', array(':nid' => (int) $nid))
->fetchAll();
foreach ($result as $k => $v) {
@$campaigns[$v->cid] += -50 + $v->weight;
}
}
// Process news campaigns.
if ($context['page'] != NULL && isset($context['page']->nid) && $context['page']->type == 'ding_news') {
$nid = $context['page']->nid;
$result = db_query('SELECT r.cid, d.weight
FROM {ding_campaign_rules} r, {ding_campaign} d, {node} n
WHERE r.nid = :nid
AND r.type = \'rule_news\'
AND n.status = 1
AND r.cid = d.cid
AND r.cid = n.nid
ORDER BY d.weight DESC', array(':nid' => (int) $nid))
->fetchAll();
foreach ($result as $k => $v) {
@$campaigns[$v->cid] += -45 + $v->weight;
}
}
// Process page campaigns.
if ($context['page'] != NULL && isset($context['page']->nid)) {
$nid = $context['page']->nid;
$result = db_query('SELECT r.cid, d.weight
FROM {ding_campaign_rules} r, {ding_campaign} d, {node} n
WHERE r.nid = :nid
AND r.type = \'rule_page\'
AND n.status = 1
AND r.cid = d.cid
AND r.cid = n.nid
ORDER BY d.weight DESC', array(':nid' => (int) $nid))
->fetchAll();
foreach ($result as $k => $v) {
@$campaigns[$v->cid] += -40 + $v->weight;
}
}
// Process path-based campaigns.
$result = db_query('SELECT r.cid, r.rule, d.weight
FROM {ding_campaign_rules} r, {ding_campaign} d, {node} n
WHERE r.type = \'rule_path\'
AND n.status = 1
AND r.cid = d.cid
AND r.cid = n.nid
ORDER BY d.weight DESC')
->fetchAll();
$path = drupal_get_path_alias($_GET['q']);
foreach ($result as $k => $v) {
$page_match = drupal_match_path($path, $v->rule);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $v->rule);
}
if ($page_match) {
@$campaigns[$v->cid] += -35 + $v->weight;
}
}
// Process search campaigns.
if ($context['search_term'] != NULL) {
$terms = trim(drupal_strtolower($context['search_term']));
$condition = $terms;
$multiple = FALSE;
// Multiple keywords, comma and/or space separated.
if (strpos($terms, ',') || strpos($terms, ' ')) {
$terms = preg_replace('/(,?\s+)/', ',', $terms);
$terms = explode(',', $terms);
$condition = $terms;
$multiple = TRUE;
}
$query = db_select('ding_campaign_rules', 'r');
$query->fields('r', array('cid'));
$query->join('ding_campaign', 'd', 'r.cid = d.cid');
$query->join('node', 'n', 'r.cid = n.nid');
$query->addField('d', 'weight');
$query->condition('n.status', 1, '=');
$query->condition('r.type', 'rule_term', '=');
if ($multiple) {
$query->condition('r.rule', $condition, 'IN');
}
// Single keyword.
else {
$query->condition('r.rule', $condition);
}
$query->orderBy('d.weight', 'DESC');
$result = $query->execute()->fetchAll();
foreach ($result as $k => $v) {
@$campaigns[$v->cid] += -30 + $v->weight;
}
}
// Process library campaigns.
if ($context['page'] != NULL && isset($context['page']->nid) && $context['page']->type == 'ding_library') {
$nid = $context['page']->nid;
$result = db_query('SELECT r.cid, d.weight
FROM {ding_campaign_rules} r, {ding_campaign} d, {node} n
WHERE r.nid = :nid
AND r.type = \'rule_library\'
AND n.status = 1
AND r.cid = d.cid
AND r.cid = n.nid
ORDER BY d.weight DESC', array(':nid' => (int) $nid))
->fetchAll();
foreach ($result as $k => $v) {
@$campaigns[$v->cid] += -25 + $v->weight;
}
}
// Process taxonomy campaigns.
if ($context['taxonomy_term'] != NULL && isset($context['taxonomy_term']->tid)) {
$tid = $context['taxonomy_term']->tid;
$result = db_query('SELECT r.cid, d.weight
FROM {ding_campaign_rules} r, {ding_campaign} d, {node} n
WHERE r.nid = :tid
AND r.type = \'rule_taxonomy\'
AND n.status = 1
AND r.cid = d.cid
AND r.cid = n.nid
ORDER BY d.weight DESC', array(':tid' => (int) $tid))
->fetchAll();
foreach ($result as $k => $v) {
@$campaigns[$v->cid] += -20 + $v->weight;
}
}
// Process generic campaigns.
$result = db_query('SELECT r.cid, d.weight
FROM {ding_campaign_rules} r, {ding_campaign} d, {node} n
WHERE r.type = \'rule_generic\'
AND n.status = 1
AND r.cid = d.cid
AND r.cid = n.nid
ORDER BY d.weight DESC')
->fetchAll();
foreach ($result as $k => $v) {
if (!isset($campaigns[$v->cid]) || $campaigns[$v->cid] == 0) {
$campaigns[$v->cid] += -15 + $v->weight;
}
}
asort($campaigns);
return array_slice(array_keys($campaigns), $offset, $limit);
}
/**
* Implements hook_form_alter().
*/
function ding_campaign_form_alter(&$form, &$form_state, $form_id) {
// Alter the Campaign addition form.
if ($form_id == 'ding_campaign_node_form') {
// Remove the 'N/A' type radiobox.
unset($form['field_camp_settings'][$form['field_camp_settings']['#language']]['#options']['_none']);
// Remove the 'None' value from weight dropdown.
unset($form['field_camp_weight'][$form['field_camp_weight']['#language']]['#options']['_none']);
$lang = $form['field_camp_settings']['#language'];
$form['field_camp_text_plain']['#states'] = array(
'visible' => array(
':input[name=field_camp_settings[' . $lang . ']]' => array('value' => 'plain')
)
);
$form['field_camp_image']['#states'] = array(
'visible' => array(
':input[name=field_camp_settings[' . $lang . ']]' => array('value' => 'image')
)
);
$form['field_camp_text_full']['#states'] = array(
'visible' => array(
':input[name=field_camp_settings[' . $lang . ']]' => array('value' => 'full')
)
);
$form['#validate'] = array('ding_campaign_campaign_node_form_validate');
}
else {
$enabled_types = variable_get('ding_campaign_node_types', array());
// Alter the specific node form, mentioned in Campaign settings.
if (isset($form['#node']) && isset($enabled_types[$form['#form_id']]) && ($enabled_types[$form['#form_id']] === $form['#form_id'])) {
$form['ding_campaigns_wrap'] = array(
'#type' => 'fieldset',
'#title' => 'Available campaigns',
);
$form['ding_campaigns_wrap']['ding_campaigns'] = array(
'#type' => 'checkboxes',
'#default_value' => isset($form['#node']->ding_campaigns) ? $form['#node']->ding_campaigns : array(),
'#options' => ding_campaign_get_list(),
'#description' => 'Select campaigns asociated with this node.',
);
}
}
}
/**
* Implements hook_node_insert().
*/
function ding_campaign_node_insert($node) {
// Insert the campaign type node.
if ($node->type == 'ding_campaign') {
$node_type = field_get_items('node', $node, 'field_camp_settings');
$node_weight = field_get_items('node', $node, 'field_camp_weight');
$data = array(
'cid' => $node->nid,
'type' => $node_type[0]['value'],
'weight' => $node_weight[0]['value'],
);
db_insert('ding_campaign')
->fields($data)
->execute();
return;
}
$enabled_types = variable_get('ding_campaign_node_types', array());
// Process other types of nodes (selected in backend).
if (isset($enabled_types[$node->form_id]) && $enabled_types[$node->form_id] === $node->form_id) {
if (isset($node->ding_campaigns) && is_array($node->ding_campaigns)) {
foreach ($node->ding_campaigns as $k => $v) {
$type = _ding_campaign_rule_by_type($node->type);
$data = array(
'nid' => $node->nid,
'cid' => $v,
'rule' => '[nid:' . $node->nid . ']',
'type' => $type,
);
// Skip the unused campaigns.
if ($v != 0) {
db_insert('ding_campaign_rules')
->fields($data)
->execute();
}
}
}
}
}
/**
* Implements hook_node_update().
*/
function ding_campaign_node_update($node) {
// Update the campaign if needed.
if ($node->type == 'ding_campaign') {
$node_type = field_get_items('node', $node, 'field_camp_settings');
$node_weight = field_get_items('node', $node, 'field_camp_weight');
// Update campaign fields.
$data = array(
'type' => $node_type[0]['value'],
'weight' => $node_weight[0]['value'],
);
db_update('ding_campaign')
->fields($data)
->condition('cid', $node->nid)
->execute();
return;
}
$enabled_types = variable_get('ding_campaign_node_types', array());
// Process other types of nodes (selected in backend).
if (isset($enabled_types[$node->type . '_node_form']) && $enabled_types[$node->type . '_node_form'] === $node->type . '_node_form') {
if (isset($node->ding_campaigns) && is_array($node->ding_campaigns)) {
// Renew the assigned campaigns.
db_delete('ding_campaign_rules')
->condition('nid', $node->nid)
->execute();
foreach ($node->ding_campaigns as $k => $v) {
$type = _ding_campaign_rule_by_type($node->type);
$data = array(
'nid' => $node->nid,
'cid' => $v,
'rule' => '[nid:' . $node->nid . ']',
'type' => $type,
);
// Skip the unused campaigns.
if ($v != 0) {
db_insert('ding_campaign_rules')
->fields($data)
->execute();
}
}
}
}
}
/**
* Implements hook_node_load().
*/
function ding_campaign_node_load($node) {
$nids = array_keys($node);
if ($node[$nids[0]]->type == 'ding_campaign') {
$result = db_select('ding_campaign_rules')
->fields('ding_campaign_rules', array('rule', 'type'))
->condition('cid', $node[$nids[0]]->nid)
->execute()
->fetchAll();
// Add assigned campaigns.
if (is_array($result) && count($result) > 0) {
foreach ($result as $k => $v) {
$node[$nids[0]]->camp_rule[$k] = $v->rule;
$node[$nids[0]]->camp_type[$k] = $v->type;
}
}
return;
}
$enabled_types = variable_get('ding_campaign_node_types', array());
// Process other types of nodes (selected in backend).
if (isset($enabled_types[$node[$nids[0]]->type . '_node_form']) && $enabled_types[$node[$nids[0]]->type . '_node_form'] === $node[$nids[0]]->type . '_node_form') {
$nids = array_keys($node);
$or = db_or()->condition('type', 'rule_library')->condition('type', 'rule_page')->condition('type', 'rule_news')->condition('type', 'rule_event');
$result = db_select('ding_campaign_rules')
->fields('ding_campaign_rules', array('cid'))
->condition('nid', $node[$nids[0]]->nid)
->condition($or)
->execute()
->fetchAll();
foreach ($result as $k => $v) {
$node[$nids[0]]->ding_campaigns[$v->cid] = $v->cid;
}
}
}
/**
* Implements hook_node_delete().
*/
function ding_campaign_node_delete($node) {
// Delete the campaign if needed.
if ($node->type == 'ding_campaign') {
db_delete('ding_campaign')
->condition('cid', $node->nid)
->execute();
db_delete('ding_campaign_rules')
->condition('cid', $node->nid)
->execute();
return;
}
$enabled_types = variable_get('ding_campaign_node_types', array());
// Process other types of nodes (selected in backend).
if (isset($enabled_types[$node->type . '_node_form']) && $enabled_types[$node->type . '_node_form'] === $node->type . '_node_form') {
if (isset($node->ding_campaigns) && is_array($node->ding_campaigns)) {
db_delete('ding_campaign_rules')
->condition('nid', $node->nid)
->execute();
}
}
}
/**
* Fetch available campaigns.
*
* @return
* Array of campaigns with ids and titles.
*/
function ding_campaign_get_list() {
$result = db_query("SELECT n.nid, n.title FROM {node} n
WHERE n.status <> 0
AND n.type = 'ding_campaign'
ORDER BY n.title")
->fetchAll();
$campaigns = array();
foreach ($result as $k => $v) {
$campaigns[$v->nid] = $v->title;
}
return $campaigns;
}
/**
* Implements hook_ctools_plugin_directory().
*/
function ding_campaign_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools') {
return 'plugins/' . $plugin;
}
}
/**
* Custom node form validation.
*
* @see ding_campaign_form_alter()
*/
function ding_campaign_campaign_node_form_validate($form, &$form_state) {
$input = $form_state['values'];
$language = $input['language'];
$campaign_type = $input['field_camp_settings'][$language][0]['value'];
$t_vars = array('@type' => $campaign_type);
// Forbid creation of campaign of type plain if the link is empty.
if ($campaign_type == 'plain') {
if (empty($input['field_camp_link'][$language][0]['value'])) {
form_set_error('field_camp_link', t('Campaign of type <em>@type</em> requires a link.', $t_vars));
}
}
// Force both image and link for image campaign types.
if ($campaign_type == 'image') {
if (empty($input['field_camp_link'][$language][0]['value']) || $input['field_camp_image'][$language][0]['fid'] == 0) {
form_set_error('', t('Campaign of type <em>@type</em> requires an image and link.', $t_vars));
}
}
}
/**
* Get rule name by node type.
* @param string $node_type
* Node type.
* @return string
* Rule name.
*/
function _ding_campaign_rule_by_type($node_type) {
// Set rule type
switch ($node_type) {
case 'library':
$type = 'rule_library';
break;
case 'ding_event':
$type = 'rule_event';
break;
case 'ding_news':
$type = 'rule_news';
break;
default:
$type = 'rule_page';
}
return $type;
}