Skip to content

Commit

Permalink
Refactor/video image (#156)
Browse files Browse the repository at this point in the history
* REFACTOR allow optional image for video slide

* REFACTOR Allow image on video slide

* UPDATE migration task for links

* UPDATE phpcs
  • Loading branch information
muskie9 authored Jan 18, 2019
1 parent 6acdc47 commit 3aedc38
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 20 deletions.
24 changes: 8 additions & 16 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,26 @@
# this file, please see the EditorConfig documentation:
# http://editorconfig.org/

root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 4
indent_style = tab
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

# Docs say 80 ideally, 100 ok, no more than 120
# http://doc.silverstripe.org/en/getting_started/coding_conventions/
max_line_length = 100

[*.md]
trim_trailing_whitespace = false

[*.yml]
[*.{yml,js,json,css,scss,eslintrc,feature}]
indent_size = 2
indent_style = space

#PSR 2
[**.php]
indent_style = space
[composer.json]
indent_size = 4

[{.travis.yml,package.json}]
# The indent size used in the `package.json` file cannot be changed
# https://github.com/npm/npm/pull/3180#issuecomment-16336516
indent_size = 2
indent_style = space
# Don't perform any clean-up on thirdparty files

[thirdparty/**]
trim_trailing_whitespace = false
insert_final_newline = false
10 changes: 6 additions & 4 deletions src/Model/SlideImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use function GuzzleHttp\Psr7\parse_request;
use Sheadawson\Linkable\Forms\EmbeddedObjectField;
use Sheadawson\Linkable\Forms\LinkField;
use Sheadawson\Linkable\Models\EmbeddedObject;
use Sheadawson\Linkable\Models\Link;
use SilverStripe\AssetAdmin\Forms\UploadField;
Expand Down Expand Up @@ -64,7 +65,7 @@ class SlideImage extends DataObject implements PermissionProvider
'Video' => EmbeddedObject::class,
'Page' => \Page::class,
'PageLink' => SiteTree::class,
'Link' => Link::class,
'SlideLink' => Link::class,
];

/**
Expand Down Expand Up @@ -141,7 +142,8 @@ public function getCMSFields()
'PageID',
'Image',
'SlideType',
'VideoID',
'Video',
'SlideLinkID',
]);

// Name
Expand Down Expand Up @@ -174,7 +176,7 @@ public function getCMSFields()
// Page link
$fields->replaceField(
'PageLinkID',
TreeDropdownField::create('PageLinkID', '', SiteTree::class)
LinkField::create('SlideLinkID')
->setTitle(
_t(__CLASS__ . '.PAGE_LINK', "Choose a page to link to:")
)
Expand All @@ -196,7 +198,7 @@ public function getCMSFields()
->setTitle('Image or Video'),
Wrapper::create(
$image
)->displayIf('SlideType')->isEqualTo('Image')->end(),
)->displayIf('SlideType')->isEqualTo('Image')->orIf('SlideType')->isEqualTo('Video')->end(),
Wrapper::create(
$videoField = EmbeddedObjectField::create('Video')
->setTitle('Video URL')
Expand Down
135 changes: 135 additions & 0 deletions src/Task/SlideLinkTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Dynamic\Flexslider\Task;

use Dynamic\FlexSlider\Model\SlideImage;
use Sheadawson\Linkable\Models\Link;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DB;

/**
* Class SlideLinkTask
* @package Dynamic\Flexslider\Task
*/
class SlideLinkTask extends BuildTask
{
/**
* @var string
*/
protected $title = 'Slide Link Migration Task';

/**
* @var string
*/
private static $segment = 'slide-link-migration-task';

/**
* @var array
*/
private $known_links = [];

/**
* @param \SilverStripe\Control\HTTPRequest $request
* @throws \SilverStripe\ORM\ValidationException
*/
public function run($request)
{
$this->migrateLinks();
}

/**
* @throws \SilverStripe\ORM\ValidationException
*/
protected function migrateLinks()
{
$baseTable = SlideImage::singleton()->baseTable();

$tables = [
$baseTable,
"{$baseTable}_Versions",
"{$baseTable}_Live",
];

foreach ($tables as $table) {
foreach ($this->yieldSingle(DB::query("SELECT * FROM \"{$table}\"")) as $record) {
$linkID = $record['PageLinkID'];
$linkLabel = $record['LinkLabel'];

$slideLink = $this->findOrMakeLink($linkID, $linkLabel);

if ($slideLink !== false && $slideLink instanceof Link) {
DB::prepared_query(
"UPDATE \"{$table}\" SET \"SlideLinkID\" = ? WHERE \"ID\" = ?",
[$slideLink->ID, $record['ID']]
);
}
}
}
}

/**
* @param $list
* @return \Generator
*/
private function yieldSingle($list)
{
foreach ($list as $item) {
yield $item;
}
}

/**
* @param int $linkID
* @param string $linkLabel
* @return bool|mixed|Link
* @throws \SilverStripe\ORM\ValidationException
*/
private function findOrMakeLink($linkID = 0, $linkLabel = '')
{
if (!$linkID || !($page = SiteTree::get()->byID($linkID))) {
return false;
}

if (isset($this->getKnownLinks()[$linkID])) {
return $this->getKnownLinks()[$linkID];
}

$link = Link::create();
$link->Type = 'SiteTree';
$link->SiteTreeID = $linkID;
$link->Template = 'button';

if ($linkLabel !== null && $linkLabel !== '') {
$link->Title = $linkLabel;
} else {
$link->Title = $page->Title;
}

$link->write();

$this->addKnownLink($linkID, $link);

return $link;
}

/**
* @param $linkID
* @param $linkableLinkID
* @return $this
*/
private function addKnownLink($linkID, $linkableLinkID)
{
$this->known_links[$linkID] = $linkableLinkID;

return $this;
}

/**
* @return array
*/
private function getKnownLinks()
{
return $this->known_links;
}
}

0 comments on commit 3aedc38

Please sign in to comment.