1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
*@author nicolaas [at] sunnysideup.co.nz |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
**/ |
8
|
|
|
|
9
|
|
|
class CampaignMonitorCampaignStyle extends DataObject |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
private static $db = array( |
|
|
|
|
12
|
|
|
"Title" => "Varchar(100)", |
13
|
|
|
"TemplateName" => "Varchar(200)", |
14
|
|
|
"CSSFiles" => "Text" |
15
|
|
|
); |
16
|
|
|
|
17
|
|
|
private static $indexes = array( |
|
|
|
|
18
|
|
|
"Title" => true |
19
|
|
|
); |
20
|
|
|
|
21
|
|
|
private static $has_many = array( |
|
|
|
|
22
|
|
|
"CampaignMonitorCampaigns" => "CampaignMonitorCampaign" |
23
|
|
|
); |
24
|
|
|
|
25
|
|
|
private static $searchable_fields = array( |
|
|
|
|
26
|
|
|
"Title" => "PartialMatchFilter" |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
private static $summary_fields = array( |
|
|
|
|
30
|
|
|
"Title" => "Title" |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
private static $singular_name = "Campaign Template"; |
|
|
|
|
34
|
|
|
|
35
|
|
|
private static $plural_name = "Campaign Templates"; |
|
|
|
|
36
|
|
|
|
37
|
|
|
private static $default_template = "CampaignMonitorCampaign"; |
|
|
|
|
38
|
|
|
|
39
|
|
|
public function getCMSFields() |
40
|
|
|
{ |
41
|
|
|
$fields = parent::getCMSFields(); |
42
|
|
|
$fields->addFieldToTab("Root.Debug", TextField::create("TemplateName")); |
43
|
|
|
$fields->addFieldToTab("Root.Debug", ReadonlyField::create("FileLocation")); |
44
|
|
|
$fields->addFieldToTab("Root.Debug", ReadonlyField::create("CSSFiles")); |
45
|
|
|
$fields->addFieldToTab("Root.Debug", ReadonlyField::create("CampaignMonitorCampaigns", "Used in ", implode(",", $this->CampaignMonitorCampaigns()->map()->toArray()))); |
|
|
|
|
46
|
|
|
$fields->removeFieldFromTab("Root", "CampaignMonitorCampaigns"); |
47
|
|
|
return $fields; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function canCreate($member = null) |
51
|
|
|
{ |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
View Code Duplication |
public function getFoldersToSearch() |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
$array = array( |
61
|
|
|
Director::baseFolder() ."/".SSViewer::get_theme_folder()."_campaignmonitor/templates/Email/", |
62
|
|
|
Director::baseFolder()."/campaignmonitor/templates/Email/" |
63
|
|
|
); |
64
|
|
|
foreach ($array as $key => $folder) { |
65
|
|
|
if (!file_exists($folder)) { |
66
|
|
|
unset($array[$key]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
return $array; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array |
74
|
|
|
*/ |
75
|
|
View Code Duplication |
public function getCSSFoldersToSearch() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
$array = array( |
78
|
|
|
Director::baseFolder() ."/".SSViewer::get_theme_folder()."_campaignmonitor/css/", |
79
|
|
|
Director::baseFolder()."/campaignmonitor/css/" |
80
|
|
|
|
81
|
|
|
); |
82
|
|
|
foreach ($array as $key => $folder) { |
83
|
|
|
if (!file_exists($folder)) { |
84
|
|
|
unset($array[$key]); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
return $array; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* |
92
|
|
|
* @return string | null |
|
|
|
|
93
|
|
|
*/ |
94
|
|
|
public function getFileLocation() |
95
|
|
|
{ |
96
|
|
|
if (!$this->TemplateName) { |
|
|
|
|
97
|
|
|
$this->TemplateName = "CampaignMonitorCampaign"; |
|
|
|
|
98
|
|
|
} |
99
|
|
|
foreach ($this->getFoldersToSearch() as $folder) { |
100
|
|
|
$fileLocation = $folder.$this->TemplateName.".ss"; |
|
|
|
|
101
|
|
|
if (file_exists($fileLocation)) { |
102
|
|
|
return $fileLocation; |
103
|
|
|
} else { |
|
|
|
|
104
|
|
|
//just try the next one ... |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
user_error("can not find template, last one tried: $fileLocation"); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function getCSSFiles() |
111
|
|
|
{ |
112
|
|
|
return implode(", ", $this->getCSSFilesAsArray()); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getCSSFilesAsArray() |
116
|
|
|
{ |
117
|
|
|
$dom = new DOMDocument(); |
118
|
|
|
$cssFiles = array(); |
119
|
|
|
$fileLocation = $this->getFileLocation(); |
120
|
|
|
if ($fileLocation) { |
|
|
|
|
121
|
|
|
@$dom->loadHTMLFile($fileLocation); |
|
|
|
|
122
|
|
|
$linkTags = $dom->getElementsByTagName('link'); |
123
|
|
|
foreach ($linkTags as $linkTag) { |
124
|
|
|
if (strtolower($linkTag->getAttribute("rel")) == "stylesheet") { |
125
|
|
|
$file = Director::baseFolder()."/".$linkTag->getAttribute("href"); |
126
|
|
|
if (file_exists($file)) { |
127
|
|
|
$cssFiles[$file] = $file; |
128
|
|
|
} else { |
129
|
|
|
user_error("can find css file $file"); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
// if $link_tag rel == stylesheet |
133
|
|
|
// get href value and load CSS |
134
|
|
|
} |
135
|
|
|
} else { |
136
|
|
|
user_error("Can not find template file"); |
137
|
|
|
} |
138
|
|
|
if (count($cssFiles) == 0) { |
139
|
|
|
foreach ($this->getCSSFoldersToSearch() as $folder) { |
140
|
|
|
$file = $folder."CampaignMonitorCampaign.css"; |
141
|
|
|
if (file_exists($file)) { |
142
|
|
|
$cssFiles[$file] = $file; |
143
|
|
|
break; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
return $cssFiles; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function requireDefaultRecords() |
151
|
|
|
{ |
152
|
|
|
parent::requireDefaultRecords(); |
153
|
|
|
$templates = array(); |
154
|
|
|
foreach ($this->getFoldersToSearch() as $folder) { |
155
|
|
|
$finder = new SS_FileFinder(); |
156
|
|
|
$finder->setOption('name_regex', '/^.*\.ss$/'); |
157
|
|
|
$found = $finder->find($folder); |
158
|
|
|
foreach ($found as $key => $value) { |
159
|
|
|
$template = pathinfo($value); |
160
|
|
|
$templates[$template['filename']] = $template['filename']; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
foreach ($templates as $template) { |
164
|
|
|
$filter = array("TemplateName" => $template); |
165
|
|
|
$obj = CampaignMonitorCampaignStyle::get()->filter($filter)->first(); |
166
|
|
|
if (!$obj) { |
167
|
|
|
$obj = CampaignMonitorCampaignStyle::create($filter+array("Title" => $template)); |
168
|
|
|
$obj->write(); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
$excludes = $obj = CampaignMonitorCampaignStyle::get()->exclude(array("TemplateName" => $templates)); |
|
|
|
|
172
|
|
|
foreach ($excludes as $exclude) { |
173
|
|
|
$exclude->delete(); |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
public function onBeforeWrite() |
178
|
|
|
{ |
179
|
|
|
parent::onBeforeWrite(); |
180
|
|
|
if ($this->TemplateName == "CampaignMonitorCampaign") { |
|
|
|
|
181
|
|
|
$this->Title = "Default Template"; |
|
|
|
|
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.