plugins.inc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. <?php
  2. /**
  3. * @file
  4. * Contains helper code for plugins and contexts.
  5. */
  6. /**
  7. * Determine if a pane is visible.
  8. *
  9. * @param $pane
  10. * The pane object to test for access.
  11. * @param $display
  12. * The display object containing the pane object to be tested.
  13. */
  14. function panels_pane_access($pane, $display) {
  15. ctools_include('context');
  16. return ctools_access($pane->access, $display->context);
  17. }
  18. /**
  19. * Get a list of panels available in the layout.
  20. */
  21. function panels_get_regions($layout, $display) {
  22. if ($function = ctools_plugin_get_function($layout, 'regions function')) {
  23. return $function($display, $display->layout_settings, $layout);
  24. }
  25. if (!empty($layout['regions'])) {
  26. return $layout['regions'];
  27. }
  28. return array();
  29. }
  30. /**
  31. * Get cached content for a given display and possibly pane.
  32. *
  33. * @return
  34. * The cached content, or FALSE to indicate no cached content exists.
  35. */
  36. function panels_get_cached_content($display, $args, $context, $pane = NULL) {
  37. // Never use cache on a POST.
  38. if (!empty($_POST)) {
  39. return FALSE;
  40. }
  41. $method = $pane ? $pane->cache['method'] : $display->cache['method'];
  42. $function = panels_plugin_get_function('cache', $method, 'cache get');
  43. if (!$function) {
  44. return FALSE;
  45. }
  46. $conf = $pane ? $pane->cache['settings'] : $display->cache['settings'];
  47. $cache = $function($conf, $display, $args, $context, $pane);
  48. if (empty($cache)) {
  49. return FALSE;
  50. }
  51. // Restore it.
  52. $cache->restore();
  53. return $cache;
  54. }
  55. /**
  56. * Store cached content for a given display and possibly pane.
  57. */
  58. function panels_set_cached_content($cache, $display, $args, $context, $pane = NULL) {
  59. // Never use cache on a POST.
  60. if (!empty($_POST)) {
  61. return FALSE;
  62. }
  63. $method = $pane ? $pane->cache['method'] : $display->cache['method'];
  64. $function = panels_plugin_get_function('cache', $method, 'cache set');
  65. if (!$function) {
  66. return FALSE;
  67. }
  68. $conf = $pane ? $pane->cache['settings'] : $display->cache['settings'];
  69. // Snapshot it.
  70. $cache->cache();
  71. return $function($conf, $cache, $display, $args, $context, $pane);
  72. }
  73. /**
  74. * Clear all cached content for a display.
  75. */
  76. function panels_clear_cached_content($display) {
  77. // Figure out every method we might be using to cache content in this display:
  78. $methods = array();
  79. if (!empty($display->cache['method'])) {
  80. $methods[$display->cache['method']] = TRUE;
  81. }
  82. foreach ($display->content as $pane) {
  83. if (!empty($pane->cache['method'])) {
  84. $methods[$pane->cache['method']] = TRUE;
  85. }
  86. }
  87. foreach (array_keys($methods) as $method) {
  88. $function = panels_plugin_get_function('cache', $method, 'cache clear');
  89. if ($function) {
  90. $function($display);
  91. }
  92. }
  93. }
  94. /**
  95. * An object to hold caching information while it is happening.
  96. */
  97. class panels_cache_object {
  98. var $content = '';
  99. var $head = NULL;
  100. var $css = NULL;
  101. var $js = NULL;
  102. var $tokens = NULL;
  103. var $ready = FALSE;
  104. /**
  105. * When constructed, take a snapshot of our existing out of band data.
  106. */
  107. function __construct() {
  108. $this->head = drupal_add_html_head();
  109. $this->css = drupal_add_css();
  110. $this->tokens = ctools_set_page_token();
  111. $this->js = drupal_add_js();
  112. }
  113. /**
  114. * Add content to the cache. This assumes a pure stream;
  115. * use set_content() if it's something else.
  116. */
  117. function add_content($content) {
  118. $this->content .= $content;
  119. }
  120. function set_content($content) {
  121. $this->content = $content;
  122. }
  123. /**
  124. * Set the object for storing. This overwrites.
  125. */
  126. function cache() {
  127. if ($this->ready) {
  128. return;
  129. }
  130. $this->ready = TRUE;
  131. // Simple replacement for head.
  132. $this->head = str_replace($this->head, '', drupal_add_html_head());
  133. // Slightly less simple for CSS:
  134. $css = drupal_add_css();
  135. $start = $this->css;
  136. $this->css = array();
  137. foreach ($css as $name => $data) {
  138. if (!isset($start[$name])) {
  139. $this->css[$name] = $data;
  140. }
  141. }
  142. $js = drupal_add_js();
  143. $start = $this->js;
  144. $this->js = array();
  145. // Use the advanced mapping function from Drupal >= 7.23 if available.
  146. $array_mapping_func = function_exists('drupal_array_diff_assoc_recursive') ? 'drupal_array_diff_assoc_recursive' : 'array_diff_assoc';
  147. // If there are any differences between the old and the new javascript then
  148. // store them to be added later.
  149. if ($diff = $array_mapping_func($js, $start)) {
  150. // Iterate over the diff to ensure we keep the keys on merge and don't add
  151. // unnecessary items.
  152. foreach ($diff as $key => $diff_data) {
  153. // Special case the settings key and get the difference of the data.
  154. if ($key === 'settings') {
  155. // Iterate over the diff to ensure we keep the keys on merge and don't
  156. // add unnecessary items.
  157. if (isset($diff[$key]['data'])) {
  158. foreach ($diff[$key]['data'] as $settings_key => $settings_data) {
  159. // Merge the changes with the base to get a complete settings
  160. // array.
  161. $this->js[$key]['data'][] = drupal_array_merge_deep($settings_data, $diff[$key]['data'][$settings_key]);
  162. }
  163. }
  164. }
  165. else {
  166. $this->js[$key] = $diff_data;
  167. // Check if the key was present already and if so merge the changes
  168. // with the original data to get the full settings array.
  169. if (isset($start[$key])) {
  170. $this->js[$key] = drupal_array_merge_deep($start[$key], $this->js[$key]);
  171. }
  172. }
  173. }
  174. }
  175. // And for tokens:
  176. $tokens = ctools_set_page_token();
  177. foreach ($this->tokens as $token => $argument) {
  178. if (isset($tokens[$token])) {
  179. unset($tokens[$token]);
  180. }
  181. }
  182. $this->tokens = $tokens;
  183. }
  184. /**
  185. * Restore out of band data saved to cache.
  186. */
  187. function restore() {
  188. if (!empty($this->head)) {
  189. drupal_add_html_head($this->head);
  190. }
  191. if (!empty($this->css)) {
  192. foreach ($this->css as $args) {
  193. drupal_add_css($args['data'], $args);
  194. }
  195. }
  196. if (!empty($this->js)) {
  197. foreach ($this->js as $key => $args) {
  198. if ($key !== 'settings') {
  199. drupal_add_js($args['data'], $args);
  200. }
  201. else {
  202. foreach ($args['data'] as $setting) {
  203. drupal_add_js($setting, 'setting');
  204. }
  205. }
  206. }
  207. }
  208. if (!empty($this->tokens)) {
  209. foreach ($this->tokens as $token => $key) {
  210. list($type, $argument) = $key;
  211. ctools_set_page_token($token, $type, $argument);
  212. }
  213. }
  214. }
  215. }
  216. /**
  217. * Get the title of a pane.
  218. *
  219. * @deprecated @todo this function should be removed.
  220. *
  221. * @param $pane
  222. * The $pane object.
  223. */
  224. function panels_get_pane_title(&$pane, $context = array(), $incoming_content = NULL) {
  225. ctools_include('content');
  226. return ctools_content_admin_title($pane->type, $pane->subtype, $pane->configuration, $context);
  227. }
  228. /**
  229. * Fetch metadata on a specific layout plugin.
  230. *
  231. * @param $layout
  232. * Name of a panel layout. If the layout name contains a ':' this
  233. * indicates that we need to separate the sublayout out and
  234. * load it individually.
  235. *
  236. * @return
  237. * An array with information about the requested panel layout.
  238. */
  239. function panels_get_layout($layout) {
  240. ctools_include('plugins');
  241. return ctools_get_plugins('panels', 'layouts', $layout);
  242. }
  243. /**
  244. * Fetch metadata for all layout plugins.
  245. *
  246. * @return
  247. * An array of arrays with information about all available panel layouts.
  248. */
  249. function panels_get_layouts() {
  250. ctools_include('plugins');
  251. return ctools_get_plugins('panels', 'layouts');
  252. }
  253. /**
  254. * Fetch metadata for all layout plugins that provide builders.
  255. *
  256. * The layout builders allow reusable layouts be stored in the database and
  257. * exported. Since there are different methods, we are not limiting this
  258. * to just one plugin.
  259. *
  260. * @return
  261. * An array of arrays with information about panel layouts with builders.
  262. */
  263. function panels_get_layout_builders() {
  264. ctools_include('plugins');
  265. $plugins = ctools_get_plugins('panels', 'layouts');
  266. $builders = array();
  267. foreach ($plugins as $name => $plugin) {
  268. if (!empty($plugin['builder'])) {
  269. $builders[$name] = $plugin;
  270. }
  271. }
  272. return $builders;
  273. }
  274. /**
  275. * Fetch metadata on a specific style plugin.
  276. *
  277. * @param $style
  278. * Name of a panel style.
  279. *
  280. * @return
  281. * An array with information about the requested panel style.
  282. */
  283. function panels_get_style($style) {
  284. ctools_include('plugins');
  285. return ctools_get_plugins('panels', 'styles', $style);
  286. }
  287. /**
  288. * Fetch metadata for all style plugins.
  289. *
  290. * @return
  291. * An array of arrays with information about all available panel styles.
  292. */
  293. function panels_get_styles() {
  294. ctools_include('plugins');
  295. return ctools_get_plugins('panels', 'styles');
  296. }
  297. /**
  298. * Fetch metadata on a specific caching plugin.
  299. *
  300. * @param $cache
  301. * Name of a panel cache.
  302. *
  303. * @return
  304. * An array with information about the requested panel cache.
  305. */
  306. function panels_get_cache($cache) {
  307. ctools_include('plugins');
  308. return ctools_get_plugins('panels', 'cache', $cache);
  309. }
  310. /**
  311. * Fetch metadata for all context plugins.
  312. *
  313. * @return
  314. * An array of arrays with information about all available panel caches.
  315. */
  316. function panels_get_caches() {
  317. ctools_include('plugins');
  318. return ctools_get_plugins('panels', 'cache');
  319. }
  320. /**
  321. * Fetch metadata on a specific display renderer plugin.
  322. *
  323. * @return
  324. * An array of arrays with information about the requested panels display
  325. * renderer.
  326. */
  327. function panels_get_display_renderer($renderer) {
  328. ctools_include('plugins');
  329. return ctools_get_plugins('panels', 'display_renderers', $renderer);
  330. }
  331. /**
  332. * Fetch metadata for all display renderer plugins.
  333. *
  334. * @return
  335. * An array of arrays with information about all available panels display
  336. * renderer.
  337. */
  338. function panels_get_display_renderers() {
  339. ctools_include('plugins');
  340. return ctools_get_plugins('panels', 'display_renderers');
  341. }
  342. /**
  343. * Get and initialize the class to handle rendering a display.
  344. *
  345. * @return
  346. * Either the instantiated renderer or FALSE if one could not be found.
  347. */
  348. function panels_get_renderer_handler($plugin, &$display) {
  349. if (is_string($plugin)) {
  350. $plugin = panels_get_display_renderer($plugin);
  351. }
  352. $class = ctools_plugin_get_class($plugin, 'renderer');
  353. if ($class) {
  354. $renderer = new $class();
  355. $renderer->init($plugin, $display);
  356. return $renderer;
  357. }
  358. return FALSE;
  359. }
  360. /**
  361. * Choose a renderer for a display based on a render pipeline setting.
  362. */
  363. function panels_get_renderer($pipeline_name, &$display) {
  364. // Load the pipeline.
  365. ctools_include('export');
  366. $pipeline = ctools_export_crud_load('panels_renderer_pipeline', $pipeline_name);
  367. // If we can't, or it has no renderers, default.
  368. if (!$pipeline || empty($pipeline->settings['renderers'])) {
  369. return panels_get_renderer_handler('standard', $display);
  370. }
  371. // Get contexts set on the pipeline:
  372. $contexts = array();
  373. if (!empty($pipeline->settings['contexts'])) {
  374. $contexts = ctools_context_load_contexts($pipeline->settings['context']);
  375. }
  376. // Cycle through our renderers and see.
  377. foreach ($pipeline->settings['renderers'] as $candidate) {
  378. // See if this passes selection criteria.
  379. if (!ctools_access($candidate['access'], $contexts)) {
  380. continue;
  381. }
  382. $renderer = panels_get_renderer_handler($candidate['renderer'], $display);
  383. if (!empty($candidate['options'])) {
  384. $renderer->set_options($candidate['options']);
  385. }
  386. return $renderer;
  387. }
  388. // Fall through. If no renderer is selected, use the standard renderer.
  389. return panels_get_renderer_handler('standard', $display);
  390. }
  391. /**
  392. * Sort callback for sorting renderer pipelines.
  393. *
  394. * Sort first by weight, then by title.
  395. */
  396. function _panels_renderer_pipeline_sort($a, $b) {
  397. if ($a->weight == $b->weight) {
  398. if ($a->admin_title == $b->admin_title) {
  399. return 0;
  400. }
  401. return ($a->admin_title < $b->admin_title) ? -1 : 1;
  402. }
  403. return ($a->weight < $b->weight) ? -1 : 1;
  404. }
  405. /**
  406. * Get a list of available renderer pipelines.
  407. *
  408. * This can be used to form a select or radios widget by enabling
  409. * sorting. Descriptions are left in.
  410. */
  411. function panels_get_renderer_pipelines($sort = TRUE) {
  412. ctools_include('export');
  413. $pipelines = ctools_export_crud_load_all('panels_renderer_pipeline');
  414. if ($sort) {
  415. uasort($pipelines, '_panels_renderer_pipeline_sort');
  416. }
  417. return $pipelines;
  418. }
  419. /**
  420. * Fetch metadata on a specific panels_storage plugin.
  421. *
  422. * @param $storage
  423. * Name of a panel_storage plugin.
  424. *
  425. * @return
  426. * An array with information about the requested panels_storage plugin
  427. */
  428. function panels_get_panels_storage_plugin($storage) {
  429. ctools_include('plugins');
  430. return ctools_get_plugins('panels', 'panels_storage', $storage);
  431. }
  432. /**
  433. * Fetch metadata for all panels_storage plugins.
  434. *
  435. * @return
  436. * An array of arrays with information about all available panels_storage plugins.
  437. */
  438. function panels_get_panels_storage_plugins() {
  439. ctools_include('plugins');
  440. return ctools_get_plugins('panels', 'panels_storage');
  441. }
  442. /**
  443. * Get a function from a plugin, if it exists.
  444. *
  445. * @param $plugin
  446. * The type of plugin
  447. * @param $which
  448. * Either the loaded plugin object (or the same data in array form)
  449. * or a string with the name of the desired the specific plugin.
  450. * @param $function_name
  451. * The identifier of the function. For example, 'settings form'.
  452. *
  453. * @return
  454. * The actual name of the function to call, or NULL if the function
  455. * does not exist.
  456. *
  457. * @deprecated All calls to this function should be removed.
  458. */
  459. function panels_plugin_get_function($plugin, $which, $function_name) {
  460. ctools_include('plugins');
  461. if (is_object($which) || is_array($which)) {
  462. return ctools_plugin_get_function($which, $function_name);
  463. }
  464. else {
  465. return ctools_plugin_load_function('panels', $plugin, $which, $function_name);
  466. }
  467. }