PageRenderTime 24ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/QuickAccess.php

https://bitbucket.org/fanch1/testlb
PHP | 137 lines | 68 code | 17 blank | 52 comment | 10 complexity | 8bced17f00d59a08a9ff218a58fd56f1 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, BSD-2-Clause, GPL-2.0, GPL-3.0
  1. <?php
  2. /**
  3. * 2007-2017 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * https://opensource.org/licenses/OSL-3.0
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@prestashop.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <contact@prestashop.com>
  22. * @copyright 2007-2017 PrestaShop SA
  23. * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26. /**
  27. * Class QuickAccessCore
  28. */
  29. class QuickAccessCore extends ObjectModel
  30. {
  31. /** @var string Name */
  32. public $name;
  33. /** @var string Link */
  34. public $link;
  35. /** @var bool New windows or not */
  36. public $new_window;
  37. /**
  38. * @see ObjectModel::$definition
  39. */
  40. public static $definition = array(
  41. 'table' => 'quick_access',
  42. 'primary' => 'id_quick_access',
  43. 'multilang' => true,
  44. 'fields' => array(
  45. 'link' => array('type' => self::TYPE_STRING, 'validate' => 'isUrl', 'required' => true, 'size' => 255),
  46. 'new_window' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true),
  47. /* Lang fields */
  48. 'name' => array('type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isCleanHtml', 'required' => true, 'size' => 32),
  49. ),
  50. );
  51. /**
  52. * Get all available quick_accesses
  53. *
  54. * @return array QuickAccesses
  55. */
  56. public static function getQuickAccesses($idLang)
  57. {
  58. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  59. SELECT *
  60. FROM `'._DB_PREFIX_.'quick_access` qa
  61. LEFT JOIN `'._DB_PREFIX_.'quick_access_lang` qal ON (qa.`id_quick_access` = qal.`id_quick_access` AND qal.`id_lang` = '.(int) $idLang.')
  62. ORDER BY `name` ASC');
  63. }
  64. /**
  65. * Get all available quick_accesses with token
  66. *
  67. * @return array QuickAccesses
  68. */
  69. public static function getQuickAccessesWithToken($idLang, $idEmployee)
  70. {
  71. $quickAccess = self::getQuickAccesses($idLang);
  72. if (empty($quickAccess)) {
  73. return false;
  74. }
  75. foreach ($quickAccess as $index => $quick) {
  76. // first, clean url to have a real quickLink
  77. $quick['link'] = Context::getContext()->link->getQuickLink($quick['link']);
  78. $tokenString = $idEmployee;
  79. if ('../' === $quick['link'] && Shop::getContext() == Shop::CONTEXT_SHOP) {
  80. $url = Context::getContext()->shop->getBaseURL();
  81. if (!$url) {
  82. unset($quickAccess[$index]);
  83. continue;
  84. }
  85. $quickAccess[$index]['link'] = $url;
  86. } else {
  87. preg_match('/controller=(.+)(&.+)?$/', $quick['link'], $admin_tab);
  88. if (isset($admin_tab[1])) {
  89. if (strpos($admin_tab[1], '&')) {
  90. $admin_tab[1] = substr($admin_tab[1], 0, strpos($admin_tab[1], '&'));
  91. }
  92. $quick_access[$index]['target'] = $admin_tab[1];
  93. $tokenString = $admin_tab[1].(int)Tab::getIdFromClassName($admin_tab[1]).$idEmployee;
  94. }
  95. $quickAccess[$index]['link'] = Context::getContext()->link->getBaseLink().basename(_PS_ADMIN_DIR_).'/'.$quick['link'];
  96. }
  97. if (false === strpos($quickAccess[$index]['link'], 'token')) {
  98. $separator = strpos($quickAccess[$index]['link'], '?') ? '&' : '?';
  99. $quickAccess[$index]['link'] .= $separator.'token='.Tools::getAdminToken($tokenString);
  100. }
  101. }
  102. return $quickAccess;
  103. }
  104. /**
  105. * Toggle new window
  106. *
  107. * @return bool
  108. *
  109. * @throws PrestaShopException
  110. */
  111. public function toggleNewWindow()
  112. {
  113. if (!array_key_exists('new_window', $this)) {
  114. throw new PrestaShopException('property "new_window" is missing in object '.get_class($this));
  115. }
  116. $this->setFieldsToUpdate(array('new_window' => true));
  117. $this->new_window = !(int) $this->new_window;
  118. return $this->update(false);
  119. }
  120. }