PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/src/classes/XLite/View/ItemsList/AItemsList.php

https://github.com/litecommerce/core
PHP | 670 lines | 496 code | 27 blank | 147 comment | 4 complexity | 66e918f477badbb3c2224b75787042b7 MD5 | raw file
  1. <?php
  2. // vim: set ts=4 sw=4 sts=4 et:
  3. /**
  4. * LiteCommerce
  5. *
  6. * NOTICE OF LICENSE
  7. *
  8. * This source file is subject to the Open Software License (OSL 3.0)
  9. * that is bundled with this package in the file LICENSE.txt.
  10. * It is also available through the world-wide-web at this URL:
  11. * http://opensource.org/licenses/osl-3.0.php
  12. * If you did not receive a copy of the license and are unable to
  13. * obtain it through the world-wide-web, please send an email
  14. * to licensing@litecommerce.com so we can send you a copy immediately.
  15. *
  16. * PHP version 5.3.0
  17. *
  18. * @category LiteCommerce
  19. * @author Creative Development LLC <info@cdev.ru>
  20. * @copyright Copyright (c) 2011-2012 Creative Development LLC <info@cdev.ru>. All rights reserved
  21. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  22. * @link http://www.litecommerce.com/
  23. */
  24. namespace XLite\View\ItemsList;
  25. /**
  26. * Base class for all lists
  27. *
  28. */
  29. abstract class AItemsList extends \XLite\View\Container
  30. {
  31. /**
  32. * Widget param names
  33. */
  34. const PARAM_SORT_BY = 'sortBy';
  35. const PARAM_SORT_ORDER = 'sortOrder';
  36. /**
  37. * SQL orderby directions
  38. */
  39. const SORT_ORDER_ASC = 'asc';
  40. const SORT_ORDER_DESC = 'desc';
  41. /**
  42. * Default layout template
  43. *
  44. * @var string
  45. */
  46. protected $defaultTemplate = 'common/dialog.tpl';
  47. /**
  48. * commonParams
  49. *
  50. * @var array
  51. */
  52. protected $commonParams;
  53. /**
  54. * pager
  55. *
  56. * @var \XLite\View\Pager\APager
  57. */
  58. protected $pager;
  59. /**
  60. * itemsCount
  61. *
  62. * @var integer
  63. */
  64. protected $itemsCount;
  65. /**
  66. * sortByModes
  67. *
  68. * @var array
  69. */
  70. protected $sortByModes = array();
  71. /**
  72. * sortOrderModes
  73. *
  74. * @var array
  75. */
  76. protected $sortOrderModes = array(
  77. self::SORT_ORDER_ASC => 'Ascending',
  78. self::SORT_ORDER_DESC => 'Descending',
  79. );
  80. /**
  81. * Sorting widget IDs list
  82. *
  83. * @var array
  84. */
  85. protected static $sortWidgetIds = array();
  86. /**
  87. * Return dir which contains the page body template
  88. *
  89. * @return string
  90. */
  91. abstract protected function getPageBodyDir();
  92. /**
  93. * Return class name for the list pager
  94. *
  95. * @return string
  96. */
  97. abstract protected function getPagerClass();
  98. /**
  99. * Return products list
  100. *
  101. * @param \XLite\Core\CommonCell $cnd Search condition
  102. * @param boolean $countOnly Return items list or only its size OPTIONAL
  103. *
  104. * @return array|integer
  105. */
  106. abstract protected function getData(\XLite\Core\CommonCell $cnd, $countOnly = false);
  107. /**
  108. * Get session cell name for the certain list items widget
  109. *
  110. * @return string
  111. */
  112. static public function getSessionCellName()
  113. {
  114. return str_replace('\\', '', get_called_class());
  115. }
  116. /**
  117. * Initialize widget (set attributes)
  118. *
  119. * @param array $params Widget params
  120. *
  121. * @return void
  122. */
  123. public function setWidgetParams(array $params)
  124. {
  125. parent::setWidgetParams($params);
  126. // Do not change call order
  127. $this->widgetParams += $this->getPager()->getWidgetParams();
  128. $this->requestParams = array_merge($this->requestParams, $this->getPager()->getRequestParams());
  129. }
  130. /**
  131. * getActionURL
  132. *
  133. * @param array $params Params to modify OPTIONAL
  134. *
  135. * @return string
  136. */
  137. public function getActionURL(array $params = array())
  138. {
  139. return $this->getURL($params + $this->getURLParams());
  140. }
  141. /**
  142. * Get a list of JavaScript files
  143. *
  144. * @return array
  145. */
  146. public function getJSFiles()
  147. {
  148. $list = parent::getJSFiles();
  149. // Static call of the non-static function
  150. $list[] = self::getDir() . '/items_list.js';
  151. return $list;
  152. }
  153. /**
  154. * Register files from common repository
  155. *
  156. * @return array
  157. */
  158. public function getCommonFiles()
  159. {
  160. $list = parent::getCommonFiles();
  161. $list['js'][] = 'js/jquery.blockUI.js';
  162. return $list;
  163. }
  164. /**
  165. * Get a list of CSS files
  166. *
  167. * @return array
  168. */
  169. public function getCSSFiles()
  170. {
  171. $list = parent::getCSSFiles();
  172. // Static call of the non-static function
  173. $list[] = self::getDir() . '/items_list.css';
  174. $list = self::preparePagerCSSFiles($list);
  175. return $list;
  176. }
  177. /**
  178. * Returns a list of CSS classes (separated with a space character) to be attached to the items list
  179. *
  180. * @return string
  181. */
  182. public function getListCSSClasses()
  183. {
  184. return 'items-list';
  185. }
  186. /**
  187. * Return inner head for list widgets
  188. *
  189. * @return string
  190. */
  191. protected function getListHead()
  192. {
  193. return parent::getHead();
  194. }
  195. /**
  196. * Return number of items in products list
  197. *
  198. * @return array
  199. */
  200. protected function getItemsCount()
  201. {
  202. if (!isset($this->itemsCount)) {
  203. $this->itemsCount = $this->getData($this->getSearchCondition(), true);
  204. }
  205. return $this->itemsCount;
  206. }
  207. /**
  208. * Return name of the base widgets list
  209. *
  210. * @return string
  211. */
  212. protected function getListName()
  213. {
  214. return 'itemsList';
  215. }
  216. /**
  217. * Get widget templates directory
  218. * NOTE: do not use "$this" pointer here (see "getBody()" and "get[CSS/JS]Files()")
  219. *
  220. * @return string
  221. */
  222. protected function getDir()
  223. {
  224. return 'items_list';
  225. }
  226. /**
  227. * prepare CSS file list for use with pager
  228. *
  229. * @param array $list CSS file list
  230. *
  231. * @return array
  232. */
  233. protected function preparePagerCSSFiles($list)
  234. {
  235. return array_merge($list, self::getPager()->getCSSFiles());
  236. }
  237. /**
  238. * Return file name for the center part template
  239. *
  240. * @return string
  241. */
  242. protected function getBody()
  243. {
  244. // Static call of the non-static function
  245. return self::getDir() . LC_DS . $this->getBodyTemplate();
  246. }
  247. /**
  248. * Return default template
  249. * See setWidgetParams()
  250. *
  251. * @return string
  252. */
  253. protected function getDefaultTemplate()
  254. {
  255. return $this->defaultTemplate;
  256. }
  257. /**
  258. * getPageBodyTemplate
  259. *
  260. * @return string
  261. */
  262. protected function getPageBodyTemplate()
  263. {
  264. return $this->getDir() . LC_DS . $this->getPageBodyDir() . LC_DS . $this->getPageBodyFile();
  265. }
  266. /**
  267. * getPageBodyFile
  268. *
  269. * @return string
  270. */
  271. protected function getPageBodyFile()
  272. {
  273. return 'body.tpl';
  274. }
  275. /**
  276. * getEmptyListTemplate
  277. *
  278. * @return string
  279. */
  280. protected function getEmptyListTemplate()
  281. {
  282. return $this->getEmptyListDir() . LC_DS . $this->getEmptyListFile();
  283. }
  284. /**
  285. * Return "empty list" catalog
  286. *
  287. * @return string
  288. */
  289. protected function getEmptyListDir()
  290. {
  291. return self::getDir();
  292. }
  293. /**
  294. * getEmptyListFile
  295. *
  296. * @return string
  297. */
  298. protected function getEmptyListFile()
  299. {
  300. return 'empty.tpl';
  301. }
  302. /**
  303. * isEmptyListTemplateVisible
  304. *
  305. * @return string
  306. */
  307. protected function isEmptyListTemplateVisible()
  308. {
  309. return false === $this->hasResults();
  310. }
  311. /**
  312. * Get pager parameters list
  313. *
  314. * @return array
  315. */
  316. protected function getPagerParams()
  317. {
  318. return array(
  319. \XLite\View\Pager\APager::PARAM_ITEMS_COUNT => $this->getItemsCount(),
  320. \XLite\View\Pager\APager::PARAM_LIST => $this,
  321. );
  322. }
  323. /**
  324. * Get pager
  325. *
  326. * @return \XLite\View\Pager\APager
  327. */
  328. protected function getPager()
  329. {
  330. if (!isset($this->pager)) {
  331. $this->pager = $this->getWidget($this->getPagerParams(), $this->getPagerClass());
  332. }
  333. return $this->pager;
  334. }
  335. /**
  336. * Return params list to use for search
  337. *
  338. * @return \XLite\Core\CommonCell
  339. */
  340. protected function getSearchCondition()
  341. {
  342. return new \XLite\Core\CommonCell();
  343. }
  344. /**
  345. * getPageData
  346. *
  347. * @return array
  348. */
  349. protected function getPageData()
  350. {
  351. return $this->getData($this->getPager()->getLimitCondition(null, null, $this->getSearchCondition()));
  352. }
  353. /**
  354. * getSortOrderDefault
  355. *
  356. * @return string
  357. */
  358. protected function getSortOrderModeDefault()
  359. {
  360. return self::SORT_ORDER_ASC;
  361. }
  362. /**
  363. * getSortByModeDefault
  364. *
  365. * @return string
  366. */
  367. protected function getSortByModeDefault()
  368. {
  369. return null;
  370. }
  371. /**
  372. * getSortBy
  373. *
  374. * @return string
  375. */
  376. protected function getSortBy()
  377. {
  378. return $this->getParam(self::PARAM_SORT_BY);
  379. }
  380. /**
  381. * getSortOrder
  382. *
  383. * @return string
  384. */
  385. protected function getSortOrder()
  386. {
  387. return $this->getParam(self::PARAM_SORT_ORDER);
  388. }
  389. /**
  390. * Define widget parameters
  391. *
  392. * @return void
  393. */
  394. protected function defineWidgetParams()
  395. {
  396. parent::defineWidgetParams();
  397. if (!empty($this->sortByModes)) {
  398. $this->widgetParams += array(
  399. self::PARAM_SORT_BY => new \XLite\Model\WidgetParam\Set(
  400. 'Sort by', $this->getSortByModeDefault(), false, $this->sortByModes
  401. ),
  402. self::PARAM_SORT_ORDER => new \XLite\Model\WidgetParam\Set(
  403. 'Sort order', $this->getSortOrderModeDefault(), false, $this->sortOrderModes
  404. ),
  405. );
  406. }
  407. }
  408. /**
  409. * getJSHandlerClassName
  410. *
  411. * @return string
  412. */
  413. protected function getJSHandlerClassName()
  414. {
  415. return 'ItemsList';
  416. }
  417. /**
  418. * Get URL common parameters
  419. *
  420. * @return array
  421. */
  422. protected function getCommonParams()
  423. {
  424. if (!isset($this->commonParams)) {
  425. $this->commonParams = array(
  426. self::PARAM_SESSION_CELL => $this->getSessionCell()
  427. );
  428. }
  429. return $this->commonParams;
  430. }
  431. /**
  432. * Get AJAX-specific URL parameters
  433. *
  434. * @return array
  435. */
  436. protected function getAJAXSpecificParams()
  437. {
  438. return array(
  439. self::PARAM_AJAX_WIDGET => get_class($this),
  440. self::PARAM_AJAX_TARGET => \XLite\Core\Request::getInstance()->target,
  441. );
  442. }
  443. /**
  444. * getURLParams
  445. *
  446. * @return array
  447. */
  448. protected function getURLParams()
  449. {
  450. return array('target' => \XLite\Core\Request::getInstance()->target) + $this->getCommonParams();
  451. }
  452. /**
  453. * getURLAJAXParams
  454. *
  455. * @return array
  456. */
  457. protected function getURLAJAXParams()
  458. {
  459. return $this->getCommonParams() + $this->getAJAXSpecificParams();
  460. }
  461. /**
  462. * Return specific items list parameters that will be sent to JS code
  463. *
  464. * @return array
  465. */
  466. protected function getItemsListParams()
  467. {
  468. return array(
  469. 'urlparams' => $this->getURLParams(),
  470. 'urlajaxparams' => $this->getURLAJAXParams(),
  471. 'cell' => $this->getSessionCell(),
  472. );
  473. }
  474. /**
  475. * Get sorting widget unique ID
  476. *
  477. * @param boolean $getLast Get last ID or next OPTIONAL
  478. *
  479. * @return string
  480. */
  481. protected function getSortWidgetId($getLast = false)
  482. {
  483. $class = get_called_class();
  484. if (!isset(static::$sortWidgetIds[$class])) {
  485. static::$sortWidgetIds[$class] = 0;
  486. }
  487. if (!$getLast) {
  488. static::$sortWidgetIds[$class]++;
  489. }
  490. return str_replace('\\', '-', $class) . '-sortby-' . static::$sortWidgetIds[$class];
  491. }
  492. /**
  493. * isSortByModeSelected
  494. *
  495. * @param string $sortByMode Value to check
  496. *
  497. * @return boolean
  498. */
  499. protected function isSortByModeSelected($sortByMode)
  500. {
  501. return $this->getParam(self::PARAM_SORT_BY) == $sortByMode;
  502. }
  503. /**
  504. * isSortOrderAsc
  505. *
  506. * @return boolean
  507. */
  508. protected function isSortOrderAsc()
  509. {
  510. return self::SORT_ORDER_ASC == $this->getParam(self::PARAM_SORT_ORDER);
  511. }
  512. /**
  513. * getSortOrderToChange
  514. *
  515. * @return string
  516. */
  517. protected function getSortOrderToChange()
  518. {
  519. return $this->isSortOrderAsc() ? self::SORT_ORDER_DESC : self::SORT_ORDER_ASC;
  520. }
  521. /**
  522. * Check if widget is visible
  523. *
  524. * @return boolean
  525. */
  526. protected function isVisible()
  527. {
  528. return parent::isVisible() && ($this->isDisplayWithEmptyList() || $this->hasResults());
  529. }
  530. /**
  531. * Auxiliary method to check visibility
  532. *
  533. * @return boolean
  534. */
  535. protected function isDisplayWithEmptyList()
  536. {
  537. return false;
  538. }
  539. /**
  540. * Check if there are any results to display in list
  541. *
  542. * @return void
  543. */
  544. protected function hasResults()
  545. {
  546. return 0 < $this->getItemsCount();
  547. }
  548. /**
  549. * isHeaderVisible
  550. *
  551. * @return boolean
  552. */
  553. protected function isHeaderVisible()
  554. {
  555. return false;
  556. }
  557. /**
  558. * Check if head title is visible
  559. *
  560. * @return boolean
  561. */
  562. protected function isHeadVisible()
  563. {
  564. return false;
  565. }
  566. /**
  567. * Check if pager is visible
  568. *
  569. * @return boolean
  570. */
  571. protected function isPagerVisible()
  572. {
  573. return $this->getPager()->isVisible();
  574. }
  575. /**
  576. * isFooterVisible
  577. *
  578. * @return boolean
  579. */
  580. protected function isFooterVisible()
  581. {
  582. return false;
  583. }
  584. /**
  585. * Define so called "request" parameters
  586. *
  587. * @return void
  588. */
  589. protected function defineRequestParams()
  590. {
  591. parent::defineRequestParams();
  592. $this->requestParams[] = self::PARAM_SORT_BY;
  593. $this->requestParams[] = self::PARAM_SORT_ORDER;
  594. }
  595. }