PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/phpbb/group/helper.php

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