Projet

Général

Profil

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

root / drupal7 / sites / all / modules / ctools / page_manager / plugins / task_handlers / http_response.inc @ 2c8c2b87

1
<?php
2

    
3
/**
4
 * @file
5
 *
6
 * This is the task handler plugin to handle generating 403, 404 and 301 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 Redirect'),
112
  );
113
}
114

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

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

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

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

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

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

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

    
143
  $rows = array();
144

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

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

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

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

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

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

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

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

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

    
200
  return $output;
201
}
202

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

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

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

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

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

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

    
263
  return $form;
264
}
265

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

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

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

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

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

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

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

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

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

    
331
  return $info;
332
}