hierarchical_select.admin.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /**
  3. * @file
  4. * Module settings and configuration administration UI.
  5. */
  6. /**
  7. * Form definition; admin settings.
  8. */
  9. function hierarchical_select_admin_settings($form, &$form_state) {
  10. $form['description'] = array(
  11. '#markup' => t('All settings below will be used as site-wide defaults.'),
  12. '#prefix' => '<div>',
  13. '#suffix' => '</div>',
  14. );
  15. $form['hierarchical_select_animation_delay'] = array(
  16. '#type' => 'textfield',
  17. '#title' => t('Animation delay'),
  18. '#description' => t(
  19. 'The delay that will be used for the "drop in/out" effect when a
  20. hierarchical select is being updated (in milliseconds).'
  21. ),
  22. '#size' => 5,
  23. '#maxlength' => 5,
  24. '#default_value' => variable_get('hierarchical_select_animation_delay', 400),
  25. );
  26. $form['hierarchical_select_level_labels_style'] = array(
  27. '#type' => 'select',
  28. '#title' => t('Level labels style'),
  29. '#description' => t(
  30. 'The style that will be used for level labels. This is not supported by
  31. all browsers! If you want a consistent interface, choose to use no
  32. style.'
  33. ),
  34. '#options' => array(
  35. 'none' => t('No style'),
  36. 'bold' => t('Bold'),
  37. 'inversed' => t('Inversed'),
  38. 'underlined' => t('Underlined'),
  39. ),
  40. '#default_value' => variable_get('hierarchical_select_level_labels_style', 'none'),
  41. );
  42. // TODO: port the HS client-side cache system to Drupal 7.
  43. /*
  44. $form['hierarchical_select_js_cache_system'] = array(
  45. '#type' => 'radios',
  46. '#title' => t('Cache in a HTML 5 client-side database'),
  47. '#description' => t(
  48. 'This feature only works in browsers that support the
  49. <a href="!spec-url">HTML 5 client-side database storage specification
  50. </a>.</br>
  51. After enabling this, you will notice (in supporting browsers) that
  52. refreshing the hierarchical select will not require a request to the
  53. server when a part is being requested that has been requested before.',
  54. array('!spec-url' => url('http://www.whatwg.org/specs/web-apps/current-work/multipage/section-sql.html'))
  55. ),
  56. '#options' => array(
  57. 0 => t('Disabled'),
  58. 1 => t('Enabled'),
  59. ),
  60. '#default_value' => variable_get('hierarchical_select_js_cache_system', 0),
  61. );
  62. */
  63. return system_settings_form($form);
  64. }
  65. /**
  66. * Menu callback; a table that lists all Hierarchical Select configs.
  67. */
  68. function hierarchical_select_admin_configs() {
  69. $header = array(t('Hierarchy type'), t('Hierarchy'), t('Entity type'), t('Bundle'), t('Context type'), t('Context'), t('Actions'));
  70. // Retrieve all information items
  71. $info_items = array();
  72. foreach (module_implements('hierarchical_select_config_info') as $module) {
  73. $info_items = array_merge_recursive($info_items, module_invoke($module, 'hierarchical_select_config_info'));
  74. }
  75. // Process the retrieved information into rows.
  76. $rows = array();
  77. foreach ($info_items as $id => $item) {
  78. $config_id = $item['config_id'];
  79. $rows[$id] = array(
  80. $item['hierarchy type'],
  81. $item['hierarchy'],
  82. $item['entity type'],
  83. $item['bundle'],
  84. $item['context type'],
  85. $item['context'],
  86. theme('links', array('links' => array(
  87. array(
  88. 'title' => t('Edit'),
  89. 'href' => $item['edit link'],
  90. 'fragment' => "hierarchical-select-config-form-$config_id",
  91. ),
  92. array(
  93. 'title' => t('Export'),
  94. 'href' => "admin/config/content/hierarchical_select/export/$config_id",
  95. ),
  96. array(
  97. 'title' => t('Import'),
  98. 'href' => "admin/config/content/hierarchical_select/import/$config_id",
  99. ),
  100. ))),
  101. );
  102. }
  103. return theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array(), 'caption' => t('Overview of all Hierarchical Select configurations.')));
  104. }
  105. /**
  106. * Menu callback; a table that lists all Hierarchical Select implementations
  107. * and the features they support.
  108. */
  109. function hierarchical_select_admin_implementations() {
  110. $output = '';
  111. $header = array(t('Implementation (module)'), t('Hierarchy type'), t('Entity type'), t('Create new items'), t('Entity count'));
  112. // Retrieve all information items
  113. $rows = array();
  114. foreach (module_implements('hierarchical_select_root_level') as $module) {
  115. $filename = db_query("SELECT filename FROM {system} WHERE type = :type AND name = :name", array(':type' => 'module', ':name' => $module))->fetchField();
  116. $module_info = drupal_parse_info_file(dirname($filename) . "/$module.info");
  117. // Try to extract the hierarchy type from the optional hook_hierarchical_select_config_info().
  118. $hierarchy_type = $entity_type = t('unknown');
  119. if (module_hook($module, 'hierarchical_select_implementation_info')) {
  120. $implementation = module_invoke($module, 'hierarchical_select_implementation_info');
  121. $hierarchy_type = $implementation['hierarchy type'];
  122. $entity_type = $implementation['entity type'];
  123. }
  124. $rows[] = array(
  125. $module_info['name'],
  126. $hierarchy_type,
  127. $entity_type,
  128. (module_hook($module, 'hierarchical_select_create_item')) ? t('Yes') : t('No'),
  129. (module_hook($module, 'hierarchical_select_entity_count')) ? t('Yes') : t('No'),
  130. );
  131. }
  132. $output .= '<p>';
  133. $output .= t('
  134. The table below allows you to find out <strong>which Hierarchical Select
  135. features are supported</strong> by the implementations of the Hierarchical
  136. Select API.<br />
  137. It is <strong>not a reflection of some settings</strong>.
  138. ');
  139. $output .= '</p>';
  140. $output .= theme('table', array('header' => $header, 'rows' => $rows, 'attributes' => array(), 'caption' => t('Overview of all installed Hierarchical Select implementations.')));
  141. return $output;
  142. }
  143. /**
  144. * Form definition; config export form.
  145. */
  146. function hierarchical_select_admin_export($form, &$form_state, $config_id) {
  147. require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'hierarchical_select') . '/includes/common.inc';
  148. $config = hierarchical_select_common_config_get($config_id);
  149. $code = _hierarchical_select_create_export_code($config);
  150. drupal_add_css(drupal_get_path('module', 'hierarchical_select') . '/hierarchical_select.css');
  151. drupal_add_js('$(document).ready(function() { $(".hierarchical-select-code").focus(); });', array('type' => 'inline', 'scope' => JS_DEFAULT));
  152. $lines = substr_count($code, "\n") + 1;
  153. $form['config'] = array(
  154. '#type' => 'textarea',
  155. '#title' => t('Hierarchical Select configuration %config_id', array('%config_id' => $config_id)),
  156. '#default_value' => $code,
  157. '#rows' => $lines,
  158. '#attributes' => array('class' => array('hierarchical-select-config-code')),
  159. );
  160. return $form;
  161. }
  162. /**
  163. * Form definition; config import form.
  164. */
  165. function hierarchical_select_admin_import($form, &$form_state, $config_id) {
  166. require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'hierarchical_select') . '/includes/common.inc';
  167. drupal_add_css(drupal_get_path('module', 'hierarchical_select') . '/hierarchical_select.css');
  168. drupal_add_js('$(document).ready(function() { $(".hierarchical-select-code").focus(); });', array('type' => 'inline', 'scope' => JS_DEFAULT));
  169. $form['config'] = array(
  170. '#type' => 'textarea',
  171. '#title' => t('Import Hierarchical Select configuration code'),
  172. '#cols' => 60,
  173. '#rows' => 15,
  174. '#description' => t('Copy and paste the results of an exported
  175. Hierarchical Select configuration here.<br />This will override the
  176. current Hierarchical Select configuration for %config_id.',
  177. array('%config_id' => $config_id)
  178. ),
  179. '#attributes' => array('class' => array('hierarchical-select-config-code')),
  180. );
  181. $form['interpreted_config'] = array('#type' => 'value', '#value' => NULL);
  182. $form['submit'] = array(
  183. '#type' => 'submit',
  184. '#value' => t("Import"),
  185. );
  186. $form_state['#redirect'] = NULL;
  187. return $form;
  188. }
  189. /**
  190. * Validate callback; config import form.
  191. */
  192. function hierarchical_select_admin_import_validate($form, &$form_state) {
  193. ob_start();
  194. eval($form_state['values']['config']);
  195. ob_end_clean();
  196. form_set_value($form['interpreted_config'], serialize($config), $form_state);
  197. if (empty($form_state['values']['config'])) {
  198. form_error($form['config'], t('You did not enter anything.'));
  199. }
  200. elseif ($config == NULL) {
  201. form_error($form['config'], t('There is a syntax error in the Hierarchical Select configuration you entered.'));
  202. }
  203. elseif (!isset($config['config_id']) || empty($config['config_id'])) {
  204. form_error($form['config'], t('Unable to import this configuration, because no Hierarchical Select <em>config id</em> is set.'));
  205. }
  206. }
  207. /**
  208. * Submit callback; config import form.
  209. */
  210. function hierarchical_select_admin_import_submit($form, &$form_state) {
  211. $config = unserialize($form_state['values']['interpreted_config']);
  212. $config_id = $config['config_id'];
  213. hierarchical_select_common_config_set($config_id, $config);
  214. drupal_set_message(t('Hierarchical Select configuration for %config_id imported!', array('%config_id' => $config_id)));
  215. }
  216. //----------------------------------------------------------------------------
  217. // Private functions.
  218. /**
  219. * Given a config array, create the export code for it.
  220. *
  221. * @param array $config
  222. * A Hierarchical Select config array, as described in API.txt
  223. * @return string
  224. * The code as it would appear in an editor.
  225. */
  226. function _hierarchical_select_create_export_code($config) {
  227. $output = _hierarchical_select_create_code_from_array($config);
  228. $output = '$config = ' . $output . ";\n";
  229. return $output;
  230. }
  231. /**
  232. * Given a array, create the export code for it.
  233. *
  234. * This functions is a refactoring of features_var_export() to use with the
  235. * hierarchical select module.
  236. *
  237. * @param mixed $config
  238. * A value to export as code.
  239. * @param string $prefix
  240. * Padding for nested array.
  241. * @param boolean $init
  242. * Indicator of the first level of the export.
  243. * @return string
  244. * The code as it would appear in an editor.
  245. */
  246. function _hierarchical_select_create_code_from_array($var, $prefix = '', $init = TRUE) {
  247. $output = "";
  248. $type = gettype($var);
  249. switch ($type) {
  250. case 'array':
  251. if (empty($var)) {
  252. $output = "array()";
  253. }
  254. else {
  255. $output = "array(\n";
  256. foreach ($var as $key => $value) {
  257. $value = _hierarchical_select_create_code_from_array($value, ' ', FALSE);
  258. $output .= " '$key' => " . $value . ",\n";
  259. }
  260. $output .= ')';
  261. }
  262. break;
  263. case 'string':
  264. $var = str_replace("\n", "***BREAK***", $var);
  265. $output = var_export($var, TRUE);
  266. break;
  267. case 'boolean':
  268. $var = empty($var) ? 'FALSE' : 'TRUE';
  269. $output = var_export($var, TRUE);
  270. break;
  271. default:
  272. $output = var_export($var, TRUE);
  273. }
  274. if ($prefix) {
  275. $output = str_replace("\n", "\n$prefix", $output);
  276. }
  277. if ($init) {
  278. $output = str_replace("***BREAK***", "\n", $output);
  279. }
  280. return $output;
  281. }