linkit.install 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. // Make sure our schema changes are reflected in the cached schema or
  111. // subsequent update hooks making use of it might fail.
  112. drupal_get_schema('linkit_profiles', TRUE);
  113. foreach ($old_profiles as $profile) {
  114. $data = unserialize($profile->data);
  115. // Rename the plugins
  116. $data['search_plugins'] = $data['plugins'];
  117. unset($data['plugins']);
  118. $data['attribute_plugins'] = $data['attributes'];
  119. unset($data['plugins']);
  120. $data['attribute_plugins']['target'] = array(
  121. 'enabled' => 0,
  122. 'weight' => -10,
  123. );
  124. // Add new plugins
  125. $data['insert_plugin'] = array(
  126. 'plugin' => 'raw_url',
  127. 'url_method' => 1,
  128. );
  129. // Remove reverse_menu_trail
  130. foreach ($data as $key => $item) {
  131. if(strstr($key, 'entity:')) {
  132. unset($data[$key]['reverse_menu_trail']);
  133. }
  134. }
  135. $profile->data = serialize($data);
  136. // All old profiles are migrated as field profiles
  137. // Do the update.
  138. db_update('linkit_profiles')
  139. ->fields(array(
  140. 'data' => $profile->data,
  141. 'profile_type' => "2"
  142. ))
  143. ->condition('pid', $profile->pid)
  144. ->execute();
  145. // Store the weightest profile
  146. $weightest_profile = clone $profile;
  147. // Insert an editor profile for every field profile
  148. // Copy the prfoile for latter usage
  149. $profile_editor = clone $profile;
  150. $profile_editor->pid = NULL;
  151. $data = unserialize($profile_editor->data);
  152. $data['text_formats'] = array(
  153. 'full_html' => 'full_html',
  154. 'filtered_html' => 0,
  155. 'plain_text' => 0
  156. );
  157. $profile_editor->data = serialize($data);
  158. $profile_editor->name = $profile_editor->name . '_editor';
  159. $profile_editor->admin_title = $profile_editor->admin_title . ' [editor]';
  160. $profile_editor->profile_type = 1;
  161. $profile_editor->admin_description = '';
  162. unset($profile_editor->role_rids, $profile_editor->weight);
  163. db_insert('linkit_profiles')
  164. ->fields((array)$profile_editor)
  165. ->execute();
  166. }
  167. // Update the field instances with the weightest profile
  168. $instances_info = field_info_instances();
  169. foreach ($instances_info as $entity_type_name => $entity_type) {
  170. foreach ($entity_type as $bundle_name => $bundle) {
  171. foreach ($bundle as $field_name => $field) {
  172. if(isset($field['settings'], $field['settings']['linkit'])) {
  173. $settings = &$field['settings']['linkit'];
  174. $settings['button_text'] = 'Search';
  175. unset($settings['insert_plugin']);
  176. if($settings['enable']) {
  177. $settings['profile'] = $weightest_profile->name;
  178. }
  179. else {
  180. $settings['profile'] = '';
  181. }
  182. field_update_instance($field);
  183. }
  184. }
  185. }
  186. }
  187. // Rebuild code registry so the LinkitProfile class is found.
  188. registry_rebuild();
  189. }
  190. /**
  191. * Do nothing. Update the schema version.
  192. */
  193. function linkit_update_7301() {
  194. // Do nothing.
  195. }
  196. /**
  197. * Reverted.
  198. */
  199. function linkit_update_7302() {
  200. // This is the broken verison of 7304
  201. }
  202. /**
  203. * Fixed 7302. Set URL type to "Entity view page" to preserve current behavior.
  204. */
  205. function linkit_update_7303() {
  206. require_once DRUPAL_ROOT . '/' . drupal_get_path('module', 'linkit') . '/plugins/linkit_search/file.class.php';
  207. $profiles = linkit_profile_load_all();
  208. $show_notice = FALSE;
  209. foreach ($profiles as $profile) {
  210. // If the broken 7302 has been applied, and no other changes has been made, there is not way to get the data back.
  211. // Lets add the default settings instead.
  212. if (isset($profile->data['entity:file']) && $profile->data['entity:file'] == LINKIT_FILE_URL_TYPE_ENTITY) {
  213. $profile->data['entity:file'] = array(
  214. 'result_description' => '',
  215. 'bundles' => array(
  216. 'image' => 0,
  217. 'video' => 0,
  218. 'audio' => 0,
  219. 'document' => 0,
  220. ),
  221. 'group_by_bundle' => 0,
  222. 'show_scheme' => 0,
  223. 'group_by_scheme' => 0,
  224. 'url_type' => 'entity',
  225. 'image_extra_info' => array(
  226. 'thumbnail' => 'thumbnail',
  227. 'dimensions' => 'dimensions',
  228. ),
  229. );
  230. ctools_export_crud_save('linkit_profiles', $profile);
  231. $show_notice = TRUE;
  232. }
  233. else if (isset($profile->data['entity:file']) && !isset($profile->data['entity:file']['url_type'])) {
  234. $profile->data['entity:file']['url_type'] = LINKIT_FILE_URL_TYPE_ENTITY;
  235. ctools_export_crud_save('linkit_profiles', $profile);
  236. }
  237. if ($show_notice) {
  238. return t('A previous update may have changed the settings for the "managed file" search plugin in all your linkit profiles.');
  239. }
  240. }
  241. }