PageRenderTime 79ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/system_remove/modules/avisota/AvisotaBackend.php

https://github.com/avisota/contao-core
PHP | 292 lines | 176 code | 26 blank | 90 comment | 24 complexity | 4fcaeaa8cc0f7c49f9b145ee15ceafeb 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 AvisotaBackend
  16. *
  17. * @copyright way.vision 2015
  18. * @author Sven Baumann <baumann.sv@gmail.com>
  19. * @package avisota/contao-core
  20. */
  21. class AvisotaBackend extends Controller
  22. {
  23. /**
  24. * @var AvisotaBackend
  25. */
  26. protected static $instance = null;
  27. /**
  28. * @static
  29. * @return AvisotaBackend
  30. */
  31. public static function getInstance()
  32. {
  33. if (self::$instance === null) {
  34. self::$instance = new AvisotaBackend();
  35. }
  36. return self::$instance;
  37. }
  38. protected function __construct()
  39. {
  40. parent::__construct();
  41. $this->import('Database');
  42. }
  43. /**
  44. * Get options list of recipients.
  45. *
  46. * @return array
  47. */
  48. public function getRecipients($prefixSourceId = false)
  49. {
  50. $recipients = array();
  51. $source = \Database::getInstance()
  52. ->execute("SELECT * FROM orm_avisota_recipient_source WHERE disable='' ORDER BY sorting");
  53. while ($source->next()) {
  54. if (isset($GLOBALS['orm_avisota_RECIPIENT_SOURCE'][$source->type])) {
  55. $class = $GLOBALS['orm_avisota_RECIPIENT_SOURCE'][$source->type];
  56. $instance = new $class($source->row());
  57. $options = $instance->getRecipientOptions();
  58. if (count($options)) {
  59. $sourceOptions = array();
  60. foreach ($options as $k => $v) {
  61. $sourceOptions[$source->id . ':' . $k] = $v;
  62. }
  63. $recipients[($prefixSourceId ? $source->id . ':'
  64. : '') . $source->title] = $sourceOptions;
  65. }
  66. }
  67. else {
  68. $this->log(
  69. 'Recipient source "' . $source->type . '" type not found!',
  70. 'AvisotaBackend::getRecipients()',
  71. TL_ERROR
  72. );
  73. $this->redirect('contao/main.php?act=error');
  74. }
  75. }
  76. return $recipients;
  77. }
  78. public function hookOutputBackendTemplate($content, $template)
  79. {
  80. if ($template == 'be_main') {
  81. # add form multipart enctype
  82. if (($this->Input->get('table') == 'orm_avisota_recipient_import' || $this->Input->get(
  83. 'table'
  84. ) == 'orm_avisota_recipient_remove')
  85. ) {
  86. $content = str_replace('<form', '<form enctype="multipart/form-data"', $content);
  87. }
  88. }
  89. return $content;
  90. }
  91. public function hookAvisotaMailingListLabel($row, $label, DataContainer $dc)
  92. {
  93. $result = \Database::getInstance()
  94. ->prepare(
  95. "SELECT
  96. (SELECT COUNT(rl.recipient) FROM orm_avisota_recipient_to_mailing_list rl WHERE rl.list=?) as total_recipients,
  97. (SELECT COUNT(rl.recipient) FROM orm_avisota_recipient_to_mailing_list rl INNER JOIN orm_avisota_recipient r ON r.id=rl.recipient WHERE rl.confirmed=? AND rl.list=?) as disabled_recipients,
  98. (SELECT COUNT(ml.member) FROM tl_member_to_mailing_list ml WHERE ml.list=?) as total_members,
  99. (SELECT COUNT(ml.member) FROM tl_member_to_mailing_list ml INNER JOIN tl_member m ON m.id=ml.member WHERE m.disable=? AND ml.list=?) as disabled_members"
  100. )
  101. ->execute($row['id'], '', $row['id'], $row['id'], '1', $row['id']);
  102. if ($result->next()) {
  103. if ($result->total_recipients > 0) {
  104. $label .= '<div style="padding: 1px 0;">' .
  105. '<a href="contao/main.php?do=avisota_recipients&amp;showlist=' . $row['id'] . '">' .
  106. $this->generateImage('system/modules/avisota/html/recipients.png', '') .
  107. ' ' .
  108. sprintf(
  109. $GLOBALS['TL_LANG']['orm_avisota_mailing_list']['label_recipients'],
  110. $result->total_recipients,
  111. $result->total_recipients - $result->disabled_recipients,
  112. $result->disabled_recipients
  113. ) .
  114. '</a>' .
  115. '</div>';
  116. }
  117. if ($result->total_members > 0) {
  118. $label .= '<div style="padding: 1px 0;">' .
  119. '<a href="contao/main.php?do=member&amp;avisota_showlist=' . $row['id'] . '">' .
  120. $this->generateImage('system/themes/default/images/member.gif', '') .
  121. ' ' .
  122. sprintf(
  123. $GLOBALS['TL_LANG']['orm_avisota_mailing_list']['label_members'],
  124. $result->total_members,
  125. $result->total_members - $result->disabled_members,
  126. $result->disabled_members
  127. ) .
  128. '</a>' .
  129. '</div>';
  130. }
  131. }
  132. return $label;
  133. }
  134. /**
  135. * Build custom back end modules
  136. */
  137. public function hookGetUserNavigation($modules, $showAll)
  138. {
  139. if (isset($modules['avisota'])) {
  140. foreach ($modules['avisota']['modules'] as $moduleName => &$module) {
  141. if (preg_match('#^avisota_newsletter_(\d+)$#', $moduleName, $match)) {
  142. $categoryId = $match[1];
  143. // $module['class'] = str_replace(' active', '', $module['class']);
  144. $module['href'] .= '&amp;table=orm_avisota_message&amp;id=' . $categoryId;
  145. // if this category is active
  146. if ($this->Input->get('do') == 'avisota_newsletter' &&
  147. $this->Input->get('table') == 'orm_avisota_message' &&
  148. $this->Input->get('act') != 'edit' &&
  149. $this->Input->get('id') == $categoryId
  150. ) {
  151. // remove active class from avisota_newsletter menu item
  152. $modules['avisota']['modules']['avisota_newsletter']['class'] = str_replace(
  153. ' active',
  154. '',
  155. $modules['avisota']['modules']['avisota_newsletter']['class']
  156. );
  157. // add active class to this category menu item
  158. $module['class'] .= ' active';
  159. }
  160. }
  161. }
  162. /*
  163. $arrCustomModules = array();
  164. if (\Database::getInstance()->fieldExists('showInMenu', 'orm_avisota_message_category')) {
  165. $objCategory = \Database::getInstance()->query('SELECT * FROM orm_avisota_message_category WHERE showInMenu=\'1\' ORDER BY title');
  166. while ($objCategory->next()) {
  167. $arrCustomModules['avisota_newsletter_' . $objCategory->id] = array_slice($arrModules['avisota']['modules']['avisota_newsletter'], 0);
  168. if ($objCategory->menuIcon) {
  169. $arrCustomModules['avisota_newsletter_' . $objCategory->id]['icon'] = sprintf(' style="background-image:url(\'%s\')"', $objCategory->menuIcon);
  170. }
  171. $arrCustomModules['avisota_newsletter_' . $objCategory->id]['label'] = $objCategory->title;
  172. $arrCustomModules['avisota_newsletter_' . $objCategory->id]['class'] = str_replace(' active', '', $arrCustomModules['avisota_newsletter_' . $objCategory->id]['class']);
  173. $arrCustomModules['avisota_newsletter_' . $objCategory->id]['class'] .= ' avisota_newsletter_' . $objCategory->id;
  174. $arrCustomModules['avisota_newsletter_' . $objCategory->id]['href'] .= '&amp;table=orm_avisota_message&amp;id=' . $objCategory->id;
  175. // if this category is active
  176. if ($this->Input->get('do') == 'avisota_newsletter' &&
  177. $this->Input->get('table') == 'orm_avisota_message' &&
  178. $this->Input->get('act') != 'edit' &&
  179. $this->Input->get('id') == $objCategory->id) {
  180. // remove active class from avisota_newsletter menu item
  181. $arrModules['avisota']['modules']['avisota_newsletter']['class'] = str_replace(' active', '', $arrModules['avisota']['modules']['avisota_newsletter']['class']);
  182. // add active class to this category menu item
  183. $arrCustomModules['avisota_newsletter_' . $objCategory->id]['class'] .= ' active';
  184. }
  185. }
  186. }
  187. $i = array_search('avisota_newsletter', array_keys($arrModules['avisota']['modules']));
  188. $arrModules['avisota']['modules'] = array_merge(
  189. array_slice($arrModules['avisota']['modules'], 0, $i),
  190. $arrCustomModules,
  191. array_slice($arrModules['avisota']['modules'], $i)
  192. );
  193. */
  194. }
  195. return $modules;
  196. }
  197. /**
  198. * Send an email.
  199. *
  200. * @param string $strMode
  201. * @param string $plainContent
  202. * @param string $htmlContent
  203. * @param string $recipientMail
  204. */
  205. protected function sendMail($module, $page, $plainContent, $htmlContent, $recipientMail)
  206. {
  207. $rootPage = $this->getPageDetails($page->rootId);
  208. $email = new Email();
  209. $email->subject = $GLOBALS['TL_LANG']['avisota']['notification']['mail']['subject'];
  210. $email->logFile = 'subscription.log';
  211. $email->text = $plainContent;
  212. $email->html = $htmlContent;
  213. $email->from = $module->avisota_subscription_sender
  214. ? $module->avisota_subscription_sender
  215. : (strlen(
  216. $rootPage->adminEmail
  217. ) ? $rootPage->adminEmail : $GLOBALS['TL_CONFIG']['adminEmail']);
  218. // Add sender name
  219. if (strlen($module->avisota_subscription_sender_name)) {
  220. $email->fromName = $module->avisota_subscription_sender_name;
  221. }
  222. $email->imageDir = TL_ROOT . '/';
  223. try {
  224. $email->sendTo($recipientMail);
  225. return true;
  226. }
  227. catch (Swift_RfcComplianceException $e) {
  228. return $e->getMessage();
  229. }
  230. }
  231. /**
  232. * Convert id list to name list.
  233. *
  234. * @param array $listIds
  235. *
  236. * @return array
  237. */
  238. protected function getListNames($listIds)
  239. {
  240. $lists = array();
  241. $placeholders = array();
  242. for ($i = 0; $i < count($listIds); $i++) {
  243. $placeholders[] = '?';
  244. }
  245. $list = \Database::getInstance()
  246. ->prepare(
  247. "
  248. SELECT
  249. *
  250. FROM
  251. `orm_avisota_mailing_list`
  252. WHERE
  253. `id` IN (" . implode(',', $placeholders) . ")
  254. ORDER BY
  255. `title`"
  256. )
  257. ->execute($listIds);
  258. while ($list->next()) {
  259. $lists[] = $list->title;
  260. }
  261. return $lists;
  262. }
  263. }