uc_product.module 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  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
  553. * An variant of the product, altered based on the provided data.
  554. */
  555. function _uc_product_get_variant($node, $data = FALSE) {
  556. if (!empty($node->variant)) {
  557. throw new Exception(t('Cannot create a variant of a variant.'));
  558. }
  559. $node = clone $node;
  560. if (!empty($data)) {
  561. $node->data = $data;
  562. }
  563. // Ensure that $node->data is an array (user module leaves it serialized).
  564. if (isset($node->data) && !is_array($node->data)) {
  565. $node->data = unserialize($node->data);
  566. }
  567. drupal_alter('uc_product', $node);
  568. $node->variant = TRUE;
  569. if (!isset($node->data['module'])) {
  570. $node->data['module'] = 'uc_product';
  571. }
  572. return $node;
  573. }
  574. /**
  575. * Loads a specific altered variant of a product node.
  576. *
  577. * The (possibly cached) base product remains unaltered.
  578. *
  579. * @param $nid
  580. * The nid of the product to load.
  581. * @param $data
  582. * Optional data to add to the product before invoking the alter hooks.
  583. *
  584. * @return
  585. * A variant of the product, altered based on the provided data, or FALSE
  586. * if the node is not found.
  587. */
  588. function uc_product_load_variant($nid, $data = FALSE) {
  589. if ($node = node_load($nid)) {
  590. return _uc_product_get_variant($node, $data);
  591. }
  592. else {
  593. return FALSE;
  594. }
  595. }
  596. /**
  597. * Implements hook_uc_product_alter().
  598. *
  599. * Invokes rules event to allow product modifications.
  600. */
  601. function uc_product_uc_product_alter(&$node) {
  602. if (module_exists('rules')) {
  603. rules_invoke_event('uc_product_load', $node);
  604. }
  605. }
  606. /**
  607. * Implements hook_delete().
  608. */
  609. function uc_product_delete(&$node) {
  610. $features = uc_product_feature_load_multiple($node->nid);
  611. foreach ($features as $feature) {
  612. uc_product_feature_delete($feature->pfid);
  613. }
  614. db_delete('uc_products')
  615. ->condition('nid', $node->nid)
  616. ->execute();
  617. }
  618. /**
  619. * Dynamically replaces parts of a product view based on form input.
  620. *
  621. * If a module adds an input field to the add-to-cart form which affects some
  622. * aspect of a product (e.g. display price or weight), it should attach an
  623. * #ajax callback to that form element, and use this function in the callback
  624. * to build updated content for the affected fields.
  625. *
  626. * @param $form_state
  627. * The current form state. This must contain a 'variant' entry in the
  628. * 'storage' array which represents the product as configured by user input
  629. * data. In most cases, this is provided automatically by
  630. * uc_product_add_to_cart_form_validate().
  631. * @param $keys
  632. * An array of keys in the built product content which should be replaced
  633. * (e.g. 'display_price').
  634. *
  635. * @return
  636. * An array of Ajax commands.
  637. */
  638. function uc_product_view_ajax_commands($form_state, $keys) {
  639. $commands = array();
  640. if (variable_get('uc_product_update_node_view', FALSE) && !empty($form_state['storage']['variant'])) {
  641. $node_div = '.uc-product-' . $form_state['storage']['variant']->nid;
  642. $build = node_view($form_state['storage']['variant']);
  643. foreach ($keys as $key) {
  644. if (isset($build[$key])) {
  645. $id = $node_div . '.' . str_replace('_', '-', $key);
  646. $commands[] = ajax_command_replace($id, drupal_render($build[$key]));
  647. }
  648. }
  649. }
  650. return $commands;
  651. }
  652. /**
  653. * Implements hook_view().
  654. */
  655. function uc_product_view($node, $view_mode) {
  656. // Give modules a chance to alter this product. If it is a variant, this will
  657. // have been done already by uc_product_load_variant(), so we check a flag to
  658. // be sure not to alter twice -- cf. entity_prepare_view().
  659. $variant = empty($node->variant) ? _uc_product_get_variant($node) : $node;
  660. // Skip the add to cart form in comment reply forms.
  661. if (arg(0) != 'comment' && arg(1) != 'reply') {
  662. // Build the 'add to cart' form, and use the updated variant based on data
  663. // provided by the form (e.g. attribute default options).
  664. if (module_exists('uc_cart') && $variant->nid && empty($variant->data['display_only'])) {
  665. $add_to_cart_form = drupal_get_form('uc_product_add_to_cart_form_' . $variant->nid, $variant);
  666. if (variable_get('uc_product_update_node_view', FALSE)) {
  667. $variant = $add_to_cart_form['node']['#value'];
  668. }
  669. }
  670. }
  671. $node->content['display_price'] = array(
  672. '#theme' => 'uc_product_price',
  673. '#value' => isset($variant->price) ? $variant->price : $variant->sell_price,
  674. '#suffixes' => array(),
  675. '#attributes' => array(
  676. 'class' => array(
  677. 'display-price',
  678. 'uc-product-' . $node->nid,
  679. ),
  680. ),
  681. );
  682. $node->content['model'] = array(
  683. '#theme' => 'uc_product_model',
  684. '#model' => $variant->model,
  685. '#view_mode' => $view_mode,
  686. );
  687. $node->content['list_price'] = array(
  688. '#theme' => 'uc_product_price',
  689. '#title' => t('List price:'),
  690. '#value' => $variant->list_price,
  691. '#attributes' => array(
  692. 'class' => array(
  693. 'list-price',
  694. ),
  695. ),
  696. );
  697. $node->content['cost'] = array(
  698. '#theme' => 'uc_product_price',
  699. '#title' => t('Cost:'),
  700. '#value' => $variant->cost,
  701. '#attributes' => array(
  702. 'class' => array(
  703. 'cost',
  704. 'uc-product-' . $node->nid,
  705. ),
  706. ),
  707. '#access' => user_access('administer products'),
  708. );
  709. $node->content['sell_price'] = array(
  710. '#theme' => 'uc_product_price',
  711. '#title' => t('Price:'),
  712. '#value' => $variant->sell_price,
  713. '#attributes' => array(
  714. 'class' => array(
  715. 'sell-price',
  716. ),
  717. ),
  718. );
  719. $node->content['weight'] = array(
  720. '#theme' => 'uc_product_weight',
  721. '#amount' => $variant->weight,
  722. '#units' => $variant->weight_units,
  723. '#view_mode' => $view_mode,
  724. '#attributes' => array(
  725. 'class' => array(
  726. 'uc-product-' . $node->nid,
  727. ),
  728. ),
  729. );
  730. $node->content['dimensions'] = array(
  731. '#theme' => 'uc_product_dimensions',
  732. '#length' => $variant->length,
  733. '#width' => $variant->width,
  734. '#height' => $variant->height,
  735. '#units' => $variant->length_units,
  736. '#view_mode' => $view_mode,
  737. );
  738. if (isset($add_to_cart_form)) {
  739. $node->content['add_to_cart'] = array(
  740. '#theme' => 'uc_product_add_to_cart',
  741. '#view_mode' => $view_mode,
  742. '#form' => $add_to_cart_form,
  743. );
  744. }
  745. $node->content['#node'] = $variant;
  746. return $node;
  747. }
  748. /**
  749. * Implements hook_preprocess_node().
  750. *
  751. * Product classes default to using node--product.tpl.php if they don't have
  752. * their own template.
  753. *
  754. * @see theme()
  755. * @see MODULE_preprocess_HOOK()
  756. */
  757. function uc_product_preprocess_node(&$variables) {
  758. if (uc_product_is_product($variables['type'])) {
  759. array_unshift($variables['theme_hook_suggestions'], 'node__product');
  760. }
  761. }
  762. /**
  763. * Implements hook_preprocess_html().
  764. *
  765. * Adds a body class to product node pages.
  766. *
  767. * @see html.tpl.php
  768. */
  769. function uc_product_preprocess_html(&$variables) {
  770. if ($node = menu_get_object()) {
  771. if (uc_product_is_product($node)) {
  772. $variables['classes_array'][] = 'uc-product-node';
  773. }
  774. }
  775. }
  776. /**
  777. * Implements hook_form_alter().
  778. *
  779. * @see uc_product_save_continue_submit()
  780. */
  781. function uc_product_form_alter(&$form, &$form_state, $form_id) {
  782. // Add a button to product node forms to continue editing after saving.
  783. if (uc_product_is_product_form($form)) {
  784. $form['actions']['save_continue'] = array(
  785. '#type' => 'submit',
  786. '#value' => t('Save and continue'),
  787. '#weight' => 7,
  788. '#submit' => array('node_form_submit', 'uc_product_save_continue_submit'),
  789. );
  790. }
  791. }
  792. /**
  793. * After the node is saved, redirects to the node edit page.
  794. *
  795. * Some modules add local tasks to product edit forms, but only after it has an
  796. * nid. Redirecting facilitates the common workflow of continuing to those
  797. * tasks.
  798. */
  799. function uc_product_save_continue_submit($form, &$form_state) {
  800. $form_state['redirect'] = 'node/' . $form_state['nid'] . '/edit';
  801. }
  802. /**
  803. * Implements hook_form_FORM_ID_alter().
  804. *
  805. * Adds a default image field setting to product content types.
  806. */
  807. function uc_product_form_node_type_form_alter(&$form, &$form_state) {
  808. $type = $form['#node_type'];
  809. if (!uc_product_is_product($type->type)) {
  810. return;
  811. }
  812. $form['uc_product'] = array(
  813. '#type' => 'fieldset',
  814. '#title' => t('Ubercart product settings'),
  815. '#collapsible' => TRUE,
  816. '#collapsed' => TRUE,
  817. '#group' => 'additional_settings',
  818. '#attached' => array(
  819. 'js' => array(
  820. 'vertical-tabs' => drupal_get_path('module', 'uc_product') . '/uc_product.js',
  821. ),
  822. ),
  823. );
  824. // Shippable.
  825. $form['uc_product']['uc_product_shippable'] = array(
  826. '#type' => 'checkbox',
  827. '#title' => t('Product and its derivatives are shippable.'),
  828. '#default_value' => variable_get('uc_product_shippable_' . $type->type, 1),
  829. '#description' => t('This setting can still be overridden on the node form.'),
  830. '#return_value' => 1,
  831. '#weight' => -5,
  832. );
  833. // Image field.
  834. $instances = field_info_instances('node', $type->type);
  835. $options = array('' => t('None'));
  836. foreach ($instances as $field_name => $instance) {
  837. if (strpos($instance['widget']['type'], 'image') !== FALSE) {
  838. $options[$field_name] = $instance['label'];
  839. }
  840. }
  841. $form['uc_product']['uc_image'] = array(
  842. '#type' => 'select',
  843. '#title' => t('Product image field'),
  844. '#default_value' => variable_get('uc_image_' . $type->type, NULL),
  845. '#options' => $options,
  846. '#description' => t('The selected field will be used on Ubercart pages to represent the products of this content type.'),
  847. '#weight' => -4,
  848. );
  849. }
  850. /**
  851. * Implements hook_field_extra_fields().
  852. *
  853. * Adds the "Product information".
  854. */
  855. function uc_product_field_extra_fields() {
  856. $extra = array();
  857. foreach (uc_product_types() as $type) {
  858. $extra['node'][$type] = array(
  859. 'display' => array(
  860. 'display_price' => array(
  861. 'label' => t('Display price'),
  862. 'description' => t('High-visibility sell price.'),
  863. 'weight' => -1,
  864. ),
  865. 'model' => array(
  866. 'label' => t('SKU'),
  867. 'description' => t('Product SKU/model.'),
  868. 'weight' => 0,
  869. ),
  870. 'list_price' => array(
  871. 'label' => t('List price'),
  872. 'description' => t('The listed MSRP.'),
  873. 'weight' => 2,
  874. ),
  875. 'cost' => array(
  876. 'label' => t('Cost'),
  877. 'description' => t("Your store's cost."),
  878. 'weight' => 3,
  879. ),
  880. 'sell_price' => array(
  881. 'label' => t('Sell price'),
  882. 'description' => t('Customer purchase price.'),
  883. 'weight' => 4,
  884. ),
  885. 'weight' => array(
  886. 'label' => t('Weight'),
  887. 'description' => t('Physical weight (lbs, kg, etc.).'),
  888. 'weight' => 5,
  889. ),
  890. 'dimensions' => array(
  891. 'label' => t('Dimensions'),
  892. 'description' => t('Physical dimensions of the packaged product.'),
  893. 'weight' => 6,
  894. ),
  895. 'add_to_cart' => array(
  896. 'label' => t('Add to cart form'),
  897. 'description' => t('Add to cart form'),
  898. 'weight' => 10,
  899. ),
  900. ),
  901. );
  902. }
  903. return $extra;
  904. }
  905. /**
  906. * Implements hook_field_delete_instance().
  907. *
  908. * Resets a content type's default image field setting when that field instance
  909. * is removed.
  910. */
  911. function uc_product_field_delete_instance($instance) {
  912. if ($instance['entity_type'] == 'node' && $instance['field_name'] == variable_get('uc_image_' . $instance['bundle'], NULL)) {
  913. variable_del('uc_image_' . $instance['bundle']);
  914. }
  915. }
  916. /**
  917. * Implements hook_uc_product_types().
  918. */
  919. function uc_product_uc_product_types() {
  920. return array_keys(uc_product_node_info());
  921. }
  922. /**
  923. * Implements hook_uc_store_status().
  924. *
  925. * Displays the status of the product image handlers.
  926. *
  927. * @see uc_product_image_defaults()
  928. */
  929. function uc_product_uc_store_status() {
  930. module_load_include('inc', 'content', 'includes/content.crud');
  931. // Check for filefields on products.
  932. if ($field = variable_get('uc_image_product', '')) {
  933. $instances = field_info_instances('node', 'product');
  934. $field_check = (bool) count($instances[$field]);
  935. }
  936. else {
  937. $field_check = FALSE;
  938. }
  939. if ($field_check) {
  940. $status = 'ok';
  941. $description = t('Product image support has been automatically configured by Ubercart.');
  942. }
  943. else {
  944. $status = 'warning';
  945. $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')));
  946. $items[] = t('The Image file field has not been created for products.');
  947. $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.)');
  948. }
  949. return array(array('status' => $status, 'title' => t('Images'), 'desc' => $description));
  950. }
  951. /**
  952. * Implements hook_uc_cart_display().
  953. */
  954. function uc_product_uc_cart_display($item) {
  955. $node = node_load($item->nid);
  956. $element = array();
  957. $element['nid'] = array('#type' => 'value', '#value' => $node->nid);
  958. $element['module'] = array('#type' => 'value', '#value' => 'uc_product');
  959. $element['remove'] = array('#type' => 'submit', '#value' => t('Remove'));
  960. if (node_access('view', $node) && $uri = entity_uri('node', $node)) {
  961. $element['title'] = array(
  962. '#markup' => l($item->title, $uri['path'], $uri['options']),
  963. );
  964. }
  965. else {
  966. $element['title'] = array('#markup' => $item->title);
  967. }
  968. $element['#total'] = $item->price * $item->qty;
  969. $element['#suffixes'] = array();
  970. $element['data'] = array('#type' => 'hidden', '#value' => serialize($item->data));
  971. $element['qty'] = array(
  972. '#type' => 'uc_quantity',
  973. '#title' => t('Quantity'),
  974. '#title_display' => 'invisible',
  975. '#default_value' => $item->qty,
  976. '#allow_zero' => TRUE,
  977. );
  978. $element['description'] = array('#markup' => '');
  979. if ($description = uc_product_get_description($item)) {
  980. $element['description']['#markup'] = $description;
  981. }
  982. return $element;
  983. }
  984. /**
  985. * Implements hook_uc_update_cart_item().
  986. */
  987. function uc_product_uc_update_cart_item($nid, $data = array(), $qty, $cid = NULL) {
  988. if (!$nid) return NULL;
  989. $cid = !(is_null($cid) || empty($cid)) ? $cid : uc_cart_get_id();
  990. $efq = new EntityFieldQuery();
  991. $result = $efq->entityCondition('entity_type', 'uc_cart_item')
  992. ->propertyCondition('cart_id', $cid)
  993. ->propertyCondition('nid', $nid)
  994. ->propertyCondition('data', serialize($data))
  995. ->execute();
  996. if (!empty($result['uc_cart_item'])) {
  997. $item_entity = entity_load_single('uc_cart_item', current(array_keys($result['uc_cart_item'])));
  998. if ($item_entity->qty != $qty) {
  999. $item_entity->qty = $qty;
  1000. entity_save('uc_cart_item', $item_entity);
  1001. }
  1002. }
  1003. }
  1004. /**
  1005. * Implements hook_uc_add_to_cart_data().
  1006. */
  1007. function uc_product_uc_add_to_cart_data($form_values) {
  1008. if (isset($form_values['nid'])) {
  1009. $node = node_load($form_values['nid']);
  1010. return array(
  1011. 'shippable' => isset($node->shippable) ? $node->shippable : variable_get('uc_product_shippable_' . $node->type, 1),
  1012. 'type' => $node->type
  1013. );
  1014. }
  1015. else {
  1016. return array(
  1017. 'shippable' => variable_get('uc_product_shippable_product', 1),
  1018. 'type' => 'product',
  1019. );
  1020. }
  1021. }
  1022. /**
  1023. * Implements hook_uc_product_class().
  1024. */
  1025. function uc_product_uc_product_class($pcid, $op) {
  1026. switch ($op) {
  1027. case 'insert':
  1028. db_update('node_type')
  1029. ->fields(array(
  1030. 'base' => 'uc_product',
  1031. 'custom' => 0,
  1032. ))
  1033. ->condition('type', $pcid)
  1034. ->execute();
  1035. $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));
  1036. foreach ($result as $node) {
  1037. $node->weight_units = variable_get('uc_weight_unit', 'lb');
  1038. $node->length_units = variable_get('uc_length_unit', 'in');
  1039. $node->pkg_qty = 1;
  1040. $node->default_qty = 1;
  1041. $node->shippable = 1;
  1042. uc_product_insert($node);
  1043. }
  1044. break;
  1045. }
  1046. }
  1047. /**
  1048. * Form builder for uc_catalog_buy_it_now_form().
  1049. *
  1050. * @see uc_product_forms()
  1051. * @see uc_catalog_buy_it_now_form_validate()
  1052. * @see uc_catalog_buy_it_now_form_submit()
  1053. * @ingroup forms
  1054. */
  1055. function uc_catalog_buy_it_now_form($form, &$form_state, $node) {
  1056. $form['nid'] = array('#type' => 'hidden', '#value' => $node->nid);
  1057. $form['actions'] = array('#type' => 'actions');
  1058. $form['actions']['submit'] = array(
  1059. '#type' => 'submit',
  1060. '#value' => t('Add to cart'),
  1061. '#id' => 'edit-submit-' . $node->nid,
  1062. '#attributes' => array(
  1063. 'class' => array('list-add-to-cart'),
  1064. ),
  1065. );
  1066. uc_form_alter($form, $form_state, __FUNCTION__);
  1067. return $form;
  1068. }
  1069. /**
  1070. * Redirects to the product page if attributes need to be selected.
  1071. *
  1072. * @see uc_catalog_buy_it_now_form()
  1073. * @see uc_catalog_buy_it_now_form_submit()
  1074. */
  1075. function uc_catalog_buy_it_now_form_validate($form, &$form_state) {
  1076. if (module_exists('uc_attribute')) {
  1077. $attributes = uc_product_get_attributes($form_state['values']['nid']);
  1078. if (!empty($attributes)) {
  1079. drupal_set_message(t('This product has options that need to be selected before purchase. Please select them in the form below.'), 'error');
  1080. drupal_goto('node/' . $form_state['values']['nid']);
  1081. }
  1082. }
  1083. }
  1084. /**
  1085. * Form submission handler for uc_catalog_buy_it_now_form().
  1086. *
  1087. * @see uc_catalog_buy_it_now_form()
  1088. * @see uc_catalog_buy_it_now_form_validate()
  1089. */
  1090. function uc_catalog_buy_it_now_form_submit($form, &$form_state) {
  1091. $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));
  1092. }
  1093. /**
  1094. * Form to add the $node product to the cart.
  1095. *
  1096. * @param $node
  1097. * A product node.
  1098. *
  1099. * @see uc_product_forms()
  1100. * @see uc_product_add_to_cart_form_submit()
  1101. * @ingroup forms
  1102. */
  1103. function uc_product_add_to_cart_form($form, &$form_state, $node) {
  1104. $form['nid'] = array('#type' => 'value', '#value' => $node->nid);
  1105. if ($node->default_qty > 0 && variable_get('uc_product_add_to_cart_qty', FALSE)) {
  1106. $form['qty'] = array(
  1107. '#type' => 'uc_quantity',
  1108. '#title' => t('Quantity'),
  1109. '#default_value' => $node->default_qty,
  1110. );
  1111. }
  1112. else {
  1113. $form['qty'] = array('#type' => 'hidden', '#value' => $node->default_qty ? $node->default_qty : 1);
  1114. }
  1115. $form['actions'] = array('#type' => 'actions');
  1116. $form['actions']['submit'] = array(
  1117. '#type' => 'submit',
  1118. '#value' => t('Add to cart'),
  1119. '#id' => 'edit-submit-' . $node->nid,
  1120. '#attributes' => array(
  1121. 'class' => array('node-add-to-cart'),
  1122. ),
  1123. );
  1124. $form['node'] = array(
  1125. '#type' => 'value',
  1126. '#value' => isset($form_state['storage']['variant']) ? $form_state['storage']['variant'] : $node,
  1127. );
  1128. uc_form_alter($form, $form_state, __FUNCTION__);
  1129. return $form;
  1130. }
  1131. /**
  1132. * Form validation handler for uc_product_add_to_cart_form().
  1133. */
  1134. function uc_product_add_to_cart_form_validate($form, &$form_state) {
  1135. $form_state['storage']['variant'] = uc_product_load_variant($form_state['values']['nid'], module_invoke_all('uc_add_to_cart_data', $form_state['values']));
  1136. }
  1137. /**
  1138. * Form submission handler for uc_product_add_to_cart_form().
  1139. *
  1140. * @see uc_product_add_to_cart_form()
  1141. */
  1142. function uc_product_add_to_cart_form_submit($form, &$form_state) {
  1143. $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));
  1144. }
  1145. /**
  1146. * Returns an array of product node types.
  1147. */
  1148. function uc_product_types() {
  1149. return module_invoke_all('uc_product_types');
  1150. }
  1151. /**
  1152. * Returns an associative array of product node type names keyed by ID.
  1153. */
  1154. function uc_product_type_names() {
  1155. $names = array();
  1156. // Get all the node meta data.
  1157. $node_info = module_invoke_all('node_info');
  1158. // Loop through each product node type.
  1159. foreach (uc_product_types() as $type) {
  1160. $names[$type] = $node_info[$type]['name'];
  1161. }
  1162. return $names;
  1163. }
  1164. /**
  1165. * Determines whether or not a given node or node type is a product.
  1166. *
  1167. * @param $node
  1168. * Either a full node object/array, a node ID, or a node type.
  1169. *
  1170. * @return
  1171. * TRUE or FALSE indicating whether or not a node type is a product node type.
  1172. */
  1173. function uc_product_is_product($node) {
  1174. // Load the node object if we received an integer as an argument.
  1175. if (is_numeric($node)) {
  1176. $node = node_load($node);
  1177. }
  1178. // Determine the node type based on the data type of $node.
  1179. if (is_object($node)) {
  1180. $type = $node->type;
  1181. }
  1182. elseif (is_array($node)) {
  1183. $type = $node['type'];
  1184. }
  1185. elseif (is_string($node)) {
  1186. $type = $node;
  1187. }
  1188. // If no node type was found, go ahead and return FALSE.
  1189. if (!$type) {
  1190. return FALSE;
  1191. }
  1192. // Return TRUE or FALSE depending on whether or not the node type is in the
  1193. // product types array.
  1194. return in_array($type, uc_product_types());
  1195. }
  1196. /**
  1197. * Determines whether or not a given form array is a product node form.
  1198. *
  1199. * @param $form
  1200. * The form array to examine.
  1201. *
  1202. * @return
  1203. * TRUE or FALSE indicating whether or not the form is a product node form.
  1204. */
  1205. function uc_product_is_product_form($form) {
  1206. return !empty($form['#node_edit_form']) && uc_product_is_product($form['#node']);
  1207. }
  1208. /**
  1209. * Gets all models of a product (node).
  1210. *
  1211. * Gathers any modules' models on this node, then add the node's SKU and the
  1212. * optional 'Any' option.
  1213. *
  1214. * @param $nid
  1215. * The node ID of the product.
  1216. * @param $add_blank
  1217. * String to use for the initial blank entry. If not desired, set to NULL
  1218. * or FALSE. Make sure to localize the string first. Defaults to '- Any -'.
  1219. *
  1220. * @return
  1221. * An associative array of model numbers. The key for '- Any -' is the empty
  1222. * string.
  1223. */
  1224. function uc_product_get_models($nid, $add_blank = TRUE) {
  1225. // Get any modules' SKUs on this node.
  1226. $models = module_invoke_all('uc_product_models', $nid);
  1227. // Add the base SKU of the node.
  1228. $models[] = db_query('SELECT model FROM {uc_products} WHERE nid = :nid', array(':nid' => $nid))->fetchField();
  1229. // Now we map the SKUs to the keys, for form handling, etc.
  1230. $models = drupal_map_assoc($models);
  1231. // Sort the SKUs.
  1232. asort($models);
  1233. // And finally, we prepend 'Any' so it's the first option.
  1234. if (!empty($add_blank) || $add_blank === '') {
  1235. if ($add_blank === TRUE) {
  1236. $add_blank = t('- Any -');
  1237. }
  1238. return array('' => $add_blank) + $models;
  1239. }
  1240. return $models;
  1241. }
  1242. /**
  1243. * Gets the cost of a product node.
  1244. *
  1245. * @param $node_id
  1246. * nid of the selected node
  1247. *
  1248. * @return
  1249. * float - cost
  1250. */
  1251. function uc_product_get_cost($node_id) {
  1252. $product = node_load($node_id);
  1253. return $product->cost;
  1254. }
  1255. /**
  1256. * Returns a product node's first attached image.
  1257. *
  1258. * @param $node_id
  1259. * The node's id.
  1260. * @param $style
  1261. * The image style used to format the image. 'uc_product' by default.
  1262. *
  1263. * @return
  1264. * A renderable array of the first product image, linked to the
  1265. * product node, or an empty array if no image is available.
  1266. */
  1267. function uc_product_get_picture($nid, $style = 'uc_product') {
  1268. $product = node_load($nid);
  1269. if (!$product) {
  1270. return array();
  1271. }
  1272. $field_name = variable_get('uc_image_' . $product->type, 'uc_product_image');
  1273. $output = array();
  1274. if ($field_name && isset($product->$field_name)) {
  1275. $elements = field_view_field('node', $product, $field_name, array(
  1276. 'label' => 'hidden',
  1277. 'type' => 'image',
  1278. 'settings' => array(
  1279. 'image_link' => 'content',
  1280. 'image_style' => $style,
  1281. ),
  1282. ));
  1283. // Extract the part of the render array we need.
  1284. $output = isset($elements[0]) ? $elements[0] : array();
  1285. if (isset($elements['#access'])) {
  1286. $output['#access'] = $elements['#access'];
  1287. }
  1288. }
  1289. return $output;
  1290. }
  1291. /**
  1292. * Finds the JavaScript image display module to use on product pages.
  1293. */
  1294. function uc_product_get_image_widget() {
  1295. static $got_widget = FALSE, $image_widget = array();
  1296. // Get the current image widget, if any.
  1297. if (!$got_widget) {
  1298. // Invoke hook to find widgets.
  1299. $image_widgets = uc_store_get_image_widgets();
  1300. // Find widget preference, if any.
  1301. $widget_name = variable_get('uc_product_image_widget', NULL);
  1302. if ($widget_name != NULL) {
  1303. // Widget to use has been set in admin menu.
  1304. $image_widget = $image_widgets[$widget_name];
  1305. }
  1306. else {
  1307. // Widget to use has not been chosen, so default to first element of
  1308. // array, if any.
  1309. $keys = array_keys($image_widgets);
  1310. if ($keys) {
  1311. $image_widget = $image_widgets[$keys[0]];
  1312. variable_set('uc_product_image_widget', $keys[0]);
  1313. }
  1314. }
  1315. $got_widget = TRUE;
  1316. }
  1317. return $image_widget;
  1318. }
  1319. /**
  1320. * Returns HTML for the product description.
  1321. *
  1322. * Modules adding information use hook_uc_product_description() and modules
  1323. * wanting to alter the output before rendering can do so by implementing
  1324. * hook_uc_product_description_alter(). By default, all descriptions supplied
  1325. * by modules via hook_uc_product_description() are concatenated together.
  1326. *
  1327. * NOTE: hook_uc_product_description() supercedes the deprecated
  1328. * hook_cart_item_description().
  1329. *
  1330. * @param $product
  1331. * Product.
  1332. *
  1333. * @return
  1334. * HTML rendered product description.
  1335. */
  1336. function uc_product_get_description($product) {
  1337. // Run through implementations of hook_uc_product_description().
  1338. $description = module_invoke_all('uc_product_description', $product);
  1339. // Now allow alterations via hook_uc_product_description_alter().
  1340. drupal_alter('uc_product_description', $description, $product);
  1341. return drupal_render($description);
  1342. }
  1343. /**
  1344. * Load a product class or all classes.
  1345. */
  1346. function uc_product_class_load($class_id = NULL, $reset = FALSE) {
  1347. static $classes;
  1348. if (!isset($classes) || $reset) {
  1349. // Load classes from database.
  1350. $classes = array();
  1351. $result = db_query("SELECT * FROM {uc_product_classes}");
  1352. while ($class = $result->fetchObject()) {
  1353. $class->locked = FALSE;
  1354. $classes[$class->pcid] = $class;
  1355. }
  1356. // Merge any module-defined classes.
  1357. foreach (module_invoke_all('uc_product_default_classes') as $pcid => $class) {
  1358. // The default class can be overridden by a module if needed,
  1359. // but is not treated as a real class elsewhere.
  1360. if ($pcid == 'product') {
  1361. continue;
  1362. }
  1363. $class += array(
  1364. 'pcid' => $pcid,
  1365. 'name' => $pcid,
  1366. 'description' => '',
  1367. );
  1368. if (isset($classes[$pcid])) {
  1369. // Merge defaults with the existing database info.
  1370. $classes[$pcid] = (object) array_merge($class, (array) $classes[$pcid]);
  1371. }
  1372. else {
  1373. // Ensure the class info is saved in the database.
  1374. drupal_write_record('uc_product_classes', $class);
  1375. $classes[$pcid] = (object) $class;
  1376. }
  1377. // Module-defined classes cannot be deleted.
  1378. $classes[$pcid]->locked = TRUE;
  1379. }
  1380. }
  1381. return is_null($class_id) ? $classes : $classes[$class_id];
  1382. }
  1383. /**
  1384. * Returns data for a product feature, given a feature ID and array key.
  1385. *
  1386. * @param $fid
  1387. * The string ID of the product feature you want to get data from.
  1388. * @param $key
  1389. * The key in the product feature array you want: title, callback, delete,
  1390. * settings.
  1391. *
  1392. * @return
  1393. * The value of the key you specify.
  1394. */
  1395. function uc_product_feature_data($fid, $key) {
  1396. static $features;
  1397. if (empty($features)) {
  1398. foreach (module_invoke_all('uc_product_feature') as $feature) {
  1399. $features[$feature['id']] = $feature;
  1400. }
  1401. }
  1402. return $features[$fid][$key];
  1403. }
  1404. /**
  1405. * Returns a form array with some default hidden values and submit button.
  1406. *
  1407. * @param $form
  1408. * The form array you wish to add the elements to.
  1409. *
  1410. * @return
  1411. * The form array with elements added for the nid, pfid, submit button, and
  1412. * cancel link.
  1413. *
  1414. * @ingroup forms
  1415. */
  1416. function uc_product_feature_form($form, &$form_state, $node, $feature) {
  1417. $form['nid'] = array(
  1418. '#type' => 'hidden',
  1419. '#value' => $node->nid,
  1420. );
  1421. // Forms to add a feature are only given an empty array.
  1422. if (!empty($feature)) {
  1423. $form['pfid'] = array(
  1424. '#type' => 'hidden',
  1425. '#value' => $feature['pfid'],
  1426. );
  1427. }
  1428. $form['actions'] = array('#type' => 'actions');
  1429. $form['actions']['submit'] = array(
  1430. '#type' => 'submit',
  1431. '#value' => t('Save feature'),
  1432. );
  1433. $form['actions']['cancel'] = array(
  1434. '#markup' => l(t('Cancel'), 'node/' . $node->nid . '/edit/features'),
  1435. );
  1436. return $form;
  1437. }
  1438. /**
  1439. * Saves a product feature to a product node.
  1440. *
  1441. * @param $data
  1442. * An array consisting of the following keys:
  1443. * - pfid: (optional) When editing an existing product feature, the numeric
  1444. * ID of the feature.
  1445. * - nid: The numeric ID of the product node.
  1446. * - fid: The string ID of the feature type.
  1447. * - description: The string describing the feature for the overview table.
  1448. */
  1449. function uc_product_feature_save(&$data) {
  1450. if (empty($data['nid']) && arg(0) == 'node' && intval(arg(1)) > 0) {
  1451. $data['nid'] = intval(arg(1));
  1452. }
  1453. if (empty($data['pfid'])) {
  1454. if (arg(0) == 'node' && arg(3) == 'features' && intval(arg(5)) > 0) {
  1455. $data['pfid'] = intval(arg(5));
  1456. }
  1457. }
  1458. // First attempt to update an existing row.
  1459. $result = drupal_write_record('uc_product_features', $data, !empty($data['pfid']) ? 'pfid' : array());
  1460. // Otherwise insert this feature as a new row.
  1461. if ($result == SAVED_NEW) {
  1462. drupal_set_message(t('The product feature has been added.'));
  1463. }
  1464. elseif ($result == SAVED_UPDATED) {
  1465. drupal_set_message(t('The product feature has been updated.'));
  1466. }
  1467. else {
  1468. drupal_set_message(t('The product feature was unable to be saved.'));
  1469. }
  1470. return 'node/' . $data['nid'] . '/edit/features';
  1471. }
  1472. /**
  1473. * Loads all product feature for a node.
  1474. *
  1475. * @param $nid
  1476. * The product node ID.
  1477. *
  1478. * @return
  1479. * The array of all product features object.
  1480. */
  1481. function uc_product_feature_load_multiple($nid) {
  1482. $features = db_query("SELECT * FROM {uc_product_features} WHERE nid = :nid ORDER BY pfid ASC", array(':nid' => $nid))->fetchAllAssoc('pfid');
  1483. return $features;
  1484. }
  1485. /**
  1486. * Loads a product feature object.
  1487. *
  1488. * @todo: should return an object instead of array.
  1489. *
  1490. * @param $pfid
  1491. * The product feature ID.
  1492. * @param $fid
  1493. * Optional. Specify a specific feature id.
  1494. *
  1495. * @return
  1496. * The product feature array.
  1497. */
  1498. function uc_product_feature_load($pfid) {
  1499. $feature = db_query("SELECT * FROM {uc_product_features} WHERE pfid = :pfid", array(':pfid' => $pfid))->fetchAssoc();
  1500. return $feature;
  1501. }
  1502. /**
  1503. * Deletes a product feature object.
  1504. *
  1505. * @param $pfid
  1506. * The product feature ID.
  1507. *
  1508. * @return
  1509. * The product feature object.
  1510. */
  1511. function uc_product_feature_delete($pfid) {
  1512. $feature = uc_product_feature_load($pfid);
  1513. // Call the delete function for this product feature if it exists.
  1514. $func = uc_product_feature_data($feature['fid'], 'delete');
  1515. if (function_exists($func)) {
  1516. $func($feature);
  1517. }
  1518. db_delete('uc_product_features')
  1519. ->condition('pfid', $pfid)
  1520. ->execute();
  1521. return SAVED_DELETED;
  1522. }
  1523. /**
  1524. * Creates a file field with an image field widget, and attach it to products.
  1525. *
  1526. * This field is used by default on the product page, as well as on the cart
  1527. * and catalog pages to represent the products they list. Instances are added
  1528. * to new product classes, and other node types that claim product-ness should
  1529. * call this function for themselves.
  1530. *
  1531. * @param $type
  1532. * The content type to which the image field is to be attached. This may be a
  1533. * a single type as a string, or an array of types. If NULL, all product
  1534. * types get an instance of the field.
  1535. */
  1536. function uc_product_add_default_image_field($type = NULL) {
  1537. $field = field_info_field('uc_product_image');
  1538. // Set up field if it doesn't exist.
  1539. if (!$field) {
  1540. $field = array(
  1541. 'field_name' => 'uc_product_image',
  1542. 'type' => 'image',
  1543. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  1544. );
  1545. field_create_field($field);
  1546. // Initialize this because field_info_field() would have set it.
  1547. $field['bundles'] = array();
  1548. }
  1549. $label = t('Image');
  1550. $new_instance = array(
  1551. 'entity_type' => 'node',
  1552. 'label' => $label,
  1553. 'weight' => -2,
  1554. 'widget' => array(
  1555. 'type' => 'image_image',
  1556. ),
  1557. 'display' => array(
  1558. 'full' => array(
  1559. 'label' => 'hidden',
  1560. 'type' => 'uc_product_image',
  1561. ),
  1562. 'teaser' => array(
  1563. 'label' => 'hidden',
  1564. 'type' => 'uc_product_image',
  1565. ),
  1566. ),
  1567. );
  1568. if ($type) {
  1569. // Accept single or multiple types as input.
  1570. $types = (array) $type;
  1571. }
  1572. else {
  1573. $types = uc_product_types();
  1574. }
  1575. foreach ($types as $type) {
  1576. $new_instance['bundle'] = $type;
  1577. $field_name = variable_get('uc_image_' . $type, '');
  1578. if (empty($field_name)) {
  1579. $field_name = 'uc_product_image';
  1580. }
  1581. $new_instance['field_name'] = $field_name;
  1582. $instance = field_info_instance('node', $field_name, $type);
  1583. // Only add the instance if it doesn't exist. Don't overwrite any changes.
  1584. if (!$instance) {
  1585. field_create_instance($new_instance);
  1586. variable_set('uc_image_' . $type, $field_name);
  1587. }
  1588. }
  1589. }
  1590. /**
  1591. * Implements hook_image_default_styles().
  1592. */
  1593. function uc_product_image_default_styles() {
  1594. $styles = array();
  1595. $styles['uc_product'] = array(
  1596. 'effects' => array(
  1597. array(
  1598. 'name' => 'image_scale',
  1599. 'data' => array(
  1600. 'width' => '250',
  1601. 'height' => '250',
  1602. 'upscale' => 0,
  1603. ),
  1604. 'weight' => '0',
  1605. ),
  1606. ),
  1607. );
  1608. $styles['uc_product_full'] = array(
  1609. 'effects' => array(),
  1610. );
  1611. $styles['uc_product_list'] = array(
  1612. 'effects' => array(
  1613. array(
  1614. 'name' => 'image_scale',
  1615. 'data' => array(
  1616. 'width' => '100',
  1617. 'height' => '100',
  1618. 'upscale' => 0,
  1619. ),
  1620. 'weight' => '0',
  1621. ),
  1622. ),
  1623. );
  1624. $styles['uc_thumbnail'] = array(
  1625. 'effects' => array(
  1626. array(
  1627. 'name' => 'image_scale',
  1628. 'data' => array(
  1629. 'width' => '35',
  1630. 'height' => '35',
  1631. 'upscale' => 0,
  1632. ),
  1633. 'weight' => '0',
  1634. ),
  1635. ),
  1636. );
  1637. return $styles;
  1638. }
  1639. /**
  1640. * Implements hook_views_api().
  1641. */
  1642. function uc_product_views_api() {
  1643. return array(
  1644. 'api' => '2.0',
  1645. 'path' => drupal_get_path('module', 'uc_product') . '/views',
  1646. );
  1647. }
  1648. /**
  1649. * Implements hook_features_api().
  1650. */
  1651. function uc_product_features_api() {
  1652. return array(
  1653. 'uc_product_classes' => array(
  1654. 'name' => t('Ubercart product classes'),
  1655. 'feature_source' => TRUE,
  1656. 'default_hook' => 'uc_product_default_classes',
  1657. 'file' => drupal_get_path('module', 'uc_product') . '/uc_product.features.inc',
  1658. ),
  1659. );
  1660. }
  1661. /**
  1662. * Implements hook_action_info().
  1663. */
  1664. function uc_product_action_info() {
  1665. return array(
  1666. 'uc_product_action_add_to_cart' => array(
  1667. 'label' => t('Add to cart'),
  1668. 'type' => 'node',
  1669. 'configurable' => FALSE,
  1670. 'triggers' => array('any'),
  1671. ),
  1672. );
  1673. }
  1674. /**
  1675. * Action implementation: adds a product to the cart.
  1676. */
  1677. function uc_product_action_add_to_cart($product, $context = array()) {
  1678. uc_cart_add_item($product->nid, 1, NULL, NULL, TRUE, FALSE, TRUE);
  1679. }