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