1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
*@author nicolaas[at]sunnysideup.co.nz |
5
|
|
|
*@description: more details per template. Records are auto-added |
6
|
|
|
*@link: http://localhost/www.mysite.co.nz/admin/templates/TemplateoverviewDescription/13/edit |
7
|
|
|
**/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class TemplateoverviewDescription extends DataObject |
|
|
|
|
11
|
|
|
{ |
12
|
|
|
private static $db = array( |
|
|
|
|
13
|
|
|
"Title" => "Varchar", |
14
|
|
|
"Description" => "Text", |
15
|
|
|
"ToDoListHyperLink" => "Varchar(255)", |
16
|
|
|
"ClassNameLink" => "Varchar(120)" |
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
private static $has_one = array( |
|
|
|
|
20
|
|
|
"Image1" => "Image", |
21
|
|
|
"Image2" => "Image", |
22
|
|
|
"Image3" => "Image", |
23
|
|
|
"Image4" => "Image", |
24
|
|
|
"Image5" => "Image", |
25
|
|
|
"Image6" => "Image", |
26
|
|
|
"Image7" => "Image" |
27
|
|
|
); |
28
|
|
|
|
29
|
|
|
private static $searchable_fields = array( |
|
|
|
|
30
|
|
|
"ClassNameLink" => "PartialMatchFilter" |
31
|
|
|
); |
32
|
|
|
|
33
|
|
|
private static $summary_fields = array( |
|
|
|
|
34
|
|
|
"ClassNameLink", |
35
|
|
|
"ToDoListHyperLink" |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
private static $field_labels = array( |
|
|
|
|
39
|
|
|
"ClassNameLink" => "Page Type Name", |
40
|
|
|
"ToDoListHyperLink" => "Link to To Do" |
41
|
|
|
); |
42
|
|
|
|
43
|
|
|
private static $singular_name = 'Template Description'; |
|
|
|
|
44
|
|
|
|
45
|
|
|
private static $plural_name = 'Template Descriptions'; |
|
|
|
|
46
|
|
|
|
47
|
|
|
private static $default_sort = 'ClassNameLink ASC'; |
|
|
|
|
48
|
|
|
|
49
|
|
|
private static $indexes = array( |
|
|
|
|
50
|
|
|
"ClassNameLink" => true |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
private static $casting = array( |
|
|
|
|
54
|
|
|
"Title" => "Varchar" |
55
|
|
|
); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Location where we keep the template overview designs. |
59
|
|
|
* @var String |
60
|
|
|
*/ |
61
|
|
|
private static $image_folder_name = "templateoverview_advanced/designz"; |
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Location where we keep the template overview designs. |
65
|
|
|
* @var String |
66
|
|
|
*/ |
67
|
|
|
private static $image_source_folder = ""; |
|
|
|
|
68
|
|
|
|
69
|
|
|
public function canCreate($member = null) |
70
|
|
|
{ |
71
|
|
|
return false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function canDelete($member = null) |
75
|
|
|
{ |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function ClassNameLinkFancy() |
|
|
|
|
80
|
|
|
{ |
81
|
|
|
return implode(" ", preg_split('/(?<=\\w)(?=[A-Z])/', $this->ClassNameLink)); |
|
|
|
|
82
|
|
|
return preg_replace("/(?<=[^A-Z])([A-Z])/", "$1", $this->ClassNameLink); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function Title() |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
return $this->getTitle(); |
88
|
|
|
} |
89
|
|
|
public function getTitle() |
|
|
|
|
90
|
|
|
{ |
91
|
|
|
return $this->ClassNameLinkFancy(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function ModelAdminLink() |
95
|
|
|
{ |
96
|
|
|
return TemplateoverviewDescriptionModelAdmin::get_full_url_segment().$this->ClassName."/EditForm/field/TemplateoverviewDescription/item/".$this->ID."/edit/"; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getCMSFields() |
100
|
|
|
{ |
101
|
|
|
$fields = parent::getCMSFields(); |
102
|
|
|
$fields->removeByName("ClassNameLink"); |
103
|
|
|
$fields->removeByName("Image1"); |
104
|
|
|
$fields->removeByName("Image2"); |
105
|
|
|
$fields->removeByName("Image3"); |
106
|
|
|
$fields->removeByName("Image4"); |
107
|
|
|
$fields->removeByName("Image5"); |
108
|
|
|
$fields->addFieldToTab("Root.Design", new UploadField("Image1", "Design One")); |
109
|
|
|
$fields->addFieldToTab("Root.Design", new UploadField("Image2", "Design Two")); |
110
|
|
|
$fields->addFieldToTab("Root.Design", new UploadField("Image6", "Design Three")); |
111
|
|
|
$fields->addFieldToTab("Root.Instructions", new UploadField("Image3", "Instructions One")); |
112
|
|
|
$fields->addFieldToTab("Root.Instructions", new UploadField("Image4", "Instructions Two")); |
113
|
|
|
$fields->addFieldToTab("Root.Instructions", new UploadField("Image5", "Instructions Three")); |
114
|
|
|
$fields->addFieldToTab("Root.Instructions", new UploadField("Image7", "Instructions Four")); |
115
|
|
|
$fields->addFieldToTab("Root.Main", new HeaderField("ClassNameLinkInfo", "Details for: ".$this->ClassNameLink), "Description"); |
|
|
|
|
116
|
|
|
$fields->addFieldToTab("Root.Main", new LiteralField("BackLink", '<p><a href="'.$page->Link().'#sectionFor-'.$this->ClassNameLink.'">go back to template overview page</a> - dont forget to SAVE FIRST.</p>')); |
|
|
|
|
117
|
|
|
return $fields; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function requireDefaultRecords() |
121
|
|
|
{ |
122
|
|
|
parent::requireDefaultRecords(); |
123
|
|
|
$data = ClassInfo::subclassesFor("SiteTree"); |
124
|
|
|
$fileList = null; |
125
|
|
|
if ($this->Config()->get("image_source_folder")) { |
126
|
|
|
$fileList = CMSHelp::get_list_of_files($this->Config()->get("image_source_folder")); |
127
|
|
|
if (!is_array($fileList)) { |
128
|
|
|
$fileList = null; |
129
|
|
|
} elseif (!count($fileList)) { |
130
|
|
|
$fileList = null; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
if ($fileList) { |
134
|
|
|
$destinationDir = Director::baseFolder()."/assets/".$this->Config()->get("image_folder_name")."/"; |
135
|
|
|
$destinationFolder = Folder::find_or_make($this->Config()->get("image_folder_name")); |
136
|
|
|
} |
137
|
|
|
if ($data) { |
138
|
|
|
foreach ($data as $className) { |
139
|
|
|
$object = TemplateoverviewDescription::get() |
140
|
|
|
->filter(array("ClassNameLink" => $className))->First(); |
141
|
|
|
if (!$object) { |
142
|
|
|
$object = new TemplateoverviewDescription(); |
143
|
|
|
$object->ClassNameLink = $className; |
|
|
|
|
144
|
|
|
$object->write(); |
145
|
|
|
DB::alteration_message("adding template description for $className", "created"); |
146
|
|
|
} else { |
147
|
|
|
$otherObjects = TemplateoverviewDescription::get() |
148
|
|
|
->filter(array("ClassNameLink" => $className))->exclude(array("ID" => $object->ID)); |
149
|
|
|
foreach ($otherObjects as $otherObject) { |
150
|
|
|
DB::alteration_message("Deleting superfluous TemplateoverviewDescription with Class Name Link: $className", "deleted"); |
151
|
|
|
$otherObject->delete(); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
if ($fileList) { |
155
|
|
|
$i = 0; |
156
|
|
|
foreach ($fileList as $fileArray) { |
157
|
|
|
$explodeByDot = explode(".", $fileArray["FileName"]); |
158
|
|
|
if (is_array($explodeByDot) && count($explodeByDot)) { |
159
|
|
|
$base = $explodeByDot[0]; |
160
|
|
|
$explodeByUnderscore = explode("_", $base); |
161
|
|
|
if (is_array($explodeByUnderscore) && count($explodeByUnderscore)) { |
162
|
|
|
$base = $explodeByUnderscore[0]; |
163
|
|
|
$classNameOptionArray = array($className); |
164
|
|
|
for ($j = 0; $j < 10; $j++) { |
165
|
|
|
$classNameOptionArray[] = $className.$j; |
166
|
|
|
} |
167
|
|
|
foreach ($classNameOptionArray as $potentialBase) { |
168
|
|
|
if ($base == $potentialBase) { |
169
|
|
|
$i++; |
170
|
|
|
$filename = "".$this->Config()->get("image_folder_name")."/".$fileArray["FileName"]; |
171
|
|
|
if (!file_exists($destinationDir.$fileArray["FileName"])) { |
172
|
|
|
copy($fileArray["FullLocation"], $destinationDir.$fileArray["FileName"]); |
|
|
|
|
173
|
|
|
} |
174
|
|
|
$image = $Image::get() |
|
|
|
|
175
|
|
|
->filter(array("ParentID" => $destinationFolder->ID, "Name" => $fileArray["FileName"]))->First(); |
|
|
|
|
176
|
|
|
if (!$image) { |
177
|
|
|
$image = new Image(); |
178
|
|
|
$image->ParentID = $destinationFolder->ID; |
179
|
|
|
$image->Filename = $filename; |
180
|
|
|
$image->Name = $fileArray["FileName"]; |
181
|
|
|
$image->Title = $fileArray["Title"]; |
182
|
|
|
$image->write(); |
183
|
|
|
} |
184
|
|
|
$fieldName = "Image$i"."ID"; |
185
|
|
|
if ($object->$fieldName != $image->ID) { |
186
|
|
|
$object->$fieldName = $image->ID; |
187
|
|
|
$object->write(); |
188
|
|
|
DB::alteration_message("adding image to $className: ".$image->Title." (".$image->Filename.") using $fieldName field.", "created"); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} else { |
196
|
|
|
DB::alteration_message("no design images found for $className", "deleted"); |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
$helpDirectory = Director::baseFolder()."/".Config::inst()->get("CMSHelp", "help_file_directory_name")."/"; |
201
|
|
|
if (!file_exists($helpDirectory)) { |
202
|
|
|
mkdir($helpDirectory); |
203
|
|
|
} |
204
|
|
|
$this->createManifestExcludeFile($helpDirectory); |
205
|
|
|
|
206
|
|
|
$devDirectory = Director::baseFolder()."/".Config::inst()->get("CMSHelp", "dev_file_directory_name")."/"; |
207
|
|
|
if (!file_exists($devDirectory)) { |
208
|
|
|
mkdir($devDirectory); |
209
|
|
|
} |
210
|
|
|
$this->createManifestExcludeFile($devDirectory); |
211
|
|
|
$this->createHTACCESSDenyAll($devDirectory); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
View Code Duplication |
private function createManifestExcludeFile($dir) |
|
|
|
|
215
|
|
|
{ |
216
|
|
|
$myFile = $dir.'_manifest_exclude'; |
217
|
|
|
if (!file_exists($myFile)) { |
218
|
|
|
$handle = fopen($myFile, 'w') or user_error('Cannot open file: '.$myFile); |
|
|
|
|
219
|
|
|
$data = ''; |
220
|
|
|
fwrite($handle, $data); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
View Code Duplication |
private function createHTACCESSDenyAll($dir) |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
$myFile = $dir.'.htaccess'; |
227
|
|
|
if (!file_exists($myFile)) { |
228
|
|
|
$handle = fopen($myFile, 'w') or user_error('Cannot open file: '.$myFile); |
|
|
|
|
229
|
|
|
$data = ' |
230
|
|
|
Order Deny,Allow |
231
|
|
|
Deny from all |
232
|
|
|
'; |
233
|
|
|
fwrite($handle, $data); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
public function validate() |
238
|
|
|
{ |
239
|
|
|
if ($this->ID) { |
240
|
|
|
if ( |
241
|
|
|
TemplateoverviewDescription::get() |
242
|
|
|
->filter(array("ClassNameLink" => $this->ClassNameLink)) |
|
|
|
|
243
|
|
|
->exclude(array("ID" => $this->ID)) |
244
|
|
|
) { |
245
|
|
|
return new ValidationResult(false, _t("TemplateoverviewDescription.ALREADYEXISTS", "This template already exists")); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
return new ValidationResult(); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function onBeforeWrite() |
252
|
|
|
{ |
253
|
|
|
parent::onBeforeWrite(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function forTemplate() |
|
|
|
|
257
|
|
|
{ |
258
|
|
|
return $this->ClassNameLink; |
|
|
|
|
259
|
|
|
} |
260
|
|
|
} |
261
|
|
|
|
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.