hooks.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. /**
  3. * All plugin hook handlers are bundled here
  4. */
  5. /**
  6. * Override canEditMetadata
  7. *
  8. * @param string $hook the name of the hook
  9. * @param string $type the type of the hook
  10. * @param bool $returnvalue current return value
  11. * @param array $params supplied params
  12. *
  13. * @return bool
  14. */
  15. function file_tools_can_edit_metadata_hook($hook, $type, $returnvalue, $params) {
  16. if ($returnvalue) {
  17. // already have access
  18. return $returnvalue;
  19. }
  20. if (empty($params) || !is_array($params)) {
  21. // invalid input
  22. return $returnvalue;
  23. }
  24. $entity = elgg_extract("entity", $params);
  25. $user = elgg_extract("user", $params);
  26. if (empty($user) || !elgg_instanceof($user, "user")) {
  27. return $returnvalue;
  28. }
  29. if (empty($entity) || !elgg_instanceof($entity, "object", FILE_TOOLS_SUBTYPE)) {
  30. return $returnvalue;
  31. }
  32. $container_entity = $entity->getContainerEntity();
  33. if (empty($container_entity) || !elgg_instanceof($container_entity, "group")) {
  34. return $returnvalue;
  35. }
  36. if ($container_entity->isMember($user) && ($container_entity->file_tools_structure_management_enable != "no")) {
  37. // is group member
  38. $returnvalue = true;
  39. }
  40. return $returnvalue;
  41. }
  42. /**
  43. * override the folder icon url
  44. *
  45. * @param string $hook the name of the hook
  46. * @param string $type the type of the hook
  47. * @param string $returnvalue current return value
  48. * @param array $params supplied params
  49. *
  50. * @return string
  51. */
  52. function file_tools_folder_icon_hook($hook, $type, $returnvalue, $params) {
  53. if (empty($params) || !is_array($params)) {
  54. return $returnvalue;
  55. }
  56. $entity = elgg_extract("entity", $params);
  57. $size = elgg_extract("size", $params, "small");
  58. if (empty($entity) || !elgg_instanceof($entity, "object", FILE_TOOLS_SUBTYPE)) {
  59. return $returnvalue;
  60. }
  61. switch($size){
  62. case "topbar":
  63. case "tiny":
  64. case "small":
  65. $returnvalue = "mod/file_tools/_graphics/folder/" . $size . ".png";
  66. break;
  67. default:
  68. $returnvalue = "mod/file_tools/_graphics/folder/medium.png";
  69. break;
  70. }
  71. return $returnvalue;
  72. }
  73. /**
  74. * change the folder access options
  75. *
  76. * @param string $hook the name of the hook
  77. * @param string $type the type of the hook
  78. * @param array $returnvalue current return value
  79. * @param array $params supplied params
  80. *
  81. * @return array
  82. */
  83. function file_tools_write_acl_plugin_hook($hook, $type, $returnvalue, $params) {
  84. if (empty($params) || !is_array($params)) {
  85. return $returnvalue;
  86. }
  87. if (!elgg_in_context("file_tools")) {
  88. return $returnvalue;
  89. }
  90. $page_owner = elgg_get_page_owner_entity();
  91. if (empty($page_owner) || !elgg_instanceof($page_owner, "group")) {
  92. return $returnvalue;
  93. }
  94. $returnvalue = array(
  95. $page_owner->group_acl => elgg_echo("groups:group") . ": " . $page_owner->name,
  96. ACCESS_LOGGED_IN => elgg_echo("LOGGED_IN"),
  97. ACCESS_PUBLIC => elgg_echo("PUBLIC")
  98. );
  99. return $returnvalue;
  100. }
  101. /**
  102. * Listen to the file pagehandler
  103. *
  104. * @param string $hook the name of the hook
  105. * @param string $type the type of the hook
  106. * @param array $returnvalue current return value
  107. * @param array $params supplied params
  108. *
  109. * @return array|bool
  110. */
  111. function file_tools_file_route_hook($hook, $type, $returnvalue, $params) {
  112. if (empty($returnvalue) || !is_array($returnvalue)) {
  113. return $returnvalue;
  114. }
  115. $page = elgg_extract("segments", $returnvalue);
  116. switch ($page[0]) {
  117. case "view":
  118. if (!elgg_is_logged_in() && isset($page[1])) {
  119. if (!get_entity($page[1])) {
  120. gatekeeper();
  121. }
  122. }
  123. break;
  124. case "owner":
  125. if (file_tools_use_folder_structure()) {
  126. $returnvalue = false;
  127. include(dirname(dirname(__FILE__)) . "/pages/list.php");
  128. }
  129. break;
  130. case "group":
  131. if (file_tools_use_folder_structure()) {
  132. $returnvalue = false;
  133. include(dirname(dirname(__FILE__)) . "/pages/list.php");
  134. }
  135. break;
  136. case "add":
  137. $returnvalue = false;
  138. include(dirname(dirname(__FILE__)) . "/pages/file/new.php");
  139. break;
  140. case "zip":
  141. if (isset($page[1])) {
  142. $returnvalue = false;
  143. elgg_set_page_owner_guid($page[1]);
  144. register_error(elgg_echo("changebookmark"));
  145. forward("file/add/" . $page[1] . "?upload_type=zip");
  146. }
  147. break;
  148. case "bulk_download":
  149. $returnvalue = false;
  150. include(dirname(dirname(__FILE__)) . "/pages/file/download.php");
  151. break;
  152. }
  153. return $returnvalue;
  154. }
  155. /**
  156. * Set correct url on widgets
  157. *
  158. * @param string $hook the name of the hook
  159. * @param string $type the type of the hook
  160. * @param string $returnvalue current return value
  161. * @param array $params supplied params
  162. *
  163. * @return string
  164. */
  165. function file_tools_widget_url_hook($hook, $type, $returnvalue, $params) {
  166. if (!empty($returnvalue)) {
  167. return $returnvalue;
  168. }
  169. if (empty($params) || !is_array($params)) {
  170. return $returnvalue;
  171. }
  172. $widget = elgg_extract("entity", $params);
  173. if (empty($widget) || !elgg_instanceof($widget, "object", "widget")) {
  174. return $returnvalue;
  175. }
  176. $owner = $widget->getOwnerEntity();
  177. switch ($widget->handler) {
  178. case "file_tree":
  179. if (elgg_instanceof($owner, "user")) {
  180. $returnvalue = "file/owner/" . $owner->username;
  181. } elseif (elgg_instanceof($owner, "group")) {
  182. $returnvalue = "file/group/" . $owner->getGUID() . "/all";
  183. }
  184. break;
  185. case "filerepo":
  186. if (elgg_instanceof($owner, "user")) {
  187. $returnvalue = "file/owner/" . $owner->username;
  188. } elseif (elgg_instanceof($owner, "group")) {
  189. $returnvalue = "file/group/" . $owner->getGUID() . "/all";
  190. }
  191. break;
  192. case "group_files":
  193. $returnvalue = "file/group/" . $owner->getGUID() . "/all";
  194. break;
  195. case "index_file":
  196. $returnvalue = "file/all";
  197. break;
  198. }
  199. return $returnvalue;
  200. }
  201. /**
  202. * Set folder breadcrumb menu
  203. *
  204. * @param string $hook the name of the hook
  205. * @param string $type the type of the hook
  206. * @param ElggMenuItem[] $returnvalue current return value
  207. * @param array $params supplied params
  208. *
  209. * @return ElggMenuItem[]
  210. */
  211. function file_tools_folder_breadcrumb_hook($hook, $type, $returnvalue, $params) {
  212. if (empty($params) || !is_array($params)) {
  213. return $returnvalue;
  214. }
  215. $folder = elgg_extract("entity", $params);
  216. if (!empty($folder) && elgg_instanceof($folder, "object", FILE_TOOLS_SUBTYPE)) {
  217. $container = $folder->getContainerEntity();
  218. $priority = 9999999;
  219. $folder_options = array(
  220. "name" => "folder_" . $folder->getGUID(),
  221. "text" => $folder->title,
  222. "href" => false,
  223. "priority" => $priority
  224. );
  225. $returnvalue[] = ElggMenuItem::factory($folder_options);
  226. $parent_guid = (int) $folder->parent_guid;
  227. while (!empty($parent_guid) && ($parent = get_entity($parent_guid))) {
  228. $priority--;
  229. $folder_options = array(
  230. "name" => "folder_" . $parent->getGUID(),
  231. "text" => $parent->title,
  232. "href" => $parent->getURL(),
  233. "priority" => $priority
  234. );
  235. $returnvalue[] = ElggMenuItem::factory($folder_options);
  236. $parent_guid = (int) $parent->parent_guid;
  237. }
  238. } else {
  239. $container = elgg_get_page_owner_entity();
  240. }
  241. // make main folder item
  242. $main_folder_options = array(
  243. "name" => "main_folder",
  244. "text" => elgg_echo("file_tools:list:folder:main"),
  245. "priority" => 0
  246. );
  247. if (elgg_instanceof($container, "group")) {
  248. $main_folder_options["href"] = "file/group/" . $container->getGUID() . "/all#";
  249. } else {
  250. $main_folder_options["href"] = "file/owner/" . $container->username . "/all#";
  251. }
  252. $returnvalue[] = ElggMenuItem::factory($main_folder_options);
  253. return $returnvalue;
  254. }
  255. /**
  256. * build the folder tree
  257. *
  258. * @param string $hook the name of the hook
  259. * @param string $type the type of the hook
  260. * @param ElggMenuItem[] $returnvalue current return value
  261. * @param array $params supplied params
  262. *
  263. * @return ElggMenuItem[]
  264. */
  265. function file_tools_folder_sidebar_tree_hook($hook, $type, $returnvalue, $params) {
  266. if (empty($params) || !is_array($params)) {
  267. return $returnvalue;
  268. }
  269. $container = elgg_extract("container", $params);
  270. if (empty($container) || (!elgg_instanceof($container, "user") && !elgg_instanceof($container, "group"))) {
  271. return $returnvalue;
  272. }
  273. $main_menu_item = ElggMenuItem::factory(array(
  274. "name" => "root",
  275. "text" => elgg_echo("file_tools:list:folder:main"),
  276. "href" => "#",
  277. "id" => "0",
  278. "rel" => "root",
  279. "priority" => 0
  280. ));
  281. if ($folders = file_tools_get_folders($container->getGUID())) {
  282. $main_menu_item->setChildren(file_tools_make_menu_items($folders));
  283. }
  284. $returnvalue[] = $main_menu_item;
  285. return $returnvalue;
  286. }
  287. /**
  288. * change the entity menu options
  289. *
  290. * @param string $hook the name of the hook
  291. * @param string $type the type of the hook
  292. * @param ElggMenuItem[] $returnvalue current return value
  293. * @param array $params supplied params
  294. *
  295. * @return ElggMenuItem[]
  296. */
  297. function file_tools_entity_menu_hook($hook, $type, $returnvalue, $params) {
  298. if (empty($params) || !is_array($params)) {
  299. return $returnvalue;
  300. }
  301. $entity = elgg_extract("entity", $params);
  302. if (empty($entity) || !elgg_instanceof($entity, "object")) {
  303. return $returnvalue;
  304. }
  305. if (elgg_instanceof($entity, "object", FILE_TOOLS_SUBTYPE)) {
  306. $remove_items = array("like", "unlike");
  307. foreach ($returnvalue as $index => $menu_item) {
  308. if (in_array($menu_item->getName(), $remove_items)) {
  309. unset($returnvalue[$index]);
  310. }
  311. }
  312. } elseif (elgg_instanceof($entity, "object", "file")) {
  313. $returnvalue[] = ElggMenuItem::factory(array(
  314. "name" => "download",
  315. "text" => elgg_view_icon("download"),
  316. "href" => "file/download/" . $entity->getGUID(),
  317. "title" => elgg_echo("file:download"),
  318. "priority" => 200
  319. ));
  320. }
  321. return $returnvalue;
  322. }
  323. /**
  324. * Set correct url for folder entities
  325. *
  326. * @param string $hook the name of the hook
  327. * @param string $type the type of the hook
  328. * @param string $returnvalue current return value
  329. * @param array $params supplied params
  330. *
  331. * @return string
  332. */
  333. function file_tools_folder_url_handler($hook, $type, $returnvalue, $params) {
  334. if (empty($params) || !is_array($params)) {
  335. return $returnvalue;
  336. }
  337. $entity = elgg_extract("entity", $params);
  338. if (empty($entity) || !elgg_instanceof($entity, "object", FILE_TOOLS_SUBTYPE)) {
  339. return $returnvalue;
  340. }
  341. $container = $entity->getContainerEntity();
  342. if (elgg_instanceof($container, "group")) {
  343. $returnvalue = "file/group/" . $container->getGUID() . "/all#" . $entity->getGUID();
  344. } else {
  345. $returnvalue = "file/owner/" . $container->username . "#" . $entity->getGUID();
  346. }
  347. return $returnvalue;
  348. }