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.

VacationHandler.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. # $Id: VacationHandler.php 1845 2016-05-22 16:17:46Z christian_boltz $
  3. class VacationHandler extends PFAHandler {
  4. protected $db_table = 'vacation';
  5. protected $id_field = 'email';
  6. protected $domain_field = 'domain';
  7. function init($id) {
  8. die('VacationHandler is not yet ready to be used with *Handler methods'); # obvious TODO: remove when it's ready ;-)
  9. }
  10. protected function initStruct() {
  11. $this->struct=array(
  12. # field name allow display in... type $PALANG label $PALANG description default / options / ...
  13. # editing? form list
  14. 'email' => pacol( $this->new, 1, 1, 'text', 'pLogin_username' , '' , '' ),
  15. 'domain' => pacol( 1, 0, 0, 'text', '' , '' , '' ),
  16. 'subject' => pacol( 1, 1, 0, 'text', 'pUsersVacation_subject' , '' , '' ),
  17. 'body' => pacol( 1, 1, 0, 'text', 'pUsersVacation_body' , '' , '' ),
  18. 'activefrom' => pacol( 1, 1, 1, 'text', 'pUsersVacation_activefrom' , '' , '' ),
  19. 'activeuntil' => pacol( 1, 1, 1, 'text', 'pUsersVacation_activeuntil' , '' , '' ),
  20. 'cache' => pacol( 0, 0, 0, 'text', '' , '' , '' ), # leftover from 2.2
  21. 'active' => pacol( 1, 1, 1, 'bool', 'active' , '' , 1 ),
  22. 'created' => pacol( 0, 0, 1, 'ts', 'created' , '' ),
  23. 'modified' => pacol( 0, 0, 1, 'ts', 'last_modified' , '' ),
  24. # TODO: add virtual 'notified' column and allow to display who received a vacation response?
  25. );
  26. }
  27. protected function initMsg() {
  28. $this->msg['error_already_exists'] = 'pCreate_mailbox_username_text_error1'; # TODO: better error message
  29. $this->msg['error_does_not_exist'] = 'pCreate_mailbox_username_text_error1'; # TODO: better error message
  30. $this->msg['confirm_delete'] = 'confirm_delete_vacation'; # unused?
  31. if ($this->new) {
  32. $this->msg['logname'] = 'edit_vacation';
  33. $this->msg['store_error'] = 'pVacation_result_error';
  34. $this->msg['successmessage'] = 'pVacation_result_removed'; # TODO: or pVacation_result_added - depends on 'active'... -> we probably need a new message
  35. } else {
  36. $this->msg['logname'] = 'edit_vacation';
  37. $this->msg['store_error'] = 'pVacation_result_error';
  38. $this->msg['successmessage'] = 'pVacation_result_removed'; # TODO: or pVacation_result_added - depends on 'active'... -> we probably need a new message
  39. }
  40. }
  41. public function webformConfig() {
  42. return array(
  43. # $PALANG labels
  44. 'formtitle_create' => 'pUsersVacation_welcome',
  45. 'formtitle_edit' => 'pUsersVacation_welcome',
  46. 'create_button' => 'save',
  47. # various settings
  48. 'required_role' => 'admin',
  49. 'listview' => 'list-virtual.php',
  50. 'early_init' => 1, # 0 for create-domain
  51. );
  52. }
  53. protected function validate_new_id() {
  54. # vacation can only be enabled if a mailbox with this name exists
  55. $handler = new MailboxHandler();
  56. return $handler->init($address);
  57. }
  58. public function delete() {
  59. $this->errormsg[] = '*** deletion not implemented yet ***';
  60. return false; # XXX function aborts here! XXX
  61. }
  62. protected $username = null;
  63. function __construct($username) {
  64. $this->username = $username;
  65. $this->id = $username;
  66. }
  67. /**
  68. * Removes the autoreply alias etc for this user; namely, if they're away we remove their vacation alias and
  69. * set the vacation table record to false.
  70. * @return boolean true on success.
  71. */
  72. function remove() {
  73. if (!$this->updateAlias(0)) return false;
  74. // tidy up vacation table.
  75. $vacation_data = array(
  76. 'active' => db_get_boolean(false),
  77. );
  78. $result = db_update('vacation', 'email', $this->username, $vacation_data);
  79. $result = db_delete('vacation_notification', 'on_vacation', $this->username);
  80. # TODO db_log() call (maybe except if called from set_away?)
  81. /* crap error handling; oh for exceptions... */
  82. return true;
  83. }
  84. /**
  85. * @return boolean true indicates this server supports vacation messages, and users are able to change their own.
  86. */
  87. function vacation_supported() {
  88. return Config::bool('vacation') && Config::bool('vacation_control');
  89. }
  90. /**
  91. * @return boolean true if on vacation, otherwise false
  92. * Why do we bother storing true/false in the vacation table if the alias dictates it anyway?
  93. */
  94. function check_vacation() {
  95. $handler = new AliasHandler();
  96. if (!$handler->init($this->id)) {
  97. # print_r($handler->errormsg); # TODO: error handling
  98. return false;
  99. }
  100. if (!$handler->view()) {
  101. # print_r($handler->errormsg); # TODO: error handling
  102. return false;
  103. }
  104. $result = $handler->result();
  105. if ($result['on_vacation']) return true;
  106. return false;
  107. }
  108. /**
  109. * Retrieve information on someone who is on vacation
  110. * @return struct|boolean stored information on vacation - array(subject - string, message - string, active - boolean, activeFrom - date, activeUntil - date)
  111. * will return false if no existing data
  112. */
  113. function get_details() {
  114. $table_vacation = table_by_key('vacation');
  115. $E_username = escape_string($this->username);
  116. $sql = "SELECT * FROM $table_vacation WHERE email = '$E_username'";
  117. $result = db_query($sql);
  118. if($result['rows'] != 1) {
  119. return false;
  120. }
  121. $row = db_array($result['result']);
  122. $boolean = ($row['active'] == db_get_boolean(true));
  123. # TODO: only return true and store the db result array in $this->whatever for consistency with the other classes
  124. return array(
  125. 'subject' => $row['subject'],
  126. 'body' => $row['body'],
  127. 'active' => $boolean ,
  128. 'interval_time' => $row['interval_time'],
  129. 'activeFrom' => $row['activefrom'],
  130. 'activeUntil' => $row['activeuntil'],
  131. );
  132. }
  133. /**
  134. * @param string $subject
  135. * @param string $body
  136. * @param string $interval_time
  137. * @param date $activeFrom
  138. * @param date $activeUntil
  139. */
  140. function set_away($subject, $body, $interval_time, $activeFrom, $activeUntil) {
  141. $this->remove(); // clean out any notifications that might already have been sent.
  142. $E_username = escape_string($this->username);
  143. $activeFrom = date ("Y-m-d 00:00:00", strtotime ($activeFrom)); # TODO check if result looks like a valid date
  144. $activeUntil = date ("Y-m-d 23:59:59", strtotime ($activeUntil)); # TODO check if result looks like a valid date
  145. list(/*NULL*/,$domain) = explode('@', $this->username);
  146. $vacation_data = array(
  147. 'email' => $this->username,
  148. 'domain' => $domain,
  149. 'subject' => $subject,
  150. 'body' => $body,
  151. 'interval_time' => $interval_time,
  152. 'active' => db_get_boolean(true),
  153. 'activefrom' => $activeFrom,
  154. 'activeuntil' => $activeUntil,
  155. 'cache' => '',
  156. );
  157. // is there an entry in the vacaton table for the user, or do we need to insert?
  158. $table_vacation = table_by_key('vacation');
  159. $result = db_query("SELECT * FROM $table_vacation WHERE email = '$E_username'");
  160. if($result['rows'] == 1) {
  161. $result = db_update('vacation', 'email', $this->username, $vacation_data);
  162. } else {
  163. $result = db_insert('vacation', $vacation_data);
  164. }
  165. # TODO error check
  166. # TODO wrap whole function in db_begin / db_commit (or rollback)?
  167. return $this->updateAlias(1);
  168. }
  169. /**
  170. * add/remove the vacation alias
  171. * @param int $vacationActive
  172. */
  173. protected function updateAlias($vacationActive) {
  174. $handler = new AliasHandler();
  175. if (!$handler->init($this->id)) {
  176. # print_r($handler->errormsg); # TODO: error handling
  177. return false;
  178. }
  179. $values = array (
  180. 'on_vacation' => $vacationActive,
  181. );
  182. if (!$handler->set($values)) {
  183. # print_r($handler->errormsg); # TODO: error handling
  184. return false;
  185. }
  186. # TODO: supress logging in AliasHandler if called from VacationHandler (VacationHandler should log itsself)
  187. if (!$handler->store()) {
  188. print_r($handler->errormsg); # TODO: error handling
  189. return false;
  190. }
  191. # still here? then everything worked
  192. return true;
  193. }
  194. }
  195. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */