context-task-handler.inc 18 KB

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