Browse code

Update cronjob to use the newly introduced memberfiles configurations instead of the deprecated root page settings

Benjamin Roth authored on08/05/2025 11:02:03
Showing1 changed files
... ...
@@ -30,6 +30,7 @@ use Doctrine\DBAL\Connection;
30 30
 use NotificationCenter\Model\Notification;
31 31
 use Psr\Log\LoggerInterface;
32 32
 use Psr\Log\LogLevel;
33
+use vonRotenberg\MemberfilesBundle\Model\MemberfilesConfigModel;
33 34
 
34 35
 class SecureDownloadsJob
35 36
 {
... ...
@@ -52,17 +53,17 @@ class SecureDownloadsJob
52 53
     public function sendFileNotifications(string $scope)
53 54
     {
54 55
         // Get archives with notifications enabled
55
-        $Root = PageModel::findBy(array("sd_nc_enable = ?","type = 'root'"),'1');
56
+        $Configurations = MemberfilesConfigModel::findBy(["hasNotifications = ?", "enabled = ?"], ['1','1']);
56 57
 
57
-        if ($Root !== null)
58
+        if ($Configurations !== null)
58 59
         {
59
-            while ($Root->next())
60
+            while ($Configurations->next())
60 61
             {
61 62
                 // Do we have new items
62 63
                 $Members = $this->db->executeQuery("SELECT s.pid FROM tl_member_secureDownloads s INNER JOIN tl_files f ON f.uuid = s.uuid WHERE s.nc_sent != '1' GROUP BY s.pid");
63 64
 
64 65
                 // Load groups and notification models if we have news to share
65
-                if ($Members->rowCount() && ($Notification = Notification::findByPk($Root->sd_nc_notification)) !== null)
66
+                if ($Members->rowCount() && ($Notification = Notification::findByPk($Configurations->sd_nc_notification)) !== null)
66 67
                 {
67 68
                     foreach ($Members->iterateAssociative() as $member)
68 69
                     {
... ...
@@ -108,72 +109,73 @@ class SecureDownloadsJob
108 109
      */
109 110
     public function importFiles(string $scope)
110 111
     {
111
-        $objRoots = PageModel::findPublishedRootPages();
112
+        $projectDir = System::getContainer()->getParameter('kernel.project_dir');
113
+        $Configurations = MemberfilesConfigModel::findAll();
112 114
 
113 115
         $intFileCount = 0;
114 116
 
115 117
         // No root pages available
116
-        if ($objRoots === null)
118
+        if ($Configurations === null)
117 119
         {
118 120
             return;
119 121
         }
120 122
 
121 123
         // Iterate page roots
122
-        while ($objRoots->next())
124
+        while ($Configurations->next())
123 125
         {
124 126
             if (System::getContainer()->getParameter('kernel.debug'))
125 127
             {
126
-                $this->logger->log(LogLevel::DEBUG, sprintf('Starting secure downloads import for %s', $objRoots->title), array('contao' => new ContaoContext(__METHOD__, 'CRON')));
128
+                $this->logger->log(LogLevel::DEBUG, sprintf('Starting secure downloads import for %s', $Configurations->title), array('contao' => new ContaoContext(__METHOD__, 'CRON')));
127 129
             }
128 130
 
129 131
             // Continue if secure downloads is disabled
130
-            if (!$objRoots->secureDownloadsEnabled)
132
+            if (!$Configurations->enabled)
131 133
             {
132 134
                 continue;
133 135
             }
134 136
 
135 137
             // Get folder models
136
-            $objFolder = FilesModel::findByUuid($objRoots->secureDownloadsSRC);
137
-            $objTarget = FilesModel::findByUuid($objRoots->secureDownloadsTarget);
138
+            $Source = FilesModel::findByUuid($Configurations->source);
139
+            $Target = FilesModel::findByUuid($Configurations->target);
138 140
 
139 141
             // Continue if folder doesn't exist
140
-            if ($objFolder === null || !file_exists(TL_ROOT . "/".$objFolder->path) || $objTarget === null || !file_exists(TL_ROOT . "/".$objTarget->path))
142
+            if ($Source === null || !file_exists($projectDir . "/".$Source->path) || $Target === null || !file_exists($projectDir . "/".$Target->path))
141 143
             {
142
-                $this->logger->log(LogLevel::WARNING, sprintf('Source or target folder is missing for page root %s. %s %s', $objRoots->title,$objFolder->path,$objTarget->path), array('contao' => new ContaoContext(__METHOD__, 'ERROR')));
144
+                $this->logger->log(LogLevel::WARNING, sprintf('Source or target folder is missing for page root %s. %s %s', $Configurations->title,$Source->path,$Target->path), array('contao' => new ContaoContext(__METHOD__, 'ERROR')));
143 145
                 continue;
144 146
             }
145 147
 
146 148
             // Read files inside folder
147
-            $objFiles = FilesModel::findByPid($objFolder->uuid);
149
+            $Files = FilesModel::findByPid($Source->uuid);
148 150
 
149 151
             // Continue if folder is empty
150
-            if ($objFiles === null)
152
+            if ($Files === null)
151 153
             {
152 154
                 continue;
153 155
             }
154 156
 
155 157
             // Escape single quotes in fragment regexp
156
-            $objRoots->secureDownloadsRegExp = addcslashes($objRoots->secureDownloadsRegExp,'\'');
158
+            $Configurations->regexp = addcslashes($Configurations->regexp,'\'');
157 159
 
158 160
             // Iterate files
159
-            while ($objFiles->next())
161
+            while ($Files->next())
160 162
             {
161 163
                 // Skip subfolders and special files
162
-                if ($objFiles->type == 'folder' || strpos($objFiles->name,'.') === 0)
164
+                if ($Files->type == 'folder' || strpos($Files->name,'.') === 0)
163 165
                 {
164 166
                     continue;
165 167
                 }
166 168
 
167 169
                 // Continue if file doesn't exist
168
-                if (!file_exists(TL_ROOT . "/".$objFiles->path))
170
+                if (!file_exists($projectDir . "/".$Files->path))
169 171
                 {
170 172
                     continue;
171 173
                 }
172 174
 
173
-                $objFile = new File($objFiles->path);
175
+                $objFile = new File($Files->path);
174 176
 
175 177
                 // Check for fragments
176
-                if (!preg_match('/'.$objRoots->secureDownloadsRegExp.'/U',$objFile->filename,$fragments))
178
+                if (!preg_match('/'.$Configurations->regexp.'/U',$objFile->filename,$fragments))
177 179
                 {
178 180
                     $this->logger->log(LogLevel::WARNING, sprintf('File "%s" is missing a member fragment or has the wrong syntax.', $objFile->name), array('contao' => new ContaoContext(__METHOD__, 'ERROR')));
179 181
                     continue;
... ...
@@ -182,7 +184,7 @@ class SecureDownloadsJob
182 184
                 // Find member
183 185
                 $arrColumns = array();
184 186
                 $arrValues = array();
185
-                foreach (StringUtil::deserialize($objRoots->secureDownloadsFields, true) as $i => $field)
187
+                foreach (StringUtil::deserialize($Configurations->fields, true) as $i => $field)
186 188
                 {
187 189
                     $arrColumns[] = "$field = ?";
188 190
                     $arrValues[] = $fragments[$i+1];
... ...
@@ -196,19 +198,19 @@ class SecureDownloadsJob
196 198
                 }
197 199
 
198 200
                 // Remove fragments from file name
199
-                $baseFilename = preg_replace('/'.$objRoots->secureDownloadsRegExp.'/U','',$objFile->filename);
201
+                $baseFilename = preg_replace('/'.$Configurations->regexp.'/U','',$objFile->filename);
200 202
                 $sanitizedFilename = $baseFilename.'.'.$objFile->extension;
201 203
 
202 204
                 // Check or create folder structure
203 205
                 $strSubFolder = sprintf("%03d",ceil(($objMember->id+1)/100)-1).'/'.$objMember->id;
204
-                $strTargetFolder = $objTarget->path.'/'.$strSubFolder;
206
+                $strTargetFolder = $Target->path.'/'.$strSubFolder;
205 207
 
206 208
                 $arrFolders = explode('/',$strTargetFolder);
207 209
                 $strStartPath = '';
208 210
 
209 211
                 foreach ($arrFolders as $folder)
210 212
                 {
211
-                    if (!is_dir(TL_ROOT . '/' . $strStartPath . $folder))
213
+                    if (!is_dir($projectDir . '/' . $strStartPath . $folder))
212 214
                     {
213 215
                         Files::getInstance()->mkdir($strStartPath . $folder);
214 216
                     }
... ...
@@ -217,7 +219,7 @@ class SecureDownloadsJob
217 219
 
218 220
                 // Do not overwrite existing files
219 221
                 $copyId = 1;
220
-                while(file_exists(TL_ROOT . "/".$strTargetFolder.'/'.$sanitizedFilename))
222
+                while(file_exists($projectDir . "/".$strTargetFolder.'/'.$sanitizedFilename))
221 223
                 {
222 224
                     $sanitizedFilename = $baseFilename.'_'.$copyId++.'.'.$objFile->extension;
223 225
                 }