DirectoryPlugin.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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(
  78. 'directory/users/:filter/sort_by/:sort/reverse/:reverse',
  79. array('action' => 'userdirectory'),
  80. array('filter' => '[0-9a-zA-Z]|(0-9)'),
  81. array('sort' => '[a-z]+'),
  82. array('reverse' => '[0-9]')
  83. );
  84. $m->connect(
  85. 'directory/users/:filter/sort_by/:sort',
  86. array('action' => 'userdirectory'),
  87. array('filter' => '[0-9a-zA-Z]|(0-9)'),
  88. array('sort' => '[a-z]+')
  89. );
  90. $m->connect(
  91. 'directory/users/:filter',
  92. array('action' => 'userdirectory'),
  93. array('filter' => '[0-9a-zA-Z]|(0-9)')
  94. );
  95. $m->connect(
  96. 'directory/users/sort_by/:sort/reverse/:reverse',
  97. array('action' => 'userdirectory'),
  98. array('sort' => '[a-z]+'),
  99. array('reverse' => '[0-9]')
  100. );
  101. $m->connect(
  102. 'directory/users/sort_by/:sort',
  103. array('action' => 'userdirectory'),
  104. array('sort' => '[a-z]+')
  105. );
  106. $m->connect(
  107. 'directory/users',
  108. array('action' => 'userdirectory')
  109. );
  110. $m->connect(
  111. 'groups/:filter',
  112. array('action' => 'groupdirectory'),
  113. array('filter' => '[0-9a-zA-Z]|(0-9)')
  114. );
  115. $m->connect(
  116. 'groups',
  117. array('action' => 'groupdirectory')
  118. );
  119. $m->connect(
  120. 'groups/all',
  121. array('action' => 'groupdirectory')
  122. );
  123. return true;
  124. }
  125. /**
  126. * Hijack the routing (URL -> Action) for the normal directory page
  127. * and substitute our group directory action
  128. *
  129. * @param string $path path to connect
  130. * @param array $defaults path defaults
  131. * @param array $rules path rules
  132. * @param array $result unused
  133. *
  134. * @return boolean hook return
  135. */
  136. function onStartConnectPath(&$path, &$defaults, &$rules, &$result)
  137. {
  138. if (in_array($path, array('group', 'group/', 'groups', 'groups/'))) {
  139. $defaults['action'] = 'groupdirectory';
  140. return true;
  141. }
  142. return true;
  143. }
  144. // The following three function are to replace the existing groups
  145. // list page with the directory plugin's group directory page
  146. /**
  147. * Hijack the mapping (Action -> URL) and return the URL to our
  148. * group directory page instead of the normal groups page
  149. *
  150. * @param Action $action action to find a path for
  151. * @param array $params parameters to pass to the action
  152. * @param string $fragment any url fragement
  153. * @param boolean $addSession whether to add session variable
  154. * @param string $url resulting URL to local resource
  155. *
  156. * @return string the local URL
  157. */
  158. function onEndLocalURL(&$action, &$params, &$fragment, &$addSession, &$url) {
  159. if (in_array($action, array('group', 'group/', 'groups', 'groups/'))) {
  160. $url = common_local_url('groupdirectory');
  161. }
  162. return true;
  163. }
  164. /**
  165. * Link in a styelsheet for the onboarding actions
  166. *
  167. * @param Action $action Action being shown
  168. *
  169. * @return boolean hook flag
  170. */
  171. public function onEndShowStylesheets(Action $action)
  172. {
  173. if (in_array(
  174. $action->trimmed('action'),
  175. array('userdirectory', 'groupdirectory'))
  176. ) {
  177. $action->cssLink($this->path('css/directory.css'));
  178. }
  179. return true;
  180. }
  181. /**
  182. * Fool the public nav into thinking it's on the regular
  183. * group page when it's actually on our injected group
  184. * directory page. This way "Groups" gets hilighted when
  185. * when we're on the groups directory page.
  186. *
  187. * @param type $action the current action
  188. *
  189. * @return boolean hook flag
  190. */
  191. function onStartPublicGroupNav($action)
  192. {
  193. if ($action->trimmed('action') == 'groupdirectory') {
  194. $action->actionName = 'groups';
  195. }
  196. return true;
  197. }
  198. /**
  199. * Modify the public local nav to add a link to the user directory
  200. *
  201. * @param Action $action The current action handler. Use this to
  202. * do any output.
  203. *
  204. * @return boolean hook value; true means continue processing,
  205. * false means stop.
  206. *
  207. * @see Action
  208. */
  209. function onEndPublicGroupNav($nav)
  210. {
  211. // XXX: Maybe this should go under search instead?
  212. $actionName = $nav->action->trimmed('action');
  213. $nav->out->menuItem(
  214. common_local_url('userdirectory'),
  215. // TRANS: Menu item text for user directory.
  216. _m('MENU','People'),
  217. // TRANS: Menu item title for user directory.
  218. _m('People.'),
  219. $actionName == 'userdirectory',
  220. 'nav_directory'
  221. );
  222. return true;
  223. }
  224. /*
  225. * Version info
  226. */
  227. function onPluginVersion(array &$versions)
  228. {
  229. $versions[] = array(
  230. 'name' => 'Directory',
  231. 'version' => self::PLUGIN_VERSION,
  232. 'author' => 'Zach Copley',
  233. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/Directory',
  234. // TRANS: Plugin description.
  235. 'rawdescription' => _m('Add a user directory.')
  236. );
  237. return true;
  238. }
  239. }