context-task-handler.inc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. <?php
  2. /**
  3. * @file
  4. * Support for creating 'context' type task handlers.
  5. *
  6. * Context task handlers expect the task to provide 0 or more contexts. The
  7. * task handler should use those contexts as selection rules, as well as
  8. * rendering with them.
  9. *
  10. * The functions and forms in this file should be common to every context type
  11. * task handler made.
  12. *
  13. * Forms:
  14. * - ...
  15. */
  16. /**
  17. * Render a context type task handler given a list of handlers
  18. * attached to a type.
  19. *
  20. * @param $task
  21. * The $task object in use.
  22. * @param $subtask
  23. * The id of the subtask in use.
  24. * @param $contexts
  25. * The context objects in use.
  26. * @param $args
  27. * The raw arguments behind the contexts.
  28. * @param $page
  29. * If TRUE then this renderer owns the page and can use theme('page')
  30. * for no blocks; if false, output is returned regardless of any no
  31. * blocks settings.
  32. * @return
  33. * Either the output or NULL if there was output, FALSE if no handler
  34. * accepted the task. If $page is FALSE then the $info block is returned instead.
  35. */
  36. function ctools_context_handler_render($task, $subtask, $contexts, $args) {
  37. // Load the landlers, choosing only enabled handlers.
  38. $handlers = page_manager_load_sorted_handlers($task, $subtask ? $subtask['name'] : '', TRUE);
  39. $id = ctools_context_handler_get_render_handler($task, $subtask, $handlers, $contexts, $args);
  40. if ($id) {
  41. return ctools_context_handler_render_handler($task, $subtask, $handlers[$id], $contexts, $args);
  42. }
  43. return FALSE;
  44. }
  45. /**
  46. * Figure out which of the listed handlers should be used to render.
  47. */
  48. function ctools_context_handler_get_render_handler($task, $subtask, $handlers, $contexts, $args) {
  49. // Try each handler.
  50. foreach ($handlers as $id => $handler) {
  51. $plugin = page_manager_get_task_handler($handler->handler);
  52. // First, see if the handler has a tester.
  53. $function = ctools_plugin_get_function($plugin, 'test');
  54. if ($function) {
  55. $test = $function($handler, $contexts, $args);
  56. if ($test) {
  57. return $id;
  58. }
  59. }
  60. else {
  61. // If not, if it's a 'context' type handler, use the default tester.
  62. if ($plugin['handler type'] == 'context') {
  63. $test = ctools_context_handler_default_test($handler, $contexts, $args);
  64. if ($test) {
  65. return $id;
  66. }
  67. }
  68. }
  69. }
  70. return FALSE;
  71. }
  72. /**
  73. * Default test function to see if a task handler should be rendered.
  74. *
  75. * This tests against the standard selection criteria that most task
  76. * handlers should be implementing.
  77. */
  78. function ctools_context_handler_default_test($handler, $base_contexts, $args) {
  79. ctools_include('context');
  80. // Add my contexts
  81. $contexts = ctools_context_handler_get_handler_contexts($base_contexts, $handler);
  82. // Test.
  83. return ctools_context_handler_select($handler, $contexts);
  84. }
  85. /**
  86. * Render a task handler.
  87. */
  88. function ctools_context_handler_render_handler($task, $subtask, $handler, $contexts, $args, $page = TRUE) {
  89. $function = page_manager_get_renderer($handler);
  90. if (!$function) {
  91. return NULL;
  92. }
  93. if ($page) {
  94. if ($subtask) {
  95. $task_name = page_manager_make_task_name($task['name'], $subtask['name']);
  96. }
  97. else {
  98. $task_name = $task['name'];
  99. }
  100. page_manager_get_current_page(array(
  101. 'name' => $task_name,
  102. 'task' => $task,
  103. 'subtask' => $subtask,
  104. 'contexts' => $contexts,
  105. 'arguments' => $args,
  106. 'handler' => $handler,
  107. ));
  108. }
  109. $info = $function($handler, $contexts, $args);
  110. if (!$info) {
  111. return NULL;
  112. }
  113. $context = array(
  114. 'args' => $args,
  115. 'contexts' => $contexts,
  116. 'task' => $task,
  117. 'subtask' => $subtask,
  118. 'handler' => $handler
  119. );
  120. drupal_alter('ctools_render', $info, $page, $context);
  121. // If we don't own the page, let the caller deal with rendering.
  122. if (!$page) {
  123. return $info;
  124. }
  125. if (!empty($info['response code']) && $info['response code'] != 200) {
  126. switch ($info['response code']) {
  127. case 403:
  128. return MENU_ACCESS_DENIED;
  129. case 404:
  130. return MENU_NOT_FOUND;
  131. case 410:
  132. drupal_add_http_header('Status', '410 Gone');
  133. drupal_exit();
  134. break;
  135. case 301:
  136. case 302:
  137. case 303:
  138. case 304:
  139. case 305:
  140. case 307:
  141. $info += array(
  142. 'query' => array(),
  143. 'fragment' => '',
  144. );
  145. $options = array(
  146. 'query' => $info['query'],
  147. 'fragment' => $info['fragment'],
  148. );
  149. return drupal_goto($info['destination'], $options, $info['response code']);
  150. // @todo -- should other response codes be supported here?
  151. }
  152. }
  153. $plugin = page_manager_get_task_handler($handler->handler);
  154. if (module_exists('contextual') && user_access('access contextual links') && isset($handler->task)) {
  155. // Provide a contextual link to edit this, if we can:
  156. $callback = isset($plugin['contextual link']) ? $plugin['contextual link'] : 'ctools_task_handler_default_contextual_link';
  157. if ($callback && function_exists($callback)) {
  158. $links = $callback($handler, $plugin, $contexts, $args);
  159. }
  160. if (!empty($links) && is_array($links)) {
  161. $build = array(
  162. '#theme_wrappers' => array('container'),
  163. '#attributes' => array('class' => array('contextual-links-region')),
  164. );
  165. if (!is_array($info['content'])) {
  166. $build['content']['#markup'] = $info['content'];
  167. }
  168. else {
  169. $build['content'] = $info['content'];
  170. }
  171. $build['contextual_links'] = array(
  172. '#prefix' => '<div class="contextual-links-wrapper">',
  173. '#suffix' => '</div>',
  174. '#theme' => 'links__contextual',
  175. '#links' => $links,
  176. '#attributes' => array('class' => array('contextual-links')),
  177. '#attached' => array(
  178. 'library' => array(array('contextual', 'contextual-links')),
  179. ),
  180. );
  181. $info['content'] = $build;
  182. }
  183. }
  184. foreach (ctools_context_handler_get_task_arguments($task, $subtask) as $id => $argument) {
  185. $plugin = ctools_get_argument($argument['name']);
  186. $cid = ctools_context_id($argument, 'argument');
  187. if (!empty($contexts[$cid]) && ($function = ctools_plugin_get_function($plugin, 'breadcrumb'))) {
  188. $function($argument['settings'], $contexts[$cid]);
  189. }
  190. }
  191. if (isset($info['title'])) {
  192. drupal_set_title($info['title'], PASS_THROUGH);
  193. }
  194. // Only directly output if $page was set to true.
  195. if (!empty($info['no_blocks'])) {
  196. ctools_set_no_blocks(FALSE);
  197. }
  198. return $info['content'];
  199. }
  200. /**
  201. * Default function to provide contextual link for a task as defined by the handler.
  202. *
  203. * This provides a simple link to th main content operation and is suitable
  204. * for most normal handlers. Setting 'contextual link' to a function overrides
  205. * this and setting it to FALSE will prevent a contextual link from appearing.
  206. */
  207. function ctools_task_handler_default_contextual_link($handler, $plugin, $contexts, $args) {
  208. if (!user_access('administer page manager')) {
  209. return;
  210. }
  211. $task = page_manager_get_task($handler->task);
  212. $title = !empty($task['tab title']) ? $task['tab title'] : t('Edit @type', array('@type' => $plugin['title']));
  213. $trail = array();
  214. if (!empty($plugin['tab operation'])) {
  215. if (is_array($plugin['tab operation'])) {
  216. $trail = $plugin['tab operation'];
  217. }
  218. else if (function_exists($plugin['tab operation'])) {
  219. $trail = $plugin['tab operation']($handler, $contexts, $args);
  220. }
  221. }
  222. $path = page_manager_edit_url(page_manager_make_task_name($handler->task, $handler->subtask), $trail);
  223. $links = array(array(
  224. 'href' => $path,
  225. 'title' => $title,
  226. 'query' => drupal_get_destination(),
  227. ));
  228. return $links;
  229. }
  230. /**
  231. * Called to execute actions that should happen before a handler is rendered.
  232. */
  233. function ctools_context_handler_pre_render($handler, $contexts, $args) { }
  234. /**
  235. * Compare arguments to contexts for selection purposes.
  236. *
  237. * @param $handler
  238. * The handler in question.
  239. * @param $contexts
  240. * The context objects provided by the task.
  241. *
  242. * @return
  243. * TRUE if these contexts match the selection rules. NULL or FALSE
  244. * otherwise.
  245. */
  246. function ctools_context_handler_select($handler, $contexts) {
  247. if (empty($handler->conf['access'])) {
  248. return TRUE;
  249. }
  250. ctools_include('context');
  251. return ctools_access($handler->conf['access'], $contexts);
  252. }
  253. /**
  254. * Get the array of summary strings for the arguments.
  255. *
  256. * These summary strings are used to communicate to the user what
  257. * arguments the task handlers are selecting.
  258. *
  259. * @param $task
  260. * The loaded task plugin.
  261. * @param $subtask
  262. * The subtask id.
  263. * @param $handler
  264. * The handler to be checked.
  265. */
  266. function ctools_context_handler_summary($task, $subtask, $handler) {
  267. if (empty($handler->conf['access']['plugins'])) {
  268. return array();
  269. }
  270. ctools_include('context');
  271. $strings = array();
  272. $contexts = ctools_context_handler_get_all_contexts($task, $subtask, $handler);
  273. foreach ($handler->conf['access']['plugins'] as $test) {
  274. $plugin = ctools_get_access_plugin($test['name']);
  275. if ($string = ctools_access_summary($plugin, $contexts, $test)) {
  276. $strings[] = $string;
  277. }
  278. }
  279. return $strings;
  280. }
  281. // --------------------------------------------------------------------------
  282. // Tasks and Task handlers can both have their own sources of contexts.
  283. // Sometimes we need all of these contexts at once (when editing
  284. // the task handler, for example) but sometimes we need them separately
  285. // (when a task has contexts loaded and is trying out the task handlers,
  286. // for example). Therefore there are two paths we can take to getting contexts.
  287. /**
  288. * Load the contexts for a task, using arguments.
  289. *
  290. * This creates the base array of contexts, loaded from arguments, suitable
  291. * for use in rendering.
  292. */
  293. function ctools_context_handler_get_task_contexts($task, $subtask, $args) {
  294. $contexts = ctools_context_handler_get_base_contexts($task, $subtask);
  295. $arguments = ctools_context_handler_get_task_arguments($task, $subtask);
  296. ctools_context_get_context_from_arguments($arguments, $contexts, $args);
  297. return $contexts;
  298. }
  299. /**
  300. * Load the contexts for a task handler.
  301. *
  302. * This expands a base set of contexts passed in from a task with the
  303. * contexts defined on the task handler. The contexts from the task
  304. * must already have been loaded.
  305. */
  306. function ctools_context_handler_get_handler_contexts($contexts, $handler) {
  307. $object = ctools_context_handler_get_handler_object($handler);
  308. return ctools_context_load_contexts($object, FALSE, $contexts);
  309. }
  310. /**
  311. * Load the contexts for a task and task handler together.
  312. *
  313. * This pulls the arguments from a task and everything else from a task
  314. * handler and loads them as a group. Since there is no data, this loads
  315. * the contexts as placeholders.
  316. */
  317. function ctools_context_handler_get_all_contexts($task, $subtask, $handler) {
  318. $contexts = array();
  319. $object = ctools_context_handler_get_task_object($task, $subtask, $handler);
  320. $contexts = ctools_context_load_contexts($object, TRUE, $contexts);
  321. ctools_context_handler_set_access_restrictions($task, $subtask, $handler, $contexts);
  322. return $contexts;
  323. }
  324. /**
  325. * Create an object suitable for use with the context system that kind of
  326. * expects things in a certain, kind of clunky format.
  327. */
  328. function ctools_context_handler_get_handler_object($handler) {
  329. $object = new stdClass;
  330. $object->name = $handler->name;
  331. $object->contexts = isset($handler->conf['contexts']) ? $handler->conf['contexts'] : array();
  332. $object->relationships = isset($handler->conf['relationships']) ? $handler->conf['relationships'] : array();
  333. return $object;
  334. }
  335. /**
  336. * Create an object suitable for use with the context system that kind of
  337. * expects things in a certain, kind of clunky format. This one adds in
  338. * arguments from the task.
  339. */
  340. function ctools_context_handler_get_task_object($task, $subtask, $handler) {
  341. $object = new stdClass;
  342. $object->name = !empty($handler->name) ? $handler->name : 'temp';
  343. $object->base_contexts = ctools_context_handler_get_base_contexts($task, $subtask, TRUE);
  344. $object->arguments = ctools_context_handler_get_task_arguments($task, $subtask);
  345. $object->contexts = isset($handler->conf['contexts']) ? $handler->conf['contexts'] : array();
  346. $object->relationships = isset($handler->conf['relationships']) ? $handler->conf['relationships'] : array();
  347. return $object;
  348. }
  349. /**
  350. * Get base contexts from a task, if it has any.
  351. *
  352. * Tasks can get their contexts either from base contexts or arguments; base
  353. * contexts extract their information from the environment.
  354. */
  355. function ctools_context_handler_get_base_contexts($task, $subtask, $placeholders = FALSE) {
  356. if ($function = ctools_plugin_get_function($task, 'get base contexts')) {
  357. return $function($task, $subtask, $placeholders);
  358. }
  359. return array();
  360. }
  361. /**
  362. * Get the arguments from a task that are used to load contexts.
  363. */
  364. function ctools_context_handler_get_task_arguments($task, $subtask) {
  365. if ($function = ctools_plugin_get_function($task, 'get arguments')) {
  366. return $function($task, $subtask);
  367. }
  368. return array();
  369. }
  370. /**
  371. * Set any access restrictions on the contexts for a handler.
  372. *
  373. * Both the task and the handler could add restrictions to the contexts
  374. * based upon the access control. These restrictions might be useful
  375. * to limit what kind of content appears in the add content dialog;
  376. * for example, if we have an access item that limits a node context
  377. * to only 'story' and 'page' types, there is no need for content that
  378. * only applies to the 'poll' type to appear.
  379. */
  380. function ctools_context_handler_set_access_restrictions($task, $subtask, $handler, &$contexts) {
  381. // First, for the task:
  382. if ($function = ctools_plugin_get_function($task, 'access restrictions')) {
  383. $function($task, $subtask, $contexts);
  384. }
  385. // Then for the handler:
  386. if (isset($handler->conf['access'])) {
  387. ctools_access_add_restrictions($handler->conf['access'], $contexts);
  388. }
  389. }
  390. /**
  391. * Form to choose context based selection rules for a task handler.
  392. *
  393. * The configuration will be assumed to go simply in $handler->conf and
  394. * will be keyed by the argument ID.
  395. */
  396. function ctools_context_handler_edit_criteria($form, &$form_state) {
  397. if (!isset($form_state['handler']->conf['access'])) {
  398. $form_state['handler']->conf['access'] = array();
  399. }
  400. ctools_include('context');
  401. ctools_include('modal');
  402. ctools_include('ajax');
  403. ctools_modal_add_plugin_js(ctools_get_access_plugins());
  404. ctools_include('context-access-admin');
  405. $form_state['module'] = (isset($form_state['module'])) ? $form_state['module'] : 'page_manager_task_handler';
  406. // Encode a bunch of info into the argument so we can get our cache later
  407. $form_state['callback argument'] = $form_state['task_name'] . '*' . $form_state['handler']->name;
  408. $form_state['access'] = $form_state['handler']->conf['access'];
  409. $form_state['no buttons'] = TRUE;
  410. $form_state['contexts'] = ctools_context_handler_get_all_contexts($form_state['task'], $form_state['subtask'], $form_state['handler']);
  411. $form['markup'] = array(
  412. '#markup' => '<div class="description">' .
  413. t('If there is more than one variant on a page, when the page is visited each variant is given an opportunity to be displayed. Starting from the first variant and working to the last, each one tests to see if its selection rules will pass. The first variant that meets its criteria (as specified below) will be used.') .
  414. '</div>',
  415. );
  416. $form = ctools_access_admin_form($form, $form_state);
  417. return $form;
  418. }
  419. /**
  420. * Submit handler for rules selection
  421. */
  422. function ctools_context_handler_edit_criteria_submit(&$form, &$form_state) {
  423. $form_state['handler']->conf['access']['logic'] = $form_state['values']['logic'];
  424. }
  425. /**
  426. * Edit contexts that go with this panel.
  427. */
  428. function ctools_context_handler_edit_context($form, &$form_state) {
  429. ctools_include('context-admin');
  430. ctools_context_admin_includes();
  431. $handler = $form_state['handler'];
  432. $page = $form_state['page'];
  433. $cache_name = $handler->name ? $handler->name : 'temp';
  434. if (isset($page->context_cache[$cache_name])) {
  435. $cache = $page->context_cache[$cache_name];
  436. }
  437. else {
  438. $cache = ctools_context_handler_get_task_object($form_state['task'], $form_state['subtask'], $form_state['handler']);
  439. $form_state['page']->context_cache[$cache_name] = $cache;
  440. }
  441. $form['right'] = array(
  442. '#prefix' => '<div class="clearfix"><div class="right-container">',
  443. '#suffix' => '</div>',
  444. );
  445. $form['left'] = array(
  446. '#prefix' => '<div class="left-container">',
  447. '#suffix' => '</div></div>',
  448. );
  449. $module = 'page_manager_context::' . $page->task_name;
  450. ctools_context_add_context_form($module, $form, $form_state, $form['right']['contexts_table'], $cache);
  451. ctools_context_add_relationship_form($module, $form, $form_state, $form['right']['relationships_table'], $cache);
  452. $theme_vars = array();
  453. $theme_vars['object'] = $cache;
  454. $theme_vars['header'] = t('Summary of contexts');
  455. $form['left']['summary'] = array(
  456. '#prefix' => '<div class="page-manager-contexts">',
  457. '#suffix' => '</div>',
  458. '#markup' => theme('ctools_context_list', $theme_vars),
  459. );
  460. $form_state['context_object'] = &$cache;
  461. return $form;
  462. }
  463. /**
  464. * Process submission of the context edit form.
  465. */
  466. function ctools_context_handler_edit_context_submit(&$form, &$form_state) {
  467. $handler = &$form_state['handler'];
  468. $cache_name = $handler->name ? $handler->name : 'temp';
  469. $handler->conf['contexts'] = $form_state['context_object']->contexts;
  470. $handler->conf['relationships'] = $form_state['context_object']->relationships;
  471. if (isset($form_state['page']->context_cache[$cache_name])) {
  472. unset($form_state['page']->context_cache[$cache_name]);
  473. }
  474. }