Locations.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ DZD0dqqhjryrP/UpMNg7m6WMSXozSETJnFhVQsVnKO8=
  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. * locations table service class
  14. * TODO: add children like on the categories service
  15. */
  16. namespace Ppb\Service\Table\Relational;
  17. use Cube\Db\Select,
  18. Ppb\Db\Table\Locations as LocationsTable,
  19. Cube\Navigation;
  20. class Locations extends AbstractServiceTableRelational
  21. {
  22. /**
  23. *
  24. * locations table navigation object
  25. *
  26. * @var \Cube\Navigation
  27. */
  28. protected $_data;
  29. /**
  30. *
  31. * class constructor
  32. */
  33. public function __construct()
  34. {
  35. parent::__construct();
  36. $this->setInsertRows(3)
  37. ->setTable(
  38. new LocationsTable());
  39. }
  40. /**
  41. *
  42. * set locations table data.
  43. * This data will be used for traversing the locations tree
  44. *
  45. * @param string|\Cube\Db\Select $where SQL where clause, or a select object
  46. * @param array|\Traversable $data Optional. Custom categories data
  47. *
  48. * @return $this
  49. */
  50. public function setData($where = null, array $data = null)
  51. {
  52. if ($data === null) {
  53. $locations = $this->_table->fetchAll($where);
  54. $data = array();
  55. foreach ($locations as $row) {
  56. $data[$row['parent_id']][] = array(
  57. 'className' => '\Ppb\Navigation\Page\Location',
  58. 'id' => (int)$row['id'],
  59. 'label' => $row['name'],
  60. 'order' => (int)$row['order_id'],
  61. 'isoCode' => $row['iso_code'],
  62. );
  63. }
  64. reset($data);
  65. $tree = $this->_createTree($data, current($data));
  66. $this->_data = new Navigation($tree);
  67. }
  68. else {
  69. $this->_data = $data;
  70. }
  71. return $this;
  72. }
  73. /**
  74. *
  75. * get all table columns needed to generate the
  76. * locations management table in the admin area
  77. *
  78. * @return array
  79. */
  80. public function getColumns()
  81. {
  82. return array(
  83. array(
  84. 'label' => '',
  85. 'class' => 'size-tiny',
  86. 'element_id' => null,
  87. 'children' => array(
  88. 'key' => 'parent_id',
  89. 'value' => 'id',
  90. ),
  91. ),
  92. array(
  93. 'label' => $this->_('Name'),
  94. 'element_id' => 'name',
  95. ),
  96. array(
  97. 'label' => $this->_('ISO Code'),
  98. 'class' => 'size-mini',
  99. 'element_id' => 'iso_code',
  100. ),
  101. array(
  102. 'label' => $this->_('Order ID'),
  103. 'class' => 'size-mini',
  104. 'element_id' => 'order_id',
  105. ),
  106. array(
  107. 'label' => $this->_('Delete'),
  108. 'class' => 'size-mini',
  109. 'element_id' => array(
  110. 'id', 'delete'
  111. ),
  112. ),
  113. );
  114. }
  115. /**
  116. *
  117. * get all form elements that are needed to generate the
  118. * locations management table in the admin area
  119. *
  120. * @return array
  121. */
  122. public function getElements()
  123. {
  124. return array(
  125. array(
  126. 'id' => 'id',
  127. 'element' => 'hidden',
  128. ),
  129. array(
  130. 'id' => 'name',
  131. 'element' => 'text',
  132. 'attributes' => array(
  133. 'class' => 'form-control input-large',
  134. ),
  135. ),
  136. array(
  137. 'id' => 'iso_code',
  138. 'element' => 'text',
  139. 'attributes' => array(
  140. 'class' => 'form-control input-mini',
  141. ),
  142. ),
  143. array(
  144. 'id' => 'order_id',
  145. 'element' => 'text',
  146. 'attributes' => array(
  147. 'class' => 'form-control input-mini',
  148. ),
  149. ),
  150. array(
  151. 'id' => 'delete',
  152. 'element' => 'checkbox',
  153. ),
  154. );
  155. }
  156. /**
  157. *
  158. * get locations multi options array
  159. *
  160. * @param string|array|\Cube\Db\Select $where SQL where clause, a select object, or an array of ids
  161. * @param string|array $order
  162. * @param bool $default whether to add a default value field in the data
  163. * @param bool $fullName display full branch name (with parents)
  164. *
  165. * @return array
  166. */
  167. public function getMultiOptions($where = null, $order = null, $default = false, $fullName = false)
  168. {
  169. if (!$where instanceof Select) {
  170. $select = $this->_table->select()
  171. ->order(array('order_id ASC', 'name ASC'));
  172. if ($where !== null) {
  173. if (is_array($where)) {
  174. $select->where("id IN (?)", $where);
  175. }
  176. else {
  177. $select->where("parent_id = ?", $where);
  178. }
  179. }
  180. else {
  181. $select->where('parent_id is null');
  182. }
  183. $where = $select;
  184. }
  185. return parent::getMultiOptions($where, $order, $default, $fullName);
  186. }
  187. }