Skip to content

Instantly share code, notes, and snippets.

Created November 22, 2016 14:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/fd09ade107038de9c6c030012622ad6f to your computer and use it in GitHub Desktop.
Save anonymous/fd09ade107038de9c6c030012622ad6f to your computer and use it in GitHub Desktop.
<?php
class Page extends SiteTree {
private static $db = array(
'ShowClients' => 'Boolean',
'SlideTimer' => 'Int'
);
private static $has_one = array(
'FooterImage' => 'Image',
'ImageBlocks' => 'Image'
);
private static $has_many = array(
'Slides' => 'TopSlide',
'ContentBlocks' => 'ContentBlock',
'InfoBlocks' => 'InfoBlock',
'ImageBlocks' => 'Image',
'TwoColorBlocks' => 'TwoColorBlock',
'ItemBlocks' => 'ItemBlocks'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$footerImageField = UploadField::create('FooterImage', 'Image on bottom of page');
$footerImageField->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
$fields->addFieldToTab('Root.ExtraFields', $footerImageField);
$imageBlock = UploadField::create('ImageBlocks', 'Gives you an imageBlock');
$imageBlock->getValidator()->setAllowedExtensions(array('jpg', 'jpeg', 'png', 'gif'));
$fields->addFieldToTab('Root.ExtraFields', $imageBlock);
$fields->addFieldToTab('Root.ExtraFields', CheckboxField::create('ShowClients', 'Display pager with Clients?'));
$fields->addFieldToTab('Root.Slides', NumericField::create('SlideTimer', 'Miliseconds per slide'));
$sliderConfig = GridFieldConfig_RelationEditor::create();
$sliderConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$sliderConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'Title' => 'Heading',
'Content' => 'Content',
'Position' => 'Content position',
'BackgroundThumbnail' => 'Background Image'
));
$sliderField = new GridField(
'Slides',
'TopSlide',
$this->Slides(),
$sliderConfig
);
$fields->addFieldToTab('Root.Slides', $sliderField);
$contentBlockConfig = GridFieldConfig_RelationEditor::create();
$contentBlockConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$contentBlockConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'AwesomeIcon' => 'Icon',
'Title' => 'Title',
'Content' => 'Content'
));
$contentBlockField = new GridField(
'ContentBlocks',
'ContentBlock',
$this->ContentBlocks(),
$contentBlockConfig
);
$fields->addFieldToTab('Root.ContentBlocks', $contentBlockField);
// Group of item Blocks
$itemBlockConfig = GridFieldConfig_RelationEditor::create();
$itemBlockConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$itemBlockConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'ItemImage' => 'Image',
'Title' => 'Title',
'Content' => 'Content',
'ClassGroup' => 'ClassGroup'
));
$itemBlockField = new GridField(
'ItemBlocks',
'ItemBlocks',
$this->ItemBlocks(),
$itemBlockConfig
);
$fields->addFieldToTab('Root.ItemBlocks', $itemBlockField);
// 1 Item info block
$infoBlockConfig = GridFieldConfig_RelationEditor::create();
// $infoBlocksConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$infoBlockConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'Content' => 'Content'
));
$infoBlockField = new GridField(
'InfoBlocks',
'InfoBlock',
$this->InfoBlocks(),
$infoBlockConfig
);
$fields->addFieldToTab('Root.InfoBlocks', $infoBlockField);
$twoColorConfig = GridFieldConfig_RelationEditor::create();
$twoColorConfig->getComponentByType('GridFieldDataColumns')->setDisplayFields(array(
'Image1' => 'Image One',
'Content' => 'Content One',
'Image2' => 'Image Two',
'Content2' => 'Content Two'
));
$twoColorField = new GridField(
'TwoColorBlocks',
'TwoColorBlock',
$this->TwoColorBlocks(),
$twoColorConfig
);
$fields->addFieldToTab('Root.TwoColorBlocks', $twoColorField);
return $fields;
}
}
class Page_Controller extends ContentController {
private static $allowed_actions = array (
'NewsletterForm'
);
public function init() {
parent::init();
Requirements::block(THIRDPARTY_DIR . '/jquery/jquery.js');
}
public function ContactPage() {
return ContactPage::get()->First();
}
public function PrimaryLocation() {
$location = Location::get()->filter('PrimaryLocation',1)->First();
if (!$location) {
return false;
}
$location->CleanPhone = preg_replace('/[^0-9+]/', '', $location->Phone);
$location->ObfuscatedEmail = str_replace('@', '@replace-me.', $location->Email);
return $location;
}
public function Clients() {
return Client::get()->sort('SortOrder');
}
public function Bundles() {
return Bundle::get()->sort('SortOrder');
}
public function Communities() {
return Community::get()->sort('Sortorder');
}
public function SelectedBundleLink() {
return BundleForm::get()->First();
}
public function BlockClass() {
$blockCount = $this->ContentBlocks()->Count();
$class = 'item-4';
if ($blockCount % 4 != 0) {
if ($blockCount % 3 == 0 || $blockCount % 5 == 0) {
$class = 'item-3';
} elseif ($blockCount % 2 == 0) {
$class = 'item-2';
}
}
return $class;
}
public function NewsletterForm() {
$fields = new FieldList(
TextField::create('Name', 'Name:'),
EmailField::create('Email', 'Email:')
);
$actions = new FieldList(
FormAction::create('SendNewsletterForm')->setTitle('Subscribe')->setAttribute('class', 'btn btn-right btn-big')
);
$validator = new RequiredFields('Name', 'Email', 'Message');
return new Form($this, 'NewsletterForm', $fields, $actions, $validator);
}
public function SendNewsletterForm($data, $form) {
// Save subscribtion to dataobject
$subscription = new NewsletterSubscribtion();
$form->saveInto($subscription);
$subscription->write();
// Send subscribtion by mail
$From = $data['Name'] . '<' . $data['Email'] . '>';
$To = $this->ContactPage->Mailto;
$Subject = 'Newsletter Subscription from CrowdTech.com';
$email = new Email($From, $To, $Subject);
$email->setTemplate('SubscriptionEmail');
$email->populateTemplate($data);
$email->send();
// Redirect user
$this->redirect($this->Link("?Success=1#NewsLetter"));
}
public function Success() {
return isset($_REQUEST['Success']) && $_REQUEST['Success'] == "1";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment