1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bummzack\SilverStripeEmogrify; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Assets\File; |
6
|
|
|
use SilverStripe\Core\Config\Configurable; |
7
|
|
|
use SilverStripe\Core\Path; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* A plugin that can be configured by the SilverStripe configuration API. |
11
|
|
|
* Wraps the emogrifier-plugin. |
12
|
|
|
* |
13
|
|
|
* @package Bummzack\SilverStripeEmogrify |
14
|
|
|
*/ |
15
|
|
|
class EmogrifierPlugin extends \Bummzack\SwiftMailer\EmogrifyPlugin\EmogrifierPlugin |
16
|
|
|
{ |
17
|
|
|
use Configurable; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* The default CSS file that should be used for styling Emails. |
21
|
|
|
* Can be set via config YAML. |
22
|
|
|
* |
23
|
|
|
* @config |
24
|
|
|
* @var string|null |
25
|
|
|
*/ |
26
|
|
|
private static $css_file = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* EmogrifierPlugin constructor. |
30
|
|
|
*/ |
31
|
|
|
public function __construct() |
32
|
|
|
{ |
33
|
|
|
if ($file = $this->config()->css_file) { |
34
|
|
|
$this->loadCssFromFile($file); |
35
|
|
|
} |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Load CSS styles from file and apply them to the current emogrifier instance. |
40
|
|
|
* _Attention:_ loaded styles will be lost if the emogrifier instance is set to a different one! |
41
|
|
|
* @param string $file the path to the CSS file to load, |
42
|
|
|
* if the file path isn't absolute, it's assumed to be relative to `BASE_PATH` |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
|
|
public function loadCssFromFile($file) |
46
|
|
|
{ |
47
|
|
|
if (file_exists($file)) { |
48
|
|
|
$path = $file; |
49
|
|
|
} else { |
50
|
|
|
$path = Path::join(BASE_PATH, $file); |
51
|
|
|
if (!file_exists($path)) { |
52
|
|
|
throw new \InvalidArgumentException('File at "' . $path . '" does not exist'); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (strtolower(File::get_file_extension($path)) !== 'css') { |
57
|
|
|
throw new \InvalidArgumentException('File "' . $path . '" does not have .css extension.'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$this->setCss(file_get_contents($path)); |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|