linkit.install 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the Linkit module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function linkit_schema() {
  10. $schema = array();
  11. $schema['linkit_profiles'] = array(
  12. 'description' => 'Base table holding Linkit profiles.',
  13. 'export' => array(
  14. 'key' => 'name',
  15. 'key name' => 'Name',
  16. 'primary key' => 'pid',
  17. 'object' => 'LinkitProfile',
  18. 'identifier' => 'linkit_profile',
  19. 'status' => 'linkit_profiles_status',
  20. 'load callback' => 'linkit_profile_load',
  21. 'load all callback' => 'linkit_profile_load_all',
  22. 'bulk export' => TRUE,
  23. 'api' => array(
  24. 'owner' => 'linkit',
  25. 'api' => 'linkit_profiles',
  26. 'minimum_version' => 1,
  27. 'current_version' => 1,
  28. ),
  29. ),
  30. 'fields' => array(
  31. 'pid' => array(
  32. 'type' => 'serial',
  33. 'unsigned' => TRUE,
  34. 'not null' => TRUE,
  35. 'no export' => TRUE,
  36. 'description' => 'Serial id for this profile.',
  37. ),
  38. 'name' => array(
  39. 'type' => 'varchar',
  40. 'length' => 128,
  41. 'not null' => TRUE,
  42. 'description' => 'Machine-readable name for this profile.',
  43. ),
  44. 'admin_title' => array(
  45. 'type' => 'varchar',
  46. 'length' => 128,
  47. 'not null' => TRUE,
  48. 'description' => 'Administrative title for this profile.',
  49. ),
  50. 'admin_description' => array(
  51. 'type' => 'text',
  52. 'not null' => TRUE,
  53. 'size' => 'medium',
  54. 'description' => 'Administrative description for this profile.',
  55. ),
  56. 'profile_type' => array(
  57. 'type' => 'varchar',
  58. 'length' => 128,
  59. 'not null' => TRUE,
  60. 'description' => 'The profile type.',
  61. ),
  62. 'data' => array(
  63. 'type' => 'blob',
  64. 'size' => 'big',
  65. 'not null' => TRUE,
  66. 'serialize' => TRUE,
  67. 'description' => 'Serialized data containing the profile settings.',
  68. ),
  69. ),
  70. 'primary key' => array('pid'),
  71. 'unique keys' => array(
  72. 'name' => array('name'),
  73. ),
  74. 'indexes' => array(
  75. 'pid' => array('pid'),
  76. 'profile_type' => array('profile_type'),
  77. ),
  78. );
  79. return $schema;
  80. }
  81. /**
  82. * Migrate settings from v2 to v3 if needed.
  83. */
  84. function linkit_update_7300() {
  85. if (!db_field_exists('linkit_profiles', 'role_rids')) {
  86. // Already 3.x, no need for migration from 2.x.
  87. return;
  88. }
  89. // Get old profiles.
  90. $old_profiles = db_query("SELECT * FROM {linkit_profiles} ORDER BY weight DESC");
  91. //Drop redundant fields
  92. db_drop_field('linkit_profiles', 'role_rids');
  93. db_drop_field('linkit_profiles', 'weight');
  94. db_add_field('linkit_profiles', 'profile_type',
  95. array(
  96. 'type' => 'varchar',
  97. 'length' => 128,
  98. 'not null' => TRUE,
  99. 'description' => 'The profile type.',
  100. 'default' => ''
  101. )
  102. );
  103. db_add_field('linkit_profiles', 'admin_description',
  104. array(
  105. 'type' => 'text',
  106. 'size' => 'medium',
  107. 'description' => 'Administrative description for this profile.',
  108. )
  109. );
  110. foreach ($old_profiles as $profile) {
  111. $data = unserialize($profile->data);
  112. // Rename the plugins
  113. $data['search_plugins'] = $data['plugins'];
  114. unset($data['plugins']);
  115. $data['attribute_plugins'] = $data['attributes'];
  116. unset($data['plugins']);
  117. $data['attribute_plugins']['target'] = array(
  118. 'enabled' => 0,
  119. 'weight' => -10,
  120. );
  121. // Add new plugins
  122. $data['insert_plugin'] = array(
  123. 'plugin' => 'raw_url',
  124. 'url_method' => 1,
  125. );
  126. // Remove reverse_menu_trail
  127. foreach ($data as $key => $item) {
  128. if(strstr($key, 'entity:')) {
  129. unset($data[$key]['reverse_menu_trail']);
  130. }
  131. }
  132. $profile->data = serialize($data);
  133. // All old profiles are migrated as field profiles
  134. // Do the update.
  135. db_update('linkit_profiles')
  136. ->fields(array(
  137. 'data' => $profile->data,
  138. 'profile_type' => "2"
  139. ))
  140. ->condition('pid', $profile->pid)
  141. ->execute();
  142. // Store the weightest profile
  143. $weightest_profile = clone $profile;
  144. // Insert an editor profile for every field profile
  145. // Copy the prfoile for latter usage
  146. $profile_editor = clone $profile;
  147. $profile_editor->pid = NULL;
  148. $data = unserialize($profile_editor->data);
  149. $data['text_formats'] = array(
  150. 'full_html' => 'full_html',
  151. 'filtered_html' => 0,
  152. 'plain_text' => 0
  153. );
  154. $profile_editor->data = serialize($data);
  155. $profile_editor->name = $profile_editor->name . '_editor';
  156. $profile_editor->admin_title = $profile_editor->admin_title . ' [editor]';
  157. $profile_editor->profile_type = 1;
  158. $profile_editor->admin_description = '';
  159. unset($profile_editor->role_rids, $profile_editor->weight);
  160. db_insert('linkit_profiles')
  161. ->fields((array)$profile_editor)
  162. ->execute();
  163. }
  164. // Update the field instances with the weightest profile
  165. $instances_info = field_info_instances();
  166. foreach ($instances_info as $entity_type_name => $entity_type) {
  167. foreach ($entity_type as $bundle_name => $bundle) {
  168. foreach ($bundle as $field_name => $field) {
  169. if(isset($field['settings'], $field['settings']['linkit'])) {
  170. $settings = &$field['settings']['linkit'];
  171. $settings['button_text'] = 'Search';
  172. unset($settings['insert_plugin']);
  173. if($settings['enable']) {
  174. $settings['profile'] = $weightest_profile->name;
  175. }
  176. else {
  177. $settings['profile'] = '';
  178. }
  179. field_update_instance($field);
  180. }
  181. }
  182. }
  183. }
  184. // Rebuild code registry so the LinkitProfile class is found.
  185. registry_rebuild();
  186. }
  187. /**
  188. * Do nothing. Update the schema version.
  189. */
  190. function linkit_update_7301() {
  191. // Do nothing.
  192. }
  193. /**
  194. * Set URL type to "Entity view page" to preserve current behavior.
  195. */
  196. function linkit_update_7302() {
  197. require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'linkit') . '/plugins/linkit_search/file.class.php';
  198. $profiles = linkit_profile_load_all();
  199. foreach ($profiles as $profile) {
  200. if (isset($profile->data['entity:file'])) {
  201. $profile->data['entity:file'] = LINKIT_FILE_URL_TYPE_ENTITY;
  202. ctools_export_crud_save('linkit_profiles', $profile);
  203. }
  204. }
  205. }