Skip to content

Commit

Permalink
ENHANCEMENT SlideType default value task (#158)
Browse files Browse the repository at this point in the history
* ENHANCEMENT SlideType default value task

BUGFIX check if LinkLabel isset in SlideLinkTask

* UPDATE phpcs fix
  • Loading branch information
muskie9 authored Feb 5, 2019
1 parent 0ae5e23 commit 2979dcb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
72 changes: 72 additions & 0 deletions src/Task/DefaultSlideTypeTask.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Dynamic\Flexslider\Task;

use Dynamic\FlexSlider\Model\SlideImage;
use SilverStripe\Dev\BuildTask;
use SilverStripe\ORM\DB;

/**
* Class DefaultSlideTypeTask
* @package Dynamic\Flexslider\Task
*/
class DefaultSlideTypeTask extends BuildTask
{
/**
* @var string
*/
private static $segment = 'default-slide-type-task';

/**
* @var string
*/
protected $title = 'Flexslider - Default Slide Type Task';

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

/**
*
*/
protected function setDefaults()
{
$default = SlideImage::singleton()->config()->get('defaults');

if (isset($default['SlideType'])) {
$baseTable = SlideImage::singleton()->baseTable();

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

foreach ($tables as $table) {
$query = DB::query("SELECT * FROM \"{$table}\" WHERE \"SlideType\" IS NULL");

foreach ($this->yieldSingle($query) as $record) {
DB::prepared_query(
"UPDATE \"{$table}\" SET \"SlideType\" = ? WHERE \"ID\" = ?",
[$default['SlideType'], $record['ID']]
);
}
}
}
}

/**
* @param $list
* @return \Generator
*/
protected function yieldSingle($list)
{
foreach ($list as $item) {
yield $item;
}
}
}
2 changes: 1 addition & 1 deletion src/Task/SlideLinkTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected function migrateLinks()
foreach ($tables as $table) {
foreach ($this->yieldSingle(DB::query("SELECT * FROM \"{$table}\"")) as $record) {
$linkID = $record['PageLinkID'];
$linkLabel = $record['LinkLabel'];
$linkLabel = isset($record['LinkLabel']) ? $record['LinkLabel'] : null;

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

Expand Down

0 comments on commit 2979dcb

Please sign in to comment.