product_list.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. /**
  3. * @file
  4. * Webform module product_list component.
  5. */
  6. /**
  7. * Implements _webform_defaults_component().
  8. */
  9. function _webform_defaults_product_list() {
  10. return array(
  11. 'name' => '',
  12. 'form_key' => NULL,
  13. 'email' => 1,
  14. 'pid' => 0,
  15. 'weight' => 0,
  16. 'value' => '',
  17. 'mandatory' => 0,
  18. 'extra' => array(
  19. 'width' => '',
  20. 'unique' => 0,
  21. 'disabled' => 0,
  22. 'title_display' => 0,
  23. 'description' => '',
  24. 'attributes' => array(),
  25. 'private' => FALSE,
  26. 'products' => NULL,
  27. 'multiple' => NULL,
  28. 'order' => NULL,
  29. ),
  30. );
  31. }
  32. /**
  33. * Implements _webform_theme_component().
  34. */
  35. function _webform_theme_product_list() {
  36. return array(
  37. 'uc_webform_display_product_list' => array(
  38. 'render element' => 'element',
  39. 'file' => 'components/product_list.inc',
  40. 'path' => drupal_get_path('module', 'uc_webform'),
  41. ),
  42. 'uc_webform_render_product_list' => array(
  43. 'render element' => 'element',
  44. 'file' => 'components/product_list.inc',
  45. 'path' => drupal_get_path('module', 'uc_webform'),
  46. ),
  47. );
  48. }
  49. /**
  50. * Theme function for when a product is displayed in a webform submission.
  51. */
  52. function theme_uc_webform_display_product_list($variables) {
  53. $element = $variables['element'];
  54. $output = '';
  55. // TODO: Should this theme uc_webform_display_product_list be declared in
  56. // hook_theme()?
  57. if (!empty($element['#value'])) {
  58. $output = (count($element['#value']) > 1) ? theme('item_list', array('items' => $element['#value'])) : $element['#value'][0];
  59. }
  60. return $output;
  61. }
  62. /**
  63. * Theme function for a product when it is rendered as a part of a list.
  64. */
  65. function theme_uc_webform_render_product_list($products) {
  66. // TODO: Should this theme uc_webform_render_product_list be declared in
  67. // hook_theme()?
  68. $output = array();
  69. foreach ($products as $key => $product) {
  70. if (!empty($product)) {
  71. $output[$key] = '<span>' . $product['title'] . ', ' . t('Price:') . ' ' . $product['price'] . '</span>';
  72. }
  73. }
  74. return $output;
  75. }
  76. /**
  77. * Generate the form for editing a component.
  78. */
  79. function _webform_edit_product_list($component) {
  80. $products = array();
  81. $form = array();
  82. $product_types = uc_product_types();
  83. // Limit selection to only those products that do *not* contain attributes.
  84. $attrib_query = db_select('uc_product_attributes', 'pa')
  85. ->fields('pa', array('nid'));
  86. $query = db_select('node', 'n')
  87. ->fields('n', array('nid', 'title'))
  88. ->condition('n.nid', $attrib_query, 'NOT IN');
  89. // JOIN node with product.
  90. $query->join('uc_products', 'p', 'n.nid = p.nid');
  91. $query->addField('p', 'model');
  92. $query->orderBy('n.title');
  93. foreach ($query->execute() as $product) {
  94. $products[$product->nid . '_' . $product->model] = check_plain($product->title);
  95. }
  96. // Most options are stored in the "extra" array, which stores any settings
  97. // unique to a particular component type.
  98. $form['extra']['products'] = array(
  99. '#type' => 'select',
  100. '#title' => t('Products'),
  101. '#default_value' => $component['extra']['products'],
  102. '#multiple' => TRUE,
  103. '#description' => t('Please select your products. Only products without attributes are displayed.'),
  104. '#weight' => -3,
  105. '#size' => 20,
  106. '#required' => TRUE,
  107. '#options' => $products,
  108. );
  109. $form['extra']['multiple'] = array(
  110. '#type' => 'checkbox',
  111. '#title' => t('Multiple'),
  112. '#default_value' => $component['extra']['multiple'],
  113. '#description' => t('Check this option if the user is allowed to select multiple products.'),
  114. '#weight' => -2,
  115. );
  116. $form['extra']['order'] = array(
  117. '#type' => 'radios',
  118. '#title' => t('Product Order'),
  119. '#default_value' => $component['extra']['order'],
  120. '#multiple' => TRUE,
  121. '#description' => t('Please select the order in which the products will be displayed.'),
  122. '#weight' => -3,
  123. '#size' => 20,
  124. '#required' => TRUE,
  125. '#options' => array(
  126. 'order_by_price_asc' => t('Price (ascending)'),
  127. 'order_by_price_desc' => t('Price (descending)'),
  128. 'order_by_title_asc' => t('Title (ascending)'),
  129. 'order_by_title_desc' => t('Title (descending)'),
  130. ),
  131. );
  132. return $form;
  133. }
  134. /**
  135. * Implements _webform_render_component().
  136. */
  137. function _webform_render_product_list($component, $value = NULL) {
  138. $stock_description = "";
  139. foreach ($component['extra']['products'] as $val) {
  140. $product_info = explode('_', $val, 2);
  141. $node = node_load($product_info[0]);
  142. if (module_exists('uc_stock')) {
  143. $stock_level = uc_stock_level($product_info[1]);
  144. }
  145. else {
  146. $stock_level = FALSE;
  147. }
  148. // Check stock levels. The product is only selectable if it is in stock.
  149. if (($stock_level === FALSE) or (intval($stock_level) > 0)) {
  150. $product['title'] = check_plain($node->title);
  151. $money = round($node->sell_price, 2);
  152. // Keep trailing zeros.
  153. $product['price'] = sprintf("%01.2f", $money);
  154. $products[$val] = $product;
  155. }
  156. else {
  157. $stock_description .= check_plain($node->title) . ' ' . t('is out of stock.') . '<br />';
  158. }
  159. }
  160. // Users may choose to sort the products based on price or title.
  161. switch ($component['extra']['order']) {
  162. case 'order_by_price_asc':
  163. // Obtain a list of columns.
  164. foreach ($products as $key => $row) {
  165. $price[$key] = $row['price'];
  166. }
  167. // Sort the products with price ascending.
  168. array_multisort($price, SORT_ASC, $products);
  169. break;
  170. case 'order_by_price_desc':
  171. // Obtain a list of columns.
  172. foreach ($products as $key => $row) {
  173. $price[$key] = $row['price'];
  174. }
  175. // Sort the products with price descending.
  176. array_multisort($price, SORT_DESC, $products);
  177. break;
  178. case 'order_by_title_asc':
  179. // Obtain a list of columns.
  180. foreach ($products as $key => $row) {
  181. $title[$key] = $row['title'];
  182. }
  183. // Sort the products with title ascending.
  184. array_multisort($title, SORT_ASC, $products);
  185. break;
  186. case 'order_by_title_desc':
  187. // Obtain a list of columns.
  188. foreach ($products as $key => $row) {
  189. $title[$key] = $row['title'];
  190. }
  191. // Sort the products with title descending.
  192. array_multisort($title, SORT_DESC, $products);
  193. break;
  194. }
  195. // Add the currency sign back to the price.
  196. $currency_sign = variable_get('uc_currency_sign', '$');
  197. foreach ($products as $prod_key => $prod_val) {
  198. $products[$prod_key]['price'] = $currency_sign . $products[$prod_key]['price'];
  199. }
  200. $form_item = array();
  201. if ($component['extra']['multiple'] == 0) {
  202. $form_item = array(
  203. '#type' => 'radios',
  204. '#title' => $component['name'],
  205. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  206. '#weight' => $component['weight'],
  207. '#description' => $stock_description . _webform_filter_descriptions($component['extra']['description']),
  208. '#prefix' => '<div class="webform-component-' . $component['type'] . '" id="webform-component-' . $component['form_key'] . '">',
  209. '#suffix' => '</div>',
  210. '#pre_render' => array('webform_element_title_display'),
  211. '#post_render' => array('webform_element_wrapper'),
  212. '#required' => $component['mandatory'],
  213. '#options' => theme('uc_webform_render_product_list', $products),
  214. );
  215. }
  216. elseif ($component['extra']['multiple'] == 1) {
  217. $form_item = array(
  218. '#type' => 'checkboxes',
  219. '#title' => $component['name'],
  220. '#title_display' => $component['extra']['title_display'] ? $component['extra']['title_display'] : 'before',
  221. '#weight' => $component['weight'],
  222. '#description' => $stock_description . _webform_filter_descriptions($component['extra']['description']),
  223. '#prefix' => '<div class="webform-component-' . $component['type'] . '" id="webform-component-' . $component['form_key'] . '">',
  224. '#suffix' => '</div>',
  225. '#pre_render' => array('webform_element_title_display'),
  226. '#post_render' => array('webform_element_wrapper'),
  227. '#required' => $component['mandatory'],
  228. '#options' => theme('uc_webform_render_product_list', $products),
  229. );
  230. }
  231. if (isset($value)) {
  232. switch ($value[0]) {
  233. case 'radio_product_list':
  234. $form_item['#default_value'] = $value[1];
  235. break;
  236. case 'checkboxes_product_list':
  237. array_shift($value);
  238. $form_item['#default_value'] = $value;
  239. break;
  240. default:
  241. break;
  242. }
  243. }
  244. return $form_item;
  245. }
  246. /**
  247. * Implements _webform_display_component().
  248. */
  249. function _webform_display_product_list($component, $value, $format = 'html') {
  250. $products = array();
  251. if (isset($value)) {
  252. foreach ($value as $product) {
  253. if ($product == 0 || $product == 'checkboxes_product_list' || $product == 'radio_product_list') {
  254. continue;
  255. }
  256. else {
  257. $product_info = explode('_', $product, 2);
  258. $node = node_load($product_info[0]);
  259. $product_title = $node->title . ', ' . t('SKU:') . $node->model;
  260. array_push($products, $product_title);
  261. }
  262. }
  263. }
  264. $element = array(
  265. '#title' => $component['name'],
  266. '#weight' => $component['weight'],
  267. '#theme' => 'uc_webform_display_product_list',
  268. '#theme_wrappers' => $format == 'html' ? array('webform_element') : array('webform_element_text'),
  269. '#post_render' => array('webform_element_wrapper'),
  270. '#component' => $component,
  271. '#format' => $format,
  272. '#value' => $products,
  273. );
  274. return $element;
  275. }
  276. /**
  277. * Implementation of _webform_submit_component().
  278. */
  279. function _webform_submit_product_list($component, $value) {
  280. $return = array();
  281. switch (gettype($value)) {
  282. case 'string':
  283. // Value came from a radio select list.
  284. $return[0] = 'radio_product_list';
  285. $return[1] = $value;
  286. break;
  287. case 'array':
  288. // Value came from a checkboxes select list.
  289. $return[0] = 'checkboxes_product_list';
  290. $count = 1;
  291. foreach ($value as $product) {
  292. if ($product != 0) {
  293. $return[$count] = $product;
  294. $count++;
  295. }
  296. }
  297. break;
  298. }
  299. return $return;
  300. }
  301. /**
  302. * Calculate and returns statistics about results for this component.
  303. *
  304. * Note that this function does not filter for completed checkouts. The
  305. * _uc_webform_product_list_orders() function handles those.
  306. */
  307. function _webform_analysis_product_list($component, $sids = array(), $single = FALSE) {
  308. $rows = array();
  309. $query = db_select('webform_submitted_data', 'wsd');
  310. $query->addExpression('COUNT(data)', 'datacount');
  311. $query->fields('wsd', array('data'))
  312. ->condition('nid', $component['nid'], '=')
  313. ->condition('cid', $component['cid'], '=')
  314. ->groupBy('data');
  315. foreach ($query->execute() as $product) {
  316. $product_info = explode('_', $product->data, 2);
  317. $rows[] = array(
  318. 0 => $product_info[1],
  319. 1 => $product->datacount,
  320. );
  321. }
  322. return $rows;
  323. }
  324. /**
  325. * Return the result of a component value for display in a table.
  326. */
  327. function _webform_table_product_list($component, $value) {
  328. $results = '';
  329. if (isset($value)) {
  330. foreach ($value as $key => $product) {
  331. if ($key != 0) {
  332. $product_info = explode('_', $product, 2);
  333. $results .= "$product_info[1]<br />";
  334. }
  335. }
  336. }
  337. return $results;
  338. }
  339. /**
  340. * Implements _webform_csv_headers_component().
  341. */
  342. function _webform_csv_headers_product_list($component, $export_options) {
  343. $headers = array(
  344. 0 => array(),
  345. 1 => array(),
  346. 2 => array(),
  347. );
  348. if ($component['extra']['multiple'] && $export_options['select_format'] == 'separate') {
  349. $headers[0][] = '';
  350. $headers[1][] = $component['name'];
  351. $count = 0;
  352. foreach ($component['extra']['products'] as $product) {
  353. if ($count != 0) {
  354. // Empty column per sub-field in main header.
  355. $headers[0][] = '';
  356. $headers[1][] = '';
  357. }
  358. $product_info = explode('_', $product, 2);
  359. $headers[2][] = $product_info[1];
  360. $count++;
  361. }
  362. }
  363. else {
  364. $headers[0][] = '';
  365. $headers[1][] = '';
  366. $headers[2][] = $component['name'];
  367. }
  368. return $headers;
  369. }
  370. /**
  371. * Implements _webform_csv_data_component().
  372. */
  373. function _webform_csv_data_product_list($component, $export_options, $value) {
  374. $return = array();
  375. // Checkboxes.
  376. if ($component['extra']['multiple']) {
  377. foreach ($component['extra']['products'] as $key => $product) {
  378. $index = array_search($product, (array) $value);
  379. if ($index !== FALSE) {
  380. if ($export_options['select_format'] == 'separate') {
  381. $return[] = 'X';
  382. }
  383. else {
  384. $return[] = $export_options['select_keys'] ? $key : $product;
  385. }
  386. unset($value[$index]);
  387. }
  388. elseif ($export_options['select_format'] == 'separate') {
  389. $return[] = '';
  390. }
  391. }
  392. }
  393. // Radio Buttons.
  394. else {
  395. $key = $value[1];
  396. if ($export_options['select_keys']) {
  397. $return = $key;
  398. }
  399. else {
  400. $return = isset($component['extra']['products'][$key]) ? $component['extra']['products'][$key] : $key;
  401. }
  402. }
  403. if ($component['extra']['multiple'] && $export_options['select_format'] == 'compact') {
  404. $return = implode(',', (array) $return);
  405. }
  406. return $return;
  407. }