Browse code

Change looping order to fix the news only being sent to the first selected group

Benjamin Roth authored on05/08/2024 13:41:03
Showing1 changed files
... ...
@@ -68,57 +68,58 @@ class SendNewsNotificationJob
68 68
                 // Load groups and notification models if we have news to share
69 69
                 if ($News->rowCount() && ($Notification = Notification::findByPk($Archive->nc_notification)) !== null && ($Groups = MemberGroupModel::findMultipleByIds($arrGroupIds)) !== null)
70 70
                 {
71
-                    while ($Groups->next())
71
+                    foreach ($News->iterateAssociative() as $item)
72 72
                     {
73
-                        // Skip disabled groups
74
-                        if ($Groups->disable)
75
-                        {
76
-                            continue;
77
-                        }
73
+                        $arrNewsIds[] = $item['id'];
78 74
 
79
-                        // Get group members
80
-                        $Members = MemberModel::findBy(array("`groups` LIKE '%\"".$Groups->id."\"%'","(start='' OR start<='$time') AND (stop='' OR stop>'" . ($time + 60) . "') AND disable=''"),null);
81
-
82
-                        // Send notification to each member
83 75
                         $arrNews = array();
84 76
                         $arrNewsPlain = array();
85
-                        foreach ($News->iterateAssociative() as $item)
86
-                        {
87
-                            $arrNewsIds[] = $item['id'];
88 77
 
89
-                            if ($Members !== null)
78
+                        $strText = '';
79
+                        $objContentElement = ContentModel::findPublishedByPidAndTable($item['id'], 'tl_news');
80
+
81
+                        if ($objContentElement !== null)
82
+                        {
83
+                            while ($objContentElement->next())
90 84
                             {
91
-                                $strText = '';
92
-                                $objContentElement = ContentModel::findPublishedByPidAndTable($item['id'], 'tl_news');
85
+                                $strText .= Controller::getContentElement($objContentElement->current());
86
+                            }
87
+                        }
88
+                        $arrRow = array(
89
+                            'date' => date('d.m.Y',$item['date']),
90
+                            'headline' => $item['headline'],
91
+                            'teaser' => $item['teaser'] ? StringUtil::substr(strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$item['teaser'])),128) : '',
92
+                            'text_plain' => $strText ? strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$strText)) : ($item['teaser'] ? strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$item['teaser'])) : ''),
93
+                            'text_html' => $strText ?: $item['teaser']
94
+                        );
95
+
96
+                        if (($objJumpTo = $Archive->getRelated('jumpTo')) !== null)
97
+                        {
98
+                            //                                    $arrRow['url'] = $objJumpTo->getAbsoluteUrl(sprintf((Config::get('useAutoItem') ? '/%s' : '/items/%s'),$item['alias'])).'?ltoken=%%_TOKEN_%%';
99
+                            $arrRow['url'] = $objJumpTo->getAbsoluteUrl(sprintf((Config::get('useAutoItem') ? '/%s' : '/items/%s'),$item['alias']));
100
+                        }
93 101
 
94
-                                if ($objContentElement !== null)
95
-                                {
96
-                                    while ($objContentElement->next())
97
-                                    {
98
-                                        $strText .= Controller::getContentElement($objContentElement->current());
99
-                                    }
100
-                                }
101
-                                $arrRow = array(
102
-                                    'date' => date('d.m.Y',$item['date']),
103
-                                    'headline' => $item['headline'],
104
-                                    'teaser' => $item['teaser'] ? StringUtil::substr(strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$item['teaser'])),128) : '',
105
-                                    'text_plain' => $strText ? strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$strText)) : ($item['teaser'] ? strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$item['teaser'])) : ''),
106
-                                    'text_html' => $strText ?: $item['teaser']
107
-                                );
108
-
109
-                                if (($objJumpTo = $Archive->getRelated('jumpTo')) !== null)
110
-                                {
111
-    //                                    $arrRow['url'] = $objJumpTo->getAbsoluteUrl(sprintf((Config::get('useAutoItem') ? '/%s' : '/items/%s'),$item['alias'])).'?ltoken=%%_TOKEN_%%';
112
-                                    $arrRow['url'] = $objJumpTo->getAbsoluteUrl(sprintf((Config::get('useAutoItem') ? '/%s' : '/items/%s'),$item['alias']));
113
-                                }
102
+                        $arrNews[] = $arrRow;
103
+
104
+                        $arrNewsPlain[] = date('d.m.Y',$item['date']).' - '.$item['headline'];
105
+                        $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>';
106
+                        $arrNewsFullPlain[] = date('d.m.Y',$item['date']).' - '.$item['headline'] . "\n" . $arrRow['text_plain'];
107
+                        $arrNewsFullHtml[] = '<div style="margin-bottom: 15px;"><h3>'.$arrRow['date'].' '.$arrRow['headline'].'</h3>' . $arrRow['text_html'] . ($arrRow['url'] ? '<p><a href="'.$arrRow['url'].'">&raquo; Ganze Nachricht lesen...</a></p>' : '') .'</div>';
114 108
 
