CookieUsage.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ 2Easa4v10DL30CvKSKzQvSSE/3edmfyhxQpu/rOIvtw=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2014 Online Ventures Software LTD & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.2
  11. */
  12. /**
  13. * cookie usage view helper class
  14. */
  15. namespace Ppb\View\Helper;
  16. use Cube\Controller\Front,
  17. Cube\View\Helper\AbstractHelper;
  18. class CookieUsage extends AbstractHelper
  19. {
  20. /**
  21. * cookie name
  22. */
  23. const COOKIE_USAGE = 'CookieUsage';
  24. /**
  25. *
  26. * settings array
  27. *
  28. * @var array
  29. */
  30. protected $_settings;
  31. public function __construct(array $settings)
  32. {
  33. $this->setSettings($settings);
  34. }
  35. /**
  36. *
  37. * set settings array
  38. *
  39. * @param array $settings
  40. * @return $this
  41. */
  42. public function setSettings(array $settings)
  43. {
  44. $this->_settings = $settings;
  45. return $this;
  46. }
  47. /**
  48. *
  49. * render cookie usage confirmation message
  50. *
  51. * @return null|string
  52. */
  53. public function cookieUsage()
  54. {
  55. $output = null;
  56. if ($this->_settings['enable_cookie_usage_confirmation']) {
  57. $view = $this->getView();
  58. $bootstrap = Front::getInstance()->getBootstrap();
  59. /** @var \Cube\Session $session */
  60. $session = $bootstrap->getResource('session');
  61. if (!$session->getCookie(self::COOKIE_USAGE)) {
  62. /** @var \Cube\View\Helper\Script $helper */
  63. $helper = $view->getHelper('script');
  64. $cookieKey = $session->getCookieKey(self::COOKIE_USAGE);
  65. $cookiePath = (!empty($view->baseUrl)) ? $view->baseUrl : $view::URI_DELIMITER;
  66. $helper->addBodyCode('<script type="text/javascript" src="' . $view->baseUrl . '/js/cookie.js"></script>')
  67. ->addBodyCode("
  68. <script type=\"text/javascript\">
  69. $('.btn-cookie-confirm').on('click', function() {
  70. $.cookie('" . $cookieKey . "', '1', {path: '" . $cookiePath . "', expires: 30});
  71. $('.cookie-usage').remove();
  72. });
  73. </script>");
  74. $translate = $this->getTranslate();
  75. $output = '<div class="cookie-usage">
  76. <div class="row">
  77. <div class="col-sm-10">' . $translate->_($this->_settings['cookie_usage_message']) . '</div>
  78. <div class="col-sm-2 text-right">
  79. <button class="btn btn-sm btn-primary btn-cookie-confirm">' . $translate->_('I Understand') . '</button>
  80. </div>
  81. </div>
  82. </div>';
  83. }
  84. }
  85. return $output;
  86. }
  87. }