widget.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. public $widgetOpts;
  45. public $scoped;
  46. protected $avatarSize = AVATAR_STREAM_SIZE;
  47. /**
  48. * Action (HTMLOutputter) to use for output
  49. */
  50. public $out = null;
  51. /**
  52. * Prepare the widget for use
  53. *
  54. * @param Action $out output helper, defaults to null
  55. * @param array $widgetOpts
  56. */
  57. public function __construct(?Action $out = null, array $widgetOpts = [])
  58. {
  59. $this->out = $out;
  60. if (!array_key_exists('scoped', $widgetOpts)) {
  61. $this->widgetOpts['scoped'] = Profile::current();
  62. }
  63. $this->scoped = $this->widgetOpts['scoped'];
  64. }
  65. /**
  66. * Show the widget
  67. *
  68. * Emit the HTML for the widget, using the configured outputter.
  69. *
  70. * @return void
  71. */
  72. public function show()
  73. {
  74. }
  75. /**
  76. * Get HTMLOutputter
  77. *
  78. * Return the HTMLOutputter for the widget.
  79. *
  80. * @return HTMLOutputter the output helper
  81. */
  82. public function getOut()
  83. {
  84. return $this->out;
  85. }
  86. /**
  87. * Delegate output methods to the outputter attribute.
  88. *
  89. * @param string $name Name of the method
  90. * @param array $arguments Arguments called
  91. *
  92. * @return mixed Return value of the method.
  93. */
  94. public function __call($name, $arguments)
  95. {
  96. return call_user_func_array(array($this->out, $name), $arguments);
  97. }
  98. /**
  99. * Default avatar size for this widget.
  100. */
  101. public function avatarSize()
  102. {
  103. return $this->avatarSize;
  104. }
  105. protected function showAvatar(Profile $profile, $size=null)
  106. {
  107. $avatar_url = $profile->avatarUrl($size ?: $this->avatarSize());
  108. $this->out->element('img', array('src' => $avatar_url,
  109. 'class' => 'avatar u-photo',
  110. 'width' => $this->avatarSize(),
  111. 'height' => $this->avatarSize(),
  112. 'alt' => $profile->getBestName()));
  113. }
  114. }