views_panes.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. <?php
  2. /**
  3. * @file
  4. * Content type plugin to allow Views to be exposed as a display type,
  5. * leaving most of the configuration on the view.
  6. */
  7. /**
  8. * Implements hook_ctools_content_types()
  9. */
  10. function views_content_views_panes_ctools_content_types() {
  11. return array(
  12. 'title' => t('View panes'),
  13. 'admin settings' => 'views_content_admin_form',
  14. 'all contexts' => TRUE,
  15. );
  16. }
  17. /**
  18. * Return all content types available.
  19. */
  20. function views_content_views_panes_content_type_content_types($plugin) {
  21. $types = array();
  22. // It can be fairly intensive to calculate this, so let's cache this in the
  23. // cache_views table. The nice thing there is that if views ever change, that
  24. // table will always be cleared. Except for the occasional default view, so
  25. // we must use the Views caching functions in order to respect Views caching
  26. // settings.
  27. views_include('cache');
  28. $data = views_cache_get('views_content_panes', TRUE);
  29. if (!empty($data->data)) {
  30. $types = $data->data;
  31. }
  32. if (empty($types)) {
  33. $types = array();
  34. $views = views_get_all_views();
  35. foreach ($views as $view) {
  36. if (!empty($view->disabled)) {
  37. continue;
  38. }
  39. $view->init_display();
  40. foreach ($view->display as $id => $display) {
  41. if (empty($display->handler->panel_pane_display)) {
  42. continue;
  43. }
  44. $info = _views_content_panes_content_type($view, $display);
  45. if ($info) {
  46. $types[$view->name . '-' . $id] = $info;
  47. }
  48. }
  49. $view->destroy();
  50. }
  51. views_cache_set('views_content_panes', $types, TRUE);
  52. }
  53. return $types;
  54. }
  55. /**
  56. * Return a single content type.
  57. */
  58. function views_content_views_panes_content_type_content_type($subtype, $plugin) {
  59. list($name, $display) = explode('-', $subtype);
  60. $view = views_get_view($name);
  61. if (empty($view)) {
  62. return;
  63. }
  64. $view->set_display($display);
  65. $retval = _views_content_panes_content_type($view, $view->display[$display]);
  66. $view->destroy();
  67. return $retval;
  68. }
  69. function _views_content_panes_content_type($view, $display) {
  70. // Ensure the handler is the right type, as Views will fall back to
  71. // the default display if something is broken:
  72. if (!is_a($display->handler, 'views_content_plugin_display_panel_pane')) {
  73. return;
  74. }
  75. $title = views_content_get_display_title($view, $display->id);
  76. $description = $display->handler->get_option('pane_description');
  77. if (!$description) {
  78. $description = $view->description;
  79. }
  80. $category = $display->handler->get_option('pane_category');
  81. if (!$category['name']) {
  82. $category['name'] = t('View panes');
  83. }
  84. $icon = 'icon_views_page.png';
  85. $contexts = array();
  86. $arguments = $display->handler->get_argument_input();
  87. ctools_include('views');
  88. foreach ($arguments as $argument) {
  89. $contexts[] = ctools_views_get_argument_context($argument);
  90. }
  91. $allow = $display->handler->get_option('allow');
  92. return array(
  93. 'title' => $title,
  94. 'icon' => $icon,
  95. 'description' => filter_xss_admin($description),
  96. 'required context' => $contexts,
  97. 'category' => array($category['name'], $category['weight']),
  98. 'no title override' => empty($allow['title_override']),
  99. );
  100. }
  101. /**
  102. * Output function for the 'views' content type.
  103. *
  104. * Outputs a view based on the module and delta supplied in the configuration.
  105. */
  106. function views_content_views_panes_content_type_render($subtype, $conf, $panel_args, $contexts) {
  107. if (!is_array($contexts)) {
  108. $contexts = array($contexts);
  109. }
  110. list($name, $display) = explode('-', $subtype);
  111. $view = views_get_view($name);
  112. if (empty($view)) {
  113. return;
  114. }
  115. $view->set_display($display);
  116. views_content_views_panes_add_defaults($conf, $view);
  117. if (!$view->display_handler->access($GLOBALS['user']) || empty($view->display_handler->panel_pane_display)) {
  118. return;
  119. }
  120. $view->display_handler->set_pane_conf($conf);
  121. $args = array();
  122. $arguments = $view->display_handler->get_option('arguments');
  123. $context_keys = isset($conf['context']) ? $conf['context'] : array();
  124. foreach ($view->display_handler->get_argument_input() as $id => $argument) {
  125. switch ($argument['type']) {
  126. case 'context':
  127. $key = array_shift($context_keys);
  128. if (isset($contexts[$key])) {
  129. if (strpos($argument['context'], '.')) {
  130. list($context, $converter) = explode('.', $argument['context'], 2);
  131. $args[] = ctools_context_convert_context($contexts[$key], $converter, array('sanitize' => FALSE));
  132. }
  133. else {
  134. $args[] = $contexts[$key]->argument;
  135. }
  136. }
  137. else {
  138. $args[] = isset($arguments[$id]['exception']['value']) ? $arguments[$id]['exception']['value'] : 'all';
  139. }
  140. break;
  141. case 'fixed':
  142. $args[] = $argument['fixed'];
  143. break;
  144. case 'panel':
  145. $args[] = isset($panel_args[$argument['panel']]) ? $panel_args[$argument['panel']] : NULL;
  146. break;
  147. case 'user':
  148. $args[] = (isset($conf['arguments'][$id]) && $conf['arguments'][$id] !== '') ? ctools_context_keyword_substitute($conf['arguments'][$id], array(), $contexts) : NULL;
  149. break;
  150. case 'wildcard':
  151. // Put in the wildcard.
  152. $args[] = isset($arguments[$id]['wildcard']) ? $arguments[$id]['wildcard'] : '*';
  153. break;
  154. case 'none':
  155. default:
  156. // Put in NULL.
  157. // views.module knows what to do with NULL (or missing) arguments
  158. $args[] = NULL;
  159. break;
  160. }
  161. }
  162. // remove any trailing NULL arguments as these are non-args:
  163. while (count($args) && end($args) === NULL) {
  164. array_pop($args);
  165. }
  166. $view->set_arguments($args);
  167. $allow = $view->display_handler->get_option('allow');
  168. if (!empty($conf['path'])) {
  169. $conf['path'] = ctools_context_keyword_substitute($conf['path'], array(), $contexts);
  170. }
  171. if ($allow['path_override'] && !empty($conf['path'])) {
  172. $view->override_path = $conf['path'];
  173. }
  174. else if ($path = $view->display_handler->get_option('inherit_panels_path')) {
  175. $view->override_path = $_GET['q'];
  176. }
  177. $block = new stdClass();
  178. $block->module = 'views';
  179. $block->delta = $view->name . '-' . $display;
  180. if (($allow['link_to_view'] && !empty($conf['link_to_view'])) ||
  181. (!$allow['link_to_view'] && $view->display_handler->get_option('link_to_view'))) {
  182. $block->title_link = $view->get_url();
  183. }
  184. // more link
  185. if ($allow['more_link']) {
  186. if (empty($conf['more_link'])) {
  187. $view->display_handler->set_option('use_more', FALSE);
  188. }
  189. else {
  190. $view->display_handler->set_option('use_more', TRUE);
  191. // make sure the view runs the count query so we know whether or not the
  192. // more link applies.
  193. $view->get_total_rows = TRUE;
  194. }
  195. }
  196. if ($allow['items_per_page'] && isset($conf['items_per_page'])) {
  197. $view->set_items_per_page($conf['items_per_page']);
  198. }
  199. if ($allow['offset']) {
  200. $view->set_offset($conf['offset']);
  201. }
  202. if ($allow['use_pager']) {
  203. // Only set use_pager if they differ, this way we can avoid overwriting the
  204. // pager type that Views uses.
  205. $pager = $view->display_handler->get_option('pager');
  206. if ($conf['use_pager'] && ($pager['type'] == 'none' || $pager['type'] == 'some')) {
  207. $pager['type'] = 'full';
  208. }
  209. elseif (!$conf['use_pager'] && $pager['type'] != 'none' && $pager['type'] != 'some') {
  210. $pager['type'] = $view->get_items_per_page() || !empty($pager['options']['items_per_page']) ? 'some' : 'none';
  211. }
  212. if ($conf['use_pager']) {
  213. if (!isset($pager['options']['id']) || (isset($conf['pager_id']) && $pager['options']['id'] != $conf['pager_id'])) {
  214. $pager['options']['id'] = (int) $conf['pager_id'];
  215. }
  216. }
  217. $view->display_handler->set_option('pager', $pager);
  218. }
  219. if ($allow['fields_override']) {
  220. if ($conf['fields_override']) {
  221. $fields = $view->get_items('field');
  222. foreach ($fields as $field => $field_display) {
  223. $fields[$field]['exclude'] = empty($conf['fields_override'][$field]);
  224. }
  225. $view->display_handler->set_option('fields', $fields);
  226. }
  227. }
  228. if ($allow['exposed_form'] && !empty($conf['exposed'])) {
  229. foreach ($conf['exposed'] as $filter_name => $filter_value) {
  230. if (!is_array($filter_value)) {
  231. $conf['exposed'][$filter_name] = ctools_context_keyword_substitute($filter_value, array(), $contexts);
  232. }
  233. }
  234. $view->set_exposed_input($conf['exposed']);
  235. }
  236. $stored_feeds = drupal_add_feed();
  237. $block->content = $view->preview();
  238. if (empty($view->result) && !$view->display_handler->get_option('empty') && empty($view->style_plugin->definition['even empty'])) {
  239. return;
  240. }
  241. // Add contextual links to the output.
  242. $block = (array) $block;
  243. views_add_block_contextual_links($block, $view, $display, 'panel_pane');
  244. $block = (object) $block;
  245. $block->title = $view->get_title();
  246. if (empty($view->total_rows) || $view->total_rows <= $view->get_items_per_page()) {
  247. unset($block->more);
  248. }
  249. if ((!empty($allow['feed_icons']) && !empty($conf['feed_icons'])) ||
  250. (empty($allow['feed_icons']) && $view->display_handler->get_option('feed_icons'))) {
  251. $new_feeds = drupal_add_feed();
  252. if ($diff = array_diff(array_keys($new_feeds), array_keys($stored_feeds))) {
  253. foreach ($diff as $url) {
  254. $block->feeds[$url] = $new_feeds[$url];
  255. }
  256. }
  257. }
  258. return $block;
  259. }
  260. /**
  261. * Add defaults to view pane settings.
  262. * This helps cover us if $allow settings changed and there are no actual
  263. * settings for a particular item.
  264. */
  265. function views_content_views_panes_add_defaults(&$conf, $view) {
  266. $pager = $view->display_handler->get_option('pager');
  267. if (empty($conf)) {
  268. $conf = array();
  269. }
  270. $conf += array(
  271. 'link_to_view' => $view->display_handler->get_option('link_to_view'),
  272. 'more_link' => $view->display_handler->get_option('use_more'),
  273. 'feed_icons' => FALSE,
  274. 'use_pager' => $pager['type'] != 'none' && $pager['type'] != 'some',
  275. 'pager_id' => isset($pager['options']['id']) ? $pager['options']['id'] : 0,
  276. 'items_per_page' => !empty($pager['options']['items_per_page']) ? $pager['options']['items_per_page'] : 10,
  277. 'offset' => !empty($pager['options']['offset']) ? $pager['options']['offset'] : 0,
  278. 'path_override' => FALSE,
  279. 'path' => $view->get_path(),
  280. 'fields_override' => $view->display_handler->get_option('fields_override'),
  281. );
  282. }
  283. /**
  284. * Returns an edit form for a block.
  285. */
  286. function views_content_views_panes_content_type_edit_form($form, &$form_state) {
  287. $conf = $form_state['conf'];
  288. $contexts = $form_state['contexts'];
  289. // This allows older content to continue to work, where we used to embed
  290. // the display directly.
  291. list($name, $display_id) = explode('-', $form_state['subtype_name']);
  292. $view = views_get_view($name);
  293. if (empty($view)) {
  294. $form['markup'] = array('#markup' => t('Broken/missing/deleted view.'));
  295. return $form;
  296. }
  297. $view->set_display($display_id);
  298. // If it couldn't set the display and we got the default display instead,
  299. // fail.
  300. if ($view->current_display == 'default') {
  301. $form['markup'] = array('#markup' => t('Broken/missing/deleted view display.'));
  302. return $form;
  303. }
  304. $allow = $view->display_handler->get_option('allow');
  305. // Provide defaults for everything in order to prevent warnings.
  306. views_content_views_panes_add_defaults($conf, $view);
  307. $form['arguments']['#tree'] = TRUE;
  308. foreach ($view->display_handler->get_argument_input() as $id => $argument) {
  309. if ($argument['type'] == 'user') {
  310. $form['arguments'][$id] = array(
  311. '#type' => 'textfield',
  312. '#default_value' => isset($conf['arguments'][$id]) ? $conf['arguments'][$id] : '',
  313. '#description' => t('You may use keywords for substitutions.'),
  314. '#title' => check_plain($argument['label']),
  315. );
  316. }
  317. }
  318. if ($allow['link_to_view'] ) {
  319. $form['link_to_view'] = array(
  320. '#type' => 'checkbox',
  321. '#default_value' => isset($conf['link_to_view']) ? $conf['link_to_view'] : $view->display_handler->get_option('link_to_view'),
  322. '#title' => t('Link title to page'),
  323. );
  324. }
  325. if ($allow['more_link']) {
  326. $form['more_link'] = array(
  327. '#type' => 'checkbox',
  328. '#default_value' => isset($conf['more_link']) ? $conf['more_link'] : $view->display_handler->get_option('use_more'),
  329. '#description' => t('The text of this link will be "@more". This setting can only be modified on the View configuration.', array('@more' => $view->display_handler->use_more_text())),
  330. '#title' => t('Provide a "more" link.'),
  331. );
  332. }
  333. if (!empty($allow['feed_icons'])) {
  334. $form['feed_icons'] = array(
  335. '#type' => 'checkbox',
  336. '#default_value' => !empty($conf['feed_icons']),
  337. '#title' => t('Display feed icons'),
  338. );
  339. }
  340. $view->init_style();
  341. if ($allow['fields_override'] && $view->style_plugin->uses_fields()) {
  342. $form['fields_override'] = array(
  343. '#type' => 'fieldset',
  344. '#title' => 'Fields to display',
  345. '#collapsible' => TRUE,
  346. '#tree' => TRUE,
  347. );
  348. foreach ($view->display_handler->get_handlers('field') as $field => $handler) {
  349. $title = $handler->ui_name();
  350. if ($handler->options['label']) {
  351. $title .= ' (' . check_plain($handler->options['label']) . ')';
  352. }
  353. $form['fields_override'][$field] = array(
  354. '#type' => 'checkbox',
  355. '#title' => $title,
  356. '#default_value' => isset($conf['fields_override'][$field]) ? $conf['fields_override'][$field] : !$handler->options['exclude'],
  357. );
  358. }
  359. }
  360. ctools_include('dependent');
  361. if ($allow['use_pager']) {
  362. $form['use_pager'] = array(
  363. '#type' => 'checkbox',
  364. '#title' => t('Use pager'),
  365. '#default_value' => $conf['use_pager'],
  366. '#id' => 'use-pager-checkbox',
  367. '#prefix' => '<div class="container-inline">',
  368. );
  369. $form['pager_id'] = array(
  370. '#type' => 'textfield',
  371. '#default_value' => $conf['pager_id'],
  372. '#title' => t('Pager ID'),
  373. '#size' => 4,
  374. '#id' => 'use-pager-textfield',
  375. '#dependency' => array('use-pager-checkbox' => array(1)),
  376. '#suffix' => '</div>',
  377. );
  378. }
  379. if ($allow['items_per_page']) {
  380. $form['items_per_page'] = array(
  381. '#type' => 'textfield',
  382. '#default_value' => $conf['items_per_page'],
  383. '#title' => t('Num items'),
  384. '#size' => 4,
  385. '#description' => t('Select the number of items to display, or 0 to display all results.'),
  386. );
  387. }
  388. if ($allow['offset']) {
  389. $form['offset'] = array(
  390. '#type' => 'textfield',
  391. '#default_value' => $conf['offset'],
  392. '#title' => t('Offset'),
  393. '#size' => 4,
  394. '#description' => t('Enter the number of items to skip; enter 0 to skip no items.'),
  395. );
  396. }
  397. if ($allow['path_override']) {
  398. $form['path'] = array(
  399. '#type' => 'textfield',
  400. '#default_value' => isset($conf['path']) ? $conf['path'] : $view->get_path(),
  401. '#title' => t('Override path'),
  402. '#size' => 30,
  403. '#description' => t('If this is set, override the View URL path; this can sometimes be useful to set to the panel URL.'),
  404. );
  405. if (!empty($contexts)) {
  406. $form['path']['#description'] .= ' ' . t('You may use substitutions in this path.');
  407. $form['contexts'] = array(
  408. '#type' => 'fieldset',
  409. '#title' => t('Substitutions'),
  410. '#collapsible' => TRUE,
  411. '#collapsed' => TRUE,
  412. );
  413. $rows = array();
  414. foreach ($contexts as $context) {
  415. foreach (ctools_context_get_converters('%' . check_plain($context->keyword) . ':', $context) as $keyword => $title) {
  416. $rows[] = array(
  417. check_plain($keyword),
  418. t('@identifier: @title', array('@title' => $title, '@identifier' => $context->identifier)),
  419. );
  420. }
  421. }
  422. $header = array(t('Keyword'), t('Value'));
  423. $form['contexts']['context'] = array('#markup' => theme('table', array('header' => $header, 'rows' => $rows)));
  424. }
  425. }
  426. if (empty($conf['exposed'])) {
  427. $conf['exposed'] = array();
  428. }
  429. if ($allow['exposed_form']) {
  430. // If the exposed form is part of pane configuration, get the exposed
  431. // form re-tool it for our use.
  432. $exposed_form_state = array(
  433. 'view' => &$view,
  434. 'display' => &$view->display[$display_id],
  435. );
  436. $view->set_exposed_input($conf['exposed']);
  437. if (version_compare(views_api_version(), '3', '>=')) {
  438. $exposed_form_state['exposed_form_plugin'] = $view->display_handler->get_plugin('exposed_form');
  439. }
  440. $view->init_handlers();
  441. $exposed_form = array();
  442. $exposed_form = views_exposed_form($exposed_form, $exposed_form_state);
  443. $form['exposed'] = array(
  444. '#tree' => TRUE,
  445. );
  446. foreach ($exposed_form['#info'] as $id => $info) {
  447. $form['exposed'][$id] = array(
  448. '#type' => 'item',
  449. '#id' => 'views-exposed-pane',
  450. );
  451. if (!empty($info['label'])) {
  452. $form['exposed'][$id]['#title'] = $info['label'];
  453. }
  454. if (!empty($info['operator']) && !empty($exposed_form[$info['operator']])) {
  455. $form['exposed'][$id][$info['operator']] = $exposed_form[$info['operator']];
  456. $form['exposed'][$id][$info['operator']]['#parents'] = array('exposed', $info['operator']);
  457. $form['exposed'][$id][$info['operator']]['#default_value'] = isset($conf['exposed'][$info['operator']]) ? $conf['exposed'][$info['operator']] : '';
  458. }
  459. $form['exposed'][$id][$info['value']] = $exposed_form[$info['value']];
  460. $form['exposed'][$id][$info['value']]['#parents'] = array('exposed', $info['value']);
  461. $form['exposed'][$id][$info['value']]['#default_value'] = isset($conf['exposed'][$info['value']]) ? $conf['exposed'][$info['value']] : '';
  462. }
  463. }
  464. // The exposed sort stuff doesn't fall into $exposed_form['#info'] so we
  465. // have to handle it separately.
  466. if (isset($exposed_form['sort_by'])) {
  467. $form['exposed']['sort_by'] = $exposed_form['sort_by'];
  468. }
  469. if (isset($exposed_form['sort_order'])) {
  470. $form['exposed']['sort_order'] = $exposed_form['sort_order'];
  471. }
  472. // Add the view object to the form to allow additional customization
  473. $form_state['view'] = $view;
  474. return $form;
  475. }
  476. /**
  477. * Store form values in $conf.
  478. */
  479. function views_content_views_panes_content_type_edit_form_submit(&$form, &$form_state) {
  480. // Copy everything from our defaults.
  481. $keys = array('link_to_view', 'more_link', 'feed_icons', 'use_pager',
  482. 'pager_id', 'items_per_page', 'offset', 'path_override', 'path', 'arguments', 'fields_override', 'exposed');
  483. foreach ($keys as $key) {
  484. if (isset($form_state['values'][$key])) {
  485. $form_state['conf'][$key] = $form_state['values'][$key];
  486. }
  487. }
  488. }
  489. /**
  490. * Returns the administrative title for a type.
  491. */
  492. function views_content_views_panes_content_type_admin_title($subtype, $conf, $contexts) {
  493. list($name, $display) = explode('-', $subtype);
  494. $view = views_get_view($name);
  495. if (empty($view) || empty($view->display[$display])) {
  496. return t('Deleted/missing view @view', array('@view' => $name));
  497. }
  498. $view->set_display($display);
  499. views_content_views_panes_add_defaults($conf, $view);
  500. $title = views_content_get_display_title($view, $display);
  501. return check_plain($title);
  502. }
  503. /**
  504. * Returns the administrative title for a type.
  505. */
  506. function views_content_views_panes_content_type_admin_info($subtype, $conf, $contexts) {
  507. $info = array();
  508. list($view_name, $display_name) = explode('-', $subtype);
  509. $view = views_get_view($view_name);
  510. if (empty($view) || empty($view->display[$display_name])) {
  511. return;
  512. }
  513. $view->set_display($display_name);
  514. views_content_views_panes_add_defaults($conf, $view);
  515. // Add arguments first
  516. if (!empty($conf['arguments'])) {
  517. $keys = array_keys($conf['arguments']);
  518. $values = array_values($conf['arguments']);
  519. $argument_input = $view->display_handler->get_option('argument_input');
  520. foreach ($conf['arguments'] as $key => $value) {
  521. if (!empty($value)){
  522. $label = $argument_input[$key]['label'];
  523. $info[] = $label . ': ' . $value;
  524. }
  525. }
  526. }
  527. $block = new stdClass;
  528. if ($info) {
  529. $block->title = array_shift($info);
  530. $info[] = $view->display_handler->get_option('pane_description');
  531. $block->content = theme('item_list', array('items' => $info));
  532. }
  533. else {
  534. $block->title = $view->display_handler->get_option('pane_description');
  535. $block->content = '';
  536. }
  537. return $block;
  538. }