PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Intraface/shared/email/Email.php

https://github.com/intraface/intraface.dk
PHP | 455 lines | 305 code | 55 blank | 95 comment | 56 complexity | 8349eff21375edc27db40b909bb6a191 MD5 | raw file
  1. <?php
  2. /**
  3. * Queues and saves e-mails
  4. *
  5. * Emails must be connected to the module from which they are saved, so you are always
  6. * able to find an email.
  7. *
  8. * @package Intraface
  9. * @author Lars Olesen <lars@legestue.net>
  10. * @version @package-version@
  11. *
  12. */
  13. require_once 'Intraface/functions.php';
  14. require_once 'Intraface/modules/contact/Contact.php';
  15. class Email extends Intraface_Standard
  16. {
  17. public $kernel;
  18. public $error;
  19. public $id;
  20. public $contact;
  21. public $type;
  22. protected $dbquery;
  23. protected $db;
  24. /**
  25. * Constructor
  26. *
  27. * @param object $kernel Kernel object
  28. * @param integer $id E-mail id
  29. *
  30. * @return void
  31. */
  32. function __construct($kernel, $id = 0)
  33. {
  34. $this->kernel = $kernel;
  35. $this->id = (int)$id;
  36. $this->error = new Intraface_Error;
  37. $this->type = array(
  38. 1 => 'quotation',
  39. 2 => 'order',
  40. 3 => 'invoice',
  41. 4 => 'creditnote',
  42. 5 => 'reminder',
  43. 6 => 'todo',
  44. 7 => 'newslist',
  45. 8 => 'newsletter',
  46. 9 => 'contact',
  47. 10 => 'electronic_invoice',
  48. 11 => 'email_to_search',
  49. 12 => 'webshop',
  50. 13 => 'onlinepayment'
  51. );
  52. $this->status = array(
  53. 1 => 'draft',
  54. 2 => 'outbox',
  55. 3 => 'sent'
  56. );
  57. if ($this->id > 0) {
  58. $this->load();
  59. }
  60. }
  61. function getKernel()
  62. {
  63. return $this->kernel;
  64. }
  65. /**
  66. * @return boolean
  67. */
  68. function load()
  69. {
  70. if ($this->id == 0) {
  71. return false;
  72. }
  73. $db = new DB_Sql;
  74. $db->query('SET NAMES utf8'); /* To be removed when everything is in utf-8 */
  75. $sql = "SELECT id, email.date_sent, DATE_FORMAT(date_sent, '%d-%m-%Y') as date_sent_dk, subject, from_name, from_email, user_id, body, status, contact_id, contact_person_id, type_id, belong_to_id, status, bcc_to_user FROM email WHERE intranet_id = ".$this->kernel->intranet->get('id')." AND id = " . $this->id;
  76. $db->query($sql);
  77. if (!$db->nextRecord()) {
  78. return false;
  79. }
  80. $this->value['id'] = $db->f('id');
  81. $this->value['subject'] = $db->f('subject');
  82. $this->value['from_name'] = $db->f('from_name');
  83. $this->value['from_email'] = $db->f('from_email');
  84. $this->value['body'] = $db->f('body');
  85. $this->value['status'] = $db->f('status');
  86. $this->value['contact_id'] = $db->f('contact_id');
  87. $this->value['contact_person_id'] = $db->f('contact_person_id');
  88. $this->value['type_id'] = $db->f('type_id');
  89. $this->value['belong_to_id'] = $db->f('belong_to_id');
  90. $this->value['status'] = $this->status[$db->f('status')];
  91. $this->value['status_key'] = $db->f('status');
  92. $this->value['user_id'] = $db->f('user_id');
  93. $this->value['bcc_to_user'] = $db->f('bcc_to_user');
  94. $this->value['date_sent_dk'] = $db->f('date_sent_dk');
  95. $this->value['date_sent'] = $db->f('date_sent');
  96. if ($db->f('contact_id') == 0) {
  97. return false;
  98. }
  99. return true;
  100. }
  101. /**
  102. * @return Contact object
  103. */
  104. function getContact()
  105. {
  106. $this->kernel->useModule('contact');
  107. $this->contact = new Contact($this->kernel, $this->get('contact_id'));
  108. if ($this->get('contact_person_id') != 0 && $this->contact->get('type') == 'corporation') {
  109. $this->contact->loadContactPerson($this->get('contact_person_id'));
  110. }
  111. $this->value['contact_email'] = $this->contact->address->get('email');
  112. $this->value['contact_name'] = $this->contact->address->get('name');
  113. return $this->contact;
  114. }
  115. /**
  116. * @param struct $var Values to validate
  117. *
  118. * @return boolean
  119. */
  120. function validate($var)
  121. {
  122. $validator = new Intraface_Validator($this->error);
  123. if ($this->id == 0) {
  124. $validator->isNumeric($var['belong_to'], 'belong_to');
  125. $validator->isNumeric($var['type_id'], 'type_id');
  126. $validator->isNumeric($var['contact_id'], 'contact_id');
  127. }
  128. $validator->isString($var['subject'], 'there was an error in subject', '');
  129. $validator->isString($var['body'], 'there was an error in body', '');
  130. settype($var['from_email'], 'string'); //
  131. $validator->isEmail($var['from_email'], 'there was an error in from email', 'allow_empty');
  132. settype($var['from_name'], 'string'); //
  133. $validator->isString($var['from_name'], 'there was and error in from name', '', 'allow_empty');
  134. if ($this->error->isError()) {
  135. return false;
  136. }
  137. return true;
  138. }
  139. /**
  140. * @param struct $var Values to save
  141. *
  142. * @return integer id of the saved email
  143. */
  144. function save($var)
  145. {
  146. $var = safeToDb($var);
  147. if (!$this->validate($var)) {
  148. return 0;
  149. }
  150. $db = new DB_Sql;
  151. $db->query('SET NAMES utf8');
  152. if ($this->id == 0) {
  153. $sql_type = "INSERT INTO ";
  154. $sql_end = ", date_created = NOW(),
  155. belong_to_id = ".(int)$var['belong_to'] . ",
  156. type_id = ".(int)$var['type_id'] . ",
  157. contact_id=".$var['contact_id'];
  158. } else {
  159. $sql_type = "UPDATE ";
  160. $sql_end = " WHERE id = " . $this->id;
  161. }
  162. if (!empty($var['date_deadline'])) {
  163. $date_deadline = "'".$var['date_deadline']."'";
  164. } else {
  165. $date_deadline = 'NOW()';
  166. }
  167. $sql_extra = '';
  168. // gemme userid hvis vi er inde i systemet
  169. if (is_object($this->kernel->user) and $this->kernel->user->get('id') > 0) {
  170. //$db->query("UPDATE email SET user_id = ".$this->kernel->user->get('id')." WHERE id = " . $this->id);
  171. $sql_extra = ', user_id = ' . $db->quote($this->kernel->user->get('id'), 'integer');
  172. }
  173. if (!isset($var['contact_person_id'])) {
  174. $var['contact_person_id'] = 0;
  175. }
  176. if (!isset($var['bcc_to_user'])) {
  177. $var['bcc_to_user'] = 0;
  178. }
  179. // status 1 = draft
  180. $sql = $sql_type . " email SET
  181. contact_person_id = ".(int)$var['contact_person_id'].",
  182. bcc_to_user = ".(int)$var['bcc_to_user'].",
  183. date_updated = NOW(),
  184. intranet_id = " . $this->kernel->intranet->get('id') . ",
  185. subject = '".$var['subject']."',
  186. body = '".$var['body']."',
  187. date_deadline = ".$date_deadline.",
  188. status = 1 " . $sql_extra;
  189. if (isset($var['from_name'])) {
  190. $sql .= ", from_name = '".$var['from_name']."'";
  191. }
  192. if (isset($var['from_email'])) {
  193. $sql .= ", from_email = '".$var['from_email']."'";
  194. }
  195. $sql .= $sql_end;
  196. $db->query($sql);
  197. if ($this->id == 0) {
  198. $this->id = $db->insertedId();
  199. }
  200. if ($this->id > 0) {
  201. $this->load();
  202. }
  203. return $this->id;
  204. }
  205. /**
  206. * Saves error msg in the database
  207. *
  208. * @param string $error Error msg to save
  209. *
  210. * @return boolean
  211. */
  212. function saveErrorMsg($error)
  213. {
  214. $db = new DB_Sql;
  215. $db->query("UPDATE email SET error_msg = '".$error."' WHERE id = " . $this->id);
  216. return true;
  217. }
  218. /**
  219. * Sets date and status when sent
  220. *
  221. * @return boolean
  222. */
  223. function setIsSent()
  224. {
  225. if ($this->id == 0) {
  226. return false;
  227. }
  228. $db = new DB_Sql;
  229. $db->query("UPDATE email SET status = 3, date_sent = NOW() WHERE id = " . $this->id);
  230. return true;
  231. }
  232. /**
  233. * Checks if e-mail can be sent
  234. *
  235. * @return boolean
  236. */
  237. function isReadyToSend()
  238. {
  239. if ($this->id == 0) {
  240. $this->error->set('the message can not be send because it has no id');
  241. return false;
  242. }
  243. if ($this->get('from_email') == '' && (!isset($this->kernel->intranet->address) || $this->kernel->intranet->address->get('email') == '')) {
  244. $this->error->set('you need to fill in an e-mail address for the intranet, to be able to send mails');
  245. return false;
  246. }
  247. return true;
  248. }
  249. function queue()
  250. {
  251. if (!$this->isReadyToSend()) {
  252. return false;
  253. }
  254. $db = new DB_Sql;
  255. // Putter e-mailen i outboxen (status = 2)
  256. $db->query("UPDATE email SET status = 2 WHERE intranet_id = ".$this->kernel->intranet->get('id')." AND id = " . $this->id);
  257. return true;
  258. }
  259. function getTo()
  260. {
  261. $contact = $this->getContact();
  262. if ($this->get('contact_id') == 0 or !is_object($contact)) {
  263. $this->error->set('Der kunne ikke sendes e-mail til email #' . $this->get('id') . ' fordi der ikke var nogen kunde sat');
  264. return false;
  265. }
  266. $validator = new Intraface_Validator($this->error);
  267. if ($contact->get('type') == 'corporation' && $this->get('contact_person_id') != 0) {
  268. $contact->loadContactPerson($this->get('contact_person_id'));
  269. if ($validator->isEmail($contact->contactperson->get('email'))) {
  270. return array($contact->contactperson->get('email') => $contact->contactperson->get('name'));
  271. }
  272. }
  273. if ($validator->isEmail($contact->address->get('email'))) {
  274. return array($contact->address->get('email') => $contact->address->get('name'));
  275. }
  276. return false;
  277. }
  278. function getFrom()
  279. {
  280. if ($this->get('from_email')) {
  281. if ($this->get('from_name')) {
  282. return array($this->get('from_email') => $this->get('from_name'));
  283. } else {
  284. return array($this->get('from_email'));
  285. }
  286. } else { // Standardafsender
  287. return array($this->kernel->intranet->address->get('email') => $this->kernel->intranet->address->get('name'));
  288. }
  289. }
  290. function getBody()
  291. {
  292. return $this->get('body');
  293. }
  294. function getSubject()
  295. {
  296. return $this->get('subject');
  297. }
  298. /**
  299. * Der er ingen grund til at man kan �ndre en attachment
  300. * Man kan gemme og slette
  301. *
  302. * Der skal knyttes flere attachments til en e-mail
  303. *
  304. * @param integer $file_id Id of file in the file system
  305. * @param string $filename Which filename to use
  306. *
  307. * @return boolean
  308. */
  309. function attachFile($file_id, $filename)
  310. {
  311. if (!is_numeric($file_id)) {
  312. $this->error->set('Fil-id skal være et tal');
  313. }
  314. if (empty($filename)) {
  315. $this->error->set('Navnet skal være en streng');
  316. }
  317. if ($this->error->isError()) {
  318. return 0;
  319. }
  320. $db = new DB_Sql;
  321. $db->query("INSERT INTO email_attachment
  322. SET
  323. email_id = '".$this->get('id')."',
  324. intranet_id = ".$this->kernel->intranet->get('id').",
  325. filename = '".$filename."',
  326. file_id = '".$file_id."'");
  327. return 1;
  328. }
  329. /**
  330. * @return array with attachments
  331. */
  332. function getAttachments()
  333. {
  334. $db = new DB_Sql;
  335. $db->query("SELECT file_id, filename FROM email_attachment
  336. WHERE intranet_id = " .$this->kernel->intranet->get('id') . " AND email_id = " . $this->id);
  337. $file = array();
  338. $i = 0;
  339. while ($db->nextRecord()) {
  340. $file[$i]['id'] = $db->f('file_id');
  341. $file[$i]['filename'] = $db->f('filename');
  342. $i++;
  343. }
  344. return $file;
  345. }
  346. /**
  347. * @deprecated
  348. *
  349. * @todo Denne funktion bør nok erstatte det meste af funktionen send(), s� send()
  350. * netop kun sender en e-mail!
  351. *
  352. * @return boolean
  353. */
  354. function sendAll($mailer)
  355. {
  356. $gateway = new Intraface_shared_email_EmailGateway($this->getKernel());
  357. return $gateway->sendAll($mailer);
  358. }
  359. function delete()
  360. {
  361. if ($this->get('status') == 'sent' or $this->id == 0) {
  362. return false;
  363. }
  364. $db = new DB_Sql;
  365. $db->query("DELETE FROM email WHERE id = " . $this->id . " AND intranet_id = " . $this->kernel->intranet->get('id'));
  366. return true;
  367. }
  368. /**
  369. * @deprecated
  370. *
  371. * @return array
  372. */
  373. function getList()
  374. {
  375. $gateway = new Intraface_shared_email_EmailGateway($this->getKernel());
  376. return $gateway->getAll();
  377. }
  378. /**
  379. * @deprecated
  380. *
  381. * @return integer of how many are in queue ot be sent
  382. */
  383. function countQueue()
  384. {
  385. $gateway = new Intraface_shared_email_EmailGateway($this->getKernel());
  386. return $gateway->countQueue();
  387. }
  388. /**
  389. * Checks how many emails has been sent the last hour
  390. *
  391. * @deprecated
  392. *
  393. * @return integer with numbers of e-mails sent
  394. */
  395. function sentThisHour()
  396. {
  397. $gateway = new Intraface_shared_email_EmailGateway($this->kernel);
  398. return $gateway->sentThisHour();
  399. }
  400. }