DirectoryPlugin.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Adds a user directory
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Plugin
  24. * @package StatusNet
  25. * @author Zach Copely <zach@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. exit(1);
  32. }
  33. /**
  34. * Directory plugin main class
  35. *
  36. * @category Plugin
  37. * @package StatusNet
  38. * @author Zach Copley <zach@status.net>
  39. * @copyright 2011 StatusNet, Inc.
  40. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  41. * @link http://status.net/
  42. */
  43. class DirectoryPlugin extends Plugin
  44. {
  45. const PLUGIN_VERSION = '2.0.0';
  46. private $dir = null;
  47. /**
  48. * Initializer for this plugin
  49. *
  50. * @return boolean hook value; true means continue processing,
  51. * false means stop.
  52. */
  53. function initialize()
  54. {
  55. return true;
  56. }
  57. /**
  58. * Cleanup for this plugin.
  59. *
  60. * @return boolean hook value; true means continue processing,
  61. * false means stop.
  62. */
  63. function cleanup()
  64. {
  65. return true;
  66. }
  67. /**
  68. * Map URLs to actions
  69. *
  70. * @param URLMapper $m path-to-action mapper
  71. *
  72. * @return boolean hook value; true means continue processing,
  73. * false means stop.
  74. */
  75. public function onRouterInitialized(URLMapper $m)
  76. {
  77. $m->connect('directory/users/:filter/sort_by/:sort/reverse/:reverse',
  78. ['action' => 'userdirectory'],
  79. ['filter' => '[0-9a-zA-Z]|(0-9)',
  80. 'sort' => '[a-z]+',
  81. 'reverse' => '[0-9]']);
  82. $m->connect('directory/users/:filter/sort_by/:sort',
  83. ['action' => 'userdirectory'],
  84. ['filter' => '[0-9a-zA-Z]|(0-9)',
  85. 'sort' => '[a-z]+']);
  86. $m->connect('directory/users/:filter',
  87. ['action' => 'userdirectory'],
  88. ['filter' => '[0-9a-zA-Z]|(0-9)']);
  89. $m->connect('directory/users/sort_by/:sort/reverse/:reverse',
  90. ['action' => 'userdirectory'],
  91. ['sort' => '[a-z]+',
  92. 'reverse' => '[0-9]']);
  93. $m->connect('directory/users/sort_by/:sort',
  94. ['action' => 'userdirectory'],
  95. ['sort' => '[a-z]+']);
  96. $m->connect('directory/users',
  97. ['action' => 'userdirectory']);
  98. $m->connect('groups/:filter',
  99. ['action' => 'groupdirectory'],
  100. ['filter' => '[0-9a-zA-Z]|(0-9)']);
  101. $m->connect('groups',
  102. ['action' => 'groupdirectory']);
  103. $m->connect('groups/all',
  104. ['action' => 'groupdirectory']);
  105. return true;
  106. }
  107. /**
  108. * Hijack the routing (URL -> Action) for the normal directory page
  109. * and substitute our group directory action
  110. *
  111. * @param string $path path to connect
  112. * @param array $defaults path defaults
  113. * @param array $rules path rules
  114. * @param array $result unused
  115. *
  116. * @return boolean hook return
  117. */
  118. function onStartConnectPath(&$path, &$defaults, &$rules, &$result)
  119. {
  120. if (in_array($path, array('group', 'group/', 'groups', 'groups/'))) {
  121. $defaults['action'] = 'groupdirectory';
  122. return true;
  123. }
  124. return true;
  125. }
  126. // The following three function are to replace the existing groups
  127. // list page with the directory plugin's group directory page
  128. /**
  129. * Hijack the mapping (Action -> URL) and return the URL to our
  130. * group directory page instead of the normal groups page
  131. *
  132. * @param Action $action action to find a path for
  133. * @param array $params parameters to pass to the action
  134. * @param string $fragment any url fragement
  135. * @param boolean $addSession whether to add session variable
  136. * @param string $url resulting URL to local resource
  137. *
  138. * @return string the local URL
  139. */
  140. function onEndLocalURL(&$action, &$params, &$fragment, &$addSession, &$url) {
  141. if (in_array($action, array('group', 'group/', 'groups', 'groups/'))) {
  142. $url = common_local_url('groupdirectory');
  143. }
  144. return true;
  145. }
  146. /**
  147. * Link in a styelsheet for the onboarding actions
  148. *
  149. * @param Action $action Action being shown
  150. *
  151. * @return boolean hook flag
  152. */
  153. public function onEndShowStylesheets(Action $action)
  154. {
  155. if (in_array(
  156. $action->trimmed('action'),
  157. array('userdirectory', 'groupdirectory'))
  158. ) {
  159. $action->cssLink($this->path('css/directory.css'));
  160. }
  161. return true;
  162. }
  163. /**
  164. * Fool the public nav into thinking it's on the regular
  165. * group page when it's actually on our injected group
  166. * directory page. This way "Groups" gets hilighted when
  167. * when we're on the groups directory page.
  168. *
  169. * @param type $action the current action
  170. *
  171. * @return boolean hook flag
  172. */
  173. function onStartPublicGroupNav($action)
  174. {
  175. if ($action->trimmed('action') == 'groupdirectory') {
  176. $action->actionName = 'groups';
  177. }
  178. return true;
  179. }
  180. /**
  181. * Modify the public local nav to add a link to the user directory
  182. *
  183. * @param Action $action The current action handler. Use this to
  184. * do any output.
  185. *
  186. * @return boolean hook value; true means continue processing,
  187. * false means stop.
  188. *
  189. * @see Action
  190. */
  191. function onEndPublicGroupNav($nav)
  192. {
  193. // XXX: Maybe this should go under search instead?
  194. $actionName = $nav->action->trimmed('action');
  195. $nav->out->menuItem(
  196. common_local_url('userdirectory'),
  197. // TRANS: Menu item text for user directory.
  198. _m('MENU','People'),
  199. // TRANS: Menu item title for user directory.
  200. _m('People.'),
  201. $actionName == 'userdirectory',
  202. 'nav_directory'
  203. );
  204. return true;
  205. }
  206. /*
  207. * Version info
  208. */
  209. public function onPluginVersion(array &$versions): bool
  210. {
  211. $versions[] = array(
  212. 'name' => 'Directory',
  213. 'version' => self::PLUGIN_VERSION,
  214. 'author' => 'Zach Copley',
  215. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/Directory',
  216. // TRANS: Plugin description.
  217. 'rawdescription' => _m('Add a user directory.')
  218. );
  219. return true;
  220. }
  221. }