Projet

Général

Profil

Paste
Télécharger (11,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / ctools / page_manager / plugins / task_handlers / http_response.inc @ 651307cd

1 85ad3d82 Assos Assos
<?php
2
3
/**
4
 * @file
5
 *
6 08475715 Assos Assos
 * This is the task handler plugin to handle generating 403, 404, 301 and 302
7
 * response codes.
8 85ad3d82 Assos Assos
 */
9
10
// Plugin definition
11
$plugin = array(
12
  // is a 'context' handler type, meaning it supports the API of the
13
  // context handlers provided by ctools context plugins.
14
  'handler type' => 'context',
15
  'visible' => TRUE, // may be added up front.
16
17
  // Administrative fields.
18
  'title' => t('HTTP response code'),
19
  'admin summary' => 'page_manager_http_response_admin_summary',
20
  'admin title' => 'page_manager_http_response_title',
21
  'operations' => array(
22
    'settings' => array(
23
      'title' => t('General'),
24
      'description' => t('Change general settings for this variant.'),
25
      'form' => 'page_manager_http_response_edit_settings',
26
    ),
27
    'criteria' => array(
28
      'title' => t('Selection rules'),
29
      'description' => t('Control the criteria used to decide whether or not this variant is used.'),
30
      'ajax' => FALSE,
31
      'form' => array(
32
        'order' => array(
33
          'form' => t('Selection rules'),
34
        ),
35
        'forms' => array(
36
          'form' => array(
37
            'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
38
            'form id' => 'ctools_context_handler_edit_criteria',
39
          ),
40
        ),
41
      ),
42
    ),
43
    'context' => array(
44
      'title' => t('Contexts'),
45
      'ajax' => FALSE,
46
      'description' => t('Add additional context objects to this variant that can be used by the content.'),
47
      'form' => array(
48
        'order' => array(
49
          'form' => t('Context'),
50
        ),
51
        'forms' => array(
52
          'form' => array(
53
            'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
54
            'form id' => 'ctools_context_handler_edit_context',
55
          ),
56
        ),
57
      ),
58
    ),
59
  ),
60
61
  // Callback to render the data.
62
  'render' => 'page_manager_http_response_render',
63
64
  'add features' => array(
65
    'criteria' => t('Selection rules'),
66
    'context' => t('Contexts'),
67
  ),
68
  // Where to go when finished.
69
  'add finish' => 'settings',
70
71
  'required forms' => array(
72
    'settings' => t('Panel settings'),
73
  ),
74
75
  'edit forms' => array(
76
    'criteria' => t('Selection rules'),
77
    'settings' => t('General'),
78
    'context' => t('Contexts'),
79
  ),
80
  'forms' => array(
81
    'settings' => array(
82
      'form id' => 'page_manager_http_response_edit_settings',
83
    ),
84
    'context' => array(
85
      'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
86
      'form id' => 'ctools_context_handler_edit_context',
87
    ),
88
    'criteria' => array(
89
      'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
90
      'form id' => 'ctools_context_handler_edit_criteria',
91
    ),
92
  ),
93
  'default conf' => array(
94
    'title' => t('HTTP response code'),
95
    'contexts' => array(),
96
    'relationships' => array(),
97
    'code' => '404',
98
    'destination' => '',
99
  ),
100
);
101
102
/**
103
 * Provide a list of the response codes we support.
104
 *
105
 * Abstracted so it can be more readily used both on input and output.
106
 */
107
function page_manager_http_response_codes() {
108
  return array(
109
    403 => t('403 Access denied'),
110
    404 => t('404 Page not found'),
111
    410 => t('410 Gone'),
112 08475715 Assos Assos
    301 => t('301 Permanent redirect'),
113
    302 => t('302 Temporary redirect'),
114 85ad3d82 Assos Assos
  );
115
}
116
117
function page_manager_http_response_admin_summary($handler, $task, $subtask, $page, $show_title = TRUE) {
118
  $task_name = page_manager_make_task_name($task['name'], $subtask['name']);
119
  $output = '';
120
121
  ctools_include('context');
122
  ctools_include('context-task-handler');
123
124
  // Get the operations
125
  $operations = page_manager_get_operations($page);
126
127
  // Get operations for just this handler.
128
  $operations = $operations['handlers']['children'][$handler->name]['children']['actions']['children'];
129
  $args = array('handlers', $handler->name, 'actions');
130
  $rendered_operations = page_manager_render_operations($page, $operations, array(), array('class' => array('actions')), 'actions', $args);
131
132
  $plugin = page_manager_get_task_handler($handler->handler);
133
134
  $object = ctools_context_handler_get_task_object($task, $subtask, $handler);
135
  $context = ctools_context_load_contexts($object, TRUE);
136
137
  $access = ctools_access_group_summary(!empty($handler->conf['access']) ? $handler->conf['access'] : array(), $context);
138
  if ($access) {
139
    $access = t('This panel will be selected if @conditions.', array('@conditions' => $access));
140
  }
141
  else {
142
    $access = t('This panel will always be selected.');
143
  }
144
145
  $rows = array();
146
147
  $type = $handler->type == t('Default') ? t('In code') : $handler->type;
148
  $rows[] = array(
149
    array('class' => array('page-summary-label'), 'data' => t('Storage')),
150
    array('class' => array('page-summary-data'), 'data' => $type),
151
    array('class' => array('page-summary-operation'), 'data' => ''),
152
  );
153
154
  if (!empty($handler->disabled)) {
155
    $link = l(t('Enable'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'actions', 'enable')));
156
    $text = t('Disabled');
157
  }
158
  else {
159
    $link = l(t('Disable'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'actions', 'disable')));
160
    $text = t('Enabled');
161
  }
