UserAvatar.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. * @param string $name
  18. * @param string $label
  19. * @param array $attributes
  20. */
  21. public function __construct($name, $label, $attributes = [])
  22. {
  23. if (isset($attributes['image_size'])) {
  24. $this->imageSize = $attributes['image_size'];
  25. unset($attributes['image_size']);
  26. }
  27. if (isset($attributes['sub_title'])) {
  28. $this->subTitle = $attributes['sub_title'];
  29. unset($attributes['sub_title']);
  30. }
  31. parent::__construct($name, $label, $attributes);
  32. $this->setType('hidden');
  33. }
  34. /**
  35. * @inheritDoc
  36. */
  37. public function setValue($value)
  38. {
  39. $this->user = !is_a($value, 'Chamilo\UserBundle\Entity\User')
  40. ? UserManager::getManager()->find($value)
  41. : $value;
  42. parent::setValue($this->user->getId());
  43. }
  44. /**
  45. * @inheritDoc
  46. */
  47. public function toHtml()
  48. {
  49. if (!$this->user) {
  50. return '';
  51. }
  52. $userInfo = api_get_user_info($this->user->getId());
  53. $userPicture = isset($userInfo["avatar_{$this->imageSize}"])
  54. ? $userInfo["avatar_{$this->imageSize}"]
  55. : $userInfo["avatar"];
  56. if (!$this->subTitle) {
  57. $this->subTitle = $this->user->getUsername();
  58. }
  59. $html = parent::toHtml();
  60. $html .= '
  61. <div class="media">
  62. <div class="media-left">
  63. <img src="'.$userPicture.'" alt="'.$this->user->getCompleteName().'">
  64. </div>
  65. <div class="media-body">
  66. <h4 class="media-heading">'.$this->user->getCompleteName().'</h4>
  67. '.$this->subTitle.'
  68. </div>
  69. </div>
  70. ';
  71. return $html;
  72. }
  73. }