panels_renderer_ipe.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. <?php
  2. /**
  3. * @file
  4. */
  5. /**
  6. * Renderer class for all In-Place Editor (IPE) behavior.
  7. */
  8. class panels_renderer_ipe extends panels_renderer_editor {
  9. // The IPE operates in normal render mode, not admin mode.
  10. var $admin = FALSE;
  11. // Whether or not the user has access.
  12. var $access = NULL;
  13. function invoke_panels_ipe_access() {
  14. if (user_access('bypass access in place editing')) {
  15. return TRUE;
  16. }
  17. // Modules can return TRUE, FALSE or NULL, for allowed, disallowed,
  18. // or don't care - respectively. On the first FALSE, we deny access,
  19. // otherwise allow.
  20. foreach (module_invoke_all('panels_ipe_access', $this->display) as $result) {
  21. if ($result === FALSE) {
  22. return FALSE;
  23. }
  24. }
  25. return TRUE;
  26. }
  27. function access() {
  28. if (is_null($this->access)) {
  29. $this->access = $this->invoke_panels_ipe_access();
  30. }
  31. return $this->access;
  32. }
  33. function render() {
  34. $output = parent::render();
  35. if ($this->access()) {
  36. return "<div id='panels-ipe-display-{$this->clean_key}' class='panels-ipe-display-container'>$output</div>";
  37. }
  38. return $output;
  39. }
  40. function add_meta() {
  41. if (!$this->access()) {
  42. return parent::add_meta();
  43. }
  44. ctools_include('display-edit', 'panels');
  45. ctools_include('content');
  46. if (empty($this->display->cache_key)) {
  47. $this->cache = panels_edit_cache_get_default($this->display);
  48. }
  49. // @todo we may need an else to load the cache, but I am not sure we
  50. // actually need to load it if we already have our cache key, and doing
  51. // so is a waste of resources.
  52. ctools_include('cleanstring');
  53. $this->clean_key = ctools_cleanstring($this->display->cache_key);
  54. $button = array(
  55. '#type' => 'link',
  56. '#title' => t('Customize this page'),
  57. '#href' => $this->get_url('save_form'),
  58. '#options' => array('query' => drupal_get_destination()),
  59. '#id' => 'panels-ipe-customize-page',
  60. '#attributes' => array(
  61. 'class' => array('panels-ipe-startedit', 'panels-ipe-pseudobutton'),
  62. ),
  63. '#ajax' => array(
  64. 'progress' => 'throbber',
  65. 'ipe_cache_key' => $this->clean_key,
  66. ),
  67. '#prefix' => '<div class="panels-ipe-pseudobutton-container">',
  68. '#suffix' => '</div>',
  69. );
  70. panels_ipe_toolbar_add_button($this->display->cache_key, 'panels-ipe-startedit', $button);
  71. // @todo this actually should be an IPE setting instead.
  72. if (user_access('change layouts in place editing')) {
  73. $button = array(
  74. '#type' => 'link',
  75. '#title' => t('Change layout'),
  76. '#href' => $this->get_url('change_layout'),
  77. '#options' => array('query' => drupal_get_destination()),
  78. '#attributes' => array(
  79. 'class' => array('panels-ipe-change-layout', 'panels-ipe-pseudobutton', 'ctools-modal-layout'),
  80. ),
  81. '#ajax' => array(
  82. 'progress' => 'throbber',
  83. 'ipe_cache_key' => $this->clean_key,
  84. ),
  85. '#prefix' => '<div class="panels-ipe-pseudobutton-container">',
  86. '#suffix' => '</div>',
  87. );
  88. panels_ipe_toolbar_add_button($this->display->cache_key, 'panels-ipe-change-layout', $button);
  89. }
  90. ctools_include('ajax');
  91. ctools_include('modal');
  92. ctools_modal_add_js();
  93. ctools_add_css('panels_dnd', 'panels');
  94. ctools_add_css('panels_admin', 'panels');
  95. ctools_add_js('panels-base', 'panels');
  96. ctools_add_js('panels_ipe', 'panels_ipe');
  97. ctools_add_css('panels_ipe', 'panels_ipe');
  98. drupal_add_js(array('PanelsIPECacheKeys' => array($this->clean_key)), 'setting');
  99. drupal_add_library('system', 'ui.draggable');
  100. drupal_add_library('system', 'ui.droppable');
  101. drupal_add_library('system', 'ui.sortable');
  102. parent::add_meta();
  103. }
  104. /**
  105. * Override & call the parent, then pass output through to the dnd wrapper
  106. * theme function.
  107. *
  108. * @param $pane
  109. */
  110. function render_pane(&$pane) {
  111. $output = parent::render_pane($pane);
  112. if (empty($output)) {
  113. return;
  114. }
  115. if (!$this->access()) {
  116. return $output;
  117. }
  118. // If there are region locks, add them.
  119. if (!empty($pane->locks['type']) && $pane->locks['type'] == 'regions') {
  120. static $key = NULL;
  121. $javascript = &drupal_static('drupal_add_js', array());
  122. // drupal_add_js breaks as we add these, but we can't just lump them
  123. // together because panes can be rendered independently. So game the system:
  124. if (empty($key)) {
  125. $settings['Panels']['RegionLock'][$pane->pid] = $pane->locks['regions'];
  126. drupal_add_js($settings, 'setting');
  127. // These are just added via [] so we have to grab the last one
  128. // and reference it.
  129. $keys = array_keys($javascript['settings']['data']);
  130. $key = end($keys);
  131. }
  132. else {
  133. $javascript['settings']['data'][$key]['Panels']['RegionLock'][$pane->pid] = $pane->locks['regions'];
  134. }
  135. }
  136. if (empty($pane->IPE_empty)) {
  137. // Add an inner layer wrapper to the pane content before placing it into
  138. // draggable portlet.
  139. $output = "<div class=\"panels-ipe-portlet-content\">$output</div>";
  140. }
  141. else {
  142. $output = "<div class=\"panels-ipe-portlet-content panels-ipe-empty-pane\">$output</div>";
  143. }
  144. // Hand it off to the plugin/theme for placing draggers/buttons.
  145. $output = theme('panels_ipe_pane_wrapper', array('output' => $output, 'pane' => $pane, 'display' => $this->display, 'renderer' => $this));
  146. if (!empty($pane->locks['type']) && $pane->locks['type'] == 'immovable') {
  147. return "<div id=\"panels-ipe-paneid-{$pane->pid}\" class=\"panels-ipe-nodrag panels-ipe-portlet-wrapper panels-ipe-portlet-marker\">" . $output . "</div>";
  148. }
  149. return "<div id=\"panels-ipe-paneid-{$pane->pid}\" class=\"panels-ipe-portlet-wrapper panels-ipe-portlet-marker\">" . $output . "</div>";
  150. }
  151. function prepare_panes($panes) {
  152. if (!$this->access()) {
  153. return parent::prepare_panes($panes);
  154. }
  155. // Set to admin mode just for this to ensure all panes are represented.
  156. $this->admin = TRUE;
  157. $panes = parent::prepare_panes($panes);
  158. $this->admin = FALSE;
  159. }
  160. function render_pane_content(&$pane) {
  161. if (!$this->access()) {
  162. return parent::render_pane_content($pane);
  163. }
  164. if (!empty($pane->shown) && panels_pane_access($pane, $this->display)) {
  165. $content = parent::render_pane_content($pane);
  166. }
  167. // Ensure that empty panes have some content.
  168. if (empty($content) || empty($content->content)) {
  169. if (empty($content)) {
  170. $content = new stdClass();
  171. }
  172. // Get the administrative title.
  173. $content_type = ctools_get_content_type($pane->type);
  174. $title = ctools_content_admin_title($content_type, $pane->subtype, $pane->configuration, $this->display->context);
  175. $content->content = t('Placeholder for empty or inaccessible "@title"', array('@title' => html_entity_decode($title, ENT_QUOTES)));
  176. // Add these to prevent notices.
  177. $content->type = 'panels_ipe';
  178. $content->subtype = 'panels_ipe';
  179. $pane->IPE_empty = TRUE;
  180. }
  181. return $content;
  182. }
  183. /**
  184. * Add an 'empty' pane placeholder above all the normal panes.
  185. *
  186. * @param $region_id
  187. * @param $panes
  188. */
  189. function render_region($region_id, $panes) {
  190. if (!$this->access()) {
  191. return parent::render_region($region_id, $panes);
  192. }
  193. // Generate this region's 'empty' placeholder pane from the IPE plugin.
  194. $empty_ph = theme('panels_ipe_placeholder_pane', array('region_id' => $region_id, 'region_title' => $this->plugins['layout']['regions'][$region_id]));
  195. // Wrap the placeholder in some guaranteed markup.
  196. $control = '<div class="panels-ipe-placeholder panels-ipe-on panels-ipe-portlet-marker panels-ipe-portlet-static">' . $empty_ph . theme('panels_ipe_add_pane_button', array('region_id' => $region_id, 'display' => $this->display, 'renderer' => $this)) . "</div>";
  197. $output = parent::render_region($region_id, $panes);
  198. $output = theme('panels_ipe_region_wrapper', array('output' => $output, 'region_id' => $region_id, 'display' => $this->display, 'controls' => $control, 'renderer' => $this));
  199. $classes = 'panels-ipe-region';
  200. return "<div id='panels-ipe-regionid-$region_id' class='panels-ipe-region'>$output</div>";
  201. }
  202. /**
  203. * This is a generic lock test.
  204. */
  205. function ipe_test_lock($url, $break) {
  206. if (!empty($this->cache->locked)) {
  207. if ($break != 'break') {
  208. $account = user_load($this->cache->locked->uid);
  209. $name = format_username($account);
  210. $lock_age = format_interval(time() - $this->cache->locked->updated);
  211. $message = t("This panel is being edited by user !user, and is therefore locked from editing by others. This lock is !age old.\n\nClick OK to break this lock and discard any changes made by !user.", array('!user' => $name, '!age' => $lock_age));
  212. $this->commands[] = array(
  213. 'command' => 'unlockIPE',
  214. 'message' => $message,
  215. 'break_path' => url($this->get_url($url, 'break')),
  216. 'key' => $this->clean_key,
  217. );
  218. return TRUE;
  219. }
  220. // Break the lock.
  221. panels_edit_cache_break_lock($this->cache);
  222. }
  223. }
  224. /**
  225. *
  226. */
  227. function get_panels_storage_op_for_ajax($method) {
  228. switch ($method) {
  229. case 'ajax_unlock_ipe':
  230. case 'ajax_save_form':
  231. return 'update';
  232. case 'ajax_change_layout':
  233. case 'ajax_set_layout':
  234. return 'change layout';
  235. }
  236. return parent::get_panels_storage_op_for_ajax($method);
  237. }
  238. /**
  239. * AJAX callback to unlock the IPE.
  240. *
  241. * This is called whenever something server side determines that editing
  242. * has stopped and cleans up no longer needed locks.
  243. *
  244. * It has no visible return value as this is considered a background task
  245. * and the client side has already given all indications that things are
  246. * now in a 'normal' state.
  247. */
  248. function ajax_unlock_ipe() {
  249. panels_edit_cache_clear($this->cache);
  250. $this->commands[] = array();
  251. }
  252. /**
  253. * AJAX entry point to create the controller form for an IPE.
  254. */
  255. function ajax_save_form($break = NULL) {
  256. if ($this->ipe_test_lock('save-form', $break)) {
  257. return;
  258. }
  259. // Reset the $_POST['ajax_html_ids'] values to preserve
  260. // proper IDs on form elements when they are rebuilt
  261. // by the Panels IPE without refreshing the page.
  262. $_POST['ajax_html_ids'] = array();
  263. $form_state = array(
  264. 'renderer' => $this,
  265. 'display' => &$this->display,
  266. 'content_types' => $this->cache->content_types,
  267. 'rerender' => FALSE,
  268. 'no_redirect' => TRUE,
  269. // Panels needs this to make sure that the layout gets callbacks.
  270. 'layout' => $this->plugins['layout'],
  271. );
  272. $output = drupal_build_form('panels_ipe_edit_control_form', $form_state);
  273. if (empty($form_state['executed'])) {
  274. // At this point, we want to save the cache to ensure that we have a lock.
  275. $this->cache->ipe_locked = TRUE;
  276. panels_edit_cache_set($this->cache);
  277. $this->commands[] = array(
  278. 'command' => 'initIPE',
  279. 'key' => $this->clean_key,
  280. 'data' => drupal_render($output),
  281. 'lockPath' => url($this->get_url('unlock_ipe')),
  282. );
  283. return;
  284. }
  285. // Check to see if we have a lock that was broken. If so we need to
  286. // inform the user and abort.
  287. if (empty($this->cache->ipe_locked)) {
  288. $this->commands[] = ajax_command_alert(t('A lock you had has been externally broken, and all your changes have been reverted.'));
  289. $this->commands[] = array(
  290. 'command' => 'cancelIPE',
  291. 'key' => $this->clean_key,
  292. );
  293. return;
  294. }
  295. // Otherwise it was submitted.
  296. if (!empty($form_state['clicked_button']['#save-display'])) {
  297. // Saved. Save the cache.
  298. panels_edit_cache_save($this->cache);
  299. // A rerender should fix IDs on added panes as well as ensure style changes are
  300. // rendered.
  301. $this->meta_location = 'inline';
  302. $this->commands[] = ajax_command_replace("#panels-ipe-display-{$this->clean_key}", panels_render_display($this->display, $this));
  303. $buttons = &drupal_static('panels_ipe_toolbar_buttons', array());
  304. $output = theme('panels_ipe_toolbar', array('buttons' => $buttons));
  305. $this->commands[] = ajax_command_replace('#panels-ipe-control-container', $output);
  306. $storage_id = $this->cache->display->storage_id;
  307. cache_clear_all('panels_mini_load:' . $storage_id, 'cache_panels', TRUE);
  308. }
  309. else {
  310. // Cancelled. Clear the cache.
  311. panels_edit_cache_clear($this->cache);
  312. }
  313. $this->commands[] = array(
  314. 'command' => 'endIPE',
  315. 'key' => $this->clean_key,
  316. );
  317. }
  318. /**
  319. * AJAX entry point to create the controller form for an IPE.
  320. */
  321. function ajax_change_layout($break = NULL) {
  322. if ($this->ipe_test_lock('change_layout', $break)) {
  323. return;
  324. }
  325. // At this point, we want to save the cache to ensure that we have a lock.
  326. $this->cache->ipe_locked = TRUE;
  327. panels_edit_cache_set($this->cache);
  328. ctools_include('plugins', 'panels');
  329. ctools_include('common', 'panels');
  330. // @todo figure out a solution for this, it's critical
  331. if (isset($this->display->allowed_layouts)) {
  332. $layouts = $this->display->allowed_layouts;
  333. }
  334. else {
  335. $layouts = panels_common_get_allowed_layouts('panels_page');
  336. }
  337. // Filter out builders.
  338. $layouts = array_filter($layouts, '_panels_builder_filter');
  339. // Let other modules filter the layouts.
  340. drupal_alter('panels_layouts_available', $layouts);
  341. // Define the current layout.
  342. $current_layout = $this->plugins['layout']['name'];
  343. $output = panels_common_print_layout_links($layouts, $this->get_url('set_layout'), array('attributes' => array('class' => array('use-ajax'))), $current_layout);
  344. $this->commands[] = ctools_modal_command_display(t('Change layout'), $output);
  345. $this->commands[] = array(
  346. 'command' => 'IPEsetLockState',
  347. 'key' => $this->clean_key,
  348. 'lockPath' => url($this->get_url('unlock_ipe')),
  349. );
  350. }
  351. function ajax_set_layout($layout) {
  352. ctools_include('context');
  353. ctools_include('display-layout', 'panels');
  354. $form_state = array(
  355. 'layout' => $layout,
  356. 'display' => $this->display,
  357. 'finish' => t('Save'),
  358. 'no_redirect' => TRUE,
  359. );
  360. // Reset the $_POST['ajax_html_ids'] values to preserve
  361. // proper IDs on form elements when they are rebuilt
  362. // by the Panels IPE without refreshing the page.
  363. $_POST['ajax_html_ids'] = array();
  364. $output = drupal_build_form('panels_change_layout', $form_state);
  365. $output = drupal_render($output);
  366. if (!empty($form_state['executed'])) {
  367. if (isset($form_state['back'])) {
  368. return $this->ajax_change_layout();
  369. }
  370. if (!empty($form_state['clicked_button']['#save-display'])) {
  371. // Saved. Save the cache.
  372. panels_edit_cache_save($this->cache);
  373. $this->display->skip_cache = TRUE;
  374. // Since the layout changed, we have to update these things in the
  375. // renderer in order to get the right settings.
  376. $layout = panels_get_layout($this->display->layout);
  377. $this->plugins['layout'] = $layout;
  378. if (!isset($layout['regions'])) {
  379. $this->plugins['layout']['regions'] = panels_get_regions($layout, $this->display);
  380. }
  381. $this->meta_location = 'inline';
  382. $this->commands[] = ajax_command_replace("#panels-ipe-display-{$this->clean_key}", panels_render_display($this->display, $this));
  383. $this->commands[] = ctools_modal_command_dismiss();
  384. return;
  385. }
  386. }
  387. $this->commands[] = ctools_modal_command_display(t('Change layout'), $output);
  388. }
  389. /**
  390. * Create a command array to redraw a pane.
  391. */
  392. function command_update_pane($pid) {
  393. if (is_object($pid)) {
  394. $pane = $pid;
  395. }
  396. else {
  397. $pane = $this->display->content[$pid];
  398. }
  399. $this->commands[] = ajax_command_replace("#panels-ipe-paneid-$pane->pid", $this->render_pane($pane));
  400. $this->commands[] = ajax_command_changed("#panels-ipe-display-{$this->clean_key}");
  401. }
  402. /**
  403. * Create a command array to add a new pane.
  404. */
  405. function command_add_pane($pid) {
  406. if (is_object($pid)) {
  407. $pane = $pid;
  408. }
  409. else {
  410. $pane = $this->display->content[$pid];
  411. }
  412. $this->commands[] = array(
  413. 'command' => 'insertNewPane',
  414. 'regionId' => $pane->panel,
  415. 'renderedPane' => $this->render_pane($pane),
  416. );
  417. $this->commands[] = ajax_command_changed("#panels-ipe-display-{$this->clean_key}");
  418. $this->commands[] = array(
  419. 'command' => 'addNewPane',
  420. 'key' => $this->clean_key,
  421. );
  422. }
  423. }
  424. /**
  425. * FAPI callback to create the Save/Cancel form for the IPE.
  426. */
  427. function panels_ipe_edit_control_form($form, &$form_state) {
  428. $display = &$form_state['display'];
  429. // @todo -- this should be unnecessary as we ensure cache_key is set in add_meta()
  430. // $display->cache_key = isset($display->cache_key) ? $display->cache_key : $display->did;
  431. // Annoyingly, theme doesn't have access to form_state so we have to do this.
  432. $form['#display'] = $display;
  433. $layout = panels_get_layout($display->layout);
  434. $layout_panels = panels_get_regions($layout, $display);
  435. $form['panel'] = array('#tree' => TRUE);
  436. $form['panel']['pane'] = array('#tree' => TRUE);
  437. foreach ($layout_panels as $panel_id => $title) {
  438. // Make sure we at least have an empty array for all possible locations.
  439. if (!isset($display->panels[$panel_id])) {
  440. $display->panels[$panel_id] = array();
  441. }
  442. $form['panel']['pane'][$panel_id] = array(
  443. // Use 'hidden' instead of 'value' so the js can access it.
  444. '#type' => 'hidden',
  445. '#default_value' => implode(',', (array) $display->panels[$panel_id]),
  446. );
  447. }
  448. $form['buttons']['submit'] = array(
  449. '#type' => 'submit',
  450. '#value' => t('Save'),
  451. '#id' => 'panels-ipe-save',
  452. '#attributes' => array('class' => array('panels-ipe-save')),
  453. '#submit' => array('panels_edit_display_form_submit'),
  454. '#save-display' => TRUE,
  455. );
  456. $form['buttons']['cancel'] = array(
  457. '#type' => 'submit',
  458. '#id' => 'panels-ipe-cancel',
  459. '#attributes' => array('class' => array('panels-ipe-cancel')),
  460. '#value' => t('Cancel'),
  461. );
  462. return $form;
  463. }