DirectoryPlugin.php 7.4 KB

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