FlatRatesLocationGroups.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ HvoA/w5sRlmsGBwbblSCDtfTdlJpLzD9jOK9GEcPk6frjNe5F4amnF2xUX7SmtkZkn0l9qv/xu9Ekagx1/6w+w==
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2017 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.9 [rev.7.9.01]
  11. */
  12. /**
  13. * flat rates location groups composite element
  14. *
  15. * creates an element that will contain an unlimited number of rows that include the following columns:
  16. * - a "first" field - for the postage cost of the first item in an invoice
  17. * - an "additional" field - for each additional item in an invoice
  18. * - a name text field
  19. * - a value select field of type ChznSelect
  20. */
  21. namespace Ppb\Form\Element;
  22. use Cube\Controller\Front,
  23. Cube\Form\Element,
  24. Cube\Locale\Format as LocaleFormat;
  25. class FlatRatesLocationGroups extends Element
  26. {
  27. const FIELD_NAME = 'name';
  28. const FIELD_LOCATIONS = 'locations';
  29. const FIELD_FIRST = 'first';
  30. const FIELD_ADDL = 'addl';
  31. /**
  32. *
  33. * type of element - override the variable from the parent class
  34. *
  35. * @var string
  36. */
  37. protected $_element = 'flatRatesLocationGroups';
  38. /**
  39. *
  40. * chzn select elements options
  41. *
  42. * @var array
  43. */
  44. protected $_chznMultiOptions = array();
  45. /**
  46. *
  47. * default currency
  48. *
  49. * @var string
  50. */
  51. protected $_currency;
  52. /**
  53. *
  54. * class constructor
  55. *
  56. * @param string $name
  57. */
  58. public function __construct($name)
  59. {
  60. parent::__construct($this->_element, $name);
  61. $translate = $this->getTranslate();
  62. $baseUrl = Front::getInstance()->getRequest()->getBaseUrl();
  63. $settings = Front::getInstance()->getBootstrap()->getResource('settings');
  64. $this->_currency = $settings['currency'];
  65. $this->setHeaderCode('<link href="' . $baseUrl . '/js/chosen/chosen.css" media="screen" rel="stylesheet" type="text/css">')
  66. ->setBodyCode('<script type="text/javascript" src="' . $baseUrl . '/js/chosen/chosen.jquery.min.js"></script>')
  67. ->setBodyCode(
  68. "<script type=\"text/javascript\">" . "\n"
  69. . " $('." . ChznSelect::ELEMENT_CLASS . "').chosen(); " . "\n"
  70. . "</script>");
  71. $this->setBodyCode(
  72. "<script type=\"text/javascript\">" . "\n"
  73. . " $(document).on('click', '.delete-field-row', function(e) { " . "\n"
  74. . " e.preventDefault(); " . "\n"
  75. . " var cnt = 0; " . "\n"
  76. . " $(this).closest('.field-row').remove(); " . "\n"
  77. . " $('.{$name}-row').each(function() { " . "\n"
  78. . " var selectName = $(this).find('select').attr('name').replace(/(\[\d+\])/g, '[' + cnt + ']');" . "\n"
  79. . " $(this).find('select').attr('name', selectName); " . "\n"
  80. . " cnt++; " . "\n"
  81. . " }); " . "\n"
  82. . " }); " . "\n"
  83. . " $(document).on('click', '.add-field-row', function(e) { " . "\n"
  84. . " e.preventDefault(); " . "\n"
  85. . " var nbRows = $('.{$name}-row').length; " . "\n"
  86. . " var row = $(this).closest('.field-row'); " . "\n"
  87. . " var cloned = row.clone(true, true); " . "\n"
  88. . " cloned.find('.add-field-row').remove(); " . "\n"
  89. . " cloned.find('select').val(row.find('select').val()); " . "\n" // as per jquery clone bug (doesnt copy selected values)
  90. . " cloned.find('.chzn-container').remove(); " . "\n"
  91. . " cloned.find('select').css({display: 'inline-block'}).removeAttr('id').removeClass('chzn-done'); " . "\n"
  92. . " $('<button>').attr('class', 'delete-field-row btn').html('" . $translate->_('Delete') . "').appendTo(cloned); " . "\n"
  93. . " cloned.insertBefore(row); " . "\n"
  94. . " var selectName = row.find('select').attr('name').replace(/(\[\d+\])/g, '[' + nbRows + ']'); " . "\n"
  95. . " row.find('.chzn-container').remove(); " . "\n"
  96. . " row.find('select').css({display: 'inline-block'}).removeAttr('id').removeClass('chzn-done').attr('name', selectName); " . "\n"
  97. . " row.find('input[type=text]').val(''); " . "\n"
  98. . " row.find('select').val(''); " . "\n"
  99. . " $('." . ChznSelect::ELEMENT_CLASS . "').chosen(); " . "\n"
  100. . " }); " . "\n"
  101. . "</script>");
  102. }
  103. /**
  104. *
  105. * get chzn elements multi options
  106. *
  107. * @return array
  108. */
  109. public function getChznMultiOptions()
  110. {
  111. return $this->_chznMultiOptions;
  112. }
  113. /**
  114. *
  115. * set chzn elements multi options
  116. *
  117. * @param array $chznMultiOptions
  118. *
  119. * @return $this
  120. */
  121. public function setChznMultiOptions($chznMultiOptions)
  122. {
  123. $this->_chznMultiOptions = (array)$chznMultiOptions;
  124. return $this;
  125. }
  126. /**
  127. *
  128. * render the form element
  129. *
  130. * @return string
  131. */
  132. public function render()
  133. {
  134. $output = null;
  135. $counter = 0;
  136. $values = $this->getValue();
  137. foreach ((array)$values[self::FIELD_NAME] as $id => $key) {
  138. if (!empty($key)) {
  139. $output .= $this->_renderRow(false, $key, $values[self::FIELD_LOCATIONS][$id], $values[self::FIELD_FIRST][$id], $values[self::FIELD_ADDL][$id], $counter++);
  140. }
  141. }
  142. $output .= $this->_renderRow(true, null, null, null, null, $counter);
  143. return $output;
  144. }
  145. /**
  146. *
  147. * render a single row of the element
  148. *
  149. * @param bool $new
  150. * @param string $key
  151. * @param string $locations
  152. * @param string $firstPrice
  153. * @param string $addlPrice
  154. * @param int $counter
  155. *
  156. * @return string
  157. */
  158. protected function _renderRow($new = true, $key = null, $locations = null, $firstPrice = null, $addlPrice = null, $counter = null)
  159. {
  160. $translate = $this->getTranslate();
  161. $brackets = '';
  162. if ($counter !== null) {
  163. $brackets = '[' . $counter . ']';
  164. }
  165. $chznSelect = new ChznSelect($this->_name . '[' . self::FIELD_LOCATIONS . ']' . $brackets, false);
  166. $chznSelect->setAttributes(array(
  167. 'data-placeholder' => $translate->_('Choose Locations...'),
  168. ))
  169. ->setMultiOptions(
  170. $this->getChznMultiOptions())
  171. ->setMultiple()
  172. ->setValue($locations);
  173. $localizedFirstPrice = LocaleFormat::getInstance()->numericToLocalized($firstPrice, true);
  174. $localizedAddlPrice = LocaleFormat::getInstance()->numericToLocalized($addlPrice, true);
  175. return '<div class="field-row ' . $this->_name . '-row">'
  176. . ' <input type="text" name="' . $this->_name . '[' . self::FIELD_FIRST . '][]" '
  177. . ' placeholder="' . $translate->_('First') . '" class="form-control input-mini input-flat-rates"'
  178. . ' value="' . $localizedFirstPrice . '" '
  179. . $this->_endTag
  180. . ' <input type="text" name="' . $this->_name . '[' . self::FIELD_ADDL . '][]" '
  181. . ' placeholder="' . $translate->_('Addl.') . '" class="form-control input-mini input-flat-rates"'
  182. . ' value="' . $localizedAddlPrice . '" '
  183. . $this->_endTag
  184. . ' <input type="text" name="' . $this->_name . '[' . self::FIELD_NAME . '][]" '
  185. . $this->renderAttributes()
  186. . 'value="' . $key . '" '
  187. . $this->_endTag
  188. . ' '
  189. . $chznSelect->render()
  190. . (($new === true) ?
  191. ' <button class="add-field-row btn btn-default">' . $translate->_('Add') . '</button>' :
  192. ' <button class="delete-field-row btn btn-default">' . $translate->_('Delete') . '</button>')
  193. . '</div>';
  194. }
  195. }