Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
* SlideImage - moved statics from config.yml to class

* FlexSlider - added requirements to `contentcontrollerInit()`

* FlexSliderExtension - added depreciation method as this file is no longer used

* updated README to remove references to `FlexSliderExtension`
  • Loading branch information
jsirish committed Sep 15, 2016
1 parent 804d06e commit 12f6e5e
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 74 deletions.
26 changes: 1 addition & 25 deletions _config/config.yml
@@ -1,27 +1,3 @@
---
Name: FlexSlider
---
SlideImage:
db:
Name: Varchar(255)
Headline: Varchar(255)
Description: Text
SortOrder: Int
ShowSlide: Boolean
has_one:
Image: Image
Page: Page
PageLink: SiteTree
singular_name:
- Slide
plural_name:
- Slides
default_order:
- SortOrder
summary_fields:
Image.CMSThumbnail: Image
Name: Name
searchable_fields:
Name: Name
Title: Title
Description: Description
---
74 changes: 74 additions & 0 deletions code/FlexSlider.php
Expand Up @@ -2,31 +2,53 @@

class FlexSlider extends DataExtension
{
/**
* @var array
*/
public static $db = array(
'Animation' => "Enum('slide, fade', 'slide')",
'Loop' => 'Boolean',
'Animate' => 'Boolean',
'ThumbnailNav' => 'Boolean',
);

/**
* @var array
*/
public static $has_many = array(
'Slides' => 'SlideImage',
);

/**
* @var array
*/
public static $defaults = array(
'Loop' => '1',
'Animate' => '1',
);

/**
*
*/
public function populateDefaults()
{
parent::populateDefaults();
$this->Loop = 1;
$this->Animate = 1;
}

/**
* @param FieldList $fields
*/
public function updateCMSFields(FieldList $fields)
{
$fields->removeByName(array(
'Animation',
'Loop',
'Animate',
'ThumbnailNav',
));

// Slides
if ($this->owner->ID) {
$config = GridFieldConfig_RecordEditor::create();
Expand Down Expand Up @@ -57,8 +79,60 @@ public function updateCMSFields(FieldList $fields)
}
}

/**
* @return DataList
*/
public function SlideShow()
{
return $this->owner->Slides()->filter(array('ShowSlide' => 1))->sort('SortOrder');
}

/**
* add requirements to Page_Controller init()
*/
public function contentcontrollerInit()
{
// Flexslider options
$animate = ($this->owner->Animate) ? 'true' : 'false';
$loop = ($this->owner->Loop) ? 'true' : 'false';
$sync = ($this->owner->ThumbnailNav == true) ? "sync: '#carousel'," : '';
$before = (method_exists($this->owner->ClassName, 'flexSliderBeforeAction'))
? $this->owner->flexSliderBeforeAction()
: 'function(){}';
$after = (method_exists($this->owner->ClassName, 'flexSliderAfterAction'))
? $this->owner->flexSliderAfterAction()
: 'function(){}';
$speed = (method_exists($this->owner->ClassName, 'setFlexSliderSpeed'))
? $this->owner->setFlexSliderSpeed()
: 7000;

// only call custom script if page has Slides and DataExtension
if (Object::has_extension($this->owner->ClassName, 'FlexSlider')) {
if ($this->owner->Slides()->exists()) {
Requirements::customScript("
(function($) {
$(document).ready(function(){
$('.flexslider').flexslider({
slideshow: ".$animate.",
animation: '".$this->owner->Animation."',
animationLoop: ".$loop.",
controlNav: true,
directionNav: true,
prevText: '',
nextText: '',
pauseOnAction: true,
pauseOnHover: true,
".$sync."
start: function(slider){
$('body').removeClass('loading');
},
before: ".$before.',
after: '.$after.',
slideshowSpeed: '.$speed.'
});
});
}(jQuery));');
}
}
}
}
52 changes: 8 additions & 44 deletions code/FlexSliderExtension.php
@@ -1,51 +1,15 @@
<?php

/**
* Class FlexSliderExtension
*
* @deprecated 1.0.0 This class's methods have been moved to {@link FlexSlider}
*/
class FlexSliderExtension extends Extension
{
public function onAfterInit()
public function __construct()
{

// Flexslider options
$animate = ($this->owner->Animate) ? 'true' : 'false';
$loop = ($this->owner->Loop) ? 'true' : 'false';
$sync = ($this->owner->ThumbnailNav == true) ? "sync: '#carousel'," : '';
$before = (method_exists($this->owner->ClassName, 'flexSliderBeforeAction'))
? $this->owner->flexSliderBeforeAction()
: 'function(){}';
$after = (method_exists($this->owner->ClassName, 'flexSliderAfterAction'))
? $this->owner->flexSliderAfterAction()
: 'function(){}';
$speed = (method_exists($this->owner->ClassName, 'setFlexSliderSpeed'))
? $this->owner->setFlexSliderSpeed()
: 7000;

// only call custom script if page has Slides and DataExtension
if (Object::has_extension($this->owner->data()->ClassName, 'FlexSlider')) {
if ($this->owner->data()->Slides()->exists()) {
Requirements::customScript("
(function($) {
$(document).ready(function(){
$('.flexslider').flexslider({
slideshow: ".$animate.",
animation: '".$this->owner->Animation."',
animationLoop: ".$loop.",
controlNav: true,
directionNav: true,
prevText: '',
nextText: '',
pauseOnAction: true,
pauseOnHover: true,
".$sync."
start: function(slider){
$('body').removeClass('loading');
},
before: ".$before.',
after: '.$after.',
slideshowSpeed: '.$speed.'
});
});
}(jQuery));');
}
}
trigger_error('Class no longer used, please remove references to this class from any extension declarations.', E_USER_NOTICE);
parent::__construct();
}
}
82 changes: 81 additions & 1 deletion code/SlideImage.php
Expand Up @@ -2,11 +2,68 @@

class SlideImage extends DataObject implements PermissionProvider
{
//TODO: move to config.yml
/**
* @var string
*/
private static $singular_name = 'Slide';

/**
* @var string
*/
private static $plural_name = 'Slides';

/**
* @var array
*/
private static $db = array(
'Name' => 'Varchar(255)',
'Headline' => 'Varchar(255)',
'Description' => 'Text',
'SortOrder' => 'Int',
'ShowSlide' => 'Boolean',
);

/**
* @var array
*/
private static $has_one = array(
'Image' => 'Image',
'Page' => 'Page',
'PageLink' => 'SiteTree',
);

/**
* @var string
*/
private static $default_sort = 'SortOrder';

/**
* @var array
*/
private static $defaults = array(
'ShowSlide' => true,
);

/**
* @var array
*/
private static $summary_fields = array(
'Image.CMSThumbnail' => 'Image',
'Name' => 'Name',
);

/**
* @var array
*/
private static $searchable_fields = array(
'Name',
'Headline',
'Description',
);

/**
* @return FieldList
*/
public function getCMSFields()
{
$fields = parent::getCMSFields();
Expand Down Expand Up @@ -40,6 +97,9 @@ public function getCMSFields()
return $fields;
}

/**
* @return ValidationResult
*/
public function validate()
{
$result = parent::validate();
Expand All @@ -55,6 +115,9 @@ public function validate()
return $result;
}

/**
* @return array
*/
public function providePermissions()
{
return array(
Expand All @@ -63,21 +126,38 @@ public function providePermissions()
'Slide_CREATE' => 'Slide Create',
);
}

/**
* @param null $member
* @return bool|int
*/
public function canCreate($member = null)
{
return Permission::check('Slide_CREATE');
}

/**
* @param null $member
* @return bool|int
*/
public function canEdit($member = null)
{
return Permission::check('Slide_EDIT');
}

/**
* @param null $member
* @return bool|int
*/
public function canDelete($member = null)
{
return Permission::check('Slide_DELETE');
}

/**
* @param null $member
* @return bool
*/
public function canView($member = null)
{
return true;
Expand Down
4 changes: 0 additions & 4 deletions docs/en/index.md
Expand Up @@ -12,16 +12,12 @@ In your `mysite/_config/config.yml` add the following to the desired page type o
Page:
extensions:
- FlexSlider
Page_Controller:
extensions:
- FlexSliderExtension
```

Alternatively you could add the following to your `mysite/_config.php` file:

```
Page::add_extension('FlexSlider');
Page_Controller::add_extension('FlexSliderExtension')
```

After attaching the DataExtension to your page type or DataObject run a `dev/build` then `?flush=all`.
Expand Down

0 comments on commit 12f6e5e

Please sign in to comment.