plugins.inc 14 KB

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