1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* * |
4
|
|
|
*@author nicolaas[at] sunnysideup.co.nz |
5
|
|
|
* |
6
|
|
|
**/ |
7
|
|
|
|
8
|
|
|
class Advertisement extends DataObject |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
private static $thumbnail_size = 140; |
|
|
|
|
11
|
|
|
|
12
|
|
|
private static $width = 0; |
|
|
|
|
13
|
|
|
|
14
|
|
|
private static $height = 0; |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* must be a string as booleans dont work well on configs |
18
|
|
|
* @varchar yes / no |
19
|
|
|
*/ |
20
|
|
|
private static $resize_images = "yes"; |
|
|
|
|
21
|
|
|
|
22
|
|
|
public static function recommended_image_size_statement() |
23
|
|
|
{ |
24
|
|
|
$array = array(); |
25
|
|
|
if (Config::inst()->get("Advertisement", "width")) { |
26
|
|
|
$array[] = "width = ".Config::inst()->get("Advertisement", "width")."px"; |
27
|
|
|
} |
28
|
|
|
if (Config::inst()->get("Advertisement", "height")) { |
29
|
|
|
$array[] = "height = ".Config::inst()->get("Advertisement", "height")."px"; |
30
|
|
|
} |
31
|
|
|
$count = count($array); |
32
|
|
|
if ($count == 0) { |
33
|
|
|
return _t("Advertisement.NO_RECOMMENDED_SIZE_HAS_BEEN_SET", "No recommeded image size has been set."); |
34
|
|
|
} else { |
35
|
|
|
return _t("Advertisement.RECOMMENDED_SIZE", "Recommended Size").": ".implode(" "._t("Advertisement.AND", "and")." ", $array)."."; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
private static $db = array( |
|
|
|
|
40
|
|
|
"Title" => "Varchar(255)", |
41
|
|
|
"ExternalLink" => "Varchar(150)", |
42
|
|
|
"Description" => "Text", |
43
|
|
|
"Sort" => "Int" |
44
|
|
|
); |
45
|
|
|
|
46
|
|
|
private static $has_one = array( |
|
|
|
|
47
|
|
|
"AdvertisementImage" => "Image", |
48
|
|
|
"LinkedPage" => "SiteTree", |
49
|
|
|
"AdditionalImage" => "Image" |
50
|
|
|
); |
51
|
|
|
|
52
|
|
|
private static $belongs_many_many = array( |
|
|
|
|
53
|
|
|
"Parents" => "SiteTree", |
54
|
|
|
); |
55
|
|
|
|
56
|
|
|
private static $casting = array( |
|
|
|
|
57
|
|
|
"FullTitle" => "HTMLText", |
58
|
|
|
"Link" => "Varchar", |
59
|
|
|
"GroupID" => "Int" |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
private static $defaults = array( |
|
|
|
|
63
|
|
|
"Sort" => 1000 |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
private static $default_sort = "\"Sort\" ASC, \"Title\" ASC"; |
|
|
|
|
67
|
|
|
|
68
|
|
|
private static $searchable_fields = array( |
|
|
|
|
69
|
|
|
"Title" => "PartialMatchFilter" |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
private static $singular_name = "Advertisement"; |
|
|
|
|
73
|
|
|
|
74
|
|
|
private static $plural_name = "Advertisements"; |
|
|
|
|
75
|
|
|
|
76
|
|
|
private static $summary_fields = array( |
|
|
|
|
77
|
|
|
"FullTitle" => "Image", |
78
|
|
|
"Link" => "Link" |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
public function getLink() |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$link = ''; |
84
|
|
|
if ($this->ExternalLink) { |
|
|
|
|
85
|
|
|
$link = $this->ExternalLink; |
|
|
|
|
86
|
|
|
} elseif ($this->LinkedPageID) { |
|
|
|
|
87
|
|
|
if ($this->LinkedPage()) { |
|
|
|
|
88
|
|
|
$link = $this->LinkedPage()->Link(); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
return $link; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function Link() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
return $this->getLink(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function getFullTitle() |
100
|
|
|
{ |
101
|
|
|
$s = $this->Title; |
|
|
|
|
102
|
|
|
if ($this->AdvertisementImageID) { |
|
|
|
|
103
|
|
|
$image = $this->AdvertisementImage(); |
|
|
|
|
104
|
|
|
if ($image && $image->exists()) { |
105
|
|
|
$thumb = $image->setSize(Config::inst()->get("Advertisement", "thumbnail_size"), Config::inst()->get("Advertisement", "thumbnail_size")); |
106
|
|
|
if ($thumb) { |
107
|
|
|
$s = " <img src=\"".$thumb->Link()."\" title=\"".$thumb->Link()."\"/ style=\"vertical-align: top; display: block; float: left; padding-right: 10px; \"><div style=\"width: 100%;\">".$s."</div><div style=\"clear: left;\"></div>"; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
return DBField::create_field("HTMLText", $s); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public function FullTitle() |
115
|
|
|
{ |
116
|
|
|
return $this->getFullTitle(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getGroupID() |
|
|
|
|
120
|
|
|
{ |
121
|
|
|
if ($this->AdvertisementImageID) { |
|
|
|
|
122
|
|
|
$image = Image::get()->byID($this->AdvertisementImageID); |
|
|
|
|
123
|
|
|
if ($image) { |
124
|
|
|
return $image->ParentID; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function GroupID() |
|
|
|
|
130
|
|
|
{ |
131
|
|
|
return $this->getGroupID(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function getCMSFields() |
135
|
|
|
{ |
136
|
|
|
$fields = parent::getCMSFields(); |
137
|
|
|
$fields->removeFieldFromTab("Root", "Sort"); |
138
|
|
|
$fields->removeFieldFromTab("Root.Main", "AdvertisementImage"); |
139
|
|
|
$fields->removeFieldFromTab("Root.Main", "AdvertisementImageID"); |
140
|
|
|
$fields->removeFieldFromTab("Root.Main", "LinkedPageID"); |
141
|
|
|
$fields->removeFieldFromTab("Root.Main", "ExternalLink"); |
142
|
|
|
$fields->removeFieldFromTab("Root.Parents", "Parents"); |
143
|
|
|
$fields->removeFieldFromTab("Root", "Parents"); |
144
|
|
|
$fields->addFieldToTab("Root.Main", ReadonlyField::create("Link", 'Calculated Link')); |
145
|
|
|
$fields->addFieldToTab("Root.Main", $mainImageField = new UploadField($name = "AdvertisementImage", $title = $this->i18n_singular_name())); |
146
|
|
|
$mainImageField->setRightTitle(self::recommended_image_size_statement()); |
147
|
|
|
$fields->addFieldToTab( |
148
|
|
|
"Root.Main", |
149
|
|
|
$additionalImageField = UploadField::create( |
150
|
|
|
$name = "AdditionalImage", |
151
|
|
|
$title = $this->i18n_singular_name()." "._t("Advertisement.ADDITIONAL_IMAGE", "additional image") |
152
|
|
|
) |
153
|
|
|
); |
154
|
|
|
$additionalImageField->setRightTitle(self::recommended_image_size_statement()); |
155
|
|
|
if ($this->ID) { |
156
|
|
|
$treeField = TreeMultiselectField::create( |
157
|
|
|
"Parents", |
158
|
|
|
_t("Advertisement.GETCMSFIELDSPARENTID", "only show on ... (leave blank to show on all " |
159
|
|
|
.$this->i18n_singular_name() |
160
|
|
|
." pages)"), |
161
|
|
|
"SiteTree" |
162
|
|
|
); |
163
|
|
|
/*$callback = $this->callbackFilterFunctionForMultiSelect(); |
|
|
|
|
164
|
|
|
if($callback) { |
165
|
|
|
$treeField->setFilterFunction ($callback); |
166
|
|
|
}*/ |
167
|
|
|
$fields->addFieldToTab("Root.ShownOn", $treeField); |
168
|
|
|
} |
169
|
|
|
$fields->addFieldToTab( |
170
|
|
|
"Root.OptionalLink", |
171
|
|
|
$externalLinkField = new TextField($name = "ExternalLink", $title = _t("Advertisement.GETCMSFIELDSEXTERNALLINK", "link to external site")) |
172
|
|
|
); |
173
|
|
|
$externalLinkField->setRightTitle(_t("Advertisement.GETCMSFIELDSEXTERNALLINK_EXPLANATION", "(e.g. http://www.wikipedia.org) - this will override an internal link")); |
174
|
|
|
$fields->addFieldToTab("Root.OptionalLink", TreeDropdownField::create($name = "LinkedPageID", $title = _t("Advertisement.GETCMSFIELDSEXTERNALLINKID", "link to a page on this website"), $sourceObject = "SiteTree")); |
175
|
|
|
if (class_exists("DataObjectSorterController")) { |
|
|
|
|
176
|
|
|
//sorted on parent page... |
177
|
|
|
} else { |
178
|
|
|
$fields->addFieldToTab( |
179
|
|
|
"Root.Position", |
180
|
|
|
$sortField = NumericField::create( |
181
|
|
|
"Sort", |
182
|
|
|
_t("Advertisement.SORT", "Sort index number") |
183
|
|
|
) |
184
|
|
|
); |
185
|
|
|
$sortField->setRightTitle(_t("Advertisement.SORT_EXPLANATION", "the lower the number, the earlier it shows")); |
186
|
|
|
} |
187
|
|
|
$fields->removeFieldFromTab("Root.Main", "AlternativeSortNumber"); |
188
|
|
|
return $fields; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function onBeforeWrite() |
192
|
|
|
{ |
193
|
|
|
parent::onBeforeWrite(); |
194
|
|
|
if (! $this->Sort) { |
|
|
|
|
195
|
|
|
$defaults = $this->Config()->get("defaults"); |
196
|
|
|
$this->Sort = isset($defaults["Sort"]) ? $defaults["Sort"] : 0; |
|
|
|
|
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
|
201
|
|
|
public function requireDefaultRecords() |
202
|
|
|
{ |
203
|
|
|
parent::requireDefaultRecords(); |
204
|
|
|
DB::query("UPDATE \"Advertisement\" SET \"Title\" = \"ID\" WHERE \"Title\" = '' OR \"Title\" IS NULL;"); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
|
208
|
|
|
protected function callbackFilterFunctionForMultiSelect() |
209
|
|
|
{ |
210
|
|
|
$inc = Config::inst()->get("AdvertisementDecorator", "page_classes_with_advertisements"); |
211
|
|
|
$exc = Config::inst()->get("AdvertisementDecorator", "page_classes_without_advertisements"); |
212
|
|
|
if (is_array($inc) && count($inc)) { |
213
|
|
|
$string = 'return in_array($obj->class, array(\''.implode("','", $inc).'\'));'; |
214
|
|
|
} elseif (is_array($exc) && count($exc)) { |
215
|
|
|
$string = 'return !in_array($obj->class, array(\''.implode("','", $exc).'\'));'; |
216
|
|
|
} |
217
|
|
|
if (isset($string)) { |
218
|
|
|
return create_function('$obj', $string); |
|
|
|
|
219
|
|
|
} else { |
220
|
|
|
return false; |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
public function Image() |
|
|
|
|
225
|
|
|
{ |
226
|
|
|
//will be depreciated in the future. |
227
|
|
|
return $this->ResizedAdvertisementImage(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
|
231
|
|
|
//back-up function... |
232
|
|
|
public function ResizedAdvertisementImage() |
|
|
|
|
233
|
|
|
{ |
234
|
|
|
$resizedImage = null; |
235
|
|
|
$imageID = intval($this->AdvertisementImageID+ 0); |
|
|
|
|
236
|
|
|
if ($imageID) { |
237
|
|
|
if ($this->Config()->get("resize_images") == 'yes') { |
238
|
|
|
$imageObject = Image::get()->byID($imageID); |
239
|
|
|
$resizedImage = $imageObject; |
240
|
|
|
if ($imageObject) { |
241
|
|
|
if ($imageObject->ID) { |
242
|
|
|
$imageObject->Title = Convert::raw2att($this->Title); |
|
|
|
|
243
|
|
|
$w = Config::inst()->get("Advertisement", "width"); |
244
|
|
|
$h = Config::inst()->get("Advertisement", "height"); |
245
|
|
|
if ($h && $w) { |
246
|
|
|
$resizedImage = $imageObject->SetSize($w, $h); |
247
|
|
|
} elseif ($h) { |
248
|
|
|
$resizedImage = $imageObject->SetHeight($h); |
249
|
|
|
} elseif ($w) { |
250
|
|
|
$resizedImage = $imageObject->SetWidth($w); |
251
|
|
|
} else { |
252
|
|
|
$resizedImage = $imageObject; |
253
|
|
|
} |
254
|
|
|
} else { |
|
|
|
|
255
|
|
|
//debug::show("no image"); |
|
|
|
|
256
|
|
|
} |
257
|
|
|
} else { |
|
|
|
|
258
|
|
|
//debug::show("could not find image"); |
|
|
|
|
259
|
|
|
} |
260
|
|
|
} else { |
261
|
|
|
return $this->AdvertisementImage(); |
|
|
|
|
262
|
|
|
} |
263
|
|
|
} else { |
|
|
|
|
264
|
|
|
//debug::show("no imageID ($imageID) "); |
|
|
|
|
265
|
|
|
} |
266
|
|
|
return $resizedImage; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function ThinyThumb() |
270
|
|
|
{ |
271
|
|
|
return "This function has not been implemented"; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
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.