UserAvatar.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. use Chamilo\UserBundle\Entity\User;
  4. /**
  5. * Class UserAvatar
  6. * FormValidator element to add an user avatar wrapping a hidden input with its user ID
  7. * Is necessary set an instance of Chamilo\UserBundle\Entity\User as value. The exported value is the user ID.
  8. */
  9. class UserAvatar extends HTML_QuickForm_input
  10. {
  11. /** @var User */
  12. private $user = null;
  13. private $imageSize = 'small';
  14. private $subTitle = '';
  15. /**
  16. * UserAvatar constructor.
  17. *
  18. * @param string $name
  19. * @param string $label
  20. * @param array $attributes
  21. */
  22. public function __construct($name, $label, $attributes = [])
  23. {
  24. if (isset($attributes['image_size'])) {
  25. $this->imageSize = $attributes['image_size'];
  26. unset($attributes['image_size']);
  27. }
  28. if (isset($attributes['sub_title'])) {
  29. $this->subTitle = $attributes['sub_title'];
  30. unset($attributes['sub_title']);
  31. }
  32. parent::__construct($name, $label, $attributes);
  33. $this->setType('hidden');
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function setValue($value)
  39. {
  40. $this->user = !is_a($value, 'Chamilo\UserBundle\Entity\User')
  41. ? UserManager::getManager()->find($value)
  42. : $value;
  43. parent::setValue($this->user->getId());
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function toHtml()
  49. {
  50. if (!$this->user) {
  51. return '';
  52. }
  53. $userInfo = api_get_user_info($this->user->getId());
  54. $userPicture = isset($userInfo["avatar_{$this->imageSize}"])
  55. ? $userInfo["avatar_{$this->imageSize}"]
  56. : $userInfo["avatar"];
  57. if (!$this->subTitle) {
  58. $this->subTitle = $this->user->getUsername();
  59. }
  60. $html = parent::toHtml();
  61. $html .= '
  62. <div class="media">
  63. <div class="media-left">
  64. <img src="'.$userPicture.'" alt="'.UserManager::formatUserFullName($this->user).'">
  65. </div>
  66. <div class="media-body">
  67. <h4 class="media-heading">'.UserManager::formatUserFullName($this->user).'</h4>
  68. '.$this->subTitle.'
  69. </div>
  70. </div>
  71. ';
  72. return $html;
  73. }
  74. }