search_api_views.install 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 $name => $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 $key => &$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. * @return
  68. * TRUE iff the identifier was changed.
  69. */
  70. function _search_api_views_update_7101_helper(&$field, array $fields) {
  71. if (is_array($field)) {
  72. $change = FALSE;
  73. $new_field = array();
  74. foreach ($field as $k => $v) {
  75. $new_k = $k;
  76. $change |= _search_api_views_update_7101_helper($new_k, $fields);
  77. $change |= _search_api_views_update_7101_helper($v, $fields);
  78. $new_field[$new_k] = $v;
  79. }
  80. $field = $new_field;
  81. return $change;
  82. }
  83. if (isset($fields[$field])) {
  84. $field = $fields[$field];
  85. return TRUE;
  86. }
  87. return FALSE;
  88. }
  89. /**
  90. * Delete the now unnecessary "search_api_views_max_fields_depth" variable.
  91. */
  92. function search_api_views_update_7102() {
  93. variable_del('search_api_views_max_fields_depth');
  94. }