您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

Controller.class.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. class Controller {
  3. public $config;
  4. public $locale;
  5. public $layout;
  6. public $user;
  7. //public $helper;
  8. public $utility;
  9. public $view;
  10. public $viewDir;
  11. public $appTitle;
  12. public $db;
  13. public $security;
  14. public $cookie;
  15. public $session;
  16. public $logger;
  17. public $media;
  18. public $allow;
  19. public $allowedRoles;
  20. public $permissionDenied;
  21. public $showBreadcrumbs;
  22. public $breadcrumbs;
  23. public $actionTitle;
  24. public $appRequestType; //web [default], mob, desk (TODO: read the http header appRequestType)
  25. public $memoryCache;
  26. public $alerts;
  27. public $paginationRange;
  28. public $userLocale;
  29. function __construct() {
  30. global $config, $layout, $locale, $memoryCache, $user, $db, $security, $session, $cookie, $logger;
  31. $this->db = $db;
  32. $this->config = $config;
  33. $this->layout = $layout;
  34. $this->locale = $locale;
  35. $this->defLang = $this->locale->defaultLanguage;
  36. $this->utility = new Utility();
  37. //$this->helper = new Helper();
  38. $this->alerts = new Alerts(); //Deprecated
  39. $this->media = new Media();
  40. $this->user = $user;
  41. $this->view = new stdClass();
  42. $this->viewDir = null;
  43. $this->appTitle = $this->config['settings']['app-title'];
  44. $this->security = $security;
  45. $this->session = $session;
  46. $this->cookie = $cookie;
  47. $this->logger = $logger;
  48. $this->allow = [];
  49. $this->permissionDenied = false;
  50. $this->appRequestType = 'web';
  51. $this->memoryCache = $memoryCache;
  52. $this->paginationRange = 10;
  53. $this->userLocale = null;
  54. $this->showBreadcrumbs = true;
  55. $this->breadcrumbs = [];
  56. $this->actionTitle = '';
  57. $this->controllerName = '';
  58. $this->actionName = '';
  59. $this->setContentLocale();
  60. }
  61. public function allowAccess() {
  62. return false;
  63. }
  64. public function beforeRender($content=null) {
  65. return false;
  66. }
  67. public function checkPermissions($allowedRoles=[]) {
  68. if (is_array($allowedRoles) && !empty($allowedRoles)) {
  69. return $this->user->checkPermissions($allowedRoles);
  70. }
  71. return true;
  72. }
  73. public function setContentLocale() {
  74. $this->userLocale = $this->locale->setCurrentLanguage();
  75. $this->view->userLocale = $this->userLocale;
  76. $this->locale->setLocaleEnvironment($this->userLocale);
  77. }
  78. public function setView($file=null, $compact=true) {
  79. $content = '';
  80. //Check whether allowAccess() method is overriden in the child class and return its content
  81. if (!in_array($file, $this->allow)) {
  82. $allowAccess = $this->allowAccess();
  83. if ($allowAccess !== false) {
  84. $data = json_decode($allowAccess, true);
  85. return $data['html'];
  86. }
  87. }
  88. if (!is_null($file)) {
  89. $ob_string = $this->config['settings']['gzip-content'] ? 'ob_gzhandler' : '';
  90. ob_start($ob_string);
  91. include VIEWS_DIR.$this->viewDir.'/'.$file.'.view.php';
  92. $content = ob_get_clean();
  93. }
  94. $isUTF8 = mb_detect_encoding($content, 'UTF-8', true);
  95. if (!$isUTF8)
  96. return utf8_encode(utf8_decode($content));
  97. else
  98. return $content;
  99. }
  100. public function setJsonView($file=null, $compress=true, $jsRedirect='', $jsonData=[]) {
  101. $content = $this->setView($file, $compress);
  102. //You can overraid this method in mainController for all controllers or in a specific controller
  103. $beforeRender = $this->beforeRender($content);
  104. if ($beforeRender !== false) {
  105. $content = $beforeRender;
  106. }
  107. return $this->setRawJsonResponse('ok', null, ['jsonData'=>$jsonData, 'page'=>1, 'html'=>$content, 'jsRedirect'=>$jsRedirect]);
  108. }
  109. public function setRawJsonResponse($status='ok', $msg=null, $args=[], $jsArgs=null) {
  110. return json_encode(array_merge(['status'=>$status, 'msg'=>$msg, 'jsArgs'=>$jsArgs, 'userId'=>$this->user->getUserId(), 'groupId'=>$this->user->getGroupId(), 'username'=>$this->user->getUserField('userUsername'), 'apiKey' => $this->config['settings']['api-key'], 'userLang'=>$this->locale->setCurrentLanguage()], $args));
  111. }
  112. public function setJsonError($msg='', $action=null) {
  113. return $this->setRawJsonResponse('err', $msg, ['action', $action]);
  114. }
  115. public function partial($path, $params=[], $compact=true) {
  116. if (!empty($params)) extract($params);
  117. ob_start();
  118. include VIEWS_DIR.'Elements/'.$path.'.part.php';
  119. $content = ob_get_clean();
  120. return $content;
  121. }
  122. public function setPagination($dbRef, $totalRows, $currentPage, $link) {
  123. $this->view->totalPages = $dbRef->totalPages;
  124. $this->view->totalRows = $totalRows;
  125. $this->view->currentPage = $currentPage;
  126. $this->view->pageNumbers = [];
  127. $this->view->hasPrevPage = $this->view->currentPage > 1 ? true : false;
  128. $this->view->hasNextPage = $this->view->currentPage < $this->view->totalPages ? true : false;
  129. $this->view->prevPageLink = $this->utility->setHash($link.'/'.((int)$this->view->currentPage-1));
  130. $this->view->nextPageLink = $this->utility->setHash($link.'/'.((int)$this->view->currentPage+1));
  131. $this->view->pagNumbRange = $this->paginationRange;
  132. $this->view->pagLimitLeft = 1;
  133. $this->view->pagLimitRight = ($this->view->pagNumbRange<$this->view->totalPages)
  134. ? $this->view->pagLimitLeft+($this->view->pagNumbRange-1) : $this->view->totalPages;
  135. if ($this->view->currentPage > $this->view->pagNumbRange) {
  136. if ($this->view->totalPages > $this->view->pagNumbRange) {
  137. //$this->view->pagLimitLeft = $this->view->totalPages-$this->view->pagNumbRange;
  138. //$this->view->pagLimitRight = $this->view->pagLimitLeft+$this->view->pagNumbRange;
  139. $this->view->pagLimitLeft = $this->view->pagNumbRange+1;
  140. $this->view->pagLimitRight = ($this->view->pagLimitLeft+$this->view->pagNumbRange) < $this->view->totalPages ? $this->view->pagLimitLeft+$this->view->pagNumbRange : $this->view->totalPages;
  141. }
  142. }
  143. if ($this->view->totalPages > 1) {
  144. for($i=$this->view->pagLimitLeft; $i<=$this->view->pagLimitRight; $i++) {
  145. $pageActive = $i == $this->view->currentPage ? true : false;
  146. $this->view->pageNumbers[$i] = ['active'=>$pageActive, 'label'=>$i, 'link'=>$this->utility->setHash($link.'/'.$i)];
  147. }
  148. }
  149. }
  150. public function validateForm($data=[], $checkPasswords=false) {
  151. if (is_array($data) && !empty($data)) {
  152. $pwd1 = null;
  153. $pwd2 = null;
  154. foreach($data as $key => $item) {
  155. if (isset($item['required']) && (int)$item['required'] == 1) {
  156. if (trim($item['value']) == '') {
  157. return ['msg'=>vsprintf(_('"%s" is a required field.'), [$item['label']]), 'class'=>$item['class']];
  158. }
  159. if ($item['type'] == 'email' && trim($item['value']) != '') {
  160. if (!filter_var($item['value'], FILTER_VALIDATE_EMAIL)) {
  161. return ['msg'=>vsprintf(_('"%s" is not a valid email address.'), [$item['label']]), 'class'=>$item['class']];
  162. }
  163. }
  164. if ($checkPasswords) {
  165. if ($item['type'] == 'password1' || $item['type'] == 'password2' || $item['type'] == 'password') {
  166. /*if (strlen($item['value']) < $this->security->passwordMinLength) {
  167. return ['msg'=>vsprintf(_('"%s" field length is too short.'), [$item['label']]), 'class'=>$item['class']];
  168. }*/
  169. if (!$this->security->validatePassword($item['value'])) {
  170. return ['msg'=>vsprintf(_('The password provided is not valid. The password must contain at least %s characters and at least 1 non-alphanumeric symbol (!, ?, -, etc.)'), [$item['label'], $this->security->passwordMinLength]), 'class'=>$item['class']];
  171. }
  172. }
  173. if ($item['type'] == 'password1') {
  174. $pwd1 = trim($item['value']);
  175. }
  176. if ($item['type'] == 'password2') {
  177. $pwd2 = trim($item['value']);
  178. }
  179. if (!is_null($pwd1) && !is_null($pwd2)) {
  180. if ($pwd1 != $pwd2) {
  181. return ['msg'=>_('The password fields do not match.'), 'class'=>$item['class']];
  182. }
  183. }
  184. }
  185. }
  186. }
  187. return true;
  188. }
  189. return false;
  190. }
  191. public function setJson($structure=null) {
  192. return json_encode($structure);
  193. }
  194. public function parseArgs($args) {
  195. $params = isset($args['params']) ? $args['params'] : false;
  196. if ($params !== false) {
  197. parse_str($params, $output);
  198. } else {
  199. $output = null;
  200. }
  201. return $output;
  202. }
  203. public function getPost($key=null, $default=false) {
  204. $value = isset($_POST[$key]) ? $_POST[$key] : $default;
  205. return $value;
  206. }
  207. public function redirect($controller, $action, $args=null) {
  208. $args = $this->parseArgs($args);
  209. return Dispatch::route($controller, $action, $args);
  210. }
  211. public function compactText($content=null) {
  212. $content = str_replace(array("\n","\r","\t"), '', $content);
  213. $content = preg_replace('/\s+/', ' ', $content);
  214. return $content;
  215. }
  216. }