widget.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Base class for UI widgets
  18. *
  19. * @category Widget
  20. * @package GNUsocial
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Sarven Capadisli <csarven@status.net>
  23. * @copyright 2009-2019 Free Software Foundation, Inc http://www.fsf.org
  24. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  25. */
  26. defined('GNUSOCIAL') || die();
  27. /**
  28. * Base class for UI widgets
  29. *
  30. * A widget is a cluster of HTML elements that provide some functionality
  31. * that's used on different parts of the site. Examples would be profile
  32. * lists, notice lists, navigation menus (tabsets) and common forms.
  33. *
  34. * @category Widget
  35. * @package GNUsocial
  36. * @author Evan Prodromou <evan@status.net>
  37. * @author Sarven Capadisli <csarven@status.net>
  38. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  39. *
  40. * @see HTMLOutputter
  41. */
  42. class Widget
  43. {
  44. protected $avatarSize = AVATAR_STREAM_SIZE;
  45. /**
  46. * Action (HTMLOutputter) to use for output
  47. */
  48. public $out = null;
  49. /**
  50. * Prepare the widget for use
  51. *
  52. * @param Action $out output helper, defaults to null
  53. * @param array $widgetOpts
  54. */
  55. public function __construct(?Action $out = null, array $widgetOpts = [])
  56. {
  57. $this->out = $out;
  58. if (!array_key_exists('scoped', $widgetOpts)) {
  59. $this->widgetOpts['scoped'] = Profile::current();
  60. }
  61. $this->scoped = $this->widgetOpts['scoped'];
  62. }
  63. /**
  64. * Show the widget
  65. *
  66. * Emit the HTML for the widget, using the configured outputter.
  67. *
  68. * @return void
  69. */
  70. public function show()
  71. {
  72. }
  73. /**
  74. * Get HTMLOutputter
  75. *
  76. * Return the HTMLOutputter for the widget.
  77. *
  78. * @return HTMLOutputter the output helper
  79. */
  80. public function getOut()
  81. {
  82. return $this->out;
  83. }
  84. /**
  85. * Delegate output methods to the outputter attribute.
  86. *
  87. * @param string $name Name of the method
  88. * @param array $arguments Arguments called
  89. *
  90. * @return mixed Return value of the method.
  91. */
  92. public function __call($name, $arguments)
  93. {
  94. return call_user_func_array(array($this->out, $name), $arguments);
  95. }
  96. /**
  97. * Default avatar size for this widget.
  98. */
  99. public function avatarSize()
  100. {
  101. return $this->avatarSize;
  102. }
  103. protected function showAvatar(Profile $profile, $size=null)
  104. {
  105. $avatar_url = $profile->avatarUrl($size ?: $this->avatarSize());
  106. $this->out->element('img', array('src' => $avatar_url,
  107. 'class' => 'avatar u-photo',
  108. 'width' => $this->avatarSize(),
  109. 'height' => $this->avatarSize(),
  110. 'alt' => $profile->getBestName()));
  111. }
  112. }