115
-                                $arrNews[] = $arrRow;
109
+                        while ($Groups->next())
110
+                        {
111
+                            // Skip disabled groups
112
+                            if ($Groups->disable)
113
+                            {
114
+                                continue;
115
+                            }
116 116
 
117
-                                $arrNewsPlain[] = date('d.m.Y',$item['date']).' - '.$item['headline'];
118
-                                $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>';
119
-                                $arrNewsFullPlain[] = date('d.m.Y',$item['date']).' - '.$item['headline'] . "\n" . $arrRow['text_plain'];
120
-                                $arrNewsFullHtml[] = '<div style="margin-bottom: 15px;"><h3>'.$arrRow['date'].' '.$arrRow['headline'].'</h3>' . $arrRow['text_html'] . ($arrRow['url'] ? '<p><a href="'.$arrRow['url'].'">&raquo; Ganze Nachricht lesen...</a></p>' : '') .'</div>';
117
+                            // Get group members
118
+                            $Members = MemberModel::findBy(array("`groups` LIKE '%\"".$Groups->id."\"%'","(start='' OR start<='$time') AND (stop='' OR stop>'" . ($time + 60) . "') AND disable=''"),null);
121 119
 
120
+                            // Send notification to each member
121
+                            if ($Members !== null)
122
+                            {
122 123
                                 while ($Members->next())
123 124
                                 {
124 125
                                     if (!$Members->nc_news_subscribe)
... ...
@@ -145,9 +146,9 @@ class SendNewsNotificationJob
145 146
                                 }
146 147
                             }
147 148
                         }
148
-
149 149
                     }
150 150
 
151
+
151 152
                     // Flag news as sent
152 153
                     if (count($arrNewsIds))
