search_api_autocomplete.install 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the Search API autocomplete module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function search_api_autocomplete_schema() {
  10. $schema['search_api_autocomplete_search'] = array(
  11. 'description' => 'Stores autocomplete settings for searches on Search API indexes.',
  12. 'fields' => array(
  13. 'id' => array(
  14. 'description' => 'The primary identifier for a search.',
  15. 'type' => 'serial',
  16. 'unsigned' => TRUE,
  17. 'not null' => TRUE,
  18. ),
  19. 'machine_name' => array(
  20. 'description' => 'A string identifier for a search.',
  21. 'type' => 'varchar',
  22. 'length' => 100,
  23. 'not null' => TRUE,
  24. ),
  25. 'name' => array(
  26. 'description' => 'A human-readable name for the search.',
  27. 'type' => 'varchar',
  28. 'length' => 50,
  29. ),
  30. 'index_id' => array(
  31. 'description' => 'The {search_api_index}.machine_name this search belongs to.',
  32. 'type' => 'varchar',
  33. 'length' => 50,
  34. 'not null' => TRUE,
  35. ),
  36. 'type' => array(
  37. 'description' => 'The type of search, usually a module name.',
  38. 'type' => 'varchar',
  39. 'length' => 50,
  40. 'not null' => TRUE,
  41. ),
  42. 'enabled' => array(
  43. 'description' => 'A flag indicating whether autocompletion for this search is enabled.',
  44. 'type' => 'int',
  45. 'size' => 'tiny',
  46. 'not null' => TRUE,
  47. 'default' => 1,
  48. ),
  49. 'options' => array(
  50. 'description' => 'The options used to configure autocompletion for this search.',
  51. 'type' => 'text',
  52. 'serialize' => TRUE,
  53. 'not null' => TRUE,
  54. ),
  55. 'status' => array(
  56. 'description' => 'The exportable status of the entity.',
  57. 'type' => 'int',
  58. 'not null' => TRUE,
  59. 'default' => 0x01,
  60. 'size' => 'tiny',
  61. ),
  62. 'module' => array(
  63. 'description' => 'The name of the providing module if the entity has been defined in code.',
  64. 'type' => 'varchar',
  65. 'length' => 255,
  66. 'not null' => FALSE,
  67. ),
  68. ),
  69. 'primary key' => array('id'),
  70. 'unique keys' => array(
  71. 'machine_name' => array('machine_name'),
  72. ),
  73. 'indexes' => array(
  74. 'enabled' => array('enabled'),
  75. ),
  76. );
  77. return $schema;
  78. }
  79. /**
  80. * Update permissions to the new system with search-specific permissions.
  81. */
  82. function search_api_autocomplete_update_7101() {
  83. $roles = db_select('role_permission', 'r')
  84. ->fields('r', array('rid'))
  85. ->condition('permission', 'use search_api_autocomplete')
  86. ->execute()
  87. ->fetchCol();
  88. $searches = db_select('search_api_autocomplete_search', 's')
  89. ->fields('s', array('machine_name'))
  90. ->execute()
  91. ->fetchCol();
  92. try {
  93. $tx = db_transaction();
  94. db_delete('role_permission')
  95. ->condition('permission', 'use search_api_autocomplete')
  96. ->execute();
  97. if ($roles && $searches) {
  98. $insert = db_insert('role_permission')
  99. ->fields(array('rid', 'permission', 'module'));
  100. foreach ($roles as $rid) {
  101. foreach ($searches as $id) {
  102. $insert->values(array(
  103. 'rid' => $rid,
  104. 'permission' => 'use search_api_autocomplete for ' . $id,
  105. 'module' => 'search_api_autocomplete',
  106. ));
  107. }
  108. }
  109. $insert->execute();
  110. }
  111. }
  112. catch (PDOException $e) {
  113. $tx->rollback();
  114. throw new DrupalUpdateException(t('A database error occurred during update: @msg', array('@msg' => $e->getMessage())));
  115. }
  116. }