PageRenderTime 50ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/applications/noviusos_page/classes/controller/admin/page.ctrl.php

https://github.com/jay3/core
PHP | 321 lines | 250 code | 39 blank | 32 comment | 29 complexity | 506985d11ab2130c0c3b033793336293 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * NOVIUS OS - Web OS for digital communication
  4. *
  5. * @copyright 2011 Novius
  6. * @license GNU Affero General Public License v3 or (at your option) any later version
  7. * http://www.gnu.org/licenses/agpl-3.0.html
  8. * @link http://www.novius-os.org
  9. */
  10. namespace Nos\Page;
  11. class Controller_Admin_Page extends \Nos\Controller_Admin_Crud
  12. {
  13. protected $page_parent = false;
  14. public function before()
  15. {
  16. parent::before();
  17. \Nos\I18n::current_dictionary('noviusos_page::common', 'nos::common');
  18. }
  19. /**
  20. * Saves the wysiwyg. They're not part of the fields and handled automatically, because we don't know how much there are.
  21. *
  22. * @param $page
  23. * @param $data
  24. */
  25. public function before_save($page, $data)
  26. {
  27. if ($this->item->page_entrance && !$this->item->published()) {
  28. $this->send_error(new \Exception(__('The home page must be published. To unpublish this page, set another page as home page first.')));
  29. }
  30. parent::before_save($page, $data);
  31. //Set up the first page created in a context as the homepage.
  32. if ($this->is_new) {
  33. // The first page we create is a homepage
  34. $context_has_home = (int) (bool) Model_Page::count(array(
  35. 'where' => array(
  36. array('page_entrance', '=', 1),
  37. array('page_context', $this->item->page_context),
  38. ),
  39. ));
  40. // $context_has_home is either 0 or 1 with the double cast
  41. $page->page_home = 1 - $context_has_home;
  42. $page->page_entrance = 1 - $context_has_home;
  43. }
  44. foreach (\Input::post('wysiwyg', array()) as $key => $text) {
  45. $page->wysiwygs->$key = $text;
  46. }
  47. }
  48. protected function fieldset($fieldset)
  49. {
  50. $fieldset = parent::fieldset($fieldset);
  51. $fieldset->field('page_parent_id')->setRendererOptions(array(
  52. 'context' => $this->item->page_context,
  53. ));
  54. $checkbox_menu = '<label><input type="checkbox" data-id="same_menu_title">'.__('Use title').'</label>';
  55. $cache_duration = $fieldset->field('page_cache_duration');
  56. $cache_duration->set_template(str_replace('{{duration}}', '{field} {required}', $cache_duration->label));
  57. $fieldset->field('page_lock')->set_template('{label} {field} {required}');
  58. $fieldset->field('page_menu_title')->set_template("\t\t<span class=\"{error_class}\">{label}{required}</span>\n\t\t<br />\n\t\t<span class=\"{error_class}\">{field} <br />$checkbox_menu {error_msg}</span>\n");
  59. $form_attributes = $fieldset->get_config('form_attributes');
  60. if (!isset($form_attributes['class'])) {
  61. $form_attributes['class'] = '';
  62. }
  63. $form_attributes['class'] .= ' fill-parent';
  64. $fieldset->set_config('form_attributes', $form_attributes);
  65. return $fieldset;
  66. }
  67. public function action_set_homepage()
  68. {
  69. try {
  70. $id = \Input::post('id', 0);
  71. if (empty($id) && \Fuel::$env === \Fuel::DEVELOPMENT) {
  72. $id = \Input::get('id');
  73. }
  74. $this->item = $this->crud_item($id);
  75. $this->checkPermission('set_homepage');
  76. $contexts = $this->item->get_all_context();
  77. $pages_context = $this->item->find_context('all');
  78. $pages_old = Model_Page::find('all', array(
  79. 'where' => array(
  80. array('page_entrance', '=', 1),
  81. array('page_context', 'IN', $contexts),
  82. array('page_id', 'NOT IN', array_keys($pages_context)),
  83. ),
  84. ));
  85. foreach ($pages_context as $page_context) {
  86. $page_context->page_home = 1;
  87. $page_context->page_entrance = 1;
  88. $page_context->save();
  89. }
  90. foreach ($pages_old as $page_old) {
  91. $page_old->page_home = 0;
  92. $page_old->page_entrance = 0;
  93. $page_old->save();
  94. }
  95. $dispatchEvent = array(
  96. 'name' => get_class($this->item),
  97. 'action' => 'update',
  98. 'id' => array_merge(array_keys($pages_context), array_keys($pages_old)),
  99. 'context_common_id' => array($this->item->page_context_common_id),
  100. 'context' => array_values($contexts),
  101. );
  102. $body = array(
  103. 'notify' => strtr(__('No sooner said than done. The home page is now ‘{{title}}’.'), array(
  104. '{{title}}' => $this->item->title_item(),
  105. )),
  106. 'dispatchEvent' => $dispatchEvent,
  107. );
  108. } catch (\Exception $e) {
  109. $this->send_error($e);
  110. }
  111. \Response::json($body);
  112. }
  113. public function action_duplicate($id = null)
  114. {
  115. return $this->_duplicate($id, \Input::post('include_children', false));
  116. }
  117. protected function _duplicate($id = null, $recursive = false)
  118. {
  119. $page = $this->crud_item($id);
  120. $contexts_list = $page->find_context('all');
  121. $has_children = $page->find_children();
  122. $has_children = !empty($has_children);
  123. $contexts = \Input::post( ($recursive ? 'contexts_multi' : 'contexts_single'), null);
  124. if (empty($contexts) && (count($contexts_list) > 1 || $has_children)) {
  125. \Response::json(array(
  126. 'action' => array(
  127. 'action' => 'nosDialog',
  128. 'dialog' => array(
  129. 'ajax' => true,
  130. 'contentUrl' => 'admin/noviusos_page/page/popup_duplicate/'.$id,
  131. 'title' => strtr(__('Duplicating the page ‘{{title}}’'), array(
  132. '{{title}}' => $page->title_item(),
  133. )),
  134. 'width' => 500,
  135. 'height' => 330,
  136. ),
  137. ),
  138. ));
  139. }
  140. try {
  141. $contexts = empty($contexts) ? $page->get_context() : $contexts;
  142. static::duplicate_page($page, $contexts, $recursive);
  143. \Response::json(array(
  144. 'dispatchEvent' => array(
  145. 'name' => 'Nos\Page\Model_Page',
  146. 'action' => 'insert',
  147. 'context' => $contexts,
  148. ),
  149. 'notify' => $recursive ? __('Here you are! The page and its subpages have just been duplicated.') : __('Here you are! The page has just been duplicated.'),
  150. ));
  151. } catch (\Exception $e) {
  152. $this->send_error($e);
  153. }
  154. }
  155. public function action_popup_duplicate($id = null, $recursive = false)
  156. {
  157. $page = $this->crud_item($id);
  158. $contexts_list = $page->find_context('all');
  159. return \View::forge('noviusos_page::admin/popup_duplicate', array(
  160. 'item' => $page,
  161. 'action' => 'admin/noviusos_page/page/duplicate/'.$id,
  162. 'crud' => $this->config,
  163. 'contexts_list' => $contexts_list,
  164. ), false);
  165. }
  166. protected static function duplicate_page($page, $context, $recursive, $parent = null, $common_id = null)
  167. {
  168. static $child_common_ids = array();
  169. $all = $page->find_context($context);
  170. // When cloning 1 context only
  171. if (!is_array($context)) {
  172. $all = array($all);
  173. }
  174. // Find the main context (or the only context we want to duplicate)
  175. /**
  176. * @var $main Model_Page
  177. */
  178. $main = null;
  179. foreach ($all as $item) {
  180. if ((is_array($context) && $item->is_main_context()) || $context == $item->get_context()) {
  181. $main = $item;
  182. break;
  183. }
  184. }
  185. // The main context was not duplicated, find a replacement
  186. if (empty($main)) {
  187. foreach (array_keys(\Nos\Tools_Context::contexts()) as $code) {
  188. if (in_array($code, $context)) {
  189. foreach ($all as $item) {
  190. if ($code == $item->get_context()) {
  191. $main = $item;
  192. break 2;
  193. }
  194. }
  195. }
  196. }
  197. }
  198. $parents = array();
  199. // Duplicate the main context (or the only context we want to duplicate)
  200. $clone = clone $main;
  201. $clone->page_entrance = 0;
  202. $clone->page_home = 0;
  203. $clone->page_published = 0;
  204. $clone->page_context_common_id = $common_id;
  205. $clone->page_context_is_main = empty($common_id) ? 1 : 0;
  206. // Change the title for the root item only (children path will inherit from their parent)
  207. if (!empty($parent)) {
  208. $clone->set_parent($parent);
  209. }
  210. // Duplicate up to 5 times
  211. $try = 1;
  212. do {
  213. try {
  214. $failed = false;
  215. if (empty($parent)) {
  216. $title_append = strtr(__(' (copy {{count}})'), array(
  217. '{{count}}' => $try,
  218. ));
  219. $clone->page_title = $main->page_title.$title_append;
  220. $clone->page_virtual_name = null;
  221. $clone->page_virtual_url = null;
  222. }
  223. $clone->save();
  224. break;
  225. } catch (\Nos\BehaviourDuplicateException $e) {
  226. $failed = true;
  227. if (!empty($parent)) {
  228. break;
  229. }
  230. $try++;
  231. if ($try > 5) {
  232. throw new \Exception(__('Slow down, slow down. You have duplicated this page 5 times already. Edit them first before creating more duplicates.'));
  233. }
  234. }
  235. } while ($try <= 5);
  236. if ($failed) {
  237. throw new \Exception(__('Something went wrong. Please refresh your browser window. If the page has not been duplicated, please try again. Contact your developer or Novius OS if the problem persists. We apologise for the inconvenience caused.').' - '.$try.', '.$parent->id);
  238. }
  239. $parents[$clone->get_context()] = $clone;
  240. // We need this from the main context to clone the other ones
  241. $common_id = $clone->page_context_common_id;
  242. // Clone other contexts
  243. foreach ($all as $item) {
  244. // Main context is already cloned
  245. if ($item->id == $main->id) {
  246. continue;
  247. }
  248. $clone = clone $item;
  249. $clone->page_entrance = 0;
  250. $clone->page_home = 0;
  251. $clone->page_published = 0;
  252. $clone->page_context_common_id = $common_id;
  253. $clone->page_virtual_name = null;
  254. $clone->page_virtual_url = null;
  255. $clone->page_context_is_main = empty($common_id) ? 1 : 0;
  256. if (empty($parent)) {
  257. $clone->page_title = $clone->page_title.$title_append;
  258. } else {
  259. $clone->set_parent($parent);
  260. }
  261. try {
  262. $clone->save();
  263. } catch (\Nos\BehaviourDuplicateException $e) {
  264. throw new \Exception(__('Something went wrong. Please refresh your browser window and try again. Contact your developer or Novius OS if the problem persists. We apologise for the inconvenience caused.'));
  265. }
  266. $parents[$clone->get_context()] = $clone;
  267. }
  268. // Clone children if appropriate
  269. if ($recursive) {
  270. // $all already contains only the contexts we want to duplicate
  271. // Clone each context separately
  272. foreach ($all as $parent) {
  273. foreach ($parent->find_children() as $child) {
  274. $child_common_id = $child->page_context_common_id;
  275. $clone_common_id = \Arr::get($child_common_ids, $child_common_id, null);
  276. $child_common_ids[$child_common_id] = static::duplicate_page($child, $child->get_context(), $recursive, $parents[$child->get_context()], $clone_common_id);
  277. }
  278. }
  279. }
  280. return $common_id;
  281. }
  282. }