/[drupal]/contributions/modules/connect/connect_admin.php
ViewVC logotype

Contents of /contributions/modules/connect/connect_admin.php

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations) (download) (as text)
Fri Apr 3 19:09:52 2009 UTC (7 months, 3 weeks ago) by stevem
Branch: MAIN
CVS Tags: DRUPAL-5--2-0-BETA1, HEAD
Changes since 1.2: +17 -10 lines
File MIME type: text/x-php
improved requirements checking; fix email success confirmation
1 <?php
2 // $Id$
3
4 /**** FORM: select campaign functions ****/
5
6 function connect_node_functions_form() {
7 drupal_add_css(drupal_get_path('module', 'connect') .'/connect.css');
8 $child = array();
9 $options = array();
10 $p_nid = arg(1);
11 $parent = node_load($p_nid);
12
13 // determine possible and enabled actions
14 $requirements_OK = TRUE;
15 $actions = connect_get_actions($p_nid);
16 $action_list = connect_call_hooks($parent, $child, 'describe', 'parent');
17 unset($action_list['connect_action_basic']);
18 foreach ($action_list as $function=>$action) {
19 $status = '';
20 if (in_array($function, $actions)) {
21 $this_OK = _connect_hook_check_requirements($parent, $child, $function, 'parent');
22 if ($this_OK === TRUE) {
23 $status = ' ' . theme_image(drupal_get_path('module', 'connect') . '/images/accept.png', '(ACTIVE)', 'This function is active.');
24 }
25 else {
26 $requirements_OK = FALSE;
27 $status = ' ' . theme_image(drupal_get_path('module', 'connect') . '/images/exclamation.png', '(INACTIVE)', 'This function is not active. Please check the settings tab.');
28 }
29 }
30 $options[$function] = $action['title'] . $status . '<p class="connect-comment">' . $action['desc'] . '</p>';
31 }
32 if ($requirements_OK !== TRUE) {
33 drupal_set_message('One or more of your selected functions requires additional settings to be configured.<br />Please check the settings tab for details.' , 'error');
34 }
35
36 $form = array();
37 $form['parent_id'] = array(
38 '#type' => 'value',
39 '#value' => $p_nid,
40 );
41 $form['connect_actions'] = array(
42 '#type' => 'fieldset',
43 '#title' => 'Choose functions to apply to this campaign',
44 );
45 $form['connect_actions']['actions'] = array(
46 '#type' => 'checkboxes',
47 '#title' => '',
48 '#options' => $options,
49 '#default_value' => $actions,
50 );
51 $form['submit'] = array(
52 '#type' => 'submit',
53 '#value' => t('Submit'),
54 );
55 return $form;
56 }
57
58 function connect_node_functions_form_submit($form_id, $form_values) {
59 // save action options
60 $options = array();
61 foreach( $form_values['actions'] as $key=>$value ) {
62 if ("$key" == "$value") {
63 $options[] = $value;
64 }
65 }
66
67 // restore mandatory action
68 $options[] = 'connect_action_basic';
69 connect_node_options( $form_values['parent_id'], 'connect_actions', $options );
70
71 drupal_set_message('Your selected functions have been updated.');
72 }
73
74
75 /**** FORM: function settings ****/
76
77 function connect_node_settings_form() {
78 drupal_add_css(drupal_get_path('module', 'connect') .'/connect.css');
79 $child = NULL;
80 $p_nid = arg(1);
81 $parent = node_load($p_nid);
82 $form = array();
83
84 // test for requirements
85 $requirements_message = '';
86 $actions = connect_get_actions($p_nid);
87 $action_list = connect_call_hooks($parent, $child, 'describe', 'parent');
88 unset($action_list['connect_action_basic']);
89 foreach ($action_list as $function => $action) {
90 if (in_array($function, $actions)) {
91 $this_OK = _connect_hook_check_requirements($parent, $child, $function, 'parent');
92 if ($this_OK !== TRUE) $requirements_message .= $this_OK;
93 }
94 }
95 if (!empty($requirements_message)) {
96 $requirements_message = 'The following items need to to be configured for your actions to work properly:<br />'. $requirements_message;
97 drupal_set_message($requirements_message);
98 }
99
100 // store the parent nid
101 $form['parent_id'] = array(
102 '#type' => 'value',
103 '#value' => $p_nid,
104 );
105
106 // required variables
107 $map = connect_get_map($p_nid);
108 $required = connect_get_required_vars($parent, $child);
109 if (!empty($required['variables'])) {
110 $form['campaign_variables'] = array('#tree' => TRUE);
111 foreach ( $required['variables'] as $action=>$vars ) {
112 $description = $action($parent, $child, 'describe');
113 $form['campaign_variables']["variables_$action"] = array(
114 '#type' => 'fieldset',
115 '#title' => $description['title'],
116 );
117 foreach( $vars as $key=>$formitem ) {
118 $form['campaign_variables']["variables_$action"][$key] = $formitem;
119 }
120 if ($action == 'connect_action_basic') {
121 $form['campaign_variables']["variables_$action"]['#weight'] = -1;
122 }
123 }
124 }
125
126 // parent node fields
127 if ( !empty($required['parent']) ) {
128 $options = connect_get_node_fields($parent->type);
129 $form['variables_parent'] = array(
130 '#type' => 'fieldset',
131 '#title' => 'Required fields in parent/campaign node',
132 '#tree' => TRUE,
133 );
134 $form['variables_parent']['message'] = array(
135 '#value' => '<em>'. t('The selected functions store or use information from the campaign/parent node. Please identify which fields in the parent node correspond to the function settings below.') .'</em>',
136 );
137 foreach ( $required['parent'] as $key=>$desc ) {
138 $form['variables_parent'][$key] = array(
139 '#type' => 'select',
140 '#title' => $desc,
141 '#options' => $options,
142 '#default_value' => isset($map[$key]) ? $map[$key]: '',
143 '#required' => TRUE,
144 );
145 }
146 }
147
148 // child node fields
149 if (connect_node_options($parent->nid, 'participant_type')) {
150 if (!empty($required['child'])) {
151 $form['variables_child'] = array(
152 '#type' => 'fieldset',
153 '#title' => 'Required fields in child/participant node',
154 '#tree' => TRUE,
155 );
156 $form['variables_child']['message'] = array(
157 '#value' => '<em>'. t('The selected functions store or use information from the participant/child node. Please identify which fields in the child node correspond to the function settings below.') .'</em>',
158 );
159 $child_type = connect_node_options( $parent->nid, 'participant_type' );
160 $options = connect_get_node_fields($child_type);
161 foreach ( $required['child'] as $key=>$desc ) {
162 $form['variables_child'][$key] = array(
163 '#type' => 'select',
164 '#title' => $desc,
165 '#options' => $options,
166 '#default_value' => isset($map[$key]) ? $map[$key]: '',
167 '#required' => TRUE,
168 );
169 }
170 }
171 }
172 else {
173 $form['variables_child']['message'] = array(
174 '#value' => t('Please select a participant type and return here to set up your variables.'),
175 );
176 }
177
178 $form['submit'] = array(
179 '#type' => 'submit',
180 '#value' => t('Submit'),
181 );
182
183 return $form;
184 }
185
186
187 function connect_node_settings_form_validate($form_id, $form_values) {
188 $child = NULL;
189 $parent->nid = $form_values['parent_id']; // required by connect_call_hooks
190 $parent->data = $form_values;
191 connect_call_hooks($parent, $child, 'admin-validate', 'parent');
192 }
193
194 // handle nested arrays
195 function _connect_set_values_from_form($item, $parent_id) {
196 foreach ($item as $key=>$value) {
197 if (is_array($value)) {
198 _connect_set_values_from_form($value, $parent_id);
199 }
200 else {
201 connect_node_options($parent_id, $key, $value);
202 }
203 }
204 }
205
206 function connect_node_settings_form_submit($form_id, $form_values) {
207 // save overall settings
208 foreach ($form_values['campaign_variables'] as $function=>$vars) {
209 _connect_set_values_from_form($vars, $form_values['parent_id']);
210 }
211
212 // save variable->field mapping
213 $options = array();
214 foreach ( array('variables_parent','variables_child') as $formitem) {
215 if (isset($form_values[$formitem])) {
216 foreach ($form_values[$formitem] as $key=>$value) {
217 if ( !empty($value) ) {
218 $options[$key] = $value;
219 }
220 }
221 }
222 }
223 connect_node_options( $form_values['parent_id'], 'connect_map', $options );
224
225 $null = drupal_get_messages(NULL); // clear bogus _connect_hook_check_requirements errors
226 drupal_set_message('The campaign configuration has been updated.');
227 }
228
229 /**** FORM: connect module administration ****/
230
231 function connect_admin_form() {
232 // grab all node types, format for use in form
233 $sql = "SELECT type FROM {node_type};";
234 $result = db_query($sql);
235 while ($row = db_fetch_object($result)) {
236 $type_options[$row->type] = $row->type;
237 }
238
239 // remove some standard node types
240 $remove = array('blog', 'forum', 'page', 'story');
241 $type_options = array_diff($type_options, $remove);
242
243 $form = array();
244 if (!empty($type_options)) {
245 // participant node types
246 $form['connect_participant_nodes'] = array(
247 '#type' => 'checkboxes',
248 '#title' => t('Which node types hold participant/child information?'),
249 '#options' => $type_options,
250 '#required' => TRUE,
251 '#default_value' => variable_get('connect_participant_nodes', array()),
252 );
253
254 // parent node types
255 $form['connect_parent_nodes'] = array(
256 '#type' => 'checkboxes',
257 '#title' => t('Which node types can be used as campaign/parent nodes?'),
258 '#options' => $type_options,
259 '#required' => TRUE,
260 '#default_value' => variable_get('connect_parent_nodes', array()),
261 );
262
263 // captcha
264 $form['connect_captcha_required'] = array(
265 '#type' => 'radios',
266 '#title' => t('Should connect require a CAPTCHA?'),
267 '#options' => array( 'yes' => 'Yes', 'no' => 'No' ),
268 '#default_value' => variable_get('connect_captcha_required', 'yes'),
269 );
270
271 // cache settings
272 $form['connect_cache'] = array(
273 '#value' => 'Define the timeout value for your cached data using the format "999 X", where "999" is an integer and "X" is one of mhd, for minutes, hours, days. Leave blank, or set the number to zero (0) to set an unlimited cache lifetime.',
274 );
275 require_once(drupal_get_path('module','connect') . '/connect_lookup.php');
276 $cache_names = _connect_get_cache_names();
277 foreach ($cache_names as $key=>$title) {
278 $form['connect_cache']["connect_cache_$key"] = array(
279 '#type' => 'textfield',
280 '#title' => $title,
281 '#size' => 10,
282 '#default_value' => variable_get("connect_cache_$key", ''),
283 );
284 }
285
286 $form['submit'] = array(
287 '#type' => 'submit',
288 '#value' => t('Submit'),
289 );
290 }
291 else {
292 $form['message'] = array(
293 '#value' => t('No node types have been defined yet. Please set up some content types and return here to set up Connect.'),
294 );
295 }
296 return $form;
297 }
298
299 function connect_admin_form_validate($form_id, $form_values) {
300 // parent node type cannot be a child node type as well
301 $test = array_intersect_assoc($form_values['connect_participant_nodes'], $form_values['connect_parent_nodes']);
302 foreach ($test as $key=>$val) {
303 if (empty($val)) {
304 unset($test[$key]);
305 }
306 }
307 if (!empty($test)) {
308 form_set_error('', 'A node type cannot be both a parent and a child node.');
309 }
310
311 // validate cache timeouts
312 require_once(drupal_get_path('module','connect') . '/connect_lookup.php');
313 $cache_names = _connect_get_cache_names();
314 $regex = '/^[0-9]{1,}\ [mhd]$/';
315 foreach ($cache_names as $key=>$title) {
316 if (isset($form_values["connect_cache_$key"]) && !preg_match($regex,$form_values["connect_cache_$key"])) {
317 form_set_error("connect_cache_$key", 'Please specify cache lifetime in the "999 X" format.');
318 }
319 }
320 }
321
322 function connect_admin_form_submit($form_id, $form_values) {
323 // save participant node info
324 $data = array();
325 foreach ($form_values['connect_participant_nodes'] as $key=>$value) {
326 if ($key === $value) {
327 $data[] = $value;
328 }
329 }
330 if (! empty($data)) {
331 variable_set('connect_participant_nodes', $data);
332 }
333
334 // save parent node info
335 $data = array();
336 foreach ($form_values['connect_parent_nodes'] as $key=>$value) {
337 if ($key === $value) {
338 $data[] = $value;
339 }
340 }
341 if (! empty($data)) {
342 variable_set('connect_parent_nodes', $data);
343 }
344 // captcha
345 $captcha = $form_values['connect_captcha_required'] == 'no' ? 'no' : 'yes';
346 variable_set('connect_captcha_required', $captcha);
347
348 // cache timeouts
349 require_once(drupal_get_path('module','connect') . '/connect_lookup.php');
350 $cache_names = _connect_get_cache_names();
351 foreach ($cache_names as $key=>$title) {
352 $interval = empty($form_values["connect_cache_$key"]) ? 0 : $form_values["connect_cache_$key"];
353 variable_set("connect_cache_$key", $interval);
354 }
355 drupal_set_message(t('The connect settings have been updated.'));
356 }
357
358

  ViewVC Help
Powered by ViewVC 1.1.2