162
163
  $rows[] = array(
164
    array('class' => array('page-summary-label'), 'data' => t('Status')),
165
    array('class' => array('page-summary-data'), 'data' => $text),
166
    array('class' => array('page-summary-operation'), 'data' => $link),
167
  );
168
169
  $link = l(t('Edit'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'criteria')));
170
  $rows[] = array(
171
    array('class' => array('page-summary-label'), 'data' => t('Selection rule')),
172
    array('class' => array('page-summary-data'), 'data' => $access),
173
    array('class' => array('page-summary-operation'), 'data' => $link),
174
  );
175
176
  $link = l(t('Edit'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'settings')));
177
  $codes = page_manager_http_response_codes();
178
  $rows[] = array(
179
    array('class' => array('page-summary-label'), 'data' => t('Response code')),
180
    array('class' => array('page-summary-data'), 'data' => $codes[$handler->conf['code']]),
181
    array('class' => array('page-summary-operation'), 'data' => $link),
182
  );
183
184
  $info = theme('table', array('header' => array(), 'rows' => $rows, 'attributes' => array('class' => array('page-manager-handler-summary'))));
185
186
  $title = $handler->conf['title'];
187
  if ($title != t('Panel')) {
188
    $title = t('Panel: @title', array('@title' => $title));
189
  }
190
191
  $output .= '<div class="clearfix">';
192
  if ($show_title) {
193
  $output .= '<div class="handler-title clearfix">';
194
    $output .= '<div class="actions handler-actions">' . $rendered_operations['actions'] . '</div>';
195
    $output .= '<span class="title-label">' . $title . '</span>';
196
  }
197
198
  $output .= '</div>';
199
  $output .= $info;
200
  $output .= '</div>';
201
202
  return $output;
203
}
204
205
/**
206
 * Set up a title for the panel based upon the selection rules.
207
 */
208
function page_manager_http_response_title($handler, $task, $subtask) {
209
  if (isset($handler->conf['title'])) {
210
    return check_plain($handler->conf['title']);
211
  }
212
  else {
213
    return t('HTTP response code');
214
  }
215
}
216
217
/**
218
 * General settings for the panel
219
 */