153 154
                     {
Browse code

Always reset send notification toggle on news after processing, even if there were no selectable members

Benjamin Roth authored on13/12/2023 08:11:05
Showing1 changed files
... ...
@@ -80,11 +80,13 @@ class SendNewsNotificationJob
80 80
                         $Members = MemberModel::findBy(array("`groups` LIKE '%\"".$Groups->id."\"%'","(start='' OR start<='$time') AND (stop='' OR stop>'" . ($time + 60) . "') AND disable=''"),null);
81 81
 
82 82
                         // Send notification to each member
83
-                        if ($Members !== null)
83
+                        $arrNews = array();
84
+                        $arrNewsPlain = array();
85
+                        foreach ($News->iterateAssociative() as $item)
84 86
                         {
85
-                            $arrNews = array();
86
-                            $arrNewsPlain = array();
87
-                            foreach ($News->iterateAssociative() as $item)
87
+                            $arrNewsIds[] = $item['id'];
88
+
89
+                            if ($Members !== null)
88 90
                             {
89 91
                                 $strText = '';
90 92
                                 $objContentElement = ContentModel::findPublishedByPidAndTable($item['id'], 'tl_news');
... ...
@@ -106,7 +108,7 @@ class SendNewsNotificationJob
106 108
 
107 109
                                 if (($objJumpTo = $Archive->getRelated('jumpTo')) !== null)
108 110
                                 {
109
-//                                    $arrRow['url'] = $objJumpTo->getAbsoluteUrl(sprintf((Config::get('useAutoItem') ? '/%s' : '/items/%s'),$item['alias'])).'?ltoken=%%_TOKEN_%%';
111
+    //                                    $arrRow['url'] = $objJumpTo->getAbsoluteUrl(sprintf((Config::get('useAutoItem') ? '/%s' : '/items/%s'),$item['alias'])).'?ltoken=%%_TOKEN_%%';
110 112
                                     $arrRow['url'] = $objJumpTo->getAbsoluteUrl(sprintf((Config::get('useAutoItem') ? '/%s' : '/items/%s'),$item['alias']));
111 113
                                 }
112 114
 
... ...
@@ -117,33 +119,33 @@ class SendNewsNotificationJob
117 119
                                 $arrNewsFullPlain[] = date('d.m.Y',$item['date']).' - '.$item['headline'] . "\n" . $arrRow['text_plain'];
118 120
                                 $arrNewsFullHtml[] = '<div style="margin-bottom: 15px;"><h3>'.$arrRow['date'].' '.$arrRow['headline'].'</h3>' . $arrRow['text_html'] . ($arrRow['url'] ? '<p><a href="'.$arrRow['url'].'">&raquo; Ganze Nachricht lesen...</a></p>' : '') .'</div>';
119 121
 
120
-                                $arrNewsIds[] = $item['id'];
121
-                            }
122
-                            while ($Members->next())
123
-                            {
124
-                                if (!$Members->nc_news_subscribe)
122
+                                while ($Members->next())
125 123
                                 {
126
-                                    continue;
127
-                                }
124
+                                    if (!$Members->nc_news_subscribe)
125
+                                    {
126
+                                        continue;
127
+                                    }
128 128
 
129
-                                //$strToken = \TokenLogin::getOrRenewUserToken($Members->current());
130
-                                $strToken = '';
131
-
132
-                                $Notification->send(array
133
-                                (
134
-                                    'member_email'      => $Members->email.($Members->nc_news_additionalEmail ? ','.$Members->nc_news_additionalEmail : ''),
135
-                                    'member_firstname' => $Members->firstname,
136
-                                    'member_lastname' => $Members->lastname,
137
-                                    'news_topics'       => implode("\n",$arrNewsPlain),
138
-                                    'news_topics_html'  => "<ul>\n<li>".str_replace('%%_TOKEN_%%',$strToken,implode("</li>\n<li>",$arrNewsHtml))."</li>\n</ul>",
139
-                                    'news_full' => implode("\n",$arrNewsFullPlain),
140
-                                    'news_full_html' => implode("",$arrNewsFullHtml),
141
-                                    'member_login_token'  => $strToken,
142
-                                    'admin_email'  => $admin_email,
143
-                                ),
144
-                                    $GLOBALS['TL_LANGUAGE']);
129
+                                    //$strToken = \TokenLogin::getOrRenewUserToken($Members->current());
130
+                                    $strToken = '';
131
+
132
+                                    $Notification->send(array
133
+                                    (
134
+                                        'member_email'      => $Members->email.($Members->nc_news_additionalEmail ? ','.$Members->nc_news_additionalEmail : ''),
135
+                                        'member_firstname' => $Members->firstname,
136
+                                        'member_lastname' => $Members->lastname,
137
+                                        'news_topics'       => implode("\n",$arrNewsPlain),
138
+                                        'news_topics_html'  => "<ul>\n<li>".str_replace('%%_TOKEN_%%',$strToken,implode("</li>\n<li>",$arrNewsHtml))."</li>\n</ul>",
139
+                                        'news_full' => implode("\n",$arrNewsFullPlain),
140
+                                        'news_full_html' => implode("",$arrNewsFullHtml),
141
+                                        'member_login_token'  => $strToken,
142
+                                        'admin_email'  => $admin_email,
143
+                                    ),
144
+                                        $GLOBALS['TL_LANGUAGE']);
145
+                                }
145 146
                             }
146 147
                         }
148
+
147 149
                     }
148 150
 
149 151
                     // Flag news as sent
Browse code

Send notifications to active members regardless of login status

Benjamin Roth authored on11/12/2023 15:01:09
Showing1 changed files
... ...
@@ -77,7 +77,7 @@ class SendNewsNotificationJob
77 77
                         }
78 78
 
79 79
                         // Get group members
