44
55namespace PhpList \Core \Domain \Messaging \Service ;
66
7- use Doctrine \ORM \EntityManagerInterface ;
87use PhpList \Core \Domain \Common \HtmlToText ;
98use PhpList \Core \Domain \Common \RemotePageFetcher ;
109use PhpList \Core \Domain \Common \TextParser ;
1514use PhpList \Core \Domain \Identity \Repository \AdministratorRepository ;
1615use PhpList \Core \Domain \Messaging \Model \Dto \MessagePrecacheDto ;
1716use PhpList \Core \Domain \Messaging \Model \Message ;
18- use PhpList \Core \Domain \Messaging \Model \TemplateImage ;
19- use PhpList \Core \Domain \Messaging \Repository \TemplateImageRepository ;
2017use PhpList \Core \Domain \Messaging \Repository \TemplateRepository ;
18+ use PhpList \Core \Domain \Messaging \Service \Manager \TemplateImageManager ;
2119use Psr \SimpleCache \CacheInterface ;
2220
2321class MessagePrecacheService
2422{
2523 public function __construct (
2624 private readonly CacheInterface $ cache ,
27- private readonly MessageDataLoader $ messageDataLoader ,
2825 private readonly ConfigProvider $ configProvider ,
2926 private readonly HtmlToText $ htmlToText ,
3027 private readonly TextParser $ textParser ,
3128 private readonly TemplateRepository $ templateRepository ,
32- private readonly TemplateImageRepository $ templateImageRepository ,
3329 private readonly RemotePageFetcher $ remotePageFetcher ,
3430 private readonly EventLogManager $ eventLogManager ,
3531 private readonly AdminAttributeDefinitionRepository $ adminAttributeDefRepository ,
3632 private readonly AdministratorRepository $ adminRepository ,
37- private readonly EntityManagerInterface $ entityManager ,
33+ private readonly TemplateImageManager $ templateImageManager ,
3834 private readonly bool $ useManualTextPart ,
3935 private readonly string $ uploadImageDir ,
4036 private readonly string $ publicSchema ,
@@ -45,7 +41,7 @@ public function __construct(
4541 * Retrieve the base (unpersonalized) message content for a campaign from cache,
4642 * or cache it on first access. Handle [URL:] token fetch and basic placeholder replacements.
4743 */
48- public function getOrCacheBaseMessageContent (Message $ campaign , ?bool $ forwardContent = false ): ? MessagePrecacheDto
44+ public function precacheMessage (Message $ campaign , $ loadedMessageData , ?bool $ forwardContent = false ): bool
4945 {
5046 $ cacheKey = sprintf ('messaging.message.base.%d ' , $ campaign ->getId ());
5147
@@ -55,7 +51,6 @@ public function getOrCacheBaseMessageContent(Message $campaign, ?bool $forwardCo
5551 }
5652 $ domain = $ this ->configProvider ->getValue (ConfigOption::Domain);
5753
58- $ loadedMessageData = ($ this ->messageDataLoader )($ campaign );
5954 $ messagePrecacheDto = new MessagePrecacheDto ();
6055
6156 // parse the reply-to field into its components - email and name
@@ -136,7 +131,7 @@ public function getOrCacheBaseMessageContent(Message $campaign, ?bool $forwardCo
136131 entry: 'Error fetching URL: ' .$ loadedMessageData ['sendurl ' ].' cannot proceed ' ,
137132 );
138133
139- return null ;
134+ return false ;
140135 }
141136 }
142137 }
@@ -192,7 +187,7 @@ public function getOrCacheBaseMessageContent(Message $campaign, ?bool $forwardCo
192187
193188 $ this ->cache ->set ($ cacheKey , $ messagePrecacheDto );
194189
195- return $ messagePrecacheDto ;
190+ return true ;
196191 }
197192
198193 private function buildBasicReplacements (Message $ campaign , string $ subject ): array
@@ -214,6 +209,7 @@ private function parseFromField(string $fromField): array
214209 }
215210 $ name = trim (str_replace ([$ email , '" ' ], ['' , '' ], $ fromField ));
216211 $ name = trim (str_replace (['< ' , '> ' ], '' , $ name ));
212+
217213 return [$ name , $ email ];
218214 }
219215
@@ -222,6 +218,7 @@ private function applyReplacements(?string $input, array $replacements): ?string
222218 if ($ input === null ) {
223219 return null ;
224220 }
221+
225222 return str_ireplace (array_keys ($ replacements ), array_values ($ replacements ), $ input );
226223 }
227224
@@ -232,74 +229,10 @@ private function parseLogoPlaceholders($content)
232229 foreach ($ logoInstances [0 ] as $ index => $ logoInstance ) {
233230 $ size = sprintf ('%d ' , $ logoInstances [1 ][$ index ]);
234231 $ logoSize = !empty ($ size ) ? $ size : '500 ' ;
235- $ this ->createCachedLogoImage ((int )$ logoSize );
232+ $ this ->templateImageManager -> createCachedLogoImage ((int )$ logoSize );
236233 $ content = str_replace ($ logoInstance , 'ORGANISATIONLOGO ' .$ logoSize .'.png ' , $ content );
237234 }
238235
239236 return $ content ;
240237 }
241-
242- private function createCachedLogoImage (int $ size ): void
243- {
244- $ logoImageId = $ this ->configProvider ->getValue (ConfigOption::OrganisationLogo);
245- if (empty ($ logoImageId )) {
246- return ;
247- }
248-
249- $ orgLogoImage = $ this ->templateImageRepository ->findByFilename ("ORGANISATIONLOGO $ size.png " );
250- if (!empty ($ orgLogoImage ->getData ())) {
251- return ;
252- }
253-
254- $ logoImage = $ this ->templateImageRepository ->findById ((int ) $ logoImageId );
255- $ imageContent = base64_decode ($ logoImage ->getData ());
256- if (empty ($ imageContent )) {
257- //# fall back to a single pixel, so that there are no broken images
258- $ imageContent = base64_decode ('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAABGdBTUEAALGPC/xhBQAAAAZQTFRF////AAAAVcLTfgAAAAF0Uk5TAEDm2GYAAAABYktHRACIBR1IAAAACXBIWXMAAAsSAAALEgHS3X78AAAAB3RJTUUH0gQCEx05cqKA8gAAAApJREFUeJxjYAAAAAIAAUivpHEAAAAASUVORK5CYII= ' );
259- }
260-
261- $ imgSize = getimagesizefromstring ($ imageContent );
262- $ sizeW = $ imgSize [0 ];
263- $ sizeH = $ imgSize [1 ];
264- if ($ sizeH > $ sizeW ) {
265- $ sizeFactor = (float ) ($ size / $ sizeH );
266- } else {
267- $ sizeFactor = (float ) ($ size / $ sizeW );
268- }
269- $ newWidth = (int ) ($ sizeW * $ sizeFactor );
270- $ newHeight = (int ) ($ sizeH * $ sizeFactor );
271-
272- if ($ sizeFactor < 1 ) {
273- $ original = imagecreatefromstring ($ imageContent );
274- //# creates a black image (why would you want that....)
275- $ resized = imagecreatetruecolor ($ newWidth , $ newHeight );
276- imagesavealpha ($ resized , true );
277- //# white. All the methods to make it transparent didn't work for me @@TODO really make transparent
278- $ transparent = imagecolorallocatealpha ($ resized , 255 , 255 , 255 , 127 );
279- imagefill ($ resized , 0 , 0 , $ transparent );
280-
281- if (imagecopyresized ($ resized , $ original , 0 , 0 , 0 , 0 , $ newWidth , $ newHeight , $ sizeW , $ sizeH )) {
282- $ this ->entityManager ->remove ($ orgLogoImage );
283-
284- //# rather convoluted way to get the image contents
285- $ buffer = ob_get_contents ();
286- ob_end_clean ();
287- ob_start ();
288- imagepng ($ resized );
289- $ imageContent = ob_get_contents ();
290- ob_end_clean ();
291- echo $ buffer ;
292- }
293- }
294- // else copy original
295- $ templateImage = (new TemplateImage ())
296- ->setFilename ("ORGANISATIONLOGO $ size.png " )
297- ->setMimetype ($ imgSize ['mime ' ])
298- ->setData (base64_encode ($ imageContent ))
299- ->setWidth ($ newWidth )
300- ->setHeight ($ newHeight );
301-
302- $ this ->entityManager ->persist ($ templateImage );
303-
304- }
305238}
0 commit comments