views.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. <?php
  2. /**
  3. * @file
  4. * Content type plugin to expose all views as content.
  5. */
  6. if (variable_get('ctools_content_all_views', TRUE)) {
  7. $plugin = array(
  8. 'title' => t('All views'),
  9. 'defaults' => array(
  10. 'override_pager_settings' => FALSE,
  11. 'use_pager' => FALSE,
  12. 'nodes_per_page' => 10,
  13. 'pager_id' => 0,
  14. 'offset' => 0,
  15. 'more_link' => FALSE,
  16. 'feed_icons' => FALSE,
  17. 'panel_args' => FALSE,
  18. 'link_to_view' => FALSE,
  19. 'args' => '',
  20. 'url' => '',
  21. ),
  22. 'add form' => array(
  23. 'views_content_views_select_display' => t('Select display'),
  24. 'views_content_views_content_type_edit_form' => array(
  25. 'default' => TRUE, // put wrapper here, not on the previous form.
  26. 'title' => t('Configure view'),
  27. ),
  28. ),
  29. 'all contexts' => TRUE,
  30. );
  31. }
  32. /**
  33. * Return all content types available.
  34. */
  35. function views_content_views_content_type_content_types($plugin) {
  36. $types = array();
  37. // It can be fairly intensive to calculate this, so let's cache this in the
  38. // cache_views table. The nice thing there is that if views ever change, that
  39. // table will always be cleared. Except for the occasional default view, so
  40. // we must use the Views caching functions in order to respect Views caching
  41. // settings.
  42. views_include('cache');
  43. $data = views_cache_get('views_content_all', TRUE);
  44. if (!empty($data->data)) {
  45. $types = $data->data;
  46. }
  47. if (empty($types)) {
  48. $views = views_get_all_views();
  49. foreach ($views as $view) {
  50. if (empty($view->disabled)) {
  51. $types[$view->name] = _views_content_views_content_type($view);
  52. }
  53. }
  54. views_cache_set('views_content_all', $types, TRUE);
  55. }
  56. return $types;
  57. }
  58. /**
  59. * Create the content type info array to give back to ctools for a given display.
  60. */
  61. function _views_content_views_content_type($view) {
  62. $title = $view->get_human_name();
  63. $icon = 'icon_views_page_legacy.png';
  64. return array(
  65. 'view' => $view->name,
  66. 'title' => $title,
  67. 'icon' => $icon,
  68. 'description' => filter_xss_admin($view->description),
  69. 'category' => t('Views'),
  70. );
  71. }
  72. /**
  73. * Output function for the 'views' content type.
  74. *
  75. * Outputs a view based on the module and delta supplied in the configuration.
  76. */
  77. function views_content_views_content_type_render($subtype, $conf, $panel_args, $contexts) {
  78. if (!is_array($contexts)) {
  79. $contexts = array($contexts);
  80. }
  81. $view = _views_content_views_update_conf($conf, $subtype);
  82. if (empty($view) || !is_object($view) || empty($view->display_handler)) {
  83. return;
  84. }
  85. if (!$view->display_handler->access($GLOBALS['user'])) {
  86. return;
  87. }
  88. $arguments = explode('/', $_GET['q']);
  89. $args = $conf['args'];
  90. foreach ($arguments as $id => $arg) {
  91. $args = str_replace("%$id", $arg, $args);
  92. }
  93. foreach ($panel_args as $id => $arg) {
  94. if (is_string($arg)) {
  95. $args = str_replace("@$id", $arg, $args);
  96. }
  97. }
  98. $args = preg_replace(',/?(%\d|@\d),', '', $args);
  99. $args = $args ? explode('/', $args) : array();
  100. if ($conf['panel_args'] && is_array($panel_args)) {
  101. $args = array_merge($panel_args, $args);
  102. }
  103. if (isset($conf['context']) && is_array($conf['context'])) {
  104. foreach ($conf['context'] as $count => $context_info) {
  105. if (!strpos($context_info, '.')) {
  106. // old skool: support pre-converter contexts as well.
  107. $cid = $context_info;
  108. $converter = '';
  109. }
  110. else {
  111. list($cid, $converter) = explode('.', $context_info, 2);
  112. }
  113. if (!empty($contexts[$cid])) {
  114. $arg = ctools_context_convert_context($contexts[$cid], $converter, array('sanitize' => FALSE));
  115. array_splice($args, $count, 0, array($arg));
  116. }
  117. else {
  118. // Make sure we put an argument in even if it was not there.
  119. $arg = NULL;
  120. }
  121. }
  122. }
  123. $view->set_arguments($args);
  124. if ($conf['url']) {
  125. $view->override_path = $conf['url'];
  126. }
  127. $block = new stdClass();
  128. $block->module = 'views';
  129. $block->delta = $view->name . '-' . $view->current_display;
  130. if (!empty($conf['link_to_view'])) {
  131. $block->title_link = $view->get_url();
  132. }
  133. if (!empty($conf['more_link'])) {
  134. $block->more = array('href' => $view->get_url());
  135. $view->display_handler->set_option('use_more', FALSE);
  136. }
  137. // Only set use_pager if they differ, this way we can avoid overwriting the
  138. // pager type that Views uses.
  139. if ($conf['override_pager_settings']) {
  140. if (method_exists($view, 'init_pager')) {
  141. // Views 3 version
  142. $view->set_items_per_page($conf['nodes_per_page']);
  143. $view->set_offset($conf['offset']);
  144. $pager = $view->display_handler->get_option('pager');
  145. if ($conf['use_pager'] && ($pager['type'] == 'none' || $pager['type'] == 'some')) {
  146. $pager['type'] = 'full';
  147. }
  148. elseif (!$conf['use_pager'] && $pager['type'] != 'none' && $pager['type'] != 'some') {
  149. $pager['type'] = $view->get_items_per_page() ? 'some' : 'none';
  150. }
  151. if ($conf['use_pager']) {
  152. if (!isset($pager['options']['id']) || $pager['options']['id'] != $conf['pager_id']) {
  153. $pager['options']['id'] = $conf['pager_id'];
  154. }
  155. }
  156. $view->display_handler->set_option('pager', $pager);
  157. }
  158. else {
  159. if (!$view->display_handler->get_option('use_pager') || empty($conf['use_pager'])) {
  160. $view->display_handler->set_option('use_pager', $conf['use_pager']);
  161. }
  162. $view->display_handler->set_option('pager_element', $conf['pager_id']);
  163. $view->display_handler->set_option('items_per_page', $conf['nodes_per_page']);
  164. $view->display_handler->set_option('offset', $conf['offset']);
  165. }
  166. }
  167. $stored_feeds = drupal_add_feed();
  168. $block->content = $view->preview();
  169. $block->title = $view->get_title();
  170. if (empty($view->result) && !$view->display_handler->get_option('empty') && empty($view->style_plugin->definition['even empty'])) {
  171. return;
  172. }
  173. if (!empty($conf['feed_icons'])) {
  174. $new_feeds = drupal_add_feed();
  175. if ($diff = array_diff(array_keys($new_feeds), array_keys($stored_feeds))) {
  176. foreach ($diff as $url) {
  177. $block->feeds[$url] = $new_feeds[$url];
  178. }
  179. }
  180. }
  181. $view->destroy();
  182. return $block;
  183. }
  184. /**
  185. * Returns an edit form for a block.
  186. */
  187. function views_content_views_select_display($form, &$form_state) {
  188. $view = views_get_view($form_state['subtype_name']);
  189. if (empty($view)) {
  190. return;
  191. }
  192. $displays = array();
  193. foreach ($view->display as $id => $display) {
  194. // Content pane views should never be used this way.
  195. if ($display->display_plugin != 'panel_pane') {
  196. $displays[$id] = views_content_get_display_label($view, $id);
  197. }
  198. }
  199. $form['display'] = array(
  200. '#type' => 'select',
  201. '#title' => t('Display'),
  202. '#options' => $displays,
  203. '#description' => t('Choose which display of this view you wish to use.')
  204. );
  205. return $form;
  206. }
  207. /**
  208. * Submit the basic view edit form.
  209. *
  210. * This just dumps everything into the $conf array.
  211. */
  212. function views_content_views_select_display_submit(&$form, &$form_state) {
  213. $form_state['conf']['display'] = $form_state['values']['display'];
  214. }
  215. /**
  216. * Returns an edit form for a block.
  217. */
  218. function views_content_views_content_type_edit_form($form, &$form_state) {
  219. $conf = $form_state['conf'];
  220. $view = _views_content_views_update_conf($conf, $form_state['subtype_name']);
  221. if (empty($view) || !is_object($view)) {
  222. $form['markup'] = array('#value' => t('Broken/missing/deleted view.'));
  223. return;
  224. }
  225. $form_state['title'] = t('Configure view @view (@display)', array('@view' => $view->get_human_name(), '@display' => views_content_get_display_label($view, $view->current_display)));
  226. // @todo
  227. // If using the older format, just a context is listed. We should go through
  228. // and check for that and forcibly set them to the right converter so that
  229. // it doesn't get changed to some whacky default. Oooor just let it get changed
  230. // to 'no context', I suppose.
  231. $required = array();
  232. if (isset($view->display_handler) && $arguments = $view->display_handler->get_handlers('argument')) {
  233. foreach ($arguments as $arg) {
  234. $required[] = new ctools_context_optional($arg->ui_name(), 'any');
  235. }
  236. }
  237. if ($required) {
  238. $form['context'] = ctools_context_converter_selector($form_state['contexts'], $required, isset($conf['context']) ? $conf['context'] : array());
  239. }
  240. $form['link_to_view'] = array(
  241. '#type' => 'checkbox',
  242. '#default_value' => $conf['link_to_view'],
  243. '#title' => t('Link title to view'),
  244. );
  245. $form['more_link'] = array(
  246. '#type' => 'checkbox',
  247. '#default_value' => $conf['more_link'],
  248. '#title' => t('Provide a "more" link that links to the view'),
  249. '#description' => t('This is independent of any more link that may be provided by the view itself; if you see two more links, turn this one off. Views will only provide a more link if using the "block" type, however, so if using embed, use this one.'),
  250. );
  251. $form['feed_icons'] = array(
  252. '#type' => 'checkbox',
  253. '#default_value' => $conf['feed_icons'],
  254. '#title' => t('Display feed icons'),
  255. );
  256. $form['pager_settings'] = array(
  257. '#type' => 'fieldset',
  258. '#collapsible' => FALSE,
  259. '#title' => t('Custom pager settings'),
  260. );
  261. $form['pager_settings']['override_pager_settings'] = array(
  262. '#type' => 'checkbox',
  263. '#title' => t('Use different pager settings from view settings'),
  264. '#default_value' => $conf['override_pager_settings'],
  265. '#id' => 'override-pager-checkbox',
  266. );
  267. if ($view->display_handler->get_option('use_ajax')) {
  268. $form['pager_settings']['warning'] = array(
  269. '#value' => '<div>' . t('<strong>Warning: </strong> This view has AJAX enabled. Overriding the pager settings will work initially, but when the view is updated via AJAX, the original settings will be used. You should not override pager settings on Views with the AJAX setting enabled.') . '</div>',
  270. );
  271. }
  272. $form['pager_settings']['use_pager'] = array(
  273. '#prefix' => '<div class="container-inline">',
  274. '#type' => 'checkbox',
  275. '#title' => t('Use pager'),
  276. '#default_value' => $conf['use_pager'],
  277. '#id' => 'use-pager-checkbox',
  278. '#dependency' => array('override-pager-checkbox' => array(1)),
  279. );
  280. $form['pager_settings']['pager_id'] = array(
  281. '#type' => 'textfield',
  282. '#default_value' => $conf['pager_id'],
  283. '#title' => t('Pager ID'),
  284. '#size' => 4,
  285. '#id' => 'use-pager-textfield',
  286. '#dependency' => array('override-pager-checkbox' => array(1), 'use-pager-checkbox' => array(1)),
  287. '#dependency_count' => 2,
  288. '#suffix' => '</div>',
  289. );
  290. $form['pager_settings']['nodes_per_page'] = array(
  291. '#type' => 'textfield',
  292. '#default_value' => $conf['nodes_per_page'],
  293. '#size' => 4,
  294. '#title' => t('Num posts'),
  295. '#dependency' => array('override-pager-checkbox' => array(1)),
  296. );
  297. $form['pager_settings']['offset'] = array(
  298. '#type' => 'textfield',
  299. '#default_value' => $conf['offset'],
  300. '#title' => t('Offset'),
  301. '#size' => 4,
  302. '#description' => t('The number of items to skip and not display.'),
  303. '#dependency' => array('override-pager-checkbox' => array(1)),
  304. );
  305. $form['panel_args'] = array(
  306. '#type' => 'checkbox',
  307. '#title' => t('Send arguments'),
  308. '#default_value' => $conf['panel_args'],
  309. '#description' => t('Select this to send all arguments from the panel directly to the view. If checked, the panel arguments will come after any context arguments above and precede any additional arguments passed in through the Arguments field below. Note that arguments do not include the base URL; only values after the URL or set as placeholders are considered arguments.'),
  310. );
  311. $form['args'] = array(
  312. '#type' => 'textfield',
  313. '#default_value' => $conf['args'],
  314. '#title' => t('Arguments'),
  315. '#size' => 30,
  316. '#description' => t('Additional arguments to send to the view as if they were part of the URL in the form of arg1/arg2/arg3. You may use %0, %1, ..., %N to grab arguments from the URL. Or use @0, @1, @2, ..., @N to use arguments passed into the panel. Note: use these values only as a last resort. In future versions of Panels these may go away.'),
  317. );
  318. $form['url'] = array(
  319. '#type' => 'textfield',
  320. '#default_value' => $conf['url'],
  321. '#title' => t('Override URL'),
  322. '#size' => 30,
  323. '#description' => t('If this is set, override the View URL; this can sometimes be useful to set to the panel URL'),
  324. );
  325. $view->destroy();
  326. return $form;
  327. }
  328. /**
  329. * Store form values in $conf.
  330. */
  331. function views_content_views_content_type_edit_form_submit(&$form, &$form_state) {
  332. // Copy everything from our defaults.
  333. foreach (array_keys($form_state['plugin']['defaults']) as $key) {
  334. $form_state['conf'][$key] = $form_state['values'][$key];
  335. }
  336. }
  337. /**
  338. * Returns the administrative title for a type.
  339. */
  340. function views_content_views_content_type_admin_title($subtype, $conf) {
  341. $view = _views_content_views_update_conf($conf, $subtype);
  342. if (!is_object($view)) {
  343. return t('Deleted/missing view @view', array('@view' => $view));
  344. }
  345. $title = views_content_get_display_label($view, $view->current_display);
  346. return t('View: @name', array('@name' => $view->get_human_name() . ': ' . $title));
  347. }
  348. /**
  349. * Returns the administrative title for a type.
  350. */
  351. function views_content_views_content_type_admin_info($subtype, $conf, $contexts) {
  352. $view = _views_content_views_update_conf($conf, $subtype);
  353. if (!is_object($view)) {
  354. return t('Deleted/missing view @view', array('@view' => $view));
  355. }
  356. $display = empty($conf['display']) ? $view->current_display : $conf['display'];
  357. $block = new stdClass();
  358. $block->title = t('View information');
  359. $block->content = '<ul>';
  360. $block->content .= '<li>' . t('Using display @display.', array('@display' => views_content_get_display_label($view, $display))) . '</li>';
  361. if (!empty($conf['context']) && $arguments = $view->display_handler->get_handlers('argument')) {
  362. $argument = reset($arguments);
  363. foreach ($conf['context'] as $count => $context_info) {
  364. if (!$argument) {
  365. break;
  366. }
  367. if (!strpos($context_info, '.')) {
  368. // old skool: support pre-converter contexts as well.
  369. $cid = $context_info;
  370. $converter = '';
  371. }
  372. else {
  373. list($cid, $converter) = explode('.', $context_info, 2);
  374. }
  375. if (!empty($contexts[$cid])) {
  376. $converters = ctools_context_get_converters($cid . '.', $contexts[$cid]);
  377. $converter = !empty($converters[$context_info]) ? $converters[$context_info] : t('Default');
  378. $block->content .= '<li>' . t('Argument @arg using context @context converted into @converter', array(
  379. '@arg' => $argument->ui_name(), '@context' => $contexts[$cid]->get_identifier(),
  380. '@converter' => $converter)) . '</li>';
  381. }
  382. $argument = next($arguments);
  383. }
  384. }
  385. $block->content .= '<li>' . t('@count items displayed.', array('@count' => $conf['nodes_per_page'])) . '</li>';
  386. if ($conf['use_pager']) {
  387. $block->content .= '<li>' . t('With pager.') . '</li>';
  388. }
  389. else {
  390. $block->content .= '<li>' . t('Without pager.') . '</li>';
  391. }
  392. if ($conf['offset']) {
  393. $block->content .= '<li>' . t('Skipping first @count results', array('@count' => $conf['offset'])) . '</li>';
  394. }
  395. if ($conf['more_link']) {
  396. $block->content .= '<li>' . t('With more link.') . '</li>';
  397. }
  398. if ($conf['feed_icons']) {
  399. $block->content .= '<li>' . t('With feed icon.') . '</li>';
  400. }
  401. if ($conf['panel_args']) {
  402. $block->content .= '<li>' . t('Sending arguments.') . '</li>';
  403. }
  404. if ($conf['args']) {
  405. $block->content .= '<li>' . t('Using arguments: @args', array('@args' => $conf['args'])) . '</li>';
  406. }
  407. if ($conf['url']) {
  408. $block->content .= '<li>' . t('Using url: @url', array('@url' => $conf['url'])) . '</li>';
  409. }
  410. $view->destroy();
  411. return $block;
  412. }
  413. /**
  414. * Update the $conf to deal with updates from Drupal 5.
  415. *
  416. * @param &$conf
  417. * The $conf array to modify.
  418. * @param $subtype
  419. * The subtype in use. This should just be the view name, but in older
  420. * versions it was the view name with a dash and the display ID.
  421. * If this is the case, we can use it to correct the 'display' setting
  422. * in the $conf.
  423. * @return
  424. * The $view with the initialized display. If the $view could not be
  425. * loaded, the name attempted will be loaded for use in errors.
  426. * Correct error checking on this function checks against is_object().
  427. */
  428. function _views_content_views_update_conf(&$conf, $subtype) {
  429. $plugin = ctools_get_content_type('views');
  430. // Special: Existing content types get a different default than new ones:
  431. if (!empty($conf) && !isset($conf['override_pager_settings'])) {
  432. $conf['override_pager_settings'] = TRUE;
  433. }
  434. // Make sure that our defaults are always set if there is no
  435. // previous setting. This helps updates go more smoothly.
  436. foreach ($plugin['defaults'] as $key => $value) {
  437. if (!isset($conf[$key])) {
  438. $conf[$key] = $value;
  439. }
  440. }
  441. if (strpos($subtype, '-')) {
  442. list($name, $display) = explode('-', $subtype);
  443. $view = views_get_view($name);
  444. if (!isset($conf['display'])) {
  445. $conf['display'] = $display;
  446. }
  447. }
  448. else {
  449. $name = $subtype;
  450. $view = views_get_view($subtype);
  451. $display = isset($conf['display']) ? $conf['display'] : 'default';
  452. }
  453. if (empty($view)) {
  454. return $name;
  455. }
  456. $view->set_display($display);
  457. // $view->current_display will now reflect this value.
  458. // If set NOT to override, go ahead and refresh from the view.
  459. if (empty($conf['override_pager_settings'])) {
  460. if (method_exists($view, 'init_pager')) {
  461. $pager = $view->display_handler->get_option('pager');
  462. $conf['use_pager'] = $pager['type'] != 'none' && $pager['type'] != 'some';
  463. $conf['pager_id'] = isset($pager['options']['id']) ? $pager['options']['id'] : 0;
  464. $conf['offset'] = isset($pager['options']['offset']) ? $pager['options']['offset'] : 0;
  465. $conf['nodes_per_page'] = isset($pager['options']['items_per_page']) ? $pager['options']['items_per_page'] : 0;
  466. }
  467. else {
  468. $conf['use_pager'] = $view->display_handler->get_option('use_pager');
  469. $conf['pager_id'] = $view->display_handler->get_option('element_id');
  470. $conf['nodes_per_page'] = $view->display_handler->get_option('items_per_page');
  471. $conf['offset'] = $view->display_handler->get_option('offset');
  472. }
  473. }
  474. return $view;
  475. }