PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/controller/contribution/base.php

https://github.com/phpbb/customisation-db
PHP | 260 lines | 149 code | 32 blank | 79 comment | 27 complexity | ec12ffe6b0e576498b50c0c03597ac80 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Customisation Database package.
  5. *
  6. * @copyright (c) phpBB Limited <https://www.phpbb.com>
  7. * @license GNU General Public License, version 2 (GPL-2.0)
  8. *
  9. * For full copyright and license information, please see
  10. * the docs/CREDITS.txt file.
  11. *
  12. */
  13. namespace phpbb\titania\controller\contribution;
  14. use phpbb\titania\access;
  15. use phpbb\titania\contribution\type\collection as type_collection;
  16. use phpbb\titania\count;
  17. use phpbb\titania\ext;
  18. class base
  19. {
  20. /** @var \phpbb\auth\auth */
  21. protected $auth;
  22. /** @var \phpbb\config\config */
  23. protected $config;
  24. /** @var \phpbb\db\driver\driver_interface */
  25. protected $db;
  26. /** @var \phpbb\template\template */
  27. protected $template;
  28. /** @var \phpbb\user */
  29. protected $user;
  30. /** @var \phpbb\titania\controller\helper */
  31. protected $helper;
  32. /** @var type_collection */
  33. protected $types;
  34. /** @var \phpbb\request\request_interface */
  35. protected $request;
  36. /** @var \phpbb\titania\cache\service */
  37. protected $cache;
  38. /** @var \phpbb\titania\config\config */
  39. protected $ext_config;
  40. /** @var \phpbb\titania\display */
  41. protected $display;
  42. /** @var \phpbb\titania\access */
  43. protected $access;
  44. /** @var \titania_contribution */
  45. protected $contrib;
  46. /** @var bool */
  47. protected $is_author;
  48. /**
  49. * Constructor
  50. *
  51. * @param \phpbb\auth\auth $auth
  52. * @param \phpbb\config\config $config
  53. * @param \phpbb\db\driver\driver_interface $db
  54. * @param \phpbb\template\template $template
  55. * @param \phpbb\user $user
  56. * @param \phpbb\titania\controller\helper $helper
  57. * @param type_collection $types
  58. * @param \phpbb\request\request $request
  59. * @param \phpbb\titania\cache\service $cache
  60. * @param \phpbb\titania\config\config $ext_config
  61. * @param \phpbb\titania\display $display
  62. * @param \phpbb\titania\access $access
  63. */
  64. public function __construct(\phpbb\auth\auth $auth, \phpbb\config\config $config, \phpbb\db\driver\driver_interface $db, \phpbb\template\template $template, \phpbb\user $user, \phpbb\titania\controller\helper $helper, type_collection $types, \phpbb\request\request $request, \phpbb\titania\cache\service $cache, \phpbb\titania\config\config $ext_config, \phpbb\titania\display $display, access $access)
  65. {
  66. $this->auth = $auth;
  67. $this->config = $config;
  68. $this->db = $db;
  69. $this->template = $template;
  70. $this->user = $user;
  71. $this->helper = $helper;
  72. $this->types = $types;
  73. $this->request = $request;
  74. $this->cache = $cache;
  75. $this->ext_config = $ext_config;
  76. $this->display = $display;
  77. $this->access = $access;
  78. // Add common lang
  79. $this->user->add_lang_ext('phpbb/titania', 'contributions');
  80. }
  81. /**
  82. * Load contribution.
  83. *
  84. * @param string $contrib_type Contrib type URL identifier.
  85. * @param string $contrib Contrib name clean.
  86. * @throws \Exception Throws exception if contrib is not found.
  87. * @return null
  88. */
  89. protected function load_contrib($contrib_type, $contrib)
  90. {
  91. $type = ($contrib_type) ? $this->types->type_from_url($contrib_type) : false;
  92. $this->contrib = new \titania_contribution;
  93. if (!$this->contrib->load($contrib, $type) || !$this->contrib->is_visible())
  94. {
  95. throw new \Exception($this->user->lang['CONTRIB_NOT_FOUND']);
  96. }
  97. $this->is_author = $this->contrib->is_active_coauthor || $this->contrib->is_author;
  98. $this->set_access_level();
  99. }
  100. /**
  101. * Assign navigation tabs.
  102. *
  103. * @param string $page Current active page.
  104. * @return null
  105. */
  106. protected function generate_navigation($page)
  107. {
  108. // Count the number of FAQ items to display
  109. $flags = count::get_flags($this->access->get_level());
  110. $faq_count = count::from_db($this->contrib->contrib_faq_count, $flags);
  111. $is_disabled = in_array($this->contrib->contrib_status, array(ext::TITANIA_CONTRIB_CLEANED, ext::TITANIA_CONTRIB_DISABLED));
  112. /**
  113. * Menu Array
  114. *
  115. * 'filename' => array(
  116. * 'title' => 'nav menu title',
  117. * 'url' => $page_url,
  118. * 'auth' => ($can_see_page) ? true : false, // Not required, always true if missing
  119. * ),
  120. */
  121. $nav_ary = array(
  122. 'details' => array(
  123. 'title' => 'CONTRIB_DETAILS',
  124. 'url' => $this->contrib->get_url(),
  125. ),
  126. 'faq' => array(
  127. 'title' => 'CONTRIB_FAQ',
  128. 'url' => $this->contrib->get_url('faq'),
  129. 'auth' => !$this->access->is_public() || $faq_count,
  130. 'count' => $faq_count,
  131. ),
  132. 'support' => array(
  133. 'title' => 'CONTRIB_SUPPORT',
  134. 'url' => $this->contrib->get_url('support'),
  135. 'auth' => $this->ext_config->support_in_titania || $this->access->get_level() < access::PUBLIC_LEVEL,
  136. ),
  137. 'demo' => array(
  138. 'title' => 'CONTRIB_DEMO',
  139. 'url' => '',
  140. 'auth' => !empty($this->contrib->contrib_demo),
  141. ),
  142. 'manage' => array(
  143. 'title' => 'CONTRIB_MANAGE',
  144. 'url' => $this->contrib->get_url('manage'),
  145. 'auth' => (($this->is_author && $this->auth->acl_get('u_titania_post_edit_own')) && !$is_disabled) || $this->auth->acl_get('u_titania_mod_contrib_mod') || $this->contrib->type->acl_get('moderate'),
  146. ),
  147. );
  148. if ($this->contrib->contrib_demo)
  149. {
  150. $demo_menu = array();
  151. $allowed_branches = $this->contrib->type->get_allowed_branches(true);
  152. krsort($allowed_branches);
  153. $is_external = $this->contrib->contrib_status != ext::TITANIA_CONTRIB_APPROVED || !$this->contrib->options['demo'];
  154. foreach ($allowed_branches as $branch => $name)
  155. {
  156. if ($this->contrib->type instanceof \phpbb\titania\contribution\style\type && $branch === 31)
  157. {
  158. // Disable links only to the 3.1 styles demo
  159. continue;
  160. }
  161. $demo_url = $this->contrib->get_demo_url(
  162. $branch,
  163. !$is_external
  164. );
  165. if ($demo_url)
  166. {
  167. $demo_menu[] = array(
  168. 'url' => $demo_url,
  169. 'title' => $name,
  170. 'external' => $is_external,
  171. );
  172. }
  173. }
  174. if (sizeof($demo_menu) == 1)
  175. {
  176. $nav_ary['demo']['url'] = $demo_menu[0]['url'];
  177. $nav_ary['demo']['external'] = $demo_menu[0]['external'];
  178. }
  179. else if (!empty($demo_menu))
  180. {
  181. $nav_ary['demo']['sub_menu'] = $demo_menu;
  182. }
  183. else
  184. {
  185. unset($nav_ary['demo']);
  186. }
  187. }
  188. $this->display->generate_nav($nav_ary, $page, 'details');
  189. }
  190. /**
  191. * Assign breadcrumbs to template.
  192. *
  193. * @return void
  194. */
  195. protected function generate_breadcrumbs()
  196. {
  197. // Search for a category with the same name as the contrib type. This is a bit ugly, but there really isn't any better option
  198. $categories = $this->cache->get_categories();
  199. $category = new \titania_category;
  200. foreach ($categories as $category_id => $category_row)
  201. {
  202. $category->__set_array($category_row);
  203. $name = $category->get_name();
  204. if ($name == $this->contrib->type->lang['lang'] || $name == $this->contrib->type->lang['langs'])
  205. {
  206. // Generate the main breadcrumbs
  207. $this->display->generate_breadcrumbs(array(
  208. $name => $category->get_url(),
  209. ));
  210. }
  211. }
  212. }
  213. /**
  214. * Set user's access level.
  215. *
  216. * @return null
  217. */
  218. protected function set_access_level()
  219. {
  220. if ($this->access->is_public() && $this->user->data['is_registered'] && !$this->user->data['is_bot'])
  221. {
  222. if ($this->is_author)
  223. {
  224. $this->access->set_level(access::AUTHOR_LEVEL);
  225. }
  226. }
  227. }
  228. }