StockLevels.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ CIRslYEf3mfCjDYVWa6h00QEiY+aTS/CqM7LtuIw6fY=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2016 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.8
  11. */
  12. /**
  13. * listing stock levels form element
  14. * allows the creation of the composite element used if we have product attributes
  15. * product attributes are generated from custom fields
  16. */
  17. namespace Ppb\Form\Element;
  18. use Cube\Form\Element,
  19. Cube\Controller\Front,
  20. Ppb\View\Helper\ProductAttributes as ProductAttributesHelper;
  21. class StockLevels extends TextAutocomplete
  22. {
  23. const FIELD_OPTIONS = 'options';
  24. const FIELD_QUANTITY = 'quantity';
  25. const FIELD_PRICE = 'price';
  26. /**
  27. *
  28. * custom fields array
  29. *
  30. * @var array
  31. */
  32. protected $_customFields;
  33. /**
  34. *
  35. * form data - needed to display only selected product attributes
  36. *
  37. * @var array
  38. */
  39. protected $_formData;
  40. /**
  41. *
  42. * whether we have an empty stock levels element
  43. *
  44. * @var bool
  45. */
  46. protected $_empty = false;
  47. public function __construct($name)
  48. {
  49. parent::__construct($name);
  50. $baseUrl = Front::getInstance()->getRequest()->getBaseUrl();
  51. $translate = $this->getTranslate();
  52. $this->setBodyCode('<script type="text/javascript" src="' . $baseUrl . '/js/bootbox.min.js"></script>')
  53. ->setBodyCode(
  54. "<script type=\"text/javascript\">
  55. $(document).on('click', 'button[name=\"" . $this->_name . "\"]', function(e) {
  56. e.preventDefault();
  57. var btn = $(this);
  58. bootbox.confirm(\"" . $translate->_('Do you wish to set the same quantity value on all fields below?') . "\", function(result) {
  59. if (result) {
  60. var quantity = btn.closest('div').find('input[name*=\"" . self::FIELD_QUANTITY . "\"]').val();
  61. btn.closest('.form-group').find('input[name*=\"" . self::FIELD_QUANTITY . "\"]').val(quantity);
  62. }
  63. });
  64. });
  65. </script>");
  66. }
  67. /**
  68. *
  69. * get custom fields array
  70. *
  71. * @return array
  72. */
  73. public function getCustomFields()
  74. {
  75. return $this->_customFields;
  76. }
  77. /**
  78. *
  79. * set custom fields array
  80. *
  81. * @param array $customFields
  82. *
  83. * @return $this
  84. */
  85. public function setCustomFields($customFields)
  86. {
  87. $this->_customFields = $customFields;
  88. return $this;
  89. }
  90. /**
  91. *
  92. * get form data
  93. *
  94. * @param string $key
  95. *
  96. * @return mixed
  97. */
  98. public function getFormData($key = null)
  99. {
  100. if ($key !== null) {
  101. if (!empty($this->_formData[$key])) {
  102. return $this->_formData[$key];
  103. }
  104. return null;
  105. }
  106. return $this->_formData;
  107. }
  108. /**
  109. *
  110. * set form data
  111. *
  112. * @param array $formData
  113. *
  114. * @return $this
  115. */
  116. public function setFormData($formData)
  117. {
  118. $this->_formData = $formData;
  119. return $this;
  120. }
  121. /**
  122. *
  123. * check empty flag
  124. *
  125. * @return boolean
  126. */
  127. public function isEmpty()
  128. {
  129. return $this->_empty;
  130. }
  131. /**
  132. *
  133. * set empty flag
  134. *
  135. * @param boolean $empty
  136. *
  137. * @return $this
  138. */
  139. public function setEmpty($empty)
  140. {
  141. $this->_empty = $empty;
  142. return $this;
  143. }
  144. public function render()
  145. {
  146. $output = null;
  147. $array = array();
  148. $customFields = $this->getCustomFields();
  149. foreach ($customFields as $key => $customField) {
  150. if ($customField['product_attribute']) {
  151. $customFields[$key]['multiOptions']
  152. = $multiOptions
  153. = \Ppb\Utility::unserialize($customField['multiOptions']);
  154. if (!empty($multiOptions['key'])) {
  155. $id = intval(str_replace('custom_field_', '', $customField['id']));
  156. $value = array_filter($multiOptions['key']);
  157. $customFieldData = (array)$this->getFormData($customField['id']);
  158. $array[$id] = array_intersect($customFieldData, $value);
  159. }
  160. }
  161. }
  162. $cartesian = array_filter(
  163. $this->_cartesian($array));
  164. if (count($cartesian) > 0) {
  165. $values = $this->getValue();
  166. $translate = $this->getTranslate();
  167. $cloneButton = true;
  168. $helper = new ProductAttributesHelper();
  169. foreach ($cartesian as $key => $row) {
  170. $value = str_ireplace(
  171. array("'", '"'),
  172. array('&#039;', '&quot;'), serialize($row));
  173. $price = null;
  174. $quantity = null;
  175. foreach ((array)$values as $k => $v) {
  176. if (!empty($values[$k][self::FIELD_OPTIONS])) {
  177. if (\Ppb\Utility::unserialize($values[$k][self::FIELD_OPTIONS]) == $row) {
  178. $quantity = (!empty($values[$k][self::FIELD_QUANTITY])) ?
  179. abs(intval($values[$k][self::FIELD_QUANTITY])) : null;
  180. $price = (!empty($values[$k][self::FIELD_PRICE])) ?
  181. abs(floatval($values[$k][self::FIELD_PRICE])) : null;
  182. }
  183. }
  184. }
  185. $output .= '<input type="hidden" name="' . $this->_name . '[' . $key . '][' . self::FIELD_OPTIONS . ']" '
  186. . 'value="' . $value . '">';
  187. $this->removeAttribute('placeholder')->addAttribute('placeholder', $translate->_('Qty'));
  188. $output .= '<label class="col-sm-4 control-label">' . $helper->productAttributes($row)->display() . '</label>'
  189. . '<div class="col-sm-8">'
  190. . ' <input type="text" name="' . $this->_name . '[' . $key . '][' . self::FIELD_QUANTITY . ']" '
  191. . $this->renderAttributes()
  192. . ' value="' . $quantity . '" '
  193. . $this->_endTag;
  194. if ($cloneButton === true) {
  195. $output .= '<button type="button" name="' . $this->_name . '" class="btn btn-link">
  196. <i class="fa fa-clone" alt="' . $translate->_('Copy Quantity Value') . '"></i>
  197. </button>';
  198. }
  199. $this->removeAttribute('placeholder')->addAttribute('placeholder', $translate->_('Price'));
  200. $output .= ' '
  201. . $translate->_('Add Extra:')
  202. . ' <input type="text" name="' . $this->_name . '[' . $key . '][' . self::FIELD_PRICE . ']" '
  203. . $this->renderAttributes()
  204. . ' value="' . $price . '" '
  205. . $this->_endTag;
  206. $output .= '</div>'
  207. . '<div class="clearfix"></div>';
  208. $cloneButton = false;
  209. }
  210. }
  211. if ($output == null) {
  212. $this->setEmpty(true);
  213. $hidden = new Element\Hidden($this->_name);
  214. $output = $hidden->render();
  215. }
  216. return '<div class="stock-levels">' . $output . '</div>';
  217. }
  218. /**
  219. *
  220. * create the cartesian product of the input array
  221. *
  222. * @param array $input
  223. *
  224. * @return array
  225. */
  226. protected function _cartesian($input)
  227. {
  228. // filter out empty values
  229. $input = array_filter($input);
  230. $result = array(array());
  231. foreach ($input as $key => $values) {
  232. $append = array();
  233. foreach ($result as $product) {
  234. foreach ($values as $item) {
  235. $product[$key] = $item;
  236. $append[] = $product;
  237. }
  238. }
  239. $result = $append;
  240. }
  241. return $result;
  242. }
  243. }