uc_catalog.install 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the uc_catalog module.
  5. */
  6. /**
  7. * Implements hook_enable().
  8. */
  9. function uc_catalog_enable() {
  10. foreach (uc_product_types() as $type) {
  11. uc_catalog_add_node_type($type);
  12. }
  13. uc_catalog_add_image_field();
  14. }
  15. /**
  16. * Implements hook_uninstall().
  17. */
  18. function uc_catalog_uninstall() {
  19. if ($vid = variable_get('uc_catalog_vid', 0)) {
  20. $vocab = taxonomy_vocabulary_load($vid);
  21. drupal_set_message(t('The Ubercart Catalog vocabulary has not been deleted. If you need to delete it, <a href="@url">please do so manually</a>.', array('@url' => url('admin/structure/taxonomy/' . $vocab->machine_name . '/edit'))), 'warning');
  22. }
  23. db_delete('variable')
  24. ->condition('name', 'uc_catalog_%', 'LIKE')
  25. ->execute();
  26. }
  27. /**
  28. * Implements hook_update_dependencies().
  29. */
  30. function uc_catalog_update_dependencies() {
  31. $dependencies['uc_catalog'][7000] = array(
  32. 'system' => 7020,
  33. 'taxonomy' => 7002,
  34. );
  35. $dependencies['uc_catalog'][7001] = array(
  36. 'system' => 7059,
  37. );
  38. // Try to alter vocabulary machine name before taxonomy reference field is
  39. // created. If dependency is honored, uc_catalog_update_7003 is a no-op.
  40. $dependencies['taxonomy'][7004] = array(
  41. 'uc_catalog' => 7002,
  42. );
  43. return $dependencies;
  44. }
  45. /**
  46. * Implements hook_update_last_removed().
  47. */
  48. function uc_catalog_update_last_removed() {
  49. return 8;
  50. }
  51. /**
  52. * Sets up a default term image for the Catalog.
  53. */
  54. function uc_catalog_update_7000() {
  55. if (!module_exists('image')) {
  56. module_enable(array('file', 'image'), FALSE);
  57. }
  58. // Copied from uc_catalog_add_image_field(), since the module won't be
  59. // enabled during an upgrade.
  60. $field = field_info_field('uc_catalog_image');
  61. if (!$field) {
  62. $field = array(
  63. 'field_name' => 'uc_catalog_image',
  64. 'type' => 'image',
  65. );
  66. field_create_field($field);
  67. }
  68. $instance = field_info_instance('taxonomy_term', 'uc_catalog_image', 'catalog');
  69. // Only add the instance if it doesn't exist. Don't overwrite any changes.
  70. if (!$instance) {
  71. $label = t('Image');
  72. $instance = array(
  73. 'field_name' => 'uc_catalog_image',
  74. 'entity_type' => 'taxonomy_term',
  75. 'bundle' => 'catalog',
  76. 'label' => $label,
  77. 'widget' => array(
  78. 'type' => 'image_image',
  79. ),
  80. 'display' => array(
  81. 'full' => array(
  82. 'label' => 'hidden',
  83. 'type' => 'image',
  84. 'settings' => array(
  85. 'image_link' => 'content',
  86. 'image_style' => 'uc_category',
  87. ),
  88. ),
  89. ),
  90. );
  91. field_create_instance($instance);
  92. }
  93. }
  94. /**
  95. * Migrates uploaded catalog images to the new {file} table.
  96. */
  97. function uc_catalog_update_7001(&$sandbox) {
  98. if (!db_table_exists('uc_catalog_images')) {
  99. return;
  100. }
  101. if (!isset($sandbox['progress'])) {
  102. $sandbox['progress'] = 0;
  103. $sandbox['current_fid'] = 0;
  104. $sandbox['max'] = db_query("SELECT COUNT(*) FROM {files} f INNER JOIN {uc_catalog_images} u ON u.fid = f.fid")->fetchField();
  105. }
  106. $limit = 500;
  107. // The old {files} tables still exists. We migrate core data from upload
  108. // module, but any contrib module using it will need to do its own update.
  109. $result = db_query_range('SELECT tid, f.fid, uid, u.filename, u.filepath AS uri, u.filemime, u.filesize, status, timestamp FROM {files} f INNER JOIN {uc_catalog_images} u ON u.fid = f.fid WHERE f.fid > :current ORDER BY f.fid', 0, $limit, array(':current' => $sandbox['current_fid']), array('fetch' => PDO::FETCH_ASSOC));
  110. // We will convert filepaths to uri using the default schmeme
  111. // and stripping off the existing file directory path.
  112. $basename = variable_get('file_directory_path', conf_path() . '/files');
  113. $scheme = variable_get('file_default_scheme', 'public') . '://';
  114. $fids = array();
  115. foreach ($result as $file) {
  116. // Get term id for the image.
  117. $tid = $file['tid'];
  118. // Don't break the insert query with extra data.
  119. unset($file['tid']);
  120. $file['uri'] = $scheme . str_replace($basename, '', $file['uri']);
  121. $file['uri'] = file_stream_wrapper_uri_normalize($file['uri']);
  122. db_merge('file_managed')
  123. ->key(array('fid' => $file['fid']))
  124. ->fields(array(
  125. 'uid' => $file['uid'],
  126. 'filename' => $file['filename'],
  127. 'uri' => $file['uri'],
  128. 'filemime' => $file['filemime'],
  129. 'filesize' => $file['filesize'],
  130. 'status' => $file['status'],
  131. 'timestamp' => $file['timestamp'],
  132. ))
  133. ->execute();
  134. $fids[] = $file['fid'];
  135. // Add the usage entry for the file.
  136. file_usage_add((object) $file, 'file', 'taxonomy_term', $tid);
  137. $term = (object) array(
  138. 'tid' => $tid,
  139. 'uc_catalog_image' => array(
  140. LANGUAGE_NONE => array(
  141. 0 => array(
  142. 'fid' => $file['fid'],
  143. ),
  144. ),
  145. ),
  146. );
  147. _update_7000_field_sql_storage_write('taxonomy_term', 'catalog', $term->tid, NULL, 'uc_catalog_image', $term->uc_catalog_image);
  148. $sandbox['progress']++;
  149. $sandbox['current_fid'] = $file['fid'];
  150. $sandbox['message'] = check_plain($file['filename']);
  151. }
  152. // TODO: delete the found fids from {files}?
  153. if ($sandbox['progress'] < $sandbox['max']) {
  154. $sandbox['#finished'] = min(0.99, $sandbox['progress'] / $sandbox['max']);
  155. }
  156. else {
  157. $sandbox['#finished'] = 1;
  158. }
  159. }
  160. /**
  161. * Sets Catalog vocabulary machine name.
  162. */
  163. function uc_catalog_update_7002() {
  164. db_query("UPDATE {taxonomy_vocabulary} SET machine_name = :name WHERE vid = :vid", array(':name' => 'catalog', ':vid' => variable_get('uc_catalog_vid', 0)));
  165. }
  166. /**
  167. * Renames Catalog taxonomy reference field created in taxonomy_update_7004().
  168. */
  169. function uc_catalog_update_7003() {
  170. $vid = variable_get('uc_catalog_vid', 0);
  171. $field_name = 'taxonomy_vocabulary_' . $vid;
  172. if ($field = field_info_field($field_name)) {
  173. $field['field_name'] = 'taxonomy_catalog';
  174. unset($field['id']);
  175. field_create_field($field);
  176. foreach ($field['bundles'] as $type) {
  177. $instance = array(
  178. 'field_name' => 'taxonomy_catalog',
  179. 'entity_type' => 'node',
  180. 'bundle' => $type,
  181. 'label' => t('Catalog'),
  182. 'settings' => array(
  183. 'allowed_values' => array(
  184. array(
  185. 'vid' => $vid,
  186. 'parent' => '0',
  187. ),
  188. ),
  189. ),
  190. );
  191. field_create_instance($instance);
  192. }
  193. if (isset($field['id'])) {
  194. field_delete_field($field_name);
  195. }
  196. }
  197. }
  198. /**
  199. * Delete unused catalog description.
  200. */
  201. function uc_catalog_update_7004() {
  202. variable_del('uc_catalog_description');
  203. }
  204. /**
  205. * Delete unused catalog settings.
  206. */
  207. function uc_catalog_update_7005() {
  208. variable_del('uc_catalog_show_subcategories');
  209. variable_del('uc_catalog_category_columns');
  210. variable_del('uc_product_nodes_per_page');
  211. variable_del('uc_catalog_grid_display');
  212. variable_del('uc_catalog_grid_display_width');
  213. variable_del('uc_catalog_grid_display_title');
  214. variable_del('uc_catalog_grid_display_model');
  215. variable_del('uc_catalog_grid_display_image');
  216. variable_del('uc_catalog_grid_display_sell_price');
  217. variable_del('uc_catalog_grid_display_add_to_cart');
  218. variable_del('uc_catalog_grid_display_attributes');
  219. }
  220. /**
  221. * Delete unused catalog name variable.
  222. */
  223. function uc_catalog_update_7006() {
  224. variable_del('uc_catalog_name');
  225. }