AbstractServiceTableRelational.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ izFniGuTYYgvSTMRkxOADFvrdtUdSmJmMzmNTBopws61I8WxlNii41cj2YI6zRHyhyTyH6s5azNt5yP1XKfTGw==
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2015 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.5
  11. */
  12. /**
  13. * abstract service class for data tables that have a relational structure (one to many)
  14. */
  15. namespace Ppb\Service\Table\Relational;
  16. use Ppb\Service\Table\AbstractServiceTable;
  17. abstract class AbstractServiceTableRelational extends AbstractServiceTable
  18. {
  19. const NAME_SEPARATOR = ' :: ';
  20. /**
  21. *
  22. * locations table navigation object
  23. *
  24. * @var \Cube\Navigation
  25. */
  26. protected $_data;
  27. /**
  28. *
  29. * parent id field
  30. *
  31. * @var integer
  32. */
  33. protected $_parentId;
  34. /**
  35. *
  36. * column to check against when adding rows
  37. *
  38. * @var string
  39. */
  40. protected $_mainColumn = 'name';
  41. /**
  42. *
  43. * get parent id
  44. *
  45. * @return integer
  46. */
  47. public function getParentId()
  48. {
  49. return $this->_parentId;
  50. }
  51. /**
  52. *
  53. * set parent id
  54. *
  55. * @param integer $parentId
  56. *
  57. * @return $this
  58. */
  59. public function setParentId($parentId)
  60. {
  61. $this->_parentId = (int)$parentId;
  62. return $this;
  63. }
  64. /**
  65. *
  66. * get locations table data
  67. *
  68. * @return \Cube\Navigation
  69. */
  70. public function getData()
  71. {
  72. if (empty($this->_data)) {
  73. $this->setData();
  74. }
  75. return $this->_data;
  76. }
  77. /**
  78. *
  79. * save data in the table (update if an id exists or insert otherwise)
  80. *
  81. *
  82. * @param array $data
  83. *
  84. * @return \Ppb\Service\Table\AbstractServiceTable
  85. * @throws \InvalidArgumentException
  86. */
  87. public function save(array $data)
  88. {
  89. if (!empty($data['parent_id'])) {
  90. $parentId = (string)$data['parent_id'];
  91. unset($data['parent_id']);
  92. foreach ($data['id'] as $key => $value) {
  93. $data['parent_id'][$key] = $parentId;
  94. }
  95. }
  96. if (!empty($data['user_id'])) {
  97. $userId = (string)$data['user_id'];
  98. unset($data['user_id']);
  99. foreach ($data['id'] as $key => $value) {
  100. $data['user_id'][$key] = $userId;
  101. }
  102. }
  103. return parent::save($data);
  104. }
  105. /**
  106. *
  107. * fetches all matched rows
  108. *
  109. * @param string|\Cube\Db\Select $where SQL where clause, or a select object
  110. * @param string|array $order
  111. * @param int $count
  112. * @param int $offset
  113. *
  114. * @return \Cube\Db\Table\Rowset\AbstractRowset
  115. */
  116. public function fetchAll($where = null, $order = null, $count = null, $offset = null)
  117. {
  118. if ($where === null) {
  119. $where = 'parent_id IS NULL';
  120. }
  121. if ($order === null) {
  122. $order = 'order_id ASC, name ASC';
  123. }
  124. return parent::fetchAll($where, $order, $count, $offset);
  125. }
  126. /**
  127. *
  128. * get multi options array
  129. * translation sentences are inserted dynamically in the language array
  130. *
  131. * @param string|\Cube\Db\Select $where SQL where clause, or a select object
  132. * @param string|array $order custom results ordering
  133. * @param bool|string $default whether to add a default value field in the data
  134. * @param bool $fullName display full branch name (with parents)
  135. *
  136. * @return array
  137. */
  138. public function getMultiOptions($where = null, $order = null, $default = false, $fullName = false)
  139. {
  140. $data = array();
  141. $translate = $this->getTranslate();
  142. if ($default !== false) {
  143. $data[] = $default;
  144. }
  145. $rows = $this->_table->fetchAll($where, $order);
  146. foreach ($rows as $row) {
  147. $categoryName = null;
  148. if ($fullName === true) {
  149. $parts = array();
  150. if (!empty($fullName)) {
  151. $parts = explode(self::NAME_SEPARATOR, $row['full_name']);
  152. }
  153. foreach ((array) $parts as $key => $part) {
  154. $parts[$key] = $translate->_($part);
  155. }
  156. $categoryName = implode(self::NAME_SEPARATOR, $parts);
  157. }
  158. if (empty($categoryName)) {
  159. $categoryName = $translate->_($row['name']);
  160. }
  161. $data[(int)$row['id']] = $categoryName;
  162. }
  163. return $data;
  164. }
  165. /**
  166. *
  167. * get breadcrumbs array
  168. * fetch directly from database
  169. * 7.2 - added a fix which was causing this method to loop indefinitely if a category that was to be counted didn't exist
  170. *
  171. * @param $id
  172. *
  173. * @return array
  174. */
  175. public function getBreadcrumbs($id)
  176. {
  177. $breadcrumbs = array();
  178. if ($id) {
  179. $translate = $this->getTranslate();
  180. do {
  181. $row = $this->findBy('id', $id);
  182. $id = 0;
  183. if (count($row) > 0) {
  184. $breadcrumbs[$row['id']] = $translate->_($row['name']);
  185. $id = $row['parent_id'];
  186. }
  187. } while ($id > 0);
  188. }
  189. return array_reverse($breadcrumbs, true);
  190. }
  191. /**
  192. *
  193. * get children array
  194. * fetch directly from database
  195. *
  196. * @param int $id parent id
  197. * @param bool $root
  198. *
  199. * @return array
  200. */
  201. public function getChildren($id, $root = false)
  202. {
  203. $children = array();
  204. if ($id) {
  205. $translate = $this->getTranslate();
  206. $ids = array($id);
  207. if ($root) {
  208. $row = $this->findBy('id', $id);
  209. $children[$row['id']] = $translate->_($row['name']);
  210. }
  211. do {
  212. if ($ids) {
  213. $select = $this->getTable()->select()
  214. ->where('parent_id IN (?)', $ids);
  215. $categories = $this->fetchAll($select);
  216. $ids = array();
  217. foreach ($categories as $row) {
  218. $ids[] = $row['id'];
  219. $children[$row['id']] = $translate->_($row['name']);
  220. }
  221. }
  222. } while (count($ids) > 0);
  223. }
  224. return $children;
  225. }
  226. /**
  227. *
  228. * get the root object of a certain id
  229. *
  230. * @param $id
  231. *
  232. * @return \Cube\Db\Table\Row\AbstractRow
  233. */
  234. public function getRoot($id)
  235. {
  236. do {
  237. $row = $this->findBy('id', $id);
  238. $id = (!empty($row['parent_id'])) ? $row['parent_id'] : null;
  239. } while ($id !== null);
  240. return $row;
  241. }
  242. /**
  243. *
  244. * get full branch name, including parents
  245. *
  246. * @param int $id
  247. * @param string $separator
  248. *
  249. * @return string|null
  250. */
  251. public function getFullName($id, $separator = null)
  252. {
  253. if ($separator === null) {
  254. $separator = self::NAME_SEPARATOR;
  255. }
  256. $name = implode($separator, array_values($this->getBreadcrumbs($id)));
  257. return (!empty($name)) ? $name : null;
  258. }
  259. /**
  260. *
  261. * the method will create a navigation tree
  262. *
  263. * @param $list
  264. * @param $parent
  265. *
  266. * @return array
  267. */
  268. protected function _createTree(&$list, $parent)
  269. {
  270. $tree = array();
  271. foreach ($parent as $row) {
  272. if (isset($list[$row['id']])) {
  273. $row['pages'] = $this->_createTree($list, $list[$row['id']]);
  274. }
  275. $tree[] = $row;
  276. }
  277. return $tree;
  278. }
  279. /**
  280. *
  281. * set table data.
  282. * This data will be used for traversing the navigation tree
  283. *
  284. * @param string|\Cube\Db\Select $where SQL where clause, or a select object
  285. * @param array|\Traversable $data Optional. Custom categories data
  286. *
  287. * @return $this
  288. */
  289. abstract public function setData($where = null, array $data = null);
  290. }