80
-                        $Members = MemberModel::findBy(array("`groups` LIKE '%\"".$Groups->id."\"%'","login='1' AND (start='' OR start<='$time') AND (stop='' OR stop>'" . ($time + 60) . "') AND disable=''"),null);
80
+                        $Members = MemberModel::findBy(array("`groups` LIKE '%\"".$Groups->id."\"%'","(start='' OR start<='$time') AND (stop='' OR stop>'" . ($time + 60) . "') AND disable=''"),null);
81 81
 
82 82
                         // Send notification to each member
83 83
                         if ($Members !== null)
Browse code

Fix error if there are no news to send

Benjamin Roth authored on11/12/2023 12:25:11
Showing1 changed files
... ...
@@ -64,11 +64,10 @@ class SendNewsNotificationJob
64 64
                 // Do we have new news items
65 65
                 $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]);
66 66
 
67
+                $arrNewsIds = array();
67 68
                 // Load groups and notification models if we have news to share
68 69
                 if ($News->rowCount() && ($Notification = Notification::findByPk($Archive->nc_notification)) !== null && ($Groups = MemberGroupModel::findMultipleByIds($arrGroupIds)) !== null)
69 70
                 {
70
-                    $arrNewsIds = array();
71
-
72 71
                     while ($Groups->next())
73 72
                     {
74 73
                         // Skip disabled groups
Browse code

Possible fix for count error in job

Benjamin Roth authored on15/09/2023 10:01:05
Showing1 changed files
... ...
@@ -67,6 +67,7 @@ class SendNewsNotificationJob
67 67
                 // Load groups and notification models if we have news to share
68 68
                 if ($News->rowCount() && ($Notification = Notification::findByPk($Archive->nc_notification)) !== null && ($Groups = MemberGroupModel::findMultipleByIds($arrGroupIds)) !== null)
69 69
                 {
70
+                    $arrNewsIds = array();
70 71
 
71 72
                     while ($Groups->next())
72 73
                     {
... ...
@@ -84,7 +85,6 @@ class SendNewsNotificationJob
84 85
                         {
85 86
                             $arrNews = array();
86 87
                             $arrNewsPlain = array();
87
-                            $arrNewsIds = array();
88 88
                             foreach ($News->iterateAssociative() as $item)
89 89
                             {
90 90
                                 $strText = '';
Browse code

Fix another teaser is null issue

Benjamin Roth authored on08/09/2023 10:06:36
Showing1 changed files
... ...
@@ -101,7 +101,7 @@ class SendNewsNotificationJob
101 101
                                     'date' => date('d.m.Y',$item['date']),
102 102
                                     'headline' => $item['headline'],
103 103
                                     'teaser' => $item['teaser'] ? StringUtil::substr(strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$item['teaser'])),128) : '',
104
-                                    'text_plain' => $strText ? strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$strText)) : strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$item['teaser'])),
104
+                                    'text_plain' => $strText ? strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$strText)) : ($item['teaser'] ? strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$item['teaser'])) : ''),
105 105
                                     'text_html' => $strText ?: $item['teaser']
106 106
                                 );
107 107
 
Browse code

Add full message tokens

Benjamin Roth authored on04/04/2023 16:12:58
Showing1 changed files
... ...
@@ -13,6 +13,8 @@ declare(strict_types=1);
13 13
 namespace vonRotenberg\NewsmailerBundle\Cron;
14 14
 
15 15
 use Contao\Config;
16
+use Contao\ContentModel;
17
+use Contao\Controller;
16 18
 use Contao\CoreBundle\Monolog\ContaoContext;
17 19
 use Contao\CoreBundle\ServiceAnnotation\CronJob;
18 20
 use Contao\Date;
... ...
@@ -51,6 +53,8 @@ class SendNewsNotificationJob
51 53
 
52 54
         if ($Archives !== null)
