views.php 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673
  1. <?php
  2. /**
  3. * Elgg's view system.
  4. *
  5. * The view system is the primary templating engine in Elgg and renders
  6. * all output. Views are short, parameterised PHP scripts for displaying
  7. * output that can be regsitered, overridden, or extended. The view type
  8. * determines the output format and location of the files that renders the view.
  9. *
  10. * Elgg uses a two step process to render full output: first
  11. * content-specific elements are rendered, then the resulting
  12. * content is inserted into a layout and displayed. This makes it
  13. * easy to maintain a consistent look on all pages.
  14. *
  15. * A view corresponds to a single file on the filesystem and the views
  16. * name is its directory structure. A file in
  17. * <code>mod/plugins/views/default/myplugin/example.php</code>
  18. * is called by saying (with the default viewtype):
  19. * <code>echo elgg_view('myplugin/example');</code>
  20. *
  21. * View names that are registered later override those that are
  22. * registered earlier. For plugins this corresponds directly
  23. * to their load order: views in plugins lower in the list override
  24. * those higher in the list.
  25. *
  26. * Plugin views belong in the views/ directory under an appropriate
  27. * viewtype. Views are automatically registered.
  28. *
  29. * Views can be embedded-you can call a view from within a view.
  30. * Views can also be prepended or extended by any other view.
  31. *
  32. * Any view can extend any other view if registered with
  33. * {@link elgg_extend_view()}.
  34. *
  35. * Viewtypes are set by passing $_REQUEST['view']. The viewtype
  36. * 'default' is a standard HTML view. Types can be defined on the fly
  37. * and you can get the current viewtype with {@link elgg_get_viewtype()}.
  38. *
  39. * @note Internal: Plugin views are autoregistered before their init functions
  40. * are called, so the init order doesn't affect views.
  41. *
  42. * @note Internal: The file that determines the output of the view is the last
  43. * registered by {@link elgg_set_view_location()}.
  44. *
  45. * @package Elgg.Core
  46. * @subpackage Views
  47. */
  48. /**
  49. * The viewtype override.
  50. *
  51. * @global string $CURRENT_SYSTEM_VIEWTYPE
  52. * @see elgg_set_viewtype()
  53. */
  54. global $CURRENT_SYSTEM_VIEWTYPE;
  55. $CURRENT_SYSTEM_VIEWTYPE = "";
  56. /**
  57. * Manually set the viewtype.
  58. *
  59. * View types are detected automatically. This function allows
  60. * you to force subsequent views to use a different viewtype.
  61. *
  62. * @tip Call elgg_set_viewtype() with no parameter to reset.
  63. *
  64. * @param string $viewtype The view type, e.g. 'rss', or 'default'.
  65. *
  66. * @return bool
  67. */
  68. function elgg_set_viewtype($viewtype = "") {
  69. global $CURRENT_SYSTEM_VIEWTYPE;
  70. $CURRENT_SYSTEM_VIEWTYPE = $viewtype;
  71. return true;
  72. }
  73. /**
  74. * Return the current view type.
  75. *
  76. * Viewtypes are automatically detected and can be set with $_REQUEST['view']
  77. * or {@link elgg_set_viewtype()}.
  78. *
  79. * @note Internal: Viewtype is determined in this order:
  80. * - $CURRENT_SYSTEM_VIEWTYPE Any overrides by {@link elgg_set_viewtype()}
  81. * - $CONFIG->view The default view as saved in the DB.
  82. *
  83. * @return string The view.
  84. * @see elgg_set_viewtype()
  85. */
  86. function elgg_get_viewtype() {
  87. global $CURRENT_SYSTEM_VIEWTYPE, $CONFIG;
  88. if ($CURRENT_SYSTEM_VIEWTYPE != "") {
  89. return $CURRENT_SYSTEM_VIEWTYPE;
  90. }
  91. $viewtype = get_input('view', '', false);
  92. if (_elgg_is_valid_viewtype($viewtype)) {
  93. return $viewtype;
  94. }
  95. if (isset($CONFIG->view) && _elgg_is_valid_viewtype($CONFIG->view)) {
  96. return $CONFIG->view;
  97. }
  98. return 'default';
  99. }
  100. /**
  101. * Register a viewtype.
  102. *
  103. * @param string $viewtype The view type to register
  104. * @return bool
  105. */
  106. function elgg_register_viewtype($viewtype) {
  107. global $CONFIG;
  108. if (!isset($CONFIG->view_types) || !is_array($CONFIG->view_types)) {
  109. $CONFIG->view_types = array();
  110. }
  111. if (!in_array($viewtype, $CONFIG->view_types)) {
  112. $CONFIG->view_types[] = $viewtype;
  113. }
  114. return true;
  115. }
  116. /**
  117. * Checks if $viewtype is registered.
  118. *
  119. * @param string $viewtype The viewtype name
  120. *
  121. * @return bool
  122. * @since 1.9.0
  123. */
  124. function elgg_is_registered_viewtype($viewtype) {
  125. global $CONFIG;
  126. if (!isset($CONFIG->view_types) || !is_array($CONFIG->view_types)) {
  127. return false;
  128. }
  129. return in_array($viewtype, $CONFIG->view_types);
  130. }
  131. /**
  132. * Checks if $viewtype is a string suitable for use as a viewtype name
  133. *
  134. * @param string $viewtype Potential viewtype name. Alphanumeric chars plus _ allowed.
  135. *
  136. * @return bool
  137. * @access private
  138. * @since 1.9
  139. */
  140. function _elgg_is_valid_viewtype($viewtype) {
  141. if (!is_string($viewtype) || $viewtype === '') {
  142. return false;
  143. }
  144. if (preg_match('/\W/', $viewtype)) {
  145. return false;
  146. }
  147. return true;
  148. }
  149. /**
  150. * Register a viewtype to fall back to a default view if a view isn't
  151. * found for that viewtype.
  152. *
  153. * @tip This is useful for alternate html viewtypes (such as for mobile devices).
  154. *
  155. * @param string $viewtype The viewtype to register
  156. *
  157. * @return void
  158. * @since 1.7.2
  159. */
  160. function elgg_register_viewtype_fallback($viewtype) {
  161. _elgg_services()->views->registerViewtypeFallback($viewtype);
  162. }
  163. /**
  164. * Checks if a viewtype falls back to default.
  165. *
  166. * @param string $viewtype Viewtype
  167. *
  168. * @return boolean
  169. * @since 1.7.2
  170. */
  171. function elgg_does_viewtype_fallback($viewtype) {
  172. return _elgg_services()->views->doesViewtypeFallback($viewtype);
  173. }
  174. /**
  175. * Register a view to be available for ajax calls
  176. *
  177. * @warning Only views that begin with 'js/' and 'css/' have their content
  178. * type set to 'text/javascript' and 'text/css'. Other views are served as
  179. * 'text/html'.
  180. *
  181. * @param string $view The view name
  182. * @return void
  183. * @since 1.8.3
  184. */
  185. function elgg_register_ajax_view($view) {
  186. elgg_register_external_view($view, false);
  187. }
  188. /**
  189. * Unregister a view for ajax calls
  190. *
  191. * @param string $view The view name
  192. * @return void
  193. * @since 1.8.3
  194. */
  195. function elgg_unregister_ajax_view($view) {
  196. elgg_unregister_external_view($view);
  197. }
  198. /**
  199. * Registers a view as being available externally (i.e. via URL).
  200. *
  201. * @param string $view The name of the view.
  202. * @param boolean $cacheable Whether this view can be cached.
  203. * @return void
  204. * @since 1.9.0
  205. */
  206. function elgg_register_external_view($view, $cacheable = false) {
  207. global $CONFIG;
  208. if (!isset($CONFIG->allowed_ajax_views)) {
  209. $CONFIG->allowed_ajax_views = array();
  210. }
  211. $CONFIG->allowed_ajax_views[$view] = true;
  212. if ($cacheable) {
  213. _elgg_services()->views->registerCacheableView($view);
  214. }
  215. }
  216. /**
  217. * Check whether a view is registered as cacheable.
  218. *
  219. * @param string $view The name of the view.
  220. * @return boolean
  221. * @access private
  222. * @since 1.9.0
  223. */
  224. function _elgg_is_view_cacheable($view) {
  225. return _elgg_services()->views->isCacheableView($view);
  226. }
  227. /**
  228. * Unregister a view for ajax calls
  229. *
  230. * @param string $view The view name
  231. * @return void
  232. * @since 1.9.0
  233. */
  234. function elgg_unregister_external_view($view) {
  235. global $CONFIG;
  236. if (isset($CONFIG->allowed_ajax_views[$view])) {
  237. unset($CONFIG->allowed_ajax_views[$view]);
  238. }
  239. }
  240. /**
  241. * Set an alternative base location for a view.
  242. *
  243. * Views are expected to be in plugin_name/views/. This function can
  244. * be used to change that location.
  245. *
  246. * @note Internal: Core view locations are stored in $CONFIG->viewpath.
  247. *
  248. * @tip This is useful to optionally register views in a plugin.
  249. *
  250. * @param string $view The name of the view
  251. * @param string $location The base location path
  252. * @param string $viewtype The view type
  253. *
  254. * @return void
  255. */
  256. function elgg_set_view_location($view, $location, $viewtype = '') {
  257. _elgg_services()->views->setViewLocation($view, $location, $viewtype);
  258. }
  259. /**
  260. * Returns whether the specified view exists
  261. *
  262. * @note If $recurse is true, also checks if a view exists only as an extension.
  263. *
  264. * @param string $view The view name
  265. * @param string $viewtype If set, forces the viewtype
  266. * @param bool $recurse If false, do not check extensions
  267. *
  268. * @return bool
  269. */
  270. function elgg_view_exists($view, $viewtype = '', $recurse = true) {
  271. return _elgg_services()->views->viewExists($view, $viewtype, $recurse);
  272. }
  273. /**
  274. * Return a parsed view.
  275. *
  276. * Views are rendered by a template handler and returned as strings.
  277. *
  278. * Views are called with a special $vars variable set,
  279. * which includes any variables passed as the second parameter.
  280. * For backward compatbility, the following variables are also set but we
  281. * recommend that you do not use them:
  282. * - $vars['config'] The $CONFIG global. (Use {@link elgg_get_config()} instead).
  283. * - $vars['url'] The site URL. (use {@link elgg_get_site_url()} instead).
  284. * - $vars['user'] The logged in user. (use {@link elgg_get_logged_in_user_entity()} instead).
  285. *
  286. * Custom template handlers can be set with {@link set_template_handler()}.
  287. *
  288. * The output of views can be intercepted by registering for the
  289. * view, $view_name plugin hook.
  290. *
  291. * @warning Any variables in $_SESSION will override passed vars
  292. * upon name collision. See https://github.com/Elgg/Elgg/issues/2124
  293. *
  294. * @param string $view The name and location of the view to use
  295. * @param array $vars Variables to pass to the view.
  296. * @param boolean $bypass If set to true, elgg_view will bypass any specified
  297. * alternative template handler; by default, it will
  298. * hand off to this if requested (see set_template_handler)
  299. * @param boolean $ignored This argument is ignored and will be removed eventually
  300. * @param string $viewtype If set, forces the viewtype for the elgg_view call to be
  301. * this value (default: standard detection)
  302. *
  303. * @return string The parsed view
  304. */
  305. function elgg_view($view, $vars = array(), $bypass = false, $ignored = false, $viewtype = '') {
  306. return _elgg_services()->views->renderView($view, $vars, $bypass, $viewtype);
  307. }
  308. /**
  309. * Display a view with a deprecation notice. No missing view NOTICE is logged
  310. *
  311. * @see elgg_view()
  312. *
  313. * @param string $view The name and location of the view to use
  314. * @param array $vars Variables to pass to the view
  315. * @param string $suggestion Suggestion with the deprecation message
  316. * @param string $version Human-readable *release* version: 1.7, 1.8, ...
  317. *
  318. * @return string The parsed view
  319. * @access private
  320. */
  321. function elgg_view_deprecated($view, array $vars, $suggestion, $version) {
  322. return _elgg_services()->views->renderDeprecatedView($view, $vars, $suggestion, $version);
  323. }
  324. /**
  325. * Extends a view with another view.
  326. *
  327. * The output of any view can be prepended or appended to any other view.
  328. *
  329. * The default action is to append a view. If the priority is less than 500,
  330. * the output of the extended view will be appended to the original view.
  331. *
  332. * Views can be extended multiple times, and extensions are not checked for
  333. * uniqueness. Use {@link elgg_unextend_view()} to help manage duplicates.
  334. *
  335. * Priority can be specified and affects the order in which extensions
  336. * are appended or prepended.
  337. *
  338. * @note Internal: View extensions are stored in
  339. * $CONFIG->views->extensions[$view][$priority] = $view_extension
  340. *
  341. * @param string $view The view to extend.
  342. * @param string $view_extension This view is added to $view
  343. * @param int $priority The priority, from 0 to 1000,
  344. * to add at (lowest numbers displayed first)
  345. * @param string $viewtype I'm not sure why this is here @todo
  346. *
  347. * @return void
  348. * @since 1.7.0
  349. */
  350. function elgg_extend_view($view, $view_extension, $priority = 501, $viewtype = '') {
  351. _elgg_services()->views->extendView($view, $view_extension, $priority, $viewtype);
  352. }
  353. /**
  354. * Unextends a view.
  355. *
  356. * @param string $view The view that was extended.
  357. * @param string $view_extension This view that was added to $view
  358. *
  359. * @return bool
  360. * @since 1.7.2
  361. */
  362. function elgg_unextend_view($view, $view_extension) {
  363. return _elgg_services()->views->unextendView($view, $view_extension);
  364. }
  365. /**
  366. * Assembles and outputs a full page.
  367. *
  368. * A "page" in Elgg is determined by the current view type and
  369. * can be HTML for a browser, RSS for a feed reader, or
  370. * Javascript, PHP and a number of other formats.
  371. *
  372. * For HTML pages, use the 'head', 'page' plugin hook for setting meta elements
  373. * and links.
  374. *
  375. * @param string $title Title
  376. * @param string $body Body
  377. * @param string $page_shell Optional page shell to use. See page/shells view directory
  378. * @param array $vars Optional vars array to pass to the page
  379. * shell. Automatically adds title, body, head, and sysmessages
  380. *
  381. * @return string The contents of the page
  382. * @since 1.8
  383. */
  384. function elgg_view_page($title, $body, $page_shell = 'default', $vars = array()) {
  385. $params = array();
  386. $params['identifier'] = _elgg_services()->request->getFirstUrlSegment();
  387. $params['segments'] = _elgg_services()->request->getUrlSegments();
  388. array_shift($params['segments']);
  389. $page_shell = elgg_trigger_plugin_hook('shell', 'page', $params, $page_shell);
  390. $system_messages = _elgg_services()->systemMessages;
  391. $messages = null;
  392. if ($system_messages->count()) {
  393. $messages = $system_messages->dumpRegister();
  394. if (isset($messages['error'])) {
  395. // always make sure error is the first type
  396. $errors = array(
  397. 'error' => $messages['error']
  398. );
  399. unset($messages['error']);
  400. $messages = array_merge($errors, $messages);
  401. }
  402. }
  403. $vars['title'] = $title;
  404. $vars['body'] = $body;
  405. $vars['sysmessages'] = $messages;
  406. // head has keys 'title', 'metas', 'links'
  407. $head_params = _elgg_views_prepare_head($title);
  408. $vars['head'] = elgg_trigger_plugin_hook('head', 'page', $vars, $head_params);
  409. $vars = elgg_trigger_plugin_hook('output:before', 'page', null, $vars);
  410. // check for deprecated view
  411. if ($page_shell == 'default' && elgg_view_exists('pageshells/pageshell')) {
  412. elgg_deprecated_notice("pageshells/pageshell is deprecated by page/$page_shell", 1.8);
  413. $output = elgg_view('pageshells/pageshell', $vars);
  414. } else {
  415. $output = elgg_view("page/$page_shell", $vars);
  416. }
  417. $vars['page_shell'] = $page_shell;
  418. // Allow plugins to modify the output
  419. return elgg_trigger_plugin_hook('output', 'page', $vars, $output);
  420. }
  421. /**
  422. * Prepare the variables for the html head
  423. *
  424. * @param string $title Page title for <head>
  425. * @return array
  426. * @access private
  427. */
  428. function _elgg_views_prepare_head($title) {
  429. $params = array(
  430. 'links' => array(),
  431. 'metas' => array(),
  432. );
  433. if (empty($title)) {
  434. $params['title'] = elgg_get_config('sitename');
  435. } else {
  436. $params['title'] = $title . ' : ' . elgg_get_config('sitename');
  437. }
  438. $params['metas']['content-type'] = array(
  439. 'http-equiv' => 'Content-Type',
  440. 'content' => 'text/html; charset=utf-8',
  441. );
  442. $params['metas']['description'] = array(
  443. 'name' => 'description',
  444. 'content' => elgg_get_config('sitedescription')
  445. );
  446. // https://developer.chrome.com/multidevice/android/installtohomescreen
  447. $params['metas']['viewport'] = array(
  448. 'name' => 'viewport',
  449. 'content' => 'width=device-width',
  450. );
  451. $params['metas']['mobile-web-app-capable'] = array(
  452. 'name' => 'mobile-web-app-capable',
  453. 'content' => 'yes',
  454. );
  455. $params['metas']['apple-mobile-web-app-capable'] = array(
  456. 'name' => 'apple-mobile-web-app-capable',
  457. 'content' => 'yes',
  458. );
  459. $params['links']['apple-touch-icon'] = array(
  460. 'rel' => 'apple-touch-icon',
  461. 'href' => elgg_normalize_url('_graphics/favicon-128.png'),
  462. );
  463. // favicons
  464. $params['links']['icon-ico'] = array(
  465. 'rel' => 'icon',
  466. 'href' => elgg_normalize_url('_graphics/favicon.ico'),
  467. );
  468. $params['links']['icon-vector'] = array(
  469. 'rel' => 'icon',
  470. 'sizes' => '16x16 32x32 48x48 64x64 128x128',
  471. 'type' => 'image/svg+xml',
  472. 'href' => elgg_normalize_url('_graphics/favicon.svg'),
  473. );
  474. $params['links']['icon-16'] = array(
  475. 'rel' => 'icon',
  476. 'sizes' => '16x16',
  477. 'type' => 'image/png',
  478. 'href' => elgg_normalize_url('_graphics/favicon-16.png'),
  479. );
  480. $params['links']['icon-32'] = array(
  481. 'rel' => 'icon',
  482. 'sizes' => '32x32',
  483. 'type' => 'image/png',
  484. 'href' => elgg_normalize_url('_graphics/favicon-32.png'),
  485. );
  486. $params['links']['icon-64'] = array(
  487. 'rel' => 'icon',
  488. 'sizes' => '64x64',
  489. 'type' => 'image/png',
  490. 'href' => elgg_normalize_url('_graphics/favicon-64.png'),
  491. );
  492. $params['links']['icon-128'] = array(
  493. 'rel' => 'icon',
  494. 'sizes' => '128x128',
  495. 'type' => 'image/png',
  496. 'href' => elgg_normalize_url('_graphics/favicon-128.png'),
  497. );
  498. // RSS feed link
  499. global $autofeed;
  500. if (isset($autofeed) && $autofeed == true) {
  501. $url = current_page_url();
  502. if (substr_count($url,'?')) {
  503. $url .= "&view=rss";
  504. } else {
  505. $url .= "?view=rss";
  506. }
  507. $params['links']['rss'] = array(
  508. 'rel' => 'alternative',
  509. 'type' => 'application/rss+xml',
  510. 'title' => 'RSS',
  511. 'href' => elgg_format_url($url),
  512. );
  513. }
  514. return $params;
  515. }
  516. /**
  517. * Displays a layout with optional parameters.
  518. *
  519. * Layouts provide consistent organization of pages and other blocks of content.
  520. * There are a few default layouts in core:
  521. * - admin A special layout for the admin area.
  522. * - one_column A single content column.
  523. * - one_sidebar A content column with sidebar.
  524. * - two_sidebar A content column with two sidebars.
  525. * - widgets A widget canvas.
  526. *
  527. * The layout views take the form page/layouts/$layout_name
  528. * See the individual layouts for what options are supported. The three most
  529. * common layouts have these parameters:
  530. * one_column
  531. * content => string
  532. * one_sidebar
  533. * content => string
  534. * sidebar => string (optional)
  535. * content
  536. * content => string
  537. * sidebar => string (optional)
  538. * buttons => string (override the default add button)
  539. * title => string (override the default title)
  540. * filter_context => string (selected content filter)
  541. * See the content layout view for more parameters
  542. *
  543. * @param string $layout_name The name of the view in page/layouts/.
  544. * @param array $vars Associative array of parameters for the layout view
  545. *
  546. * @return string The layout
  547. */
  548. function elgg_view_layout($layout_name, $vars = array()) {
  549. $params = array();
  550. $params['identifier'] = _elgg_services()->request->getFirstUrlSegment();
  551. $params['segments'] = _elgg_services()->request->getUrlSegments();
  552. array_shift($params['segments']);
  553. $layout_name = elgg_trigger_plugin_hook('layout', 'page', $params, $layout_name);
  554. if (is_string($vars) || $vars === null) {
  555. elgg_deprecated_notice("The use of unlimited optional string arguments in elgg_view_layout() was deprecated in favor of an options array", 1.8);
  556. $arg = 1;
  557. $param_array = array();
  558. while ($arg < func_num_args()) {
  559. $param_array['area' . $arg] = func_get_arg($arg);
  560. $arg++;
  561. }
  562. } else {
  563. $param_array = $vars;
  564. }
  565. $param_array['layout'] = $layout_name;
  566. $params = elgg_trigger_plugin_hook('output:before', 'layout', null, $param_array);
  567. // check deprecated location
  568. if (elgg_view_exists("canvas/layouts/$layout_name")) {
  569. elgg_deprecated_notice("canvas/layouts/$layout_name is deprecated by page/layouts/$layout_name", 1.8);
  570. $output = elgg_view("canvas/layouts/$layout_name", $params);
  571. } elseif (elgg_view_exists("page/layouts/$layout_name")) {
  572. $output = elgg_view("page/layouts/$layout_name", $params);
  573. } else {
  574. $output = elgg_view("page/layouts/default", $params);
  575. }
  576. return elgg_trigger_plugin_hook('output:after', 'layout', $params, $output);
  577. }
  578. /**
  579. * Render a menu
  580. *
  581. * @see elgg_register_menu_item() for documentation on adding menu items and
  582. * navigation.php for information on the different menus available.
  583. *
  584. * This function triggers a 'register', 'menu:<menu name>' plugin hook that enables
  585. * plugins to add menu items just before a menu is rendered. This is used by
  586. * dynamic menus (menus that change based on some input such as the user hover
  587. * menu). Using elgg_register_menu_item() in response to the hook can cause
  588. * incorrect links to show up. See the blog plugin's blog_owner_block_menu()
  589. * for an example of using this plugin hook.
  590. *
  591. * An additional hook is the 'prepare', 'menu:<menu name>' which enables plugins
  592. * to modify the structure of the menu (sort it, remove items, set variables on
  593. * the menu items).
  594. *
  595. * elgg_view_menu() uses views in navigation/menu
  596. *
  597. * @param string $menu_name The name of the menu
  598. * @param array $vars An associative array of display options for the menu.
  599. * Options include:
  600. * sort_by => string or php callback
  601. * string options: 'name', 'priority', 'title' (default),
  602. * 'register' (registration order) or a
  603. * php callback (a compare function for usort)
  604. * handler: string the page handler to build action URLs
  605. * entity: \ElggEntity to use to build action URLs
  606. * class: string the class for the entire menu.
  607. * show_section_headers: bool show headers before menu sections.
  608. *
  609. * @return string
  610. * @since 1.8.0
  611. */
  612. function elgg_view_menu($menu_name, array $vars = array()) {
  613. global $CONFIG;
  614. $vars['name'] = $menu_name;
  615. $vars = elgg_trigger_plugin_hook('parameters', "menu:$menu_name", $vars, $vars);
  616. $sort_by = elgg_extract('sort_by', $vars, 'text');
  617. if (isset($CONFIG->menus[$menu_name])) {
  618. $menu = $CONFIG->menus[$menu_name];
  619. } else {
  620. $menu = array();
  621. }
  622. // Give plugins a chance to add menu items just before creation.
  623. // This supports dynamic menus (example: user_hover).
  624. $menu = elgg_trigger_plugin_hook('register', "menu:$menu_name", $vars, $menu);
  625. $builder = new \ElggMenuBuilder($menu);
  626. $vars['menu'] = $builder->getMenu($sort_by);
  627. $vars['selected_item'] = $builder->getSelected();
  628. // Let plugins modify the menu
  629. $vars['menu'] = elgg_trigger_plugin_hook('prepare', "menu:$menu_name", $vars, $vars['menu']);
  630. if (elgg_view_exists("navigation/menu/$menu_name")) {
  631. return elgg_view("navigation/menu/$menu_name", $vars);
  632. } else {
  633. return elgg_view("navigation/menu/default", $vars);
  634. }
  635. }
  636. /**
  637. * Render a menu item (usually as a link)
  638. *
  639. * @param \ElggMenuItem $item The menu item
  640. * @param array $vars Options to pass to output/url if a link
  641. * @return string
  642. * @since 1.9.0
  643. */
  644. function elgg_view_menu_item(\ElggMenuItem $item, array $vars = array()) {
  645. if (!isset($vars['class'])) {
  646. $vars['class'] = 'elgg-menu-content';
  647. }
  648. $vars = array_merge($item->getValues(), $vars);
  649. if ($item->getLinkClass()) {
  650. $vars['class'] .= ' ' . $item->getLinkClass();
  651. }
  652. if ($item->getHref() === false || $item->getHref() === null) {
  653. $text = $item->getText();
  654. // if contains elements, don't wrap
  655. if (preg_match('~<[a-z]~', $text)) {
  656. return $text;
  657. } else {
  658. return elgg_format_element('span', array('class' => 'elgg-non-link'), $text);
  659. }
  660. }
  661. if (!isset($vars['rel']) && !isset($vars['is_trusted'])) {
  662. $vars['is_trusted'] = true;
  663. }
  664. if ($item->getConfirmText()) {
  665. $vars['confirm'] = $item->getConfirmText();
  666. }
  667. return elgg_view('output/url', $vars);
  668. }
  669. /**
  670. * Returns a string of a rendered entity.
  671. *
  672. * Entity views are either determined by setting the view property on the entity
  673. * or by having a view named after the entity $type/$subtype. Entities that have
  674. * neither a view property nor a defined $type/$subtype view will fall back to
  675. * using the $type/default view.
  676. *
  677. * The entity view is called with the following in $vars:
  678. * - \ElggEntity 'entity' The entity being viewed
  679. *
  680. * @tip This function can automatically appends annotations to entities if in full
  681. * view and a handler is registered for the entity:annotate. See https://github.com/Elgg/Elgg/issues/964 and
  682. * {@link elgg_view_entity_annotations()}.
  683. *
  684. * @param \ElggEntity $entity The entity to display
  685. * @param array $vars Array of variables to pass to the entity view.
  686. * In Elgg 1.7 and earlier it was the boolean $full_view
  687. * 'full_view' Whether to show a full or condensed view. (Default: true)
  688. * 'item_view' Alternative view used to render this entity
  689. * @param boolean $bypass If true, will not pass to a custom template handler.
  690. * {@link set_template_handler()}
  691. * @param boolean $debug Complain if views are missing
  692. *
  693. * @return string HTML to display or false
  694. * @todo The annotation hook might be better as a generic plugin hook to append content.
  695. */
  696. function elgg_view_entity(\ElggEntity $entity, $vars = array(), $bypass = false, $debug = false) {
  697. // No point continuing if entity is null
  698. if (!$entity || !($entity instanceof \ElggEntity)) {
  699. return false;
  700. }
  701. global $autofeed;
  702. $autofeed = true;
  703. $defaults = array(
  704. 'full_view' => true,
  705. );
  706. if (is_array($vars)) {
  707. $vars = array_merge($defaults, $vars);
  708. } else {
  709. elgg_deprecated_notice("Update your use of elgg_view_entity()", 1.8);
  710. $vars = array(
  711. 'full_view' => $vars,
  712. );
  713. }
  714. $vars['entity'] = $entity;
  715. $entity_type = $entity->getType();
  716. $entity_subtype = $entity->getSubtype();
  717. if (empty($entity_subtype)) {
  718. $entity_subtype = 'default';
  719. }
  720. $entity_views = array(
  721. elgg_extract('item_view', $vars, ''),
  722. $entity->view,
  723. "$entity_type/$entity_subtype",
  724. "$entity_type/default",
  725. );
  726. $contents = '';
  727. foreach ($entity_views as $view) {
  728. if (elgg_view_exists($view)) {
  729. $contents = elgg_view($view, $vars, $bypass, $debug);
  730. break;
  731. }
  732. }
  733. // Marcus Povey 20090616 : Speculative and low impact approach for fixing #964
  734. if ($vars['full_view']) {
  735. $annotations = elgg_view_entity_annotations($entity, $vars['full_view']);
  736. if ($annotations) {
  737. $contents .= $annotations;
  738. }
  739. }
  740. return $contents;
  741. }
  742. /**
  743. * View the icon of an entity
  744. *
  745. * Entity views are determined by having a view named after the entity $type/$subtype.
  746. * Entities that do not have a defined icon/$type/$subtype view will fall back to using
  747. * the icon/$type/default view.
  748. *
  749. * @param \ElggEntity $entity The entity to display
  750. * @param string $size The size: tiny, small, medium, large
  751. * @param array $vars An array of variables to pass to the view. Some possible
  752. * variables are img_class and link_class. See the
  753. * specific icon view for more parameters.
  754. *
  755. * @return string HTML to display or false
  756. */
  757. function elgg_view_entity_icon(\ElggEntity $entity, $size = 'medium', $vars = array()) {
  758. // No point continuing if entity is null
  759. if (!$entity || !($entity instanceof \ElggEntity)) {
  760. return false;
  761. }
  762. $vars['entity'] = $entity;
  763. $vars['size'] = $size;
  764. $entity_type = $entity->getType();
  765. $subtype = $entity->getSubtype();
  766. if (empty($subtype)) {
  767. $subtype = 'default';
  768. }
  769. $contents = '';
  770. if (elgg_view_exists("icon/$entity_type/$subtype")) {
  771. $contents = elgg_view("icon/$entity_type/$subtype", $vars);
  772. }
  773. if (empty($contents)) {
  774. $contents = elgg_view("icon/$entity_type/default", $vars);
  775. }
  776. if (empty($contents)) {
  777. $contents = elgg_view("icon/default", $vars);
  778. }
  779. return $contents;
  780. }
  781. /**
  782. * Returns a string of a rendered annotation.
  783. *
  784. * Annotation views are expected to be in annotation/$annotation_name.
  785. * If a view is not found for $annotation_name, the default annotation/default
  786. * will be used.
  787. *
  788. * @warning annotation/default is not currently defined in core.
  789. *
  790. * The annotation view is called with the following in $vars:
  791. * - \ElggEntity 'annotation' The annotation being viewed.
  792. *
  793. * @param \ElggAnnotation $annotation The annotation to display
  794. * @param array $vars Variable array for view.
  795. * 'item_view' Alternative view used to render an annotation
  796. * @param bool $bypass If true, will not pass to a custom
  797. * template handler. {@link set_template_handler()}
  798. * @param bool $debug Complain if views are missing
  799. *
  800. * @return string/false Rendered annotation
  801. */
  802. function elgg_view_annotation(\ElggAnnotation $annotation, array $vars = array(), $bypass = false, $debug = false) {
  803. global $autofeed;
  804. $autofeed = true;
  805. $defaults = array(
  806. 'full_view' => true,
  807. );
  808. $vars = array_merge($defaults, $vars);
  809. $vars['annotation'] = $annotation;
  810. // @todo setting the view on an annotation is not advertised anywhere
  811. // do we want to keep this?
  812. $view = $annotation->view;
  813. if (is_string($view)) {
  814. return elgg_view($view, $vars, $bypass, $debug);
  815. }
  816. $name = $annotation->name;
  817. if (empty($name)) {
  818. return false;
  819. }
  820. $annotation_views = array(
  821. elgg_extract('item_view', $vars, ''),
  822. "annotation/$name",
  823. "annotation/default",
  824. );
  825. $contents = '';
  826. foreach ($annotation_views as $view) {
  827. if (elgg_view_exists($view)) {
  828. $contents = elgg_view($view, $vars, $bypass, $debug);
  829. break;
  830. }
  831. }
  832. return $contents;
  833. }
  834. /**
  835. * Returns a rendered list of entities with pagination. This function should be
  836. * called by wrapper functions.
  837. *
  838. * @see elgg_list_entities()
  839. * @see list_user_friends_objects()
  840. * @see elgg_list_entities_from_metadata()
  841. * @see elgg_list_entities_from_relationships()
  842. * @see elgg_list_entities_from_annotations()
  843. *
  844. * @param array $entities Array of entities
  845. * @param array $vars Display variables
  846. * 'count' The total number of entities across all pages
  847. * 'offset' The current indexing offset
  848. * 'limit' The number of entities to display per page (default from settings)
  849. * 'full_view' Display the full view of the entities?
  850. * 'list_class' CSS class applied to the list
  851. * 'item_class' CSS class applied to the list items
  852. * 'item_view' Alternative view to render list items
  853. * 'pagination' Display pagination?
  854. * 'list_type' List type: 'list' (default), 'gallery'
  855. * 'list_type_toggle' Display the list type toggle?
  856. * 'no_results' Message to display if no results (string|Closure)
  857. *
  858. * @return string The rendered list of entities
  859. * @access private
  860. */
  861. function elgg_view_entity_list($entities, $vars = array(), $offset = 0, $limit = null, $full_view = true,
  862. $list_type_toggle = true, $pagination = true) {
  863. if (!is_int($offset)) {
  864. $offset = (int)get_input('offset', 0);
  865. }
  866. // list type can be passed as request parameter
  867. $list_type = get_input('list_type', 'list');
  868. if (get_input('listtype')) {
  869. elgg_deprecated_notice("'listtype' has been deprecated by 'list_type' for lists", 1.8);
  870. $list_type = get_input('listtype');
  871. }
  872. if (is_array($vars)) {
  873. // new function
  874. $defaults = array(
  875. 'items' => $entities,
  876. 'list_class' => 'elgg-list-entity',
  877. 'full_view' => true,
  878. 'pagination' => true,
  879. 'list_type' => $list_type,
  880. 'list_type_toggle' => false,
  881. 'offset' => $offset,
  882. 'limit' => null,
  883. );
  884. $vars = array_merge($defaults, $vars);
  885. } else {
  886. // old function parameters
  887. elgg_deprecated_notice("Please update your use of elgg_view_entity_list()", 1.8);
  888. if ($limit === null) {
  889. $limit = elgg_get_config('default_limit');
  890. }
  891. $vars = array(
  892. 'items' => $entities,
  893. 'count' => (int) $vars, // the old count parameter
  894. 'offset' => $offset,
  895. 'limit' => (int) $limit,
  896. 'full_view' => $full_view,
  897. 'pagination' => $pagination,
  898. 'list_type' => $list_type,
  899. 'list_type_toggle' => $list_type_toggle,
  900. 'list_class' => 'elgg-list-entity',
  901. );
  902. }
  903. if (!$vars["limit"] && !$vars["offset"]) {
  904. // no need for pagination if listing is unlimited
  905. $vars["pagination"] = false;
  906. }
  907. if ($vars['list_type'] != 'list') {
  908. return elgg_view('page/components/gallery', $vars);
  909. } else {
  910. return elgg_view('page/components/list', $vars);
  911. }
  912. }
  913. /**
  914. * Returns a rendered list of annotations, plus pagination. This function
  915. * should be called by wrapper functions.
  916. *
  917. * @param array $annotations Array of annotations
  918. * @param array $vars Display variables
  919. * 'count' The total number of annotations across all pages
  920. * 'offset' The current indexing offset
  921. * 'limit' The number of annotations to display per page
  922. * 'full_view' Display the full view of the annotation?
  923. * 'list_class' CSS Class applied to the list
  924. * 'item_view' Alternative view to render list items
  925. * 'offset_key' The url parameter key used for offset
  926. * 'no_results' Message to display if no results (string|Closure)
  927. *
  928. * @return string The list of annotations
  929. * @access private
  930. */
  931. function elgg_view_annotation_list($annotations, array $vars = array()) {
  932. $defaults = array(
  933. 'items' => $annotations,
  934. 'offset' => null,
  935. 'limit' => null,
  936. 'list_class' => 'elgg-list-annotation elgg-annotation-list', // @todo remove elgg-annotation-list in Elgg 1.9
  937. 'full_view' => true,
  938. 'offset_key' => 'annoff',
  939. );
  940. $vars = array_merge($defaults, $vars);
  941. if (!$vars["limit"] && !$vars["offset"]) {
  942. // no need for pagination if listing is unlimited
  943. $vars["pagination"] = false;
  944. }
  945. return elgg_view('page/components/list', $vars);
  946. }
  947. /**
  948. * Display a plugin-specified rendered list of annotations for an entity.
  949. *
  950. * This displays the output of functions registered to the entity:annotation,
  951. * $entity_type plugin hook.
  952. *
  953. * This is called automatically by the framework from {@link elgg_view_entity()}
  954. *
  955. * @param \ElggEntity $entity Entity
  956. * @param bool $full_view Display full view?
  957. *
  958. * @return mixed string or false on failure
  959. * @todo Change the hook name.
  960. */
  961. function elgg_view_entity_annotations(\ElggEntity $entity, $full_view = true) {
  962. if (!($entity instanceof \ElggEntity)) {
  963. return false;
  964. }
  965. $entity_type = $entity->getType();
  966. $annotations = elgg_trigger_plugin_hook('entity:annotate', $entity_type,
  967. array(
  968. 'entity' => $entity,
  969. 'full_view' => $full_view,
  970. )
  971. );
  972. return $annotations;
  973. }
  974. /**
  975. * Renders a title.
  976. *
  977. * This is a shortcut for {@elgg_view page/elements/title}.
  978. *
  979. * @param string $title The page title
  980. * @param array $vars View variables (was submenu be displayed? (deprecated))
  981. *
  982. * @return string The HTML (etc)
  983. */
  984. function elgg_view_title($title, $vars = array()) {
  985. if (!is_array($vars)) {
  986. elgg_deprecated_notice('setting $submenu in elgg_view_title() is deprecated', 1.8);
  987. $vars = array('submenu' => $vars);
  988. }
  989. $vars['title'] = $title;
  990. return elgg_view('page/elements/title', $vars);
  991. }
  992. /**
  993. * Displays a UNIX timestamp in a friendly way
  994. *
  995. * @see elgg_get_friendly_time()
  996. *
  997. * @param int $time A UNIX epoch timestamp
  998. *
  999. * @return string The friendly time HTML
  1000. * @since 1.7.2
  1001. */
  1002. function elgg_view_friendly_time($time) {
  1003. return elgg_view('output/friendlytime', array('time' => $time));
  1004. }
  1005. /**
  1006. * Returns rendered comments and a comment form for an entity.
  1007. *
  1008. * @tip Plugins can override the output by registering a handler
  1009. * for the comments, $entity_type hook. The handler is responsible
  1010. * for formatting the comments and the add comment form.
  1011. *
  1012. * @param \ElggEntity $entity The entity to view comments of
  1013. * @param bool $add_comment Include a form to add comments?
  1014. * @param array $vars Variables to pass to comment view
  1015. *
  1016. * @return string|false Rendered comments or false on failure
  1017. */
  1018. function elgg_view_comments($entity, $add_comment = true, array $vars = array()) {
  1019. if (!($entity instanceof \ElggEntity)) {
  1020. return false;
  1021. }
  1022. $vars['entity'] = $entity;
  1023. $vars['show_add_form'] = $add_comment;
  1024. $vars['class'] = elgg_extract('class', $vars, "{$entity->getSubtype()}-comments");
  1025. $output = elgg_trigger_plugin_hook('comments', $entity->getType(), $vars, false);
  1026. if ($output) {
  1027. return $output;
  1028. } else {
  1029. return elgg_view('page/elements/comments', $vars);
  1030. }
  1031. }
  1032. /**
  1033. * Wrapper function for the image block display pattern.
  1034. *
  1035. * Fixed width media on the side (image, icon, flash, etc.).
  1036. * Descriptive content filling the rest of the column.
  1037. *
  1038. * This is a shortcut for {@elgg_view page/components/image_block}.
  1039. *
  1040. * @param string $image The icon and other information
  1041. * @param string $body Description content
  1042. * @param array $vars Additional parameters for the view
  1043. *
  1044. * @return string
  1045. * @since 1.8.0
  1046. */
  1047. function elgg_view_image_block($image, $body, $vars = array()) {
  1048. $vars['image'] = $image;
  1049. $vars['body'] = $body;
  1050. return elgg_view('page/components/image_block', $vars);
  1051. }
  1052. /**
  1053. * Wrapper function for the module display pattern.
  1054. *
  1055. * Box with header, body, footer
  1056. *
  1057. * This is a shortcut for {@elgg_view page/components/module}.
  1058. *
  1059. * @param string $type The type of module (main, info, popup, aside, etc.)
  1060. * @param string $title A title to put in the header
  1061. * @param string $body Content of the module
  1062. * @param array $vars Additional parameters for the module
  1063. *
  1064. * @return string
  1065. * @since 1.8.0
  1066. */
  1067. function elgg_view_module($type, $title, $body, array $vars = array()) {
  1068. $vars['type'] = $type;
  1069. $vars['title'] = $title;
  1070. $vars['body'] = $body;
  1071. return elgg_view('page/components/module', $vars);
  1072. }
  1073. /**
  1074. * Renders a human-readable representation of a river item
  1075. *
  1076. * @param \ElggRiverItem $item A river item object
  1077. * @param array $vars An array of variables for the view
  1078. * 'item_view' Alternative view to render the item
  1079. * @return string returns empty string if could not be rendered
  1080. */
  1081. function elgg_view_river_item($item, array $vars = array()) {
  1082. if (!($item instanceof \ElggRiverItem)) {
  1083. return '';
  1084. }
  1085. // checking default viewtype since some viewtypes do not have unique views per item (rss)
  1086. $view = $item->getView();
  1087. if (!$view || !elgg_view_exists($view, 'default')) {
  1088. return '';
  1089. }
  1090. $subject = $item->getSubjectEntity();
  1091. $object = $item->getObjectEntity();
  1092. if (!$subject || !$object) {
  1093. // subject is disabled or subject/object deleted
  1094. return '';
  1095. }
  1096. // @todo this needs to be cleaned up
  1097. // Don't hide objects in closed groups that a user can see.
  1098. // see https://github.com/elgg/elgg/issues/4789
  1099. // else {
  1100. // // hide based on object's container
  1101. // $visibility = \Elgg\GroupItemVisibility::factory($object->container_guid);
  1102. // if ($visibility->shouldHideItems) {
  1103. // return '';
  1104. // }
  1105. // }
  1106. $vars['item'] = $item;
  1107. $river_views = array(
  1108. elgg_extract('item_view', $vars, ''),
  1109. "river/item",
  1110. );
  1111. $contents = '';
  1112. foreach ($river_views as $view) {
  1113. if (elgg_view_exists($view)) {
  1114. $contents = elgg_view($view, $vars);
  1115. break;
  1116. }
  1117. }
  1118. return $contents;
  1119. }
  1120. /**
  1121. * Convenience function for generating a form from a view in a standard location.
  1122. *
  1123. * This function assumes that the body of the form is located at "forms/$action" and
  1124. * sets the action by default to "action/$action". Automatically wraps the forms/$action
  1125. * view with a <form> tag and inserts the anti-csrf security tokens.
  1126. *
  1127. * @tip This automatically appends elgg-form-action-name to the form's class. It replaces any
  1128. * slashes with dashes (blog/save becomes elgg-form-blog-save)
  1129. *
  1130. * @example
  1131. * <code>echo elgg_view_form('login');</code>
  1132. *
  1133. * This would assume a "login" form body to be at "forms/login" and would set the action
  1134. * of the form to "http://yoursite.com/action/login".
  1135. *
  1136. * If elgg_view('forms/login') is:
  1137. * <input type="text" name="username" />
  1138. * <input type="password" name="password" />
  1139. *
  1140. * Then elgg_view_form('login') generates:
  1141. * <form action="http://yoursite.com/action/login" method="post">
  1142. * ...security tokens...
  1143. * <input type="text" name="username" />
  1144. * <input type="password" name="password" />
  1145. * </form>
  1146. *
  1147. * @param string $action The name of the action. An action name does not include
  1148. * the leading "action/". For example, "login" is an action name.
  1149. * @param array $form_vars $vars environment passed to the "input/form" view
  1150. * @param array $body_vars $vars environment passed to the "forms/$action" view
  1151. *
  1152. * @return string The complete form
  1153. */
  1154. function elgg_view_form($action, $form_vars = array(), $body_vars = array()) {
  1155. global $CONFIG;
  1156. $defaults = array(
  1157. 'action' => $CONFIG->wwwroot . "action/$action",
  1158. 'body' => elgg_view("forms/$action", $body_vars)
  1159. );
  1160. $form_class = 'elgg-form-' . preg_replace('/[^a-z0-9]/i', '-', $action);
  1161. // append elgg-form class to any class options set
  1162. if (isset($form_vars['class'])) {
  1163. $form_vars['class'] = $form_vars['class'] . " $form_class";
  1164. } else {
  1165. $form_vars['class'] = $form_class;
  1166. }
  1167. $form_vars = array_merge($defaults, $form_vars);
  1168. $form_vars['action_name'] = $action;
  1169. return elgg_view('input/form', $form_vars);
  1170. }
  1171. /**
  1172. * Create a tagcloud for viewing
  1173. *
  1174. * @see elgg_get_tags
  1175. *
  1176. * @param array $options Any elgg_get_tags() options except:
  1177. *
  1178. * type => must be single entity type
  1179. *
  1180. * subtype => must be single entity subtype
  1181. *
  1182. * @return string
  1183. * @since 1.7.1
  1184. */
  1185. function elgg_view_tagcloud(array $options = array()) {
  1186. $type = $subtype = '';
  1187. if (isset($options['type'])) {
  1188. $type = $options['type'];
  1189. }
  1190. if (isset($options['subtype'])) {
  1191. $subtype = $options['subtype'];
  1192. }
  1193. $tag_data = elgg_get_tags($options);
  1194. return elgg_view("output/tagcloud", array(
  1195. 'value' => $tag_data,
  1196. 'type' => $type,
  1197. 'subtype' => $subtype,
  1198. ));
  1199. }
  1200. /**
  1201. * View an item in a list
  1202. *
  1203. * @param \ElggEntity|\ElggAnnotation $item
  1204. * @param array $vars Additional parameters for the rendering
  1205. * 'item_view' Alternative view used to render list items
  1206. * @return string
  1207. * @since 1.8.0
  1208. * @access private
  1209. */
  1210. function elgg_view_list_item($item, array $vars = array()) {
  1211. if ($item instanceof \ElggEntity) {
  1212. return elgg_view_entity($item, $vars);
  1213. } else if ($item instanceof \ElggAnnotation) {
  1214. return elgg_view_annotation($item, $vars);
  1215. } else if ($item instanceof \ElggRiverItem) {
  1216. return elgg_view_river_item($item, $vars);
  1217. }
  1218. return '';
  1219. }
  1220. /**
  1221. * View one of the elgg sprite icons
  1222. *
  1223. * Shorthand for <span class="elgg-icon elgg-icon-$name"></span>
  1224. *
  1225. * @param string $name The specific icon to display
  1226. * @param mixed $vars The additional classname as a string ('float', 'float-alt' or a custom class)
  1227. * or an array of variables (array('class' => 'float')) to pass to the icon view.
  1228. *
  1229. * @return string The html for displaying an icon
  1230. * @throws InvalidArgumentException
  1231. */
  1232. function elgg_view_icon($name, $vars = array()) {
  1233. if (empty($vars)) {
  1234. $vars = array();
  1235. }
  1236. if ($vars === true) {
  1237. elgg_deprecated_notice("Using a boolean to float the icon is deprecated. Use the class float.", 1.9);
  1238. $vars = array('class' => 'float');
  1239. }
  1240. if (is_string($vars)) {
  1241. $vars = array('class' => $vars);
  1242. }
  1243. if (!is_array($vars)) {
  1244. throw new \InvalidArgumentException('$vars needs to be a string or an array');
  1245. }
  1246. if (!array_key_exists('class', $vars)) {
  1247. $vars['class'] = array();
  1248. }
  1249. if (!is_array($vars['class'])) {
  1250. $vars['class'] = array($vars['class']);
  1251. }
  1252. $vars['class'][] = "elgg-icon-$name";
  1253. return elgg_view("output/icon", $vars);
  1254. }
  1255. /**
  1256. * Displays a user's access collections, using the core/friends/collections view
  1257. *
  1258. * @param int $owner_guid The GUID of the owning user
  1259. *
  1260. * @return string A formatted rendition of the collections
  1261. * @todo Move to the friends/collection.php page.
  1262. * @access private
  1263. */
  1264. function elgg_view_access_collections($owner_guid) {
  1265. if ($collections = get_user_access_collections($owner_guid)) {
  1266. $user = get_user($owner_guid);
  1267. if ($user) {
  1268. $entities = $user->getFriends(array('limit' => 0));
  1269. } else {
  1270. $entities = array();
  1271. }
  1272. foreach ($collections as $key => $collection) {
  1273. $collections[$key]->members = get_members_of_access_collection($collection->id, true);
  1274. $collections[$key]->entities = $entities;
  1275. }
  1276. }
  1277. return elgg_view('core/friends/collections', array('collections' => $collections));
  1278. }
  1279. /**
  1280. * Auto-registers views from a location.
  1281. *
  1282. * @note Views in plugin/views/ are automatically registered for active plugins.
  1283. * Plugin authors would only need to call this if optionally including
  1284. * an entire views structure.
  1285. *
  1286. * @param string $view_base Optional The base of the view name without the view type.
  1287. * @param string $folder Required The folder to begin looking in
  1288. * @param string $base_location_path The base views directory to use with elgg_set_view_location()
  1289. * @param string $viewtype The type of view we're looking at (default, rss, etc)
  1290. *
  1291. * @return bool returns false if folder can't be read
  1292. * @since 1.7.0
  1293. * @see elgg_set_view_location()
  1294. * @access private
  1295. */
  1296. function autoregister_views($view_base, $folder, $base_location_path, $viewtype) {
  1297. return _elgg_services()->views->autoregisterViews($view_base, $folder, $base_location_path, $viewtype);
  1298. }
  1299. /**
  1300. * Minifies simplecache CSS and JS views by handling the "simplecache:generate" hook
  1301. *
  1302. * @param string $hook The name of the hook
  1303. * @param string $type View type (css, js, or unknown)
  1304. * @param string $content Content of the view
  1305. * @param array $params Array of parameters
  1306. *
  1307. * @return string|null View content minified (if css/js type)
  1308. * @access private
  1309. */
  1310. function _elgg_views_minify($hook, $type, $content, $params) {
  1311. if (preg_match('~[\.-]min\.~', $params['view'])) {
  1312. // bypass minification
  1313. return;
  1314. }
  1315. if ($type == 'js') {
  1316. if (elgg_get_config('simplecache_minify_js')) {
  1317. return JSMin::minify($content);
  1318. }
  1319. } elseif ($type == 'css') {
  1320. if (elgg_get_config('simplecache_minify_css')) {
  1321. $cssmin = new CSSmin();
  1322. return $cssmin->run($content);
  1323. }
  1324. }
  1325. }
  1326. /**
  1327. * Inserts module names into anonymous modules by handling the "simplecache:generate" hook.
  1328. *
  1329. * @param string $hook The name of the hook
  1330. * @param string $type View type (css, js, or unknown)
  1331. * @param string $content Content of the view
  1332. * @param array $params Array of parameters
  1333. *
  1334. * @return string|null View content minified (if css/js type)
  1335. * @access private
  1336. */
  1337. function _elgg_views_amd($hook, $type, $content, $params) {
  1338. $filter = new \Elgg\Amd\ViewFilter();
  1339. return $filter->filter($params['view'], $content);
  1340. }
  1341. /**
  1342. * Add the rss link to the extras when if needed
  1343. *
  1344. * @return void
  1345. * @access private
  1346. */
  1347. function elgg_views_add_rss_link() {
  1348. global $autofeed;
  1349. if (isset($autofeed) && $autofeed == true) {
  1350. $url = current_page_url();
  1351. if (substr_count($url, '?')) {
  1352. $url .= "&view=rss";
  1353. } else {
  1354. $url .= "?view=rss";
  1355. }
  1356. $url = elgg_format_url($url);
  1357. elgg_register_menu_item('extras', array(
  1358. 'name' => 'rss',
  1359. 'text' => elgg_view_icon('rss'),
  1360. 'href' => $url,
  1361. 'title' => elgg_echo('feed:rss'),
  1362. ));
  1363. }
  1364. }
  1365. /**
  1366. * Sends X-Frame-Options header on page requests
  1367. *
  1368. * @access private
  1369. */
  1370. function _elgg_views_send_header_x_frame_options() {
  1371. header('X-Frame-Options: SAMEORIGIN');
  1372. }
  1373. /**
  1374. * Checks for usage of core views that have been removed
  1375. *
  1376. * @access private
  1377. */
  1378. function _elgg_views_deprecate_removed_views() {
  1379. $removed_views = array(
  1380. "1.10" => array(
  1381. 'core/settings/tools',
  1382. ),
  1383. );
  1384. $views_svc = _elgg_services()->views;
  1385. foreach ($removed_views as $version => $names) {
  1386. foreach ($names as $name) {
  1387. if ($views_svc->viewExists($name)) {
  1388. elgg_deprecated_notice("The view $name is no longer used and should not be provided or extended.", $version);
  1389. }
  1390. }
  1391. }
  1392. }
  1393. /**
  1394. * Registers deprecated views to avoid making some pages from older plugins
  1395. * completely empty.
  1396. *
  1397. * @access private
  1398. */
  1399. function elgg_views_handle_deprecated_views() {
  1400. $location = _elgg_services()->views->getViewLocation('page_elements/contentwrapper');
  1401. if ($location === "/var/www/views/") {
  1402. elgg_extend_view('page_elements/contentwrapper', 'page/elements/wrapper');
  1403. }
  1404. }
  1405. /**
  1406. * Initialize viewtypes on system boot event
  1407. * This ensures simplecache is cleared during upgrades. See #2252
  1408. *
  1409. * @return void
  1410. * @access private
  1411. * @elgg_event_handler boot system
  1412. */
  1413. function elgg_views_boot() {
  1414. global $CONFIG;
  1415. elgg_register_simplecache_view('css/ie');
  1416. elgg_register_simplecache_view('js/text.js');
  1417. elgg_register_js('elgg.require_config', elgg_get_simplecache_url('js', 'elgg/require_config'), 'head');
  1418. elgg_register_js('require', '/vendors/requirejs/require-2.1.10.min.js', 'head');
  1419. elgg_register_js('jquery', '/vendors/jquery/jquery-1.11.0.min.js', 'head');
  1420. elgg_register_js('jquery-migrate', '/vendors/jquery/jquery-migrate-1.2.1.min.js', 'head');
  1421. elgg_register_js('jquery-ui', '/vendors/jquery/jquery-ui-1.10.4.min.js', 'head');
  1422. // this is the only lib that isn't required to be loaded sync in head
  1423. elgg_define_js('jquery.form', array(
  1424. 'src' => '/vendors/jquery/jquery.form.min.js',
  1425. 'deps' => array('jquery'),
  1426. 'exports' => 'jQuery.fn.ajaxForm',
  1427. ));
  1428. elgg_define_js('jquery.ui', array(
  1429. 'src' => '/vendors/jquery/jquery-ui-1.10.4.min.js',
  1430. 'deps' => array('jquery'),
  1431. ));
  1432. $elgg_js_url = elgg_get_simplecache_url('js', 'elgg');
  1433. elgg_register_js('elgg', $elgg_js_url, 'head');
  1434. elgg_load_js('elgg.require_config');
  1435. elgg_load_js('require');
  1436. elgg_load_js('jquery');
  1437. elgg_load_js('jquery-migrate');
  1438. elgg_load_js('jquery-ui');
  1439. elgg_load_js('elgg');
  1440. $lightbox_js_url = elgg_get_simplecache_url('js', 'lightbox');
  1441. elgg_register_js('lightbox', $lightbox_js_url);
  1442. elgg_register_css('lightbox', 'vendors/jquery/colorbox/theme/colorbox.css');
  1443. $elgg_css_url = elgg_get_simplecache_url('css', 'elgg');
  1444. elgg_register_css('elgg', $elgg_css_url);
  1445. elgg_load_css('elgg');
  1446. elgg_register_ajax_view('js/languages');
  1447. elgg_register_plugin_hook_handler('simplecache:generate', 'js', '_elgg_views_amd');
  1448. elgg_register_plugin_hook_handler('simplecache:generate', 'css', '_elgg_views_minify');
  1449. elgg_register_plugin_hook_handler('simplecache:generate', 'js', '_elgg_views_minify');
  1450. elgg_register_plugin_hook_handler('output:before', 'layout', 'elgg_views_add_rss_link');
  1451. elgg_register_plugin_hook_handler('output:before', 'page', '_elgg_views_send_header_x_frame_options');
  1452. // discover the core viewtypes
  1453. // @todo the cache is loaded in load_plugins() but we need to know viewtypes earlier
  1454. $view_path = $CONFIG->viewpath;
  1455. $viewtype_dirs = scandir($view_path);
  1456. foreach ($viewtype_dirs as $viewtype) {
  1457. if (_elgg_is_valid_viewtype($viewtype) && is_dir($view_path . $viewtype)) {
  1458. elgg_register_viewtype($viewtype);
  1459. }
  1460. }
  1461. // set default icon sizes - can be overridden in settings.php or with plugin
  1462. if (!isset($CONFIG->icon_sizes)) {
  1463. $icon_sizes = array(
  1464. 'topbar' => array('w' => 16, 'h' => 16, 'square' => true, 'upscale' => true),
  1465. 'tiny' => array('w' => 25, 'h' => 25, 'square' => true, 'upscale' => true),
  1466. 'small' => array('w' => 40, 'h' => 40, 'square' => true, 'upscale' => true),
  1467. 'medium' => array('w' => 100, 'h' => 100, 'square' => true, 'upscale' => true),
  1468. 'large' => array('w' => 200, 'h' => 200, 'square' => false, 'upscale' => false),
  1469. 'master' => array('w' => 550, 'h' => 550, 'square' => false, 'upscale' => false),
  1470. );
  1471. elgg_set_config('icon_sizes', $icon_sizes);
  1472. }
  1473. }
  1474. return function(\Elgg\EventsService $events, \Elgg\HooksRegistrationService $hooks) {
  1475. $events->registerHandler('boot', 'system', 'elgg_views_boot');
  1476. $events->registerHandler('init', 'system', 'elgg_views_handle_deprecated_views');
  1477. $events->registerHandler('ready', 'system', '_elgg_views_deprecate_removed_views');
  1478. };