220
function page_manager_http_response_edit_settings($form, &$form_state) {
221 c22e192e Assos Assos
  ctools_include('page_manager.admin', 'page_manager', '');
222
  ctools_include('export', 'ctools');
223
224 85ad3d82 Assos Assos
  $conf = $form_state['handler']->conf;
225
  $form['title'] = array(
226
    '#type' => 'textfield',
227
    '#default_value' => $conf['title'],
228
    '#title' => t('Administrative title'),
229
    '#description' => t('Administrative title of this variant.'),
230
  );
231
232 e4c061ad Assos Assos
  $name = isset($conf['name']) ? $conf['name'] : FALSE;
233
  $form['name'] = array(
234
    '#type' => 'machine_name',
235
    '#title' => t('Machine name'),
236
    '#required' => FALSE,
237
    '#default_value' => $name,
238
    '#description' => t("A unique machine-readable name for this variant. It must only contain lowercase letters, numbers, and underscores. This name will be used when exporting the variant. If left empty the variant's name will be used instead."),
239
    '#size' => 32,
240
    '#maxlength' => 32,
241
    '#machine_name' => array(
242
      'exists' => 'page_manager_handler_check_machine_name',
243
      'source' => array('title'),
244
    ),
245
    '#field_prefix' => '<span dir="ltr">' . $form_state['task_name'] . '__',
246
    '#field_suffix' => '</span>&lrm;',
247
  );
248
249 85ad3d82 Assos Assos
  $form['code'] = array(
250
    '#title' => t('Response code'),
251
    '#type' => 'select',
252
    '#options' => page_manager_http_response_codes(),
253
    '#default_value' => $conf['code'],
254
  );
255
256
  ctools_include('dependent');
257
  $form['destination'] = array(
258
    '#type' => 'textfield',
259
    '#title' => t('Redirect destination'),
260
    '#default_value' => $conf['destination'],
261 08475715 Assos Assos
    '#dependency' => array('edit-code' => array(301, 302)),
262 85ad3d82 Assos Assos
    '#description' => t('Enter the path to redirect to. You may use keyword substitutions from contexts. You can use external urls (http://www.example.com/foo) or internal urls (node/1).'),
263
  );
264
265
  return $form;
266
}
267
268
function page_manager_http_response_edit_settings_submit($form, &$form_state) {
269 e4c061ad Assos Assos
  $machine_name = $form_state['handler']->name;
270 c22e192e Assos Assos
  $name = $form_state['task_name'] . '__' . $form_state['values']['name'];
271 e4c061ad Assos Assos
272
  // If new name doesn't equal machine name, we need to update and redirect.
273
  if ($machine_name !== $name) {
274
    $form_state['handler']->name = $name;
275 c22e192e Assos Assos
    // If there's a trail, we need to replace it for redirection.
276
    if (isset($form_state['trail'])) {
277
      $form_state['new trail'] = $form_state['trail'];
278
      $delta = array_search($machine_name, $form_state['new trail']);
279
      $form_state['new trail'][$delta] = $name;
280
    }
281
    // If handler id is set, replace it.
282
    if ($form_state['handler_id']) {
283
      $form_state['handler_id'] = $name;
284
    }
285
    // If we're defining a new custom handler, move page handler to new name.
286
    if (isset($form_state['page']->handlers[$machine_name]) && isset($form_state['page']->handler_info[$machine_name])) {
287
      $form_state['page']->handlers[$name] = $form_state['page']->handlers[$machine_name];
288
      unset($form_state['page']->handlers[$machine_name]);
289
      $form_state['page']->handler_info[$name] = $form_state['page']->handler_info[$machine_name];
290
      unset($form_state['page']->handler_info[$machine_name]);
291
    }
292 e4c061ad Assos Assos
  }
293
294 85ad3d82 Assos Assos
  $form_state['handler']->conf['title'] = $form_state['values']['title'];
295 e4c061ad Assos Assos
  $form_state['handler']->conf['name'] = $form_state['values']['name'];
296 85ad3d82 Assos Assos
  $form_state['handler']->conf['code'] = $form_state['values']['code'];
297
  $form_state['handler']->conf['destination'] = $form_state['values']['destination'];
298
}
299
300
function page_manager_http_response_render($handler, $base_contexts, $args, $test = TRUE) {
301
  // Go through arguments and see if they match.
302
  ctools_include('context');
303
  ctools_include('context-task-handler');
304
305
  // Add my contexts
306
  $contexts = ctools_context_handler_get_handler_contexts($base_contexts, $handler);
307
308
  // Test.
309
  if ($test && !ctools_context_handler_select($handler, $contexts)) {
310
    return;
311
  }
312
313
  if (isset($handler->handler)) {
314
    ctools_context_handler_pre_render($handler, $contexts, $args);
315
  }
316
317
  $info['response code'] = $handler->conf['code'];
318 08475715 Assos Assos
  if ($info['response code'] == 301 || $info['response code'] == 302) {
319 85ad3d82 Assos Assos
    $path = ctools_context_keyword_substitute($handler->conf['destination'], array(), $contexts);
320
    $url = parse_url($path);
321
    if (isset($url['query'])) {
322
      $path = strtr($path, array('?' . $url['query'] => ''));
323
      $info['query'] = drupal_get_query_array($url['query']);
324
    }
325
    if (isset($url['fragment'])) {
326
      $path = strtr($path, array('#' . $url['fragment'] => ''));
327
      $info['fragment'] = $url['fragment'];
328
    }
329
330
    $info['destination'] = rtrim($path, '?');
331
  }
332
333
  return $info;
334
}