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 @ c304a780

1
<?php
2

    
3
/**
4
 * @file
5
 * This is the task handler plugin to handle generating 403, 404, 301 and 302
6
 * response codes.
7
 */
8

    
9
// Plugin definition.
10
$plugin = array(
11
  // Is a 'context' handler type, meaning it supports the API of the
12
  // context handlers provided by ctools context plugins.
13
  'handler type' => 'context',
14
  'visible' => TRUE, // may be added up front.
15

    
16
  // Administrative fields.
17
  'title' => t('HTTP response code'),
18
  'admin summary' => 'page_manager_http_response_admin_summary',
19
  'admin title' => 'page_manager_http_response_title',
20
  'operations' => array(
21
    'settings' => array(
22
      'title' => t('General'),
23
      'description' => t('Change general settings for this variant.'),
24
      'form' => 'page_manager_http_response_edit_settings',
25
    ),
26
    'criteria' => array(
27
      'title' => t('Selection rules'),
28
      'description' => t('Control the criteria used to decide whether or not this variant is used.'),
29
      'ajax' => FALSE,
30
      'form' => array(
31
        'order' => array(
32
          'form' => t('Selection rules'),
33
        ),
34
        'forms' => array(
35
          'form' => array(
36
            'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
37
            'form id' => 'ctools_context_handler_edit_criteria',
38
          ),
39
        ),
40
      ),
41
    ),
42
    'context' => array(
43
      'title' => t('Contexts'),
44
      'ajax' => FALSE,
45
      'description' => t('Add additional context objects to this variant that can be used by the content.'),
46
      'form' => array(
47
        'order' => array(
48
          'form' => t('Context'),
49
        ),
50
        'forms' => array(
51
          'form' => array(
52
            'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
53
            'form id' => 'ctools_context_handler_edit_context',
54
          ),
55
        ),
56
      ),
57
    ),
58
  ),
59

    
60
  // Callback to render the data.
61
  'render' => 'page_manager_http_response_render',
62

    
63
  'add features' => array(
64
    'criteria' => t('Selection rules'),
65
    'context' => t('Contexts'),
66
  ),
67
  // Where to go when finished.
68
  'add finish' => 'settings',
69

    
70
  'required forms' => array(
71
    'settings' => t('Panel settings'),
72
  ),
73

    
74
  'edit forms' => array(
75
    'criteria' => t('Selection rules'),
76
    'settings' => t('General'),
77
    'context' => t('Contexts'),
78
  ),
79
  'forms' => array(
80
    'settings' => array(
81
      'form id' => 'page_manager_http_response_edit_settings',
82
    ),
83
    'context' => array(
84
      'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
85
      'form id' => 'ctools_context_handler_edit_context',
86
    ),
87
    'criteria' => array(
88
      'include' => drupal_get_path('module', 'ctools') . '/includes/context-task-handler.inc',
89
      'form id' => 'ctools_context_handler_edit_criteria',
90
    ),
91
  ),
92
  'default conf' => array(
93
    'title' => t('HTTP response code'),
94
    'contexts' => array(),
95
    'relationships' => array(),
96
    'code' => '404',
97
    'destination' => '',
98
  ),
99
);
100

    
101
/**
102
 * Provide a list of the response codes we support.
103
 *
104
 * Abstracted so it can be more readily used both on input and output.
105
 */
