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.

192 lines
5.5 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. use Moxl\Xec\Action\Vcard4\Get;
  16. use Moxl\Xec\Action\Vcard4\Set;
  17. use Respect\Validation\Validator;
  18. class Vcard4 extends WidgetBase
  19. {
  20. function load()
  21. {
  22. $this->registerEvent('vcard4_get_handle', 'onMyVcard4');
  23. $this->registerEvent('vcard4_set_handle', 'onMyVcard4');
  24. }
  25. function display()
  26. {
  27. $cd = new \Modl\ContactDAO();
  28. $me = $cd->get();
  29. $this->view->assign('getvcard', $this->call('ajaxGetVcard'));
  30. if($me == null) {
  31. $this->view->assign('form', $this->prepareForm(new \modl\Contact()));
  32. } else {
  33. $this->view->assign('form', $this->prepareForm($me));
  34. }
  35. }
  36. function prepareForm($me) {
  37. $vcardform = $this->tpl();
  38. $vcardform->assign('me', $me);
  39. $vcardform->assign('desc', trim($me->description));
  40. $vcardform->assign('gender', getGender());
  41. $vcardform->assign('marital', getMarital());
  42. $vcardform->assign('countries',getCountries());
  43. $vcardform->assign(
  44. 'submit',
  45. $this->call('ajaxVcardSubmit', "movim_form_to_json('vcard4')")
  46. );
  47. $vcardform->assign(
  48. 'privacy',
  49. $this->call('ajaxChangePrivacy', "this.checked")
  50. );
  51. // The datepicker arrays
  52. $days = $months = $years = array();
  53. for($i=1; $i<= 31; $i++) {
  54. if($i < 10){
  55. $j = '0'.$i;
  56. } else {
  57. $j = (string)$i;
  58. }
  59. $days[$i] = $j;
  60. }
  61. for($i=1; $i<= 12; $i++) {
  62. if($i < 10){
  63. $j = '0'.$i;
  64. } else {
  65. $j = (string)$i;
  66. }
  67. $m = getMonths();
  68. $months[$j] = $m[$i];
  69. }
  70. for($i=date('o'); $i>= 1920; $i--) { array_push($years, $i); }
  71. $vcardform->assign('days', $days);
  72. $vcardform->assign('months', $months);
  73. $vcardform->assign('years', $years);
  74. return $vcardform->draw('_vcard4_form', true);
  75. }
  76. function onMyVcard4($packet) {
  77. $c = $packet->content;
  78. $html = $this->prepareForm($c);
  79. Notification::append(null, $this->__('vcard.updated'));
  80. RPC::call('movim_fill', 'vcard_form', $html);
  81. RPC::commit();
  82. }
  83. function onMyVcard4Received() {
  84. RPC::call('movim_button_reset', '#vcard4validate');
  85. Notification::append(null, $this->__('vcard.updated'));
  86. RPC::commit();
  87. }
  88. function onMyVcard4NotReceived() {
  89. Notification::append(null, $this->__('vcard.not_updated'));
  90. RPC::commit();
  91. }
  92. function ajaxGetVcard() {
  93. $r = new Get;
  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. if(Validator::stringType()->length(0, 40)->validate($vcard->name->value))
  119. $c->name = $vcard->name->value;
  120. if(Validator::stringType()->length(0, 40)->validate($vcard->fn->value))
  121. $c->fn = $vcard->fn->value;
  122. if(Validator::url()->validate($vcard->url->value))
  123. $c->url = $vcard->url->value;
  124. if(Validator::in(array_keys(getGender()))->validate($vcard->gender->value))
  125. $c->gender = $vcard->gender->value;
  126. if(Validator::in(array_keys(getMarital()))->validate($vcard->marital->value))
  127. $c->marital = $vcard->marital->value;
  128. $c->adrlocality = $vcard->locality->value;
  129. $c->adrcountry = $vcard->country->value;
  130. if(Validator::email()->validate($vcard->email->value))
  131. $c->email = $vcard->email->value;
  132. $c->twitter = $vcard->twitter->value;
  133. $c->skype = $vcard->skype->value;
  134. $c->yahoo = $vcard->yahoo->value;
  135. if(Validator::stringType()->validate($vcard->desc->value))
  136. $c->description = trim($vcard->desc->value);
  137. $cd = new \Modl\ContactDAO();
  138. $cd->set($c);
  139. $r = new Set;
  140. $r->setData($c)->request();
  141. $r = new Moxl\Xec\Action\Vcard\Set;
  142. $r->setData($vcard)->request();
  143. }
  144. function ajaxChangePrivacy($value) {
  145. if($value == true) {
  146. \modl\Privacy::set($this->user->getLogin(), 1);
  147. Notification::append(null, $this->__('vcard.public'));
  148. } else {
  149. \modl\Privacy::set($this->user->getLogin(), 0);
  150. Notification::append(null, $this->__('vcard.restricted'));
  151. }
  152. }
  153. }