AbstractAction.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ CBEPj2TttQrY75HSDT6Q9fJrsTTxInXqMo0cRpSl3jY=
  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.0
  11. */
  12. /**
  13. * the class will be extended by every controller in all modules
  14. */
  15. namespace Ppb\Controller\Action;
  16. use Cube\Controller\Action\AbstractAction as CubeAbstractAction,
  17. Cube\Controller\Action\Helper,
  18. Cube\Controller\Request\AbstractRequest,
  19. Cube\Controller\Response\ResponseInterface,
  20. Cube\Authentication\Authentication,
  21. Cube\Controller\Front,
  22. Cube\Translate,
  23. Cube\Translate\Adapter\AbstractAdapter as TranslateAdapter,
  24. Ppb\Service\Users as UsersService;
  25. abstract class AbstractAction extends CubeAbstractAction
  26. {
  27. /**
  28. *
  29. * flash messenger helper
  30. *
  31. * @var \Cube\Controller\Action\Helper\FlashMessenger
  32. */
  33. protected $_flashMessenger;
  34. /**
  35. *
  36. * logged in user
  37. *
  38. * @var \Ppb\Db\Table\Row\User
  39. */
  40. protected $_user = null;
  41. /**
  42. *
  43. * settings array
  44. *
  45. * @var array
  46. */
  47. protected $_settings;
  48. /**
  49. *
  50. * translate adapter
  51. *
  52. * @var \Cube\Translate\Adapter\AbstractAdapter
  53. */
  54. protected $_translate;
  55. /**
  56. *
  57. * class constructor
  58. *
  59. * @param \Cube\Controller\Request\AbstractRequest $request
  60. * @param \Cube\Controller\Response\ResponseInterface $response
  61. */
  62. public function __construct(AbstractRequest $request, ResponseInterface $response)
  63. {
  64. $this->setRequest($request)
  65. ->setResponse($response);
  66. $this->_helper = new Helper\Broker($this);
  67. $this->_flashMessenger = $this->_helper->getHelper('FlashMessenger');
  68. $bootstrap = Front::getInstance()->getBootstrap();
  69. $this->_settings = $bootstrap->getResource('settings');
  70. $this->_user = $bootstrap->getResource('user');
  71. $this->init();
  72. }
  73. /**
  74. *
  75. * set translate adapter
  76. *
  77. * @param \Cube\Translate\Adapter\AbstractAdapter $translate
  78. *
  79. * @return $this
  80. */
  81. public function setTranslate(TranslateAdapter $translate)
  82. {
  83. $this->_translate = $translate;
  84. return $this;
  85. }
  86. /**
  87. *
  88. * get translate adapter
  89. *
  90. * @return \Cube\Translate\Adapter\AbstractAdapter
  91. */
  92. public function getTranslate()
  93. {
  94. if (!$this->_translate instanceof TranslateAdapter) {
  95. $translate = Front::getInstance()->getBootstrap()->getResource('translate');
  96. if ($translate instanceof Translate) {
  97. $this->setTranslate(
  98. $translate->getAdapter());
  99. }
  100. }
  101. return $this->_translate;
  102. }
  103. protected function _setNoLayout()
  104. {
  105. Front::getInstance()->getBootstrap()->getResource('view')->setNoLayout();
  106. }
  107. /**
  108. *
  109. * checks if an admin is logged in
  110. * - will only work if using _forward() method or action() helper
  111. *
  112. * @param array $roles
  113. *
  114. * @return bool
  115. */
  116. protected function _loggedInAdmin($roles = array())
  117. {
  118. $authentication = Authentication::getInstance();
  119. if ($authentication->hasIdentity()) {
  120. $storage = $authentication->getStorage()->read();
  121. if (empty($roles)) {
  122. $roles = array_keys(UsersService::getAdminRoles());
  123. }
  124. if (in_array($storage['role'], $roles)) {
  125. return true;
  126. }
  127. }
  128. return false;
  129. }
  130. /**
  131. *
  132. * dummy function used as a placeholder for translatable sentences
  133. *
  134. * @param $string
  135. *
  136. * @return string
  137. */
  138. protected function _($string)
  139. {
  140. return $string;
  141. }
  142. }