forked from dingproject/ding-campaign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ding_campaign.module
591 lines (529 loc) · 19.7 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
<?php
/**
* @file ding_campaign.module
* Provides the campaign node type for the Ding! project.
*/
// Default number of campaigns to display.
define('DING_CAMPAIGN_DEFAULT_COUNT', 3);
/**
* Implementation of hook_menu().
*/
function ding_campaign_menu() {
$items = array();
$items['node/%node/campaign_rules'] = array(
'title' => 'Display rules',
'page callback' => 'ding_campaign_admin_rule_page',
'page arguments' => array(1),
'access callback' => 'ding_campaign_access',
'access arguments' => array(1),
'file' => 'ding_campaign.admin.inc',
'type' => MENU_LOCAL_TASK,
'options' => array('admin' => TRUE),
'weight' => 2,
);
$items['node/%node/campaign_rules/ahah'] = array(
'title' => 'Display rules',
'page callback' => 'drupal_get_form',
'page arguments' => array('ding_campaign_admin_rule_form_ahah', 1),
'access callback' => 'ding_campaign_access',
'access arguments' => array(1),
'file' => 'ding_campaign.admin.inc',
'type' => MENU_CALLBACK,
);
$items['ding_campaign/autocomplete/%'] = array(
'title' => 'Campaign rules autocomplete callback',
'page callback' => 'ding_campaign_admin_autocomplete',
'page arguments' => array(2),
'access callback' => 'node_content_access',
'access arguments' => array('create', 'campaign', NULL),
'file' => 'ding_campaign.admin.inc',
'type' => MENU_CALLBACK,
);
$items['admin/settings/ding_campaign'] = array(
'title' => 'Ding! Campaign',
'page callback' => 'drupal_get_form',
'page arguments' => array('ding_campaign_admin_settings_form'),
'access arguments' => array('administer site configuration'),
'file' => 'ding_campaign.admin.inc',
);
return $items;
}
/**
* Menu access callback.
*/
function ding_campaign_access($node) {
if ($node->type == 'campaign') {
return node_content_access('update', $node, NULL);
}
return FALSE;
}
/**
* Implementation of hook_perm().
*/
function ding_campaign_perm() {
return array(
'associate other content to campaign',
);
}
/**
* Implementation of hook_nodeapi().
*
* When a node revision is deleted, we need to remove the corresponding record
* from our table. The only way to handle revision deletion is by implementing
* hook_nodeapi().
*/
function ding_campaign_nodeapi(&$node, $op, $teaser, $page) {
switch ($op) {
case 'load':
if ($node->type == 'campaign') {
$additions = db_fetch_array(db_query('SELECT * FROM {ding_campaign} WHERE vid = %d;', $node->vid));
unset($additions['vid']);
unset($additions['nid']);
// Also fetch associated rules.
$additions['campaign_rules'] = array();
$rules_query = db_query("SELECT * FROM {ding_campaign_rule} WHERE nid = %d;", $node->nid);
while ($rule = db_fetch_array($rules_query)) {
$additions['campaign_rules'][$rule['delta']] = $rule;
}
return $additions;
}
else {
// Get the current campaign association for the node.
if (in_array($node->type, variable_get('ding_campaign_selector_node_types', array()))) {
$query = db_query("SELECT nid FROM {ding_campaign_rule} WHERE type = 'page' AND value_id = %d;", $node->nid);
$node->ding_campaigns = array();
while ($campaign_nid = db_result($query)) {
$node->ding_campaigns[$campaign_nid] = $campaign_nid;
}
}
}
break;
case 'update':
if ($node->type == 'campaign') {
// Check if there's an existing row in {ding_campaign}
$prev_vid = db_result(db_query("SELECT vid FROM {ding_campaign} WHERE nid = %d", $node->nid));
// If this is a new node or we're adding a new revision,
if ($node->revision || !$prev_vid) {
drupal_write_record('ding_campaign', $node);
}
else {
drupal_write_record('ding_campaign', $node, array('nid', 'vid'));
}
db_query('UPDATE {ding_campaign_rule} SET status = %d WHERE nid = %d', array(
':status' => $node->status,
':nid' => $node->nid,
));
}
else {
// When updating a node, purge the current campaign selections first.
if (in_array($node->type, variable_get('ding_campaign_selector_node_types', array())) && user_access('associate other content to campaign')) {
db_query("DELETE FROM {ding_campaign_rule} WHERE type = 'page' AND value_id = %d;", $node->nid);
}
}
break;
case 'insert':
if ($node->type == 'campaign') {
drupal_write_record('ding_campaign', $node);
}
else {
if (!isset($node->ding_campaigns) || !is_array($node->ding_campaigns)) {
return;
}
$campaigns = array_filter($node->ding_campaigns);
if (!empty($campaigns) && in_array($node->type, variable_get('ding_campaign_selector_node_types', array())) && user_access('associate other content to campaign')) {
$cols = array();
$params = array();
$now = $_SERVER['REQUEST_TIME'];
foreach ($campaigns as $campaign_nid) {
// We use $now as delta to be reasonably sure not to have
// delta collisions within the same nid, since nid+delta is
// the primary key for ding_campaign_rule.
$cols[] = "(%d, 1, $now, 'page', '%s', %d)";
$params[] = $campaign_nid;
$params[] = $node->title . ' [nid:' . $node->nid . ']';
$params[] = $node->nid;
}
db_query('INSERT INTO {ding_campaign_rule} (nid, status, delta, type, value, value_id) VALUES ' . implode(',', $cols), $params);
}
}
break;
case 'delete':
// Notice that we're matching all revision, by using the node's nid.
db_query('DELETE FROM {ding_campaign} WHERE nid = %d;', $node->nid);
db_query('DELETE FROM {ding_campaign_rule} WHERE nid = %d;', $node->nid);
break;
case 'delete revision':
// Notice that we're matching a single revision based on the node's vid.
db_query('DELETE FROM {ding_campaign} WHERE vid = %d;', $node->vid);
break;
}
}
/**
* Implementation of hook_elements().
*/
function ding_campaign_elements() {
$types = array();
$types['ding_campaign_rule'] = array(
'#input' => TRUE,
'#process' => array('ding_campaign_rule_element_process'),
'#element_validate' => array('ding_campaign_rule_element_validate'),
'#default_value' => array(
'nid' => NULL,
'delta' => 0,
'type' => 'taxonomy',
'value' => '',
),
);
return $types;
}
/**
* Implementation of hook_theme().
*
* This lets us tell Drupal about our theme functions and their arguments.
*/
function ding_campaign_theme() {
return array(
'ding_campaign_rule' => array(
'arguments' => array('element' => NULL),
),
'ding_campaign_relevant_campaigns' => array(
'context' => array('context' => NULL, 'max_count' => NULL, 'offset' => NULL),
),
);
}
/**
* Implementation of hook_form_alter().
*/
function ding_campaign_form_alter(&$form, $form_state, $form_id) {
if ($form['#id'] == 'node-form') {
if ($form['type']['#value'] == 'campaign') {
$node = $form['#node'];
// We have a bit of JavaScript to help with the interface for this
// module's editing interface.
drupal_add_js(drupal_get_path('module', 'ding_campaign') . '/js/ding_campaign.edit.js');
// For this node type, we don't want splittable teasers, so let's
// disable that.
unset($form['body_field']['teaser_js']);
unset($form['body_field']['teaser_include']);
$form['campaign_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Campaign settings'),
'#weight' => -3,
);
$form['campaign_settings']['campaign_type'] = array(
'#type' => 'radios',
'#title' => t('Campaign type'),
'#options' => array(
'text-only' => t('Text only'),
'image-only' => t('Image only'),
'wysiwyg-title' => t('WYSIWYG with title'),
),
'#attributes' => array('class' => 'campaign-type-select'),
'#default_value' => (isset($node->campaign_type)) ? $node->campaign_type : 'text-only',
);
$form['campaign_settings']['campaign_theme'] = array(
'#type' => 'radios',
'#title' => t('Campaign theme'),
'#options' => variable_get('ding_campaign_theme_choices', ding_campaign_default_theme_choices()),
'#default_value' => (isset($node->campaign_theme)) ? $node->campaign_theme : '',
);
// Make sure we have a valid default value. Select the first option,
// if the default value is not a valid option.
if (!empty($form['campaign_settings']['campaign_theme']['#options']) && !isset($form['campaign_settings']['campaign_theme']['#options'][$form['campaign_settings']['campaign_theme']['#default_value']])) {
$keys = array_keys($form['campaign_settings']['campaign_theme']['#options']);
$form['campaign_settings']['campaign_theme']['#default_value'] = $keys[0];
}
$form['campaign_settings']['campaign_weight'] = array(
'#type' => 'weight',
'#title' => t('Weight'),
'#default_value' => (isset($node->campaign_weight)) ? $node->campaign_weight : 0,
);
}
if (in_array($form['#node']->type, variable_get('ding_campaign_selector_node_types', array())) && user_access('associate other content to campaign')) {
// For node types that have the campaign selector enabled.
$form['ding_campaign_select'] = array(
'#type' => 'fieldset',
'#title' => t('Campaigns'),
'#description' => t('Select the campaigns that should be displayed when this node is displayed as a page.'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['ding_campaign_select']['ding_campaigns'] = array(
'#type' => 'checkboxes',
'#options' => ding_campaign_load_option_list(),
'#attributes' => array('class' => 'ding-campaign-select'),
'#default_value' => (isset($form['#node']->ding_campaigns)) ? $form['#node']->ding_campaigns : array(),
);
drupal_add_css(drupal_get_path('module', 'ding_campaign') . '/css/ding_campaign.select.css');
}
}
}
/**
* Implementation of hook_ctools_plugin_dierctory().
*/
function ding_campaign_ctools_plugin_directory($module, $plugin) {
if ($module == 'ctools' && $plugin == 'content_types') {
return 'plugins/' . $plugin;
}
}
/**
* Implementation of hook_admin_theme().
*/
function ding_campaign_admin_theme($op = 'info', $option = NULL) {
switch ($op) {
case 'info':
$options = array();
$options['ding_campaign_rules'] = array(
'title' => t('Campaign rules'),
'description' => t('Use the administration theme when campaign display rules.'),
'default' => TRUE,
);
return $options;
break;
case 'check':
switch ($option) {
case 'ding_campaign_rules':
return (arg(0) == 'node' && arg(2) == 'campaign_rules');
}
break;
}
}
/**
* Process callback to expand our form element into several fields.
*/
function ding_campaign_rule_element_process($element, $form_state) {
$element['#tree'] = TRUE;
if (!isset($element['#value'])) {
$element['#value'] = $element['#default_value'];
}
else {
$element['#value'] += $element['#default_value'];
}
$element['nid'] = array(
'#type' => 'value',
'#value' => $element['#nid'],
);
$element['delta'] = array(
'#type' => 'value',
'#value' => (isset($element['#delta'])) ? $element['#delta'] : $element['#value']['delta'],
);
// Generate a list of options based on the available campagin types
$options = ding_campaign_campaign_types();
foreach($options as $id => $type) {
$options[$id] = $type['title'];
}
$element['type'] = array(
'#type' => 'select',
'#title' => t('Type'),
'#options' => $options,
'#default_value' => $element['#value']['type'],
);
// If the Ding Library module is present, add an extra option.
if (function_exists('ding_library_menu')) {
$element['type']['#options']['library'] = t('Library');
}
$element['value'] = array(
'#type' => 'textfield',
'#title' => t('Value'),
'#default_value' => $element['#value']['value'],
// Set up autocomplete pointing to a dummy path that just returns an
// empty array. We set the correct path via JavaScript.
'#autocomplete_path' => 'ding_campaign/autocomplete/dummy/empty',
'#size' => 60,
);
// Generate som help based on the available campagin types
$help = '';
foreach (ding_campaign_campaign_types() as $id => $type) {
$help .= '<dt class="' . $id . '">' . t($type['title']) . '</dt><dd class="' . $id . '">' . t($type['description']) . '</dd>';
}
$element['help'] = array(
'#type' => 'markup',
'#value' => '<dl>'.$help.'</dl>',
);
return $element;
}
/**
* Our element's validation function.
*/
function ding_campaign_rule_element_validate($element, &$form_state) {
return $form;
}
/**
* Get relevant campaigns.
*
* @param array $context
* Array of context items – search_term, library, page and taxonomy.
* @param integer $max_count
* The maximum number of campaigns to return.
* @param integer $offset
* The index of the campaign from which to count when returning campaigns.
* @return array
* Array of node ids of relevant campaigns, sorted by relevancy.
*/
function ding_campaign_get_relevant($context, $max_count = -1, $offset = 0) {
// Make sure we have a positive number before we proceed.
$max_count = (integer) $max_count;
if ($max_count < 1) {
$max_count = DING_CAMPAIGN_DEFAULT_COUNT;
}
$campaigns = array();
if (isset($context['page']) && !empty($context['page'])) {
if ($context['page'] instanceof stdClass) {
$nid = $context['page']->nid;
}
else {
$nid = (int) $context['page'];
}
$query = db_query("SELECT nid FROM {ding_campaign_rule} WHERE type = 'page' AND status = 1 AND value_id = %d;", $nid);
while ($camp_nid = db_result($query)) {
$campaigns[$camp_nid] += 9;
}
}
// Get all path-based campaign rules.
$query = db_query("SELECT * FROM {ding_campaign_rule} WHERE type = 'path' AND status = 1;");
while ($row = db_fetch_array($query)) {
$path = drupal_get_path_alias($_GET['q']);
// Compare with the internal and path alias (if any).
$page_match = drupal_match_path($path, $row['value']);
if ($path != $_GET['q']) {
$page_match = $page_match || drupal_match_path($_GET['q'], $row['value']);
}
if ($page_match) {
$campaigns[$row['nid']] += 8;
}
}
if (isset($context['search_term']) && !empty($context['search_term'])) {
// If there's more than one search term, make a SQL IN condition by
// splitting them and joining them with commas.
$keys = trim(drupal_strtolower(check_plain($context['search_term'])));
if (strpos($keys, ' ')) {
$keys = implode("','", array_merge(array($keys), explode(' ', $keys)));
$condition = "IN ('" . $keys . "')";
}
else {
$condition = "= '" . $keys . "'";
}
$query = db_query("SELECT nid FROM {ding_campaign_rule} WHERE type = 'search_term' AND status = 1 AND value " . $condition);
while ($camp_nid = db_result($query)) {
$campaigns[$camp_nid] += 7;
}
}
if (isset($context['library']) && !empty($context['library'])) {
if ($context['library'] instanceof stdClass) {
$nid = $context['library']->nid;
}
else {
$nid = (int) $context['library'];
}
$query = db_query("SELECT nid FROM {ding_campaign_rule} WHERE type = 'library' AND status = 1 AND value_id = %d;", $nid);
while ($camp_nid = db_result($query)) {
$campaigns[$camp_nid] += 5;
}
}
if (isset($context['taxonomy']) && !empty($context['taxonomy'])) {
if ($context['taxonomy'] instanceof stdClass) {
$tid = $context['taxonomy']->tid;
}
else {
$tid = (int) $context['taxonomy'];
}
$query = db_query("SELECT nid FROM {ding_campaign_rule} WHERE type = 'taxonomy' AND status = 1 AND value_id = %d;", $tid);
while ($camp_nid = db_result($query)) {
$campaigns[$camp_nid] += 3;
}
}
// Give a small (and random) amount of relevancy to generic campaigns
// that have not scored in any of the other categories.
$query = db_query("SELECT nid FROM {ding_campaign_rule} WHERE type = 'generic' AND status = 1");
while ($camp_nid = db_result($query)) {
if (!isset($campaigns[$camp_nid]) || $campaigns[$camp_nid] == 0) {
$campaigns[$camp_nid] = (mt_rand(1, 10) / 10);
}
}
arsort($campaigns);
return array_slice(array_keys($campaigns), $offset, $max_count);
}
/**
* Theme function to format the our custom form element.
*
* We use the container-inline class so that all three of the HTML elements
* are placed next to each other, rather than on separate lines.
*/
function theme_ding_campaign_rule($element) {
return theme('form_element', $element, '<div class="campaign-rule-wrap container-inline">'. $element['#children'] .'</div>');
}
/**
* Theme function to get and render relevant campaigns based on context.
*
* @param array $context
* The context array passed to ding_campaign_get_relevant()
* @param integer $max_count
* The maximum number of campaigns to return.
* @param integer $offset
* The index of the campaign from which to count when returning campaigns.
* @return string
* Rendered campaign nodes, or FALSE if no match was found.
* @see ding_campaign_get_relevant().
*/
function theme_ding_campaign_relevant_campaigns($context, $max_count = DING_CAMPAIGN_DEFAULT_COUNT, $offset = 0) {
$campaigns = ding_campaign_get_relevant($context, $max_count, $offset);
$output = '';
foreach ($campaigns as $nid) {
if ($nid > 0) {
$node = node_load($nid);
if ($node && $node->status) {
if($node->campaign_type == 'wysiwyg-title') {
$rendered_node = node_view($node, FALSE, TRUE, TRUE);
}
else {
$rendered_node = node_view($node, TRUE, FALSE, FALSE);
}
$output .= $rendered_node;
}
}
}
if (!empty($output)) {
return $output;
}
return FALSE;
}
/**
* Load list of available campaigns as an option list.
*/
function ding_campaign_load_option_list() {
$options = array();
$query = db_query("SELECT nid, title FROM node WHERE type = 'campaign' AND status <> 0 ORDER BY title;");
while ($row = db_fetch_array($query)) {
$options[$row['nid']] = $row['title'];
}
return $options;
}
/**
* Provide an array of default campaign theme choices.
*/
function ding_campaign_default_theme_choices() {
return array(
'red' => t('Red'),
'blue' => t('Blue'),
'green' => t('Green'),
);
}
/**
* Returns an array of campaign types.
*/
function ding_campaign_campaign_types() {
$types = array(
'page' => array('title' => t('Specific page'),
'description' => t('Adds relevance to the campaign if shown with a specific node.')),
'path' => array('title' => t('Path'),
'description' => t('Adds relevance to the campaign if the current path or path alias matches. Do not include the leading /. Use * as wildcard. Example: blog/* matches all blog posts.')),
'search_term' => array('title' => t('Search term'),
'description' => t('Adds relevance to the campaign on search result pages for the selected term.')),
'taxonomy' => array('title' => t('Taxonomy term'),
'description' => t('Adds relevance to the campaign if shown with a node containing the selected term.')),
'generic' => array('title' => t('Generic'),
'description' => t('Gives a small bit of rank on all pages, so that the campaign will show up if there is not enough relevant campaigns.')),
);
return $types;
}
require_once('ding_campaign.features.inc');