106
function page_manager_http_response_codes() {
107
  return array(
108
    403 => t('403 Access denied'),
109
    404 => t('404 Page not found'),
110
    410 => t('410 Gone'),
111
    301 => t('301 Permanent redirect'),
112
    302 => t('302 Temporary redirect'),
113
  );
114
}
115

    
116
function page_manager_http_response_admin_summary($handler, $task, $subtask, $page, $show_title = TRUE) {
117
  $task_name = page_manager_make_task_name($task['name'], $subtask['name']);
118
  $output = '';
119

    
120
  ctools_include('context');
121
  ctools_include('context-task-handler');
122

    
123
  // Get the operations.
124
  $operations = page_manager_get_operations($page);
125

    
126
  // Get operations for just this handler.
127
  $operations = $operations['handlers']['children'][$handler->name]['children']['actions']['children'];
128
  $args = array('handlers', $handler->name, 'actions');
129
  $rendered_operations = page_manager_render_operations($page, $operations, array(), array('class' => array('actions')), 'actions', $args);
130

    
131
  $plugin = page_manager_get_task_handler($handler->handler);
132

    
133
  $object = ctools_context_handler_get_task_object($task, $subtask, $handler);
134
  $context = ctools_context_load_contexts($object, TRUE);
135

    
136
  $access = ctools_access_group_summary(!empty($handler->conf['access']) ? $handler->conf['access'] : array(), $context);
137
  if ($access) {
138
    $access = t('This panel will be selected if @conditions.', array('@conditions' => $access));
139
  }
140
  else {
141
    $access = t('This panel will always be selected.');
142
  }
143

    
144
  $rows = array();
145

    
146
  $type = $handler->type == t('Default') ? t('In code') : $handler->type;
147
  $rows[] = array(
148
    array('class' => array('page-summary-label'), 'data' => t('Storage')),
149
    array('class' => array('page-summary-data'), 'data' => $type),
150
    array('class' => array('page-summary-operation'), 'data' => ''),
151
  );
152

    
153
  if (!empty($handler->disabled)) {
154
    $link = l(t('Enable'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'actions', 'enable')));
155
    $text = t('Disabled');
156
  }
157
  else {
158
    $link = l(t('Disable'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'actions', 'disable')));
159
    $text = t('Enabled');
160
  }
161

    
162
  $rows[] = array(
163
    array('class' => array('page-summary-label'), 'data' => t('Status')),
164
    array('class' => array('page-summary-data'), 'data' => $text),
165
    array('class' => array('page-summary-operation'), 'data' => $link),
166
  );
167

    
168
  $link = l(t('Edit'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'criteria')));
169
  $rows[] = array(
170
    array('class' => array('page-summary-label'), 'data' => t('Selection rule')),
171
    array('class' => array('page-summary-data'), 'data' => $access),
172
    array('class' => array('page-summary-operation'), 'data' => $link),
173
  );
174

    
175
  $link = l(t('Edit'), page_manager_edit_url($task_name, array('handlers', $handler->name, 'settings')));
176
  $codes = page_manager_http_response_codes();
177
  $rows[] = array(
178
    array('class' => array('page-summary-label'), 'data' => t('Response code')),
179
    array('class' => array('page-summary-data'), 'data' => $codes[$handler->conf['code']]),
180
    array('class' => array('page-summary-operation'), 'data' => $link),
181
  );
182

    
183
  $info = theme('table', array('header' => array(), 'rows' => $rows, 'attributes' => array('class' => array('page-manager-handler-summary'))));
184

    
185
  $title = $handler->conf['title'];
186
  if ($title != t('Panel')) {
187
    $title = t('Panel: @title', array('@title' => $title));
188
  }
189

    
190
  $output .= '<div class="clearfix">';
191
  if ($show_title) {
192
    $output .= '<div class="handler-title clearfix">';
193
    $output .= '<div class="actions handler-actions">' . $rendered_operations['actions'] . '</div>';
194
    $output .= '<span class="title-label">' . $title . '</span>';
195
  }
196

    
197
  $output .= '</div>';
198
  $output .= $info;
199
  $output .= '</div>';
200

    
201
  return $output;
202
}
203

    
204
/**
205
 * Set up a title for the panel based upon the selection rules.
206
 */
207
function page_manager_http_response_title($handler, $task, $subtask) {
208
  if (isset($handler->conf['title'])) {
209
    return check_plain($handler->conf['title']);
210
  }
211
  else {
212
    return t('HTTP response code');
213
  }
214
}
215

    
216
/**
217
 * General settings for the panel.
218
 */
