Category.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ K9PfLDm6IYP5RLNLNc//Ik1uYkiidL77Wm6NaMualOA=
  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.0
  11. */
  12. /**
  13. * categories table row object model
  14. */
  15. namespace Ppb\Db\Table\Row;
  16. class Category extends AbstractRow
  17. {
  18. /**
  19. *
  20. * generate link for categories browse pages
  21. *
  22. * @return array
  23. */
  24. public function link()
  25. {
  26. if ($slug = $this->getData('slug')) {
  27. return array(
  28. 'module' => 'listings',
  29. 'controller' => 'browse',
  30. 'action' => 'index',
  31. 'category_slug' => $slug,
  32. );
  33. }
  34. else {
  35. return array(
  36. 'module' => 'listings',
  37. 'controller' => 'browse',
  38. 'action' => 'index',
  39. 'category_name' => $this->getData('name'),
  40. 'parent_id' => $this->getData('id'),
  41. );
  42. }
  43. }
  44. /**
  45. *
  46. * generate link for categories list pages
  47. *
  48. * @return array
  49. */
  50. public function browseLink()
  51. {
  52. $slug = $this->getData('slug');
  53. if (!empty($slug)) {
  54. return array(
  55. 'module' => 'listings',
  56. 'controller' => 'categories',
  57. 'action' => 'browse',
  58. 'category_slug' => $slug
  59. );
  60. }
  61. else {
  62. return array(
  63. 'module' => 'listings',
  64. 'controller' => 'categories',
  65. 'action' => 'browse',
  66. 'category_name' => $this->getData('name'),
  67. 'parent_id' => $this->getData('id'),
  68. );
  69. }
  70. }
  71. /**
  72. *
  73. * get counter by listing type(s)
  74. *
  75. * @param string|array $filters listing types filter
  76. *
  77. * @return int
  78. */
  79. public function getCounter($filters = null)
  80. {
  81. $data = \Ppb\Utility::unserialize($this->getData('counter'));
  82. if ($filters === null) {
  83. $filters = array('auction', 'product');
  84. }
  85. $filters = (!is_array($filters)) ? array($filters) : $filters;
  86. $counter = 0;
  87. foreach ($filters as $value) {
  88. if (isset($data[$value])) {
  89. $counter += intval($data[$value]);
  90. }
  91. }
  92. return $counter;
  93. }
  94. /**
  95. *
  96. * category counter - addition operation
  97. *
  98. * @param string $listingType
  99. *
  100. * @return $this
  101. */
  102. public function addCounter($listingType)
  103. {
  104. $data = \Ppb\Utility::unserialize($this->getData('counter'));
  105. if (isset($data[$listingType])) {
  106. $data[$listingType]++;
  107. }
  108. else {
  109. $data[$listingType] = 1;
  110. }
  111. $this->save(array(
  112. 'counter' => serialize($data)
  113. ));
  114. return $this;
  115. }
  116. /**
  117. *
  118. * category counter - subtraction operation
  119. *
  120. * @param string $listingType
  121. *
  122. * @return $this
  123. */
  124. public function subtractCounter($listingType)
  125. {
  126. $data = \Ppb\Utility::unserialize($this->getData('counter'));
  127. if (isset($data[$listingType])) {
  128. $data[$listingType]--;
  129. }
  130. else {
  131. $data[$listingType] = 0;
  132. }
  133. if ($data[$listingType] < 0) {
  134. $data[$listingType] = 0;
  135. }
  136. $this->save(array(
  137. 'counter' => serialize($data)
  138. ));
  139. return $this;
  140. }
  141. }