CategoriesBrowse.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ 3icKrXBspA0UZYkRLJIdUiqxC+TjHUuANW7uQokKrVs=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.3
  11. */
  12. /**
  13. * categories browse custom form element
  14. */
  15. namespace Ppb\Form\Element;
  16. use Cube\Form\Element,
  17. Cube\Controller\Front;
  18. class CategoriesBrowse extends Element
  19. {
  20. const ACTIVE_CATEGORY = 'active-category';
  21. const STORES_CATEGORIES = 'stores-categories';
  22. /**
  23. *
  24. * type of element - override the variable from the parent class
  25. *
  26. * @var string
  27. */
  28. protected $_element = 'CategoriesBrowse';
  29. /**
  30. *
  31. * view object
  32. *
  33. * @var \Cube\View
  34. */
  35. protected $_view;
  36. /**
  37. *
  38. * active request
  39. *
  40. * @var \Cube\Controller\Request\AbstractRequest
  41. */
  42. protected $_request;
  43. /**
  44. *
  45. * class constructor
  46. *
  47. * @param string $name
  48. */
  49. public function __construct($name)
  50. {
  51. parent::__construct('text', $name);
  52. $frontController = Front::getInstance();
  53. $this->_view = $frontController->getBootstrap()->getResource('view');
  54. $this->_request = $frontController->getRequest();
  55. }
  56. public function render()
  57. {
  58. $output = null;
  59. $translate = $this->getTranslate();
  60. $settings = Front::getInstance()->getBootstrap()->getResource('settings');
  61. $activeCategory = (isset($this->_attributes[self::ACTIVE_CATEGORY])) ? $this->_attributes[self::ACTIVE_CATEGORY] : null;
  62. $storesCategories = (isset($this->_attributes[self::STORES_CATEGORIES])) ? $this->_attributes[self::STORES_CATEGORIES] : false;
  63. if ($activeCategory !== null) {
  64. $output .= '<div class="category-breadcrumbs">';
  65. $breadcrumbs = array();
  66. foreach ($activeCategory as $key => $value) {
  67. $breadcrumbs[] = '<a href="' . $this->_view->url(array('category_name' => $value, 'parent_id' => $key), null, true,
  68. array('category_slug', 'page', 'submit')) . '">' . $translate->_($value) . '</a> ';
  69. }
  70. $output .= implode(' > ', $breadcrumbs)
  71. . '[ <a href="' . $this->_view->url(null, null, true,
  72. array('category_slug', 'category_name', 'parent_id', 'page', 'submit')) . '">' . $translate->_('Reset') . '</a> ]'
  73. . '</div>';
  74. }
  75. $params = $this->_request->getParams(
  76. array('parent_id', 'page', 'limit', 'submit', 'category_slug', 'category_name', 'controller', 'action')
  77. );
  78. $params = array_filter(
  79. $params, function (&$element) {
  80. if (is_array($element)) {
  81. return array_filter($element) ? true : false;
  82. }
  83. return (!empty($element));
  84. });
  85. /** @var \Ppb\Db\Table\Row\Category $category */
  86. foreach ($this->_customData['rowset'] as $category) {
  87. $counter = $category->getCounter();
  88. if ($counter > 0 || !$settings['hide_empty_categories'] || count($params) > 0 || $storesCategories) {
  89. $url = $this->_view->url(array('category_name' => $category['name'], 'parent_id' => $category['id']), null, true, array('category_slug', 'page', 'submit'));
  90. $output .= '<div><a href="' . $url . '">'
  91. . $translate->_($category['name'])
  92. . (($settings['category_counters'] && !count($params) && !$storesCategories) ? ' (' . $counter . ')' : '')
  93. . '</a></div>';
  94. }
  95. }
  96. $output .= '<input type="hidden" name="' . $this->_name . '" value="' . $this->getValue() . '" '
  97. . $this->_endTag;
  98. return $output;
  99. }
  100. }