uc_product.module 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  1. <?php
  2. /**
  3. * @file
  4. * The product module for Ubercart.
  5. *
  6. * Provides information that is common to all products, and user-defined product
  7. * classes for more specification.
  8. */
  9. /**
  10. * Implements hook_menu().
  11. */
  12. function uc_product_menu() {
  13. $items = array();
  14. $items['admin/store/products'] = array(
  15. 'title' => 'Products',
  16. 'description' => 'Administer products, classes, and more.',
  17. 'page callback' => 'drupal_goto',
  18. 'page arguments' => array('admin/store/products/view'),
  19. 'access arguments' => array('administer products'),
  20. 'weight' => -2,
  21. 'position' => 'left',
  22. );
  23. // admin/store/products/view is provided by Views.
  24. $items['admin/store/products/classes'] = array(
  25. 'title' => 'Manage classes',
  26. 'description' => 'Create and edit product node types.',
  27. 'access arguments' => array('administer product classes'),
  28. 'page callback' => 'uc_product_class_default',
  29. 'weight' => -2,
  30. 'file' => 'uc_product.admin.inc',
  31. );
  32. $items['admin/store/settings/products'] = array(
  33. 'title' => 'Products',
  34. 'description' => 'Configure product settings.',
  35. 'access arguments' => array('administer products'),
  36. 'page callback' => 'drupal_get_form',
  37. 'page arguments' => array('uc_product_settings_form'),
  38. 'file' => 'uc_product.admin.inc',
  39. );
  40. // Insert subitems into the edit node page for product types.
  41. $items['node/%node/edit/product'] = array(
  42. 'title' => 'Product',
  43. 'access callback' => 'uc_product_edit_access',
  44. 'access arguments' => array(1),
  45. 'weight' => -10,
  46. 'type' => MENU_DEFAULT_LOCAL_TASK,
  47. 'file' => 'uc_product.admin.inc',
  48. );
  49. $features = module_invoke_all('uc_product_feature');
  50. if (!empty($features)) {
  51. $items['node/%node/edit/features'] = array(
  52. 'title' => 'Features',
  53. 'page callback' => 'uc_product_features',
  54. 'page arguments' => array(1),
  55. 'access callback' => 'uc_product_feature_access',
  56. 'access arguments' => array(1),
  57. 'weight' => 10,
  58. 'type' => MENU_LOCAL_TASK,
  59. 'file' => 'uc_product.admin.inc',
  60. );
  61. $items['node/%node/edit/features/%/%'] = array(
  62. 'page callback' => 'uc_product_feature_edit',
  63. 'page arguments' => array(1, 4, 5),
  64. 'access callback' => 'uc_product_feature_access',
  65. 'access arguments' => array(1),
  66. 'file' => 'uc_product.admin.inc',
  67. );
  68. $items['node/%node/edit/features/%/%uc_product_feature/delete'] = array(
  69. 'title' => 'Delete feature',
  70. 'page callback' => 'drupal_get_form',
  71. 'page arguments' => array('uc_product_feature_confirm_delete', 1, 4, 5),
  72. 'access callback' => 'uc_product_feature_access',
  73. 'access arguments' => array(1),
  74. 'file' => 'uc_product.admin.inc',
  75. );
  76. }
  77. $items['admin/store/settings/products/defaults'] = array(
  78. 'access arguments' => array('administer products'),
  79. 'page callback' => 'uc_product_image_defaults',
  80. 'file' => 'uc_product.admin.inc',
  81. );
  82. $items['admin/store/products/classes/%uc_product_class'] = array(
  83. 'title' => 'Product class',
  84. 'access arguments' => array('administer product classes'),
  85. 'page callback' => 'drupal_get_form',
  86. 'page arguments' => array('uc_product_class_form', 4),
  87. 'file' => 'uc_product.admin.inc',
  88. );
  89. $items['admin/store/products/classes/%uc_product_class/edit'] = array(
  90. 'title' => 'Edit',
  91. 'type' => MENU_DEFAULT_LOCAL_TASK,
  92. 'weight' => -5,
  93. 'file' => 'uc_product.admin.inc',
  94. );
  95. $items['admin/store/products/classes/%uc_product_class/delete'] = array(
  96. 'access callback' => 'uc_product_class_delete_access',
  97. 'access arguments' => array(4),
  98. 'page callback' => 'drupal_get_form',
  99. 'page arguments' => array('uc_product_class_delete_confirm', 4),
  100. 'file' => 'uc_product.admin.inc',
  101. );
  102. return $items;
  103. }
  104. /**
  105. * Menu access callback for deleting product classes.
  106. */
  107. function uc_product_class_delete_access($class) {
  108. return user_access('administer product classes') && !$class->locked;
  109. }
  110. /**
  111. * Implements hook_admin_paths().
  112. */
  113. function uc_product_admin_paths() {
  114. $paths = array(
  115. 'node/*/edit/product' => TRUE,
  116. 'node/*/edit/features' => TRUE,
  117. 'node/*/edit/features/*' => TRUE,
  118. );
  119. return $paths;
  120. }
  121. /**
  122. * Implements hook_permission().
  123. */
  124. function uc_product_permission() {
  125. $perms = array(
  126. 'administer products' => array(
  127. 'title' => t('Administer products'),
  128. ),
  129. 'administer product classes' => array(
  130. 'title' => t('Administer product classes'),
  131. ),
  132. 'administer product features' => array(
  133. 'title' => t('Administer product features'),
  134. ),
  135. 'administer own product features' => array(
  136. 'title' => t('Administer own product features'),
  137. ),
  138. );
  139. return $perms;
  140. }
  141. /**
  142. * Menu access callback for 'node/%node/edit/features'.
  143. */
  144. function uc_product_feature_access($node) {
  145. global $user;
  146. return uc_product_is_product($node) && $node->type != 'product_kit' &&
  147. ((user_access('administer product features') || (user_access('administer own product features') && ($user->uid == $node->uid))));
  148. }
  149. /**
  150. * Implements hook_theme().
  151. */
  152. function uc_product_theme() {
  153. return array(
  154. 'uc_product_model' => array(
  155. 'variables' => array('model' => '', 'view_mode' => 'full', 'attributes' => array()),
  156. 'file' => 'uc_product.theme.inc',
  157. ),
  158. 'uc_product_add_to_cart' => array(
  159. 'variables' => array('form' => NULL, 'view_mode' => 'full'),
  160. 'file' => 'uc_product.theme.inc',
  161. ),
  162. 'uc_product_price' => array(
  163. 'render element' => 'element',
  164. 'file' => 'uc_product.theme.inc',
  165. ),
  166. 'uc_product_weight' => array(
  167. 'variables' => array('amount' => 0, 'units' => NULL, 'view_mode' => 'full', 'attributes' => array()),
  168. 'file' => 'uc_product.theme.inc',
  169. ),
  170. 'uc_product_dimensions' => array(
  171. 'variables' => array('length' => 0, 'width' => 0, 'height' => 0, 'units' => NULL, 'view_mode' => 'full', 'attributes' => array()),
  172. 'file' => 'uc_product.theme.inc',
  173. ),
  174. 'uc_product_image' => array(
  175. 'variables' => array('images' => array(), 'view_mode' => 'full'),
  176. 'file' => 'uc_product.theme.inc',
  177. ),
  178. 'uc_product_feature_add_form' => array(
  179. 'render element' => 'form',
  180. 'file' => 'uc_product.admin.inc',
  181. ),
  182. );
  183. }
  184. /**
  185. * Implements hook_field_formatter_info().
  186. */
  187. function uc_product_field_formatter_info() {
  188. return array(
  189. 'uc_product_image' => array(
  190. 'label' => t('Ubercart product'),
  191. 'description' => t('The first image is displayed with the "uc_product" image style, and subsequent images are displayed below it with the "uc_thumbnail" image style.'),
  192. 'field types' => array('image'),
  193. ),
  194. );
  195. }
  196. /**
  197. * Implements hook_field_formatter_view().
  198. */
  199. function uc_product_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  200. $element = array();
  201. switch ($display['type']) {
  202. case 'uc_product_image':
  203. $element[0] = array(
  204. '#theme' => 'uc_product_image',
  205. '#images' => $items,
  206. );
  207. break;
  208. }
  209. return $element;
  210. }
  211. /**
  212. * Implements hook_node_info().
  213. *
  214. * Creates node types for each product class and other product modules.
  215. */
  216. function uc_product_node_info($reset = FALSE) {
  217. static $types = array();
  218. $title_label = t('Name');
  219. if (empty($types) || $reset) {
  220. $types = array();
  221. $defaults = module_invoke_all('uc_product_default_classes');
  222. $types['product'] = isset($defaults['product']) ? $defaults['product'] : array();
  223. $types['product'] += array(
  224. 'name' => t('Product'),
  225. 'base' => 'uc_product',
  226. 'description' => t('Use <em>products</em> to represent items for sale on the website, including all the unique information that can be attributed to a specific model number.'),
  227. 'title_label' => $title_label,
  228. );
  229. $classes = uc_product_class_load(NULL, $reset);
  230. foreach ($classes as $class) {
  231. $class = (array) $class;
  232. $class['base'] = 'uc_product';
  233. $types[$class['pcid']] = $class + array(
  234. 'title_label' => $title_label,
  235. );
  236. }
  237. }
  238. return $types;
  239. }
  240. /**
  241. * Implements hook_node_type_update().
  242. *
  243. * Ensure product class names and descriptions are synchronised if the
  244. * content type is updated.
  245. */
  246. function uc_product_node_type_update($info) {
  247. $existing_type = !empty($info->old_type) ? $info->old_type : $info->type;
  248. db_update('uc_product_classes')
  249. ->fields(array(
  250. 'pcid' => $info->type,
  251. 'name' => $info->name,
  252. 'description' => $info->description,
  253. ))
  254. ->condition('pcid', $existing_type)
  255. ->execute();
  256. // Reset static variable data and load new info.
  257. uc_product_node_info(TRUE);
  258. }
  259. /**
  260. * Implements hook_forms().
  261. *
  262. * Register an "add to cart" form for each product to prevent id collisions.
  263. */
  264. function uc_product_forms($form_id, $args) {
  265. $forms = array();
  266. if (isset($args[0]->nid) && isset($args[0]->type)) {
  267. $product = $args[0];
  268. if (in_array($product->type, array_keys(uc_product_node_info()))) {
  269. $forms['uc_product_add_to_cart_form_' . $product->nid] = array('callback' => 'uc_product_add_to_cart_form');
  270. $forms['uc_catalog_buy_it_now_form_' . $product->nid] = array('callback' => 'uc_catalog_buy_it_now_form');
  271. }
  272. }
  273. return $forms;
  274. }
  275. /**
  276. * Menu access callback for 'node/%node/edit/product'.
  277. */
  278. function uc_product_edit_access($node) {
  279. // Re-inherit access callback for 'node/%node/edit'.
  280. return uc_product_is_product($node) && node_access('update', $node);
  281. }
  282. /**
  283. * Implements hook_form().
  284. *
  285. * @ingroup forms
  286. */
  287. function uc_product_form($node, $form_state) {
  288. $type = node_type_get_type($node);
  289. $form['title'] = array(
  290. '#type' => 'textfield',
  291. '#title' => check_plain($type->title_label),
  292. '#required' => TRUE,
  293. '#default_value' => $node->title,
  294. '#maxlength' => 255,
  295. '#weight' => -5,
  296. );
  297. $form['base'] = array(
  298. '#type' => 'fieldset',
  299. '#title' => t('Product information'),
  300. '#collapsible' => TRUE,
  301. '#collapsed' => TRUE,
  302. '#weight' => -10,
  303. '#attributes' => array('class' => array('product-field')),
  304. '#group' => 'additional_settings',
  305. '#attached' => array(
  306. 'js' => array(
  307. 'vertical-tabs' => drupal_get_path('module', 'uc_product') . '/uc_product.js',
  308. ),
  309. ),
  310. );
  311. $form['base']['model'] = array(
  312. '#type' => 'textfield',
  313. '#title' => t('SKU'),
  314. '#required' => TRUE,
  315. '#default_value' => $node->model,
  316. '#description' => t('Product SKU/model.'),
  317. '#weight' => 0,
  318. '#size' => 32,
  319. );
  320. $form['base']['prices'] = array(
  321. '#type' => 'container',
  322. '#attributes' => array('class' => array('uc-inline-form', 'clearfix')),
  323. '#weight' => 5,
  324. );
  325. $form['base']['prices']['list_price'] = array(
  326. '#type' => 'uc_price',
  327. '#title' => t('List price'),
  328. '#default_value' => $node->list_price,
  329. '#description' => t('The listed MSRP.'),
  330. '#weight' => 0,
  331. );
  332. $form['base']['prices']['cost'] = array(
  333. '#type' => 'uc_price',
  334. '#title' => t('Cost'),
  335. '#default_value' => $node->cost,
  336. '#description' => t("Your store's cost."),
  337. '#weight' => 1,
  338. );
  339. $form['base']['prices']['sell_price'] = array(
  340. '#type' => 'uc_price',
  341. '#title' => t('Sell price'),
  342. '#required' => TRUE,
  343. '#default_value' => $node->sell_price,
  344. '#description' => t('Customer purchase price.'),
  345. '#weight' => 2,
  346. );
  347. $form['base']['shippable'] = array(
  348. '#type' => 'checkbox',
  349. '#title' => t('Product is shippable.'),
  350. '#default_value' => $node->shippable,
  351. '#weight' => 10,
  352. );
  353. $form['base']['weight'] = array(
  354. '#type' => 'container',
  355. '#attributes' => array('class' => array('uc-inline-form', 'clearfix')),
  356. '#states' => array(
  357. 'invisible' => array(
  358. 'input[name="shippable"]' => array('checked' => FALSE),
  359. ),
  360. ),
  361. '#weight' => 15,
  362. );
  363. $form['base']['weight']['weight'] = array(
  364. '#type' => 'textfield',
  365. '#title' => t('Weight'),
  366. '#default_value' => $node->weight,
  367. '#size' => 10,
  368. '#element_validate' => array('uc_store_validate_number'),
  369. );
  370. $units = array(
  371. 'lb' => t('Pounds'),
  372. 'kg' => t('Kilograms'),
  373. 'oz' => t('Ounces'),
  374. 'g' => t('Grams'),
  375. );
  376. $form['base']['weight']['weight_units'] = array(
  377. '#type' => 'select',
  378. '#title' => t('Units'),
  379. '#default_value' => $node->weight_units,
  380. '#options' => $units,
  381. );
  382. $form['base']['dimensions'] = array(
  383. '#type' => 'container',
  384. '#attributes' => array('class' => array('uc-inline-form', 'clearfix')),
  385. '#states' => array(
  386. 'invisible' => array(
  387. 'input[name="shippable"]' => array('checked' => FALSE),
  388. ),
  389. ),
  390. '#weight' => 20,
  391. );
  392. $form['base']['dimensions']['dim_length'] = array(
  393. '#type' => 'textfield',
  394. '#title' => t('Length'),
  395. '#default_value' => $node->length,
  396. '#size' => 10,
  397. '#element_validate' => array('uc_store_validate_number'),
  398. );
  399. $form['base']['dimensions']['dim_width'] = array(
  400. '#type' => 'textfield',
  401. '#title' => t('Width'),
  402. '#default_value' => $node->width,
  403. '#size' => 10,
  404. '#element_validate' => array('uc_store_validate_number'),
  405. );
  406. $form['base']['dimensions']['dim_height'] = array(
  407. '#type' => 'textfield',
  408. '#title' => t('Height'),
  409. '#default_value' => $node->height,
  410. '#size' => 10,
  411. '#element_validate' => array('uc_store_validate_number'),
  412. );
  413. $form['base']['dimensions']['length_units'] = array(
  414. '#type' => 'select',
  415. '#title' => t('Units'),
  416. '#options' => array(
  417. 'in' => t('Inches'),
  418. 'ft' => t('Feet'),
  419. 'cm' => t('Centimeters'),
  420. 'mm' => t('Millimeters'),
  421. ),
  422. '#default_value' => $node->length_units,
  423. );
  424. $form['base']['pkg_qty'] = array(
  425. '#type' => 'uc_quantity',
  426. '#title' => t('Maximum package quantity'),
  427. '#default_value' => $node->pkg_qty,
  428. '#description' => t('At most, how many of these items can fit in your largest box? Orders that exceed this value will be split into multiple packages when retrieving shipping quotes.'),
  429. '#weight' => 25,
  430. '#states' => array(
  431. 'invisible' => array(
  432. 'input[name="shippable"]' => array('checked' => FALSE),
  433. ),
  434. ),
  435. );
  436. if (variable_get('uc_product_add_to_cart_qty', FALSE)) {
  437. $form['base']['default_qty'] = array(
  438. '#type' => 'uc_quantity',
  439. '#title' => t('Default quantity to add to cart'),
  440. '#default_value' => $node->default_qty,
  441. '#description' => t('Use 0 to disable the quantity field next to the add to cart button.'),
  442. '#weight' => 27,
  443. '#allow_zero' => TRUE,
  444. );
  445. }
  446. else {
  447. $form['base']['default_qty'] = array(
  448. '#type' => 'value',
  449. '#value' => $node->default_qty,
  450. );
  451. }
  452. $form['base']['ordering'] = array(
  453. '#type' => 'weight',
  454. '#title' => t('List position'),
  455. '#description' => t("Specify a value to set this product's position in product lists.<br />Products in the same position will be sorted alphabetically."),
  456. '#delta' => 25,
  457. '#default_value' => $node->ordering,
  458. '#weight' => 30,
  459. );
  460. return $form;
  461. }
  462. /**
  463. * Implements hook_prepare().
  464. */
  465. function uc_product_prepare($node) {
  466. $defaults = array(
  467. 'model' => '',
  468. 'list_price' => 0,
  469. 'cost' => 0,
  470. 'sell_price' => 0,
  471. 'weight' => 0,
  472. 'weight_units' => variable_get('uc_weight_unit', 'lb'),
  473. 'length' => 0,
  474. 'width' => 0,
  475. 'height' => 0,
  476. 'length_units' => variable_get('uc_length_unit', 'in'),
  477. 'pkg_qty' => 1,
  478. 'default_qty' => 1,
  479. 'shippable' => variable_get('uc_product_shippable_' . $node->type, 1),
  480. 'ordering' => 0,
  481. );
  482. foreach ($defaults as $key => $value) {
  483. if (!isset($node->$key)) {
  484. $node->$key = $value;
  485. }
  486. }
  487. }
  488. /**
  489. * Implements hook_insert().
  490. */
  491. function uc_product_insert($node) {
  492. if (isset($node->dim_length)) {
  493. $node->length = $node->dim_length;
  494. }
  495. if (isset($node->dim_width)) {
  496. $node->width = $node->dim_width;
  497. }
  498. if (isset($node->dim_height)) {
  499. $node->height = $node->dim_height;
  500. }
  501. drupal_write_record('uc_products', $node);
  502. }
  503. /**
  504. * Implements hook_update().
  505. */
  506. function uc_product_update($node) {
  507. if (isset($node->dim_length)) {
  508. $node->length = $node->dim_length;
  509. }
  510. if (isset($node->dim_width)) {
  511. $node->width = $node->dim_width;
  512. }
  513. if (isset($node->dim_height)) {
  514. $node->height = $node->dim_height;
  515. }
  516. if (!empty($node->revision)) {
  517. drupal_write_record('uc_products', $node);
  518. }
  519. else {
  520. drupal_write_record('uc_products', $node, 'vid');
  521. }
  522. }
  523. /**
  524. * Implements hook_load().
  525. */
  526. function uc_product_load($nodes) {
  527. $vids = array();
  528. foreach ($nodes as $node) {
  529. $vids[$node->nid] = $node->vid;
  530. }
  531. $result = db_query('SELECT nid, model, list_price, cost, sell_price, weight, weight_units, length, width, height, length_units, pkg_qty, default_qty, ordering, shippable FROM {uc_products} WHERE vid IN (:vids)', array(':vids' => $vids));
  532. foreach ($result as $node) {
  533. foreach ($node as $field => $value) {
  534. $nodes[$node->nid]->$field = $value;
  535. }
  536. $nodes[$node->nid]->price = $nodes[$node->nid]->sell_price;
  537. }
  538. }
  539. /**
  540. * Gets a specific, cloned, altered variant of a product node.
  541. *
  542. * Generally, you should always use uc_product_load_variant() instead,
  543. * except when node_load() cannot be invoked, e.g. when implementing
  544. * hook_node_load().
  545. *
  546. * @param $node
  547. * The product node to alter. Throws an exception if this is already a
  548. * product variant.
  549. * @param $data
  550. * Optional data to add to the product before invoking the alter hooks.
  551. *
  552. * @return object
  553. * An variant of the product, altered based on the provided data.
  554. *
  555. * @throws \Exception
  556. * If the caller tries to create a variant of a variant.
  557. */
  558. function _uc_product_get_variant($node, $data = FALSE) {
  559. if (!empty($node->variant)) {
  560. throw new Exception(t('Cannot create a variant of a variant.'));
  561. }
  562. $node = clone $node;
  563. if (!empty($data)) {
  564. $node->data = $data;
  565. }
  566. // Ensure that $node->data is an array (user module leaves it serialized).
  567. if (isset($node->data) && !is_array($node->data)) {
  568. $node->data = unserialize($node->data);
  569. }
  570. drupal_alter('uc_product', $node);
  571. $node->variant = TRUE;
  572. if (!isset($node->data['module'])) {
  573. $node->data['module'] = 'uc_product';
  574. }
  575. return $node;
  576. }
  577. /**
  578. * Loads a specific altered variant of a product node.
  579. *
  580. * The (possibly cached) base product remains unaltered.
  581. *
  582. * @param $nid
  583. * The nid of the product to load.
  584. * @param $data
  585. * Optional data to add to the product before invoking the alter hooks.
  586. *
  587. * @return
  588. * A variant of the product, altered based on the provided data, or FALSE
  589. * if the node is not found.
  590. */
  591. function uc_product_load_variant($nid, $data = FALSE) {
  592. if ($node = node_load($nid)) {
  593. return _uc_product_get_variant($node, $data);
  594. }
  595. else {
  596. return FALSE;
  597. }
  598. }
  599. /**
  600. * Implements hook_uc_product_alter().
  601. *
  602. * Invokes rules event to allow product modifications.
  603. */
  604. function uc_product_uc_product_alter(&$node) {
  605. if (module_exists('rules')) {
  606. rules_invoke_event('uc_product_load', $node);
  607. }
  608. }
  609. /**
  610. * Implements hook_delete().
  611. */
  612. function uc_product_delete(&$node) {
  613. $features = uc_product_feature_load_multiple($node->nid);
  614. foreach ($features as $feature) {
  615. uc_product_feature_delete($feature->pfid);
  616. }
  617. db_delete('uc_products')
  618. ->condition('nid', $node->nid)
  619. ->execute();
  620. }
  621. /**
  622. * Dynamically replaces parts of a product view based on form input.
  623. *
  624. * If a module adds an input field to the add-to-cart form which affects some
  625. * aspect of a product (e.g. display price or weight), it should attach an
  626. * #ajax callback to that form element, and use this function in the callback
  627. * to build updated content for the affected fields.
  628. *
  629. * @param $form_state
  630. * The current form state. This must contain a 'variant' entry in the
  631. * 'storage' array which represents the product as configured by user input
  632. * data. In most cases, this is provided automatically by
  633. * uc_product_add_to_cart_form_validate().
  634. * @param $keys
  635. * An array of keys in the built product content which should be replaced
  636. * (e.g. 'display_price').
  637. *
  638. * @return
  639. * An array of Ajax commands.
  640. */
  641. function uc_product_view_ajax_commands($form_state, $keys) {
  642. $commands = array();
  643. if (variable_get('uc_product_update_node_view', FALSE) && !empty($form_state['storage']['variant'])) {
  644. $node_div = '.uc-product-' . $form_state['storage']['variant']->nid;
  645. $build = node_view($form_state['storage']['variant']);
  646. foreach ($keys as $key) {
  647. if (isset($build[$key])) {
  648. $id = $node_div . '.' . str_replace('_', '-', $key);
  649. $commands[] = ajax_command_replace($id, drupal_render($build[$key]));
  650. }
  651. }
  652. }
  653. return $commands;
  654. }
  655. /**
  656. * Implements hook_view().
  657. */
  658. function uc_product_view($node, $view_mode) {
  659. // Give modules a chance to alter this product. If it is a variant, this will
  660. // have been done already by uc_product_load_variant(), so we check a flag to
  661. // be sure not to alter twice -- cf. entity_prepare_view().
  662. $variant = empty($node->variant) ? _uc_product_get_variant($node) : $node;
  663. // Skip the add to cart form in comment reply forms.
  664. if (arg(0) != 'comment' && arg(1) != 'reply') {
  665. // Build the 'add to cart' form, and use the updated variant based on data
  666. // provided by the form (e.g. attribute default options).
  667. if (module_exists('uc_cart') && $variant->nid && empty($variant->data['display_only'])) {
  668. $add_to_cart_form = drupal_get_form('uc_product_add_to_cart_form_' . $variant->nid, $variant);
  669. if (variable_get('uc_product_update_node_view', FALSE)) {
  670. $variant = $add_to_cart_form['node']['#value'];
  671. }
  672. }
  673. }
  674. $node->content['display_price'] = array(
  675. '#theme' => 'uc_product_price',
  676. '#value' => isset($variant->price) ? $variant->price : $variant->sell_price,
  677. '#suffixes' => array(),
  678. '#attributes' => array(
  679. 'class' => array(
  680. 'display-price',
  681. 'uc-product-' . $node->nid,
  682. ),
  683. ),
  684. );
  685. $node->content['model'] = array(
  686. '#theme' => 'uc_product_model',
  687. '#model' => $variant->model,
  688. '#view_mode' => $view_mode,
  689. );
  690. $node->content['list_price'] = array(
  691. '#theme' => 'uc_product_price',
  692. '#title' => t('List price:'),
  693. '#value' => $variant->list_price,
  694. '#attributes' => array(
  695. 'class' => array(
  696. 'list-price',
  697. ),
  698. ),
  699. );
  700. $node->content['cost'] = array(
  701. '#theme' => 'uc_product_price',
  702. '#title' => t('Cost:'),
  703. '#value' => $variant->cost,
  704. '#attributes' => array(
  705. 'class' => array(
  706. 'cost',
  707. 'uc-product-' . $node->nid,
  708. ),
  709. ),
  710. '#access' => user_access('administer products'),
  711. );
  712. $node->content['sell_price'] = array(
  713. '#theme' => 'uc_product_price',
  714. '#title' => t('Price:'),
  715. '#value' => $variant->sell_price,
  716. '#attributes' => array(
  717. 'class' => array(
  718. 'sell-price',
  719. ),
  720. ),
  721. );
  722. $node->content['weight'] = array(
  723. '#theme' => 'uc_product_weight',
  724. '#amount' => $variant->weight,
  725. '#units' => $variant->weight_units,
  726. '#view_mode' => $view_mode,
  727. '#attributes' => array(
  728. 'class' => array(
  729. 'uc-product-' . $node->nid,
  730. ),
  731. ),
  732. );
  733. $node->content['dimensions'] = array(
  734. '#theme' => 'uc_product_dimensions',
  735. '#length' => $variant->length,
  736. '#width' => $variant->width,
  737. '#height' => $variant->height,
  738. '#units' => $variant->length_units,
  739. '#view_mode' => $view_mode,
  740. );
  741. if (isset($add_to_cart_form)) {
  742. $node->content['add_to_cart'] = array(
  743. '#theme' => 'uc_product_add_to_cart',
  744. '#view_mode' => $view_mode,
  745. '#form' => $add_to_cart_form,
  746. );
  747. }
  748. $node->content['#node'] = $variant;
  749. return $node;
  750. }
  751. /**
  752. * Implements hook_preprocess_node().
  753. *
  754. * Product classes default to using node--product.tpl.php if they don't have
  755. * their own template.
  756. *
  757. * @see theme()
  758. * @see MODULE_preprocess_HOOK()
  759. */
  760. function uc_product_preprocess_node(&$variables) {
  761. if (uc_product_is_product($variables['type'])) {
  762. array_unshift($variables['theme_hook_suggestions'], 'node__product');
  763. }
  764. }
  765. /**
  766. * Implements hook_preprocess_html().
  767. *
  768. * Adds a body class to product node pages.
  769. *
  770. * @see html.tpl.php
  771. */
  772. function uc_product_preprocess_html(&$variables) {
  773. if ($node = menu_get_object()) {
  774. if (uc_product_is_product($node)) {
  775. $variables['classes_array'][] = 'uc-product-node';
  776. }
  777. }
  778. }
  779. /**
  780. * Implements hook_form_alter().
  781. *
  782. * @see uc_product_save_continue_submit()
  783. */
  784. function uc_product_form_alter(&$form, &$form_state, $form_id) {
  785. // Add a button to product node forms to continue editing after saving.
  786. if (uc_product_is_product_form($form)) {
  787. $form['actions']['save_continue'] = array(
  788. '#type' => 'submit',
  789. '#value' => t('Save and continue'),
  790. '#weight' => 7,
  791. '#submit' => array('node_form_submit', 'uc_product_save_continue_submit'),
  792. );
  793. }
  794. }
  795. /**
  796. * After the node is saved, redirects to the node edit page.
  797. *
  798. * Some modules add local tasks to product edit forms, but only after it has an
  799. * nid. Redirecting facilitates the common workflow of continuing to those
  800. * tasks.
  801. */
  802. function uc_product_save_continue_submit($form, &$form_state) {
  803. $form_state['redirect'] = 'node/' . $form_state['nid'] . '/edit';
  804. }
  805. /**
  806. * Implements hook_form_FORM_ID_alter().
  807. *
  808. * Adds a default image field setting to product content types.
  809. */
  810. function uc_product_form_node_type_form_alter(&$form, &$form_state) {
  811. $type = $form['#node_type'];
  812. if (!uc_product_is_product($type->type)) {
  813. return;
  814. }
  815. $form['uc_product'] = array(
  816. '#type' => 'fieldset',
  817. '#title' => t('Ubercart product settings'),
  818. '#collapsible' => TRUE,
  819. '#collapsed' => TRUE,
  820. '#group' => 'additional_settings',
  821. '#attached' => array(
  822. 'js' => array(
  823. 'vertical-tabs' => drupal_get_path('module', 'uc_product') . '/uc_product.js',
  824. ),
  825. ),
  826. );
  827. // Shippable.
  828. $form['uc_product']['uc_product_shippable'] = array(
  829. '#type' => 'checkbox',
  830. '#title' => t('Product and its derivatives are shippable.'),
  831. '#default_value' => variable_get('uc_product_shippable_' . $type->type, 1),
  832. '#description' => t('This setting can still be overridden on the node form.'),
  833. '#return_value' => 1,
  834. '#weight' => -5,
  835. );
  836. // Image field.
  837. $instances = field_info_instances('node', $type->type);
  838. $options = array('' => t('None'));
  839. foreach ($instances as $field_name => $instance) {
  840. if (strpos($instance['widget']['type'], 'image') !== FALSE || ($instance['widget']['type'] == 'media_generic' && isset($instance['widget']['settings']['allowed_types']) && $instance['widget']['settings']['allowed_types'] == array('image' => 'image'))) {
  841. $options[$field_name] = $instance['label'];
  842. }
  843. }
  844. $form['uc_product']['uc_image'] = array(
  845. '#type' => 'select',
  846. '#title' => t('Product image field'),
  847. '#default_value' => variable_get('uc_image_' . $type->type, NULL),
  848. '#options' => $options,
  849. '#description' => t('The selected field will be used on Ubercart pages to represent the products of this content type.'),
  850. '#weight' => -4,
  851. );
  852. }
  853. /**
  854. * Implements hook_field_extra_fields().
  855. *
  856. * Adds the "Product information".
  857. */
  858. function uc_product_field_extra_fields() {
  859. $extra = array();
  860. foreach (uc_product_types() as $type) {
  861. $extra['node'][$type] = array(
  862. 'display' => array(
  863. 'display_price' => array(
  864. 'label' => t('Display price'),
  865. 'description' => t('High-visibility sell price.'),
  866. 'weight' => -1,
  867. ),
  868. 'model' => array(
  869. 'label' => t('SKU'),
  870. 'description' => t('Product SKU/model.'),
  871. 'weight' => 0,
  872. ),
  873. 'list_price' => array(
  874. 'label' => t('List price'),
  875. 'description' => t('The listed MSRP.'),
  876. 'weight' => 2,
  877. ),
  878. 'cost' => array(
  879. 'label' => t('Cost'),
  880. 'description' => t("Your store's cost."),
  881. 'weight' => 3,
  882. ),
  883. 'sell_price' => array(
  884. 'label' => t('Sell price'),
  885. 'description' => t('Customer purchase price.'),
  886. 'weight' => 4,
  887. ),
  888. 'weight' => array(
  889. 'label' => t('Weight'),
  890. 'description' => t('Physical weight (lbs, kg, etc.).'),
  891. 'weight' => 5,
  892. ),
  893. 'dimensions' => array(
  894. 'label' => t('Dimensions'),
  895. 'description' => t('Physical dimensions of the packaged product.'),
  896. 'weight' => 6,
  897. ),
  898. 'add_to_cart' => array(
  899. 'label' => t('Add to cart form'),
  900. 'description' => t('Add to cart form'),
  901. 'weight' => 10,
  902. ),
  903. ),
  904. );
  905. }
  906. return $extra;
  907. }
  908. /**
  909. * Implements hook_field_delete_instance().
  910. *
  911. * Resets a content type's default image field setting when that field instance
  912. * is removed.
  913. */
  914. function uc_product_field_delete_instance($instance) {
  915. if ($instance['entity_type'] == 'node' && $instance['field_name'] == variable_get('uc_image_' . $instance['bundle'], NULL)) {
  916. variable_del('uc_image_' . $instance['bundle']);
  917. }
  918. }
  919. /**
  920. * Implements hook_uc_product_types().
  921. */
  922. function uc_product_uc_product_types() {
  923. return array_keys(uc_product_node_info());
  924. }
  925. /**
  926. * Implements hook_uc_store_status().
  927. *
  928. * Displays the status of the product image handlers.
  929. *
  930. * @see uc_product_image_defaults()
  931. */
  932. function uc_product_uc_store_status() {
  933. // Check for filefields on products.
  934. if ($field = variable_get('uc_image_product', '')) {
  935. $instances = field_info_instances('node', 'product');
  936. $field_check = (bool) count($instances[$field]);
  937. }
  938. else {
  939. $field_check = FALSE;
  940. }
  941. if ($field_check) {
  942. $status = 'ok';
  943. $description = t('Product image support has been automatically configured by Ubercart.');
  944. }
  945. else {
  946. $status = 'warning';
  947. $description = t('<a href="!url">Click here</a> to automatically configure the following items for core image support:', array('!url' => url('admin/store/settings/products/defaults')));
  948. $items[] = t('The Image file field has not been created for products.');
  949. $description .= theme('item_list', array('items' => $items)) . t('(This action is not required and should not be taken if you do not need images or have implemented your own image support.)');
  950. }
  951. return array(array('status' => $status, 'title' => t('Images'), 'desc' => $description));
  952. }
  953. /**
  954. * Implements hook_uc_cart_display().
  955. */
  956. function uc_product_uc_cart_display($item) {
  957. $node = node_load($item->nid);
  958. $element = array();
  959. $element['nid'] = array('#type' => 'value', '#value' => $node->nid);
  960. $element['module'] = array('#type' => 'value', '#value' => 'uc_product');
  961. $element['remove'] = array('#type' => 'submit', '#value' => t('Remove'));
  962. if (node_access('view', $node) && $uri = entity_uri('node', $node)) {
  963. $element['title'] = array(
  964. '#markup' => l($item->title, $uri['path'], $uri['options']),
  965. );
  966. }
  967. else {
  968. $element['title'] = array('#markup' => $item->title);
  969. }
  970. $element['#total'] = $item->price * $item->qty;
  971. $element['#suffixes'] = array();
  972. $element['data'] = array('#type' => 'hidden', '#value' => serialize($item->data));
  973. $element['qty'] = array(
  974. '#type' => 'uc_quantity',
  975. '#title' => t('Quantity'),
  976. '#title_display' => 'invisible',
  977. '#default_value' => $item->qty,
  978. '#allow_zero' => TRUE,
  979. );
  980. $element['description'] = array('#markup' => '');
  981. if ($description = uc_product_get_description($item)) {
  982. $element['description']['#markup'] = $description;
  983. }
  984. return $element;
  985. }
  986. /**
  987. * Implements hook_uc_update_cart_item().
  988. */
  989. function uc_product_uc_update_cart_item($nid, $data = array(), $qty, $cid = NULL) {
  990. if (!$nid) return NULL;
  991. $cid = !(is_null($cid) || empty($cid)) ? $cid : uc_cart_get_id();
  992. $efq = new EntityFieldQuery();
  993. $result = $efq->entityCondition('entity_type', 'uc_cart_item')
  994. ->propertyCondition('cart_id', $cid)
  995. ->propertyCondition('nid', $nid)
  996. ->propertyCondition('data', serialize($data))
  997. ->execute();
  998. if (!empty($result['uc_cart_item'])) {
  999. $item_entity = entity_load_single('uc_cart_item', current(array_keys($result['uc_cart_item'])));
  1000. if ($item_entity->qty != $qty) {
  1001. $item_entity->qty = $qty;
  1002. entity_save('uc_cart_item', $item_entity);
  1003. }
  1004. }
  1005. }
  1006. /**
  1007. * Implements hook_uc_add_to_cart_data().
  1008. */
  1009. function uc_product_uc_add_to_cart_data($form_values) {
  1010. if (isset($form_values['nid'])) {
  1011. $node = node_load($form_values['nid']);
  1012. return array(
  1013. 'shippable' => isset($node->shippable) ? $node->shippable : variable_get('uc_product_shippable_' . $node->type, 1),
  1014. 'type' => $node->type
  1015. );
  1016. }
  1017. else {
  1018. return array(
  1019. 'shippable' => variable_get('uc_product_shippable_product', 1),
  1020. 'type' => 'product',
  1021. );
  1022. }
  1023. }
  1024. /**
  1025. * Implements hook_uc_product_class().
  1026. */
  1027. function uc_product_uc_product_class($pcid, $op) {
  1028. switch ($op) {
  1029. case 'insert':
  1030. db_update('node_type')
  1031. ->fields(array(
  1032. 'base' => 'uc_product',
  1033. 'custom' => 0,
  1034. ))
  1035. ->condition('type', $pcid)
  1036. ->execute();
  1037. $result = db_query("SELECT n.vid, n.nid FROM {node} n LEFT JOIN {uc_products} p ON n.vid = p.vid WHERE n.type = :pcid AND p.vid IS NULL", array(':pcid' => $pcid));
  1038. foreach ($result as $node) {
  1039. $node->weight_units = variable_get('uc_weight_unit', 'lb');
  1040. $node->length_units = variable_get('uc_length_unit', 'in');
  1041. $node->pkg_qty = 1;
  1042. $node->default_qty = 1;
  1043. $node->shippable = 1;
  1044. uc_product_insert($node);
  1045. }
  1046. break;
  1047. }
  1048. }
  1049. /**
  1050. * Form builder for uc_catalog_buy_it_now_form().
  1051. *
  1052. * @see uc_product_forms()
  1053. * @see uc_catalog_buy_it_now_form_validate()
  1054. * @see uc_catalog_buy_it_now_form_submit()
  1055. * @ingroup forms
  1056. */
  1057. function uc_catalog_buy_it_now_form($form, &$form_state, $node) {
  1058. $form['nid'] = array('#type' => 'hidden', '#value' => $node->nid);
  1059. $form['actions'] = array('#type' => 'actions');
  1060. $form['actions']['submit'] = array(
  1061. '#type' => 'submit',
  1062. '#value' => t('Add to cart'),
  1063. '#id' => 'edit-submit-' . $node->nid,
  1064. '#attributes' => array(
  1065. 'class' => array('list-add-to-cart'),
  1066. ),
  1067. );
  1068. uc_form_alter($form, $form_state, __FUNCTION__);
  1069. return $form;
  1070. }
  1071. /**
  1072. * Redirects to the product page if attributes need to be selected.
  1073. *
  1074. * @see uc_catalog_buy_it_now_form()
  1075. * @see uc_catalog_buy_it_now_form_submit()
  1076. */
  1077. function uc_catalog_buy_it_now_form_validate($form, &$form_state) {
  1078. if (module_exists('uc_attribute')) {
  1079. $attributes = uc_product_get_attributes($form_state['values']['nid']);
  1080. if (!empty($attributes)) {
  1081. drupal_set_message(t('This product has options that need to be selected before purchase. Please select them in the form below.'), 'error');
  1082. drupal_goto('node/' . $form_state['values']['nid']);
  1083. }
  1084. }
  1085. }
  1086. /**
  1087. * Form submission handler for uc_catalog_buy_it_now_form().
  1088. *
  1089. * @see uc_catalog_buy_it_now_form()
  1090. * @see uc_catalog_buy_it_now_form_validate()
  1091. */
  1092. function uc_catalog_buy_it_now_form_submit($form, &$form_state) {
  1093. $form_state['redirect'] = uc_cart_add_item($form_state['values']['nid'], 1, module_invoke_all('uc_add_to_cart_data', $form_state['values']), NULL, variable_get('uc_cart_add_item_msg', TRUE));
  1094. }
  1095. /**
  1096. * Form to add the $node product to the cart.
  1097. *
  1098. * @param $node
  1099. * A product node.
  1100. *
  1101. * @see uc_product_forms()
  1102. * @see uc_product_add_to_cart_form_submit()
  1103. * @ingroup forms
  1104. */
  1105. function uc_product_add_to_cart_form($form, &$form_state, $node) {
  1106. $form['nid'] = array('#type' => 'value', '#value' => $node->nid);
  1107. if ($node->default_qty > 0 && variable_get('uc_product_add_to_cart_qty', FALSE)) {
  1108. $form['qty'] = array(
  1109. '#type' => 'uc_quantity',
  1110. '#title' => t('Quantity'),
  1111. '#default_value' => $node->default_qty,
  1112. );
  1113. }
  1114. else {
  1115. $form['qty'] = array('#type' => 'hidden', '#value' => $node->default_qty ? $node->default_qty : 1);
  1116. }
  1117. $form['actions'] = array('#type' => 'actions');
  1118. $form['actions']['submit'] = array(
  1119. '#type' => 'submit',
  1120. '#value' => t('Add to cart'),
  1121. '#id' => 'edit-submit-' . $node->nid,
  1122. '#attributes' => array(
  1123. 'class' => array('node-add-to-cart'),
  1124. ),
  1125. );
  1126. // Ajax forms may have no form state when triggered from a cached page, so
  1127. // ensure the variant is available for form alter functions to use.
  1128. if (!empty($form_state['rebuild']) && empty($form_state['storage']['variant'])) {
  1129. $form_state['storage']['variant'] = uc_product_load_variant($form_state['values']['nid'], module_invoke_all('uc_add_to_cart_data', $form_state['values']));
  1130. }
  1131. $form['node'] = array(
  1132. '#type' => 'value',
  1133. '#value' => isset($form_state['storage']['variant']) ? $form_state['storage']['variant'] : $node,
  1134. );
  1135. uc_form_alter($form, $form_state, __FUNCTION__);
  1136. return $form;
  1137. }
  1138. /**
  1139. * Form validation handler for uc_product_add_to_cart_form().
  1140. */
  1141. function uc_product_add_to_cart_form_validate($form, &$form_state) {
  1142. $form_state['storage']['variant'] = uc_product_load_variant($form_state['values']['nid'], module_invoke_all('uc_add_to_cart_data', $form_state['values']));
  1143. }
  1144. /**
  1145. * Form submission handler for uc_product_add_to_cart_form().
  1146. *
  1147. * @see uc_product_add_to_cart_form()
  1148. */
  1149. function uc_product_add_to_cart_form_submit($form, &$form_state) {
  1150. $form_state['redirect'] = uc_cart_add_item($form_state['values']['nid'], $form_state['values']['qty'], module_invoke_all('uc_add_to_cart_data', $form_state['values']), NULL, variable_get('uc_cart_add_item_msg', TRUE));
  1151. }
  1152. /**
  1153. * Returns an array of product node types.
  1154. */
  1155. function uc_product_types() {
  1156. return module_invoke_all('uc_product_types');
  1157. }
  1158. /**
  1159. * Returns an associative array of product node type names keyed by ID.
  1160. */
  1161. function uc_product_type_names() {
  1162. $names = array();
  1163. // Get all the node meta data.
  1164. $node_info = module_invoke_all('node_info');
  1165. // Loop through each product node type.
  1166. foreach (uc_product_types() as $type) {
  1167. $names[$type] = $node_info[$type]['name'];
  1168. }
  1169. return $names;
  1170. }
  1171. /**
  1172. * Determines whether or not a given node or node type is a product.
  1173. *
  1174. * @param $node
  1175. * Either a full node object/array, a node ID, or a node type.
  1176. *
  1177. * @return
  1178. * TRUE or FALSE indicating whether or not a node type is a product node type.
  1179. */
  1180. function uc_product_is_product($node) {
  1181. // Load the node object if we received an integer as an argument.
  1182. if (is_numeric($node)) {
  1183. $node = node_load($node);
  1184. }
  1185. // Determine the node type based on the data type of $node.
  1186. if (is_object($node)) {
  1187. $type = $node->type;
  1188. }
  1189. elseif (is_array($node)) {
  1190. $type = $node['type'];
  1191. }
  1192. elseif (is_string($node)) {
  1193. $type = $node;
  1194. }
  1195. // If no node type was found, go ahead and return FALSE.
  1196. if (!$type) {
  1197. return FALSE;
  1198. }
  1199. // Return TRUE or FALSE depending on whether or not the node type is in the
  1200. // product types array.
  1201. return in_array($type, uc_product_types());
  1202. }
  1203. /**
  1204. * Determines whether or not a given form array is a product node form.
  1205. *
  1206. * @param $form
  1207. * The form array to examine.
  1208. *
  1209. * @return
  1210. * TRUE or FALSE indicating whether or not the form is a product node form.
  1211. */
  1212. function uc_product_is_product_form($form) {
  1213. return !empty($form['#node_edit_form']) && uc_product_is_product($form['#node']);
  1214. }
  1215. /**
  1216. * Gets all models of a product (node).
  1217. *
  1218. * Gathers any modules' models on this node, then add the node's SKU and the
  1219. * optional 'Any' option.
  1220. *
  1221. * @param $nid
  1222. * The node ID of the product.
  1223. * @param $add_blank
  1224. * String to use for the initial blank entry. If not desired, set to NULL
  1225. * or FALSE. Make sure to localize the string first. Defaults to '- Any -'.
  1226. *
  1227. * @return
  1228. * An associative array of model numbers. The key for '- Any -' is the empty
  1229. * string.
  1230. */
  1231. function uc_product_get_models($nid, $add_blank = TRUE) {
  1232. // Get any modules' SKUs on this node.
  1233. $models = module_invoke_all('uc_product_models', $nid);
  1234. // Add the base SKU of the node.
  1235. $models[] = db_query('SELECT model FROM {uc_products} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
  1236. // Now we map the SKUs to the keys, for form handling, etc.
  1237. $models = drupal_map_assoc($models);
  1238. // Sort the SKUs.
  1239. asort($models);
  1240. // And finally, we prepend 'Any' so it's the first option.
  1241. if (!empty($add_blank) || $add_blank === '') {
  1242. if ($add_blank === TRUE) {
  1243. $add_blank = t('- Any -');
  1244. }
  1245. return array('' => $add_blank) + $models;
  1246. }
  1247. return $models;
  1248. }
  1249. /**
  1250. * Gets the cost of a product node.
  1251. *
  1252. * @param $node_id
  1253. * nid of the selected node
  1254. *
  1255. * @return
  1256. * float - cost
  1257. */
  1258. function uc_product_get_cost($node_id) {
  1259. $product = node_load($node_id);
  1260. return $product->cost;
  1261. }
  1262. /**
  1263. * Returns a product node's first attached image.
  1264. *
  1265. * @param $node_id
  1266. * The node's id.
  1267. * @param $style
  1268. * The image style used to format the image. 'uc_product' by default.
  1269. *
  1270. * @return
  1271. * A renderable array of the first product image, linked to the
  1272. * product node, or an empty array if no image is available.
  1273. */
  1274. function uc_product_get_picture($nid, $style = 'uc_product') {
  1275. $product = node_load($nid);
  1276. if (!$product) {
  1277. return array();
  1278. }
  1279. $field_name = variable_get('uc_image_' . $product->type, 'uc_product_image');
  1280. $output = array();
  1281. if ($field_name && isset($product->$field_name)) {
  1282. $elements = field_view_field('node', $product, $field_name, array(
  1283. 'label' => 'hidden',
  1284. 'type' => 'image',
  1285. 'settings' => array(
  1286. 'image_link' => 'content',
  1287. 'image_style' => $style,
  1288. ),
  1289. ));
  1290. // Extract the part of the render array we need.
  1291. $output = isset($elements[0]) ? $elements[0] : array();
  1292. if (isset($elements['#access'])) {
  1293. $output['#access'] = $elements['#access'];
  1294. }
  1295. }
  1296. return $output;
  1297. }
  1298. /**
  1299. * Finds the JavaScript image display module to use on product pages.
  1300. */
  1301. function uc_product_get_image_widget() {
  1302. static $got_widget = FALSE, $image_widget = array();
  1303. // Get the current image widget, if any.
  1304. if (!$got_widget) {
  1305. // Invoke hook to find widgets.
  1306. $image_widgets = uc_store_get_image_widgets();
  1307. // Find widget preference, if any.
  1308. $widget_name = variable_get('uc_product_image_widget', NULL);
  1309. if ($widget_name == 'none') {
  1310. // Don't use any image widgets.
  1311. }
  1312. elseif ($widget_name != NULL) {
  1313. // Widget to use has been set in admin menu.
  1314. $image_widget = $image_widgets[$widget_name];
  1315. }
  1316. else {
  1317. // Widget to use has not been chosen, so default to first element of
  1318. // array, if any.
  1319. $keys = array_keys($image_widgets);
  1320. if ($keys) {
  1321. $image_widget = $image_widgets[$keys[0]];
  1322. variable_set('uc_product_image_widget', $keys[0]);
  1323. }
  1324. }
  1325. $got_widget = TRUE;
  1326. }
  1327. return $image_widget;
  1328. }
  1329. /**
  1330. * Returns HTML for the product description.
  1331. *
  1332. * Modules adding information use hook_uc_product_description() and modules
  1333. * wanting to alter the output before rendering can do so by implementing
  1334. * hook_uc_product_description_alter(). By default, all descriptions supplied
  1335. * by modules via hook_uc_product_description() are concatenated together.
  1336. *
  1337. * NOTE: hook_uc_product_description() supercedes the deprecated
  1338. * hook_cart_item_description().
  1339. *
  1340. * @param $product
  1341. * Product.
  1342. *
  1343. * @return
  1344. * HTML rendered product description.
  1345. */
  1346. function uc_product_get_description($product) {
  1347. // Run through implementations of hook_uc_product_description().
  1348. $description = module_invoke_all('uc_product_description', $product);
  1349. // Now allow alterations via hook_uc_product_description_alter().
  1350. drupal_alter('uc_product_description', $description, $product);
  1351. return drupal_render($description);
  1352. }
  1353. /**
  1354. * Load a product class or all classes.
  1355. */
  1356. function uc_product_class_load($class_id = NULL, $reset = FALSE) {
  1357. static $classes;
  1358. if (!isset($classes) || $reset) {
  1359. // Load classes from database.
  1360. $classes = array();
  1361. $result = db_query("SELECT * FROM {uc_product_classes}");
  1362. while ($class = $result->fetchObject()) {
  1363. $class->locked = FALSE;
  1364. $classes[$class->pcid] = $class;
  1365. }
  1366. // Merge any module-defined classes.
  1367. foreach (module_invoke_all('uc_product_default_classes') as $pcid => $class) {
  1368. // The default class can be overridden by a module if needed,
  1369. // but is not treated as a real class elsewhere.
  1370. if ($pcid == 'product') {
  1371. continue;
  1372. }
  1373. $class += array(
  1374. 'pcid' => $pcid,
  1375. 'name' => $pcid,
  1376. 'description' => '',
  1377. );
  1378. if (isset($classes[$pcid])) {
  1379. // Merge defaults with the existing database info.
  1380. $classes[$pcid] = (object) array_merge($class, (array) $classes[$pcid]);
  1381. }
  1382. else {
  1383. // Ensure the class info is saved in the database.
  1384. drupal_write_record('uc_product_classes', $class);
  1385. $classes[$pcid] = (object) $class;
  1386. }
  1387. // Module-defined classes cannot be deleted.
  1388. $classes[$pcid]->locked = TRUE;
  1389. }
  1390. }
  1391. return is_null($class_id) ? $classes : $classes[$class_id];
  1392. }
  1393. /**
  1394. * Returns data for a product feature, given a feature ID and array key.
  1395. *
  1396. * @param $fid
  1397. * The string ID of the product feature you want to get data from.
  1398. * @param $key
  1399. * The key in the product feature array you want: title, callback, delete,
  1400. * settings.
  1401. *
  1402. * @return
  1403. * The value of the key you specify.
  1404. */
  1405. function uc_product_feature_data($fid, $key) {
  1406. static $features;
  1407. if (empty($features)) {
  1408. foreach (module_invoke_all('uc_product_feature') as $feature) {
  1409. $features[$feature['id']] = $feature;
  1410. }
  1411. }
  1412. return $features[$fid][$key];
  1413. }
  1414. /**
  1415. * Returns a form array with some default hidden values and submit button.
  1416. *
  1417. * @param $form
  1418. * The form array you wish to add the elements to.
  1419. *
  1420. * @return
  1421. * The form array with elements added for the nid, pfid, submit button, and
  1422. * cancel link.
  1423. *
  1424. * @ingroup forms
  1425. */
  1426. function uc_product_feature_form($form, &$form_state, $node, $feature) {
  1427. $form['nid'] = array(
  1428. '#type' => 'hidden',
  1429. '#value' => $node->nid,
  1430. );
  1431. // Forms to add a feature are only given an empty array.
  1432. if (!empty($feature)) {
  1433. $form['pfid'] = array(
  1434. '#type' => 'hidden',
  1435. '#value' => $feature['pfid'],
  1436. );
  1437. }
  1438. $form['actions'] = array('#type' => 'actions');
  1439. $form['actions']['submit'] = array(
  1440. '#type' => 'submit',
  1441. '#value' => t('Save feature'),
  1442. );
  1443. $form['actions']['cancel'] = array(
  1444. '#markup' => l(t('Cancel'), 'node/' . $node->nid . '/edit/features'),
  1445. );
  1446. return $form;
  1447. }
  1448. /**
  1449. * Saves a product feature to a product node.
  1450. *
  1451. * @param $data
  1452. * An array consisting of the following keys:
  1453. * - pfid: (optional) When editing an existing product feature, the numeric
  1454. * ID of the feature.
  1455. * - nid: The numeric ID of the product node.
  1456. * - fid: The string ID of the feature type.
  1457. * - description: The string describing the feature for the overview table.
  1458. */
  1459. function uc_product_feature_save(&$data) {
  1460. if (empty($data['nid']) && arg(0) == 'node' && intval(arg(1)) > 0) {
  1461. $data['nid'] = intval(arg(1));
  1462. }
  1463. if (empty($data['pfid'])) {
  1464. if (arg(0) == 'node' && arg(3) == 'features' && intval(arg(5)) > 0) {
  1465. $data['pfid'] = intval(arg(5));
  1466. }
  1467. }
  1468. // First attempt to update an existing row.
  1469. $result = drupal_write_record('uc_product_features', $data, !empty($data['pfid']) ? 'pfid' : array());
  1470. // Otherwise insert this feature as a new row.
  1471. if ($result == SAVED_NEW) {
  1472. drupal_set_message(t('The product feature has been added.'));
  1473. }
  1474. elseif ($result == SAVED_UPDATED) {
  1475. drupal_set_message(t('The product feature has been updated.'));
  1476. }
  1477. else {
  1478. drupal_set_message(t('The product feature was unable to be saved.'));
  1479. }
  1480. return 'node/' . $data['nid'] . '/edit/features';
  1481. }
  1482. /**
  1483. * Loads all product feature for a node.
  1484. *
  1485. * @param $nid
  1486. * The product node ID.
  1487. *
  1488. * @return
  1489. * The array of all product features object.
  1490. */
  1491. function uc_product_feature_load_multiple($nid) {
  1492. $features = db_query("SELECT * FROM {uc_product_features} WHERE nid = :nid ORDER BY pfid ASC", array(':nid' => $nid))->fetchAllAssoc('pfid');
  1493. return $features;
  1494. }
  1495. /**
  1496. * Loads a product feature object.
  1497. *
  1498. * @todo: should return an object instead of array.
  1499. *
  1500. * @param $pfid
  1501. * The product feature ID.
  1502. * @param $fid
  1503. * Optional. Specify a specific feature id.
  1504. *
  1505. * @return
  1506. * The product feature array.
  1507. */
  1508. function uc_product_feature_load($pfid) {
  1509. $feature = db_query("SELECT * FROM {uc_product_features} WHERE pfid = :pfid", array(':pfid' => $pfid))->fetchAssoc();
  1510. return $feature;
  1511. }
  1512. /**
  1513. * Deletes a product feature object.
  1514. *
  1515. * @param $pfid
  1516. * The product feature ID.
  1517. *
  1518. * @return
  1519. * The product feature object.
  1520. */
  1521. function uc_product_feature_delete($pfid) {
  1522. $feature = uc_product_feature_load($pfid);
  1523. // Call the delete function for this product feature if it exists.
  1524. $func = uc_product_feature_data($feature['fid'], 'delete');
  1525. if (function_exists($func)) {
  1526. $func($pfid);
  1527. }
  1528. db_delete('uc_product_features')
  1529. ->condition('pfid', $pfid)
  1530. ->execute();
  1531. return SAVED_DELETED;
  1532. }
  1533. /**
  1534. * Creates a file field with an image field widget, and attach it to products.
  1535. *
  1536. * This field is used by default on the product page, as well as on the cart
  1537. * and catalog pages to represent the products they list. Instances are added
  1538. * to new product classes, and other node types that claim product-ness should
  1539. * call this function for themselves.
  1540. *
  1541. * @param $type
  1542. * The content type to which the image field is to be attached. This may be a
  1543. * a single type as a string, or an array of types. If NULL, all product
  1544. * types get an instance of the field.
  1545. */
  1546. function uc_product_add_default_image_field($type = NULL) {
  1547. $field = field_info_field('uc_product_image');
  1548. // Set up field if it doesn't exist.
  1549. if (!$field) {
  1550. $field = array(
  1551. 'field_name' => 'uc_product_image',
  1552. 'type' => 'image',
  1553. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1554. );
  1555. field_create_field($field);
  1556. // Initialize this because field_info_field() would have set it.
  1557. $field['bundles'] = array();
  1558. }
  1559. $label = t('Image');
  1560. $new_instance = array(
  1561. 'entity_type' => 'node',
  1562. 'label' => $label,
  1563. 'weight' => -2,
  1564. 'widget' => array(
  1565. 'type' => 'image_image',
  1566. ),
  1567. 'display' => array(
  1568. 'full' => array(
  1569. 'label' => 'hidden',
  1570. 'type' => 'uc_product_image',
  1571. ),
  1572. 'teaser' => array(
  1573. 'label' => 'hidden',
  1574. 'type' => 'uc_product_image',
  1575. ),
  1576. ),
  1577. );
  1578. if ($type) {
  1579. // Accept single or multiple types as input.
  1580. $types = (array) $type;
  1581. }
  1582. else {
  1583. $types = uc_product_types();
  1584. }
  1585. foreach ($types as $type) {
  1586. $new_instance['bundle'] = $type;
  1587. $field_name = variable_get('uc_image_' . $type, '');
  1588. if (empty($field_name)) {
  1589. $field_name = 'uc_product_image';
  1590. }
  1591. $new_instance['field_name'] = $field_name;
  1592. $instance = field_info_instance('node', $field_name, $type);
  1593. // Only add the instance if it doesn't exist. Don't overwrite any changes.
  1594. if (!$instance) {
  1595. field_create_instance($new_instance);
  1596. variable_set('uc_image_' . $type, $field_name);
  1597. }
  1598. }
  1599. }
  1600. /**
  1601. * Implements hook_image_default_styles().
  1602. */
  1603. function uc_product_image_default_styles() {
  1604. $styles = array();
  1605. $styles['uc_product'] = array(
  1606. 'effects' => array(
  1607. array(
  1608. 'name' => 'image_scale',
  1609. 'data' => array(
  1610. 'width' => '250',
  1611. 'height' => '250',
  1612. 'upscale' => 0,
  1613. ),
  1614. 'weight' => '0',
  1615. ),
  1616. ),
  1617. );
  1618. $styles['uc_product_full'] = array(
  1619. 'effects' => array(),
  1620. );
  1621. $styles['uc_product_list'] = array(
  1622. 'effects' => array(
  1623. array(
  1624. 'name' => 'image_scale',
  1625. 'data' => array(
  1626. 'width' => '100',
  1627. 'height' => '100',
  1628. 'upscale' => 0,
  1629. ),
  1630. 'weight' => '0',
  1631. ),
  1632. ),
  1633. );
  1634. $styles['uc_thumbnail'] = array(
  1635. 'effects' => array(
  1636. array(
  1637. 'name' => 'image_scale',
  1638. 'data' => array(
  1639. 'width' => '35',
  1640. 'height' => '35',
  1641. 'upscale' => 0,
  1642. ),
  1643. 'weight' => '0',
  1644. ),
  1645. ),
  1646. );
  1647. return $styles;
  1648. }
  1649. /**
  1650. * Implements hook_views_api().
  1651. */
  1652. function uc_product_views_api() {
  1653. return array(
  1654. 'api' => '2.0',
  1655. 'path' => drupal_get_path('module', 'uc_product') . '/views',
  1656. );
  1657. }
  1658. /**
  1659. * Implements hook_features_api().
  1660. */
  1661. function uc_product_features_api() {
  1662. return array(
  1663. 'uc_product_classes' => array(
  1664. 'name' => t('Ubercart product classes'),
  1665. 'feature_source' => TRUE,
  1666. 'default_hook' => 'uc_product_default_classes',
  1667. 'file' => drupal_get_path('module', 'uc_product') . '/uc_product.features.inc',
  1668. ),
  1669. );
  1670. }
  1671. /**
  1672. * Implements hook_action_info().
  1673. */
  1674. function uc_product_action_info() {
  1675. return array(
  1676. 'uc_product_action_add_to_cart' => array(
  1677. 'label' => t('Add to cart'),
  1678. 'type' => 'node',
  1679. 'configurable' => FALSE,
  1680. 'triggers' => array('any'),
  1681. ),
  1682. );
  1683. }
  1684. /**
  1685. * Action implementation: adds a product to the cart.
  1686. */
  1687. function uc_product_action_add_to_cart($product, $context = array()) {
  1688. uc_cart_add_item($product->nid, 1, NULL, NULL, TRUE, FALSE, TRUE);
  1689. }