forked from dingproject/ding-campaign
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ding_campaign.admin.inc
279 lines (247 loc) · 9.45 KB
/
ding_campaign.admin.inc
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
<?php
/**
* Settings form.
*
* Enables the user to choose which node types the quick campaign
* selector should be added to.
*/
function ding_campaign_admin_settings_form(&$form_state) {
$form = array();
$form['ding_campaign_selector_node_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Campaign selection node types'),
'#description' => t('Select the node types that should have a campaign selector present on their editing form. This selector will enable the user to associate campaigns to his content without needing to edit the campaign.'),
'#options' => node_get_types('names'),
'#default_value' => variable_get('ding_campaign_selector_node_types', array()),
);
// Don't allow campaigns to be assigned to other campaigns.
unset($form['ding_campaign_selector_node_types']['#options']['campaign']);
// Process the default value for our theme choices.
$choices = variable_get('ding_campaign_theme_choices', ding_campaign_default_theme_choices());
$text_choices = '';
foreach ($choices as $class => $name) {
$text_choices .= $class . '|' . $name . "\n";
}
$form['ding_campaign_theme_choices'] = array(
'#type' => 'textarea',
'#title' => t('Campaign themes'),
'#description' => t('Provide a list of themes available when creating or editing campaigns. Provide a list of pipe-delimited in the form of "css-class|Long descriptive name", like "forest|Forest green with trees", one on each line.'),
'#required' => TRUE,
'#default_value' => $text_choices,
);
return system_settings_form($form);
}
/**
* Validation for our systems settings form.
*/
function ding_campaign_admin_settings_form_validate($form, &$form_state) {
// Filter out the empty values from the node types and get the type
// names of the enabled types, and store these as the value.
$form_state['values']['ding_campaign_selector_node_types'] = array_keys(array_filter($form_state['values']['ding_campaign_selector_node_types']));
$choices = array();
foreach (preg_split('/\n/', $form_state['values']['ding_campaign_theme_choices']) as $line) {
if (strpos($line, '|')) {
list($class, $name) = explode('|', $line);
$choices[$class] = $name;
}
}
if (empty($choices)) {
form_set_error('ding_campaign_theme_choices', t('Please define one or more theme choices.'));
}
$form_state['values']['ding_campaign_theme_choices'] = $choices;
}
/**
* Rules editing page callback.
*
* Simple wrapper for the form, only required to add the JavaScript.
*/
function ding_campaign_admin_rule_page($node) {
// Use the admin module's theme if available.
if (function_exists('_admin_init_theme')) {
_admin_init_theme();
}
drupal_add_js(drupal_get_path('module', 'ding_campaign') . '/js/ding_campaign.rules.js');
drupal_add_js(array('dingCampaignRules' => array(
'autocompleteUrl' => url('ding_campaign/autocomplete/'),
)), 'setting');
return drupal_get_form('ding_campaign_admin_rule_form', $node);
}
/**
* Admin interface for campaign display rules.
*/
function ding_campaign_admin_rule_form($form_state, $node) {
$form = array();
$form['nid'] = array(
'#type' => 'value',
'#value' => $node->nid,
);
$form['rules'] = array(
'#type' => 'fieldset',
'#title' => t('Rules'),
'#description' => t('The more rules that match on a page, the higher rank the campaign will have.'),
'#attributes' => array('id' => 'campaign-rules'),
'#tree' => TRUE,
);
if (isset($node->campaign_rules) && !empty($node->campaign_rules)) {
foreach ($node->campaign_rules as $delta => $rule) {
$form['rules'][$delta] = array(
'#type' => 'ding_campaign_rule',
'#value' => $rule,
'#nid' => $node->nid,
);
}
}
else {
$form['rules'][0] = array(
'#type' => 'ding_campaign_rule',
'#nid' => $node->nid,
);
}
$form['new_rule_button'] = array(
'#type' => 'button',
'#value' => 'Add another',
'#ahah' => array(
'path' => 'node/' . $node->nid . '/campaign_rules/ahah',
'wrapper' => 'campaign-rules',
'method' => 'append',
'effect' => 'slide'
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save changes'),
);
return $form;
}
/**
* Validation for campaign display rules form.
*/
function ding_campaign_admin_rule_form_validate($form, &$form_state) {
foreach ($form_state['values']['rules'] as $delta => $rule) {
$value = trim($rule['value']);
if ($rule['type'] == 'generic') {
// Generic fields have no value, only their type.
$form_state['values']['rules'][$delta]['value'] = NULL;
}
elseif (empty($value)) {
// Remove the empty rules, so they won't get saved to the database.
unset($form_state['values']['rules'][$delta]);
}
elseif ($rule['type'] == 'search_term') {
// Force lowercase terms (we do the same when searching,
// so searching is effectively case-insensitive).
$form_state['values']['rules'][$delta]['value'] = drupal_strtolower($value);
}
elseif ($rule['type'] == 'page' || $rule['type'] == 'library' || $rule['type'] == 'taxonomy') {
$matches = array();
// Match [nid:54], [tid:4], etc. from the text string.
if (preg_match('/\[[tn]id:(\d+)\]/', $value, $matches)) {
// Store the matched ID.
$form_state['values']['rules'][$delta]['value_id'] = $matches[1];
}
elseif (!is_numeric($value) || $value < 0) {
form_error($form['rules'][$delta], t('%value is not a valid value for the selected type (%type).', array('%value' => $rule['value'], '%type' => $rule['type'])));
}
}
}
}
/**
* Submit handler for campaign display rules form.
*/
function ding_campaign_admin_rule_form_submit($form, &$form_state) {
// First, remove the existing rules
db_query("DELETE FROM {ding_campaign_rule} WHERE nid = %d", $form_state['values']['nid']);
// Then write the new set of rules
// Use array_values to make sure that we have no missing numbers in the
// delta, and use the new key to overwrite the delta setting on the rule.
foreach (array_values($form_state['values']['rules']) as $delta => $rule) {
$rule['delta'] = $delta;
$rule['status'] = 1;
drupal_write_record('ding_campaign_rule', $rule);
}
}
/*
This function is largely based on the poll module, its been simplified for reuse.
$fields is the specific form elements you want to attach via ahah,
$name is the form fields array key... e.g. the name for $form['title'] is "title"
*/
function ding_campaign_admin_rule_form_ahah($form_state, $node) {
// Use the admin module's theme if available.
if (function_exists('_admin_init_theme')) {
_admin_init_theme();
}
$form_state = array('submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
// Add the new element to the stored form. Without adding the element to the
// form, Drupal is not aware of this new elements existence and will not
// process it. We retreive the cached form, add the element, and resave.
$form = form_get_cache($form_build_id, $form_state);
// Delta is assigned to the count of current elements, so it'll be the
// next in the zero-based list.
$delta = count(element_children($form['rules']));
$form['rules'][$delta] = array(
'#type' => 'ding_campaign_rule',
'#delta' => $delta,
'#nid' => $node->nid,
);
form_set_cache($form_build_id, $form, $form_state);
$form += array(
'#post' => $_POST,
'#programmed' => FALSE,
);
// Rebuild the form.
$form = form_builder($_POST['form_id'], $form, $form_state);
// Render and return the new field.
drupal_json(array('data' => drupal_render($form['rules'][$delta]), 'status' => TRUE));
exit();
}
/**
* Autocomplete callback for rule values.
*
* @param string $type
* The type of thing we're searching for.
* @param string $term
* The search term.
*/
function ding_campaign_admin_autocomplete($type, $term) {
$matches = array();
if (!empty($term)) {
switch ($type) {
case 'page':
$query = db_query("SELECT nid, title FROM node WHERE title LIKE('%%%s%%') LIMIT 5;", $term);
while ($row = db_fetch_array($query)) {
$value = _ding_campaign_strip_title($row['title']) . ' [nid:' . $row['nid'] . ']';
$matches[$value] = $value;
}
break;
case 'library':
$query = db_query("SELECT nid, title FROM node WHERE type = 'library' AND title LIKE('%%%s%%') LIMIT 5;", $term);
while ($row = db_fetch_array($query)) {
$value = _ding_campaign_strip_title($row['title']) . ' [nid:' . $row['nid'] . ']';
$matches[$value] = $value;
}
break;
case 'taxonomy':
$query = db_query("SELECT tid, name FROM term_data WHERE name LIKE('%%%s%%') LIMIT 5;", $term);
while ($row = db_fetch_array($query)) {
$value = _ding_campaign_strip_title($row['name']) . ' [tid:' . $row['tid'] . ']';
$matches[$value] = $value;
}
break;
case 'campaign':
$query = db_query("SELECT nid, title FROM node WHERE type = 'campaign' AND title LIKE('%%%s%%') LIMIT 5;", $term);
while ($row = db_fetch_array($query)) {
$value = _ding_campaign_strip_title($row['title']) . ' [nid:' . $row['nid'] . ']';
$matches[$value] = $value;
}
break;
}
}
drupal_json($matches);
}
/**
* Helper function to strip unhealthy characters form titles.
*/
function _ding_campaign_strip_title($title) {
return str_replace(array('"', "'", ',', '[', ']', '&', '<', '>'), '', check_plain($title));
}