-
-
Save hailwood/66a1c07bacd9ede6e50624be574f26b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class GeoContent extends DataObject | |
{ | |
protected static $db = [ | |
'Code' => 'Varchar', | |
'Content' => 'HTMLText' | |
]; | |
protected static $has_one = [ | |
'Page' => 'Page' | |
]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Page extends SiteTree | |
{ | |
private static $db = [ | |
'LeadText' => 'Text', | |
]; | |
private static $has_one = []; | |
private static $has_many = [ | |
'GeoContentBlocks' => 'GeoContent' | |
]; | |
public function getCMSFields() | |
{ | |
$fields = parent::getCMSFields(); | |
$fields->insertAfter(new Tab('GeoContent'), 'Main'); | |
$fields->dataFieldByName('Content')->setTitle('Default content - United States'); | |
$fields->insertAfter($fields->dataFieldByName('SecondaryContent'), 'Content'); | |
foreach (EcommerceCountry::list_of_allowed_entries_for_dropdown() as $code => $title) { | |
if ($code === 'US') { | |
continue; | |
} | |
$geoContentBlock = $this->GeoContent($code); | |
/** @var HtmlEditorField $field */ | |
$field = HtmlEditorField::create("Geocontent[{$code}]", "Content for {$title}"); | |
if (is_a($geoContentBlock, 'GeoContent')) { | |
$field->setValue($geoContentBlock->Content); | |
} | |
$fields->addFieldToTab('Root.GeoContent', $field); | |
} | |
return $fields; | |
} | |
public function GeoContent($country = null, $createIfNotExists = false) | |
{ | |
if ($country === null) { | |
$country = EcommerceCountry::get_country(); | |
} | |
$content = $this->GeoContentBlocks()->filter('Code', $country)->first(); | |
if ((!$content || !$content->exists()) && !$createIfNotExists) { | |
return $this; | |
} | |
if ((!$content || !$content->exists()) && $createIfNotExists) { | |
/** @var GeoContent $content */ | |
$content = GeoContent::create(); | |
$content->Code = $country; | |
$content->PageID = $this->ID; | |
$content->write(); | |
} | |
return $content; | |
} | |
protected function onBeforeWrite() | |
{ | |
parent::onBeforeWrite(); | |
foreach ($this->record as $key => $value) { | |
if (preg_match('/Geocontent\[(.+)\]/', $key, $matches)) { | |
if (!$value) { | |
$block = $this->GeoContentBlocks()->filter('Code', $matches[1])->first(); | |
if ($block && $block->exists()) { | |
$block->delete(); | |
} | |
} else { | |
$block = $this->GeoContent($matches[1], true); | |
$block->Content = $value; | |
$block->write(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment