Category.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ K9PfLDm6IYP5RLNLNc//Ik1uYkiidL77Wm6NaMualOA=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.4
  11. */
  12. /**
  13. * category selector form element
  14. */
  15. namespace Ppb\Form\Element;
  16. use Cube\Form\Element,
  17. Cube\Controller\Front,
  18. Ppb\Service\Table\Relational\Categories as CategoriesService;
  19. class Category extends Element
  20. {
  21. /**
  22. *
  23. * type of element - override the variable from the parent class
  24. *
  25. * @var string
  26. */
  27. protected $_element = 'category';
  28. /**
  29. *
  30. * base url of the application
  31. *
  32. * @var string
  33. */
  34. protected $_baseUrl;
  35. /**
  36. *
  37. * categories table service
  38. *
  39. * @var \Ppb\Service\Table\Relational\Categories
  40. */
  41. protected $_categories;
  42. /**
  43. *
  44. * refresh page on select
  45. *
  46. * @var bool
  47. */
  48. protected $_refresh = true;
  49. /**
  50. *
  51. * class constructor
  52. *
  53. * @param string $name
  54. */
  55. public function __construct($name)
  56. {
  57. parent::__construct($this->_element, $name);
  58. $view = Front::getInstance()->getBootstrap()->getResource('view');
  59. $translate = $this->getTranslate();
  60. $this->setBodyCode(
  61. "<script type=\"text/javascript\">" . "\n"
  62. . " function displayCategoryBoxes(el, selected, option) { " . "\n"
  63. . " var storeId = el.find('.btn-category').attr('data-store-id'); " . "\n"
  64. . " var categoryId = el.find('.btn-category').attr('data-store-id'); " . "\n"
  65. . " var refreshPage = el.find('.btn-category').attr('data-no-refresh'); " . "\n"
  66. . " $.post( " . "\n"
  67. . " '" . $view->url(array('module' => 'app', 'controller' => 'async', 'action' => 'select-category')) . "', " . "\n"
  68. . " { " . "\n"
  69. . " id: selected, " . "\n"
  70. . " storeId: storeId, " . "\n"
  71. . " option: option " . "\n"
  72. . " }, " . "\n"
  73. . " function(data) { " . "\n"
  74. . " el.find('.btn-category').attr('disabled', false).html(data['category_name']); " . "\n"
  75. . " el.find('.category-id').val(data['category_id']); " . "\n"
  76. . " el.find('.form-boxes').html(data['output']); " . "\n"
  77. . " if (!data['output'] && data['refresh'] && refreshPage != 'true') { " . "\n"
  78. . " $('body').addClass('loading'); " . "\n"
  79. . " el.closest('form').submit(); " . "\n"
  80. . " } " . "\n"
  81. . " }, " . "\n"
  82. . " 'json' " . "\n"
  83. . " ); " . "\n"
  84. . " } " . "\n"
  85. . " $(document).on('click', '.btn-category', function(e) { " . "\n"
  86. . " e.preventDefault(); " . "\n"
  87. . " $(this).attr('disabled', true).text('" . $translate->_('Please wait ...') . "'); " . "\n"
  88. . " var el = $(this).closest('.form-group'); " . "\n"
  89. . " el.find('.btn-category-cancel').show(); " . "\n"
  90. . " var selected = el.find('.category-id').val(); " . "\n"
  91. . " displayCategoryBoxes(el, selected, 'change'); " . "\n"
  92. . " }); " . "\n"
  93. . " $(document).on('click', '.btn-category-cancel', function(e) { " . "\n"
  94. . " e.preventDefault(); " . "\n"
  95. . " var el = $(this).closest('.form-group'); " . "\n"
  96. . " el.find('.btn-category').attr('disabled', true).text('" . $translate->_('Please wait ...') . "'); " . "\n"
  97. . " el.find('.category-id').val(''); " . "\n"
  98. . " el.find('.btn-category-cancel').hide(); " . "\n"
  99. . " displayCategoryBoxes(el, '', 'reset'); " . "\n"
  100. . " }); " . "\n"
  101. . " $(document).on('change', '.category-selector', function(e) { " . "\n"
  102. . " e.preventDefault(); " . "\n"
  103. . " $(this).closest('.form-group').find('.btn-category').attr('disabled', true).text('" . $translate->_('Please wait ...') . "'); " . "\n"
  104. . " var el = $(this).closest('.form-group'); " . "\n"
  105. . " var selected = $(this).val(); " . "\n"
  106. . " displayCategoryBoxes(el, selected, 'select'); " . "\n"
  107. . " }); " . "\n"
  108. . "</script>");
  109. }
  110. /**
  111. *
  112. * get categories table service
  113. *
  114. * @return \Ppb\Service\Table\Relational\Categories
  115. */
  116. public function getCategories()
  117. {
  118. if (!$this->_categories instanceof CategoriesService) {
  119. $this->setCategories(
  120. new CategoriesService()
  121. );
  122. }
  123. return $this->_categories;
  124. }
  125. /**
  126. *
  127. * set categories table service
  128. *
  129. * @param \Ppb\Service\Table\Relational\Categories $categories
  130. *
  131. * @return \Ppb\Form\Element\Category
  132. */
  133. public function setCategories(CategoriesService $categories)
  134. {
  135. $this->_categories = $categories;
  136. return $this;
  137. }
  138. /**
  139. *
  140. * set refresh flag
  141. *
  142. * @param boolean $refresh
  143. *
  144. * @return $this
  145. */
  146. public function setRefresh($refresh)
  147. {
  148. $this->_refresh = $refresh;
  149. return $this;
  150. }
  151. /**
  152. *
  153. * get refresh flag
  154. *
  155. * @return boolean
  156. */
  157. public function getRefresh()
  158. {
  159. return $this->_refresh;
  160. }
  161. /**
  162. *
  163. * render the form element
  164. *
  165. * @return string
  166. */
  167. public function render()
  168. {
  169. $output = null;
  170. $translate = $this->getTranslate();
  171. $value = (string)$this->getValue();
  172. $categoryName = $this->getCategories()->getFullName($value); // this is already translated
  173. $this->removeAttribute('class');
  174. $output = $this->getPrefix() . ' '
  175. . '<input type="hidden" name="' . $this->_name . '" value="' . $value . '" class="category-id" '
  176. . $this->_endTag
  177. . '<button class="btn btn-primary btn-category" '
  178. . $this->renderAttributes() . '>'
  179. . (($categoryName === null) ? $translate->_('Select Category') : $categoryName)
  180. . '</button> '
  181. . '<i class="fa fa-times btn-category-cancel" style="display: '
  182. . (($categoryName === null) ? 'none' : 'inline-block') . '"></i>'
  183. . $this->getSuffix()
  184. . '<div class="form-boxes"></div>';
  185. return $output;
  186. }
  187. }