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.

232 lines
7.0 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
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. // Sys.command("sh", [
  37. // "-c",
  38. // "sendemail -f no-reply@comptoirduvrac.re -s mail1.zourit.net:587 -t pvincent@comptoirduvrac.re -bcc 'pvincent974@gmail.com, pvincent974@laposte.net' -u TEST -m coucou -xu postmaster@comptoirduvrac.re -xp QqQeAPT6EpoK"
  39. // ]);
  40. var mails = [];
  41. for (d in dest) {
  42. if (d.email != null)
  43. mails.push(d.email);
  44. if (d.email2 != null)
  45. mails.push(d.email2);
  46. }
  47. // send mail confirmation link
  48. var e = new sugoi.mail.Mail();
  49. e.setSender(App.config.get("default_email"));
  50. e.setSubject(subject);
  51. var replyTo = form.getValueOf("senderMail");
  52. var replyToName = form.getValueOf("senderName");
  53. if (replyTo != null)
  54. e.setReplyTo(replyTo, replyToName);
  55. if (mails.length > 1) {
  56. var cc = mails.join(",");
  57. App.log('add header Cc: $cc');
  58. e.setHeader("CC:", cc);
  59. // for (x in mails)
  60. // e.addRecipient(x);
  61. e.addRecipient(e.getSender().email);
  62. } else {
  63. App.log('no header');
  64. e.addRecipient(mails[0]);
  65. }
  66. // sender : default email ( explicitly tells that the server send an email on behalf of the user )
  67. // e.setHeader("Sender", App.config.get("default_email"));
  68. var text:String = form.getValueOf("text");
  69. var html = app.processTemplate("mail/message.mtt", {text: text, group: app.user.amap, list: getListName(listId)});
  70. e.setHtmlBody(html);
  71. // App.sendMail(e, app.user.getAmap(), listId, app.user);
  72. // Sys.command("sendemail", [
  73. // "-f", "no-reply@comptoirduvrac.re", "-s", "mail1.zourit.net:587", "-t", "pvincent@comptoirduvrac.re", "-bcc",
  74. // "pvincent974@gmail.com,pvincent974@laposte.net", "-u", subject, "-m", "alors tout va bien", "-xu", "postmaster@comptoirduvrac.re", "-xp",
  75. // "QqQeAPT6EpoK"
  76. // ]);
  77. sys.io.File.saveContent('/tmp/my_file.json', "CONTENT");
  78. // store message
  79. var lm = new db.Message();
  80. lm.amap = app.user.amap;
  81. // lm.recipients = Lambda.array(Lambda.map(e.getRecipients(), function(x) return x.email));
  82. lm.recipients = mails;
  83. lm.title = e.getSubject();
  84. lm.date = Date.now();
  85. lm.body = e.getHtmlBody();
  86. if (listId != null)
  87. lm.recipientListId = listId;
  88. lm.sender = app.user;
  89. lm.insert();
  90. throw Ok("/messages", t._("The message has been sent"));
  91. }
  92. view.form = form;
  93. if (app.user.isAmapManager()) {
  94. view.sentMessages = Message.manager.search($amap == app.user.amap && $recipientListId != null, {orderBy: -date, limit: 20}, false);
  95. } else {
  96. view.sentMessages = Message.manager.search($sender == app.user && $recipientListId != null && $amap == app.user.amap, {orderBy: -date, limit: 20},
  97. false);
  98. }
  99. }
  100. @tpl("messages/message.mtt")
  101. public function doMessage(msg:Message) {
  102. if (!app.user.isAmapManager() && msg.sender.id != app.user.id)
  103. throw Error("/", t._("Non authorized access"));
  104. view.list = getListName(msg.recipientListId);
  105. view.msg = msg;
  106. // make status easier to display
  107. var s = new Array<{email:String, success:String, failure:String}>();
  108. /*if (msg.status != null){
  109. for ( k in msg.status.keys()) {
  110. var r = msg.getMailerResultMessage(k);
  111. s.push({email:k,success:r.success,failure:r.failure});
  112. }
  113. }*/
  114. view.status = s;
  115. }
  116. function getLists():FormData<String> {
  117. var out = [
  118. {value: '1', label: t._("Everyone")},
  119. {value: '2', label: t._("The board: persons in charge + contracts + memberships")},
  120. ];
  121. out.push({value: '3', label: t._("TEST: me + spouse")});
  122. out.push({value: '4', label: t._("Members without contract/order")});
  123. if (app.user.amap.hasMembership())
  124. out.push({value: '5', label: t._("Memberships to be renewed")});
  125. var contracts = db.Contract.getActiveContracts(app.user.amap, true);
  126. for (c in contracts) {
  127. var label = t._("Subscribers") + " " + c.toString();
  128. out.push({value: 'c' + c.id, label: label});
  129. }
  130. return out;
  131. }
  132. /**
  133. * get list name from id
  134. * @param listId
  135. */
  136. function getListName(listId:String) {
  137. var l = getLists();
  138. for (ll in l) {
  139. if (ll.value == listId)
  140. return ll.label;
  141. }
  142. return null;
  143. }
  144. function getSelection(listId:String) {
  145. if (listId.substr(0, 1) == "c") {
  146. // contrats
  147. var contract = Std.parseInt(listId.substr(1));
  148. var pids = db.Product.manager.search($contractId == contract, false);
  149. var pids = Lambda.map(pids, function(x) return x.id);
  150. var up = db.UserContract.manager.search($productId in pids, false);
  151. var users = [];
  152. for (order in up) {
  153. if (!Lambda.has(users, order.user)) {
  154. users.push(order.user);
  155. }
  156. if (order.user2 != null && !Lambda.has(users, order.user2)) {
  157. users.push(order.user2);
  158. }
  159. }
  160. return users;
  161. } else {
  162. var out = [];
  163. switch (listId) {
  164. case "1":
  165. // tout le monde
  166. out = Lambda.array(app.user.amap.getMembers());
  167. case "2":
  168. var users = [];
  169. users.push(app.user.amap.contact);
  170. for (c in db.Contract.manager.search($amap == app.user.amap)) {
  171. if (!Lambda.has(users, c.contact)) {
  172. users.push(c.contact);
  173. }
  174. }
  175. // ajouter les autres personnes ayant les droits Admin ou Gestion Adhérents ou Gestion Contrats
  176. for (ua in Lambda.array(db.UserAmap.manager.search($rights != null && $amap == app.user.amap, false))) {
  177. if (ua.hasRight(GroupAdmin) || ua.hasRight(Membership) || ua.hasRight(ContractAdmin())) {
  178. if (!Lambda.has(users, ua.user))
  179. users.push(ua.user);
  180. }
  181. }
  182. out = users;
  183. case "3":
  184. // moi
  185. return [app.user];
  186. case "4":
  187. return Lambda.array(db.User.getUsers_NoContracts());
  188. case "5":
  189. return Lambda.array(db.User.getUsers_NoMembership());
  190. }
  191. return out;
  192. }
  193. }
  194. }