PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/phpBB/phpbb/group/helper.php

https://github.com/phpbb/area51-phpbb3
PHP | 300 lines | 137 code | 45 blank | 118 comment | 16 complexity | 91e02ccf1ccafd3b65f8dc850f57bd98 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * This file is part of the phpBB Forum Software 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\group;
  14. use phpbb\auth\auth;
  15. use phpbb\avatar\helper as avatar_helper;
  16. use phpbb\cache\service as cache;
  17. use phpbb\config\config;
  18. use phpbb\language\language;
  19. use phpbb\event\dispatcher_interface;
  20. use phpbb\path_helper;
  21. use phpbb\user;
  22. class helper
  23. {
  24. /** @var auth */
  25. protected $auth;
  26. /** @var avatar_helper */
  27. protected $avatar_helper;
  28. /** @var cache */
  29. protected $cache;
  30. /** @var config */
  31. protected $config;
  32. /** @var language */
  33. protected $language;
  34. /** @var dispatcher_interface */
  35. protected $dispatcher;
  36. /** @var path_helper */
  37. protected $path_helper;
  38. /** @var user */
  39. protected $user;
  40. /** @var string phpBB root path */
  41. protected $phpbb_root_path;
  42. /** @var array Return templates for a group name string */
  43. protected $name_strings;
  44. /**
  45. * Constructor
  46. *
  47. * @param auth $auth Authentication object
  48. * @param avatar_helper $avatar_helper Avatar helper object
  49. * @param cache $cache Cache service object
  50. * @param config $config Configuration object
  51. * @param language $language Language object
  52. * @param dispatcher_interface $dispatcher Event dispatcher object
  53. * @param path_helper $path_helper Path helper object
  54. * @param user $user User object
  55. */
  56. public function __construct(auth $auth, avatar_helper $avatar_helper, cache $cache, config $config, language $language, dispatcher_interface $dispatcher, path_helper $path_helper, user $user)
  57. {
  58. $this->auth = $auth;
  59. $this->avatar_helper = $avatar_helper;
  60. $this->cache = $cache;
  61. $this->config = $config;
  62. $this->language = $language;
  63. $this->dispatcher = $dispatcher;
  64. $this->path_helper = $path_helper;
  65. $this->user = $user;
  66. $this->phpbb_root_path = $path_helper->get_phpbb_root_path();
  67. /** @html Group name spans and links for usage in the template */
  68. $this->name_strings = array(
  69. 'base_url' => "{$path_helper->get_phpbb_root_path()}memberlist.{$path_helper->get_php_ext()}?mode=group&amp;g={GROUP_ID}",
  70. 'tpl_noprofile' => '<span class="username">{GROUP_NAME}</span>',
  71. 'tpl_noprofile_colour' => '<span class="username-coloured" style="color: {GROUP_COLOUR};">{GROUP_NAME}</span>',
  72. 'tpl_profile' => '<a class="username" href="{PROFILE_URL}">{GROUP_NAME}</a>',
  73. 'tpl_profile_colour' => '<a class="username-coloured" href="{PROFILE_URL}" style="color: {GROUP_COLOUR};">{GROUP_NAME}</a>',
  74. );
  75. }
  76. /**
  77. * @param string $group_name The stored group name
  78. *
  79. * @return string Group name or translated group name if it exists
  80. */
  81. public function get_name($group_name)
  82. {
  83. return $this->language->is_set('G_' . utf8_strtoupper($group_name)) ? $this->language->lang('G_' . utf8_strtoupper($group_name)) : $group_name;
  84. }
  85. /**
  86. * Get group name details for placing into templates.
  87. *
  88. * @html Group name spans and links
  89. *
  90. * @param string $mode Profile (for getting an url to the profile),
  91. * group_name (for obtaining the group name),
  92. * colour (for obtaining the group colour),
  93. * full (for obtaining a coloured group name link to the group's profile),
  94. * no_profile (the same as full but forcing no profile link)
  95. * @param int $group_id The group id
  96. * @param string $group_name The group name
  97. * @param string $group_colour The group colour
  98. * @param mixed $custom_profile_url optional parameter to specify a profile url. The group id gets appended to this url as &amp;g={group_id}
  99. *
  100. * @return string A string consisting of what is wanted based on $mode.
  101. */
  102. public function get_name_string($mode, $group_id, $group_name, $group_colour = '', $custom_profile_url = false)
  103. {
  104. $s_is_bots = ($group_name === 'BOTS');
  105. // This switch makes sure we only run code required for the mode
  106. switch ($mode)
  107. {
  108. case 'full':
  109. case 'no_profile':
  110. case 'colour':
  111. // Build correct group colour
  112. $group_colour = $group_colour ? '#' . $group_colour : '';
  113. // Return colour
  114. if ($mode === 'colour')
  115. {
  116. $group_name_string = $group_colour;
  117. break;
  118. }
  119. // no break;
  120. case 'group_name':
  121. // Build correct group name
  122. $group_name = $this->get_name($group_name);
  123. // Return group name
  124. if ($mode === 'group_name')
  125. {
  126. $group_name_string = $group_name;
  127. break;
  128. }
  129. // no break;
  130. case 'profile':
  131. // Build correct profile url - only show if not anonymous and permission to view profile if registered user
  132. // For anonymous the link leads to a login page.
  133. if ($group_id && !$s_is_bots && ($this->user->data['user_id'] == ANONYMOUS || $this->auth->acl_get('u_viewprofile')))
  134. {
  135. $profile_url = ($custom_profile_url !== false) ? $custom_profile_url . '&amp;g=' . (int) $group_id : str_replace(array('={GROUP_ID}', '=%7BGROUP_ID%7D'), '=' . (int) $group_id, append_sid($this->name_strings['base_url']));
  136. }
  137. else
  138. {
  139. $profile_url = '';
  140. }
  141. // Return profile
  142. if ($mode === 'profile')
  143. {
  144. $group_name_string = $profile_url;
  145. break;
  146. }
  147. // no break;
  148. }
  149. if (!isset($group_name_string))
  150. {
  151. if (($mode === 'full' && empty($profile_url)) || $mode === 'no_profile' || $s_is_bots)
  152. {
  153. $group_name_string = str_replace(array('{GROUP_COLOUR}', '{GROUP_NAME}'), array($group_colour, $group_name), (!$group_colour) ? $this->name_strings['tpl_noprofile'] : $this->name_strings['tpl_noprofile_colour']);
  154. }
  155. else
  156. {
  157. $group_name_string = str_replace(array('{PROFILE_URL}', '{GROUP_COLOUR}', '{GROUP_NAME}'), array($profile_url, $group_colour, $group_name), (!$group_colour) ? $this->name_strings['tpl_profile'] : $this->name_strings['tpl_profile_colour']);
  158. }
  159. }
  160. $name_strings = $this->name_strings;
  161. /**
  162. * Use this event to change the output of the group name
  163. *
  164. * @event core.modify_group_name_string
  165. * @var string mode profile|group_name|colour|full|no_profile
  166. * @var int group_id The group identifier
  167. * @var string group_name The group name
  168. * @var string group_colour The group colour
  169. * @var string custom_profile_url Optional parameter to specify a profile url.
  170. * @var string group_name_string The string that has been generated
  171. * @var array name_strings Array of original return templates
  172. * @since 3.2.8-RC1
  173. */
  174. $vars = array(
  175. 'mode',
  176. 'group_id',
  177. 'group_name',
  178. 'group_colour',
  179. 'custom_profile_url',
  180. 'group_name_string',
  181. 'name_strings',
  182. );
  183. extract($this->dispatcher->trigger_event('core.modify_group_name_string', compact($vars)));
  184. return $group_name_string;
  185. }
  186. /**
  187. * Get group rank title and image
  188. *
  189. * @html Group rank image element
  190. *
  191. * @param array $group_data The current stored group data
  192. *
  193. * @return array An associative array containing the rank title (title),
  194. * the rank image as full img tag (img) and the rank image source (img_src)
  195. */
  196. public function get_rank($group_data)
  197. {
  198. $group_rank_data = array(
  199. 'title' => null,
  200. 'img' => null,
  201. 'img_src' => null,
  202. );
  203. /**
  204. * Preparing a group's rank before displaying
  205. *
  206. * @event core.get_group_rank_before
  207. * @var array group_data Array with group's data
  208. * @since 3.2.8-RC1
  209. */
  210. $vars = array('group_data');
  211. extract($this->dispatcher->trigger_event('core.get_group_rank_before', compact($vars)));
  212. if (!empty($group_data['group_rank']))
  213. {
  214. // Only obtain ranks if group rank is set
  215. $ranks = $this->cache->obtain_ranks();
  216. if (isset($ranks['special'][$group_data['group_rank']]))
  217. {
  218. $rank = $ranks['special'][$group_data['group_rank']];
  219. $group_rank_data['title'] = $rank['rank_title'];
  220. $group_rank_data['img_src'] = (!empty($rank['rank_image'])) ? $this->path_helper->update_web_root_path($this->phpbb_root_path . $this->config['ranks_path'] . '/' . $rank['rank_image']) : '';
  221. /** @html Group rank image element for usage in the template */
  222. $group_rank_data['img'] = (!empty($rank['rank_image'])) ? '<img src="' . $group_rank_data['img_src'] . '" alt="' . $rank['rank_title'] . '" title="' . $rank['rank_title'] . '" />' : '';
  223. }
  224. }
  225. /**
  226. * Modify a group's rank before displaying
  227. *
  228. * @event core.get_group_rank_after
  229. * @var array group_data Array with group's data
  230. * @var array group_rank_data Group rank data
  231. * @since 3.2.8-RC1
  232. */
  233. $vars = array(
  234. 'group_data',
  235. 'group_rank_data',
  236. );
  237. extract($this->dispatcher->trigger_event('core.get_group_rank_after', compact($vars)));
  238. return $group_rank_data;
  239. }
  240. /**
  241. * Get group avatar.
  242. * Wrapper function for \phpbb\avatar\helper::get_group_avatar()
  243. *
  244. * @param array $group_row Row from the groups table
  245. * @param string $alt Optional language string for alt tag within image, can be a language key or text
  246. * @param bool $ignore_config Ignores the config-setting, to be still able to view the avatar in the UCP
  247. * @param bool $lazy If true, will be lazy loaded (requires JS)
  248. *
  249. * @return array Avatar data
  250. */
  251. function get_avatar($group_row, $alt = 'GROUP_AVATAR', $ignore_config = false, $lazy = false)
  252. {
  253. return $this->avatar_helper->get_group_avatar($group_row, $alt, $ignore_config, $lazy);
  254. }
  255. }