stylizer_ui.class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * UI class for Stylizer.
  4. */
  5. class stylizer_ui extends ctools_export_ui {
  6. function access($op, $item) {
  7. $access = parent::access($op, $item);
  8. if ($op == 'add' && $access && empty($this->base_types)) {
  9. // Make sure there are base styles defined.
  10. $access = FALSE;
  11. }
  12. return $access;
  13. }
  14. function list_form(&$form, &$form_state) {
  15. ctools_include('stylizer');
  16. parent::list_form($form, $form_state);
  17. $all = array('all' => t('- All -'));
  18. if (empty($this->base_types)) {
  19. // Give a warning about the missing base styles.
  20. drupal_set_message($this->plugin['strings']['message']['missing base type'], 'warning');
  21. }
  22. $types = $all;
  23. foreach ($this->base_types as $module => $info) {
  24. foreach ($info as $key => $base_type) {
  25. $types[$module . '-' . $key] = $base_type['title'];
  26. }
  27. }
  28. $form['top row']['type'] = array(
  29. '#type' => 'select',
  30. '#title' => t('Type'),
  31. '#options' => $types,
  32. '#default_value' => 'all',
  33. '#weight' => -10,
  34. '#attributes' => array('class' => array('ctools-auto-submit')),
  35. );
  36. $plugins = ctools_get_style_bases();
  37. $form_state['style_plugins'] = $plugins;
  38. $options = $all;
  39. // @todo base should use $module . '-' . $name
  40. foreach ($plugins as $name => $plugin) {
  41. $options[$name] = $plugin['title'];
  42. }
  43. $form['top row']['base'] = array(
  44. '#type' => 'select',
  45. '#title' => t('Base'),
  46. '#options' => $all + $options,
  47. '#default_value' => 'all',
  48. '#weight' => -9,
  49. '#attributes' => array('class' => array('ctools-auto-submit')),
  50. );
  51. }
  52. function list_sort_options() {
  53. return array(
  54. 'disabled' => t('Enabled, title'),
  55. 'title' => t('Title'),
  56. 'name' => t('Name'),
  57. 'base' => t('Base'),
  58. 'type' => t('Type'),
  59. 'storage' => t('Storage'),
  60. );
  61. }
  62. function list_filter($form_state, $item) {
  63. if (empty($form_state['style_plugins'][$item->settings['style_base']])) {
  64. $this->style_plugin = array(
  65. 'name' => 'broken',
  66. 'title' => t('Missing plugin'),
  67. 'type' => t('Unknown'),
  68. 'module' => '',
  69. );
  70. }
  71. else {
  72. $this->style_plugin = $form_state['style_plugins'][$item->settings['style_base']];
  73. }
  74. // This isn't really a field, but by setting this we can list it in the
  75. // filter fields and have the search box pick it up.
  76. $item->plugin_title = $this->style_plugin['title'];
  77. if ($form_state['values']['type'] != 'all') {
  78. list($module, $type) = explode('-', $form_state['values']['type']);
  79. if ($module != $this->style_plugin['module'] || $type != $this->style_plugin['type']) {
  80. return TRUE;
  81. }
  82. }
  83. if ($form_state['values']['base'] != 'all' && $form_state['values']['base'] != $this->style_plugin['name']) {
  84. return TRUE;
  85. }
  86. return parent::list_filter($form_state, $item);
  87. }
  88. function list_search_fields() {
  89. $fields = parent::list_search_fields();
  90. $fields[] = 'plugin_title';
  91. return $fields;
  92. }
  93. function list_build_row($item, &$form_state, $operations) {
  94. // Set up sorting
  95. switch ($form_state['values']['order']) {
  96. case 'disabled':
  97. $this->sorts[$item->name] = empty($item->disabled) . $item->admin_title;
  98. break;
  99. case 'title':
  100. $this->sorts[$item->name] = $item->admin_title;
  101. break;
  102. case 'name':
  103. $this->sorts[$item->name] = $item->name;
  104. break;
  105. case 'type':
  106. $this->sorts[$item->name] = $this->style_plugin['type'] . $item->admin_title;
  107. break;
  108. case 'base':
  109. $this->sorts[$item->name] = $this->style_plugin['title'] . $item->admin_title;
  110. break;
  111. case 'storage':
  112. $this->sorts[$item->name] = $item->type . $item->admin_title;
  113. break;
  114. }
  115. if (!empty($this->base_types[$this->style_plugin['module']][$this->style_plugin['type']])) {
  116. $type = $this->base_types[$this->style_plugin['module']][$this->style_plugin['type']]['title'];
  117. }
  118. else {
  119. $type = t('Unknown');
  120. }
  121. $ops = theme('links__ctools_dropbutton', array('links' => $operations, 'attributes' => array('class' => array('links', 'inline'))));
  122. $this->rows[$item->name] = array(
  123. 'data' => array(
  124. array('data' => $type, 'class' => array('ctools-export-ui-type')),
  125. array('data' => check_plain($item->name), 'class' => array('ctools-export-ui-name')),
  126. array('data' => check_plain($item->admin_title), 'class' => array('ctools-export-ui-title')),
  127. array('data' => check_plain($this->style_plugin['title']), 'class' => array('ctools-export-ui-base')),
  128. array('data' => check_plain($item->type), 'class' => array('ctools-export-ui-storage')),
  129. array('data' => $ops, 'class' => array('ctools-export-ui-operations')),
  130. ),
  131. 'title' => check_plain($item->admin_description),
  132. 'class' => array(!empty($item->disabled) ? 'ctools-export-ui-disabled' : 'ctools-export-ui-enabled'),
  133. );
  134. }
  135. function list_table_header() {
  136. return array(
  137. array('data' => t('Type'), 'class' => array('ctools-export-ui-type')),
  138. array('data' => t('Name'), 'class' => array('ctools-export-ui-name')),
  139. array('data' => t('Title'), 'class' => array('ctools-export-ui-title')),
  140. array('data' => t('Base'), 'class' => array('ctools-export-ui-base')),
  141. array('data' => t('Storage'), 'class' => array('ctools-export-ui-storage')),
  142. array('data' => t('Operations'), 'class' => array('ctools-export-ui-operations')),
  143. );
  144. }
  145. function init($plugin) {
  146. ctools_include('stylizer');
  147. $this->base_types = ctools_get_style_base_types();
  148. parent::init($plugin);
  149. }
  150. function get_wizard_info(&$form_state) {
  151. $form_info = parent::get_wizard_info($form_state);
  152. ctools_include('stylizer');
  153. // For add forms, we have temporarily set the 'form type' to include
  154. // the style type so the default wizard_info can find the path. If
  155. // we did that, we have to put it back.
  156. if (!empty($form_state['type'])) {
  157. $form_state['form type'] = 'add';
  158. $form_info['show back'] = TRUE;
  159. }
  160. // Ensure these do not get out of sync.
  161. $form_state['item']->settings['name'] = $form_state['item']->name;
  162. $form_state['settings'] = $form_state['item']->settings;
  163. // Figure out the base style plugin in use and make sure that is available.
  164. $plugin = NULL;
  165. if (!empty($form_state['item']->settings['style_base'])) {
  166. $plugin = ctools_get_style_base($form_state['item']->settings['style_base']);
  167. ctools_stylizer_add_plugin_forms($form_info, $plugin, $form_state['op']);
  168. }
  169. else {
  170. // This is here so the 'finish' button does not show up, and because
  171. // we don't have the selected style we don't know what the next form(s)
  172. // will be.
  173. $form_info['order']['next'] = t('Configure style');
  174. }
  175. // If available, make sure these are available for the 'choose' form.
  176. if (!empty($form_state['item']->style_module)) {
  177. $form_state['module'] = $form_state['item']->style_module;
  178. $form_state['type'] = $form_state['item']->style_type;
  179. }
  180. $form_state['base_style_plugin'] = $plugin;
  181. $form_state['settings'] = $form_state['item']->settings;
  182. return $form_info;
  183. }
  184. /**
  185. * Store the stylizer info in our settings.
  186. *
  187. * The stylizer wizard stores its stuff in slightly different places, so
  188. * we have to find it and move it to the right place.
  189. */
  190. function store_stylizer_info(&$form_state) {
  191. /*
  192. foreach (array('name', 'admin_title', 'admin_description') as $key) {
  193. if (!empty($form_state['values'][$key])) {
  194. $form_state['item']->{$key} = $form_state['values'][$key];
  195. }
  196. }
  197. */
  198. if ($form_state['step'] != 'import') {
  199. $form_state['item']->settings = $form_state['settings'];
  200. }
  201. // Do not let the 'name' accidentally get out of sync under any circumstances.
  202. $form_state['item']->settings['name'] = $form_state['item']->name;
  203. }
  204. function edit_wizard_next(&$form_state) {
  205. $this->store_stylizer_info($form_state);
  206. parent::edit_wizard_next($form_state);
  207. }
  208. function edit_wizard_finish(&$form_state) {
  209. // These might be stored by the stylizer wizard, so we should clear them.
  210. if (isset($form_state['settings']['old_settings'])) {
  211. unset($form_state['settings']['old_settings']);
  212. }
  213. $this->store_stylizer_info($form_state);
  214. parent::edit_wizard_finish($form_state);
  215. }
  216. function edit_form_type(&$form, &$form_state) {
  217. foreach ($this->base_types as $module => $info) {
  218. foreach ($info as $key => $base_type) {
  219. $types[$module . '-' . $key] = $base_type['title'];
  220. }
  221. }
  222. $form['type'] = array(
  223. '#type' => 'select',
  224. '#title' => t('Type'),
  225. '#options' => $types,
  226. '#default_value' => 'all',
  227. '#weight' => -10,
  228. '#attributes' => array('class' => array('ctools-auto-submit')),
  229. );
  230. }
  231. function edit_form_type_submit(&$form, &$form_state) {
  232. list($form_state['item']->style_module, $form_state['item']->style_type) = explode('-', $form_state['values']['type']);
  233. }
  234. }