Browse code

Update

Benjamin Roth authored on22/03/2023 01:01:57
Showing5 changed files
... ...
@@ -14,12 +14,11 @@ use Contao\CoreBundle\DataContainer\PaletteManipulator;
14 14
  * Palettes
15 15
  */
16 16
 
17
-//$GLOBALS['TL_DCA']['tl_news']['palettes']['default'] = str_replace(array('{publish_legend}','{publish_legend:hide}'),array('{nc_legend:hide},nc_sent,nc_testmail;{publish_legend}','{nc_legend:hide},nc_sent,nc_testmail;{publish_legend:hide}'),$GLOBALS['TL_DCA']['tl_news']['palettes']['default']);
18 17
 PaletteManipulator::create()
19
-    ->addLegend('nc_legend','publish_legend',PaletteManipulator::POSITION_BEFORE)
20
-    ->addField('nc_sent','nc_legend', PaletteManipulator::POSITION_APPEND)
21
-    ->addField('nc_testmail','nc_legend', PaletteManipulator::POSITION_APPEND)
22
-    ->applyToPalette('default','tl_news')
18
+    ->addLegend('nc_legend','contact_legend')
19
+    ->addField('nc_news_subscribe','nc_legend', PaletteManipulator::POSITION_APPEND)
20
+    ->addField('nc_news_additionalEmail','nc_legend', PaletteManipulator::POSITION_APPEND)
21
+    ->applyToPalette('default','tl_member')
23 22
 ;
24 23
 