219
function page_manager_http_response_edit_settings($form, &$form_state) {
220
  ctools_include('page_manager.admin', 'page_manager', '');
221
  ctools_include('export', 'ctools');
222

    
223
  $conf = $form_state['handler']->conf;
224
  $form['title'] = array(
225
    '#type' => 'textfield',
226
    '#default_value' => $conf['title'],
227
    '#title' => t('Administrative title'),
228
    '#description' => t('Administrative title of this variant.'),
229
  );
230

    
231
  $name = isset($conf['name']) ? $conf['name'] : FALSE;
232
  $form['name'] = array(
233
    '#type' => 'machine_name',
234
    '#title' => t('Machine name'),
235
    '#required' => FALSE,
236
    '#default_value' => $name,
237
    '#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."),
238
    '#size' => 32,
239
    '#maxlength' => 32,
240
    '#machine_name' => array(
241
      'exists' => 'page_manager_handler_check_machine_name',
242
      'source' => array('title'),
243
    ),
244
    '#field_prefix' => '<span dir="ltr">' . $form_state['task_name'] . '__',
245
    '#field_suffix' => '</span>&lrm;',
246
  );
247

    
248
  $form['code'] = array(
249
    '#title' => t('Response code'),
250
    '#type' => 'select',
251
    '#options' => page_manager_http_response_codes(),
252
    '#default_value' => $conf['code'],
253
  );
254

    
255
  ctools_include('dependent');
256
  $form['destination'] = array(
257
    '#type' => 'textfield',
258
    '#title' => t('Redirect destination'),
259
    '#default_value' => $conf['destination'],
260
    '#dependency' => array('edit-code' => array(301, 302)),
261
    '#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).'),
262
  );
263

    
264
  return $form;
265
}
266

    
267
function page_manager_http_response_edit_settings_submit($form, &$form_state) {
268
  $machine_name = $form_state['handler']->name;
269
  $name = $form_state['task_name'] . '__' . $form_state['values']['name'];
270

    
271
  // If new name doesn't equal machine name, we need to update and redirect.
272
  if ($machine_name !== $name) {
273
    $form_state['handler']->name = $name;
274
    // If there's a trail, we need to replace it for redirection.
275
    if (isset($form_state['trail'])) {
276
      $form_state['new trail'] = $form_state['trail'];
277
      $delta = array_search($machine_name, $form_state['new trail']);
278
      $form_state['new trail'][$delta] = $name;
279
    }
280
    // If handler id is set, replace it.
281
    if ($form_state['handler_id']) {
282
      $form_state['handler_id'] = $name;
283
    }
284
    // If we're defining a new custom handler, move page handler to new name.
285
    if (isset($form_state['page']->handlers[$machine_name]) && isset($form_state['page']->handler_info[$machine_name])) {
286
      $form_state['page']->handlers[$name] = $form_state['page']->handlers[$machine_name];
287
      unset($form_state['page']->handlers[$machine_name]);
288
      $form_state['page']->handler_info[$name] = $form_state['page']->handler_info[$machine_name];
289
      unset($form_state['page']->handler_info[$machine_name]);
290
    }
291
  }
292

    
293
  $form_state['handler']->conf['title'] = $form_state['values']['title'];
294
  $form_state['handler']->conf['name'] = $form_state['values']['name'];
295
  $form_state['handler']->conf['code'] = $form_state['values']['code'];
296
  $form_state['handler']->conf['destination'] = $form_state['values']['destination'];
297
}
298

    
299
function page_manager_http_response_render($handler, $base_contexts, $args, $test = TRUE) {
300
  // Go through arguments and see if they match.
301
  ctools_include('context');
302
  ctools_include('context-task-handler');
303

    
304
  // Add my contexts.
305
  $contexts = ctools_context_handler_get_handler_contexts($base_contexts, $handler);
306

    
307
  // Test.
308
  if ($test && !ctools_context_handler_select($handler, $contexts)) {
309
    return;
310
  }
311

    
312
  if (isset($handler->handler)) {
313
    ctools_context_handler_pre_render($handler, $contexts, $args);
314
  }
315

    
316
  $info['response code'] = $handler->conf['code'];
317
  if ($info['response code'] == 301 || $info['response code'] == 302) {
318
    $path = ctools_context_keyword_substitute($handler->conf['destination'], array(), $contexts);
319
    $url = parse_url($path);
320
    if (isset($url['query'])) {
321
      $path = strtr($path, array('?' . $url['query'] => ''));
322
      $info['query'] = drupal_get_query_array($url['query']);
323
    }
324
    if (isset($url['fragment'])) {
325
      $path = strtr($path, array('#' . $url['fragment'] => ''));
326
      $info['fragment'] = $url['fragment'];
327
    }
328

    
329
    $info['destination'] = rtrim($path, '?');
330
  }
331

    
332
  return $info;
333
}