Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
colintucker committed Nov 5, 2017
2 parents 1a511d4 + f901199 commit aa77bef
Show file tree
Hide file tree
Showing 7 changed files with 158 additions and 8 deletions.
5 changes: 5 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,8 @@ SilverStripe\Forms\FieldList:
extensions:
- SilverWare\Extensions\Forms\StatusMessageExtension

# Paginated List Extensions:

SilverStripe\ORM\PaginatedList:
extensions:
- SilverWare\Extensions\Model\PaginatedListExtension
2 changes: 1 addition & 1 deletion client/dist/styles/bundle.css

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions client/src/styles/components/BaseListComponent.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

margin-bottom: $spacer-double;

&:last-child {
margin-bottom: 0;
}

> div.image {

position: relative;
Expand Down Expand Up @@ -120,4 +124,9 @@

}

ul.pagination {
margin-top: $spacer-double;
margin-bottom: 0;
}

}
7 changes: 3 additions & 4 deletions src/Extensions/Admin/CropPriorityFileFormExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ public function updateFormFields(FieldList $fields, Controller $controller, $nam
{
// Update Field Objects:

$fields->addFieldToTab(
'Editor.Details',
$fields->insertAfter(
'ParentID',
DropdownField::create(
'CropPriority',
_t(__CLASS__ . '.CROPPRIORITY', 'Crop priority'),
$this->getCropPriorityOptions()
),
'Path'
)
);
}

Expand Down
75 changes: 72 additions & 3 deletions src/Extensions/Lists/ListSourceExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
use SilverStripe\Forms\TextField;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\DataExtension;
use SilverStripe\ORM\DataList;
use SilverStripe\ORM\DB;
use SilverStripe\ORM\PaginatedList;
use SilverStripe\ORM\SS_List;
use SilverWare\Forms\FieldSection;
Expand All @@ -48,6 +50,12 @@
*/
class ListSourceExtension extends DataExtension
{
/**
* Define sort constants.
*/
const SORT_DEFAULT = 'default';
const SORT_RANDOM = 'random';

/**
* Maps field names to field types for this object.
*
Expand All @@ -57,6 +65,7 @@ class ListSourceExtension extends DataExtension
private static $db = [
'ItemsPerPage' => 'AbsoluteInt',
'NumberOfItems' => 'AbsoluteInt',
'SortItemsBy' => 'Varchar(32)',
'PaginateItems' => 'Boolean',
'ReverseItems' => 'Boolean',
'ImageItems' => 'Boolean'
Expand All @@ -79,6 +88,7 @@ class ListSourceExtension extends DataExtension
* @config
*/
private static $defaults = [
'SortItemsBy' => self::SORT_DEFAULT,
'ItemsPerPage' => 10,
'PaginateItems' => 0,
'ReverseItems' => 0,
Expand Down Expand Up @@ -130,6 +140,11 @@ public function updateCMSFields(FieldList $fields)
'NumberOfItems',
$this->owner->fieldLabel('NumberOfItems')
),
DropdownField::create(
'SortItemsBy',
$this->owner->fieldLabel('SortItemsBy'),
$this->owner->getSortItemsByOptions()
),
CheckboxField::create(
'ReverseItems',
$this->owner->fieldLabel('ReverseItems')
Expand All @@ -146,7 +161,7 @@ public function updateCMSFields(FieldList $fields)

if ($this->owner->canPaginate()) {

$fields->insertAfter(
$fields->insertBefore(
SelectionGroup::create(
'PaginateItems',
[
Expand All @@ -165,7 +180,7 @@ public function updateCMSFields(FieldList $fields)
)
]
)->setTitle($this->owner->fieldLabel('PaginateItems')),
'NumberOfItems'
'ReverseItems'
);

}
Expand All @@ -184,6 +199,7 @@ public function updateFieldLabels(&$labels)
$labels['Disabled'] = _t(__CLASS__ . '.DISABLED', 'Disabled');
$labels['ListSource'] = $labels['ListSourceID'] = _t(__CLASS__ . '.LISTSOURCE', 'List Source');
$labels['ImageItems'] = _t(__CLASS__ . '.IMAGEITEMS', 'Show only items with images');
$labels['SortItemsBy'] = _t(__CLASS__ . '.SORTITEMSBY', 'Sort items by');
$labels['ReverseItems'] = _t(__CLASS__ . '.REVERSEITEMS', 'Reverse items');
$labels['ItemsPerPage'] = _t(__CLASS__ . '.ITEMSPERPAGE', 'Items per page');
$labels['PaginateItems'] = _t(__CLASS__ . '.PAGINATEITEMS', 'Paginate items');
Expand Down Expand Up @@ -232,6 +248,12 @@ public function getListItems()

}

// Sort Items (if applicable):

if ($this->owner->SortItemsBy) {
$items = $this->sort($items);
}

// Remove Items without Images (if applicable):

if ($this->owner->ImageItems) {
Expand All @@ -256,7 +278,7 @@ public function getListItems()

// Paginate Items (if applicable):

if ($this->owner->PaginateItems) {
if ($this->owner->PaginateItems && $this->owner->SortItemsBy != self::SORT_RANDOM) {

$items = PaginatedList::create($items, $this->getRequest());

Expand Down Expand Up @@ -345,6 +367,19 @@ public function getListSourceOptions()
return ClassTools::singleton()->getImplementorMap(ListSource::class);
}

/**
* Answers an array of options for the sort items by field.
*
* @return array
*/
public function getSortItemsByOptions()
{
return [
self::SORT_DEFAULT => _t(__CLASS__ . '.DEFAULT', 'Default'),
self::SORT_RANDOM => _t(__CLASS__ . '.RANDOM', 'Random'),
];
}

/**
* Answers the name of the GET var to use for paginating the extended object.
*
Expand All @@ -359,6 +394,40 @@ public function getPaginationGetVar()
return 'start';
}

/**
* Sorts the given list of items.
*
* @param SS_List $list
*
* @return SS_List
*/
protected function sort(SS_List $list)
{
switch ($this->owner->SortItemsBy) {

// Random Sort Order:

case self::SORT_RANDOM:

if ($list instanceof DataList) {
return $list->sort(DB::get_conn()->random());
}

$items = $list->toArray();

shuffle($items);

return ArrayList::create($items);

// Default Sort Order:

default:

return $list;

}
}

/**
* Answers the request object from the current controller.
*
Expand Down
14 changes: 14 additions & 0 deletions src/Extensions/Model/MetaDataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -1028,6 +1028,20 @@ public function getMetaImageCaptionClassNames()
return $classes;
}

/**
* Answers the footer text for a meta image popup.
*
* @return string
*/
public function getMetaImageFooter()
{
if ($this->owner->hasMetaImageCaption()) {
return $this->owner->obj('MetaImageCaption')->Plain();
}

return $this->owner->getMetaSummaryLimited();
}

/**
* Answers a string of meta image wrapper class names for the template.
*
Expand Down
54 changes: 54 additions & 0 deletions src/Extensions/Model/PaginatedListExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* This file is part of SilverWare.
*
* PHP version >=5.6.0
*
* For full copyright and license information, please view the
* LICENSE.md file that was distributed with this source code.
*
* @package SilverWare\Extensions\Model
* @author Colin Tucker <colin@praxis.net.au>
* @copyright 2017 Praxis Interactive
* @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @link https://github.com/praxisnetau/silverware
*/

namespace SilverWare\Extensions\Model;

use SilverStripe\Core\Extension;

/**
* An extension which works around a URL ampersand-encoding bug with paginated lists.
*
* @package SilverWare\Extensions\Model
* @author Colin Tucker <colin@praxis.net.au>
* @copyright 2017 Praxis Interactive
* @license https://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @link https://github.com/praxisnetau/silverware
*/
class PaginatedListExtension extends Extension
{
/**
* Answers a summarised pagination for the extended object (fixes a bug with URL ampersands).
*
* @param integer $context
*
* @return ArrayList
*/
public function PageSummary($context = 4)
{
$summary = $this->owner->PaginationSummary($context);

foreach ($summary as $page) {

if ($page->Link) {
$page->Link = str_replace('&amp;', '&', $page->Link);
}

}

return $summary;
}
}

0 comments on commit aa77bef

Please sign in to comment.