You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.2 KiB

  1. <?php
  2. /**
  3. * @package Widgets
  4. *
  5. * @file Avatar.php
  6. * This file is part of Movim.
  7. *
  8. * @brief A widget which display all the infos of a contact, vcard 4 version
  9. *
  10. * @author Timothée Jaussoin <edhelas_at_gmail_dot_com>
  11. * Copyright (C)2013 MOVIM project
  12. *
  13. * See COPYING for licensing information.
  14. */
  15. use Moxl\Xec\Action\Avatar\Get;
  16. use Moxl\Xec\Action\Avatar\Set;
  17. use forxer\Gravatar\Gravatar;
  18. class Avatar extends WidgetBase
  19. {
  20. function load()
  21. {
  22. $this->addcss('avatar.css');
  23. $this->addjs('avatar.js');
  24. $this->registerEvent('avatar_get_handle', 'onMyAvatar');
  25. $this->registerEvent('avatar_set_handle', 'onMyAvatar');
  26. $this->registerEvent('avatar_set_errorfeaturenotimplemented', 'onMyAvatarError');
  27. $this->registerEvent('avatar_set_errorbadrequest', 'onMyAvatarError');
  28. $this->registerEvent('avatar_set_errornotallowed', 'onMyAvatarError');
  29. }
  30. function onMyAvatar($packet)
  31. {
  32. $me = $packet->content;
  33. $html = $this->prepareForm($me);
  34. RPC::call('movim_fill', 'avatar_form', $html);
  35. Notification::append(null, $this->__('avatar.updated'));
  36. }
  37. function onMyAvatarError()
  38. {
  39. $cd = new \modl\ContactDAO();
  40. $me = $cd->get();
  41. $html = $this->prepareForm($me);
  42. RPC::call('movim_fill', 'avatar_form', $html);
  43. Notification::append(null, $this->__('avatar.not_updated'));
  44. }
  45. function prepareForm($me)
  46. {
  47. $avatarform = $this->tpl();
  48. $p = new Picture;
  49. $p->get($this->user->getLogin());
  50. $avatarform->assign('photobin', $p->toBase());
  51. $avatarform->assign('me', $me);
  52. if(isset($me->email)) {
  53. $result = requestURL(Gravatar::profile($me->email, 'json'), 3);
  54. $obj = json_decode($result);
  55. if($obj != 'User not found') {
  56. $avatarform->assign('gravatar_bin', base64_encode(requestURL('http://www.gravatar.com/avatar/'.$obj->entry[0]->hash.'?s=250')));
  57. $avatarform->assign('gravatar', $obj);
  58. }
  59. }
  60. $avatarform->assign(
  61. 'submit',
  62. $this->call('ajaxSubmit', "movim_form_to_json('avatarform')")
  63. );
  64. return $avatarform->draw('_avatar_form', true);
  65. }
  66. function ajaxGetAvatar()
  67. {
  68. $r = new Get;
  69. $r->setTo($this->user->getLogin())
  70. ->setMe()
  71. ->request();
  72. }
  73. function ajaxSubmit($avatar)
  74. {
  75. $p = new \Picture;
  76. $p->fromBase((string)$avatar->photobin->value);
  77. $p->set($this->user->getLogin());
  78. $r = new Set;
  79. $r->setData($avatar->photobin->value)->request();
  80. }
  81. function display()
  82. {
  83. $cd = new \modl\ContactDAO();
  84. $me = $cd->get();
  85. $p = new Picture;
  86. if(!$p->get($this->user->getLogin())) {
  87. $this->view->assign(
  88. 'getavatar',
  89. $this->call('ajaxGetAvatar')
  90. );
  91. $this->view->assign('form', $this->prepareForm(new \modl\Contact()));
  92. } else {
  93. $this->view->assign('getavatar', '');
  94. $this->view->assign('form', $this->prepareForm($me));
  95. }
  96. }
  97. }