http_response.inc 12 KB

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