panels.module

Tracking 5.x-2.x branch
  1. drupal
    1. 5 contributions/panels/panels.module
    2. 6 contributions/panels/panels.module
    3. 7 contributions/panels/panels.module

panels.module Core API for Panels. Provides display editing and rendering capabilities.

Functions & methods

NameDescription
panels_add_buttonAdd a single button to a form.
panels_ajax_formHandle a form for AJAX in a manner that happens to be basically the opposite of the normal flow; if the form hasn't been processed, just render it and exit; if it has been submitted successfuly, however, then we return whatever the submit…
panels_ajax_passthruHelper function for our AJAX stuff to call through to the right location
panels_ajax_render
panels_api_versionReturns the API version of Panels. This didn't exist in 1.
panels_cache_clearClear a display from the cache; used if the editing is aborted.
panels_cache_getGet a display from the cache; this is used if the display is currently being edited, which can be a seriously multi-step process.
panels_cache_setSave the edited display into the cache.
panels_common_cache_clearClear a display from the cache; used if the editing is aborted.
panels_common_cache_getGet an object from cache.
panels_common_cache_setSave the edited display into the cache.
panels_cronClean up old caches
panels_delete_displayDelete a display.
panels_editMain API entry point to edit a panel display.
panels_edit_layoutAPI entry point for selecting a layout for a given display.
panels_edit_layout_settingsAPI entry point for configuring the layout settings for a given display.
panels_elementsCustom form element to do our nice images.
panels_export_displayExports the provided display into portable code.
panels_export_pane
panels_getRetrieve from global storage
panels_get_panel_style_and_settingsGiven a display and the id of a panel, get the style in which to render that panel.
panels_get_pathpanels path helper function
panels_help@file panels.module Core API for Panels. Provides display editing and rendering capabilities.
panels_imagebutton_value
panels_load_displayLoad a single display.
panels_load_displaysLoad and fill the requested $display object(s).
panels_load_includeLoad a panels include file.
panels_menuImplementation of hook_menu
panels_new_displayCreates a new display, setting the ID to our magic new id.
panels_nid_autocompleteHelper function for parsing an autocomplete node field.
panels_node_autocompleteHelper function for autocompletion of node titles. This is mostly stolen from clipper.
panels_node_typeImplementation of hook_node_type().
panels_permImplementation of hook_perm
panels_print_layoutFor external use: Given a layout ID and a $content array, return the panel display. The content array is filled in based upon the content available in the layout. If it's a two column with a content array defined like array('left' =>…
panels_print_layout_icon
panels_print_layout_linkPrint the layout link. Sends out to a theme function. @layout
panels_render_displayRender a display by loading the content into an appropriate array and then passing through to panels_render_layout.
panels_render_layoutGiven a full layout structure and a content array, render a panel display.
panels_render_paneRender a pane using the appropriate style.
panels_render_panelRender a panel, by storing the content of each pane in an appropriate array and then passing through to the theme function that will render the panel in the configured panel style.
panels_render_panesRender all the panes in a display into a $content array to be used by the display theme function.
panels_render_pane_contentRender a single pane, identifying its context, and put it into the $panes array.
panels_sanitize_displayClean up a display and make sure it has some required information if it doesn't already exist. Currently we require a context, an incoming content and a css_id.
panels_save_displaySave a display object.
panels_setGlobal storage function, used mostly so that _submit hooks can pass data back to their originator more easily. TODO: deprecated but still in use.
panels_var_export
theme_panels_imagebuttonTheme our image button.
theme_panels_layout_iconTheme the layout icon image @layout
theme_panels_layout_linkTheme the layout link image @layout
theme_panels_paneRender a panel pane like a block.

Classes

NameDescription
panels_displayForms the basis of a panel display

File

