search_api_views.install 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the search_api_views module.
  5. */
  6. /**
  7. * Updates all Search API views to use the new, specification-compliant identifiers.
  8. */
  9. function search_api_views_update_7101() {
  10. $tables = views_fetch_data();
  11. // Contains arrays with real fields mapped to field IDs for each table.
  12. $table_fields = array();
  13. foreach ($tables as $key => $table) {
  14. if (substr($key, 0, 17) != 'search_api_index_') {
  15. continue;
  16. }
  17. foreach ($table as $field => $info) {
  18. if (isset($info['real field']) && $field != $info['real field']) {
  19. $table_fields[$key][$info['real field']] = $field;
  20. }
  21. }
  22. }
  23. if (!$table_fields) {
  24. return;
  25. }
  26. foreach (views_get_all_views() as $view) {
  27. if (empty($view->base_table) || empty($table_fields[$view->base_table])) {
  28. continue;
  29. }
  30. $change = FALSE;
  31. $fields = $table_fields[$view->base_table];
  32. $change |= _search_api_views_update_7101_helper($view->base_field, $fields);
  33. if (!empty($view->display)) {
  34. foreach ($view->display as &$display) {
  35. $options = &$display->display_options;
  36. if (isset($options['style_options']['grouping'])) {
  37. $change |= _search_api_views_update_7101_helper($options['style_options']['grouping'], $fields);
  38. }
  39. if (isset($options['style_options']['columns'])) {
  40. $change |= _search_api_views_update_7101_helper($options['style_options']['columns'], $fields);
  41. }
  42. if (isset($options['style_options']['info'])) {
  43. $change |= _search_api_views_update_7101_helper($options['style_options']['info'], $fields);
  44. }
  45. if (isset($options['arguments'])) {
  46. $change |= _search_api_views_update_7101_helper($options['arguments'], $fields);
  47. }
  48. if (isset($options['fields'])) {
  49. $change |= _search_api_views_update_7101_helper($options['fields'], $fields);
  50. }
  51. if (isset($options['filters'])) {
  52. $change |= _search_api_views_update_7101_helper($options['filters'], $fields);
  53. }
  54. if (isset($options['sorts'])) {
  55. $change |= _search_api_views_update_7101_helper($options['sorts'], $fields);
  56. }
  57. }
  58. }
  59. if ($change) {
  60. $view->save();
  61. }
  62. }
  63. }
  64. /**
  65. * Helper function for replacing field identifiers.
  66. *
  67. * @param $field
  68. * Some data to be searched for field names that should be altered. Passed by
  69. * reference.
  70. * @param array $fields
  71. * An array mapping Search API field identifiers (as previously used by Views)
  72. * to the new, sanitized Views field identifiers.
  73. *
  74. * @return bool
  75. * TRUE if any data was changed, FALSE otherwise.
  76. */
  77. function _search_api_views_update_7101_helper(&$field, array $fields) {
  78. if (is_array($field)) {
  79. $change = FALSE;
  80. $new_field = array();
  81. foreach ($field as $k => $v) {
  82. $new_k = $k;
  83. $change |= _search_api_views_update_7101_helper($new_k, $fields);
  84. $change |= _search_api_views_update_7101_helper($v, $fields);
  85. $new_field[$new_k] = $v;
  86. }
  87. $field = $new_field;
  88. return $change;
  89. }
  90. if (isset($fields[$field])) {
  91. $field = $fields[$field];
  92. return TRUE;
  93. }
  94. return FALSE;
  95. }
  96. /**
  97. * Delete the now unnecessary "search_api_views_max_fields_depth" variable.
  98. */
  99. function search_api_views_update_7102() {
  100. variable_del('search_api_views_max_fields_depth');
  101. }