views_ui.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. <?php
  2. /**
  3. * @file
  4. * Contains the CTools Export UI integration code.
  5. *
  6. * Note that this is only a partial integration.
  7. */
  8. /**
  9. * CTools Export UI class handler for Views UI.
  10. */
  11. class views_ui extends ctools_export_ui {
  12. function init($plugin) {
  13. // We modify the plugin info here so that we take the defaults and
  14. // twiddle, rather than completely override them.
  15. // Reset the edit path to match what we're really using.
  16. $plugin['menu']['items']['edit']['path'] = 'view/%ctools_export_ui/edit';
  17. $plugin['menu']['items']['clone']['path'] = 'view/%ctools_export_ui/clone';
  18. $plugin['menu']['items']['clone']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
  19. $plugin['menu']['items']['export']['path'] = 'view/%ctools_export_ui/export';
  20. $plugin['menu']['items']['export']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
  21. $plugin['menu']['items']['enable']['path'] = 'view/%ctools_export_ui/enable';
  22. $plugin['menu']['items']['disable']['path'] = 'view/%ctools_export_ui/disable';
  23. $plugin['menu']['items']['delete']['path'] = 'view/%ctools_export_ui/delete';
  24. $plugin['menu']['items']['delete']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
  25. $plugin['menu']['items']['revert']['path'] = 'view/%ctools_export_ui/revert';
  26. $plugin['menu']['items']['revert']['type'] = MENU_VISIBLE_IN_BREADCRUMB;
  27. $prefix_count = count(explode('/', $plugin['menu']['menu prefix']));
  28. $plugin['menu']['items']['add-template'] = array(
  29. 'path' => 'template/%/add',
  30. 'title' => 'Add from template',
  31. 'page callback' => 'ctools_export_ui_switcher_page',
  32. 'page arguments' => array($plugin['name'], 'add_template', $prefix_count + 2),
  33. 'load arguments' => array($plugin['name']),
  34. 'access callback' => 'ctools_export_ui_task_access',
  35. 'access arguments' => array($plugin['name'], 'add_template', $prefix_count + 2),
  36. 'type' => MENU_CALLBACK,
  37. );
  38. return parent::init($plugin);
  39. }
  40. function hook_menu(&$items) {
  41. // We are using our own 'edit' still, rather than having edit on this
  42. // object (maybe in the future) so unset the edit callbacks:
  43. // Store this so we can put them back as sometimes they're needed
  44. // again laster:
  45. $stored_items = $this->plugin['menu']['items'];
  46. // We leave these to make sure the operations still exist in the plugin so
  47. // that the path finder.
  48. unset($this->plugin['menu']['items']['edit']);
  49. unset($this->plugin['menu']['items']['add']);
  50. unset($this->plugin['menu']['items']['import']);
  51. unset($this->plugin['menu']['items']['edit callback']);
  52. parent::hook_menu($items);
  53. $this->plugin['menu']['items'] = $stored_items;
  54. }
  55. function load_item($item_name) {
  56. return views_ui_cache_load($item_name);
  57. }
  58. function list_form(&$form, &$form_state) {
  59. $row_class = 'container-inline';
  60. if (!variable_get('views_ui_show_listing_filters', FALSE)) {
  61. $row_class .= " element-invisible";
  62. }
  63. views_include('admin');
  64. parent::list_form($form, $form_state);
  65. // ctools only has two rows. We want four.
  66. // That's why we create our own structure.
  67. $form['bottom row']['submit']['#attributes']['class'][] = 'js-hide';
  68. $form['first row'] = array(
  69. '#prefix' => '<div class="' . $row_class . ' ctools-export-ui-row ctools-export-ui-first-row clearfix">',
  70. '#suffix' => '</div>',
  71. 'search' => $form['top row']['search'],
  72. 'submit' => $form['bottom row']['submit'],
  73. 'reset' => $form['bottom row']['reset'],
  74. );
  75. $form['second row'] = array(
  76. '#prefix' => '<div class="' . $row_class . ' ctools-export-ui-row ctools-export-ui-second-row clearfix">',
  77. '#suffix' => '</div>',
  78. 'storage' => $form['top row']['storage'],
  79. 'disabled' => $form['top row']['disabled'],
  80. );
  81. $form['third row'] = array(
  82. '#prefix' => '<div class="' . $row_class . ' ctools-export-ui-row ctools-export-ui-third-row clearfix element-hidden">',
  83. '#suffix' => '</div>',
  84. 'order' => $form['bottom row']['order'],
  85. 'sort' => $form['bottom row']['sort'],
  86. );
  87. unset($form['top row']);
  88. unset($form['bottom row']);
  89. // Modify the look and contents of existing form elements.
  90. $form['second row']['storage']['#title'] = '';
  91. $form['second row']['storage']['#options'] = array(
  92. 'all' => t('All storage'),
  93. t('Normal') => t('In database'),
  94. t('Default') => t('In code'),
  95. t('Overridden') => t('Database overriding code'),
  96. );
  97. $form['second row']['disabled']['#title'] = '';
  98. $form['second row']['disabled']['#options']['all'] = t('All status');
  99. $form['third row']['sort']['#title'] = '';
  100. // And finally, add our own.
  101. $this->bases = array();
  102. foreach (views_fetch_base_tables() as $table => $info) {
  103. $this->bases[$table] = $info['title'];
  104. }
  105. $form['second row']['base'] = array(
  106. '#type' => 'select',
  107. '#options' => array_merge(array('all' => t('All types')), $this->bases),
  108. '#default_value' => 'all',
  109. '#weight' => -1,
  110. );
  111. $tags = array();
  112. if (isset($form_state['object']->items)) {
  113. foreach ($form_state['object']->items as $name => $view) {
  114. if (!empty($view->tag)) {
  115. $view_tags = drupal_explode_tags($view->tag);
  116. foreach ($view_tags as $tag) {
  117. $tags[$tag] = $tag;
  118. }
  119. }
  120. }
  121. }
  122. asort($tags);
  123. $form['second row']['tag'] = array(
  124. '#type' => 'select',
  125. '#title' => t('Filter'),
  126. '#options' => array_merge(array('all' => t('All tags')), array('none' => t('No tags')), $tags),
  127. '#default_value' => 'all',
  128. '#weight' => -9,
  129. );
  130. $displays = array();
  131. foreach (views_fetch_plugin_data('display') as $id => $info) {
  132. if (!empty($info['admin'])) {
  133. $displays[$id] = $info['admin'];
  134. }
  135. }
  136. asort($displays);
  137. $form['second row']['display'] = array(
  138. '#type' => 'select',
  139. '#options' => array_merge(array('all' => t('All displays')), $displays),
  140. '#default_value' => 'all',
  141. '#weight' => -1,
  142. );
  143. }
  144. function list_filter($form_state, $view) {
  145. // Don't filter by tags if all is set up.
  146. if ($form_state['values']['tag'] != 'all') {
  147. // If none is selected check whether the view has a tag.
  148. if ($form_state['values']['tag'] == 'none') {
  149. return !empty($view->tag);
  150. }
  151. else {
  152. // Check whether the tag can be found in the views tag.
  153. return strpos($view->tag, $form_state['values']['tag']) === FALSE;
  154. }
  155. }
  156. if ($form_state['values']['base'] != 'all' && $form_state['values']['base'] != $view->base_table) {
  157. return TRUE;
  158. }
  159. return parent::list_filter($form_state, $view);
  160. }
  161. function list_sort_options() {
  162. return array(
  163. 'disabled' => t('Enabled, name'),
  164. 'name' => t('Name'),
  165. 'path' => t('Path'),
  166. 'tag' => t('Tag'),
  167. 'storage' => t('Storage'),
  168. );
  169. }
  170. function list_build_row($view, &$form_state, $operations) {
  171. if (!empty($view->human_name)) {
  172. $title = $view->human_name;
  173. }
  174. else {
  175. $title = $view->get_title();
  176. if (empty($title)) {
  177. $title = $view->name;
  178. }
  179. }
  180. $paths = _views_ui_get_paths($view);
  181. $paths = implode(", ", $paths);
  182. $base = !empty($this->bases[$view->base_table]) ? $this->bases[$view->base_table] : t('Broken');
  183. $info = theme('views_ui_view_info', array('view' => $view, 'base' => $base));
  184. // Reorder the operations so that enable is the default action for a templatic views
  185. if (!empty($operations['enable'])) {
  186. $operations = array('enable' => $operations['enable']) + $operations;
  187. }
  188. // Set up sorting
  189. switch ($form_state['values']['order']) {
  190. case 'disabled':
  191. $this->sorts[$view->name] = strtolower(empty($view->disabled) . $title);
  192. break;
  193. case 'name':
  194. $this->sorts[$view->name] = strtolower($title);
  195. break;
  196. case 'path':
  197. $this->sorts[$view->name] = strtolower($paths);
  198. break;
  199. case 'tag':
  200. $this->sorts[$view->name] = strtolower($view->tag);
  201. break;
  202. case 'storage':
  203. $this->sorts[$view->name] = strtolower($view->type . $title);
  204. break;
  205. }
  206. $ops = theme('links__ctools_dropbutton', array('links' => $operations, 'attributes' => array('class' => array('links', 'inline'))));
  207. $this->rows[$view->name] = array(
  208. 'data' => array(
  209. array('data' => $info, 'class' => array('views-ui-name')),
  210. array('data' => check_plain($view->description), 'class' => array('views-ui-description')),
  211. array('data' => check_plain($view->tag), 'class' => array('views-ui-tag')),
  212. array('data' => $paths, 'class' => array('views-ui-path')),
  213. array('data' => $ops, 'class' => array('views-ui-operations')),
  214. ),
  215. 'title' => t('Machine name: ') . check_plain($view->name),
  216. 'class' => array(!empty($view->disabled) ? 'ctools-export-ui-disabled' : 'ctools-export-ui-enabled'),
  217. );
  218. }
  219. function list_render(&$form_state) {
  220. views_include('admin');
  221. views_ui_add_admin_css();
  222. if (empty($_REQUEST['js'])) {
  223. views_ui_check_advanced_help();
  224. }
  225. drupal_add_library('system', 'jquery.bbq');
  226. views_add_js('views-list');
  227. $this->active = $form_state['values']['order'];
  228. $this->order = $form_state['values']['sort'];
  229. $query = tablesort_get_query_parameters();
  230. $header = array(
  231. $this->tablesort_link(t('View name'), 'name', 'views-ui-name'),
  232. array('data' => t('Description'), 'class' => array('views-ui-description')),
  233. $this->tablesort_link(t('Tag'), 'tag', 'views-ui-tag'),
  234. $this->tablesort_link(t('Path'), 'path', 'views-ui-path'),
  235. array('data' => t('Operations'), 'class' => array('views-ui-operations')),
  236. );
  237. $table = array(
  238. 'header' => $header,
  239. 'rows' => $this->rows,
  240. 'empty' => t('No views match the search criteria.'),
  241. 'attributes' => array('id' => 'ctools-export-ui-list-items'),
  242. );
  243. return theme('table', $table);
  244. }
  245. function tablesort_link($label, $field, $class) {
  246. $title = t('sort by @s', array('@s' => $label));
  247. $initial = 'asc';
  248. if ($this->active == $field) {
  249. $initial = ($this->order == 'asc') ? 'desc' : 'asc';
  250. $label .= theme('tablesort_indicator', array('style' => $initial));
  251. }
  252. $query['order'] = $field;
  253. $query['sort'] = $initial;
  254. $link_options = array(
  255. 'html' => TRUE,
  256. 'attributes' => array('title' => $title),
  257. 'query' => $query,
  258. );
  259. $link = l($label, $_GET['q'], $link_options);
  260. if ($this->active == $field) {
  261. $class .= ' active';
  262. }
  263. return array('data' => $link, 'class' => $class);
  264. }
  265. function clone_page($js, $input, $item, $step = NULL) {
  266. drupal_set_title($this->get_page_title('clone', $item));
  267. $name = $item->{$this->plugin['export']['key']};
  268. $form_state = array(
  269. 'plugin' => $this->plugin,
  270. 'object' => &$this,
  271. 'ajax' => $js,
  272. 'item' => $item,
  273. 'op' => 'add',
  274. 'form type' => 'clone',
  275. 'original name' => $name,
  276. 'rerender' => TRUE,
  277. 'no_redirect' => TRUE,
  278. 'step' => $step,
  279. // Store these in case additional args are needed.
  280. 'function args' => func_get_args(),
  281. );
  282. $output = drupal_build_form('views_ui_clone_form', $form_state);
  283. if (!empty($form_state['executed'])) {
  284. $item->name = $form_state['values']['name'];
  285. $item->human_name = $form_state['values']['human_name'];
  286. $item->vid = NULL;
  287. views_ui_cache_set($item);
  288. drupal_goto(ctools_export_ui_plugin_menu_path($this->plugin, 'edit', $item->name));
  289. }
  290. return $output;
  291. }
  292. function add_template_page($js, $input, $name, $step = NULL) {
  293. $templates = views_get_all_templates();
  294. if (empty($templates[$name])) {
  295. return MENU_NOT_FOUND;
  296. }
  297. $template = $templates[$name];
  298. // The template description probably describes the template, not the
  299. // view that will be created from it, but users aren't that likely to
  300. // touch it.
  301. if (!empty($template->description)) {
  302. unset($template->description);
  303. }
  304. $template->is_template = TRUE;
  305. $template->type = t('Default');
  306. $output = $this->clone_page($js, $input, $template, $step);
  307. drupal_set_title(t('Create view from template @template', array('@template' => $template->get_human_name())));
  308. return $output;
  309. }
  310. function set_item_state($state, $js, $input, $item) {
  311. ctools_export_set_object_status($item, $state);
  312. menu_rebuild();
  313. if (!$js) {
  314. drupal_goto(ctools_export_ui_plugin_base_path($this->plugin));
  315. }
  316. else {
  317. return $this->list_page($js, $input);
  318. }
  319. }
  320. function list_page($js, $input) {
  321. // Remove filters values from session if filters are hidden.
  322. if (!variable_get('views_ui_show_listing_filters', FALSE) && isset($_SESSION['ctools_export_ui'][$this->plugin['name']])) {
  323. unset($_SESSION['ctools_export_ui'][$this->plugin['name']]);
  324. }
  325. // wrap output in a div for CSS
  326. $output = parent::list_page($js, $input);
  327. if (is_string($output)) {
  328. $output = '<div id="views-ui-list-page">' . $output . '</div>';
  329. }
  330. return $output;
  331. }
  332. }
  333. /**
  334. * Form callback to edit an exportable item using the wizard
  335. *
  336. * This simply loads the object defined in the plugin and hands it off.
  337. */
  338. function views_ui_clone_form($form, &$form_state) {
  339. $counter = 1;
  340. if (!isset($form_state['item'])) {
  341. $view = views_get_view($form_state['original name']);
  342. }
  343. else {
  344. $view = $form_state['item'];
  345. }
  346. do {
  347. if (empty($form_state['item']->is_template)) {
  348. $name = format_plural($counter, 'Clone of', 'Clone @count of') . ' ' . $view->get_human_name();
  349. }
  350. else {
  351. $name = $view->get_human_name();
  352. if ($counter > 1) {
  353. $name .= ' ' . $counter;
  354. }
  355. }
  356. $counter++;
  357. $machine_name = preg_replace('/[^a-z0-9_]+/', '_', drupal_strtolower($name));
  358. } while (ctools_export_crud_load($form_state['plugin']['schema'], $machine_name));
  359. $form['human_name'] = array(
  360. '#type' => 'textfield',
  361. '#title' => t('View name'),
  362. '#default_value' => $name,
  363. '#size' => 32,
  364. '#maxlength' => 255,
  365. );
  366. $form['name'] = array(
  367. '#title' => t('View name'),
  368. '#type' => 'machine_name',
  369. '#required' => TRUE,
  370. '#maxlength' => 128,
  371. '#size' => 128,
  372. '#machine_name' => array(
  373. 'exists' => 'ctools_export_ui_edit_name_exists',
  374. 'source' => array('human_name'),
  375. ),
  376. );
  377. $form['submit'] = array(
  378. '#type' => 'submit',
  379. '#value' => t('Continue'),
  380. );
  381. return $form;
  382. }