panels.module 69 KB

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