uc_product.install 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the uc_product module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function uc_product_schema() {
  10. $schema = array();
  11. $schema['uc_product_classes'] = array(
  12. 'description' => 'The list of product node types.',
  13. 'fields' => array(
  14. 'pcid' => array(
  15. 'description' => 'The node type identifier.',
  16. 'type' => 'varchar',
  17. 'length' => 32,
  18. 'not null' => TRUE,
  19. 'default' => '',
  20. ),
  21. 'name' => array(
  22. 'description' => 'The human-readable name.',
  23. 'type' => 'varchar',
  24. 'length' => 255,
  25. 'not null' => TRUE,
  26. 'default' => '',
  27. ),
  28. 'description' => array(
  29. 'description' => 'The description of the node type.',
  30. 'type' => 'text',
  31. ),
  32. ),
  33. 'primary key' => array('pcid'),
  34. );
  35. $schema['uc_product_features'] = array(
  36. 'description' => 'Stores information of features added to products.',
  37. 'fields' => array(
  38. 'pfid' => array(
  39. 'description' => 'Primary key: the product feature id.',
  40. 'type' => 'serial',
  41. 'unsigned' => TRUE,
  42. 'not null' => TRUE,
  43. ),
  44. 'nid' => array(
  45. 'description' => 'The {node}.nid of the product that has this feature.',
  46. 'type' => 'int',
  47. 'unsigned' => TRUE,
  48. 'not null' => TRUE,
  49. 'default' => 0,
  50. ),
  51. 'fid' => array(
  52. 'description' => 'The type of feature.',
  53. 'type' => 'varchar',
  54. 'length' => 32,
  55. 'not null' => TRUE,
  56. 'default' => '',
  57. ),
  58. 'description' => array(
  59. 'description' => 'The description of the feature.',
  60. 'type' => 'text',
  61. ),
  62. ),
  63. 'indexes' => array(
  64. 'nid' => array('nid'),
  65. ),
  66. 'primary key' => array('pfid'),
  67. 'foreign keys' => array(
  68. 'uc_product' => array(
  69. 'table' => 'uc_product',
  70. 'columns' => array('nid' => 'nid'),
  71. ),
  72. ),
  73. );
  74. $schema['uc_products'] = array(
  75. 'description' => 'Product information for nodes.',
  76. 'fields' => array(
  77. 'vid' => array(
  78. 'description' => 'The {node}.vid of the product.',
  79. 'type' => 'int',
  80. 'unsigned' => TRUE,
  81. 'not null' => TRUE,
  82. 'default' => 0,
  83. ),
  84. 'nid' => array(
  85. 'description' => 'The {node}.nid of the product.',
  86. 'type' => 'int',
  87. 'unsigned' => TRUE,
  88. 'not null' => TRUE,
  89. 'default' => 0,
  90. ),
  91. 'model' => array(
  92. 'description' => 'SKU or model number.',
  93. 'type' => 'varchar',
  94. 'length' => 255,
  95. 'not null' => TRUE,
  96. 'default' => '',
  97. ),
  98. 'list_price' => array(
  99. 'description' => 'Suggested retail price.',
  100. 'type' => 'numeric',
  101. 'precision' => 16,
  102. 'scale' => 5,
  103. 'not null' => TRUE,
  104. 'default' => 0.0,
  105. ),
  106. 'cost' => array(
  107. 'description' => 'The amount the store pays to sell the product.',
  108. 'type' => 'numeric',
  109. 'precision' => 16,
  110. 'scale' => 5,
  111. 'not null' => TRUE,
  112. 'default' => 0.0,
  113. ),
  114. 'sell_price' => array(
  115. 'description' => 'The amount the customer pays for the product.',
  116. 'type' => 'numeric',
  117. 'precision' => 16,
  118. 'scale' => 5,
  119. 'not null' => TRUE,
  120. 'default' => 0.0,
  121. ),
  122. 'weight' => array(
  123. 'description' => 'Physical weight.',
  124. 'type' => 'float',
  125. 'not null' => TRUE,
  126. 'default' => 0.0,
  127. ),
  128. 'weight_units' => array(
  129. 'description' => 'Unit of measure for the weight field.',
  130. 'type' => 'varchar',
  131. 'length' => 255,
  132. 'not null' => TRUE,
  133. 'default' => 'lb',
  134. ),
  135. 'length' => array(
  136. 'description' => 'Physical length of the product or its packaging.',
  137. 'type' => 'float',
  138. 'not null' => TRUE,
  139. 'default' => 0.0,
  140. ),
  141. 'width' => array(
  142. 'description' => 'Physical width of the product or its packaging.',
  143. 'type' => 'float',
  144. 'not null' => TRUE,
  145. 'default' => 0.0,
  146. ),
  147. 'height' => array(
  148. 'description' => 'Physical height of the product or its packaging.',
  149. 'type' => 'float',
  150. 'not null' => TRUE,
  151. 'default' => 0.0,
  152. ),
  153. 'length_units' => array(
  154. 'description' => 'Unit of measure for the length, width, and height.',
  155. 'type' => 'varchar',
  156. 'length' => 255,
  157. 'not null' => TRUE,
  158. 'default' => 'in',
  159. ),
  160. 'pkg_qty' => array(
  161. 'description' => 'The number of this product that fit in one package.',
  162. 'type' => 'int',
  163. 'size' => 'small',
  164. 'unsigned' => TRUE,
  165. 'not null' => TRUE,
  166. 'default' => 1,
  167. ),
  168. 'default_qty' => array(
  169. 'description' => 'The default value for the quantity field in the "Add to Cart" form.',
  170. 'type' => 'int',
  171. 'size' => 'small',
  172. 'unsigned' => TRUE,
  173. 'not null' => TRUE,
  174. 'default' => 1,
  175. ),
  176. 'ordering' => array(
  177. 'description' => 'The sort criteria for products.',
  178. 'type' => 'int',
  179. 'size' => 'tiny',
  180. 'not null' => TRUE,
  181. 'default' => 0,
  182. ),
  183. 'shippable' => array(
  184. 'description' => 'Boolean flag signifying that the product can be shipped.',
  185. 'type' => 'int',
  186. 'size' => 'tiny',
  187. 'unsigned' => TRUE,
  188. 'not null' => TRUE,
  189. 'default' => 1,
  190. ),
  191. ),
  192. 'indexes' => array(
  193. 'nid' => array('nid'),
  194. ),
  195. 'primary key' => array('vid'),
  196. 'foreign keys' => array(
  197. 'node' => array(
  198. 'table' => 'node',
  199. 'columns' => array(
  200. 'nid' => 'nid',
  201. 'vid' => 'vid',
  202. ),
  203. ),
  204. ),
  205. );
  206. return $schema;
  207. }
  208. /**
  209. * Implements hook_enable().
  210. *
  211. * Sets up the body and a default image field for products.
  212. */
  213. function uc_product_enable() {
  214. $body_label = t('Description');
  215. node_types_rebuild();
  216. $types = node_type_get_types();
  217. foreach ($types as $type) {
  218. if ($type->module == 'uc_product') {
  219. node_add_body_field($type, $body_label);
  220. uc_product_set_teaser_display($type->type);
  221. }
  222. }
  223. uc_product_add_default_image_field();
  224. }
  225. /**
  226. * Implements hook_uninstall().
  227. */
  228. function uc_product_uninstall() {
  229. db_delete('variable')
  230. ->condition(db_or()
  231. ->condition('name', 'uc_product_shippable_%', 'LIKE')
  232. ->condition('name', 'uc_image_%', 'LIKE'))
  233. ->execute();
  234. variable_del('uc_product_nodes_per_page');
  235. variable_del('uc_product_add_to_cart_qty');
  236. }
  237. /**
  238. * Implements hook_update_last_removed().
  239. */
  240. function uc_product_update_last_removed() {
  241. return 6009;
  242. }
  243. /**
  244. * Renames node permissions for the Product type.
  245. */
  246. function uc_product_update_7000() {
  247. db_update('role_permission')
  248. ->fields(array(
  249. 'permission' => 'create product products',
  250. ))
  251. ->condition('permission', 'create products')
  252. ->execute();
  253. db_update('role_permission')
  254. ->fields(array(
  255. 'permission' => 'edit own product products',
  256. ))
  257. ->condition('permission', 'edit own products')
  258. ->execute();
  259. db_update('role_permission')
  260. ->fields(array(
  261. 'permission' => 'edit all product products',
  262. ))
  263. ->condition('permission', 'edit all products')
  264. ->execute();
  265. db_update('role_permission')
  266. ->fields(array(
  267. 'permission' => 'delete own product products',
  268. ))
  269. ->condition('permission', 'delete own products')
  270. ->execute();
  271. db_update('role_permission')
  272. ->fields(array(
  273. 'permission' => 'delete all product products',
  274. ))
  275. ->condition('permission', 'delete all products')
  276. ->execute();
  277. return t('Renamed node permissions for the Product type.');
  278. }
  279. /**
  280. * Use actual node permissions for product types.
  281. */
  282. function uc_product_update_7001() {
  283. $types = db_query("SELECT pcid FROM {uc_product_classes}")->fetchCol();
  284. array_unshift($types, 'product');
  285. foreach ($types as $type) {
  286. $node_perms = array_keys(node_list_permissions($type));
  287. foreach ($node_perms as $node_perm) {
  288. $product_perm = str_replace(array('any', 'content'), array('all', 'products'), $node_perm);
  289. foreach (user_roles(FALSE, $product_perm) as $rid => $role) {
  290. db_merge('role_permission')
  291. ->key(array(
  292. 'rid' => $rid,
  293. 'permission' => $node_perm,
  294. ))
  295. ->fields(array(
  296. 'module' => 'node',
  297. ))
  298. ->execute();
  299. }
  300. // Clean up.
  301. db_delete('role_permission')
  302. ->condition('permission', $product_perm)
  303. ->execute();
  304. }
  305. }
  306. return t('Changed product node permissions to the actual node permissions.');
  307. }
  308. /**
  309. * Makes sure Image is enabled.
  310. */
  311. function uc_product_update_7002() {
  312. if (module_exists('uc_product') && !module_exists('image')) {
  313. module_enable(array('file', 'image'), FALSE);
  314. return t('Enabled Image module.');
  315. }
  316. }
  317. /**
  318. * Set default display settings for product teasers.
  319. */
  320. function uc_product_update_7003() {
  321. $types = db_query("SELECT pcid FROM {uc_product_classes}")->fetchCol();
  322. array_unshift($types, 'product');
  323. foreach ($types as $type) {
  324. uc_product_set_teaser_display($type);
  325. }
  326. variable_del('uc_product_field_enabled');
  327. variable_del('uc_product_field_weight');
  328. return t('Set display settings for product teasers.');
  329. }
  330. /**
  331. * Remove unused variables.
  332. */
  333. function uc_product_update_7004() {
  334. variable_del('uc_product_add_to_cart_teaser');
  335. variable_del('uc_teaser_add_to_cart_text');
  336. variable_del('uc_product_add_to_cart_text');
  337. }
  338. /**
  339. * Remove unused unique hash column.
  340. */
  341. function uc_product_update_7300() {
  342. db_drop_field('uc_products', 'unique_hash');
  343. }
  344. /**
  345. * Sets default display settings for product teasers.
  346. */
  347. function uc_product_set_teaser_display($type) {
  348. $settings = field_bundle_settings('node', $type);
  349. $settings['view_modes']['teaser']['custom_settings'] = TRUE;
  350. $fields = array('model', 'list_price', 'cost', 'weight', 'dimensions');
  351. foreach ($fields as $field) {
  352. $settings['extra_fields']['display'][$field]['teaser'] = array(
  353. 'weight' => 0,
  354. 'visible' => FALSE,
  355. );
  356. }
  357. field_bundle_settings('node', $type, $settings);
  358. }