AbstractService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ UN8aK+vm+N/gEjCe2ifDUemo0Rp/5wJ+JtZMHJ85Voo=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2017 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.9 [rev.7.9.02]
  11. */
  12. /**
  13. * abstract service class
  14. */
  15. namespace Ppb\Service;
  16. use Cube\Db\Table\AbstractTable,
  17. Cube\Controller\Front,
  18. Cube\Translate,
  19. Cube\Translate\Adapter\AbstractAdapter as TranslateAdapter,
  20. Ppb\Db\Table\Row\User as UserModel;
  21. abstract class AbstractService
  22. {
  23. /**
  24. *
  25. * the table that is handled through the service
  26. *
  27. * @var \Cube\Db\Table\AbstractTable
  28. */
  29. protected $_table;
  30. /**
  31. *
  32. * settings array
  33. *
  34. * @var array
  35. */
  36. protected $_settings;
  37. /**
  38. *
  39. * logged in user model / payer for site fees service classes
  40. *
  41. * @var \Ppb\Db\Table\Row\User
  42. */
  43. protected $_user;
  44. /**
  45. *
  46. * translate adapter
  47. *
  48. * @var \Cube\Translate\Adapter\AbstractAdapter
  49. */
  50. protected $_translate;
  51. /**
  52. *
  53. * cache id for query caching (column / where)
  54. *
  55. * @var mixed
  56. */
  57. protected $_cacheId = null;
  58. public function __construct()
  59. {
  60. }
  61. /**
  62. *
  63. * set the table that will be used by the service
  64. *
  65. * @param \Cube\Db\Table\AbstractTable $table
  66. *
  67. * @throws \InvalidArgumentException
  68. * @return $this
  69. */
  70. public function setTable($table = null)
  71. {
  72. if (!$table instanceof AbstractTable) {
  73. throw new \InvalidArgumentException('The table must be an instance of \Cube\Db\Table\AbstractTable');
  74. }
  75. $this->_table = $table;
  76. return $this;
  77. }
  78. /**
  79. *
  80. * get the table that is to be used by the service
  81. *
  82. * @return \Cube\Db\Table\AbstractTable
  83. */
  84. public function getTable()
  85. {
  86. return $this->_table;
  87. }
  88. /**
  89. *
  90. * get settings array
  91. *
  92. * @return array
  93. */
  94. public function getSettings()
  95. {
  96. if (!is_array($this->_settings)) {
  97. $this->setSettings(
  98. Front::getInstance()->getBootstrap()->getResource('settings'));
  99. }
  100. return $this->_settings;
  101. }
  102. /**
  103. *
  104. * set settings array
  105. *
  106. * @param array $settings
  107. *
  108. * @return $this
  109. */
  110. public function setSettings(array $settings)
  111. {
  112. $this->_settings = $settings;
  113. return $this;
  114. }
  115. /**
  116. *
  117. * get user
  118. *
  119. * @return \Ppb\Db\Table\Row\User
  120. */
  121. public function getUser()
  122. {
  123. if ($this->_user === null) {
  124. $user = Front::getInstance()->getBootstrap()->getResource('user');
  125. if ($user instanceof UserModel) {
  126. $this->setUser($user);
  127. }
  128. }
  129. return $this->_user;
  130. }
  131. /**
  132. *
  133. * set user
  134. *
  135. * @param \Ppb\Db\Table\Row\User $user
  136. *
  137. * @return $this
  138. */
  139. public function setUser($user)
  140. {
  141. $this->_user = $user;
  142. return $this;
  143. }
  144. /**
  145. *
  146. * set translate adapter
  147. *
  148. * @param \Cube\Translate\Adapter\AbstractAdapter $translate
  149. *
  150. * @return $this
  151. */
  152. public function setTranslate(TranslateAdapter $translate)
  153. {
  154. $this->_translate = $translate;
  155. return $this;
  156. }
  157. /**
  158. *
  159. * get translate adapter
  160. *
  161. * @return \Cube\Translate\Adapter\AbstractAdapter
  162. */
  163. public function getTranslate()
  164. {
  165. if (!$this->_translate instanceof TranslateAdapter) {
  166. $translate = Front::getInstance()->getBootstrap()->getResource('translate');
  167. if ($translate instanceof Translate) {
  168. $this->setTranslate(
  169. $translate->getAdapter());
  170. }
  171. }
  172. return $this->_translate;
  173. }
  174. /**
  175. *
  176. * get cache id
  177. *
  178. * @return mixed
  179. */
  180. public function getCacheId()
  181. {
  182. return $this->_cacheId;
  183. }
  184. /**
  185. *
  186. * set cache id
  187. *
  188. * @param mixed $cacheId
  189. *
  190. * @return $this
  191. */
  192. public function setCacheId($cacheId)
  193. {
  194. $this->_cacheId = $cacheId;
  195. return $this;
  196. }
  197. /**
  198. *
  199. * fetches all matched rows
  200. * is overwritten by certain service classes
  201. *
  202. * @param string|\Cube\Db\Select $where SQL where clause, or a select object
  203. * @param string|array $order
  204. * @param int $count
  205. * @param int $offset
  206. *
  207. * @return \Cube\Db\Table\Rowset\AbstractRowset
  208. */
  209. public function fetchAll($where = null, $order = null, $count = null, $offset = null)
  210. {
  211. return $this->_table->fetchAll($where, $order, $count, $offset, $this->getCacheId());
  212. }
  213. /**
  214. *
  215. * prepares data for an insert or update operation, by removing any keys that
  216. * do not correspond to columns in the selected table
  217. * serialize all arrays before saving them in the database
  218. *
  219. * @param array $data
  220. *
  221. * @return array
  222. */
  223. protected function _prepareSaveData($data = array())
  224. {
  225. foreach ($data as $key => $value) {
  226. $serialized = false;
  227. if (is_array($value)) {
  228. $value = array_filter($value);
  229. $data[$key] = (!empty($value)) ? serialize($value) : '';
  230. $serialized = true;
  231. }
  232. if (is_string($data[$key]) && !$serialized) {
  233. $data[$key] = html_entity_decode($data[$key]);
  234. }
  235. }
  236. $tableColumns = $this->getTable()->info(AbstractTable::COLS);
  237. return array_intersect_key($data, array_flip(array_values($tableColumns)));
  238. }
  239. /**
  240. *
  241. * find a row on the table by querying a certain column
  242. *
  243. * @param string $name column name
  244. * @param string $value column value
  245. *
  246. * @return \Cube\Db\Table\Row\AbstractRow|null
  247. */
  248. public function findBy($name, $value)
  249. {
  250. if ($value === null) {
  251. return null;
  252. }
  253. return $this->_table->fetchRow(
  254. $this->_table->select()->where("{$name} = ?", $value), null, null, $this->getCacheId());
  255. }
  256. /**
  257. *
  258. * round a number to the required number of decimals
  259. * multiply by 10^$round, then get the floor value of that amount then divide by 10^round
  260. *
  261. * @param float $number
  262. * @param int $decimals
  263. *
  264. * @return float
  265. */
  266. protected function _roundNumber($number, $decimals = 2)
  267. {
  268. $value = $number * pow(10, $decimals);
  269. $value = (!strpos($value, '.')) ? $value : floor($value);
  270. $number = $value / pow(10, $decimals);
  271. return $number;
  272. }
  273. /**
  274. *
  275. * dummy function used as a placeholder for translatable sentences
  276. *
  277. * @param $string
  278. *
  279. * @return mixed
  280. */
  281. protected function _($string)
  282. {
  283. return $string;
  284. }
  285. }