View source
  1. <?php
  2. /**
  3. * @file panels.module
  4. * Core API for Panels. Provides display editing and rendering capabilities.
  5. */
  6. function panels_help($section = NULL) {
  7. $output = '';
  8. switch ($section) {
  9. case 'admin/help#panels':
  10. case 'admin/panels':
  11. $output = '<p>'. t(' Panels module is the core engine for a number of sub-modules, including Panels pages, Panels nodes, Mini panels, and Views panes. Panels module allows the website adminstrator (or sometimes the end-user) to manipulate the layout of individual pages, sidebars, and content pieces, as well as easily dictate what content is displayed in the layout.') .'</p>';
  12. $output .= '<p>'. t('Most Drupal users are familiar with the block to region layout mechanism in which you can assign a block to any region defined in your theme. Panels takes this concept a massive step forward. Through the panels interface you can start by creating a layout with any number of columns, headers, and footer, and control the width of those areas.') .'</p>';
  13. $output .= '<p>'. t('After creating your layout, you can assign pieces of content to those areas in an easy drag and drop interface. Content is not limited to blocks, but can be nodes, views, or other types of content that expose themselves to panels.') .'</p>';
  14. $output .= '<p><b>'. t('Panel pages') .'</b>'. t(' are the the primary panels module, you can use this for creating single full page layouts. This replaces the standard panel that existed in the earlier versions of panels. If you are upgrading your site from Panels 1, and you cannot find where your panels went, be sure to enable the panel pages module!') .'</p>';
  15. $output .= '<p><b>'. t('Panel nodes') .'</b>'. t(' are useful for creating layouts that only occupy the content area of your pages. Frequently, it is desirable to add an area to a node layout, such as a pull quote for a newspaper or a photo block, that you don\'t necessarily want on every node. Panels Nodes lets you control the layout of a single node at a time and place content such as blog posts, images, blogs in and around the post.') .'</p>';
  16. $output .= '<p><b>'. t('Mini panels') .'</b>'. t(' are a layout mechanism for blocks. It won\'t take long using panels before you get to a point when you want a panel inside of a panel. Or a panel that can be used as a block. That is exactly what mini-panels does. You can create a small panel here with various pieces of content and then put it inside of a panels-page or panels-node.') .'</p>';
  17. $output .= '<p><b>'. t('Views panes') .'</b>'. t(" expose views so they may be added to panels. Panels will automatically detect block views without this module; however, page and embedded views will <strong>not be</strong> selectable from Panels by default. If you enable the <strong>Views panes</strong> module, you may expose individual views to Panels. The <strong>Legacy views panes</strong> module will simply expose all views, so that you may add them in any panel. Both modules provide options for customization of the views' settings on a per-Panel basis. This is useful if you have multiple administrators or want to use panels for something other than just panel pages.") .'</p>';
  18. $output .= '<p>' . t('If you do not see the above items in the list below, you may need to activate them on the <a href="!url">module administration</a> page.', array('!url' => url('admin/build/modules'))) . '</p>';
  19. return $output;
  20. }
  21. }
  22. /**
  23. * Returns the API version of Panels. This didn't exist in 1.
  24. *
  25. * @return An array with the major and minor versions
  26. */
  27. function panels_api_version() {
  28. return array(2, 0);
  29. }
  30. /**
  31. * Implementation of hook_menu
  32. */
  33. function panels_menu($may_cache) {
  34. if ($may_cache) {
  35. $items[] = array(
  36. 'path' => 'admin/panels',
  37. 'title' => t('Panels'),
  38. 'access' => user_access('access administration pages'),
  39. 'callback' => 'system_admin_menu_block_page',
  40. 'description' => t('Administer items related to the Panels module.'),
  41. );
  42. $items[] = array(
  43. 'path' => 'panels/node/autocomplete',
  44. 'title' => t('Autocomplete node'),
  45. 'callback' => 'panels_node_autocomplete',
  46. 'access' => user_access('access content'),
  47. 'type' => MENU_CALLBACK,
  48. );
  49. // TODO Deprecated generalized ajax handler. Remove if at all possible.
  50. $items[] = array(
  51. 'path' => 'panels/ajax',
  52. 'title' => t('ajax'),
  53. 'callback' => 'panels_ajax_passthru',
  54. 'callback arguments' => array('panels_ajax'),
  55. 'access' => user_access('access content'),
  56. 'type' => MENU_CALLBACK,
  57. );
  58. $items[] = array(
  59. 'path' => 'panels/common/ajax',
  60. 'title' => t('ajax'),
  61. 'callback' => 'panels_ajax_passthru',
  62. 'callback arguments' => array('panels_common_ajax'),
  63. 'access' => user_access('access content'),
  64. 'type' => MENU_CALLBACK,
  65. );
  66. $items[] = array(
  67. 'path' => 'panels/ajax/add-content',
  68. 'title' => t('ajax'),
  69. 'callback' => 'panels_ajax_passthru',
  70. 'callback arguments' => array('panels_ajax_add_content'),
  71. 'access' => user_access('access content'),
  72. 'type' => MENU_CALLBACK,
  73. );
  74. $items[] = array(
  75. 'path' => 'panels/ajax/add-config',
  76. 'title' => t('ajax'),
  77. 'callback' => 'panels_ajax_passthru',
  78. 'callback arguments' => array('panels_ajax_add_config'),
  79. 'access' => user_access('access content'),
  80. 'type' => MENU_CALLBACK,
  81. );
  82. $items[] = array(
  83. 'path' => 'panels/ajax/configure',
  84. 'title' => t('ajax'),
  85. 'callback' => 'panels_ajax_passthru',
  86. 'callback arguments' => array('panels_ajax_configure'),
  87. 'access' => user_access('access content'),
  88. 'type' => MENU_CALLBACK,
  89. );
  90. $items[] = array(
  91. 'path' => 'panels/ajax/toggle-shown',
  92. 'title' => t('ajax'),
  93. 'callback' => 'panels_ajax_passthru',
  94. 'callback arguments' => array('panels_ajax_toggle_shown'),
  95. 'access' => user_access('access content'),
  96. 'type' => MENU_CALLBACK,
  97. );
  98. $items[] = array(
  99. 'path' => 'panels/ajax/cache',
  100. 'title' => t('ajax'),
  101. 'callback' => 'panels_ajax_passthru',
  102. 'callback arguments' => array('panels_ajax_cache'),
  103. 'access' => user_access('access content'),
  104. 'type' => MENU_CALLBACK,
  105. );
  106. $items[] = array(
  107. 'path' => 'panels/ajax/cache-settings',
  108. 'title' => t('ajax'),
  109. 'callback' => 'panels_ajax_passthru',
  110. 'callback arguments' => array('panels_ajax_cache_settings'),
  111. 'access' => user_access('access content'),
  112. 'type' => MENU_CALLBACK,
  113. );
  114. $items[] = array(
  115. 'path' => 'panels/ajax/panel_settings',
  116. 'title' => t('ajax'),
  117. 'callback' => 'panels_ajax_passthru',
  118. 'callback arguments' => array('panels_panel_settings_ajax'),
  119. 'access' => user_access('access content'),
  120. 'type' => MENU_CALLBACK,
  121. );
  122. }
  123. else {
  124. drupal_add_css(panels_get_path('css/panels.css'));
  125. drupal_add_js(panels_get_path('js/panels.js'));
  126. }
  127. return $items;
  128. }
  129. /**
  130. * Load a panels include file.
  131. */
  132. function panels_load_include($include, $path = 'includes/') {
  133. require_once './' . panels_get_path("$path$include.inc");
  134. }
  135. /**
  136. * Helper function for our AJAX stuff to call through to the right location
  137. */
  138. function panels_ajax_passthru() {
  139. $args = func_get_args();
  140. $callback = array_shift($args);
  141. panels_load_include('plugins');
  142. if (arg(1) == 'common') {
  143. panels_load_include('common');
  144. }
  145. else {
  146. panels_load_include('display_edit');
  147. }
  148. return call_user_func_array($callback, $args);
  149. }
  150. /**
  151. * Simple render function to make sure output is what we want.
  152. * @ingroup panels_ajax
  153. */
  154. // TODO it's the presence/absence of the $url var that determines whether or not the deprecated submit function gets called in the js; ensure that nothing needs in, then get rid of it.
  155. function panels_ajax_render($output = NULL, $title = NULL, $url = NULL) {
  156. if (!is_object($output)) {
  157. $temp = new stdClass();
  158. $temp->output = $output;
  159. switch ($output) {
  160. case 'dismiss':
  161. $temp->type = $output;
  162. break;
  163. default:
  164. $temp->type = 'display';
  165. }
  166. $temp->title = $title;
  167. $temp->url = $url;
  168. $output = $temp;
  169. }
  170. if (!$output->output || !$output->type) {
  171. $output->output = t('The input was invalid');
  172. $output->type = 'display';
  173. $output->title = t('Error');
  174. }
  175. drupal_set_header('Content-Type: text/javascript; charset=utf-8');
  176. print drupal_to_js($output);
  177. exit;
  178. }
  179. /**
  180. * Handle a form for AJAX in a manner that happens to be basically the
  181. * opposite of the normal flow; if the form hasn't been processed,
  182. * just render it and exit; if it has been submitted successfuly, however,
  183. * then we return whatever the submit function returned and do our
  184. * next step accordingly.
  185. *
  186. * @param $form_id
  187. * The id of the form
  188. * @param $title
  189. * The title for the modal dialog, if rendered.
  190. * @param $url
  191. * The next URL to go to; may be NULL.
  192. * @param ...
  193. * Any arguments that go to the form.
  194. */
  195. function panels_ajax_form($form_id, $title, $url) {
  196. $args = func_get_args();
  197. // Remove the $title and $url
  198. array_splice($args, 1, 2);
  199. $form = call_user_func_array('drupal_retrieve_form', $args);
  200. $form['#redirect'] = FALSE;
  201. $result = drupal_process_form($form_id, $form);
  202. if (isset($result)) {
  203. return $result;
  204. }
  205. // If the form wasn't submitted successfully, render the form.
  206. $output = theme('status_messages');
  207. $output .= drupal_render_form($form_id, $form);
  208. panels_ajax_render($output, $title, $url);
  209. }
  210. /**
  211. * panels path helper function
  212. */
  213. function panels_get_path($file, $base_path = false, $module = 'panels') {
  214. if ($base_path) {
  215. $output = base_path();
  216. }
  217. return $output . drupal_get_path('module', $module) . '/' . $file;
  218. }
  219. /**
  220. * Implementation of hook_perm
  221. */
  222. function panels_perm() {
  223. return array('view all panes', 'view pane admin links', 'administer pane visibility', 'administer pane access', 'administer advanced pane settings', 'use panels caching features');
  224. }
  225. // ---------------------------------------------------------------------------
  226. // panels custom image button
  227. /**
  228. * Custom form element to do our nice images.
  229. */
  230. function panels_elements() {
  231. $type['panels_imagebutton'] = array('#input' => TRUE, '#button_type' => 'submit');
  232. return $type;
  233. }
  234. /**
  235. * Theme our image button.
  236. */
  237. function theme_panels_imagebutton($element) {
  238. return '<input type="image" ' .
  239. 'class="form-' . $element['#button_type'] . ' ' . $element['#class'] . '" ' .
  240. 'name="'. $element['#name'] .'" ' .
  241. 'value="'. check_plain($element['#default_value']) .'" ' .
  242. 'id="' . $element['#id'] . '" ' .
  243. drupal_attributes($element['#attributes']) .
  244. ' src="' . base_path() . $element['#image'] . '" ' .
  245. 'alt="' . $element['#title'] . '" ' .
  246. 'title="' . $element['#title'] . '" ' .
  247. "/>\n";
  248. }
  249. function panels_imagebutton_value() {
  250. // null function guarantees default_value doesn't get moved to #value.
  251. }
  252. /**
  253. * Add a single button to a form.
  254. */
  255. function panels_add_button($image, $name, $text, $class, $id = NULL) {
  256. return array(
  257. '#type' => 'panels_imagebutton',
  258. '#image' => panels_get_path('images/' . $image),
  259. '#title' => $text,
  260. '#default_value' => $name,
  261. '#class' => $class,
  262. '#id' => $id,
  263. );
  264. }
  265. // ---------------------------------------------------------------------------
  266. // cache handling stuff for display editing
  267. /**
  268. * Get a display from the cache; this is used if the display is currently
  269. * being edited, which can be a seriously multi-step process.
  270. */
  271. function panels_cache_get($did) {
  272. return panels_common_cache_get('panels', $did);
  273. }
  274. /**
  275. * Save the edited display into the cache.
  276. */
  277. function panels_cache_set($did, $cache) {
  278. return panels_common_cache_set('panels', $did, $cache);
  279. }
  280. /**
  281. * Clear a display from the cache; used if the editing is aborted.
  282. */
  283. function panels_cache_clear($did) {
  284. return panels_common_cache_clear('panels', $did);
  285. }
  286. // ---------------------------------------------------------------------------
  287. // cache handling stuff for non-display objects.
  288. /**
  289. * Get an object from cache.
  290. */
  291. function panels_common_cache_get($obj, $did, $skip_cache = FALSE) {
  292. static $cache = array();
  293. $key = "$obj:$did";
  294. if ($skip_cache) {
  295. unset($cache[$key]);
  296. }
  297. if (!array_key_exists($key, $cache)) {
  298. $data = db_fetch_object(db_query("SELECT * FROM {panels_object_cache} WHERE sid = '%s' AND obj = '%s' AND did = %d", session_id(), $obj, $did));
  299. if ($data) {
  300. $cache[$key] = unserialize($data->data);
  301. }
  302. }
  303. return isset($cache[$key]) ? $cache[$key] : NULL;
  304. }
  305. /**
  306. * Save the edited display into the cache.
  307. */
  308. function panels_common_cache_set($obj, $did, $cache) {
  309. panels_common_cache_clear($obj, $did);
  310. db_query("INSERT INTO {panels_object_cache} (sid, obj, did, data, timestamp) VALUES ('%s', '%s', %d, '%s', %d)", session_id(), $obj, $did, serialize($cache), time());
  311. }
  312. /**
  313. * Clear a display from the cache; used if the editing is aborted.
  314. */
  315. function panels_common_cache_clear($obj, $did) {
  316. db_query("DELETE FROM {panels_object_cache} WHERE sid = '%s' AND obj = '%s' AND did = %d", session_id(), $obj, $did);
  317. }
  318. /**
  319. * Clean up old caches
  320. */
  321. function panels_cron() {
  322. // delete anything 7 days old or more.
  323. db_query("DELETE FROM {panels_object_cache} WHERE timestamp < %d", time() - (86400 * 7));
  324. }
  325. /**
  326. * Global storage function, used mostly so that _submit hooks can pass data
  327. * back to their originator more easily. TODO: deprecated but still in use.
  328. */
  329. function panels_set($var, $value = NULL) {
  330. static $vars = array();
  331. if ($value !== NULL) {
  332. $vars[$var] = $value;
  333. }
  334. return $vars[$var];
  335. }
  336. /**
  337. * Retrieve from global storage
  338. */
  339. function panels_get($var) {
  340. return panels_set($var);
  341. }
  342. // ---------------------------------------------------------------------------
  343. // panels display editing
  344. /**
  345. * Main API entry point to edit a panel display.
  346. *
  347. * @ingroup MainAPI
  348. *
  349. * Sample implementations utiltizing the the complex $destination behavior can be found
  350. * in panels_page_edit_content() and, in a separate contrib module, OG Blueprints
  351. * (http://drupal.org/project/og_blueprints), og_blueprints_blueprint_edit().
  352. *
  353. * @param object $display instanceof panels_display \n
  354. * A fully loaded panels $display object, as returned from panels_load_display().
  355. * Merely passing a did is NOT sufficient. \n
  356. * Note that 'fully loaded' means the $display must already be loaded with any contexts
  357. * the caller wishes to have set for the display.
  358. * @param mixed $destination \n
  359. * The redirect destination that the user should be taken to on form submission or
  360. * cancellation. With panels_edit, $destination has complex effects on the return
  361. * values of panels_edit() once the form has been submitted. See the explanation of
  362. * the return value below to understand the different types of values returned by panels_edit()
  363. * at different stages of FAPI. Under most circumstances, simply passing in
  364. * drupal_get_destination() is all that's necessary.
  365. * @param array $content_types \n
  366. * An associative array of allowed content types, typically as returned from
  367. * panels_common_get_allowed_types(). Note that context partially governs available content types,
  368. * so you will want to create any relevant contexts using panels_create_context() or
  369. * panels_create_context_empty() to make sure all the appropriate content types are available.
  370. *
  371. * @return
  372. * Because the functions called by panels_edit() invoke the form API, this function
  373. * returns different values depending on the stage of form submission we're at. In Drupal 5,
  374. * the phase of form submission is indicated by the contents of $_POST['op']. Here's what you'll
  375. * get at different stages:
  376. * -# If !$_POST['op']: then we're on on the initial passthrough and the form is being
  377. * rendered, so it's the $form itself that's being returned. Because negative margins,
  378. * a common CSS technique, bork the display editor's ajax drag-and-drop, it's important
  379. * that the $output be printed, not returned. Use this syntax in the caller function: \n
  380. * print theme('page', panels_edit($display, $destination, $content_types), FALSE); \n
  381. * -# If $_POST['op'] == t('Cancel'): form submission has been cancelled. If empty($destination) == FALSE,
  382. * then there is no return value and the panels API takes care of redirecting to $destination.
  383. * If empty($destination) == TRUE, then there's still no return value, but the caller function
  384. * has to take care of form redirection.
  385. * -# If $_POST['op'] == ('Save'): the form has been submitted successfully and has run through
  386. * panels_edit_display_submit(). $output depends on the value of $destination:
  387. * - If empty($destination) == TRUE: $output contains the modified $display
  388. * object, and no redirection will occur. This option is useful if the caller
  389. * needs to perform additional operations on or with the modified $display before
  390. * the page request is complete. Using hook_form_alter() to add an additional submit
  391. * handler is typically the preferred method for something like this, but there
  392. * are certain use cases where that is infeasible and $destination = NULL should
  393. * be used instead. If this method is employed, the caller will need to handle form
  394. * redirection. Note that having $_REQUEST['destination'] set, whether via
  395. * drupal_get_destination() or some other method, will NOT interfere with this
  396. * functionality; consequently, you can use drupal_get_destination() to safely store
  397. * your desired redirect in the caller function, then simply use drupal_goto() once
  398. * panels_edit() has done its business.
  399. * - If empty($destination) == FALSE: the form will redirect to the URL string
  400. * given in $destination and NO value will be returned.
  401. */
  402. function panels_edit($display, $destination = NULL, $content_types = NULL) {
  403. panels_load_include('display_edit');
  404. panels_load_include('plugins');
  405. return _panels_edit($display, $destination, $content_types);
  406. }
  407. /**
  408. * API entry point for selecting a layout for a given display.
  409. *
  410. * @ingroup MainAPI
  411. *
  412. * Layout selection is nothing more than a list of radio items encompassing the available
  413. * layouts for this display, as defined by .inc files in the panels/layouts subdirectory.
  414. * The only real complexity occurs when a user attempts to change the layout of a display
  415. * that has some content in it.
  416. *
  417. * @param object $display instanceof panels_display \n
  418. * A fully loaded panels $display object, as returned from panels_load_display().
  419. * Merely passing a did is NOT sufficient.
  420. * @param string $finish
  421. * A string that will be used for the text of the form submission button. If no value is provided,
  422. * then the form submission button will default to t('Save').
  423. * @param mixed $destination
  424. * Basic usage is a string containing the URL that the form should redirect to upon submission.
  425. * For a discussion of advanced usages, see panels_edit().
  426. * @param mixed $allowed_layouts
  427. * Allowed layouts has three different behaviors that depend on which of three value types
  428. * are passed in by the caller:
  429. * #- if $allowed_layouts instanceof panels_allowed_layouts (includes subclasses): the most
  430. * complex use of the API. The caller is passing in a loaded panels_allowed_layouts object
  431. * that the client module previously created and stored somewhere using a custom storage
  432. * mechanism.
  433. * #- if is_string($allowed_layouts): the string will be used in a call to variable_get() which
  434. * will call the $allowed_layouts . '_allowed_layouts' var. If the data was stored properly
  435. * in the system var, the $allowed_layouts object will be unserialized and recreated.
  436. * @see panels_common_set_allowed_layouts()
  437. * #- if is_null($allowed_layouts): the default behavior, which also provides backwards
  438. * compatibility for implementations of the Panels2 API written before beta4. In this case,
  439. * a dummy panels_allowed_layouts object is created which does not restrict any layouts.
  440. * Subsequent behavior is indistinguishable from pre-beta4 behavior.
  441. *
  442. * @return
  443. * Can return nothing, or a modified $display object, or a redirection string; return values for the
  444. * panels_edit* family of functions are quite complex. See panels_edit() for detailed discussion.
  445. * @see panels_edit()
  446. */
  447. function panels_edit_layout($display, $finish, $destination = NULL, $allowed_layouts = NULL) {
  448. panels_load_include('display_edit');
  449. panels_load_include('plugins');
  450. return _panels_edit_layout($display, $finish, $destination, $allowed_layouts);
  451. }
  452. /**
  453. * API entry point for configuring the layout settings for a given display.
  454. *
  455. * @ingroup MainAPI
  456. *
  457. * For all layouts except Flexible, the layout settings form allows the user to select styles,
  458. * as defined by .inc files in the panels/styles subdirectory, for the panels in their display.
  459. * For the Flexible layout, the layout settings form allows the user to provide dimensions
  460. * for their flexible layout in addition to applying styles to panels.
  461. *
  462. * TODO and at some point, individual panes should be stylable as well as whole panels.
  463. *
  464. * @param object $display instanceof panels_display \n
  465. * A fully loaded panels $display object, as returned from panels_load_display().
  466. * Merely passing a did is NOT sufficient.
  467. * @param string $finish
  468. * A string that will be used for the text of (one of) the form submission button(s). Note that
  469. * panels will NOT wrap $finish in t() for you, so your caller should make sure to do so. \n
  470. * The submit behavior of the form is primarily governed by the value of $destination (see
  471. * below), but is secondarily governed by $finish as follows:
  472. * -# If $finish != t('Save'), then two #submit buttons will be present: one with the button
  473. * text t('Save'), and the other with the button text $finish. .
  474. * - Clicking the 'Save' button will save any changes on the form to the $display object and
  475. * keep the user on the same editing page.
  476. * - Clicking the $finish button will also save the $display object, but the user will be
  477. * redirected to the URL specified in $destination.
  478. * -# If $finish == t('Save'), then there is only one button, still called t('Save'), but it
  479. * mimics the behavior of the $finish button above by redirecting the user away from the form.
  480. * @param mixed $destination
  481. * Basic usage is a string containing the URL that the form should redirect to upon submission.
  482. * For a discussion of advanced usages that rely on NULL values for $destination, see the
  483. * panels_edit() documentation.
  484. * @param mixed $title
  485. * The $title variable has three modes of operation:
  486. * -# If $title == FALSE (the default), then no widget will appear on the panels_edit_layout_settings form
  487. * allowing the user to select a title, and other means for setting page titles will take precedent. If
  488. * no other means are used to provide a title, then the title will be hidden when rendering the $display.
  489. * -# If $title == TRUE, then two widgets will appear on the panels_edit_layout_settings form allowing the
  490. * user to input a title specific to this $display, as well as a checkbox enabling the user to disable
  491. * page titles entirely for this $display object.
  492. * -# If $title == (string), then the behavior is very similar to mode 2, but the widget description
  493. * on the title textfield will indicate that the $title string will be used as the default page title
  494. * if none is provided on this form. When utilizing this option, note that the panels API can only
  495. * provide the data for these values; you must implement the appropriate conditionals to make it true.
  496. *
  497. * @return
  498. * Can return nothing, or a modified $display object, or a redirection string; return values for the
  499. * panels_edit* family of functions are quite complex. See panels_edit() for detailed discussion.
  500. * @see panels_edit()
  501. */
  502. function panels_edit_layout_settings($display, $finish, $destination = NULL, $title = FALSE) {
  503. panels_load_include('display_edit');
  504. panels_load_include('plugins');
  505. return _panels_edit_layout_settings($display, $finish, $destination, $title);
  506. }
  507. // ---------------------------------------------------------------------------
  508. // panels database functions
  509. /**
  510. * Forms the basis of a panel display
  511. *
  512. * @ingroup MainAPI
  513. *
  514. */
  515. class panels_display {
  516. var $args = array();
  517. var $content = array();
  518. var $panels = array();
  519. var $incoming_content = NULL;
  520. var $css_id = NULL;
  521. var $context = array();
  522. var $title = '';
  523. var $hide_title = 0;
  524. function add_pane($pane, $location = FALSE) {
  525. $pane->pid = $this->next_new_pid();
  526. if (!$location || !isset($this->panels[$location])) {
  527. foreach ($this->panels as $panel_name => $panel) {
  528. if (array_key_exists($pane->pid, $panel)) {
  529. $this->panels[$panel_name][] = $pane->pid;
  530. }
  531. }
  532. }
  533. else {
  534. $this->panels[$location][] = $pane->pid;
  535. }
  536. }
  537. function duplicate_pane($pid, $location = FALSE) {
  538. $pane = $this->clone_pane($pid);
  539. $this->add_pane($pane, $location);
  540. }
  541. function clone_pane($pid) {
  542. $pane = drupal_clone($this->content[$pid]);
  543. foreach (array_keys($this->content) as $pidcheck) {
  544. // necessary?
  545. unset($pane->position);
  546. }
  547. return $pane;
  548. }
  549. function next_new_pid() {
  550. // necessary if/until we use this method and ONLY this method for adding
  551. // temporary pids. then we can do it with a nice static var.
  552. $id = array(0);
  553. foreach (array_keys($this->content) as $pid) {
  554. if (!is_numeric($pid)) {
  555. $id[] = substr($pid, 4);
  556. }
  557. }
  558. $next_id = end($id);
  559. return ++$next_id;
  560. }
  561. }
  562. /**
  563. * Clean up a display and make sure it has some required information if
  564. * it doesn't already exist. Currently we require a context, an incoming
  565. * content and a css_id.
  566. */
  567. function panels_sanitize_display(&$display) {
  568. if (!isset($display->args)) {
  569. $display->args = array();
  570. }
  571. if (!isset($display->incoming_content)) {
  572. $display->incoming_content = NULL;
  573. }
  574. if (!isset($display->context)) {
  575. $display->context = array();
  576. }
  577. if (!isset($display->css_id)) {
  578. $display->css_id = NULL;
  579. }
  580. }
  581. /**
  582. * Creates a new display, setting the ID to our magic new id.
  583. *
  584. * @ingroup MainAPI
  585. */
  586. function panels_new_display() {
  587. $display = new panels_display();
  588. $display->did = 'new';
  589. return $display;
  590. }
  591. /**
  592. * Load and fill the requested $display object(s).
  593. *
  594. * @ingroup HookInvokers
  595. *
  596. * Helper function primarily for for panels_load_display().
  597. *
  598. * @param array $dids
  599. * An indexed array of dids to be loaded from the database.
  600. *
  601. * @return $displays
  602. * An array of displays, keyed by their display dids.
  603. */
  604. function panels_load_displays($dids) {
  605. $displays = array();
  606. if (empty($dids) || !is_array($dids)) {
  607. return $displays;
  608. }
  609. $subs = implode(', ', array_fill(0, count($dids), '%d'));
  610. $result = db_query("SELECT * FROM {panels_display} WHERE did IN ($subs)", $dids);
  611. while ($obj = db_fetch_array($result)) {
  612. $display = new panels_display();
  613. foreach ($obj as $key => $value) {
  614. $display->$key = $value;
  615. // unserialize important bits
  616. if (in_array($key, array('layout_settings', 'panel_settings', 'cache'))) {
  617. $display->$key = empty($display->$key) ? array() : unserialize($display->$key);
  618. }
  619. }
  620. $display->panels = $display->content = array();
  621. $displays[$display->did] = $display;
  622. }
  623. foreach (module_implements('panels_layout_content_alter') as $module) {
  624. $function = $module . '_panels_layout_content_alter';
  625. $function($content, $layout, $settings);
  626. }
  627. $result = db_query("SELECT * FROM {panels_pane} WHERE did IN ($subs) ORDER BY did, panel, position", $dids);
  628. while ($pane = db_fetch_object($result)) {
  629. $pane->configuration = unserialize($pane->configuration);
  630. $pane->cache = empty($pane->cache) ? array() : unserialize($pane->cache);
  631. $pane->access = ($pane->access ? explode(', ', $pane->access) : array());
  632. // Old panels may not have shown property, so enable by default when loading.
  633. $pane->shown = isset($pane->shown) ? $pane->shown : TRUE;
  634. $displays[$pane->did]->panels[$pane->panel][] = $pane->pid;
  635. $displays[$pane->did]->content[$pane->pid] = $pane;
  636. }
  637. return $displays;
  638. }
  639. /**
  640. * Load a single display.
  641. *
  642. * @ingroup MainAPI
  643. *
  644. * @param int $did
  645. * The display id (did) of the display to be loaded.
  646. *
  647. * @return object $display instanceof panels_display \n
  648. * Returns a partially-loaded panels_display object. $display objects returned from
  649. * from this function have only the following data:
  650. * - $display->did (the display id)
  651. * - $display->name (the 'name' of the display, where applicable - it often isn't)
  652. * - $display->layout (a string with the system name of the display's layout)
  653. * - $display->panel_settings (custom layout style settings contained in an associative array; NULL if none)
  654. * - $display->layout_settings (panel size and configuration settings for Flexible layouts; NULL if none)
  655. * - $display->css_id (the special css_id that has been assigned to this display, if any; NULL if none)
  656. * - $display->content (an array of pane objects, keyed by pane id (pid))
  657. * - $display->panels (an associative array of panel regions, each an indexed array of pids in the order they appear in that region)
  658. * - $display->cache (any relevant data from panels_simple_cache)
  659. * - $display->args
  660. * - $display->incoming_content
  661. *
  662. * While all of these members are defined, $display->context is NEVER defined in the returned $display;
  663. * it must be set using one of the panels_context_create() functions.
  664. */
  665. function panels_load_display($did) {
  666. $displays = panels_load_displays(array($did));
  667. if (!empty($displays)) {
  668. return array_shift($displays);
  669. }
  670. }
  671. /**
  672. * Save a display object.
  673. *
  674. * @ingroup MainAPI
  675. *
  676. * Note a new $display only receives a real did once it is run through this function.
  677. * Until then, it uses a string placeholder, 'new', in place of a real did. The same
  678. * applies to all new panes (whether on a new $display or not); in addition,
  679. * panes have sequential numbers appended, of the form 'new-1', 'new-2', etc.
  680. *
  681. * @param object $display instanceof panels_display \n
  682. * The display object to be saved. Passed by reference so the caller need not use
  683. * the return value for any reason except convenience.
  684. *
  685. * @return object $display instanceof panels_display \n
  686. */
  687. function panels_save_display(&$display) {
  688. if ($display->did && $display->did != 'new') {
  689. if (empty($display->cache)) {
  690. $display->cache = array();
  691. }
  692. db_query("UPDATE {panels_display} SET layout = '%s', layout_settings = '%s', panel_settings = '%s', cache = '%s', title = '%s', hide_title = %d WHERE did = %d", $display->layout, serialize($display->layout_settings), serialize($display->panel_settings), serialize($display->cache), $display->title, $display->hide_title, $display->did);
  693. db_query("DELETE FROM {panels_pane} WHERE did = %d", $display->did);
  694. }
  695. else {
  696. $display->did = db_next_id("{panels_display}_did");
  697. db_query("INSERT INTO {panels_display} (did, layout, layout_settings, panel_settings, cache, title, hide_title) VALUES (%d, '%s', '%s', '%s', '%s', '%s', %d)", $display->did, $display->layout, serialize($display->layout_settings), serialize($display->panel_settings), serialize($display->cache), $display->title, $display->hide_title);
  698. }
  699. // update all the panes
  700. panels_load_include('plugins');
  701. foreach ((array) $display->panels as $id => $panes) {
  702. $position = 0;
  703. $new_panes = array();
  704. foreach ((array) $panes as $pid) {
  705. $pane = $display->content[$pid];
  706. $pane->position = $position++;
  707. if (!is_numeric($pid)) {
  708. unset($display->content[$pid]);
  709. $pane->pid = db_next_id("{panels_pane}_pid");
  710. }
  711. if (empty($pane->cache)) {
  712. $pane->cache = array();
  713. }
  714. $type = panels_get_content_type($pane->type);
  715. $access = isset($pane->access) ? implode(', ', $pane->access) : '';
  716. $visibility = $type['visibility serialize'] ? serialize($pane->visibility) : $pane->visibility;
  717. // doin it this way for readability
  718. $f = 'pid, did, panel, type, subtype, configuration, cache, shown, access, visibility, position';
  719. $q = "%d, %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d";
  720. $pane->shown = isset($pane->shown) ? $pane->shown : TRUE;
  721. $v = array($pane->pid, $display->did, $pane->panel, $pane->type, $pane->subtype, serialize($pane->configuration), serialize($pane->cache), $pane->shown, $access, $visibility, $pane->position);
  722. db_query("INSERT INTO {panels_pane} ($f) VALUES ($q)", $v);
  723. // and put it back so our pids and positions can be used
  724. $display->content[$pane->pid] = $pane;
  725. $new_panes[] = $pane->pid;
  726. }
  727. $display->panels[$id] = $new_panes;
  728. }
  729. // Clear any cached content for this display.
  730. panels_clear_cached_content($display);
  731. // to be nice, even tho we have a reference.
  732. return $display;
  733. }
  734. /**
  735. * Delete a display.
  736. * @ingroup MainAPI
  737. */
  738. function panels_delete_display($display) {
  739. if (is_object($display)) {
  740. $did = $display->did;
  741. }
  742. else {
  743. $did = $display;
  744. }
  745. db_query("DELETE FROM {panels_display} WHERE did = %d", $did);
  746. db_query("DELETE FROM {panels_pane} WHERE did = %d", $did);
  747. }
  748. /**
  749. * Exports the provided display into portable code.
  750. *
  751. * @ingroup MainAPI
  752. *
  753. * This function is primarily intended as a mechanism for cloning displays.
  754. * It generates an exact replica (in code) of the provided $display, with
  755. * the exception that it replaces all ids (dids and pids) with 'new-*' values.
  756. * Only once panels_save_display() is called on the code version of $display will
  757. * the exported display written to the database and permanently saved.
  758. *
  759. * @see panels_page_export() or _panels_page_fetch_display() for sample implementations.
  760. *
  761. * @param object $display instanceof panels_display \n
  762. * This export function does no loading of additional data about the provided
  763. * display. Consequently, the caller should make sure that all the desired data
  764. * has been loaded into the $display before calling this function.
  765. * @param string $prefix
  766. * A string prefix that is prepended to each line of exported code. This is primarily
  767. * used for prepending a double space when exporting so that the code indents and lines up nicely.
  768. *
  769. * @return string $output
  770. * The passed-in $display expressed as code, ready to be imported. Import by running
  771. * eval($output) in the caller function; doing so will create a new $display variable
  772. * with all the exported values. Note that if you have already defined a $display variable in
  773. * the same scope as where you eval(), your existing $display variable WILL be overwritten.
  774. */
  775. function panels_export_display($display, $prefix = '') {
  776. $output = '';
  777. $output .= $prefix . '$display = new panels_display()' . ";\n";
  778. $output .= $prefix . '$display->did = \'new\'' . ";\n";
  779. $fields = array('layout', 'layout_settings', 'panel_settings', 'cache', 'title', 'hide_title');
  780. foreach ($fields as $field) {
  781. if (isset($display->$field)) {
  782. $output .= $prefix . '$display->' . $field . ' = ' . panels_var_export($display->$field, $prefix) . ";\n";
  783. }
  784. }
  785. $output .= $prefix . '$display->content = array()' . ";\n";
  786. $output .= $prefix . '$display->panels = array()' . ";\n";
  787. $panels = array();
  788. if (!empty($display->content)) {
  789. $pid_counter = 0;
  790. $region_counters = array();
  791. foreach ($display->content as $pane) {
  792. $pane->pid = 'new-' . ++$pid_counter;
  793. $output .= panels_export_pane($pane, $prefix . ' ');
  794. $output .= "$prefix " . '$display->content[\'' . $pane->pid . '\'] = $pane' . ";\n";
  795. if (!isset($region_counters[$pane->panel])) {
  796. $region_counters[$pane->panel] = 0;
  797. }
  798. $output .= "$prefix " . '$display->panels[\'' . $pane->panel . '\'][' . $region_counters[$pane->panel]++ .'] = \'' . $pane->pid . "';\n";
  799. }
  800. }
  801. return $output;
  802. }
  803. function panels_export_pane($pane, $prefix = '') {
  804. $output = '';
  805. $output = $prefix . '$pane = new stdClass()' . ";\n";
  806. $fields = array('pid', 'panel', 'type', 'subtype', 'shown', 'access', 'visibility', 'configuration', 'cache');
  807. foreach ($fields as $field) {
  808. $output .= "$prefix " . '$pane->' . $field . ' = ' . panels_var_export($pane->$field, "$prefix ") . ";\n";
  809. }
  810. return $output;
  811. }
  812. function panels_var_export($object, $prefix = '') {
  813. if (is_array($object) && empty($object)) {
  814. $output = 'array()';
  815. }
  816. else {
  817. $output = var_export($object, TRUE);
  818. }
  819. if ($prefix) {
  820. $output = str_replace("\n", "\n$prefix", $output);
  821. }
  822. return $output;
  823. }
  824. /**
  825. * Render a display by loading the content into an appropriate
  826. * array and then passing through to panels_render_layout.
  827. *
  828. * @ingroup MainAPI
  829. * @ingroup HookInvokers
  830. * @ingroup render
  831. *
  832. * if $incoming_content is NULL, default content will be applied. Use
  833. * an empty string to indicate no content.
  834. */
  835. function panels_render_display(&$display) {
  836. panels_load_include('plugins');
  837. $layout = panels_get_layout($display->layout);
  838. if (!$layout) {
  839. return NULL;
  840. }
  841. // TODO: This may not be necessary now. Check this.
  842. panels_sanitize_display($display);
  843. $output = '';
  844. // Let modules act just prior to render.
  845. foreach (module_implements('panels_pre_render') as $module) {
  846. $function = $module . '_panels_pre_render';
  847. $output .= $function($display);
  848. }
  849. $output .= panels_render_layout($layout, $display, $display->css_id, $display->layout_settings);
  850. // Let modules act just after render.
  851. foreach (module_implements('panels_post_render') as $module) {
  852. $function = $module . '_panels_post_render';
  853. $output .= $function($display);
  854. }
  855. return $output;
  856. }
  857. /**
  858. * For external use: Given a layout ID and a $content array, return the
  859. * panel display. The content array is filled in based upon the content
  860. * available in the layout. If it's a two column with a content
  861. * array defined like array('left' => t('Left side'), 'right' =>
  862. * t('Right side')), then the $content array should be array('left' =>
  863. * $output_left, 'right' => $output_right)
  864. * @ingroup render
  865. */
  866. function panels_print_layout($id, $content) {
  867. panels_load_include('plugins');
  868. $layout = panels_get_layout($id);
  869. if (!$layout) {
  870. return;
  871. }
  872. return panels_render_layout($layout, $content);
  873. }
  874. /**
  875. * Given a full layout structure and a content array, render a panel display.
  876. * @ingroup render
  877. */
  878. function panels_render_layout($layout, $content, $css_id = NULL, $settings = array()) {
  879. if (!empty($layout['css'])) {
  880. if (file_exists(path_to_theme() . '/' . $layout['css'])) {
  881. drupal_add_css(path_to_theme() . '/' . $layout['css']);
  882. }
  883. else {
  884. drupal_add_css(panels_get_path($layout['css'], false, $layout['module']));
  885. }
  886. }
  887. $display = NULL;
  888. // This now comes after the CSS is added, because panels-within-panels must
  889. // have their CSS added in the right order; inner content before outer content.
  890. // If $content is an object, it's a $display and we have to render its panes.
  891. if (is_object($content)) {
  892. $display = $content;
  893. if (empty($display->cache['method'])) {
  894. $content = panels_render_panes($display);
  895. }
  896. else {
  897. $cache = panels_get_cached_content($display, $display->args, $display->context);
  898. if ($cache === FALSE) {
  899. $cache = new panels_cache_object();
  900. $cache->set_content(panels_render_panes($display));
  901. panels_set_cached_content($cache, $display, $display->args, $display->context);
  902. }
  903. $content = $cache->content;
  904. }
  905. }
  906. $output = theme($layout['theme'], check_plain($css_id), $content, $settings, $display);
  907. return $output;
  908. }
  909. /**
  910. * Render all the panes in a display into a $content array to be used by
  911. * the display theme function.
  912. */
  913. function panels_render_panes($display) {
  914. // Safety check.
  915. if (empty($display->content)) {
  916. return array();
  917. }
  918. // First, render all the panes into little boxes. We do this here because
  919. // some panes request to be rendered after other panes (primarily so they
  920. // can do the leftovers of forms).
  921. $panes = array();
  922. $later = array();
  923. foreach ($display->content as $pid => $pane) {
  924. $pane->shown = isset($pane->shown) ? $pane->shown : TRUE;
  925. // TODO Really ought to design a method for creating a quick-access set of content_type (and other plugin) data to help optimize render performance
  926. // If the user can't see this pane, do not render it.
  927. if (!$pane->shown || !panels_pane_access($pane, $display)) {
  928. continue;
  929. }
  930. // If this pane wants to render last, add it to the $later array.
  931. $content_type = panels_get_content_type($pane->type);
  932. if (!empty($content_type['render last'])) {
  933. $later[$pid] = $pane;
  934. continue;
  935. }
  936. $panes[$pid] = panels_render_pane_content($display, $pane);
  937. }
  938. foreach ($later as $pid => $pane) {
  939. $panes[$pid] = panels_render_pane_content($display, $pane);
  940. }
  941. // Loop through all panels, put all panes that belong to the current panel
  942. // in an array, then render the panel. Primarily this ensures that the
  943. // panes are in the proper order.
  944. $content = array();
  945. foreach ($display->panels as $panel_name => $pids) {
  946. $panel = array();
  947. foreach ($pids as $pid) {
  948. if (!empty($panes[$pid])) {
  949. $panel[$pid] = $panes[$pid];
  950. }
  951. }
  952. $content[$panel_name] = panels_render_panel($display, $panel_name, $panel);
  953. }
  954. return $content;
  955. }
  956. /**
  957. * Render a single pane, identifying its context, and put it into
  958. * the $panes array.
  959. */
  960. function panels_render_pane_content(&$display, &$pane) {
  961. if (empty($pane->context)) {
  962. $pane->context = panels_pane_select_context($pane, $display->context);
  963. if ($pane->context === FALSE) {
  964. return FALSE;
  965. }
  966. }
  967. $content = panels_get_pane_content($display, $pane, $display->args, $pane->context, $display->incoming_content);
  968. $keywords = !empty($display->keywords) ? $display->keywords : array();
  969. // Override the title if configured to
  970. if (!empty($pane->configuration['override_title'])) {
  971. // Give previous title as an available substitution here.
  972. $keywords['%title'] = $content->title;
  973. $content->title = $pane->configuration['override_title_text'];
  974. }
  975. // Pass long the css_id that is usually available.
  976. if (!empty($pane->configuration['css_id'])) {
  977. $content->css_id = $pane->configuration['css_id'];
  978. }
  979. // Pass long the css_class that is usually available.
  980. if (!empty($pane->configuration['css_class'])) {
  981. $content->css_class = $pane->configuration['css_class'];
  982. }
  983. if (!empty($content->title)) {
  984. // Perform substitutions
  985. if (!empty($keywords)) {
  986. $content->title = strtr($content->title, $keywords);
  987. }
  988. // Sterilize the title
  989. $content->title = filter_xss_admin($content->title);
  990. // If a link is specified, populate.
  991. if (!empty($content->title_link)) {
  992. if (!is_array($content->title_link)) {
  993. $url = array('href' => $content->title_link);
  994. }
  995. else {
  996. $url = $content->title_link;
  997. }
  998. // set defaults so we don't bring up notices
  999. $url += array('href' => '', 'attributes' => NULL, 'query' => NULL, 'fragment' => NULL, 'absolute' => NULL);
  1000. $content->title = l($content->title,
  1001. $url['href'],
  1002. $url['attributes'],
  1003. $url['query'],
  1004. $url['fragment'],
  1005. $url['absolute'], TRUE);
  1006. }
  1007. }
  1008. return $content;
  1009. }
  1010. /**
  1011. * Render a pane using the appropriate style.
  1012. *
  1013. * $content
  1014. * The already rendered content via panels_render_pane_content()
  1015. * $pane
  1016. * The $pane information from the display
  1017. * $display
  1018. * The display.
  1019. */
  1020. function panels_render_pane($content, $pane, $display) {
  1021. if (!empty($pane->configuration['style'])) {
  1022. $style = panels_get_style($pane->configuration['style']);
  1023. if (isset($style)) {
  1024. $output = theme($style['render pane'], $content, $pane, $display);
  1025. // This could be null if no theme function existed.
  1026. if (isset($output)) {
  1027. return $output;
  1028. }
  1029. }
  1030. }
  1031. if (!empty($content)) {
  1032. // fallback
  1033. return theme('panels_pane', $content, $pane, $display);
  1034. }
  1035. }
  1036. /**
  1037. * Given a display and the id of a panel, get the style in which to render
  1038. * that panel.
  1039. */
  1040. function panels_get_panel_style_and_settings($panel_settings, $panel) {
  1041. if (empty($panel_settings)) {
  1042. return array(panels_get_style('default'), array());
  1043. }
  1044. if (empty($panel_settings['individual']) || empty($panel_settings['panel'][$panel]['style'])) {
  1045. $style = panels_get_style($panel_settings['style']);
  1046. $style_settings = $panel_settings['style_settings']['default'];
  1047. }
  1048. else {
  1049. $style = panels_get_style($panel_settings['panel'][$panel]['style']);
  1050. $style_settings = $panel_settings['style_settings'][$panel];
  1051. }
  1052. return array($style, $style_settings);
  1053. }
  1054. /**
  1055. * Render a panel, by storing the content of each pane in an appropriate array
  1056. * and then passing through to the theme function that will render the panel
  1057. * in the configured panel style.
  1058. *
  1059. * @param $display
  1060. * A display object.
  1061. * @param $panel
  1062. * The ID of the panel being rendered
  1063. * @param $panes
  1064. * An array of panes that are assigned to the panel that's being rendered.
  1065. *
  1066. * @return
  1067. * The rendered HTML for a panel.
  1068. * @ingroup render
  1069. */
  1070. function panels_render_panel($display, $panel, $panes) {
  1071. list($style, $style_settings) = panels_get_panel_style_and_settings($display->panel_settings, $panel);
  1072. // Retrieve the pid (can be a panel page id, a mini panel id, etc.), this
  1073. // might be used (or even necessary) for some panel display styles.
  1074. // TODO: Got to fix this to use panel page name instead of pid, since pid is
  1075. // no longer guaranteed. This needs an API to be able to set the final id.
  1076. $owner_id = 0;
  1077. if (isset($display->owner) && is_object($display->owner) && isset($display->owner->id)) {
  1078. $owner_id = $display->owner->id;
  1079. }
  1080. return theme($style['render panel'], $display, $owner_id, $panes, $style_settings, $panel);
  1081. }
  1082. /**
  1083. * Print the layout link. Sends out to a theme function.
  1084. * @layout
  1085. */
  1086. function panels_print_layout_link($id, $layout, $link) {
  1087. drupal_add_css(panels_get_path('css/panels_admin.css'));
  1088. $file = panels_get_path($layout['icon'], false, $layout['module']);
  1089. $image = l(theme('image', $file), $link, NULL, NULL, NULL, NULL, TRUE);
  1090. $title = l($layout['title'], $link);
  1091. return theme('panels_layout_link', $title, $id, $image, $link);
  1092. }
  1093. // @layout
  1094. function panels_print_layout_icon($id, $layout, $title = NULL) {
  1095. drupal_add_css(panels_get_path('css/panels_admin.css'));
  1096. $file = panels_get_path($layout['icon'], false, $layout['module']);
  1097. return theme('panels_layout_icon', $id, theme('image', $file), $title);
  1098. }
  1099. /**
  1100. * Theme the layout link image
  1101. * @layout
  1102. */
  1103. function theme_panels_layout_link($title, $id, $image, $link) {
  1104. $output .= '<div class="layout-link">';
  1105. $output .= $image;
  1106. $output .= '<div>' . $title . '</div>';
  1107. $output .= '</div>';
  1108. return $output;
  1109. }
  1110. /**
  1111. * Theme the layout icon image
  1112. * @layout
  1113. */
  1114. function theme_panels_layout_icon($id, $image, $title = NULL) {
  1115. $output .= '<span class="layout-icon">';
  1116. $output .= $image;
  1117. if ($title) {
  1118. $output .= '<span class="caption">' . $title . '</span>';
  1119. }
  1120. $output .= '</span>';
  1121. return $output;
  1122. }
  1123. /**
  1124. * Render a panel pane like a block.
  1125. *
  1126. * A panel pane can have the following fields:
  1127. *
  1128. * - $pane->type -- the content type inside this pane
  1129. * - $pane->subtype -- The subtype, if applicable. If a view it will be the
  1130. * view name; if a node it will be the nid, etc.
  1131. * - $content->title -- The title of the content
  1132. * - $content->content -- The actual content
  1133. * - $content->links -- Any links associated with the content
  1134. * - $content->more -- An optional 'more' link (destination only)
  1135. * - $content->admin_links -- Administrative links associated with the content
  1136. * - $content->feeds -- Any feed icons or associated with the content
  1137. * - $content->subject -- A legacy setting for block compatibility
  1138. * - $content->module -- A legacy setting for block compatibility
  1139. * - $content->delta -- A legacy setting for block compatibility
  1140. */
  1141. function theme_panels_pane($content, $pane, $display) {
  1142. if (!empty($content->content)) {
  1143. $idstr = $classstr = '';
  1144. if (!empty($content->css_id)) {
  1145. $idstr = ' id="' . $content->css_id . '"';
  1146. }
  1147. if (!empty($content->css_class)) {
  1148. $classstr = ' ' . $content->css_class;
  1149. }
  1150. $output = "<div class=\"panel-pane$classstr\"$idstr>\n";
  1151. if (user_access('view pane admin links') && !empty($content->admin_links)) {
  1152. $output .= "<div class=\"admin-links panel-hide\">" . theme('links', $content->admin_links) . "</div>\n";
  1153. }
  1154. if (!empty($content->title)) {
  1155. $output .= "<h2 class=\"title\">$content->title</h2>\n";
  1156. }
  1157. if (!empty($content->feeds)) {
  1158. $output .= "<div class=\"feed\">" . implode(' ', $content->feeds) . "</div>\n";
  1159. }
  1160. $output .= "<div class=\"content\">$content->content</div>\n";
  1161. if (!empty($content->links)) {
  1162. $output .= "<div class=\"links\">" . theme('links', $content->links) . "</div>\n";
  1163. }
  1164. if (!empty($content->more)) {
  1165. if (empty($content->more['title'])) {
  1166. $content->more['title'] = t('more');
  1167. }
  1168. $output .= "<div class=\"more-link\">" . l($content->more['title'], $content->more['href']) . "</div>\n";
  1169. }
  1170. $output .= "</div>\n";
  1171. return $output;
  1172. }
  1173. }
  1174. /**
  1175. * Helper function for parsing an autocomplete node field.
  1176. *
  1177. * @param $string
  1178. * A string in autocomplete syntax (e.g. ".... [nid: 123]"), or a
  1179. * typed-in nid, or a node title.
  1180. *
  1181. * @return
  1182. * Either a valid nid or NULL.
  1183. */
  1184. function panels_nid_autocomplete($int) {
  1185. $nid = NULL;
  1186. if (is_numeric($int)) {
  1187. // The user typed a NID outright.
  1188. $nid = $int;
  1189. }
  1190. else {
  1191. // Else, it might be an autocomplete syntax.
  1192. $preg_matches = array();
  1193. $match = preg_match('/\[nid: (\d+)\]/', $int, $preg_matches)
  1194. || preg_match('/^nid: (\d+)/', $int, $preg_matches);
  1195. if ($match) {
  1196. $nid = $preg_matches[1];
  1197. }
  1198. }
  1199. if (isset($nid)) {
  1200. // Verify that node exists and we have access to it.
  1201. $node = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.nid = %d"), $nid));
  1202. }
  1203. else {
  1204. // Try to find a node having that title.
  1205. $node = db_fetch_object(db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE LOWER(n.title) = LOWER('%s')"), $int));
  1206. }
  1207. if ($node) {
  1208. return $node->nid;
  1209. }
  1210. }
  1211. /**
  1212. * Helper function for autocompletion of node titles.
  1213. * This is mostly stolen from clipper.
  1214. */
  1215. function panels_node_autocomplete($string) {
  1216. // TODO: Compare this to the nodequeue version, see which is better.
  1217. // TODO: The nodequeue version is totally better. Steal it.
  1218. // if there are node_types passed, we'll use those in a MySQL IN query.
  1219. if ($string != '') {
  1220. $preg_matches = array();
  1221. $match = preg_match('/\[nid: (\d+)\]/', $string, $preg_matches);
  1222. if (!$match) {
  1223. $match = preg_match('/^nid: (\d+)/', $string, $preg_matches);
  1224. }
  1225. if ($match) {
  1226. $arg = $preg_matches[1];
  1227. $where = "n.nid = %d";
  1228. }
  1229. else {
  1230. $arg = $string;
  1231. $where = "LOWER(title) LIKE LOWER('%%%s%%')";
  1232. }
  1233. $result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, u.name FROM {node} n INNER JOIN {users} u ON u.uid = n.uid WHERE $where"), $arg, 0, 10);
  1234. $matches = array();
  1235. while ($node = db_fetch_object($result)) {
  1236. $name = empty($node->name) ? variable_get('anonymous', t('Anonymous')) : check_plain($node->name);
  1237. $matches[$node->title . " [nid: $node->nid]"] = '<span class="autocomplete_title">'. check_plain($node->title) .'</span> <span class="autocomplete_user">('. t('by @user', array('@user' => $name)) .')</span>';
  1238. }
  1239. drupal_set_header('Content-Type: text/javascript; charset=utf-8');
  1240. print drupal_to_js($matches);
  1241. }
  1242. }
  1243. /**
  1244. * Implementation of hook_node_type().
  1245. *
  1246. * We implement this hook to update any pane contexts that are reliant on a
  1247. * specific node type with the new type name.
  1248. */
  1249. function panels_node_type($op, $info) {
  1250. if ($op == 'update') {
  1251. if (!empty($info->old_type) && $info->old_type != $info->type) {
  1252. // Exclude a few common pane types that we know don't use context.
  1253. $result = db_query("SELECT * FROM {panels_pane} WHERE type NOT IN ('block', 'custom')");
  1254. while ($pane = db_fetch_object($result)) {
  1255. // Check the serialized data for the presence of context data.
  1256. if (!preg_match('/s:7:\"context/', $pane->configuration) || stripos($pane->configuration, 'node-' . $info->old_type) === FALSE) {
  1257. // There's no context/no mention of our node type - next!
  1258. continue;
  1259. }
  1260. $conf = unserialize($pane->configuration);
  1261. // Manage panes with multiple contexts stored in an array.
  1262. if (is_array($conf['context'] && ($keys = array_keys($conf['context'], 'node-' . $info->old_type)))) {
  1263. foreach ($keys as $key) {
  1264. $conf['context'][$key] = 'node-' . $info->type;
  1265. }
  1266. db_query("UPDATE {panels_pane} SET configuration = '%s' WHERE pid = %d", serialize($conf), $pane->pid);
  1267. }
  1268. // Manage single-context panes.
  1269. else if ($conf['context'] == 'node-' . $info->old_type) {
  1270. $conf['context'] = 'node-' . $info->type;
  1271. db_query("UPDATE {panels_pane} SET configuration = '%s' WHERE pid = %d", serialize($conf), $pane->pid);
  1272. }
  1273. }
  1274. }
  1275. }
  1276. }