53 55
         {
56
+            list($admin_name,$admin_email) = StringUtil::splitFriendlyEmail(Config::get('adminEmail'));
57
+
54 58
             while ($Archives->next())
55 59
             {
56 60
                 $Archive = $Archives->current();
... ...
@@ -83,10 +87,22 @@ class SendNewsNotificationJob
83 87
                             $arrNewsIds = array();
84 88
                             foreach ($News->iterateAssociative() as $item)
85 89
                             {
90
+                                $strText = '';
91
+                                $objContentElement = ContentModel::findPublishedByPidAndTable($item['id'], 'tl_news');
92
+
93
+                                if ($objContentElement !== null)
94
+                                {
95
+                                    while ($objContentElement->next())
96
+                                    {
97
+                                        $strText .= Controller::getContentElement($objContentElement->current());
98
+                                    }
99
+                                }
86 100
                                 $arrRow = array(
87 101
                                     'date' => date('d.m.Y',$item['date']),
88 102
                                     'headline' => $item['headline'],
89
-                                    'teaser' => $item['teaser'] ? StringUtil::substr(strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$item['teaser'])),128) : ''
103
+                                    'teaser' => $item['teaser'] ? StringUtil::substr(strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$item['teaser'])),128) : '',
104
+                                    'text_plain' => $strText ? strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$strText)) : strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$item['teaser'])),
105
+                                    'text_html' => $strText ?: $item['teaser']
90 106
                                 );
91 107
 
92 108
                                 if (($objJumpTo = $Archive->getRelated('jumpTo')) !== null)
... ...
@@ -99,6 +115,8 @@ class SendNewsNotificationJob
99 115
 
100 116
                                 $arrNewsPlain[] = date('d.m.Y',$item['date']).' - '.$item['headline'];
101 117
                                 $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>';
118
+                                $arrNewsFullPlain[] = date('d.m.Y',$item['date']).' - '.$item['headline'] . "\n" . $arrRow['text_plain'];
119
+                                $arrNewsFullHtml[] = '<div style="margin-bottom: 15px;"><h3>'.$arrRow['date'].' '.$arrRow['headline'].'</h3>' . $arrRow['text_html'] . ($arrRow['url'] ? '<p><a href="'.$arrRow['url'].'">&raquo; Ganze Nachricht lesen...</a></p>' : '') .'</div>';
102 120
 
103 121
                                 $arrNewsIds[] = $item['id'];
104 122
                             }
... ...
@@ -119,7 +137,10 @@ class SendNewsNotificationJob
119 137
                                     'member_lastname' => $Members->lastname,
120 138
                                     'news_topics'       => implode("\n",$arrNewsPlain),
121 139
                                     'news_topics_html'  => "<ul>\n<li>".str_replace('%%_TOKEN_%%',$strToken,implode("</li>\n<li>",$arrNewsHtml))."</li>\n</ul>",
122
-                                    'member_login_token'  => $strToken
140
+                                    'news_full' => implode("\n",$arrNewsFullPlain),
141
+                                    'news_full_html' => implode("",$arrNewsFullHtml),
142
+                                    'member_login_token'  => $strToken,
143
+                                    'admin_email'  => $admin_email,
123 144
                                 ),
124 145
                                     $GLOBALS['TL_LANGUAGE']);
125 146
                             }
Browse code

Only write execution to log if there was anything to do

Benjamin Roth authored on22/03/2023 11:24:37
Showing1 changed files
... ...
@@ -132,10 +132,10 @@ class SendNewsNotificationJob
132 132
                         $strNewsIds = implode(',',$arrNewsIds);
133 133
                         $this->db->executeStatement("UPDATE tl_news SET nc_sent = '1' WHERE id IN ($strNewsIds)");
134 134
                     }
135
+
136
+                    $this->logger->log(LogLevel::INFO, 'News notifications has been sent', array('contao' => new ContaoContext(__METHOD__, 'CRON')));
135 137
                 }
136 138
             }
137 139
         }
138
-        $this->logger->log(LogLevel::INFO, 'News notification has been executed', array('contao' => new ContaoContext(__METHOD__, 'CRON')));
139
-
140 140
     }
141 141
 }
Browse code

Fix teaser is null issue

Benjamin Roth authored on22/03/2023 10:43:52
Showing1 changed files
... ...
@@ -86,7 +86,7 @@ class SendNewsNotificationJob
86 86
                                 $arrRow = array(
87 87
                                     'date' => date('d.m.Y',$item['date']),
88 88
                                     'headline' => $item['headline'],
89
-                                    'teaser' => StringUtil::substr(strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$item['teaser'])),128)
89
+                                    'teaser' => $item['teaser'] ? StringUtil::substr(strip_tags(str_ireplace(array('<br />','<br>','<br/>','</p>','<p>'),' ',$item['teaser'])),128) : ''
90 90
                                 );
