/system_remove/modules/avisota/AvisotaNewsletter.php

https://github.com/avisota/contao-core · PHP · 500 lines · 262 code · 75 blank · 163 comment · 27 complexity · 865b23ffa762e90136bcb1c0c5ed14bc MD5 · raw file

  1. <?php
  2. /**
  3. * Avisota newsletter and mailing system
  4. * Copyright © 2016 Sven Baumann
  5. *
  6. * PHP version 5
  7. *
  8. * @copyright way.vision 2015
  9. * @author Sven Baumann <baumann.sv@gmail.com>
  10. * @package avisota/contao-core
  11. * @license LGPL-3.0+
  12. * @filesource
  13. */
  14. /**
  15. * Class AvisotaNewsletter
  16. *
  17. * @copyright way.vision 2015
  18. * @author Sven Baumann <baumann.sv@gmail.com>
  19. * @package avisota/contao-core
  20. */
  21. class AvisotaNewsletter
  22. {
  23. public static function load($id)
  24. {
  25. if (!$id) {
  26. return null;
  27. }
  28. $resultSet = Database::getInstance()
  29. ->prepare(
  30. 'SELECT *
  31. FROM orm_avisota_message
  32. WHERE id=? OR alias=?'
  33. )
  34. ->execute($id, $id);
  35. if (!$resultSet->next()) {
  36. return null;
  37. }
  38. return new AvisotaNewsletter($resultSet);
  39. }
  40. /**
  41. * Subject of this newsletter.
  42. *
  43. * @var string
  44. */
  45. protected $subject;
  46. /**
  47. * Meta description of this newsletter.
  48. *
  49. * @var string
  50. */
  51. protected $description;
  52. /**
  53. * Meta keywords of this newsletter.
  54. *
  55. * @var string
  56. */
  57. protected $keywords;
  58. /**
  59. * The theme of thins newsletter.
  60. *
  61. * @var AvisotaTheme
  62. */
  63. protected $theme;
  64. /**
  65. * File attachments.
  66. *
  67. * @var array
  68. */
  69. protected $attachments;
  70. /**
  71. * The content array contains the database rows of content, grouped by cell.
  72. *
  73. * for example:
  74. * <pre>
  75. * array(
  76. * 'body' => array(
  77. * array(
  78. * 'id' => 1,
  79. * 'type' => 'text',
  80. * ...
  81. * ),
  82. * array(
  83. * 'id' => 2,
  84. * 'type' => 'image',
  85. * ...
  86. * ),
  87. * ...
  88. * ),
  89. * 'right' => array(
  90. * array(
  91. * 'id' => 6,
  92. * ...
  93. * ),
  94. * ...
  95. * ),
  96. * ...
  97. * )
  98. * </pre>
  99. *
  100. * @var array
  101. */
  102. protected $contentArray;
  103. /**
  104. * @var array
  105. */
  106. protected $data;
  107. function __construct(Database_Result $resultSet = null)
  108. {
  109. foreach ($resultSet->row() as $name => $value) {
  110. $this->$name = $value;
  111. }
  112. }
  113. function __set($name, $value)
  114. {
  115. switch ($name) {
  116. case 'subject':
  117. $this->setSubject($value);
  118. break;
  119. case 'description':
  120. $this->setDescription($value);
  121. break;
  122. case 'keywords':
  123. $this->setKeywords($value);
  124. break;
  125. case 'theme':
  126. $this->setTheme($value);
  127. break;
  128. case 'attachments':
  129. $this->setAttachments($value);
  130. break;
  131. default:
  132. $this->data[$name] = $value;
  133. }
  134. }
  135. function __get($name)
  136. {
  137. switch ($name) {
  138. case 'subject':
  139. return $this->getSubject();
  140. case 'description':
  141. return $this->getDescription();
  142. case 'keywords':
  143. return $this->getKeywords();
  144. case 'theme':
  145. return $this->getTheme();
  146. case 'attachments':
  147. return $this->getAttachments();
  148. default:
  149. return $this->data[$name];
  150. }
  151. }
  152. /**
  153. * @param string $subject
  154. */
  155. public function setSubject($subject)
  156. {
  157. $this->subject = $subject;
  158. }
  159. /**
  160. * @return string
  161. */
  162. public function getSubject()
  163. {
  164. return $this->subject;
  165. }
  166. /**
  167. * @param string $description
  168. */
  169. public function setDescription($description)
  170. {
  171. $this->description = $description;
  172. }
  173. /**
  174. * @return string
  175. */
  176. public function getDescription()
  177. {
  178. return $this->description;
  179. }
  180. /**
  181. * @param string $keywords
  182. */
  183. public function setKeywords($keywords)
  184. {
  185. $this->keywords = $keywords;
  186. }
  187. /**
  188. * @return string
  189. */
  190. public function getKeywords()
  191. {
  192. return $this->keywords;
  193. }
  194. /**
  195. * @param \AvisotaTheme $theme
  196. */
  197. public function setTheme($theme)
  198. {
  199. $this->theme = $theme;
  200. }
  201. /**
  202. * @abstract
  203. * @return AvisotaNewsletterTheme
  204. */
  205. public function getTheme()
  206. {
  207. return $this->theme;
  208. }
  209. /**
  210. * Add an attachment file.
  211. *
  212. * @param $file string
  213. */
  214. public function addAttachment($file, $name = null)
  215. {
  216. if (!$name) {
  217. $name = basename($file);
  218. }
  219. $this->attachments[$name] = $file;
  220. }
  221. /**
  222. * Add an attachment file.
  223. *
  224. * @param array $attachments
  225. */
  226. public function addAttachments($attachments)
  227. {
  228. if (is_string($attachments)) {
  229. $temp = deserialize($attachments);
  230. if (is_array($temp)) {
  231. $attachments = $temp;
  232. }
  233. else {
  234. $attachments = array($attachments);
  235. }
  236. }
  237. foreach ($attachments as $name => $file) {
  238. $this->addAttachment($file, is_numeric($name) ? null : $name);
  239. }
  240. }
  241. /**
  242. * @param array $attachments
  243. */
  244. public function setAttachments($attachments)
  245. {
  246. if (is_string($attachments)) {
  247. $temp = deserialize($attachments);
  248. if (is_array($temp)) {
  249. $attachments = $temp;
  250. }
  251. else {
  252. $attachments = array($attachments);
  253. }
  254. }
  255. $this->attachments = array();
  256. $this->addAttachments($attachments);
  257. }
  258. /**
  259. * @return array
  260. */
  261. public function getAttachments()
  262. {
  263. return $this->attachments;
  264. }
  265. /**
  266. * @param array $contentArray
  267. */
  268. public function setContentArray($contentArray)
  269. {
  270. $this->contentArray = $contentArray;
  271. }
  272. /**
  273. * @return array
  274. */
  275. public function getContentArray()
  276. {
  277. return $this->contentArray;
  278. }
  279. /**
  280. * @param array $data
  281. */
  282. public function setData($data)
  283. {
  284. $this->data = $data;
  285. }
  286. /**
  287. * @return array
  288. */
  289. public function getData()
  290. {
  291. return $this->data;
  292. }
  293. /**
  294. * Generate the newsletter html.
  295. *
  296. * @return mixed
  297. * @throws Exception
  298. */
  299. public function generateHtml()
  300. {
  301. if (!($this->theme instanceof AvisotaNewsletterTheme)) {
  302. throw new Exception('A newsletter need a theme!');
  303. }
  304. AvisotaStatic::pushNewsletter($this);
  305. $head = '';
  306. // Add meta information
  307. if ($this->description) {
  308. $head .= '<meta name="description" content="' . $this->description . '" />';
  309. }
  310. if ($this->keywords) {
  311. $head .= '<meta name="keywords" content="' . $this->keywords . '" />';
  312. }
  313. // Add style sheet newsletter.css
  314. $stylesheetCode = '';
  315. foreach ($this->theme->getStylesheets() as $stylesheetPathname) {
  316. $continue = true;
  317. // HOOK: add custom logic
  318. if (isset($GLOBALS['TL_HOOKS']['avisotaGetCss']) && is_array($GLOBALS['TL_HOOKS']['avisotaGetCss'])) {
  319. foreach ($GLOBALS['TL_HOOKS']['avisotaGetCss'] as $callback) {
  320. $this->import($callback[0]);
  321. $temp = $this->$callback[0]->$callback[1]($stylesheetPathname);
  322. if ($temp !== false) {
  323. // add content from file
  324. if (file_exists(TL_ROOT . '/' . $temp)) {
  325. $stylesheetPathname = $temp;
  326. break;
  327. }
  328. // add content directly as css
  329. else {
  330. $stylesheetCode .= trim($temp);
  331. $continue = false;
  332. }
  333. }
  334. }
  335. }
  336. if ($continue && file_exists(TL_ROOT . '/' . $stylesheetPathname)) {
  337. $file = new File($stylesheetPathname);
  338. $stylesheetCode .= AvisotaNewsletterContent::getInstance()
  339. ->cleanCSS($file->getContent(), $stylesheetPathname);
  340. $file->close();
  341. }
  342. }
  343. if ($stylesheetCode) {
  344. $head .= '<style>' . "\n" . $stylesheetCode . "\n" . '</style>';
  345. }
  346. $template = new AvisotaNewsletterTemplate($this->theme->getHtmlTemplate());
  347. $template->title = $this->subject;
  348. $template->head = $head;
  349. foreach ($this->theme->getAreas() as $areaName) {
  350. $template->$areaName = $this->generateContentHtml($areaName);
  351. }
  352. $template->newsletter = $this;
  353. $buffer = $this->replaceAndExtendURLs($template->parse());
  354. // reset static information
  355. AvisotaStatic::popNewsletter();
  356. return $buffer;
  357. }
  358. /**
  359. * Generate the content html for this newsletter.
  360. *
  361. * @return string
  362. */
  363. public function generateContentHtml($area)
  364. {
  365. if (!($this->theme instanceof AvisotaNewsletterTheme)) {
  366. throw new Exception('A newsletter need a theme!');
  367. }
  368. AvisotaStatic::pushNewsletter($this);
  369. $content = '';
  370. if (isset($this->contentArray[$area])) {
  371. foreach ($this->contentArray[$area] as $element) {
  372. $content .= AvisotaNewsletterContent::getInstance()
  373. ->generateNewsletterElement($element, NL_HTML);
  374. }
  375. }
  376. // reset static information
  377. AvisotaStatic::popNewsletter();
  378. return $content;
  379. }
  380. /**
  381. * Generate the newsletter text.
  382. *
  383. * @return mixed
  384. * @throws Exception
  385. */
  386. public function generateText()
  387. {
  388. if (!($this->theme instanceof AvisotaNewsletterTheme)) {
  389. throw new Exception('A newsletter need a theme!');
  390. }
  391. AvisotaStatic::pushNewsletter($this);
  392. $template = new AvisotaNewsletterTemplate($this->theme->getHtmlTemplate());
  393. $template->title = $this->subject;
  394. foreach ($this->theme->getAreas() as $areaName) {
  395. $template->$areaName = $this->generateContentText($areaName);
  396. }
  397. $template->newsletter = $this;
  398. $buffer = $template->parse();
  399. // reset static information
  400. AvisotaStatic::popNewsletter();
  401. return $buffer;
  402. }
  403. /**
  404. * Generate the content text for this newsletter.
  405. *
  406. * @return string
  407. */
  408. public function generateContentText($area)
  409. {
  410. if (!($this->theme instanceof AvisotaNewsletterTheme)) {
  411. throw new Exception('A newsletter need a theme!');
  412. }
  413. AvisotaStatic::pushNewsletter($this);
  414. $content = '';
  415. if (isset($this->contentArray[$area])) {
  416. foreach ($this->contentArray[$area] as $element) {
  417. $content .= AvisotaNewsletterContent::getInstance()
  418. ->generateNewsletterElement($element, NL_PLAIN);
  419. }
  420. }
  421. // reset static information
  422. AvisotaStatic::popNewsletter();
  423. return $content;
  424. }
  425. }