Projet

Général

Profil

Paste
Télécharger (10,6 ko) Statistiques
| Branche: | Révision:

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

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
  $conf = $form_state['handler']->conf;
220
  $form['title'] = array(
221
    '#type' => 'textfield',
222
    '#default_value' => $conf['title'],
223
    '#title' => t('Administrative title'),
224
    '#description' => t('Administrative title of this variant.'),
225
  );
226

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

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

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

    
260
  return $form;
261
}
262

    
263
function page_manager_http_response_edit_settings_submit($form, &$form_state) {
264
  $machine_name = $form_state['handler']->name;
265
  $name = $form_state['task_id'] . '__' . $form_state['values']['name'];
266

    
267
  // If new name doesn't equal machine name, we need to update and redirect.
268
  if ($machine_name !== $name) {
269
    $form_state['new trail'] = $form_state['trail'];
270
    $delta = array_search($machine_name, $form_state['new trail']);
271
    $form_state['new trail'][$delta] = $name;
272
    $form_state['handler']->name = $name;
273
  }
274

    
275
  $form_state['handler']->conf['title'] = $form_state['values']['title'];
276
  $form_state['handler']->conf['name'] = $form_state['values']['name'];
277
  $form_state['handler']->conf['code'] = $form_state['values']['code'];
278
  $form_state['handler']->conf['destination'] = $form_state['values']['destination'];
279
}
280

    
281
function page_manager_http_response_render($handler, $base_contexts, $args, $test = TRUE) {
282
  // Go through arguments and see if they match.
283
  ctools_include('context');
284
  ctools_include('context-task-handler');
285

    
286
  // Add my contexts
287
  $contexts = ctools_context_handler_get_handler_contexts($base_contexts, $handler);
288

    
289
  // Test.
290
  if ($test && !ctools_context_handler_select($handler, $contexts)) {
291
    return;
292
  }
293

    
294
  if (isset($handler->handler)) {
295
    ctools_context_handler_pre_render($handler, $contexts, $args);
296
  }
297

    
298
  $info['response code'] = $handler->conf['code'];
299
  if ($info['response code'] == 301) {
300
    $path = ctools_context_keyword_substitute($handler->conf['destination'], array(), $contexts);
301
    $url = parse_url($path);
302
    if (isset($url['query'])) {
303
      $path = strtr($path, array('?' . $url['query'] => ''));
304
      $info['query'] = drupal_get_query_array($url['query']);
305
    }
306
    if (isset($url['fragment'])) {
307
      $path = strtr($path, array('#' . $url['fragment'] => ''));
308
      $info['fragment'] = $url['fragment'];
309
    }
310

    
311
    $info['destination'] = rtrim($path, '?');
312
  }
313

    
314
  return $info;
315
}