Search.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ c4UGMY1shkPWkoGLnJP37qgFavx0dB2P0BTKuh5VZ1w=
  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.10 [rev.7.10.01]
  11. */
  12. namespace Ppb\Model\Elements;
  13. use Ppb\Db\Table\Row\User as UserModel,
  14. Ppb\Form\Element\CategoriesBrowse;
  15. class Search extends AbstractElements
  16. {
  17. /**
  18. *
  19. * form id
  20. *
  21. * @var array
  22. */
  23. protected $_formId = array();
  24. /**
  25. *
  26. * user object (used to generate store categories)
  27. *
  28. * @var \Ppb\Db\Table\Row\User
  29. */
  30. protected $_store;
  31. /**
  32. *
  33. * listings select object - used for displaying counters for certain form elements
  34. *
  35. * @var \Cube\Db\Select
  36. */
  37. protected $_listingsSelect;
  38. /**
  39. *
  40. * class constructor
  41. *
  42. * @param array $formId
  43. */
  44. public function __construct($formId = null)
  45. {
  46. parent::__construct();
  47. $this->_formId = (array)$formId;
  48. }
  49. /**
  50. *
  51. * get store user object
  52. *
  53. * @return \Ppb\Db\Table\Row\User
  54. */
  55. public function getStore()
  56. {
  57. return $this->_store;
  58. }
  59. /**
  60. *
  61. * set store user object
  62. *
  63. * @param \Ppb\Db\Table\Row\User $store
  64. *
  65. * @return $this
  66. */
  67. public function setStore(UserModel $store = null)
  68. {
  69. $this->_store = $store;
  70. return $this;
  71. }
  72. /**
  73. *
  74. * set countable listings select object
  75. *
  76. * @param \Cube\Db\Select $select
  77. */
  78. public function setListingsSelect($select)
  79. {
  80. $this->_listingsSelect = $select;
  81. }
  82. /**
  83. *
  84. * get countable listings select object
  85. *
  86. * @return \Cube\Db\Select
  87. */
  88. public function getListingsSelect()
  89. {
  90. return $this->_listingsSelect;
  91. }
  92. /**
  93. *
  94. * get form elements
  95. *
  96. * @return array
  97. */
  98. public function getElements()
  99. {
  100. $translate = $this->getTranslate();
  101. $settings = $this->getSettings();
  102. $categoriesSelect = $this->getCategories()->getTable()
  103. ->select()
  104. ->where('enable_auctions = ?', 1)
  105. ->order(array('order_id ASC', 'name ASC'));
  106. if ($this->_store instanceof UserModel) {
  107. $categoriesSelect->where("user_id is null OR user_id = '{$this->_store['id']}'");
  108. }
  109. else {
  110. $categoriesSelect->where('user_id is null');
  111. }
  112. $categoriesFilter = array(0);
  113. if ($parentId = $this->getData('parent_id')) {
  114. if (in_array('advanced', $this->_formId)) {
  115. $categoriesSelect->where('parent_id is null');
  116. }
  117. else {
  118. $categoriesSelect->where('parent_id = ?', $parentId);
  119. }
  120. $categoriesFilter = array_merge($categoriesFilter, array_keys(
  121. $this->getCategories()->getBreadcrumbs($parentId)));
  122. }
  123. else {
  124. $categoriesSelect->where('parent_id is null');
  125. if ($this->_store instanceof UserModel) {
  126. $storeCategories = $this->_store->getStoreSettings('store_categories');
  127. if ($storeCategories != null) {
  128. $categoriesSelect->where('id IN (?)', $storeCategories);
  129. }
  130. }
  131. }
  132. $categoriesMultiOptions = $this->getCategories()->getMultiOptions($categoriesSelect, null, $translate->_('All Categories'));
  133. $customFields = $this->getCustomFields()->getFields(
  134. array(
  135. 'type' => 'item',
  136. 'active' => 1,
  137. 'searchable' => 1,
  138. 'category_ids' => $categoriesFilter,
  139. ))->toArray();
  140. $showOnly = array(
  141. 'accept_returns' => $translate->_('Returns Accepted'),
  142. 'sold' => $translate->_('Sold Items'),
  143. );
  144. if ($settings['enable_make_offer']) {
  145. $showOnly['make_offer'] = $translate->_('Offers Accepted');
  146. }
  147. $listingTypesMultiOptions = $this->getListingTypes();
  148. $currency = $this->getView()->amount(false)->getCurrency();
  149. $currencyCode = (!empty($currency['symbol'])) ? $currency['symbol'] : $currency['iso_code'];
  150. $countriesMultiOptions = $this->getLocations()->getMultiOptions(null, null, $translate->_('All Countries'));
  151. // if ($settings['search_counters']) {
  152. // $listingsService = new ListingsService();
  153. // $listings = $listingsService->fetchAll($this->getListingsSelect());
  154. //
  155. // $categoriesCounters = array();
  156. // $listingTypesCounters = array();
  157. // $countriesCategories = array();
  158. // foreach ($listings as $listing) {
  159. // $categoriesCounters[$listing['category_id']] ++;
  160. // if ($listing['addl_category_id']) {
  161. // $categoriesCounters[$listing['addl_category_id']] ++;
  162. // }
  163. // // categories count
  164. // // listing types count
  165. // // custom fields count
  166. // // location count
  167. // }
  168. //
  169. // }
  170. $array = array(
  171. array(
  172. 'form_id' => 'global',
  173. 'id' => 'keywords',
  174. 'element' => 'text',
  175. 'label' => $this->_('Keywords'),
  176. 'attributes' => array(
  177. 'class' => 'form-control'
  178. . ((in_array('basic', $this->_formId) || in_array('stores',
  179. $this->_formId)) ? '' : ' input-xlarge'),
  180. ),
  181. ),
  182. array(
  183. 'form_id' => 'advanced',
  184. 'id' => 'parent_id',
  185. 'element' => 'select',
  186. 'label' => $this->_('Select Category'),
  187. 'multiOptions' => $categoriesMultiOptions,
  188. 'attributes' => array(
  189. 'class' => 'form-control input-large',
  190. ),
  191. 'bodyCode' => "
  192. <script type=\"text/javascript\">
  193. $(document).on('change', '[name=\"parent_id\"]', function() {
  194. $('body').addClass('loading');
  195. $(this).closest('form').prop('action', '').submit();
  196. });
  197. </script>",
  198. ),
  199. array(
  200. 'form_id' => array('basic', 'stores'),
  201. 'id' => 'parent_id',
  202. 'element' => '\\Ppb\\Form\\Element\\CategoriesBrowse',
  203. 'label' => $this->_('Categories'),
  204. 'multiOptions' => $categoriesMultiOptions,
  205. 'attributes' => array(
  206. CategoriesBrowse::ACTIVE_CATEGORY => ($parentId) ? $this->_categories->getBreadcrumbs($parentId) : null,
  207. CategoriesBrowse::STORES_CATEGORIES => ((in_array('stores', $this->_formId)) ? true : false),
  208. ),
  209. 'customData' => array(
  210. 'rowset' => $this->getCategories()->fetchAll($categoriesSelect),
  211. ),
  212. ),
  213. array(
  214. 'form_id' => 'basic',
  215. 'id' => 'price',
  216. 'element' => '\\Ppb\\Form\\Element\\Range',
  217. 'label' => $this->_('Price'),
  218. 'prefix' => $currencyCode,
  219. 'attributes' => array(
  220. 'class' => 'form-control input-tiny',
  221. )
  222. ),
  223. array(
  224. 'form_id' => array('basic', 'advanced'),
  225. 'id' => 'show_only',
  226. 'element' => 'checkbox',
  227. 'label' => $this->_('Show Only'),
  228. 'multiOptions' => $showOnly
  229. ),
  230. array(
  231. 'form_id' => array('basic', 'advanced'),
  232. 'id' => 'listing_type',
  233. 'element' => 'checkbox',
  234. 'label' => $this->_('Format'),
  235. 'multiOptions' => $listingTypesMultiOptions,
  236. ),
  237. array(
  238. 'form_id' => 'global',
  239. 'id' => 'country',
  240. 'element' => 'select',
  241. 'label' => $this->_('Location'),
  242. 'multiOptions' => $countriesMultiOptions,
  243. 'attributes' => array(
  244. 'class' => 'form-control'
  245. . ((in_array('basic', $this->_formId) || in_array('stores',
  246. $this->_formId)) ? '' : ' input-large'),
  247. ),
  248. ),
  249. array(
  250. 'form_id' => 'global',
  251. 'id' => 'sort',
  252. 'element' => 'hidden',
  253. ),
  254. array(
  255. 'form_id' => 'global',
  256. 'id' => 'show',
  257. 'element' => 'hidden',
  258. ),
  259. );
  260. foreach ($customFields as $key => $customField) {
  261. $customFields[$key]['form_id'] = array('basic', 'advanced');
  262. $customFields[$key]['id'] = 'custom_field_' . $customField['id'];
  263. // elements of type select and radio will be converted to checkboxes
  264. if (in_array($customField['element'], array('select', 'radio'))) {
  265. $customFields[$key]['element'] = 'checkbox';
  266. }
  267. if (in_array($customField['element'], array('text', 'textarea'))) {
  268. $attributes = unserialize($customField['attributes']);
  269. array_push($attributes['key'], 'class');
  270. if ($customField['element'] == 'text' && in_array('advanced', $this->_formId)) {
  271. array_push($attributes['value'], 'form-control input-default');
  272. }
  273. else {
  274. array_push($attributes['value'], 'form-control');
  275. }
  276. $customFields[$key]['attributes'] = serialize($attributes);
  277. }
  278. }
  279. array_splice($array, 3, 0, $customFields);
  280. return $array;
  281. }
  282. }