25 24
 /**
... ...
@@ -31,10 +30,6 @@ $GLOBALS['TL_DCA']['tl_member']['fields']['nc_news_subscribe'] = array
31 30
     'exclude'                 => true,
32 31
     'inputType'               => 'checkbox',
33 32
     'eval'                    => array('tl_class'=>'w50 m12', 'doNotCopy'=>true, 'feEditable'=>true, 'feViewable'=>true, 'feGroup'=>'contact'),
34
-    'save_callback'           => array
35
-    (
36
-        array('tl_member_eSM_nc_news', 'setNcNewsChangedate'),
37
-    ),
38 33
     'sql'                     => "char(1) NOT NULL default '1'"
39 34
 );
40 35
 
41 36
new file mode 100644
... ...
@@ -0,0 +1,26 @@
1
+<?xml version="1.0" ?><xliff version="1.1">
2
+    <file datatype="php" original="tl_member" source-language="en" target-language="de">
3
+        <body>
4
+            <trans-unit id="tl_member.nc_news_subscribe.0">
5
+                <source>Notifications</source>
6
+                <target>Benachrichtigungen</target>
7
+            </trans-unit>
8
+            <trans-unit id="tl_member.nc_news_subscribe.1">
9
+                <source>Receive notifications about new news articles.</source>
10
+                <target>Benachrichtigungen zu neuen News-Artikeln erhalten.</target>
11
+            </trans-unit>
12
+            <trans-unit id="tl_member.nc_news_additionalEmail.0">
13
+                <source>Additional email addresses (for notifications)</source>
14
+                <target>Weitere E-Mail-Adressen (für Benachrichtigungen)</target>
15
+            </trans-unit>
16
+            <trans-unit id="tl_member.nc_news_additionalEmail.1">
17
+                <source>Additional email addresses, comma separated, to be used in addition for sending notifications via email about new news articles.</source>
18
+                <target>Weitere E-Mail-Adressen, kommagetrennt. Werden zusätzlich genutzt für Benachrichtigungen per E-Mail über neue News-Artikel.</target>
19
+            </trans-unit>
20
+            <trans-unit id="tl_member.nc_legend">
21
+                <source>Notifications settings</source>
22
+                <target>Benachrichtigungs-Einstellungen</target>
23
+            </trans-unit>
24
+        </body>
25
+    </file>
26
+</xliff>
0 27
new file mode 100644
... ...
@@ -0,0 +1,138 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+* This file is part of newsmailer bundle for Contao.
7
+*
8
+* (c) Benjamin Roth
9
+*
10
+* @license LGPL-3.0-or-later
11
+*/
12
+
13
+namespace vonRotenberg\NewsmailerBundle\Cron;
14
+
15
+use Contao\Config;
16
+use Contao\CoreBundle\Monolog\ContaoContext;
17
+use Contao\CoreBundle\ServiceAnnotation\CronJob;
18
+use Contao\Date;
19
+use Contao\MemberGroupModel;
20
+use Contao\MemberModel;
21
+use Contao\NewsArchiveModel;
22
+use Contao\StringUtil;
23
+use Doctrine\DBAL\Connection;
24
+use NotificationCenter\Model\Notification;
25
+use Psr\Log\LoggerInterface;
26
+use Psr\Log\LogLevel;
27
+
28
+/**
29
+ * @CronJob("minutely")
30
+ */
31
+class SendNewsNotificationJob
32
+{
33
+    /** @var LoggerInterface  */
34
+    private $logger;
35
+
36
+    /** @var Connection */
37
+    private $db;
38
+
39
+    public function __construct(Connection $db, LoggerInterface $logger)
40
+    {
41
+        $this->logger = $logger;
42
+        $this->db = $db;
43
+    }
44
+
45
+    public function __invoke(string $scope)
46
+    {
47
+        list($admin_name,$admin_email) = StringUtil::splitFriendlyEmail(Config::get('adminEmail'));
48
+
49
+        // Get archives with notifications enabled
50
+        $Archives = NewsArchiveModel::findBy('nc_enable','1');
51
+
52
+        if ($Archives !== null)
53
+        {
54
+            while ($Archives->next())
55
+            {
56
+                $Archive = $Archives->current();
57
+                $arrGroupIds = StringUtil::deserialize($Archive->nc_notification_groups,true);
58
+                $time = Date::floorToMinute();
59
+
60
+                // Do we have new news items
61
+                $News = $this->db->executeQuery("SELECT id, alias, headline, `date`, teaser FROM tl_news WHERE pid = ? AND nc_sent != '1' AND date <='$time' AND (start='' OR start<='$time') AND (stop='' OR stop>'" . ($time + 60) . "') AND published='1' ORDER BY `date` DESC, `time` DESC",[$Archive->id]);
62
+
63
+                // Load groups and notification models if we have news to share
64
+                if ($News->rowCount() && ($Notification = Notification::findByPk($Archive->nc_notification)) !== null && ($Groups = MemberGroupModel::findMultipleByIds($arrGroupIds)) !== null)
65
+                {
66
+
67
+                    while ($Groups->next())
68
+                    {
69
+                        // Skip disabled groups
70
+                        if ($Groups->disable)
71
+                        {
72
+                            continue;
73
+                        }
74
+
75
+                        // Get group members
76
+                        $Members = MemberModel::findBy(array("groups LIKE '%\"".$Groups->id."\"%'","login='1' AND (start='' OR start<='$time') AND (stop='' OR stop>'" . ($time + 60) . "') AND disable=''"),null);
77
+
78
+                        // Send notification to each member
79
+                        if ($Members !== null)
80
+                        {
81
+                            $arrNews = array();
82
+                            $arrNewsPlain = array();
83
+                            foreach ($News->iterateAssociative() as $item)
84
+                            {
85
+                                $arrRow = array(
86
+                                    'date' => date('d.m.Y',$item['date']),
87
+                                    'headline' => $item['headline'],
88
+                                    'teaser' => StringUtil::substr(strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$item['teaser'])),128)
89
+                                );
90
+
91
+                                if (($objJumpTo = $Archive->getRelated('jumpTo')) !== null)
92
+                                {
93
+                                    $arrRow['url'] = $objJumpTo->getAbsoluteUrl(sprintf((Config::get('useAutoItem') ? '/%s' : '/items/%s'),$News->alias)).'?ltoken=%%_TOKEN_%%';
94
+                                }
95
+
96
+                                $arrNews[] = $arrRow;
97
+
98
+                                $arrNewsPlain[] = date('d.m.Y',$item['date']).' - '.$item['headline'];
99
+                                $arrNewsHtml[] = '<div style="margin-bottom: 15px;"><h3>'.$arrRow['date'].' '.$arrRow['headline'].'</h3><p>'.$arrRow['teaser'].'</p>'. ($arrRow['url'] ? '<p><a href="'.$arrRow['url'].'">&raquo; Ganze Nachricht lesen...</a></p>' : '') .'</div>';
100
+                            }
101
+                            while ($Members->next())
102
+                            {
103
+                                if (!$Members->nc_news_subscribe)
104
+                                {
105
+                                    continue;
106
+                                }
107
+
108
+                                //$strToken = \TokenLogin::getOrRenewUserToken($Members->current());
109
+                                $strToken = '';
110
+
111
+                                $Notification->send(array
112
+                                (
113
+                                    'member_email'      => $Members->email.($Members->nc_news_additionalEmail ? ','.$Members->nc_news_additionalEmail : ''),
114
+                                    'member_firstname' => $Members->firstname,
115
+                                    'member_lastname' => $Members->lastname,
116
+                                    'news_topics'       => implode("\n",$arrNewsPlain),
117
+                                    'news_topics_html'  => "<ul>\n<li>".str_replace('%%_TOKEN_%%',$strToken,implode("</li>\n<li>",$arrNewsHtml))."</li>\n</ul>",
118
+                                    'member_login_token'  => $strToken
119
+                                ),
120
+                                    $GLOBALS['TL_LANGUAGE']);
121
+                            }
122
+                        }
123
+                    }
124
+
125
+                    // Flag news as sent
126
+                    $arrNewsIds = array();
127
+                    foreach ($News->iterateAssociative() as $newsRow)
128
+                    {
129
+                        $arrNewsIds[] = $newsRow['id'];
130
+                    }
131
+                    $this->db->executeStatement("UPDATE tl_news SET nc_sent = '1' WHERE id IN (".implode(',',$arrNewsIds).")");
132
+                }
133
+            }
134
+        }
135
+        $this->logger->log(LogLevel::INFO, 'News notification has been executed', array('contao' => new ContaoContext(__METHOD__, 'CRON')));
136
+
137
+    }
138
+}
0 139
new file mode 100644
... ...
@@ -0,0 +1,33 @@
1
+<?php
2
+
3
+declare(strict_types=1);
4
+
5
+/*
6
+* This file is part of newsmailer bundle for Contao.
7
+*
8
+* (c) Benjamin Roth
9
+*
10
+* @license LGPL-3.0-or-later
11
+*/
12
+
13
+namespace vonRotenberg\NewsmailerBundle\EventListener;
14
+
15
+use Contao\CoreBundle\ServiceAnnotation\Callback;
16
+use Contao\Database;
17
+use Contao\DataContainer;
18
+
19
+/**
20
+ * @Callback(table="tl_member", target="fields.nc_news_subscribe.save")
21
+ */
22
+class MemberNcNewsNotificationChangedateListener
23
+{
24
+    public function __invoke($varValue, DataContainer $dc)
25
+    {
26
+        if ($varValue != $dc->activeRecord->nc_news_subscribe)
27
+        {
28
+            Database::getInstance()->prepare("UPDATE tl_member SET nc_news_subscribe_changed=? WHERE id=?")->execute(array(time(),$dc->id));
29
+        }
30
+
31
+        return $varValue;
32
+    }
33
+}
... ...
@@ -65,10 +65,10 @@ class NewsSendTestmailListener
65 65
                 {
66 66
                     $strLastname = $Member->firstname;
67 67
                     $strFirstname = $Member->lastname;
68
-                    /*if ($Member->nc_news_additionalEmail)
68
+                    if ($Member->nc_news_additionalEmail)
69 69
                     {
70 70
                         $strEmail .= ','.$Member->nc_news_additionalEmail;
71
-                    }*/
71
+                    }
72 72
 //                    $strToken = \TokenLogin::getOrRenewUserToken($Member);
73 73
                     $strToken = '';
74 74
                 } else {