PageRenderTime 51ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/www/app/AdminModule/presenters/AdminBasePresenter.php

https://github.com/bazo/Mokuji
PHP | 265 lines | 228 code | 27 blank | 10 comment | 16 complexity | 1d3e11fcd1edc1af4fb4b8f5aa54333a MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  1. <?php
  2. abstract class Admin_BasePresenter extends Presenter
  3. {
  4. protected $langs = array();
  5. /** @persistent */
  6. public $lang;
  7. protected $translator, $pathToTheme;
  8. public function startup()
  9. {
  10. parent::startup();
  11. if (!isset($this->lang))
  12. {
  13. $this->lang = $this->getHttpRequest()->detectLanguage($this->langs);
  14. if($this->lang == null) $this->lang = 'en';
  15. $this->canonicalize();
  16. }
  17. $this->translator = Environment::getService('Mokuji\Translator\Admin');
  18. $this->translator->lang = $this->lang;
  19. //$this->translator = new Admin_Translator($this->lang);
  20. $cache = Environment::getCache('langs');
  21. $langs = $cache->offsetGet('langs');
  22. if($langs == NULL)
  23. {
  24. $this->langs = $this->translator->getSupportedLangs();//$this->model('lang')->getAll();
  25. $cache->save('langs', $this->langs);
  26. }
  27. else $this->langs = $langs;
  28. $this->refreshConfig();
  29. }
  30. protected function refreshConfig()
  31. {
  32. $user = Environment::getUser();
  33. $this->template->user = $user;
  34. if($user->getIdentity() != null) $this->template->user_data = $user->getIdentity()->getData();
  35. $this->template->setTranslator($this->translator);
  36. $this->template->website = Environment::getVariable('website');
  37. $this->template->domain = 'http://'.Environment::getVariable('website');
  38. $this->template->presenter = $this;
  39. $admin_config = ConfigAdapterIni::load(APP_DIR.'/config/admin.ini');
  40. foreach($admin_config['admin'] as $var => $value)
  41. {
  42. Environment::setVariable($var, $value);
  43. }
  44. Environment::setVariable('themesDir', 'themes');
  45. }
  46. protected function compileMenuItems($menu, $module_items, $parent = null)
  47. {
  48. foreach ($module_items as $label => $link) {
  49. if(!is_array($link))
  50. {
  51. if($parent == null) $menu->add($label, $link);
  52. else $menu->getR($parent)->add($label, $link);
  53. }
  54. else
  55. {
  56. if($parent == null) $menu->add($label, $link[0]);
  57. else $menu->getR($parent)->add($label, $link[0]);
  58. unset($link[0]);
  59. $this->compileMenuItems($menu, $link, $label);
  60. }
  61. }
  62. return $menu;
  63. }
  64. public function createComponentMenu($name)
  65. {
  66. $menu = new AdminNavigationBuilder($this, $name);
  67. $menu->setTranslator($this->translator);
  68. $menu->add('Dashboard',':Admin:Dashboard:');
  69. $menu->add('Pages', ':Admin:Pages:');
  70. $menu->add('Categories', ':Admin:Categories:');
  71. $menu->add('Menus', ':Admin:Menus:');
  72. $menu->add('Themes', ':Admin:Themes:');
  73. $module_items = Environment::getApplication()->getAdminNodes();
  74. $menu = $this->compileMenuItems($menu, $module_items);
  75. $menu->add('Modules', ':Admin:Modules:');
  76. $menu->add('Settings', ':Admin:Options:');
  77. $menu->template->presenter = $this;
  78. return $menu;
  79. }
  80. /**
  81. * @param string Model name
  82. * @return Model
  83. */
  84. protected function model($model_name)
  85. {
  86. static $model_instances;
  87. $model_class = 'Admin_'.ucfirst($model_name).'Model';
  88. $model_instances[$model_name] = new $model_class();
  89. return $model_instances[$model_name];
  90. }
  91. protected function createComponentCss($name)
  92. {
  93. $theme = Environment::getVariable('theme');
  94. $css = new CssLoader($this, $name);
  95. $css->setModule('admin');
  96. $css->absolutizeUrls = true;
  97. $css->media = 'screen, tv, projection';
  98. $css->sourcePath = APP_DIR.$this->pathToTheme . "/css";
  99. //$css->sourceUri = Environment::getVariable("baseUri") . "css/admin/$theme";
  100. $css->tempUri = Environment::getVariable("baseUri") . "css/admin";
  101. $css->tempPath = WWW_DIR . "/css/admin";
  102. $css->joinFiles = false;
  103. $css->sourceUri = APP_DIR.$this->pathToTheme;
  104. $css->filters[] = array($this, "encodeImages");
  105. //$css->filters[] = array($this, "tidyCSS");
  106. return $css;
  107. }
  108. protected function createComponentJs($name)
  109. {
  110. $theme = Environment::getVariable('theme');
  111. $js = new JavaScriptLoader($this, $name);
  112. $js->setModule('admin');
  113. $js->tempUri = Environment::getVariable("baseUri")."js/admin";
  114. $js->tempPath = WWW_DIR . "/js/admin";
  115. $js->sourcePath = APP_DIR.$this->pathToTheme . "/js";
  116. $filter = new VariablesFilter();
  117. $session_conf = Environment::getVariable('session');
  118. $filter->setVariable("session_expiration", $session_conf['expiration']);
  119. $filter->setVariable("login_url", $this->link(':Admin:Login:Json'));
  120. $js->filters[] = array($filter, "apply");
  121. //$js->filters[] = array($this, "packJs");
  122. //$js->joinFiles = Environment::isProduction();
  123. $js->joinFiles = true;
  124. return $js;
  125. }
  126. protected function createComponentLangSelector($name)
  127. {
  128. $form = new LiveForm($this, $name);
  129. $renderer = $form->getRenderer();
  130. $renderer->wrappers['controls']['container'] = null;;
  131. $renderer->wrappers['pair']['container'] = null;
  132. $renderer->wrappers['label']['container'] = null;
  133. $renderer->wrappers['control']['container'] = null;
  134. $form->addSelect('lang', 'language', $this->langs)->setDefaultValue($this->lang);
  135. $form->addSubmit('ok', 'OK')->onClick[] = array($this, 'changeLang');
  136. return $form;
  137. }
  138. public function changeLang(Button $button)
  139. {
  140. $form = $button->getForm();
  141. $values = $form->getValues();
  142. $lang = $values['lang'];
  143. $this->redirect('this', array('lang' => $lang));
  144. }
  145. public function createComponentFormSearch($name)
  146. {
  147. $form = new LiveForm($this, $name);
  148. $renderer = $form->getRenderer();
  149. $renderer->wrappers['form']['container'] = Html::el('div')->id('search-form');
  150. $renderer->wrappers['controls']['container'] = null;;
  151. $renderer->wrappers['pair']['container'] = null;
  152. $renderer->wrappers['label']['container'] = null;
  153. $renderer->wrappers['control']['container'] = null;
  154. $form->addGroup('Search')->setOption('container', Html::el('fieldset')->id('search'));
  155. $form->addText('search_query','');
  156. $form->addSubmit('ok', 'GO!')->onClick[] = array($this, 'getSearchResults');
  157. }
  158. public function getSearchResults(Button $button)
  159. {
  160. $form = $button->getForm();
  161. $values = $form->getValues();
  162. $form->parent->flash('Search not implemented');
  163. $form->parent->end();
  164. }
  165. public function packJs($code)
  166. {
  167. $packer = new JavaScriptPacker($code, 'None');
  168. return $packer->pack();
  169. }
  170. public function encodeImages($code)
  171. {
  172. $encoder = new DataURIFilter();
  173. return $encoder->convert($code);
  174. }
  175. public function tidyCSS($code)
  176. {
  177. $tidy = new Css_Tidy();
  178. return $tidy->parse($code);
  179. }
  180. public function handleLogout()
  181. {
  182. Environment::getUser()->signOut();
  183. $session = $this->getSession('backlink');
  184. $session['backlink'] = null;
  185. $this->flashMessage('You have been logged off because: '.Environment::getUser()->getSignOutReason());
  186. $this->redirect(':Admin:Login:default');
  187. }
  188. public function formatLayoutTemplateFiles($presenter, $layout)
  189. {
  190. $themesDir = Environment::getVariable('themesDir');
  191. $theme = Environment::getVariable('theme');
  192. $appDir = Environment::getVariable('appDir');
  193. $path = '/' . str_replace(':', 'Module/', $presenter);
  194. $pathP = substr_replace($path, '/'.$themesDir.'/'.$theme.'/templates', strrpos($path, '/'), 0);
  195. $list = array(
  196. "$appDir$pathP/@$layout.phtml",
  197. "$appDir$pathP.@$layout.phtml",
  198. );
  199. while (($path = substr($path, 0, strrpos($path, '/'))) !== FALSE) {
  200. $list[] = "$appDir$path".'/'.$themesDir.'/'.$theme.'/templates/'."@$layout.phtml";
  201. }
  202. return $list;
  203. }
  204. public function formatTemplateFiles($presenter, $view)
  205. {
  206. $parts = explode(':', $presenter);
  207. Environment::setVariable('moduleDir', $parts[0].'Module');
  208. $themesDir = Environment::getVariable('themesDir');
  209. $theme = Environment::getVariable('theme');
  210. $appDir = Environment::getVariable('appDir');
  211. $path = '/' . str_replace(':', 'Module/', $presenter);
  212. $pathP = substr_replace($path, '/'.$themesDir.'/'.$theme.'/templates', strrpos($path, '/'), 0);
  213. $this->pathToTheme = substr($pathP,0, strrpos($pathP, '/'));
  214. $this->pathToTheme = substr($this->pathToTheme,0, strrpos($this->pathToTheme, '/'));
  215. $path = substr_replace($path, '/'.$themesDir.'/'.$theme.'/templates', strrpos($path, '/'));
  216. return array(
  217. "$appDir$pathP/$view.phtml",
  218. "$appDir$pathP.$view.phtml",
  219. "$appDir$path/@global.$view.phtml",
  220. );
  221. }
  222. public function flash($message, $type = 'info')
  223. {
  224. $this->payload->flashes[] = array('msg' => $message, 'type' => $type);
  225. parent::flashMessage($message, $type);
  226. $this->invalidateControl('flash');
  227. }
  228. public function flashCmd($cmd)
  229. {
  230. $this->payload->cmds[] = $cmd;
  231. }
  232. public function end()
  233. {
  234. $this->sendPayload();
  235. $this->terminate();
  236. }
  237. }
  238. ?>