product.inc 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <?php
  2. /**
  3. * @file
  4. * Webform module product component.
  5. */
  6. /**
  7. * Implements _webform_defaults_component().
  8. */
  9. function _webform_defaults_product() {
  10. return array(
  11. 'name' => '',
  12. 'form_key' => NULL,
  13. 'pid' => 0,
  14. 'weight' => 0,
  15. 'value' => '',
  16. 'mandatory' => 0,
  17. 'product' => NULL,
  18. 'extra' => array(
  19. 'width' => '',
  20. 'unique' => 0,
  21. 'disabled' => 0,
  22. 'title_display' => 0,
  23. 'description' => '',
  24. 'attributes' => array(),
  25. 'private' => FALSE,
  26. 'product' => NULL,
  27. ),
  28. );
  29. }
  30. /**
  31. * Implements _webform_theme_component().
  32. */
  33. function _webform_theme_product() {
  34. return array(
  35. 'uc_webform_product' => array(
  36. 'render element' => 'element',
  37. 'file' => 'components/product.inc',
  38. 'path' => drupal_get_path('module', 'uc_webform'),
  39. ),
  40. 'uc_webform_display_product' => array(
  41. 'render element' => 'element',
  42. 'file' => 'components/product.inc',
  43. 'path' => drupal_get_path('module', 'uc_webform'),
  44. ),
  45. 'uc_webform_render_product' => array(
  46. 'render element' => 'element',
  47. 'file' => 'components/product.inc',
  48. 'path' => drupal_get_path('module', 'uc_webform'),
  49. ),
  50. );
  51. }
  52. /**
  53. * Implements _webform_edit_component().
  54. * http://api.lullabot.com/_webform_edit_component/7
  55. */
  56. function _webform_edit_product($component) {
  57. $form = array();
  58. // Disabling the description if not wanted.
  59. $form['description'] = array();
  60. $form['value'] = array(
  61. '#type' => 'textfield',
  62. '#title' => t('Default quantity'),
  63. '#default_value' => $component['value'],
  64. '#description' => t('The default quantity of product.'),
  65. '#size' => 5,
  66. '#maxlength' => 10,
  67. '#weight' => 0,
  68. '#id' => 'email-value',
  69. );
  70. // Most options are stored in the "extra" array, which stores any settings
  71. // unique to a particular component type.
  72. $form['extra']['product'] = array(
  73. '#type' => 'textfield',
  74. '#title' => t('Product'),
  75. // Notice: Undefined index: product in _webform_edit_product()
  76. '#default_value' => $component['extra']['product'],
  77. '#weight' => -3,
  78. '#size' => 60,
  79. '#description' => t('Please select a product. Only products that do not contain attributes will be displayed.'),
  80. '#autocomplete_path' => 'uc_webform/autocomplete',
  81. );
  82. $form['display']['width'] = array(
  83. '#type' => 'textfield',
  84. '#title' => t('Width'),
  85. '#description' => t('Width of the textfield.') . ' ' . t('Leaving blank will use the default size.'),
  86. '#size' => 5,
  87. '#maxlength' => 10,
  88. '#weight' => 0,
  89. '#parents' => array('extra', 'width'),
  90. );
  91. $form['display']['disabled'] = array(
  92. '#type' => 'checkbox',
  93. '#title' => t('Disabled'),
  94. '#return_value' => 1,
  95. '#description' => t('Make this field non-editable. Useful for setting an unchangeable default value.'),
  96. '#weight' => 3,
  97. '#default_value' => $component['extra']['disabled'],
  98. '#parents' => array('extra', 'disabled'),
  99. );
  100. if (isset($component['extra']['width'])) {
  101. $form['display']['width']['#default_value'] = $component['extra']['width'];
  102. }
  103. return $form;
  104. }
  105. /**
  106. * Implements _webform_render_component().
  107. * http://api.lullabot.com/_webform_render_component/7
  108. */
  109. function _webform_render_product($component, $value = NULL, $filter = TRUE) {
  110. $stock_description = "";
  111. $product_info = explode('_', $component['extra']['product'], 2);
  112. $node = node_load($product_info[0]);
  113. if(!$node) {
  114. return;
  115. }
  116. $sku = $product_info[1];
  117. if (module_exists('uc_stock')) {
  118. $stock_level = uc_stock_level($sku);
  119. }
  120. else {
  121. $stock_level = FALSE;
  122. }
  123. // Check stock levels. The product is only selectable if it is in stock.
  124. if (($stock_level === FALSE) or (intval($stock_level) > 0)) {
  125. // Product is available.
  126. $product = array(
  127. 'title' => check_plain($node->title),
  128. 'price' => round($node->sell_price, 2),
  129. );
  130. // TODO Please change this theme call to use an associative array for the
  131. // $variables parameter.
  132. $element = array(
  133. '#type' => 'textfield',
  134. '#title' => $component['name'],
  135. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  136. '#weight' => $component['weight'],
  137. '#description' => $component['extra']['description'],
  138. '#default_value' => isset($value) ? check_plain($value[2]) : check_plain($component['value']),
  139. //'#default_value' => 2,
  140. '#field_prefix' => t('Quantity:') . ' ',
  141. '#field_suffix' => theme('uc_webform_render_product', $product),
  142. '#required' => $component['mandatory'],
  143. '#pre_render' => array('webform_element_title_display'),
  144. '#disabled' => $component['extra']['disabled'],
  145. '#sku' => $sku,
  146. '#element_validate' => array('_webform_render_product_validate'),
  147. );
  148. // $element['#value'] = $component['value'];
  149. //var_dump($component);
  150. // This is so that products whose quantity is disabled are added to the
  151. // cart correctly.
  152. if ($component['extra']['disabled']) {
  153. $element['#value'] = isset($value) ? check_plain($value[2]) : check_plain($component['value']);
  154. }
  155. if (isset($component['extra']['width'])) {
  156. $element['#size'] = $component['extra']['width'];
  157. }
  158. }
  159. else {
  160. // Product is out of stock.
  161. $stock_description .= check_plain($node->title) . ' ' . t('is out of stock.') . '<br />';
  162. $element = array(
  163. '#type' => 'item',
  164. '#title' => $component['name'],
  165. '#default_value' => '',
  166. '#value' => '',
  167. '#description' => $stock_description . $component['extra']['description'],
  168. );
  169. }
  170. // Change the 'width' option to the correct 'size' option.
  171. if ($component['extra']['width'] > 0) {
  172. $element['#size'] = $component['extra']['width'];
  173. }
  174. return $element;
  175. }
  176. /**
  177. * Validate product entry.
  178. */
  179. function _webform_render_product_validate($element, &$form_state) {
  180. // If the user entered a value, make sure that it's a good one.
  181. if (!empty($element['#value'])) {
  182. $match = preg_match('/\A[0-9]+\Z/', $element['#value']);
  183. if (($match == 0) || $match == FALSE) {
  184. form_error($element, t('Please enter a positive number.'));
  185. }
  186. }
  187. // Check to see that we have enough in stock.
  188. if (module_exists('uc_stock')) {
  189. $stock = uc_stock_level($element['#sku']);
  190. }
  191. else {
  192. $stock = FALSE;
  193. }
  194. if (($stock !== FALSE) && ($stock < $element['#value'])) {
  195. $error_msg = t("Only !stock of SKU: !sku remain. Please enter a different amount.", array('!stock' => check_plain($stock), '!sku' => check_plain($element['#sku'])));
  196. form_error($element, check_plain($error_msg));
  197. }
  198. }
  199. /**
  200. * Implements _webform_display_component().
  201. */
  202. function _webform_display_product($component, $value, $format = 'html') {
  203. $product_info = explode('_', $component['extra']['product'], 2);
  204. $node = node_load($product_info[0]);
  205. $result_info = array(t('Title:') . ' ' . $node->title . '<br/>' . t('SKU:') . ' ' . $product_info[1] . '<br/>' . t('Quantity:') . ' ' . $value[0]);
  206. $element = array(
  207. '#title' => $component['name'],
  208. '#weight' => $component['weight'],
  209. '#theme' => 'uc_webform_display_product',
  210. '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
  211. '#post_render' => array('webform_element_wrapper'),
  212. '#component' => $component,
  213. '#format' => $format,
  214. '#value' => $result_info,
  215. );
  216. return $element;
  217. }
  218. /**
  219. * Theme function to render an product component.
  220. */
  221. function theme_uc_webform_display_product($element) {
  222. // TODO: Should this theme uc_webform_display_product be declared in
  223. // hook_theme()?
  224. return $element['element']['#value'][0];
  225. }
  226. /**
  227. * Theme function to render an product component when rendered in a
  228. * webform.
  229. */
  230. function theme_uc_webform_render_product($product) {
  231. // TODO: Should this theme uc_webform_render_product be declared in
  232. // hook_theme()?
  233. return '<span>' . check_plain($product['title']) . ', ' . t('Price:') . ' ' . variable_get('uc_currency_sign', '$') . check_plain($product['price']) . '</span>';
  234. }
  235. /**
  236. * Implementation of _webform_submit_component().
  237. */
  238. function _webform_submit_product($component, $value) {
  239. // $return[0] = 'product';
  240. if (!empty($value)) {
  241. // Save the nid of the product.
  242. // $return[1] = $component['extra']['product'];
  243. // Save the quantity.
  244. $return[0] = $value;
  245. return $return;
  246. }
  247. else {
  248. // Save the nid of the product.
  249. // $return[1] = $component['extra']['product'];
  250. // Save the quantity.
  251. $return[0] = 0;
  252. return $return;
  253. }
  254. }
  255. /**
  256. * Implements _webform_analysis_component().
  257. */
  258. function _webform_analysis_product($component, $sids = array()) {
  259. $query = db_select('webform_submitted_data', 'wsd')
  260. ->fields('wsd', array('no', 'data'))
  261. ->condition('nid', $component['nid'], '=')
  262. ->condition('cid', $component['cid'], '=');
  263. $total = 0;
  264. foreach ($query->execute() as $result) {
  265. if ($result->no == 2) {
  266. $total += $result->data;
  267. }
  268. }
  269. $product_info = explode('_', $component['extra']['product'], 2);
  270. $product_node = node_load($product_info[0]);
  271. $rows[0] = array(t($product_info[1]), $total);
  272. return $rows;
  273. }
  274. /**
  275. * Implements _webform_table_component().
  276. */
  277. function _webform_table_product($component, $value) {
  278. return check_plain(empty($value[0]) ? '' : $value[0]);
  279. }
  280. /**
  281. * Implements _webform_csv_headers_component().
  282. */
  283. function _webform_csv_headers_product($component, $export_options) {
  284. $header = array();
  285. $product_info = explode('_', $component['extra']['product'], 2);
  286. $header[0] = '';
  287. $header[1] = $component['name'];
  288. $header[2] = $product_info[1] . ' ' . t('Quantity');
  289. return $header;
  290. }
  291. /**
  292. * Implements _webform_csv_data_component().
  293. */
  294. function _webform_csv_data_product($component, $export_options, $value) {
  295. return empty($value[0]) ? '' : $value[0];
  296. }