panels.module 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  1. <?php
  2. /**
  3. * @file panels.module
  4. *
  5. * Core functionality for the Panels engine.
  6. */
  7. define('PANELS_REQUIRED_CTOOLS_API', '2.0-alpha');
  8. define('PANELS_TITLE_FIXED', 0); // Hide title use to be true/false. So false remains old behavior.
  9. define('PANELS_TITLE_NONE', 1); // And true meant no title.
  10. define('PANELS_TITLE_PANE', 2); // And this is the new behavior, where the title field will pick from a pane.
  11. /**
  12. * Returns the API version of Panels. This didn't exist in 1.
  13. *
  14. * @todo -- this should work more like the CTools API version.
  15. *
  16. * @return An array with the major and minor versions
  17. */
  18. function panels_api_version() {
  19. return array(3, 1);
  20. }
  21. // --------------------------------------------------------------------------
  22. // Core Drupal hook implementations
  23. /**
  24. * Implementation of hook_theme()
  25. */
  26. function panels_theme() {
  27. // Safety: go away if CTools is not at an appropriate version.
  28. if (!module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
  29. return array();
  30. }
  31. $theme = array();
  32. $theme['panels_layout_link'] = array(
  33. 'variables' => array('title' => NULL, 'id' => NULL, 'image' => NULL, 'link' => NULL, 'class' => NULL),
  34. );
  35. $theme['panels_layout_icon'] = array(
  36. 'variables' => array('id' => NULL, 'image' => NULL, 'title' => NULL),
  37. );
  38. $theme['panels_pane'] = array(
  39. 'variables' => array('output' => array(), 'pane' => array(), 'display' => array()),
  40. 'path' => drupal_get_path('module', 'panels') . '/templates',
  41. 'template' => 'panels-pane',
  42. );
  43. $theme['panels_common_content_list'] = array(
  44. 'variables' => array('display' => NULL),
  45. 'file' => 'includes/common.inc',
  46. );
  47. $theme['panels_render_display_form'] = array(
  48. 'render element' => 'element',
  49. );
  50. $theme['panels_dashboard'] = array(
  51. 'variables' => array(),
  52. 'path' => drupal_get_path('module', 'panels') . '/templates',
  53. 'file' => '../includes/callbacks.inc',
  54. 'template' => 'panels-dashboard',
  55. );
  56. $theme['panels_dashboard_link'] = array(
  57. 'variables' => array('link' => array()),
  58. 'path' => drupal_get_path('module', 'panels') . '/templates',
  59. 'file' => '../includes/callbacks.inc',
  60. 'template' => 'panels-dashboard-link',
  61. );
  62. $theme['panels_dashboard_block'] = array(
  63. 'variables' => array('block' => array()),
  64. 'path' => drupal_get_path('module', 'panels') . '/templates',
  65. 'file' => '../includes/callbacks.inc',
  66. 'template' => 'panels-dashboard-block',
  67. );
  68. $theme['panels_add_content_modal'] = array(
  69. 'variables' => array('renderer' => NULL, 'categories' => array(), 'region' => NULL, 'category' => NULL, 'column_count' => 2),
  70. 'path' => drupal_get_path('module', 'panels') . '/templates',
  71. 'file' => '../includes/add-content.inc',
  72. 'template' => 'panels-add-content-modal',
  73. );
  74. $theme['panels_add_content_link'] = array(
  75. 'variables' => array('renderer' => NULL, 'region' => NULL, 'content_type' => NULL),
  76. 'path' => drupal_get_path('module', 'panels') . '/templates',
  77. 'file' => '../includes/add-content.inc',
  78. 'template' => 'panels-add-content-link',
  79. );
  80. // We don't need layout and style themes in maintenance mode.
  81. // Disabling this: See http://drupal.org/node/979912 for information.
  82. // if (defined('MAINTENANCE_MODE')) {
  83. // return $theme;
  84. // }
  85. // Register layout and style themes on behalf of all of these items.
  86. ctools_include('plugins', 'panels');
  87. // No need to worry about files; the plugin has to already be loaded for us
  88. // to even know what the theme function is, so files will be auto included.
  89. $layouts = panels_get_layouts();
  90. foreach ($layouts as $name => $data) {
  91. foreach (array('theme', 'admin theme') as $callback) {
  92. if (!empty($data[$callback])) {
  93. $theme[$data[$callback]] = array(
  94. 'variables' => array('css_id' => NULL, 'content' => NULL, 'settings' => NULL, 'display' => NULL, 'layout' => NULL, 'renderer' => NULL),
  95. 'path' => $data['path'],
  96. 'file' => $data['file'],
  97. );
  98. // if no theme function exists, assume template.
  99. if (!function_exists("theme_$data[theme]")) {
  100. $theme[$data[$callback]]['template'] = str_replace('_', '-', $data[$callback]);
  101. $theme[$data[$callback]]['file'] = $data['file']; // for preprocess.
  102. }
  103. }
  104. }
  105. }
  106. $styles = panels_get_styles();
  107. foreach ($styles as $name => $data) {
  108. if (!empty($data['render pane'])) {
  109. $theme[$data['render pane']] = array(
  110. 'variables' => array('content' => NULL, 'pane' => NULL, 'display' => NULL, 'style' => NULL, 'settings' => NULL),
  111. 'path' => $data['path'],
  112. 'file' => $data['file'],
  113. );
  114. }
  115. if (!empty($data['render region'])) {
  116. $theme[$data['render region']] = array(
  117. 'variables' => array('display' => NULL, 'owner_id' => NULL, 'panes' => NULL, 'settings' => NULL, 'region_id' => NULL, 'style' => NULL),
  118. 'path' => $data['path'],
  119. 'file' => $data['file'],
  120. );
  121. }
  122. if (!empty($data['hook theme'])) {
  123. if (is_array($data['hook theme'])) {
  124. $theme += $data['hook theme'];
  125. }
  126. else if (function_exists($data['hook theme'])) {
  127. $data['hook theme']($theme, $data);
  128. }
  129. }
  130. }
  131. return $theme;
  132. }
  133. /**
  134. * Implementation of hook_menu
  135. */
  136. function panels_menu() {
  137. // Safety: go away if CTools is not at an appropriate version.
  138. if (!module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
  139. return array();
  140. }
  141. $items = array();
  142. // Base AJAX router callback.
  143. $items['panels/ajax'] = array(
  144. 'access arguments' => array('access content'),
  145. 'page callback' => 'panels_ajax_router',
  146. 'theme callback' => 'ajax_base_page_theme',
  147. 'delivery callback' => 'ajax_deliver',
  148. 'type' => MENU_CALLBACK,
  149. );
  150. $admin_base = array(
  151. 'file' => 'includes/callbacks.inc',
  152. 'access arguments' => array('use panels dashboard'),
  153. );
  154. // Provide a nice location for a panels admin panel.
  155. $items['admin/structure/panels'] = array(
  156. 'title' => 'Panels',
  157. 'page callback' => 'panels_admin_page',
  158. 'description' => 'Get a bird\'s eye view of items related to Panels.',
  159. ) + $admin_base;
  160. $items['admin/structure/panels/dashboard'] = array(
  161. 'title' => 'Dashboard',
  162. 'page callback' => 'panels_admin_page',
  163. 'type' => MENU_DEFAULT_LOCAL_TASK,
  164. 'weight' => -10,
  165. ) + $admin_base;
  166. $items['admin/structure/panels/settings'] = array(
  167. 'title' => 'Settings',
  168. 'page callback' => 'drupal_get_form',
  169. 'page arguments' => array('panels_admin_settings_page'),
  170. 'type' => MENU_LOCAL_TASK,
  171. ) + $admin_base;
  172. $items['admin/structure/panels/settings/general'] = array(
  173. 'title' => 'General',
  174. 'page callback' => 'drupal_get_form',
  175. 'page arguments' => array('panels_admin_settings_page'),
  176. 'access arguments' => array('administer page manager'),
  177. 'type' => MENU_DEFAULT_LOCAL_TASK,
  178. 'weight' => -10,
  179. ) + $admin_base;
  180. if (module_exists('page_manager')) {
  181. $items['admin/structure/panels/settings/panel-page'] = array(
  182. 'title' => 'Panel pages',
  183. 'page callback' => 'panels_admin_panel_context_page',
  184. 'type' => MENU_LOCAL_TASK,
  185. 'weight' => -10,
  186. ) + $admin_base;
  187. }
  188. ctools_include('plugins', 'panels');
  189. $layouts = panels_get_layouts();
  190. foreach ($layouts as $name => $data) {
  191. if (!empty($data['hook menu'])) {
  192. if (is_array($data['hook menu'])) {
  193. $items += $data['hook menu'];
  194. }
  195. else if (function_exists($data['hook menu'])) {
  196. $data['hook menu']($items, $data);
  197. }
  198. }
  199. }
  200. return $items;
  201. }
  202. /**
  203. * Menu loader function to load a cache item for Panels AJAX.
  204. *
  205. * This load all of the includes needed to perform AJAX, and loads the
  206. * cache object and makes sure it is valid.
  207. */
  208. function panels_edit_cache_load($cache_key) {
  209. ctools_include('display-edit', 'panels');
  210. ctools_include('plugins', 'panels');
  211. ctools_include('ajax');
  212. ctools_include('modal');
  213. ctools_include('context');
  214. return panels_edit_cache_get($cache_key);
  215. }
  216. /**
  217. * Implementation of hook_init()
  218. */
  219. function panels_init() {
  220. // Safety: go away if CTools is not at an appropriate version.
  221. if (!module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
  222. if (user_access('administer site configuration')) {
  223. drupal_set_message(t('Panels is enabled but CTools is out of date. All Panels modules are disabled until CTools is updated. See the status page for more information.'), 'error');
  224. }
  225. return;
  226. }
  227. ctools_add_css('panels', 'panels');
  228. ctools_add_js('panels', 'panels');
  229. }
  230. /**
  231. * Implementation of hook_permission().
  232. *
  233. * @todo Almost all of these need to be moved into pipelines.
  234. */
  235. function panels_permission() {
  236. return array(
  237. 'use panels dashboard' => array(
  238. 'title' => t("Use Panels Dashboard"),
  239. 'description' => t("Allows a user to access the !link.", array('!link' => l('Panels Dashboard', 'admin/structure/panels'))),
  240. ),
  241. 'view pane admin links' => array( // @todo
  242. 'title' => t("View administrative links on Panel panes"),
  243. 'description' => t(""),
  244. ),
  245. 'administer pane access' => array( // @todo should we really have a global perm for this, or should it be moved into a pipeline question?
  246. 'title' => t("Configure access settings on Panel panes"),
  247. 'description' => t("Access rules (often also called visibility rules) can be configured on a per-pane basis. This permission allows users to configure those settings."),
  248. ),
  249. 'use panels in place editing' => array(
  250. 'title' => t("Use the Panels In-Place Editor"),
  251. 'description' => t("Allows a user to utilize Panels' In-Place Editor."),
  252. ),
  253. 'change layouts in place editing' => array(
  254. 'title' => t("Change layouts with the Panels In-Place Editor"),
  255. 'description' => t("Allows a user to change layouts with the IPE."),
  256. ),
  257. 'administer advanced pane settings' => array(
  258. 'title' => t("Configure advanced settings on Panel panes"),
  259. 'description' => t(""),
  260. ),
  261. 'administer panels layouts' => array(
  262. 'title' => t("Administer Panels layouts"),
  263. 'description' => t("Allows a user to administer exported Panels layout plugins & instances."),
  264. ),
  265. 'administer panels styles' => array(
  266. 'title' => t("Administer Panels styles"),
  267. 'description' => t("Allows a user to administer the styles of Panel panes."),
  268. ),
  269. 'use panels caching features' => array(
  270. 'title' => t("Configure caching settings on Panels"),
  271. 'description' => t("Allows a user to configure caching on Panels displays and panes."),
  272. ),
  273. 'use panels locks' => array(
  274. 'title' => t('Use panel locks'),
  275. 'description' => t('Allows a user to lock and unlock panes in a panel display.'),
  276. ),
  277. 'use ipe with page manager' => array(
  278. 'title' => t("Use the Panels In-Place Editor with Page Manager"),
  279. 'description' => t('Allows users with access to the In-Place editor to administer page manager pages. This permission is only needed for users without "use page manager" access.'),
  280. ),
  281. );
  282. }
  283. /**
  284. * Implementation of hook_flush_caches().
  285. *
  286. * We implement this so that we can be sure our legacy rendering state setting
  287. * in $conf is updated whenever caches are cleared.
  288. */
  289. //function panels_flush_caches() {
  290. // $legacy = panels_get_legacy_state();
  291. // $legacy->determineStatus();
  292. //}
  293. // ---------------------------------------------------------------------------
  294. // CTools hook implementations
  295. //
  296. // These aren't core Drupal hooks but they are just as important.
  297. /**
  298. * Implementation of hook_ctools_plugin_directory() to let the system know
  299. * we implement task and task_handler plugins.
  300. */
  301. function panels_ctools_plugin_directory($module, $plugin) {
  302. // Safety: go away if CTools is not at an appropriate version.
  303. if (!module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
  304. return;
  305. }
  306. // We don't support the 'ctools' 'cache' plugin and pretending to causes
  307. // errors when they're in use.
  308. if ($module == 'ctools' && $plugin == 'cache') {
  309. return;
  310. // if we did we'd make a plugin/ctools_cache or something.
  311. }
  312. if ($module == 'page_manager' || $module == 'panels' || $module == 'ctools' || $module == 'stylizer') {
  313. // Panels and CTools both implement a 'cache' plugin but we don't implement
  314. // the CTools version.
  315. if ($module == 'ctools' && $plugin == 'cache') {
  316. return;
  317. }
  318. return 'plugins/' . $plugin;
  319. }
  320. }
  321. /**
  322. * Implements hook_ctools_plugin_type().
  323. *
  324. * Register layout, style, cache, and display_renderer plugin types, declaring
  325. * relevant plugin type information as necessary.
  326. */
  327. function panels_ctools_plugin_type() {
  328. return array(
  329. 'layouts' => array(
  330. 'load themes' => TRUE, // Can define layouts in themes
  331. 'process' => 'panels_layout_process',
  332. 'child plugins' => TRUE,
  333. ),
  334. 'styles' => array(
  335. 'load themes' => TRUE,
  336. 'process' => 'panels_plugin_styles_process',
  337. 'child plugins' => TRUE,
  338. ),
  339. 'cache' => array(),
  340. 'display_renderers' => array(
  341. 'classes' => array('renderer'),
  342. ),
  343. );
  344. }
  345. /**
  346. * Ensure a layout has a minimal set of data.
  347. */
  348. function panels_layout_process(&$plugin) {
  349. $plugin += array(
  350. 'category' => t('Miscellaneous'),
  351. 'description' => '',
  352. );
  353. }
  354. /**
  355. * Implementation of hook_ctools_plugin_api().
  356. *
  357. * Inform CTools about version information for various plugins implemented by
  358. * Panels.
  359. *
  360. * @param string $owner
  361. * The system name of the module owning the API about which information is
  362. * being requested.
  363. * @param string $api
  364. * The name of the API about which information is being requested.
  365. */
  366. function panels_ctools_plugin_api($owner, $api) {
  367. if ($owner == 'panels' && $api == 'styles') {
  368. // As of 6.x-3.6, Panels has a slightly new system for style plugins.
  369. return array('version' => 2.0);
  370. }
  371. if ($owner == 'panels' && $api == 'pipelines') {
  372. return array(
  373. 'version' => 1,
  374. 'path' => drupal_get_path('module', 'panels') . '/includes',
  375. );
  376. }
  377. }
  378. /**
  379. * Implementation of hook_views_api().
  380. */
  381. function panels_views_api() {
  382. return array(
  383. 'api' => 2,
  384. 'path' => drupal_get_path('module', 'panels') . '/plugins/views',
  385. );
  386. }
  387. /**
  388. * Perform additional processing on a style plugin.
  389. *
  390. * Currently this is only being used to apply versioning information to style
  391. * plugins in order to ensure the legacy renderer passes the right type of
  392. * parameters to a style plugin in a hybrid environment of both new and old
  393. * plugins.
  394. *
  395. * @see _ctools_process_data()
  396. *
  397. * @param array $plugin
  398. * The style plugin that is being processed.
  399. * @param array $info
  400. * The style plugin type info array.
  401. */
  402. function panels_plugin_styles_process(&$plugin, $info) {
  403. $plugin += array(
  404. 'weight' => 0,
  405. );
  406. $compliant_modules = ctools_plugin_api_info('panels', 'styles', 2.0, 2.0);
  407. $plugin['version'] = empty($compliant_modules[$plugin['module']]) ? 1.0 : $compliant_modules[$plugin['module']]['version'];
  408. }
  409. /**
  410. * Declare what style types Panels uses.
  411. */
  412. function panels_ctools_style_base_types() {
  413. return array(
  414. 'region' => array(
  415. 'title' => t('Panel region'),
  416. 'preview' => 'panels_stylizer_region_preview',
  417. 'theme variables' => array('settings' => NULL, 'class' => NULL, 'content' => NULL),
  418. ),
  419. 'pane' => array(
  420. 'title' => t('Panel pane'),
  421. 'preview' => 'panels_stylizer_pane_preview',
  422. 'theme variables' => array('settings' => NULL, 'content' => NULL, 'pane' => NULL, 'display' => NULL),
  423. ),
  424. );
  425. }
  426. function panels_stylizer_lipsum() {
  427. return "
  428. <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus at velit dolor. Donec egestas tellus sit amet urna rhoncus adipiscing. Proin nec porttitor sem. Maecenas aliquam, purus nec tempus dignissim, nulla arcu aliquam diam, non tincidunt massa ante vel dolor. Aliquam sapien sapien, tincidunt id tristique at, pretium sagittis libero.</p>
  429. <p>Nulla facilisi. Curabitur lacinia, tellus sed tristique consequat, diam lorem scelerisque felis, at dictum purus augue facilisis lorem. Duis pharetra dignissim rutrum. Curabitur ac elit id dui dapibus tincidunt. Nulla eget sem quam, non eleifend eros. Cras porttitor tempus lectus ac scelerisque. Curabitur vehicula bibendum lorem, vitae ornare ligula venenatis ut.</p>
  430. ";
  431. }
  432. /**
  433. * Generate a preview given the current settings.
  434. */
  435. function panels_stylizer_region_preview($plugin, $settings) {
  436. ctools_stylizer_add_css($plugin, $settings);
  437. return theme($plugin['theme'], array('settings' => $settings, 'class' => ctools_stylizer_get_css_class($plugin, $settings), 'content' => panels_stylizer_lipsum()));
  438. }
  439. /**
  440. * Generate a preview given the current settings.
  441. */
  442. function panels_stylizer_pane_preview($plugin, $settings) {
  443. ctools_stylizer_add_css($plugin, $settings);
  444. $pane = new stdClass();
  445. $content = new stdClass;
  446. $content->title = t('Lorem ipsum');
  447. $content->content = panels_stylizer_lipsum();
  448. $content->type = 'dummy';
  449. $content->subtype = 'dummy';
  450. $content->css_class = ctools_stylizer_get_css_class($plugin, $settings);
  451. $display = new panels_display();
  452. if (!empty($plugin['theme'])) {
  453. return theme($plugin['theme'], array('settings' => $settings, 'content' => $content, 'pane' => $pane, 'display' => $display));
  454. }
  455. else {
  456. return theme('panels_pane', array('content' => $content, 'pane' => $pane, 'display' => $display));
  457. }
  458. }
  459. // ---------------------------------------------------------------------------
  460. // Panels display editing
  461. /**
  462. * @defgroup mainapi Functions comprising the main panels API
  463. * @{
  464. */
  465. /**
  466. * Main API entry point to edit a panel display.
  467. *
  468. * Sample implementations utiltizing the the complex $destination behavior can be found
  469. * in panels_page_edit_content() and, in a separate contrib module, OG Blueprints
  470. * (http://drupal.org/project/og_blueprints), og_blueprints_blueprint_edit().
  471. *
  472. * @ingroup mainapi
  473. *
  474. * @param object $display instanceof panels_display \n
  475. * A fully loaded panels $display object, as returned from panels_load_display().
  476. * Merely passing a did is NOT sufficient. \n
  477. * Note that 'fully loaded' means the $display must already be loaded with any contexts
  478. * the caller wishes to have set for the display.
  479. * @param mixed $destination \n
  480. * The redirect destination that the user should be taken to on form submission or
  481. * cancellation. With panels_edit, $destination has complex effects on the return
  482. * values of panels_edit() once the form has been submitted. See the explanation of
  483. * the return value below to understand the different types of values returned by panels_edit()
  484. * at different stages of FAPI. Under most circumstances, simply passing in
  485. * drupal_get_destination() is all that's necessary.
  486. * @param array $content_types \n
  487. * An associative array of allowed content types, typically as returned from
  488. * panels_common_get_allowed_types(). Note that context partially governs available content types,
  489. * so you will want to create any relevant contexts using panels_create_context() or
  490. * panels_create_context_empty() to make sure all the appropriate content types are available.
  491. *
  492. * @return
  493. * Because the functions called by panels_edit() invoke the form API, this function
  494. * returns different values depending on the stage of form submission we're at. In Drupal 5,
  495. * the phase of form submission is indicated by the contents of $_POST['op']. Here's what you'll
  496. * get at different stages:
  497. * -# If !$_POST['op']: then we're on on the initial passthrough and the form is being
  498. * rendered, so it's the $form itself that's being returned. Because negative margins,
  499. * a common CSS technique, bork the display editor's ajax drag-and-drop, it's important
  500. * that the $output be printed, not returned. Use this syntax in the caller function: \n
  501. * print theme('page', panels_edit($display, $destination, $content_types), FALSE); \n
  502. * -# If $_POST['op'] == t('Cancel'): form submission has been cancelled. If empty($destination) == FALSE,
  503. * then there is no return value and the panels API takes care of redirecting to $destination.
  504. * If empty($destination) == TRUE, then there's still no return value, but the caller function
  505. * has to take care of form redirection.
  506. * -# If $_POST['op'] == ('Save'): the form has been submitted successfully and has run through
  507. * panels_edit_display_submit(). $output depends on the value of $destination:
  508. * - If empty($destination) == TRUE: $output contains the modified $display
  509. * object, and no redirection will occur. This option is useful if the caller
  510. * needs to perform additional operations on or with the modified $display before
  511. * the page request is complete. Using hook_form_alter() to add an additional submit
  512. * handler is typically the preferred method for something like this, but there
  513. * are certain use cases where that is infeasible and $destination = NULL should
  514. * be used instead. If this method is employed, the caller will need to handle form
  515. * redirection. Note that having $_REQUEST['destination'] set, whether via
  516. * drupal_get_destination() or some other method, will NOT interfere with this
  517. * functionality; consequently, you can use drupal_get_destination() to safely store
  518. * your desired redirect in the caller function, then simply use drupal_goto() once
  519. * panels_edit() has done its business.
  520. * - If empty($destination) == FALSE: the form will redirect to the URL string
  521. * given in $destination and NO value will be returned.
  522. */
  523. function panels_edit($display, $destination = NULL, $content_types = NULL, $title = FALSE) {
  524. ctools_include('display-edit', 'panels');
  525. ctools_include('ajax');
  526. ctools_include('plugins', 'panels');
  527. return _panels_edit($display, $destination, $content_types, $title);
  528. }
  529. /**
  530. * API entry point for selecting a layout for a given display.
  531. *
  532. * Layout selection is nothing more than a list of radio items encompassing the available
  533. * layouts for this display, as defined by .inc files in the panels/layouts subdirectory.
  534. * The only real complexity occurs when a user attempts to change the layout of a display
  535. * that has some content in it.
  536. *
  537. * @param object $display instanceof panels_display \n
  538. * A fully loaded panels $display object, as returned from panels_load_display().
  539. * Merely passing a did is NOT sufficient.
  540. * @param string $finish
  541. * A string that will be used for the text of the form submission button. If no value is provided,
  542. * then the form submission button will default to t('Save').
  543. * @param mixed $destination
  544. * Basic usage is a string containing the URL that the form should redirect to upon submission.
  545. * For a discussion of advanced usages, see panels_edit().
  546. * @param mixed $allowed_layouts
  547. * Allowed layouts has three different behaviors that depend on which of three value types
  548. * are passed in by the caller:
  549. * #- if $allowed_layouts instanceof panels_allowed_layouts (includes subclasses): the most
  550. * complex use of the API. The caller is passing in a loaded panels_allowed_layouts object
  551. * that the client module previously created and stored somewhere using a custom storage
  552. * mechanism.
  553. * #- if is_string($allowed_layouts): the string will be used in a call to variable_get() which
  554. * will call the $allowed_layouts . '_allowed_layouts' var. If the data was stored properly
  555. * in the system var, the $allowed_layouts object will be unserialized and recreated.
  556. * @see panels_common_set_allowed_layouts()
  557. * #- if is_null($allowed_layouts): the default behavior, which also provides backwards
  558. * compatibility for implementations of the Panels2 API written before beta4. In this case,
  559. * a dummy panels_allowed_layouts object is created which does not restrict any layouts.
  560. * Subsequent behavior is indistinguishable from pre-beta4 behavior.
  561. *
  562. * @return
  563. * Can return nothing, or a modified $display object, or a redirection string; return values for the
  564. * panels_edit* family of functions are quite complex. See panels_edit() for detailed discussion.
  565. * @see panels_edit()
  566. */
  567. function panels_edit_layout($display, $finish, $destination = NULL, $allowed_layouts = NULL) {
  568. ctools_include('display-layout', 'panels');
  569. ctools_include('plugins', 'panels');
  570. return _panels_edit_layout($display, $finish, $destination, $allowed_layouts);
  571. }
  572. // ---------------------------------------------------------------------------
  573. // Panels database functions
  574. /**
  575. * Forms the basis of a panel display
  576. *
  577. */
  578. class panels_display {
  579. var $args = array();
  580. var $content = array();
  581. var $panels = array();
  582. var $incoming_content = NULL;
  583. var $css_id = NULL;
  584. var $context = array();
  585. var $did = 'new';
  586. var $renderer = 'standard';
  587. function add_pane(&$pane, $location = NULL) {
  588. // If no location specified, use what's set in the pane.
  589. if (empty($location)) {
  590. $location = $pane->panel;
  591. }
  592. else {
  593. $pane->panel = $location;
  594. }
  595. // Get a temporary pid for this pane.
  596. $pane->pid = "new-" . $this->next_new_pid();
  597. // Add the pane to the approprate spots.
  598. $this->content[$pane->pid] = &$pane;
  599. $this->panels[$location][] = $pane->pid;
  600. }
  601. function duplicate_pane($pid, $location = FALSE) {
  602. $pane = $this->clone_pane($pid);
  603. $this->add_pane($pane, $location);
  604. }
  605. function clone_pane($pid) {
  606. $pane = clone $this->content[$pid];
  607. return $pane;
  608. }
  609. function next_new_pid() {
  610. // We don't use static vars to record the next new pid because
  611. // temporary pids can last for years in exports and in caching
  612. // during editing.
  613. $id = array(0);
  614. foreach (array_keys($this->content) as $pid) {
  615. if (!is_numeric($pid)) {
  616. $id[] = substr($pid, 4);
  617. }
  618. }
  619. $next_id = max($id);
  620. return ++$next_id;
  621. }
  622. /**
  623. * Get the title from a display.
  624. *
  625. * The display must have already been rendered, or the setting to set the
  626. * display's title from a pane's title will not have worked.
  627. *
  628. * @return
  629. * The title to use. If NULL, this means to let any default title that may be in use
  630. * pass through. i.e, do not actually set the title.
  631. */
  632. function get_title() {
  633. switch ($this->hide_title) {
  634. case PANELS_TITLE_NONE:
  635. return '';
  636. case PANELS_TITLE_PANE:
  637. return isset($this->stored_pane_title) ? $this->stored_pane_title : '';
  638. case PANELS_TITLE_FIXED:
  639. case FALSE; // For old exported panels that are not in the database.
  640. if (!empty($this->title)) {
  641. return filter_xss_admin(ctools_context_keyword_substitute($this->title, array(), $this->context));
  642. }
  643. return NULL;
  644. }
  645. }
  646. /**
  647. * Render this panels display.
  648. *
  649. * After checking to ensure the designated layout plugin is valid, a
  650. * display renderer object is spawned and runs its rendering logic.
  651. *
  652. * @param mixed $renderer
  653. * An instantiated display renderer object, or the name of a display
  654. * renderer plugin+class to be fetched. Defaults to NULL. When NULL, the
  655. * predesignated display renderer will be used.
  656. */
  657. function render($renderer = NULL) {
  658. $layout = panels_get_layout($this->layout);
  659. if (!$layout) {
  660. return NULL;
  661. }
  662. // If we were not given a renderer object, load it.
  663. if (!is_object($renderer)) {
  664. // If the renderer was not specified, default to $this->renderer
  665. // which is either standard or was already set for us.
  666. $renderer = panels_get_renderer_handler(!empty($renderer) ? $renderer : $this->renderer, $this);
  667. if (!$renderer) {
  668. return NULL;
  669. }
  670. }
  671. $output = '';
  672. // Let modules act just prior to render.
  673. foreach (module_implements('panels_pre_render') as $module) {
  674. $function = $module . '_panels_pre_render';
  675. $output .= $function($this, $renderer);
  676. }
  677. $output .= $renderer->render();
  678. // Let modules act just after render.
  679. foreach (module_implements('panels_post_render') as $module) {
  680. $function = $module . '_panels_post_render';
  681. $output .= $function($this, $renderer);
  682. }
  683. return $output;
  684. }
  685. }
  686. /**
  687. * }@ End of 'defgroup mainapi', although other functions are specifically added later
  688. */
  689. /**
  690. * Creates a new display, setting the ID to our magic new id.
  691. */
  692. function panels_new_display() {
  693. ctools_include('export');
  694. $display = ctools_export_new_object('panels_display', FALSE);
  695. $display->did = 'new';
  696. return $display;
  697. }
  698. /**
  699. * Create a new pane.
  700. *
  701. * @todo -- use schema API for some of this?
  702. */
  703. function panels_new_pane($type, $subtype, $set_defaults = FALSE) {
  704. ctools_include('export');
  705. $pane = ctools_export_new_object('panels_pane', FALSE);
  706. $pane->pid = 'new';
  707. $pane->type = $type;
  708. $pane->subtype = $subtype;
  709. if ($set_defaults) {
  710. ctools_include('content');
  711. $content_type = ctools_get_content_type($type);
  712. $content_subtype = ctools_content_get_subtype($content_type, $subtype);
  713. $pane->configuration = ctools_content_get_defaults($content_type, $content_subtype);
  714. }
  715. return $pane;
  716. }
  717. /**
  718. * Load and fill the requested $display object(s).
  719. *
  720. * Helper function primarily for for panels_load_display().
  721. *
  722. * @param array $dids
  723. * An indexed array of dids to be loaded from the database.
  724. *
  725. * @return $displays
  726. * An array of displays, keyed by their display dids.
  727. *
  728. * @todo schema API can drasticly simplify this code.
  729. */
  730. function panels_load_displays($dids) {
  731. $displays = array();
  732. if (empty($dids) || !is_array($dids)) {
  733. return $displays;
  734. }
  735. $result = db_query("SELECT * FROM {panels_display} WHERE did IN (:dids)", array(':dids' => $dids));
  736. ctools_include('export');
  737. foreach ($result as $obj) {
  738. $displays[$obj->did] = ctools_export_unpack_object('panels_display', $obj);
  739. // Modify the hide_title field to go from a bool to an int if necessary.
  740. }
  741. $result = db_query("SELECT * FROM {panels_pane} WHERE did IN (:dids) ORDER BY did, panel, position", array(':dids' => $dids));
  742. foreach ($result as $obj) {
  743. $pane = ctools_export_unpack_object('panels_pane', $obj);
  744. $displays[$pane->did]->panels[$pane->panel][] = $pane->pid;
  745. $displays[$pane->did]->content[$pane->pid] = $pane;
  746. }
  747. return $displays;
  748. }
  749. /**
  750. * Load a single display.
  751. *
  752. * @ingroup mainapi
  753. *
  754. * @param int $did
  755. * The display id (did) of the display to be loaded.
  756. *
  757. * @return object $display instanceof panels_display \n
  758. * Returns a partially-loaded panels_display object. $display objects returned from
  759. * from this function have only the following data:
  760. * - $display->did (the display id)
  761. * - $display->name (the 'name' of the display, where applicable - it often isn't)
  762. * - $display->layout (a string with the system name of the display's layout)
  763. * - $display->panel_settings (custom layout style settings contained in an associative array; NULL if none)
  764. * - $display->layout_settings (panel size and configuration settings for Flexible layouts; NULL if none)
  765. * - $display->css_id (the special css_id that has been assigned to this display, if any; NULL if none)
  766. * - $display->content (an array of pane objects, keyed by pane id (pid))
  767. * - $display->panels (an associative array of panel regions, each an indexed array of pids in the order they appear in that region)
  768. * - $display->cache (any relevant data from panels_simple_cache)
  769. * - $display->args
  770. * - $display->incoming_content
  771. *
  772. * While all of these members are defined, $display->context is NEVER defined in the returned $display;
  773. * it must be set using one of the ctools_context_create() functions.
  774. */
  775. function panels_load_display($did) {
  776. $displays = panels_load_displays(array($did));
  777. if (!empty($displays)) {
  778. return array_shift($displays);
  779. }
  780. }
  781. /**
  782. * Save a display object.
  783. *
  784. * @ingroup mainapi
  785. *
  786. * Note a new $display only receives a real did once it is run through this function.
  787. * Until then, it uses a string placeholder, 'new', in place of a real did. The same
  788. * applies to all new panes (whether on a new $display or not); in addition,
  789. * panes have sequential numbers appended, of the form 'new-1', 'new-2', etc.
  790. *
  791. * @param object $display instanceof panels_display \n
  792. * The display object to be saved. Passed by reference so the caller need not use
  793. * the return value for any reason except convenience.
  794. *
  795. * @return object $display instanceof panels_display \n
  796. */
  797. function panels_save_display(&$display) {
  798. $update = (isset($display->did) && is_numeric($display->did)) ? array('did') : array();
  799. drupal_write_record('panels_display', $display, $update);
  800. $pids = array();
  801. if ($update) {
  802. // Get a list of all panes currently in the database for this display so we can know if there
  803. // are panes that need to be deleted. (i.e, aren't currently in our list of panes).
  804. $result = db_query("SELECT pid FROM {panels_pane} WHERE did = :did", array(':did' => $display->did));
  805. foreach ($result as $pane) {
  806. $pids[$pane->pid] = $pane->pid;
  807. }
  808. }
  809. // update all the panes
  810. ctools_include('plugins', 'panels');
  811. ctools_include('content');
  812. foreach ($display->panels as $id => $panes) {
  813. $position = 0;
  814. $new_panes = array();
  815. foreach ((array) $panes as $pid) {
  816. if (!isset($display->content[$pid])) {
  817. continue;
  818. }
  819. $pane = $display->content[$pid];
  820. $type = ctools_get_content_type($pane->type);
  821. $pane->position = $position++;
  822. $pane->did = $display->did;
  823. $old_pid = $pane->pid;
  824. drupal_write_record('panels_pane', $pane, is_numeric($pid) ? array('pid') : array());
  825. if ($pane->pid != $old_pid) {
  826. // and put it back so our pids and positions can be used
  827. unset($display->content[$id]);
  828. $display->content[$pane->pid] = $pane;
  829. // If the title pane was one of our panes that just got its ID changed,
  830. // we need to change it in the database, too.
  831. if (isset($display->title_pane) && $display->title_pane == $old_pid) {
  832. $display->title_pane = $pane->pid;
  833. // Do a simple update query to write it so we don't have to rewrite
  834. // the whole record. We can't just save writing the whole record here
  835. // because it was needed to get the did. Chicken, egg, more chicken.
  836. db_update('panels_display')
  837. ->fields(array(
  838. 'title_pane' => $pane->pid
  839. ))
  840. ->condition('did', $display->did)
  841. ->execute();
  842. }
  843. }
  844. // re-add this to the list of content for this panel.
  845. $new_panes[] = $pane->pid;
  846. // Remove this from the list of panes scheduled for deletion.
  847. if (isset($pids[$pane->pid])) {
  848. unset($pids[$pane->pid]);
  849. }
  850. }
  851. $display->panels[$id] = $new_panes;
  852. }
  853. if (!empty($pids)) {
  854. db_delete('panels_pane')->condition('pid', $pids)->execute();
  855. }
  856. // Clear any cached content for this display.
  857. panels_clear_cached_content($display);
  858. // Allow other modules to take action when a display is saved.
  859. module_invoke_all('panels_display_save', $display);
  860. // Log the change to watchdog, using the same style as node.module
  861. $watchdog_args = array('%did' => $display->did);
  862. if (!empty($display->title)) {
  863. $watchdog_args['%title'] = $display->title;
  864. watchdog('content', 'Panels: saved display "%title" with display id %did', $watchdog_args, WATCHDOG_NOTICE);
  865. }
  866. else {
  867. watchdog('content', 'Panels: saved display with id %did', $watchdog_args, WATCHDOG_NOTICE);
  868. }
  869. // to be nice, even tho we have a reference.
  870. return $display;
  871. }
  872. /**
  873. * Delete a display.
  874. */
  875. function panels_delete_display($display) {
  876. if (is_object($display)) {
  877. $did = $display->did;
  878. }
  879. else {
  880. $did = $display;
  881. }
  882. db_delete('panels_display')->condition('did', $did)->execute();
  883. db_delete('panels_pane')->condition('did', $did)->execute();
  884. }
  885. /**
  886. * Exports the provided display into portable code.
  887. *
  888. * This function is primarily intended as a mechanism for cloning displays.
  889. * It generates an exact replica (in code) of the provided $display, with
  890. * the exception that it replaces all ids (dids and pids) with 'new-*' values.
  891. * Only once panels_save_display() is called on the code version of $display will
  892. * the exported display written to the database and permanently saved.
  893. *
  894. * @see panels_page_export() or _panels_page_fetch_display() for sample implementations.
  895. *
  896. * @ingroup mainapi
  897. *
  898. * @param object $display instanceof panels_display \n
  899. * This export function does no loading of additional data about the provided
  900. * display. Consequently, the caller should make sure that all the desired data
  901. * has been loaded into the $display before calling this function.
  902. * @param string $prefix
  903. * A string prefix that is prepended to each line of exported code. This is primarily
  904. * used for prepending a double space when exporting so that the code indents and lines up nicely.
  905. *
  906. * @return string $output
  907. * The passed-in $display expressed as code, ready to be imported. Import by running
  908. * eval($output) in the caller function; doing so will create a new $display variable
  909. * with all the exported values. Note that if you have already defined a $display variable in
  910. * the same scope as where you eval(), your existing $display variable WILL be overwritten.
  911. */
  912. function panels_export_display($display, $prefix = '') {
  913. ctools_include('export');
  914. $output = ctools_export_object('panels_display', $display, $prefix);
  915. $pid_counter = &drupal_static(__FUNCTION__, 0);
  916. // Initialize empty properties.
  917. $output .= $prefix . '$display->content = array()' . ";\n";
  918. $output .= $prefix . '$display->panels = array()' . ";\n";
  919. $panels = array();
  920. $title_pid = 0;
  921. if (!empty($display->content)) {
  922. $region_counters = array();
  923. foreach ($display->content as $pane) {
  924. $pid = 'new-' . ++$pid_counter;
  925. if ($pane->pid == $display->title_pane) {
  926. $title_pid = $pid;
  927. }
  928. $pane->pid = $pid;
  929. $output .= ctools_export_object('panels_pane', $pane, $prefix . ' ');
  930. $output .= "$prefix " . '$display->content[\'' . $pane->pid . '\'] = $pane' . ";\n";
  931. if (!isset($region_counters[$pane->panel])) {
  932. $region_counters[$pane->panel] = 0;
  933. }
  934. $output .= "$prefix " . '$display->panels[\'' . $pane->panel . '\'][' . $region_counters[$pane->panel]++ .'] = \'' . $pane->pid . "';\n";
  935. }
  936. }
  937. $output .= $prefix . '$display->hide_title = ';
  938. switch ($display->hide_title) {
  939. case PANELS_TITLE_FIXED:
  940. $output .= 'PANELS_TITLE_FIXED';
  941. break;
  942. case PANELS_TITLE_NONE:
  943. $output .= 'PANELS_TITLE_NONE';
  944. break;
  945. case PANELS_TITLE_PANE:
  946. $output .= 'PANELS_TITLE_PANE';
  947. break;
  948. }
  949. $output .= ";\n";
  950. $output .= $prefix . '$display->title_pane =' . " '$title_pid';\n";
  951. return $output;
  952. }
  953. /**
  954. * Render a display by loading the content into an appropriate
  955. * array and then passing through to panels_render_layout.
  956. *
  957. * if $incoming_content is NULL, default content will be applied. Use
  958. * an empty string to indicate no content.
  959. * @ingroup hook_invocations
  960. */
  961. function panels_render_display(&$display, $renderer = NULL) {
  962. ctools_include('plugins', 'panels');
  963. ctools_include('context');
  964. if (!empty($display->context)) {
  965. if ($form_context = ctools_context_get_form($display->context)) {
  966. $form_context->form['#theme'] = 'panels_render_display_form';
  967. if (empty($form_context->form['#theme_wrappers']) || !in_array('form', $form_context->form['#theme_wrappers'])) {
  968. $form_context['#theme_wrappers'][] = 'form';
  969. }
  970. $form_context->form['#display'] = &$display;
  971. return $form_context->form;
  972. }
  973. }
  974. return $display->render($renderer);
  975. }
  976. /**
  977. * Theme function to render our panel as a form.
  978. *
  979. * When rendering a display as a form, the entire display needs to be
  980. * inside the <form> tag so that the form can be spread across the
  981. * panes. This sets up the form system to be the main caller and we
  982. * then operate as a theme function of the form.
  983. */
  984. function theme_panels_render_display_form($vars) {
  985. return $vars['element']['#display']->render();
  986. }
  987. // @layout
  988. function panels_print_layout_icon($id, $layout, $title = NULL) {
  989. ctools_add_css('panels_admin', 'panels');
  990. $file = $layout['path'] . '/' . $layout['icon'];
  991. return theme('panels_layout_icon', array('id' => $id, 'image' => theme('image', array('path' => $file, 'alt' => strip_tags($layout['title']), 'title' => strip_tags($layout['description']))), 'title' => $title));
  992. }
  993. /**
  994. * Theme the layout icon image
  995. * @layout
  996. * @todo move to theme.inc
  997. */
  998. function theme_panels_layout_icon($vars) {
  999. $id = $vars['id'];
  1000. $image = $vars['image'];
  1001. $title = $vars['title'];
  1002. $output = '<div class="layout-icon">';
  1003. $output .= $image;
  1004. if ($title) {
  1005. $output .= '<div class="caption">' . $title . '</div>';
  1006. }
  1007. $output .= '</div>';
  1008. return $output;
  1009. }
  1010. /**
  1011. * Theme the layout link image
  1012. * @layout
  1013. *
  1014. * @todo Why isn't this a template at this point?
  1015. * @todo Why does this take 4 arguments but only makes use of two?
  1016. */
  1017. function theme_panels_layout_link($vars) {
  1018. $title = $vars['title'];
  1019. $image = $vars['image'];
  1020. $class = $vars['class'];
  1021. $output = '<div class="' . implode(' ', $class) . '">';
  1022. $output .= $vars['image'];
  1023. $output .= '<div>' . $vars['title'] . '</div>';
  1024. $output .= '</div>';
  1025. return $output;
  1026. }
  1027. /**
  1028. * Print the layout link. Sends out to a theme function.
  1029. * @layout
  1030. */
  1031. function panels_print_layout_link($id, $layout, $link, $options = array(), $current_layout = FALSE) {
  1032. if (isset($options['query']['q'])) {
  1033. unset($options['query']['q']);
  1034. }
  1035. // Setup classes for layout link, including current-layout information
  1036. $class = array('layout-link');
  1037. if ($current_layout == $id) {
  1038. $options['attributes']['class'][] = 'current-layout-link';
  1039. $class[] = 'current-layout';
  1040. }
  1041. ctools_add_css('panels_admin', 'panels');
  1042. $file = $layout['path'] . '/' . $layout['icon'];
  1043. $image = l(theme('image', array('path' => $file)), $link, array('html' => true) + $options);
  1044. $title = l($layout['title'], $link, $options);
  1045. return theme('panels_layout_link', array('title' => $title, 'image' => $image, 'class' => $class));
  1046. }
  1047. /**
  1048. * Gateway to the PanelsLegacyState class/object, which does all legacy state
  1049. * checks and provides information about the cause of legacy states as needed.
  1050. *
  1051. * @return PanelsLegacyState $legacy
  1052. */
  1053. function panels_get_legacy_state() {
  1054. static $legacy = NULL;
  1055. if (!isset($legacy)) {
  1056. ctools_include('legacy', 'panels');
  1057. $legacy = new PanelsLegacyState();
  1058. }
  1059. return $legacy;
  1060. }
  1061. /**
  1062. * Get the display that is currently being rendered as a page.
  1063. *
  1064. * Unlike in previous versions of this, this only returns the display,
  1065. * not the page itself, because there are a number of different ways
  1066. * to get to this point. It is hoped that the page data isn't needed
  1067. * at this point. If it turns out there is, we will do something else to
  1068. * get that functionality.
  1069. */
  1070. function panels_get_current_page_display($change = NULL) {
  1071. static $display = NULL;
  1072. if ($change) {
  1073. $display = $change;
  1074. }
  1075. return $display;
  1076. }
  1077. /**
  1078. * Clean up the panel pane variables for the template.
  1079. */
  1080. function template_preprocess_panels_pane(&$vars) {
  1081. $content = &$vars['content'];
  1082. $vars['contextual_links'] = array();
  1083. $vars['classes_array'] = array();
  1084. $vars['admin_links'] = '';
  1085. if (module_exists('contextual') && user_access('access contextual links')) {
  1086. $links = array();
  1087. // These are specified by the content.
  1088. if (!empty($content->admin_links)) {
  1089. $links += $content->admin_links;
  1090. }
  1091. // Take any that may have been in the render array we were given and
  1092. // move them up so they appear outside the pane properly.
  1093. if (is_array($content->content) && isset($content->content['#contextual_links'])) {
  1094. $element = array(
  1095. '#type' => 'contextual_links',
  1096. '#contextual_links' => $content->content['#contextual_links'],
  1097. );
  1098. unset($content->content['#contextual_links']);
  1099. // Add content to $element array
  1100. if (is_array($content->content)) {
  1101. $element['#element'] = $content->content;
  1102. }
  1103. $element = contextual_pre_render_links($element);
  1104. if(!empty($element['#links'])) {
  1105. $links += $element['#links'];
  1106. }
  1107. }
  1108. if ($links) {
  1109. $build = array(
  1110. '#prefix' => '<div class="contextual-links-wrapper">',
  1111. '#suffix' => '</div>',
  1112. '#theme' => 'links__contextual',
  1113. '#links' => $links,
  1114. '#attributes' => array('class' => array('contextual-links')),
  1115. '#attached' => array(
  1116. 'library' => array(array('contextual', 'contextual-links')),
  1117. ),
  1118. );
  1119. $vars['classes_array'][] = 'contextual-links-region';
  1120. $vars['admin_links'] = drupal_render($build);
  1121. }
  1122. }
  1123. // basic classes
  1124. $vars['classes_array'][] = 'panel-pane';
  1125. $vars['id'] = '';
  1126. // Add some usable classes based on type/subtype
  1127. ctools_include('cleanstring');
  1128. $type_class = $content->type ? 'pane-'. ctools_cleanstring($content->type, array('lower case' => TRUE)) : '';
  1129. $subtype_class = $content->subtype ? 'pane-'. ctools_cleanstring($content->subtype, array('lower case' => TRUE)) : '';
  1130. // Sometimes type and subtype are the same. Avoid redundant classes.
  1131. $vars['classes_array'][] = $type_class;
  1132. if ($type_class != $subtype_class) {
  1133. $vars['classes_array'][] = $subtype_class;
  1134. }
  1135. // Add id and custom class if sent in.
  1136. if (!empty($content->content)) {
  1137. if (!empty($content->css_id)) {
  1138. $vars['id'] = ' id="' . $content->css_id . '"';
  1139. }
  1140. if (!empty($content->css_class)) {
  1141. $vars['classes_array'][] = $content->css_class;
  1142. }
  1143. }
  1144. // Set up some placeholders for constructing template file names.
  1145. $base = 'panels_pane';
  1146. $delimiter = '__';
  1147. // Add template file suggestion for content type and sub-type.
  1148. $vars['theme_hook_suggestions'][] = $base . $delimiter . $content->type;
  1149. $vars['theme_hook_suggestions'][] = $base . $delimiter . strtr(ctools_cleanstring($content->type, array('lower case' => TRUE)), '-', '_') . $delimiter . strtr(ctools_cleanstring($content->subtype, array('lower case' => TRUE)), '-', '_');
  1150. $vars['pane_prefix'] = !empty($content->pane_prefix) ? $content->pane_prefix : '';
  1151. $vars['pane_suffix'] = !empty($content->pane_suffix) ? $content->pane_suffix : '';
  1152. $vars['title'] = !empty($content->title) ? $content->title : '';
  1153. $vars['title_attributes_array']['class'][] = 'pane-title';
  1154. $vars['feeds'] = !empty($content->feeds) ? implode(' ', $content->feeds) : '';
  1155. $vars['links'] = !empty($content->links) ? theme('links', array('links' => $content->links)) : '';
  1156. $vars['more'] = '';
  1157. if (!empty($content->more)) {
  1158. if (empty($content->more['title'])) {
  1159. $content->more['title'] = t('more');
  1160. }
  1161. $vars['more'] = l($content->more['title'], $content->more['href'], $content->more);
  1162. }
  1163. $vars['content'] = !empty($content->content) ? $content->content : '';
  1164. }
  1165. /**
  1166. * Route Panels' AJAX calls to the correct object.
  1167. *
  1168. * Panels' AJAX is controlled mostly by renderer objects. This menu callback
  1169. * accepts the incoming request, figures out which object should handle the
  1170. * request, and attempts to route it. If no object can be found, the default
  1171. * Panels editor object is used.
  1172. *
  1173. * Calls are routed via the ajax_* method space. For example, if visiting
  1174. * panels/ajax/add-pane then $renderer::ajax_add_pane() will be called.
  1175. * This means commands can be added without having to create new callbacks.
  1176. *
  1177. * The first argument *must always* be the cache key so that a cache object
  1178. * can be passed through. Other arguments will be passed through untouched
  1179. * so that the method can do whatever it needs to do.
  1180. */
  1181. function panels_ajax_router() {
  1182. $args = func_get_args();
  1183. if (count($args) < 3) {
  1184. return MENU_NOT_FOUND;
  1185. }
  1186. ctools_include('display-edit', 'panels');
  1187. ctools_include('plugins', 'panels');
  1188. ctools_include('ajax');
  1189. ctools_include('modal');
  1190. ctools_include('context');
  1191. ctools_include('content');
  1192. $plugin_name = array_shift($args);
  1193. $method = array_shift($args);
  1194. $cache_key = array_shift($args);
  1195. $plugin = panels_get_display_renderer($plugin_name);
  1196. if (!$plugin) {
  1197. // This is the default renderer for handling AJAX commands.
  1198. $plugin = panels_get_display_renderer('editor');
  1199. }
  1200. $cache = panels_edit_cache_get($cache_key);
  1201. if (empty($cache)) {
  1202. return MENU_ACCESS_DENIED;
  1203. }
  1204. $renderer = panels_get_renderer_handler($plugin, $cache->display);
  1205. if (!$renderer) {
  1206. return MENU_ACCESS_DENIED;
  1207. }
  1208. $method = 'ajax_' . str_replace('-', '_', $method);
  1209. if (!method_exists($renderer, $method)) {
  1210. return MENU_NOT_FOUND;
  1211. }
  1212. $renderer->cache = &$cache;
  1213. ctools_include('cleanstring');
  1214. $renderer->clean_key = ctools_cleanstring($cache_key);
  1215. $output = call_user_func_array(array($renderer, $method), $args);
  1216. if (empty($output) && !empty($renderer->commands)) {
  1217. return array(
  1218. '#type' => 'ajax',
  1219. '#commands' => $renderer->commands,
  1220. );
  1221. }
  1222. else {
  1223. return $output;
  1224. }
  1225. }
  1226. // --------------------------------------------------------------------------
  1227. // Panels caching functions and callbacks
  1228. //
  1229. // When editing displays and the like, Panels has a caching system that relies
  1230. // on a callback to determine where to get the actual cache.
  1231. // @todo This system needs to be better documented so that it can be
  1232. // better used.
  1233. /**
  1234. * Get an object from cache.
  1235. */
  1236. function panels_cache_get($obj, $did, $skip_cache = FALSE) {
  1237. ctools_include('object-cache');
  1238. // we often store contexts in cache, so let's just make sure we can load
  1239. // them.
  1240. ctools_include('context');
  1241. return ctools_object_cache_get($obj, 'panels_display:' . $did, $skip_cache);
  1242. }
  1243. /**
  1244. * Save the edited object into the cache.
  1245. */
  1246. function panels_cache_set($obj, $did, $cache) {
  1247. ctools_include('object-cache');
  1248. return ctools_object_cache_set($obj, 'panels_display:' . $did, $cache);
  1249. }
  1250. /**
  1251. * Clear a object from the cache; used if the editing is aborted.
  1252. */
  1253. function panels_cache_clear($obj, $did) {
  1254. ctools_include('object-cache');
  1255. return ctools_object_cache_clear($obj, 'panels_display:' . $did);
  1256. }
  1257. /**
  1258. * Create the default cache for editing panel displays.
  1259. *
  1260. * If an application is using the Panels display editor without having
  1261. * specified a cache key, this method can be used to create the default
  1262. * cache.
  1263. */
  1264. function panels_edit_cache_get_default(&$display, $content_types = NULL, $title = FALSE) {
  1265. if (empty($content_types)) {
  1266. $content_types = ctools_content_get_available_types();
  1267. }
  1268. $display->cache_key = $display->did;
  1269. panels_cache_clear('display', $display->did);
  1270. $cache = new stdClass();
  1271. $cache->display = &$display;
  1272. $cache->content_types = $content_types;
  1273. $cache->display_title = $title;
  1274. panels_edit_cache_set($cache);
  1275. return $cache;
  1276. }
  1277. /**
  1278. * Method to allow modules to provide their own caching mechanism for the
  1279. * display editor.
  1280. */
  1281. function panels_edit_cache_get($cache_key) {
  1282. if (strpos($cache_key, ':') !== FALSE) {
  1283. list($module, $argument) = explode(':', $cache_key, 2);
  1284. return module_invoke($module, 'panels_cache_get', $argument);
  1285. }
  1286. // Fall back to our normal method:
  1287. return panels_cache_get('display', $cache_key);
  1288. }
  1289. /**
  1290. * Method to allow modules to provide their own caching mechanism for the
  1291. * display editor.
  1292. */
  1293. function panels_edit_cache_set($cache) {
  1294. $cache_key = $cache->display->cache_key;
  1295. if (strpos($cache_key, ':') !== FALSE) {
  1296. list($module, $argument) = explode(':', $cache_key, 2);
  1297. return module_invoke($module, 'panels_cache_set', $argument, $cache);
  1298. }
  1299. // Fall back to our normal method:
  1300. return panels_cache_set('display', $cache_key, $cache);
  1301. }
  1302. /**
  1303. * Method to allow modules to provide their own mechanism to write the
  1304. * cache used in the display editor.
  1305. */
  1306. function panels_edit_cache_save($cache) {
  1307. $cache_key = $cache->display->cache_key;
  1308. if (strpos($cache_key, ':') !== FALSE) {
  1309. list($module, $argument) = explode(':', $cache_key, 2);
  1310. if (function_exists($module . '_panels_cache_save')) {
  1311. return module_invoke($module, 'panels_cache_save', $argument, $cache);
  1312. }
  1313. }
  1314. // Fall back to our normal method:
  1315. return panels_save_display($cache->display);
  1316. }
  1317. /**
  1318. * Method to allow modules to provide their own mechanism to clear the
  1319. * cache used in the display editor.
  1320. */
  1321. function panels_edit_cache_clear($cache) {
  1322. $cache_key = $cache->display->cache_key;
  1323. if (strpos($cache_key, ':') !== FALSE) {
  1324. list($module, $argument) = explode(':', $cache_key, 2);
  1325. if (function_exists($module . '_panels_cache_clear')) {
  1326. return module_invoke($module, 'panels_cache_clear', $argument, $cache);
  1327. }
  1328. }
  1329. // Fall back to our normal method:
  1330. return panels_cache_clear('display', $cache_key);
  1331. }
  1332. /**
  1333. * Method to allow modules to provide a mechanism to break locks.
  1334. */
  1335. function panels_edit_cache_break_lock($cache) {
  1336. if (empty($cache->locked)) {
  1337. return;
  1338. }
  1339. $cache_key = $cache->display->cache_key;
  1340. if (strpos($cache_key, ':') !== FALSE) {
  1341. list($module, $argument) = explode(':', $cache_key, 2);
  1342. if (function_exists($module . '_panels_cache_break_lock')) {
  1343. return module_invoke($module, 'panels_cache_break_lock', $argument, $cache);
  1344. }
  1345. }
  1346. // Normal panel display editing has no locks, so we do nothing if there is
  1347. // no fallback.
  1348. return;
  1349. }
  1350. // --------------------------------------------------------------------------
  1351. // Callbacks on behalf of the panel_context plugin.
  1352. //
  1353. // The panel_context plugin lets Panels be used in page manager. These
  1354. // callbacks allow the display editing system to use the page manager
  1355. // cache rather than the default display cache. They are routed by the cache
  1356. // key via panels_edit_cache_* functions.
  1357. /**
  1358. * Get display edit cache on behalf of panel context.
  1359. *
  1360. * The key is the second half of the key in this form:
  1361. * panel_context:TASK_NAME::HANDLER_NAME::args::url;
  1362. */
  1363. function panel_context_panels_cache_get($key) {
  1364. ctools_include('common', 'panels');
  1365. ctools_include('context');
  1366. ctools_include('context-task-handler');
  1367. // this loads the panel context inc even if we don't use the plugin.
  1368. $plugin = page_manager_get_task_handler('panel_context');
  1369. list($task_name, $handler_name, $args, $q) = explode('::', $key, 4);
  1370. $page = page_manager_get_page_cache($task_name);
  1371. if (isset($page->display_cache[$handler_name])) {
  1372. return $page->display_cache[$handler_name];
  1373. }
  1374. if ($handler_name) {
  1375. $handler = &$page->handlers[$handler_name];
  1376. }
  1377. else {
  1378. $handler = &$page->new_handler;
  1379. }
  1380. $cache = new stdClass();
  1381. $task = page_manager_get_task($page->task_id);
  1382. //ctools_context_handler_get_all_contexts($page->task, $page->subtask, $handler);
  1383. $arguments = array();
  1384. if ($args) {
  1385. $arguments = explode('\\', $args);
  1386. $contexts = ctools_context_handler_get_task_contexts($task, $page->subtask, $arguments);
  1387. $contexts = ctools_context_handler_get_handler_contexts($contexts, $handler);
  1388. }
  1389. else {
  1390. $contexts = ctools_context_handler_get_all_contexts($page->task, $page->subtask, $handler);
  1391. }
  1392. $cache->display = &panels_panel_context_get_display($handler);
  1393. $cache->display->context = $contexts;
  1394. $cache->display->cache_key = 'panel_context:' . $key;
  1395. $cache->content_types = panels_common_get_allowed_types('panels_page', $cache->display->context);
  1396. $cache->display_title = TRUE;
  1397. $cache->locked = $page->locked;
  1398. return $cache;
  1399. }
  1400. /**
  1401. * Get the Page Manager cache for the panel_context plugin.
  1402. */
  1403. function _panel_context_panels_cache_get_page_cache($key, $cache) {
  1404. list($task_name, $handler_name, $args, $q) = explode('::', $key, 4);
  1405. $page = page_manager_get_page_cache($task_name);
  1406. $page->display_cache[$handler_name] = $cache;
  1407. if ($handler_name) {
  1408. $page->handlers[$handler_name]->conf['display'] = $cache->display;
  1409. $page->handler_info[$handler_name]['changed'] |= PAGE_MANAGER_CHANGED_CACHED;
  1410. }
  1411. else {
  1412. $page->new_handler->conf['display'] = $cache->display;
  1413. }
  1414. return $page;
  1415. }
  1416. /**
  1417. * Store a display edit in progress in the page cache.
  1418. */
  1419. function panel_context_panels_cache_set($key, $cache) {
  1420. $page = _panel_context_panels_cache_get_page_cache($key, $cache);
  1421. page_manager_set_page_cache($page);
  1422. }
  1423. /**
  1424. * Save all changes made to a display using the Page Manager page cache.
  1425. */
  1426. function panel_context_panels_cache_clear($key, $cache) {
  1427. $page = _panel_context_panels_cache_get_page_cache($key, $cache);
  1428. page_manager_clear_page_cache($page->task_name);
  1429. }
  1430. /**
  1431. * Save all changes made to a display using the Page Manager page cache.
  1432. */
  1433. function panel_context_panels_cache_save($key, $cache) {
  1434. $page = _panel_context_panels_cache_get_page_cache($key, $cache);
  1435. page_manager_save_page_cache($page);
  1436. }
  1437. /**
  1438. * Break the lock on a page manager page.
  1439. */
  1440. function panel_context_panels_cache_break_lock($key, $cache) {
  1441. $page = _panel_context_panels_cache_get_page_cache($key, $cache);
  1442. ctools_object_cache_clear_all('page_manager_page', $page->task_name);
  1443. }
  1444. // --------------------------------------------------------------------------
  1445. // Callbacks on behalf of the panels page wizards
  1446. //
  1447. // The page wizards are a pluggable set of 'wizards' to make it easy to create
  1448. // specific types of pages based upon whatever someone felt like putting
  1449. // together. Since they will very often have content editing, we provide
  1450. // a generic mechanism to allow them to store their editing cache in the
  1451. // wizard cache.
  1452. //
  1453. // For them to use this mechanism, they just need to use:
  1454. // $cache = panels_edit_cache_get('panels_page_wizard:' . $plugin['name']);
  1455. /**
  1456. * Get display edit cache for the panels mini export UI
  1457. *
  1458. * The key is the second half of the key in this form:
  1459. * panels_page_wizard:TASK_NAME:HANDLER_NAME;
  1460. */
  1461. function panels_page_wizard_panels_cache_get($key) {
  1462. ctools_include('page-wizard');
  1463. ctools_include('context');
  1464. $wizard_cache = page_manager_get_wizard_cache($key);
  1465. if (isset($wizard_cache->display_cache)) {
  1466. return $wizard_cache->display_cache;
  1467. }
  1468. ctools_include('common', 'panels');
  1469. $cache = new stdClass();
  1470. $cache->display = $wizard_cache->display;
  1471. $cache->display->context = !empty($wizard_cache->context) ? $wizard_cache->context : array();
  1472. $cache->display->cache_key = 'panels_page_wizard:' . $key;
  1473. $cache->content_types = panels_common_get_allowed_types('panels_page', $cache->display->context);
  1474. $cache->display_title = TRUE;
  1475. return $cache;
  1476. }
  1477. /**
  1478. * Store a display edit in progress in the page cache.
  1479. */
  1480. function panels_page_wizard_panels_cache_set($key, $cache) {
  1481. ctools_include('page-wizard');
  1482. $wizard_cache = page_manager_get_wizard_cache($key);
  1483. $wizard_cache->display_cache = $cache;
  1484. page_manager_set_wizard_cache($wizard_cache);
  1485. }
  1486. // --------------------------------------------------------------------------
  1487. // General utility functions
  1488. /**
  1489. * Perform a drupal_goto on a destination that may be an array like url().
  1490. */
  1491. function panels_goto($destination) {
  1492. if (!is_array($destination)) {
  1493. return drupal_goto($destination);
  1494. }
  1495. else {
  1496. // Prevent notices by adding defaults
  1497. $destination += array(
  1498. 'query' => NULL,
  1499. 'fragment' => NULL,
  1500. 'http_response_code' => NULL,
  1501. );
  1502. return drupal_goto($destination['path'], $destination['query'], $destination['fragment'], $destination['http_response_code']);
  1503. }
  1504. }
  1505. /**
  1506. * For external use: Given a layout ID and a $content array, return the
  1507. * panel display.
  1508. *
  1509. * The content array is filled in based upon the content available in the
  1510. * layout. If it's a two column with a content array defined like
  1511. * @code
  1512. * array(
  1513. * 'left' => t('Left side'),
  1514. * 'right' => t('Right side')
  1515. * ),
  1516. * @endcode
  1517. *
  1518. * Then the $content array should be
  1519. * @code
  1520. * array(
  1521. * 'left' => $output_left,
  1522. * 'right' => $output_right,
  1523. * )
  1524. * @endcode
  1525. *
  1526. * The output within each panel region can be either a single rendered
  1527. * HTML string or an array of rendered HTML strings as though they were
  1528. * panes. They will simply be concatenated together without separators.
  1529. */
  1530. function panels_print_layout($layout, $content, $meta = 'standard') {
  1531. ctools_include('plugins', 'panels');
  1532. // Create a temporary display for this.
  1533. $display = panels_new_display();
  1534. $display->layout = is_array($layout) ? $layout['name'] : $layout;
  1535. $display->content = $content;
  1536. // Get our simple renderer
  1537. $renderer = panels_get_renderer_handler('simple', $display);
  1538. $renderer->meta_location = $meta;
  1539. return $renderer->render();
  1540. }
  1541. /**
  1542. * Filter callback for array_filter to remove builders from a list of layouts.
  1543. */
  1544. function _panels_builder_filter($layout) {
  1545. return empty($layout['builder']);
  1546. }
  1547. // --------------------------------------------------------------------------
  1548. // Deprecated functions
  1549. //
  1550. // Everything below this line will eventually go away.
  1551. /**
  1552. * panels path helper function
  1553. */
  1554. function panels_get_path($file, $base_path = FALSE, $module = 'panels') {
  1555. $output = $base_path ? base_path() : '';
  1556. return $output . drupal_get_path('module', $module) . '/' . $file;
  1557. }
  1558. /**
  1559. * Remove default sidebar related body classes and provide own css classes
  1560. */
  1561. function panels_preprocess_html(&$vars) {
  1562. $panel_body_css = &drupal_static('panel_body_css');
  1563. if (!empty($panel_body_css['body_classes_to_remove'])) {
  1564. $classes_to_remove = explode(' ', $panel_body_css['body_classes_to_remove']);
  1565. foreach ($vars['classes_array'] as $key => $css_class) {
  1566. if (in_array($css_class, $classes_to_remove)) {
  1567. unset($vars['classes_array'][$key]);
  1568. }
  1569. }
  1570. }
  1571. if (!empty($panel_body_css['body_classes_to_add'])) {
  1572. $vars['classes_array'][] = check_plain($panel_body_css['body_classes_to_add']);
  1573. }
  1574. }