panels.module 69 KB

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