PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/net/nehmer/static/handler/view.php

https://github.com/nemein/openpsa
PHP | 317 lines | 207 code | 34 blank | 76 comment | 36 complexity | 61ab075346306be41a242e12ffea9792 MD5 | raw file
  1. <?php
  2. /**
  3. * @package net.nehmer.static
  4. * @author The Midgard Project, http://www.midgard-project.org
  5. * @copyright The Midgard Project, http://www.midgard-project.org
  6. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  7. */
  8. /**
  9. * n.n.static index page handler
  10. *
  11. * @package net.nehmer.static
  12. */
  13. class net_nehmer_static_handler_view extends midcom_baseclasses_components_handler
  14. {
  15. /**
  16. * The content topic to use
  17. *
  18. * @var midcom_db_topic
  19. */
  20. private $_content_topic = null;
  21. /**
  22. * The article to display
  23. *
  24. * @var midcom_db_article
  25. */
  26. private $_article = null;
  27. /**
  28. * The Datamanager of the article to display.
  29. *
  30. * @var midcom_helper_datamanager2_datamanager
  31. */
  32. private $_datamanager = null;
  33. /**
  34. * Simple helper which references all important members to the request data listing
  35. * for usage within the style listing.
  36. */
  37. private function _prepare_request_data()
  38. {
  39. $this->_request_data['article'] =& $this->_article;
  40. $this->_request_data['datamanager'] =& $this->_datamanager;
  41. // Populate the toolbar
  42. if ($this->_article->can_do('midgard:update'))
  43. {
  44. $this->_view_toolbar->add_item
  45. (
  46. array
  47. (
  48. MIDCOM_TOOLBAR_URL => "edit/{$this->_article->guid}/",
  49. MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('edit'),
  50. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/edit.png',
  51. MIDCOM_TOOLBAR_ACCESSKEY => 'e',
  52. )
  53. );
  54. }
  55. $article = $this->_article;
  56. if ($this->_article->topic !== $this->_content_topic->id)
  57. {
  58. $qb = net_nehmer_static_link_dba::new_query_builder();
  59. $qb->add_constraint('topic', '=', $this->_content_topic->id);
  60. $qb->add_constraint('article', '=', $this->_article->id);
  61. if ($qb->count() === 1)
  62. {
  63. // Get the link
  64. $results = $qb->execute_unchecked();
  65. $article = $results[0];
  66. }
  67. }
  68. if ($article->can_do('midgard:delete'))
  69. {
  70. $this->_view_toolbar->add_item
  71. (
  72. array
  73. (
  74. MIDCOM_TOOLBAR_URL => "delete/{$this->_article->guid}/",
  75. MIDCOM_TOOLBAR_LABEL => $this->_l10n_midcom->get('delete'),
  76. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/trash.png',
  77. MIDCOM_TOOLBAR_ACCESSKEY => 'd',
  78. )
  79. );
  80. }
  81. }
  82. /**
  83. * Maps the content topic from the request data to local member variables.
  84. */
  85. public function _on_initialize()
  86. {
  87. $this->_content_topic =& $this->_request_data['content_topic'];
  88. }
  89. /**
  90. * Can-Handle check against the article name. We have to do this explicitly
  91. * in can_handle already, otherwise we would hide all subtopics as the request switch
  92. * accepts all argument count matches unconditionally.
  93. *
  94. * Not applicable for the "index" handler, where the article name is fixed (see handle).
  95. *
  96. * @param mixed $handler_id The ID of the handler.
  97. * @param Array $args The argument list.
  98. * @param Array &$data The local request data.
  99. * @return boolean True if the request can be handled, false otherwise.
  100. */
  101. public function _can_handle_view ($handler_id, array $args, array &$data)
  102. {
  103. if ($handler_id == 'index')
  104. {
  105. return true;
  106. }
  107. $qb = midcom_db_article::new_query_builder();
  108. $qb->add_constraint('name', '=', $args[0]);
  109. $qb->add_constraint('up', '=', 0);
  110. $qb->set_limit(1);
  111. // Include the article links to the indexes if enabled
  112. if ($this->_config->get('enable_article_links'))
  113. {
  114. $mc = net_nehmer_static_link_dba::new_collector('topic', $this->_content_topic->id);
  115. $mc->add_constraint('topic', '=', $this->_content_topic->id);
  116. $links = $mc->get_values('article');
  117. $qb->begin_group('OR');
  118. if (count($links) > 0)
  119. {
  120. $qb->add_constraint('id', 'IN', $links);
  121. }
  122. $qb->add_constraint('topic', '=', $this->_content_topic->id);
  123. $qb->end_group();
  124. }
  125. else
  126. {
  127. $qb->add_constraint('topic', '=', $this->_content_topic->id);
  128. }
  129. $result = $qb->execute();
  130. if ($result)
  131. {
  132. $this->_article = $result[0];
  133. return true;
  134. }
  135. return false;
  136. }
  137. /**
  138. * Looks up an article to display. If the handler_id is 'index', the index article is tried to be
  139. * looked up, otherwise the article name is taken from args[0]. Triggered error messages are
  140. * generated accordingly. A missing index will trigger a forbidden error, a missing regular
  141. * article a 404 (from can_handle).
  142. *
  143. * Note, that the article for non-index mode operation is automatically determined in the can_handle
  144. * phase.
  145. *
  146. * If create privileges apply, we relocate to the index creation article
  147. *
  148. * @param mixed $handler_id The ID of the handler.
  149. * @param Array $args The argument list.
  150. * @param Array &$data The local request data.
  151. */
  152. public function _handler_view ($handler_id, array $args, array &$data)
  153. {
  154. if ($handler_id == 'index')
  155. {
  156. $this->_load_index_article();
  157. }
  158. if ($handler_id == 'view_raw')
  159. {
  160. midcom::get()->skip_page_style = true;
  161. }
  162. $this->_load_datamanager();
  163. if ($this->_config->get('enable_ajax_editing'))
  164. {
  165. $this->_request_data['controller'] = midcom_helper_datamanager2_controller::create('ajax');
  166. $this->_request_data['controller']->schemadb =& $this->_request_data['schemadb'];
  167. $this->_request_data['controller']->set_storage($this->_article);
  168. $this->_request_data['controller']->process_ajax();
  169. }
  170. $arg = $this->_article->name ? $this->_article->name : $this->_article->guid;
  171. if ( $arg != 'index'
  172. && $this->_config->get('hide_navigation'))
  173. {
  174. $this->add_breadcrumb("{$arg}/", $this->_article->title);
  175. }
  176. $this->_prepare_request_data();
  177. if ( $this->_config->get('enable_article_links')
  178. && $this->_content_topic->can_do('midgard:create'))
  179. {
  180. $this->_view_toolbar->add_item
  181. (
  182. array
  183. (
  184. MIDCOM_TOOLBAR_LABEL => sprintf($this->_l10n_midcom->get('create %s'), $this->_l10n->get('article link')),
  185. MIDCOM_TOOLBAR_URL => "create/link/?article={$this->_article->id}",
  186. MIDCOM_TOOLBAR_ICON => 'stock-icons/16x16/attach.png',
  187. )
  188. );
  189. }
  190. midcom::get('metadata')->set_request_metadata($this->_article->metadata->revised, $this->_article->guid);
  191. $this->bind_view_to_object($this->_article, $this->_datamanager->schema->name);
  192. if ( $this->_config->get('indexinnav')
  193. || $this->_config->get('autoindex')
  194. || $this->_article->name != 'index')
  195. {
  196. $this->set_active_leaf($this->_article->id);
  197. }
  198. if ( $this->_config->get('folder_in_title')
  199. && $this->_topic->extra != $this->_article->title)
  200. {
  201. midcom::get('head')->set_pagetitle("{$this->_topic->extra}: {$this->_article->title}");
  202. }
  203. else
  204. {
  205. midcom::get('head')->set_pagetitle($this->_article->title);
  206. }
  207. }
  208. private function _load_index_article()
  209. {
  210. $qb = midcom_db_article::new_query_builder();
  211. $qb->add_constraint('name', '=', 'index');
  212. $qb->set_limit(1);
  213. // Include the article links to the indexes if enabled
  214. if ($this->_config->get('enable_article_links'))
  215. {
  216. $mc = net_nehmer_static_link_dba::new_collector('topic', $this->_content_topic->id);
  217. $mc->add_constraint('topic', '=', $this->_content_topic->id);
  218. $links = $mc->get_values('article');
  219. $qb->begin_group('OR');
  220. if (count($links) > 0)
  221. {
  222. $qb->add_constraint('id', 'IN', $links);
  223. }
  224. $qb->add_constraint('topic', '=', $this->_content_topic->id);
  225. $qb->end_group();
  226. }
  227. else
  228. {
  229. $qb->add_constraint('topic', '=', $this->_content_topic->id);
  230. }
  231. $result = $qb->execute();
  232. if (empty($result))
  233. {
  234. if ($this->_content_topic->can_do('midgard:create'))
  235. {
  236. // Check via non-ACLd QB that the topic really doesn't have index article before relocating
  237. $index_qb = midcom_db_article::new_query_builder();
  238. $index_qb->add_constraint('topic', '=', $this->_content_topic->id);
  239. $index_qb->add_constraint('name', '=', 'index');
  240. if ($index_qb->count_unchecked() == 0)
  241. {
  242. $schemas = array_keys($this->_request_data['schemadb']);
  243. midcom::get()->relocate("createindex/{$schemas[0]}/");
  244. // This will exit.
  245. }
  246. }
  247. throw new midcom_error_forbidden('Directory index forbidden');
  248. }
  249. $this->_article = $result[0];
  250. }
  251. /**
  252. * Internal helper, loads the datamanager for the current article. Any error triggers a 500.
  253. */
  254. private function _load_datamanager()
  255. {
  256. $this->_datamanager = new midcom_helper_datamanager2_datamanager($this->_request_data['schemadb']);
  257. if (! $this->_datamanager->autoset_storage($this->_article))
  258. {
  259. throw new midcom_error("Failed to create a DM2 instance for article {$this->_article->id}.");
  260. }
  261. }
  262. /**
  263. * Shows the loaded article.
  264. *
  265. * @param mixed $handler_id The ID of the handler.
  266. * @param array &$data The local request data.
  267. */
  268. public function _show_view ($handler_id, array &$data)
  269. {
  270. if ( $this->_config->get('enable_ajax_editing')
  271. && isset($data['controller']))
  272. {
  273. // For AJAX handling it is the controller that renders everything
  274. $this->_request_data['view_article'] = $this->_request_data['controller']->get_content_html();
  275. }
  276. else
  277. {
  278. $this->_request_data['view_article'] = $data['datamanager']->get_content_html();
  279. }
  280. midcom_show_style('show-article');
  281. }
  282. }
  283. ?>