ContentSections.php 6.5 KB

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