DirectoryPlugin.php 7.5 KB

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