91 91
 
92 92
                                 if (($objJumpTo = $Archive->getRelated('jumpTo')) !== null)
Browse code

Updates

Benjamin Roth authored on22/03/2023 10:11:57
Showing1 changed files
... ...
@@ -91,7 +91,8 @@ class SendNewsNotificationJob
91 91
 
92 92
                                 if (($objJumpTo = $Archive->getRelated('jumpTo')) !== null)
93 93
                                 {
94
-                                    $arrRow['url'] = $objJumpTo->getAbsoluteUrl(sprintf((Config::get('useAutoItem') ? '/%s' : '/items/%s'),$item['alias'])).'?ltoken=%%_TOKEN_%%';
94
+//                                    $arrRow['url'] = $objJumpTo->getAbsoluteUrl(sprintf((Config::get('useAutoItem') ? '/%s' : '/items/%s'),$item['alias'])).'?ltoken=%%_TOKEN_%%';
95
+                                    $arrRow['url'] = $objJumpTo->getAbsoluteUrl(sprintf((Config::get('useAutoItem') ? '/%s' : '/items/%s'),$item['alias']));
95 96
                                 }
96 97
 
97 98
                                 $arrNews[] = $arrRow;
Browse code

Updates

Benjamin Roth authored on22/03/2023 10:08:48
Showing1 changed files
... ...
@@ -80,6 +80,7 @@ class SendNewsNotificationJob
80 80
                         {
81 81
                             $arrNews = array();
82 82
                             $arrNewsPlain = array();
83
+                            $arrNewsIds = array();
83 84
                             foreach ($News->iterateAssociative() as $item)
84 85
                             {
85 86
                                 $arrRow = array(
... ...
@@ -90,13 +91,15 @@ class SendNewsNotificationJob
90 91
 
91 92
                                 if (($objJumpTo = $Archive->getRelated('jumpTo')) !== null)
92 93
                                 {
93
-                                    $arrRow['url'] = $objJumpTo->getAbsoluteUrl(sprintf((Config::get('useAutoItem') ? '/%s' : '/items/%s'),$News->alias)).'?ltoken=%%_TOKEN_%%';
94
+                                    $arrRow['url'] = $objJumpTo->getAbsoluteUrl(sprintf((Config::get('useAutoItem') ? '/%s' : '/items/%s'),$item['alias'])).'?ltoken=%%_TOKEN_%%';
94 95
                                 }
95 96
 
96 97
                                 $arrNews[] = $arrRow;
97 98
 
98 99
                                 $arrNewsPlain[] = date('d.m.Y',$item['date']).' - '.$item['headline'];
99 100
                                 $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>';
101
+
102
+                                $arrNewsIds[] = $item['id'];
100 103
                             }
101 104
                             while ($Members->next())
102 105
                             {
... ...
@@ -123,12 +126,11 @@ class SendNewsNotificationJob
123 126
                     }
124 127
 
125 128
                     // Flag news as sent
126
-                    $arrNewsIds = array();
127
-                    foreach ($News->iterateAssociative() as $newsRow)
129
+                    if (count($arrNewsIds))
128 130
                     {
129
-                        $arrNewsIds[] = $newsRow['id'];
131
+                        $strNewsIds = implode(',',$arrNewsIds);
132
+                        $this->db->executeStatement("UPDATE tl_news SET nc_sent = '1' WHERE id IN ($strNewsIds)");
130 133
                     }
131
-                    $this->db->executeStatement("UPDATE tl_news SET nc_sent = '1' WHERE id IN (".implode(',',$arrNewsIds).")");
132 134
                 }
133 135
             }
134 136
         }
Browse code

Update

Benjamin Roth authored on22/03/2023 01:11:49
Showing1 changed files
... ...
@@ -73,7 +73,7 @@ class SendNewsNotificationJob
73 73
                         }
74 74
 
75 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);
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 77
 
78 78
                         // Send notification to each member
79 79
                         if ($Members !== null)
Browse code

Update

Benjamin Roth authored on22/03/2023 01:01:57
Showing1 changed files
1 1
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
+}