forked from ding2/ding_campaign
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ding_campaign.module
518 lines (443 loc) · 15 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
<?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(1),
'file' => 'ding_campaign.admin.inc',
'type' => MENU_LOCAL_TASK,
'access callback' => 'ding_campaign_node_access',
'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 administration pages'),
'file' => 'ding_campaign.admin.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_admin_paths().
*/
function ding_campaign_admin_paths() {
$paths = array(
'node/*/campaign_rules' => TRUE,
);
return $paths;
}
/**
* Implements hook_node_access().
*/
function ding_campaign_node_access($node) {
if ( isset($node->type) && $node->type == 'ding_campaign') {
return TRUE;
}
return FALSE;
}
/**
* 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) {
$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.' 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');
$content = '<div class="'.$base_class.' ding-campaign-item-image"><a target="_blank" href="' . $camp_link[0]['value'] . '"><img src="' . file_create_url($camp_image[0]['uri']) . '" alt="" /></a></div>';
break;
case 'full':
$camp_text_full = field_get_items('node', $node, 'field_camp_text_full');
$content = '<div class="'.$base_class.' 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 page campaigns.
if ($context['page'] != NULL && isset($context['page']->nid)) {
$nid = $context['page']->nid;
$result = db_query('SELECT r.cid
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] += 9;
}
}
// Process path-based campaigns.
$result = db_query('SELECT r.cid, r.rule
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] += 8;
}
}
// Process search campaigns.
if ($context['search_term'] != NULL) {
$terms = trim(drupal_strtolower(check_plain($context['search_term'])));
$condition = '';
// Multiple keywords, comma and/or space separated.
if (strpos($terms, ',') || strpos($terms, ' ')) {
$terms = preg_replace('/(,?\s+)/', ',', $terms);
$terms = implode('\',\'', explode(',', $terms));
$condition = 'IN (\'' . $terms . '\')';
}
// Single keyword.
else {
$condition = ' = \'' . $context['search_term'] . '\'';
}
$result = db_query('SELECT r.cid
FROM {ding_campaign_rules} r, {ding_campaign} d, {node} n
WHERE r.type = \'rule_term\'
AND n.status = 1
AND r.cid = d.cid
AND r.cid = n.nid
AND r.rule ' . $condition . '
ORDER BY d.weight DESC')
->fetchAll();
foreach ($result as $k => $v) {
@$campaigns[$v->cid] += 7;
}
}
// Process library campaigns.
if ($context['library'] != NULL && isset($context['library']->nid) && $context['library']->type == 'ding_library' ) {
$nid = $context['library']->nid;
$result = db_query('SELECT r.cid
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] += 5;
}
}
// Process taxonomy campaigns.
if ($context['taxonomy_term'] != NULL && isset($context['taxonomy_term']->tid)) {
$tid = $context['taxonomy_term']->tid;
$result = db_query('SELECT r.cid
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] += 3;
}
}
// Process generic campaigns.
$result = db_query('SELECT r.cid
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] = (mt_rand(1, 10) / 10);
}
}
arsort($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']);
$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) {
$data = array(
'nid' => $node->nid,
'cid' => $v,
'rule' => '[nid:' . $node->nid . ']',
'type' => ($node->type == 'library') ? 'rule_library' : 'rule_page',
);
// 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) {
$data = array(
'nid' => $node->nid,
'cid' => $v,
'rule' => '[nid:' . $node->nid . ']',
'type' => ($node->type == 'library') ? 'rule_library' : 'rule_page',
);
// 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');
$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();
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;
}
}
function ding_campaign_campaign_node_form_validate($form, &$form_state) {
if ($form_state['input']['field_camp_settings'][$form['field_camp_settings']['#language']] == 'plain' || $form_state['input']['field_camp_settings'][$form['field_camp_settings']['#language']] == 'image') {
if (empty($form_state['input']['field_camp_link'][$form['field_camp_link']['#language']][0]['value'])) {
form_set_error('field_camp_link', 'Empty links are not allowed for plain text and image type campaigns.');
}
}
}