start.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * Elgg media embed plugin
  4. *
  5. * @package ElggEmbed
  6. */
  7. elgg_register_event_handler('init', 'system', 'embed_init');
  8. /**
  9. * Init function
  10. */
  11. function embed_init() {
  12. elgg_extend_view('css/elgg', 'embed/css');
  13. elgg_extend_view('css/admin', 'embed/css');
  14. if (elgg_is_logged_in()) {
  15. elgg_register_plugin_hook_handler('register', 'menu:longtext', 'embed_longtext_menu');
  16. }
  17. elgg_register_plugin_hook_handler('register', 'menu:embed', 'embed_select_tab', 1000);
  18. // Page handler for the modal media embed
  19. elgg_register_page_handler('embed', 'embed_page_handler');
  20. $embed_js = elgg_get_simplecache_url('js', 'embed/embed');
  21. elgg_register_js('elgg.embed', $embed_js, 'footer');
  22. }
  23. /**
  24. * Add the embed menu item to the long text menu
  25. *
  26. * @param string $hook
  27. * @param string $type
  28. * @param array $items
  29. * @param array $vars
  30. * @return array
  31. */
  32. function embed_longtext_menu($hook, $type, $items, $vars) {
  33. if (elgg_get_context() == 'embed') {
  34. return $items;
  35. }
  36. $url = 'embed';
  37. $page_owner = elgg_get_page_owner_entity();
  38. if (elgg_instanceof($page_owner, 'group') && $page_owner->isMember()) {
  39. $url = 'embed?container_guid=' . $page_owner->getGUID();
  40. }
  41. elgg_load_js('lightbox');
  42. elgg_load_css('lightbox');
  43. elgg_require_js('jquery.form');
  44. elgg_load_js('elgg.embed');
  45. $text = elgg_echo('embed:media');
  46. // if loaded through ajax (like on /activity), pull in JS libs manually
  47. // hack for #6422 because we haven't converted everything to amd yet
  48. if (elgg_in_context('ajax')) {
  49. $externals = elgg_get_config('externals_map');
  50. $embed = elgg_extract('elgg.embed', $externals['js']);
  51. $lightbox_js = elgg_extract('lightbox', $externals['js']);
  52. $lightbox_css = elgg_extract('lightbox', $externals['css']);
  53. $text .= <<<___JS
  54. <script>
  55. require(['jquery.form']);
  56. if (typeof $.fancybox === 'undefined') {
  57. $.getScript('$lightbox_js->url');
  58. $('head').append('<link rel="stylesheet" href="$lightbox_css->url"></link>');
  59. }
  60. if (typeof elgg.embed === 'undefined') {
  61. $.getScript('$embed->url');
  62. }
  63. </script>
  64. ___JS;
  65. }
  66. $items[] = ElggMenuItem::factory(array(
  67. 'name' => 'embed',
  68. 'href' => 'javascript:void()',
  69. 'data-colorbox-opts' => json_encode([
  70. 'href' => elgg_normalize_url($url),
  71. ]),
  72. 'text' => $text,
  73. 'rel' => "embed-lightbox-{$vars['id']}",
  74. 'link_class' => "elgg-longtext-control elgg-lightbox embed-control embed-control-{$vars['id']}",
  75. 'priority' => 10,
  76. ));
  77. return $items;
  78. }
  79. /**
  80. * Select the correct embed tab for display
  81. *
  82. * @param string $hook
  83. * @param string $type
  84. * @param array $items
  85. * @param array $vars
  86. */
  87. function embed_select_tab($hook, $type, $items, $vars) {
  88. // can this ba called from page handler instead?
  89. $page = get_input('page');
  90. $tab_name = array_pop(explode('/', $page));
  91. foreach ($items as $item) {
  92. if ($item->getName() == $tab_name) {
  93. $item->setSelected();
  94. elgg_set_config('embed_tab', $item);
  95. }
  96. }
  97. if (!elgg_get_config('embed_tab') && count($items) > 0) {
  98. $items[0]->setSelected();
  99. elgg_set_config('embed_tab', $items[0]);
  100. }
  101. }
  102. /**
  103. * Serves the content for the embed lightbox
  104. *
  105. * @param array $page URL segments
  106. */
  107. function embed_page_handler($page) {
  108. $container_guid = (int)get_input('container_guid');
  109. if ($container_guid) {
  110. $container = get_entity($container_guid);
  111. if (elgg_instanceof($container, 'group') && $container->isMember()) {
  112. // embedding inside a group so save file to group files
  113. elgg_set_page_owner_guid($container_guid);
  114. }
  115. }
  116. set_input('page', $page[1]);
  117. echo elgg_view('embed/layout');
  118. // exit because this is in a modal display.
  119. exit;
  120. }
  121. /**
  122. * A special listing function for selectable content
  123. *
  124. * This calls a custom list view for entities.
  125. *
  126. * @param array $entities Array of ElggEntity objects
  127. * @param array $vars Display parameters
  128. * @return string
  129. */
  130. function embed_list_items($entities, $vars = array()) {
  131. $defaults = array(
  132. 'items' => $entities,
  133. 'list_class' => 'elgg-list-entity',
  134. );
  135. $vars = array_merge($defaults, $vars);
  136. return elgg_view('embed/list', $vars);
  137. }
  138. /**
  139. * Set the options for the list of embedable content
  140. *
  141. * @param array $options
  142. * @return array
  143. */
  144. function embed_get_list_options($options = array()) {
  145. $container_guids = array(elgg_get_logged_in_user_guid());
  146. if (elgg_get_page_owner_guid()) {
  147. $page_owner_guid = elgg_get_page_owner_guid();
  148. if ($page_owner_guid != elgg_get_logged_in_user_guid()) {
  149. $container_guids[] = $page_owner_guid;
  150. }
  151. }
  152. $defaults = array(
  153. 'limit' => 6,
  154. 'container_guids' => $container_guids,
  155. 'item_class' => 'embed-item',
  156. );
  157. $options = array_merge($defaults, $options);
  158. return $options;
  159. }