StoresSubscriptions.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ 8WlCtERu+ZSJDQp0N6K1a97mi9cbiUvoq6NNa7CWZG4=
  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.2
  11. */
  12. /**
  13. * store subscriptions table service class
  14. */
  15. namespace Ppb\Service\Table;
  16. use Ppb\Db\Table\StoresSubscriptions as StoresSubscriptionsTable,
  17. Cube\Controller\Front;
  18. class StoresSubscriptions extends AbstractServiceTable
  19. {
  20. /**
  21. *
  22. * default currency
  23. *
  24. * @var string
  25. */
  26. protected $_defaultCurrency;
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. $settings = $this->getSettings();
  31. if (isset($settings['currency'])) {
  32. $this->setDefaultCurrency($settings['currency']);
  33. }
  34. $this->setTable(
  35. new StoresSubscriptionsTable());
  36. }
  37. /**
  38. *
  39. * set the default currency variable
  40. *
  41. * @param string $currency
  42. *
  43. * @return \Ppb\Service\Table\StoresSubscriptions
  44. */
  45. public function setDefaultCurrency($currency)
  46. {
  47. $this->_defaultCurrency = $currency;
  48. return $this;
  49. }
  50. /**
  51. *
  52. * get all store subscriptions
  53. * to be used for the store subscription selector
  54. *
  55. * @return array
  56. */
  57. public function getMultiOptions()
  58. {
  59. $data = array();
  60. $translate = $this->getTranslate();
  61. $view = Front::getInstance()->getBootstrap()->getResource('view');
  62. $subscriptions = $this->fetchAll();
  63. foreach ($subscriptions as $subscription) {
  64. /** @var \Ppb\Db\Table\Row\StoreSubscription $subscription */
  65. $data[$subscription['id']] = $translate->_($subscription['name']);
  66. /** @var \Cube\View $view */
  67. if ($view->isHelper('storeSubscription')) {
  68. $data[$subscription['id']] .= ' - ' . $view->storeSubscription($subscription)->description();
  69. }
  70. }
  71. return $data;
  72. }
  73. /**
  74. *
  75. * get all table columns needed to generate the
  76. * stores subscriptions table in the admin area
  77. *
  78. * @return array
  79. */
  80. public function getColumns()
  81. {
  82. return array(
  83. array(
  84. 'label' => $this->_('Subscription Name'),
  85. 'element_id' => 'name',
  86. ),
  87. array(
  88. 'label' => $this->_('Price'),
  89. 'class' => 'size-small',
  90. 'element_id' => 'price',
  91. ),
  92. array(
  93. 'label' => $this->_('# Listings'),
  94. 'class' => 'size-mini',
  95. 'element_id' => 'listings',
  96. ),
  97. array(
  98. 'label' => $this->_('Recurring [days]'),
  99. 'class' => 'size-mini',
  100. 'element_id' => 'recurring_days',
  101. ),
  102. array(
  103. 'label' => $this->_('Featured Store'),
  104. 'class' => 'size-mini',
  105. 'element_id' => 'featured_store',
  106. ),
  107. array(
  108. 'label' => $this->_('Delete'),
  109. 'class' => 'size-mini',
  110. 'element_id' => array(
  111. 'id', 'delete'
  112. ),
  113. ),
  114. );
  115. }
  116. /**
  117. *
  118. * get all form elements that are needed to generate the
  119. * stores subscriptions table in the admin area
  120. *
  121. * @return array
  122. */
  123. public function getElements()
  124. {
  125. return array(
  126. array(
  127. 'id' => 'id',
  128. 'element' => 'hidden',
  129. ),
  130. array(
  131. 'id' => 'name',
  132. 'element' => 'text',
  133. 'attributes' => array(
  134. 'class' => 'form-control input-large',
  135. ),
  136. ),
  137. array(
  138. 'id' => 'price',
  139. 'element' => 'text',
  140. 'prefix' => $this->_defaultCurrency,
  141. 'attributes' => array(
  142. 'class' => 'form-control input-mini',
  143. ),
  144. ),
  145. array(
  146. 'id' => 'listings',
  147. 'element' => 'text',
  148. 'attributes' => array(
  149. 'class' => 'form-control input-mini',
  150. ),
  151. ),
  152. array(
  153. 'id' => 'recurring_days',
  154. 'element' => 'text',
  155. 'attributes' => array(
  156. 'class' => 'form-control input-mini',
  157. ),
  158. ),
  159. array(
  160. 'id' => 'featured_store',
  161. 'element' => 'checkbox',
  162. 'multiOptions' => array(
  163. 1 => null,
  164. ),
  165. ),
  166. array(
  167. 'id' => 'delete',
  168. 'element' => 'checkbox',
  169. ),
  170. );
  171. }
  172. /**
  173. *
  174. * fetches all matched rows
  175. *
  176. * @param string|\Cube\Db\Select $where SQL where clause, or a select object
  177. * @param string|array $order
  178. * @param int $count
  179. * @param int $offset
  180. *
  181. * @return \Ppb\Db\Table\Rowset\StoresSubscriptions
  182. */
  183. public function fetchAll($where = null, $order = null, $count = null, $offset = null)
  184. {
  185. if ($order === null) {
  186. $order = 'price ASC';
  187. }
  188. return parent::fetchAll($where, $order, $count, $offset);
  189. }
  190. /**
  191. *
  192. * save data in the table (update if an id exists or insert otherwise)
  193. *
  194. * @param array $data
  195. * @return \Ppb\Service\Table\AbstractServiceTable
  196. * @throws \InvalidArgumentException
  197. */
  198. public function save(array $data)
  199. {
  200. if (!isset($data['listings'])) {
  201. throw new \InvalidArgumentException("The form must use an element with the name 'listings'.");
  202. }
  203. foreach ($data['listings'] as $key => $value) {
  204. $data['listings'][$key] = ($value <= 0) ? 1 : $value;
  205. }
  206. return parent::save($data);
  207. }
  208. }