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.

210 lines
6.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package controller;
  2. import db.Message;
  3. import db.UserContract;
  4. import sugoi.form.ListData;
  5. import sugoi.form.elements.*;
  6. import sugoi.form.Form;
  7. class Messages extends Controller {
  8. public function new() {
  9. super();
  10. if (!app.user.canAccessMessages())
  11. throw Redirect("/");
  12. }
  13. @tpl("messages/default.mtt")
  14. function doDefault() {
  15. var form = new Form("msg");
  16. var senderName = "";
  17. var senderMail = "";
  18. if (App.current.session.data.whichUser == 1 && app.user.email2 != null) {
  19. senderMail = app.user.email2;
  20. senderName = app.user.firstName2 + " " + app.user.lastName2;
  21. } else {
  22. senderMail = app.user.email;
  23. senderName = app.user.firstName + " " + app.user.lastName;
  24. }
  25. var lists = getLists();
  26. form.addElement(new StringInput("senderName", t._("Sender name"), senderName, true));
  27. form.addElement(new StringInput("senderMail", t._("Sender E-Mail"), senderMail, true));
  28. form.addElement(new StringSelect("list", t._("Recipients"), lists, "1", true, null, "style='width:500px;'"));
  29. form.addElement(new StringInput("subject", t._("Subject:"), "", false, null, "style='width:500px;'"));
  30. form.addElement(new TextArea("text", t._("Message:"), "", false, null, "style='width:500px;height:350px;'"));
  31. if (form.checkToken()) {
  32. var listId = form.getElement("list").value;
  33. var dest = getSelection(listId);
  34. var subject = form.getValueOf("subject");
  35. App.log('about to send email <$subject> to listId=<$listId> to ${dest.length} recipient(s)');
  36. var mails = [];
  37. for (d in dest) {
  38. if (d.email != null)
  39. mails.push(d.email);
  40. if (d.email2 != null)
  41. mails.push(d.email2);
  42. }
  43. // send mail confirmation link
  44. var e = new sugoi.mail.Mail();
  45. e.setSender(App.config.get("default_email"), t._("Cagette.net"));
  46. e.setSubject(subject);
  47. var replyTo = form.getValueOf("senderMail");
  48. var replyToName = form.getValueOf("senderName");
  49. if (replyTo != null)
  50. e.setReplyTo(replyTo, replyToName);
  51. for (x in mails)
  52. e.addRecipient(x);
  53. var text:String = form.getValueOf("text");
  54. var html = app.processTemplate("mail/message.mtt", {text: text, group: app.user.amap, list: getListName(listId)});
  55. e.setHtmlBody(html);
  56. App.sendMail(e, app.user.getAmap(), listId, app.user);
  57. // store message
  58. var lm = new db.Message();
  59. lm.amap = app.user.amap;
  60. // lm.recipients = Lambda.array(Lambda.map(e.getRecipients(), function(x) return x.email));
  61. lm.recipients = mails;
  62. lm.title = e.getSubject();
  63. lm.date = Date.now();
  64. lm.body = e.getHtmlBody();
  65. if (listId != null)
  66. lm.recipientListId = listId;
  67. lm.sender = app.user;
  68. lm.insert();
  69. throw Ok("/messages", t._("The message has been sent"));
  70. } else {
  71. App.log('Administrator #${app.user.id} <${senderName}> prepares sending an email from choices list');
  72. }
  73. view.form = form;
  74. if (app.user.isAmapManager()) {
  75. view.sentMessages = Message.manager.search($amap == app.user.amap && $recipientListId != null, {orderBy: -date, limit: 20}, false);
  76. } else {
  77. view.sentMessages = Message.manager.search($sender == app.user && $recipientListId != null && $amap == app.user.amap, {orderBy: -date, limit: 20},
  78. false);
  79. }
  80. }
  81. @tpl("messages/message.mtt")
  82. public function doMessage(msg:Message) {
  83. if (!app.user.isAmapManager() && msg.sender.id != app.user.id)
  84. throw Error("/", t._("Non authorized access"));
  85. view.list = getListName(msg.recipientListId);
  86. view.msg = msg;
  87. // make status easier to display
  88. var s = new Array<{email:String, success:String, failure:String}>();
  89. /*if (msg.status != null){
  90. for ( k in msg.status.keys()) {
  91. var r = msg.getMailerResultMessage(k);
  92. s.push({email:k,success:r.success,failure:r.failure});
  93. }
  94. }*/
  95. view.status = s;
  96. }
  97. function getLists():FormData<String> {
  98. var out = [
  99. {value: '1', label: t._("Everyone")},
  100. {value: '2', label: t._("The board: persons in charge + contracts + memberships")},
  101. ];
  102. out.push({value: '3', label: t._("TEST: me + spouse")});
  103. out.push({value: '4', label: t._("Members without contract/order")});
  104. if (app.user.amap.hasMembership())
  105. out.push({value: '5', label: t._("Memberships to be renewed")});
  106. var contracts = db.Contract.getActiveContracts(app.user.amap, true);
  107. for (c in contracts) {
  108. var label = t._("Subscribers") + " " + c.toString();
  109. out.push({value: 'c' + c.id, label: label});
  110. }
  111. return out;
  112. }
  113. /**
  114. * get list name from id
  115. * @param listId
  116. */
  117. function getListName(listId:String) {
  118. var l = getLists();
  119. for (ll in l) {
  120. if (ll.value == listId)
  121. return ll.label;
  122. }
  123. return null;
  124. }
  125. function getSelection(listId:String) {
  126. if (listId.substr(0, 1) == "c") {
  127. // contrats
  128. var contract = Std.parseInt(listId.substr(1));
  129. var pids = db.Product.manager.search($contractId == contract, false);
  130. var pids = Lambda.map(pids, function(x) return x.id);
  131. var up = db.UserContract.manager.search($productId in pids, false);
  132. var users = [];
  133. for (order in up) {
  134. if (!Lambda.has(users, order.user)) {
  135. users.push(order.user);
  136. }
  137. if (order.user2 != null && !Lambda.has(users, order.user2)) {
  138. users.push(order.user2);
  139. }
  140. }
  141. return users;
  142. } else {
  143. var out = [];
  144. switch (listId) {
  145. case "1":
  146. // tout le monde
  147. out = Lambda.array(app.user.amap.getMembers());
  148. case "2":
  149. var users = [];
  150. users.push(app.user.amap.contact);
  151. for (c in db.Contract.manager.search($amap == app.user.amap)) {
  152. if (!Lambda.has(users, c.contact)) {
  153. users.push(c.contact);
  154. }
  155. }
  156. // ajouter les autres personnes ayant les droits Admin ou Gestion Adhérents ou Gestion Contrats
  157. for (ua in Lambda.array(db.UserAmap.manager.search($rights != null && $amap == app.user.amap, false))) {
  158. if (ua.hasRight(GroupAdmin) || ua.hasRight(Membership) || ua.hasRight(ContractAdmin())) {
  159. if (!Lambda.has(users, ua.user))
  160. users.push(ua.user);
  161. }
  162. }
  163. out = users;
  164. case "3":
  165. // moi
  166. return [app.user];
  167. case "4":
  168. return Lambda.array(db.User.getUsers_NoContracts());
  169. case "5":
  170. return Lambda.array(db.User.getUsers_NoMembership());
  171. }
  172. return out;
  173. }
  174. }
  175. }