start.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. <?php
  2. /**
  3. * Elgg Video Plugin
  4. * This plugin allows users to create a library of videos
  5. *
  6. * @package Elgg
  7. * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
  8. * @author Prateek Choudhary <synapticfield@gmail.com>
  9. * @copyright Prateek Choudhary
  10. */
  11. elgg_register_event_handler('init', 'system', 'videolist_init');
  12. function videolist_init() {
  13. elgg_register_library('elgg:videolist', elgg_get_plugins_path() . 'videolist/lib/videolist.php');
  14. // add a site navigation item
  15. $item = new ElggMenuItem('videolist', elgg_echo('videolist'), 'videolist/all');
  16. elgg_register_menu_item('site', $item);
  17. // Extend system CSS with our own styles
  18. elgg_extend_view('css/elgg','videolist/css');
  19. // Register a page handler, so we can have nice URLs
  20. elgg_register_page_handler('videolist', 'videolist_page_handler');
  21. // Language short codes must be of the form "videolist:key"
  22. // where key is the array key below
  23. elgg_set_config('videolist', array(
  24. 'video_url' => 'url',
  25. 'title' => 'text',
  26. 'description' => 'longtext',
  27. 'tags' => 'tags',
  28. 'access_id' => 'access',
  29. ));
  30. // add to groups
  31. add_group_tool_option('videolist', elgg_echo('groups:enablevideolist'), true);
  32. elgg_extend_view('groups/tool_latest', 'videolist/group_module');
  33. //add a widget
  34. elgg_register_widget_type('videolist', elgg_echo('videolist'), elgg_echo('videolist:widget:description'));
  35. // Register granular notification for this type
  36. register_notification_object('object', 'videolist_item', elgg_echo('videolist:new'));
  37. // Register entity type for search
  38. elgg_register_entity_type('object', 'videolist_item');
  39. // add a file link to owner blocks
  40. elgg_register_plugin_hook_handler('register', 'menu:owner_block', 'videolist_owner_block_menu');
  41. elgg_register_event_handler('annotate','all','videolist_object_notifications');
  42. elgg_register_plugin_hook_handler('object:notifications','object','videolist_object_notifications_intercept');
  43. //register entity url handler
  44. elgg_register_entity_url_handler('object', 'videolist_item', 'videolist_url');
  45. elgg_register_plugin_hook_handler('entity:icon:url', 'object', 'videolist_icon_url_override');
  46. // register for embed
  47. elgg_register_plugin_hook_handler('embed_get_sections', 'all', 'videolist_embed_get_sections');
  48. elgg_register_plugin_hook_handler('embed_get_items', 'videolist', 'videolist_embed_get_items');
  49. // handle URLs without scheme
  50. elgg_register_plugin_hook_handler('videolist:preprocess', 'url', 'videolist_preprocess_url');
  51. // Register actions
  52. $actions_path = elgg_get_plugins_path() . "videolist/actions/videolist";
  53. elgg_register_action("videolist/add", "$actions_path/add.php");
  54. elgg_register_action("videolist/edit", "$actions_path/edit.php");
  55. elgg_register_action("videolist/delete", "$actions_path/delete.php");
  56. elgg_register_event_handler('upgrade', 'system', 'videolist_run_upgrades');
  57. }
  58. /**
  59. * Dispatches blog pages.
  60. * URLs take the form of
  61. * All videos: videolist/all
  62. * User's videos: videolist/owner/<username>
  63. * Friends' videos: videolist/friends/<username>
  64. * Video watch: videolist/watch/<guid>/<title>
  65. * Video browse: videolist/browse
  66. * New video: videolist/add/<guid>
  67. * Edit video: videolist/edit/<guid>
  68. * Group videos: videolist/group/<guid>/all
  69. *
  70. * Title is ignored
  71. *
  72. * @param array $page
  73. * @return NULL
  74. */
  75. function videolist_page_handler($page) {
  76. if (!isset($page[0])) {
  77. $page[0] = 'all';
  78. }
  79. $videolist_dir = elgg_get_plugins_path() . 'videolist/pages/videolist';
  80. $page_type = $page[0];
  81. switch ($page_type) {
  82. case 'owner':
  83. include "$videolist_dir/owner.php";
  84. break;
  85. case 'friends':
  86. include "$videolist_dir/friends.php";
  87. break;
  88. case 'watch':
  89. set_input('guid', $page[1]);
  90. include "$videolist_dir/watch.php";
  91. break;
  92. case 'add':
  93. include "$videolist_dir/add.php";
  94. break;
  95. case 'edit':
  96. set_input('guid', $page[1]);
  97. include "$videolist_dir/edit.php";
  98. break;
  99. case 'group':
  100. include "$videolist_dir/owner.php";
  101. break;
  102. case 'all':
  103. default:
  104. include "$videolist_dir/all.php";
  105. break;
  106. }
  107. return true;
  108. }
  109. /**
  110. * Add a menu item to the user ownerblock
  111. *
  112. * @param string $hook
  113. * @param string $type
  114. * @param array $return
  115. * @param array $params
  116. * @return array
  117. */
  118. function videolist_owner_block_menu($hook, $type, $return, $params) {
  119. if (elgg_instanceof($params['entity'], 'user')) {
  120. $url = "videolist/owner/{$params['entity']->username}";
  121. $item = new ElggMenuItem('videolist', elgg_echo('videolist'), $url);
  122. $return[] = $item;
  123. } else {
  124. if ($params['entity']->videolist_enable != "no") {
  125. $url = "videolist/group/{$params['entity']->guid}/all";
  126. $item = new ElggMenuItem('videolist', elgg_echo('videolist:group'), $url);
  127. $return[] = $item;
  128. }
  129. }
  130. return $return;
  131. }
  132. /**
  133. * @param ElggObject $videolist_item
  134. * @return string
  135. */
  136. function videolist_url($videolist_item) {
  137. $guid = $videolist_item->guid;
  138. $title = elgg_get_friendly_title($videolist_item->title);
  139. return elgg_get_site_url() . "videolist/watch/$guid/$title";
  140. }
  141. /**
  142. * Event handler for videolist
  143. *
  144. * @param string $event
  145. * @param string $object_type
  146. * @param ElggObject $object
  147. */
  148. function videolist_object_notifications($event, $object_type, $object) {
  149. static $flag;
  150. if (!isset($flag)) {
  151. $flag = 0;
  152. }
  153. if (is_callable('object_notifications')) {
  154. if ($object instanceof ElggObject) {
  155. if ($object->getSubtype() == 'videolist_item') {
  156. if ($flag == 0) {
  157. $flag = 1;
  158. object_notifications($event, $object_type, $object);
  159. }
  160. }
  161. }
  162. }
  163. }
  164. /**
  165. * Intercepts the notification on an event of new video being created and prevents a notification from going out
  166. * (because one will be sent on the annotation)
  167. *
  168. * @param string $hook
  169. * @param string $entity_type
  170. * @param array $returnvalue
  171. * @param array $params
  172. * @return bool
  173. */
  174. function videolist_object_notifications_intercept($hook, $entity_type, $returnvalue, $params) {
  175. if (isset($params)) {
  176. if ($params['event'] == 'create' && $params['object'] instanceof ElggObject) {
  177. if ($params['object']->getSubtype() == 'videolist_item') {
  178. return true;
  179. }
  180. }
  181. }
  182. return null;
  183. }
  184. /**
  185. * Register videolist as an embed type.
  186. *
  187. * @param string $hook
  188. * @param string $type
  189. * @param array $value
  190. * @param array $params
  191. * @return array
  192. */
  193. function videolist_embed_get_sections($hook, $type, $value, $params) {
  194. $value['videolist'] = array(
  195. 'name' => elgg_echo('videolist'),
  196. 'layout' => 'list',
  197. 'icon_size' => 'medium',
  198. );
  199. return $value;
  200. }
  201. /**
  202. * Return a list of videos for embedding
  203. *
  204. * @param string $hook
  205. * @param string $type
  206. * @param array $value
  207. * @param array $params
  208. * @return array
  209. */
  210. function videolist_embed_get_items($hook, $type, $value, $params) {
  211. $options = array(
  212. 'owner_guid' => elgg_get_logged_in_user_guid(),
  213. 'type_subtype_pair' => array('object' => 'videolist_item'),
  214. 'count' => TRUE
  215. );
  216. $count = elgg_get_entities($options);
  217. $value['count'] += $count;
  218. unset($options['count']);
  219. $options['offset'] = $params['offset'];
  220. $options['limit'] = $params['limit'];
  221. $items = elgg_get_entities($options);
  222. $value['items'] = array_merge($items, $value['items']);
  223. return $value;
  224. }
  225. /**
  226. * Override the default entity icon for videoslist items
  227. *
  228. * @param string $hook
  229. * @param string $type
  230. * @param string $returnvalue
  231. * @param array $params
  232. * @return string Relative URL
  233. */
  234. function videolist_icon_url_override($hook, $type, $returnvalue, $params) {
  235. // if someone already set this, quit
  236. if ($return_value) {
  237. return null;
  238. }
  239. $videolist_item = $params['entity'];
  240. $size = $params['size'];
  241. if (!elgg_instanceof($videolist_item, 'object', 'videolist_item')) {
  242. return null;
  243. }
  244. // tiny thumbnails are too small to be useful, so give a generic video icon
  245. try {
  246. if ($size != 'tiny' && isset($videolist_item->thumbnail)) {
  247. $owner = $videolist_item->getOwnerEntity();
  248. $owner_guid = $owner->getGUID();
  249. $join_date = $owner->getTimeCreated();
  250. return "mod/videolist/thumbnail.php?joindate=$join_date&guid={$videolist_item->guid}&owner_guid=$owner_guid&size=$size";
  251. }
  252. } catch (InvalidParameterException $e) {
  253. elgg_log("Unable to get videolist icon for video with GUID {$videolist_item->guid}", 'ERROR');
  254. return "mod/videolist/graphics/videolist_icon_{$size}.png";
  255. }
  256. if (in_array($size, array('tiny', 'small', 'medium'))){
  257. return "mod/videolist/graphics/videolist_icon_{$size}.png";
  258. }
  259. return null;
  260. }
  261. /**
  262. * Prepend HTTP scheme if missing
  263. * @param string $hook
  264. * @param string $type
  265. * @param string $returnvalue
  266. * @param array $params
  267. * @return string
  268. */
  269. function videolist_preprocess_url($hook, $type, $returnvalue, $params) {
  270. $parsed = parse_url($returnvalue);
  271. if (empty($parsed['host']) && ! empty($parsed['path']) && $parsed['path'][0] !== '/') {
  272. // user probably forgot scheme
  273. $returnvalue = 'https://' . $returnvalue;
  274. }
  275. return $returnvalue;
  276. }
  277. /**
  278. * Process upgrades for the videolist plugin
  279. */
  280. function videolist_run_upgrades($event, $type, $details) {
  281. if (include_once(elgg_get_plugins_path() . 'upgrade-tools/lib/upgrade_tools.php')) {
  282. upgrade_module_run('videolist');
  283. }
  284. }