PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/wa-system/page/actions/waPageEdit.action.php

http://github.com/webasyst/webasyst-framework
PHP | 131 lines | 103 code | 25 blank | 3 comment | 15 complexity | f5adbc836e541dd94807f32e4f416a60 MD5 | raw file
Possible License(s): LGPL-3.0, CC-BY-3.0, BSD-3-Clause, MIT
  1. <?php
  2. class waPageEditAction extends waViewAction
  3. {
  4. protected $id;
  5. protected $model;
  6. protected $sidebar = true;
  7. protected $ibutton = true;
  8. public function execute()
  9. {
  10. $this->id = waRequest::get('id');
  11. $page_model = $this->getPageModel();
  12. $pages = $this->getPages();
  13. if ($this->id === null && $pages) {
  14. $page_ids = array_keys($pages);
  15. $this->id = $page_ids[0];
  16. }
  17. if (!$this->id || !($page = $page_model->getById($this->id))) {
  18. $this->id = null;
  19. $page = array();
  20. }
  21. $this->getPageParams();
  22. $this->view->assign('page', $page);
  23. $this->view->assign('preview_hash', $this->getPreviewHash());
  24. $this->view->assign('lang', substr(wa()->getLocale(), 0, 2));
  25. $this->view->assign('sidebar', $this->sidebar);
  26. $this->view->assign('ibutton', $this->ibutton);
  27. if ($this->sidebar) {
  28. $this->view->assign('pages', $pages);
  29. }
  30. $routes = wa()->getRouting()->getByApp($this->getAppId());
  31. $page_route = false;
  32. foreach ($routes as $domain => $domain_routes) {
  33. foreach ($domain_routes as $r_id => $r) {
  34. if (strpos($r['url'], '<url') !== false) {
  35. unset($routes[$domain][$r_id]);
  36. continue;
  37. }
  38. $routes[$domain][$r_id] = array(
  39. 'url' => waRouting::getUrlByRoute($r, $domain),
  40. 'exclude' => isset($r['_exclude']) ? in_array($this->id, $r['_exclude']) : false
  41. );
  42. if (!$routes[$domain][$r_id]['exclude'] && !$page_route) {
  43. $page_route = true;
  44. $this->view->assign('url', waRouting::getUrlByRoute($r, $domain));
  45. }
  46. }
  47. }
  48. $this->view->assign('routes', $routes);
  49. $this->view->assign('upload_url', wa()->getDataUrl('img', true));
  50. $this->template = $this->getConfig()->getRootPath().'/wa-system/page/templates/PageEdit.html';
  51. }
  52. protected function getPages()
  53. {
  54. return $this->getPageModel()->select('id,name,url,status')->order('sort')->fetchAll('id');
  55. }
  56. protected function getPageParams()
  57. {
  58. $params = $other_params = array();
  59. $vars = array(
  60. 'keywords' => _ws('META Keywords'),
  61. 'description' => _ws('META Description')
  62. );
  63. if ($this->id) {
  64. $page_model = $this->getPageModel();
  65. $params = $this->getPageModel()->getParams($this->id);
  66. }
  67. $main_params = array();
  68. foreach ($vars as $v => $t) {
  69. if (isset($params[$v])) {
  70. $main_params[$v] = $params[$v];
  71. unset($params[$v]);
  72. } else {
  73. $main_params[$v] = '';
  74. }
  75. }
  76. $this->view->assign('vars', $vars);
  77. $this->view->assign('params', $main_params);
  78. $this->view->assign('other_params', $params);
  79. }
  80. protected function getPreviewHash()
  81. {
  82. $hash = $this->appSettings('preview_hash');
  83. if ($hash) {
  84. $hash_parts = explode('.', $hash);
  85. if (time() - $hash_parts[1] > 14400) {
  86. $hash = '';
  87. }
  88. }
  89. if (!$hash) {
  90. $hash = uniqid().'.'.time();
  91. $app_settings_model = new waAppSettingsModel();
  92. $app_settings_model->set($this->getAppId(), 'preview_hash', $hash);
  93. }
  94. return md5($hash);
  95. }
  96. /**
  97. * @return waPageModel
  98. */
  99. protected function getPageModel()
  100. {
  101. if (!$this->model) {
  102. $this->model = $this->getAppId().'PageModel';
  103. }
  104. return new $this->model();
  105. }
  106. }