widget.php 3.7 KB

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