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.

181 lines
5.1 KiB

  1. <?php
  2. /**
  3. * @package Widgets
  4. *
  5. * @file Vcard4.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. class Vcard4 extends WidgetBase
  16. {
  17. function load()
  18. {
  19. $this->registerEvent('myvcard4valid', 'onMyVcard4Received');
  20. $this->registerEvent('myvcard4invalid', 'onMyVcard4NotReceived');
  21. $this->registerEvent('myvcard', 'onMyVcard4');
  22. }
  23. function display()
  24. {
  25. $cd = new \modl\ContactDAO();
  26. $me = $cd->get($this->user->getLogin());
  27. if($me == null) {
  28. $this->view->assign(
  29. 'getvcard',
  30. $this->genCallAjax('ajaxGetVcard')
  31. );
  32. $this->view->assign('form', $this->prepareForm(new \modl\Contact()));
  33. } else {
  34. $this->view->assign('form', $this->prepareForm($me));
  35. }
  36. }
  37. function prepareForm($me) {
  38. $vcardform = $this->tpl();
  39. $vcardform->assign('me', $me);
  40. $vcardform->assign('desc', trim($me->description));
  41. $vcardform->assign('gender', getGender());
  42. $vcardform->assign('marital', getMarital());
  43. $vcardform->assign('countries',getCountries());
  44. $vcardform->assign(
  45. 'submit',
  46. $this->genCallAjax('ajaxVcardSubmit', "movim_form_to_json('vcard4')")
  47. );
  48. $vcardform->assign(
  49. 'privacy',
  50. $this->genCallAjax('ajaxChangePrivacy', "this.checked")
  51. );
  52. // The datepicker arrays
  53. $days = $months = $years = array();
  54. for($i=1; $i<= 31; $i++) {
  55. if($i < 10){
  56. $j = '0'.$i;
  57. } else {
  58. $j = (string)$i;
  59. }
  60. $days[$i] = $j;
  61. }
  62. for($i=1; $i<= 12; $i++) {
  63. if($i < 10){
  64. $j = '0'.$i;
  65. } else {
  66. $j = (string)$i;
  67. }
  68. $m = getMonths();
  69. $months[$j] = $m[$i];
  70. }
  71. for($i=date('o'); $i>= 1920; $i--) { array_push($years, $i); }
  72. $vcardform->assign('days', $days);
  73. $vcardform->assign('months', $months);
  74. $vcardform->assign('years', $years);
  75. return $vcardform->draw('_vcard4_form', true);
  76. }
  77. function onMyVcard4($c) {
  78. $html = $this->prepareForm($c);
  79. Notification::appendNotification(t('Profile Updated'), 'success');
  80. RPC::call('movim_fill', 'vcard_form', $html);
  81. RPC::commit();
  82. }
  83. function onMyVcard4Received() {
  84. RPC::call('movim_button_reset', '#vcard4validate');
  85. Notification::appendNotification(t('Profile Updated'), 'success');
  86. RPC::commit();
  87. }
  88. function onMyVcard4NotReceived() {
  89. Notification::appendNotification(t('Profile Not Updated'), 'error');
  90. RPC::commit();
  91. }
  92. function ajaxGetVcard() {
  93. $r = new moxl\Vcard4Get();
  94. $r->setTo($this->user->getLogin())
  95. ->setMe()
  96. ->request();
  97. }
  98. function ajaxVcardSubmit($vcard) {
  99. # Format it ISO 8601:
  100. if($vcard->year->value != -1
  101. && $vcard->month->value != -1
  102. && $vcard->day->value != -1)
  103. $vcard->date->value =
  104. $vcard->year->value.'-'.
  105. $vcard->month->value.'-'.
  106. $vcard->day->value;
  107. unset($vcard->year->value);
  108. unset($vcard->month->value);
  109. unset($vcard->day->value);
  110. $cd = new \modl\ContactDAO();
  111. $c = $cd->get($this->user->getLogin());
  112. if($c == null)
  113. $c = new modl\Contact();
  114. $c->jid = $this->user->getLogin();
  115. if(isset($vcard->date->value)) {
  116. $c->date = $vcard->date->value;
  117. }
  118. $c->name = $vcard->name->value;
  119. $c->fn = $vcard->fn->value;
  120. $c->url = $vcard->url->value;
  121. $c->gender = $vcard->gender->value;
  122. $c->marital = $vcard->marital->value;
  123. $c->adrlocality = $vcard->locality->value;
  124. $c->adrcountry = $vcard->country->value;
  125. $c->email = $vcard->email->value;
  126. $c->twitter = $vcard->twitter->value;
  127. $c->skype = $vcard->skype->value;
  128. $c->yahoo = $vcard->yahoo->value;
  129. $c->description = trim($vcard->desc->value);
  130. $cd = new modl\ContactDAO();
  131. $cd->set($c);
  132. $r = new moxl\Vcard4Set();
  133. $r->setData($c)->request();
  134. $r = new moxl\VcardSet();
  135. $r->setData($vcard)->request();
  136. }
  137. function ajaxChangePrivacy($value) {
  138. if($value == true) {
  139. \modl\Privacy::set($this->user->getLogin(), 1);
  140. Notification::appendNotification(t('Your profile is now public'), 'success');
  141. } else {
  142. \modl\Privacy::set($this->user->getLogin(), 0);
  143. Notification::appendNotification(t('Your profile is now restricted'), 'success');
  144. }
  145. }
  146. }