AbstractElements.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ UJJvyOnsRa1Ob7leNeIQm0U8KUxO8TVpQfDkFrVQSik=
  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.10 [rev.7.10.01]
  11. */
  12. namespace Ppb\Model\Elements;
  13. use Cube\Controller\Front,
  14. Cube\Translate,
  15. Cube\Translate\Adapter\AbstractAdapter as TranslateAdapter,
  16. Cube\View,
  17. Ppb\Service;
  18. abstract class AbstractElements
  19. {
  20. /**
  21. *
  22. * view object
  23. *
  24. * @var \Cube\View
  25. */
  26. protected $_view;
  27. /**
  28. *
  29. * settings array
  30. *
  31. * @var array
  32. */
  33. protected $_settings;
  34. /**
  35. *
  36. * form data
  37. *
  38. * @var array
  39. */
  40. protected $_data;
  41. /**
  42. * custom fields service
  43. *
  44. * @var \Ppb\Service\CustomFields
  45. */
  46. protected $_customFields;
  47. /**
  48. *
  49. * categories service object
  50. *
  51. * @var \Ppb\Service\Table\Relational\Categories
  52. */
  53. protected $_categories;
  54. /**
  55. *
  56. * locations table service
  57. *
  58. * @var \Ppb\Service\Table\Relational\Locations
  59. */
  60. protected $_locations;
  61. /**
  62. *
  63. * tax types table service
  64. *
  65. * @var \Ppb\Service\Table\TaxTypes
  66. */
  67. protected $_taxTypes;
  68. /**
  69. *
  70. * translate adapter
  71. *
  72. * @var \Cube\Translate\Adapter\AbstractAdapter
  73. */
  74. protected $_translate;
  75. /**
  76. *
  77. * listing types available
  78. * Default: auction, product
  79. *
  80. * @var array
  81. */
  82. protected $_listingTypes = array();
  83. /**
  84. *
  85. * class constructor
  86. *
  87. * @param array $data
  88. */
  89. public function __construct(array $data = null)
  90. {
  91. if ($data !== null) {
  92. $this->setData($data);
  93. }
  94. }
  95. /**
  96. *
  97. * get view object
  98. *
  99. * @return \Cube\View
  100. */
  101. public function getView()
  102. {
  103. if (!$this->_view instanceof View) {
  104. $this->_view = Front::getInstance()->getBootstrap()->getResource('view');
  105. }
  106. return $this->_view;
  107. }
  108. /**
  109. *
  110. * set view object
  111. *
  112. * @param \Cube\View $view
  113. *
  114. * @return $this
  115. */
  116. public function setView(View $view)
  117. {
  118. $this->_view = $view;
  119. return $this;
  120. }
  121. /**
  122. *
  123. * get settings array
  124. *
  125. * @return array
  126. */
  127. public function getSettings()
  128. {
  129. if (!is_array($this->_settings)) {
  130. $this->setSettings(
  131. Front::getInstance()->getBootstrap()->getResource('settings'));
  132. }
  133. return $this->_settings;
  134. }
  135. /**
  136. *
  137. * set settings array
  138. *
  139. * @param array $settings
  140. *
  141. * @return $this
  142. */
  143. public function setSettings(array $settings)
  144. {
  145. $this->_settings = $settings;
  146. return $this;
  147. }
  148. /**
  149. *
  150. * get form data
  151. *
  152. * @param string $key
  153. *
  154. * @return mixed
  155. */
  156. public function getData($key = null)
  157. {
  158. if ($key !== null) {
  159. if (!empty($this->_data[$key])) {
  160. return $this->_data[$key];
  161. }
  162. return null;
  163. }
  164. return $this->_data;
  165. }
  166. /**
  167. *
  168. * set form data
  169. *
  170. * @param array $data
  171. *
  172. * @return $this
  173. */
  174. public function setData($data)
  175. {
  176. $this->_data = array();
  177. foreach ((array)$data as $name => $value) {
  178. $this->addData($name, $value);
  179. }
  180. return $this;
  181. }
  182. /**
  183. *
  184. * set (update) the value of a column in the data array
  185. *
  186. * @param string $key
  187. * @param mixed $value
  188. *
  189. * @return $this
  190. */
  191. public function addData($key, $value)
  192. {
  193. $this->_data[$key] = $value;
  194. return $this;
  195. }
  196. /**
  197. *
  198. * get categories table service
  199. *
  200. * @return \Ppb\Service\Table\Relational\Categories
  201. */
  202. public function getCategories()
  203. {
  204. if (!$this->_categories instanceof Service\Table\Relational\Categories) {
  205. $this->setCategories(
  206. new Service\Table\Relational\Categories());
  207. }
  208. return $this->_categories;
  209. }
  210. /**
  211. *
  212. * set categories table service
  213. *
  214. * @param \Ppb\Service\Table\Relational\Categories $categories
  215. *
  216. * @return $this
  217. */
  218. public function setCategories(Service\Table\Relational\Categories $categories)
  219. {
  220. $this->_categories = $categories;
  221. return $this;
  222. }
  223. /**
  224. *
  225. * get custom fields service
  226. *
  227. * @return \Ppb\Service\CustomFields
  228. */
  229. public function getCustomFields()
  230. {
  231. if (!$this->_customFields instanceof Service\CustomFields) {
  232. $this->setCustomFields(
  233. new Service\CustomFields());
  234. }
  235. return $this->_customFields;
  236. }
  237. /**
  238. *
  239. * set custom fields service
  240. *
  241. * @param \Ppb\Service\CustomFields $customFields
  242. *
  243. * @return $this
  244. */
  245. public function setCustomFields(Service\CustomFields $customFields)
  246. {
  247. $this->_customFields = $customFields;
  248. return $this;
  249. }
  250. /**
  251. *
  252. * get tax types service
  253. *
  254. * @return \Ppb\Service\Table\TaxTypes
  255. */
  256. public function getTaxTypes()
  257. {
  258. if (!$this->_taxTypes instanceof Service\CustomFields) {
  259. $this->setTaxTypes(
  260. new Service\Table\TaxTypes());
  261. }
  262. return $this->_taxTypes;
  263. }
  264. /**
  265. *
  266. * set tax types service
  267. *
  268. * @param \Ppb\Service\Table\TaxTypes $taxTypes
  269. *
  270. * @return $this
  271. */
  272. public function setTaxTypes(Service\Table\TaxTypes $taxTypes)
  273. {
  274. $this->_taxTypes = $taxTypes;
  275. return $this;
  276. }
  277. /**
  278. *
  279. * set translate adapter
  280. *
  281. * @param \Cube\Translate\Adapter\AbstractAdapter $translate
  282. *
  283. * @return $this
  284. */
  285. public function setTranslate(TranslateAdapter $translate)
  286. {
  287. $this->_translate = $translate;
  288. return $this;
  289. }
  290. /**
  291. *
  292. * get translate adapter
  293. *
  294. * @return \Cube\Translate\Adapter\AbstractAdapter
  295. */
  296. public function getTranslate()
  297. {
  298. if (!$this->_translate instanceof TranslateAdapter) {
  299. $translate = Front::getInstance()->getBootstrap()->getResource('translate');
  300. if ($translate instanceof Translate) {
  301. $this->setTranslate(
  302. $translate->getAdapter());
  303. }
  304. }
  305. return $this->_translate;
  306. }
  307. /**
  308. *
  309. * get item types
  310. *
  311. * @return array
  312. */
  313. public function getListingTypes()
  314. {
  315. if (empty($this->_listingTypes)) {
  316. $this->setListingTypes();
  317. }
  318. return $this->_listingTypes;
  319. }
  320. /**
  321. *
  322. * set listing types array - proxy to listings service setListingTypes() method
  323. *
  324. * @param array $listingTypes
  325. *
  326. * @return $this
  327. */
  328. public function setListingTypes(array $listingTypes = null)
  329. {
  330. if ($listingTypes === null) {
  331. $listingsService = new Service\Listings();
  332. $listingTypes = $listingsService->getListingTypes();
  333. }
  334. $this->_listingTypes = $listingTypes;
  335. return $this;
  336. }
  337. /**
  338. *
  339. * get locations table service
  340. *
  341. * @return \Ppb\Service\Table\Relational\Locations
  342. */
  343. public function getLocations()
  344. {
  345. if (!$this->_locations instanceof Service\Table\Relational\Locations) {
  346. $this->setLocations(
  347. new Service\Table\Relational\Locations());
  348. }
  349. return $this->_locations;
  350. }
  351. /**
  352. *
  353. * set locations table service
  354. *
  355. * @param \Ppb\Service\Table\Relational\Locations $locations
  356. *
  357. * @return $this
  358. */
  359. public function setLocations(Service\Table\Relational\Locations $locations)
  360. {
  361. $this->_locations = $locations;
  362. return $this;
  363. }
  364. /**
  365. *
  366. * get the first element of an array
  367. *
  368. * @param array $array
  369. *
  370. * @return mixed
  371. */
  372. public function getFirstElement(array $array)
  373. {
  374. $array = array_keys($array);
  375. return array_shift($array);
  376. }
  377. /**
  378. *
  379. * return only the elements that match the requested condition ($element[$key] in array $value)
  380. *
  381. * @param string $key
  382. * @param array $included
  383. * @param array|null $input elements array to use
  384. *
  385. * @return array
  386. */
  387. public function getElementsWithFilter($key, array $included, $input = null)
  388. {
  389. $elements = array();
  390. if ($input === null) {
  391. $input = $this->getElements();
  392. }
  393. foreach ($input as $element) {
  394. $value = (isset($element[$key])) ? $element[$key] : null;
  395. if (array_intersect((array)$value, $included)) {
  396. array_push($elements, $element);
  397. }
  398. }
  399. return $elements;
  400. }
  401. /**
  402. *
  403. * dummy function used as a placeholder for translatable sentences
  404. *
  405. * @param $string
  406. *
  407. * @return string
  408. */
  409. protected function _($string)
  410. {
  411. return $string;
  412. }
  413. abstract public function getElements();
  414. }