Merge pull request #194 from assertchris/merging-tags-and-categories

Added merging of tags and categories
This commit is contained in:
Damian Mooyman 2015-04-21 18:45:05 +12:00
commit 5e9741d3fa
7 changed files with 333 additions and 91 deletions

View File

@ -0,0 +1,51 @@
<?php
class GridFieldCategorisationConfig extends GridFieldConfig_RecordEditor {
/**
* @param int $itemsPerPage
* @param array|SS_List $mergeRecords
* @param string $parentType
* @param string $parentMethod
* @param string $childMethod
*/
public function __construct($itemsPerPage = 15, $mergeRecords, $parentType, $parentMethod, $childMethod) {
parent::__construct($itemsPerPage);
$this->removeComponentsByType("GridFieldAddNewButton");
$this->addComponent(
new GridFieldAddByDBField("buttons-before-left")
);
$this->addComponent(
new GridFieldMergeAction($mergeRecords, $parentType, $parentMethod, $childMethod)
);
$columns = $this->getComponentByType('GridFieldDataColumns');
$columns->setFieldFormatting(array(
'BlogPostsCount' => function($value, &$item) {
return $item->BlogPosts()->Count();
}
));
$this->changeColumnOrder();
}
/**
* Reorders GridField columns so that Actions is last.
*/
protected function changeColumnOrder() {
/**
* @var GridFieldDataColumns $columns
*/
$columns = $this->getComponentByType('GridFieldDataColumns');
$columns->setDisplayFields(array(
'Title' => 'Title',
'BlogPostsCount' => 'Posts',
'MergeAction' => 'MergeAction',
'Actions' => 'Actions',
));
}
}

View File

@ -0,0 +1,28 @@
<?php
class GridFieldFormAction extends GridField_FormAction {
/**
* @var array
*/
protected $extraAttributes = array();
/**
* @param array $attributes
*/
public function setExtraAttributes(array $attributes) {
$this->extraAttributes = $attributes;
}
/**
* @return array
*/
public function getAttributes()
{
$attributes = parent::getAttributes();
return array_merge(
$attributes,
$this->extraAttributes
);
}
}

View File

@ -0,0 +1,140 @@
<?php
class GridFieldMergeAction implements GridField_ColumnProvider, GridField_ActionProvider {
/**
* List of records to show in the MergeAction column.
*
* @var array|SS_List
*/
protected $records;
/**
* Type of parent DataObject (i.e BlogTag, BlogCategory).
*
* @var string
*/
protected $parentType;
/**
* Relationship method to reference parent (i.e BlogTags).
*
* @var string
*/
protected $parentMethod;
/**
* Relationship method to reference child (i.e BlogPosts).
*
* @var string
*/
protected $childMethod;
/**
* @param array|SS_List $records
* @param string $parentType
* @param string $parentMethod
* @param string $childMethod
*/
public function __construct($records = array(), $parentType, $parentMethod, $childMethod) {
$this->records = $records;
$this->parentType = $parentType;
$this->parentMethod = $parentMethod;
$this->childMethod = $childMethod;
}
/**
* {@inheritdoc}
*/
public function augmentColumns($gridField, &$columns) {
if(!in_array('MergeAction', $columns)) {
$columns[] = 'MergeAction';
}
return $columns;
}
/**
* {@inheritdoc}
*/
public function getColumnsHandled($gridField) {
return array('MergeAction');
}
/**
* {@inheritdoc}
*/
public function getColumnContent($gridField, $record, $columnName) {
if($columnName === 'MergeAction') {
$dropdown = new DropdownField('Target', 'Target', $this->records->map());
$prefix = strtolower($this->parentMethod . '-' . $this->childMethod);
$action = GridFieldFormAction::create(
$gridField,
'MergeAction' . $record->ID,
'Move',
'merge',
array(
'record' => $record->ID,
'target' => $prefix . '-target-record-' . $record->ID,
)
);
$action->setExtraAttributes(array(
'data-target' => $prefix . '-target-record-' . $record->ID
));
return $dropdown->Field() . $action->Field() . '<a class="MergeActionReveal">move posts to</a>';
}
return null;
}
/**
* {@inheritdoc}
*/
public function getColumnAttributes($gridField, $record, $columnName) {
return array('class' => 'MergeAction');
}
/**
* {@inheritdoc}
*/
public function getColumnMetadata($gridField, $columnName) {
return array('title' => 'Move Posts To');
}
/**
* {@inheritdoc}
*/
public function getActions($gridField) {
return array('merge');
}
/**
* {@inheritdoc}
*/
public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
if($actionName === 'merge') {
$controller = Controller::curr();
$request = $controller->getRequest();
$target = $request->requestVar($arguments["target"]);
$parentType = $this->parentType;
$fromParent = $parentType::get()->byId($arguments['record']);
$toParent = $parentType::get()->byId($target);
$posts = $fromParent->{$this->childMethod}();
foreach ($posts as $post) {
$relationship = $post->{$this->parentMethod}();
$relationship->remove($fromParent);
$relationship->add($toParent);
}
}
}
}

View File

@ -80,41 +80,38 @@ class Blog extends Page implements PermissionProvider {
public function getCMSFields() {
Requirements::css(BLOGGER_DIR . '/css/cms.css');
Requirements::javascript(BLOGGER_DIR . '/js/expandable-help-text.js');
Requirements::javascript(BLOGGER_DIR . '/js/merge-action.js');
$self =& $this;
$this->beforeUpdateCMSFields(function ($fields) use ($self) {
// Don't show this tab if edit is not allowed
if(!$self->canEdit()) return;
// Create categories and tag config
$config = GridFieldConfig_RecordEditor::create();
$config->removeComponentsByType("GridFieldAddNewButton");
$config->addComponent(new GridFieldAddByDBField("buttons-before-left"));
if(!$self->canEdit()) {
return;
}
$categories = GridField::create(
"Categories",
_t("Blog.Categories", "Categories"),
$self->Categories(),
$config
GridFieldCategorisationConfig::create(15, $self->Categories(), 'BlogCategory', 'Categories', 'BlogPosts')
);
$tags = GridField::create(
"Tags",
_t("Blog.Tags", "Tags"),
$self->Tags(),
$config
GridFieldCategorisationConfig::create(15, $self->Categories(), 'BlogTag', 'Tags', 'BlogPosts')
);
$fields->addFieldsToTab("Root.BlogOptions", array(
$fields->addFieldsToTab("Root.Categorisation", array(
$categories,
$tags
));
});
$fields = parent::getCMSFields();
return $fields;
}
@ -122,6 +119,7 @@ class Blog extends Page implements PermissionProvider {
* Check if this member is an editor of the blog
*
* @param Member $member
*
* @return boolean
*/
public function isEditor($member) {
@ -134,6 +132,7 @@ class Blog extends Page implements PermissionProvider {
* Check if this member is a writer of the blog
*
* @param Member $member
*
* @return boolean
*/
public function isWriter($member) {
@ -146,6 +145,7 @@ class Blog extends Page implements PermissionProvider {
* Check if this member is a contributor of the blog
*
* @param Member $member
*
* @return boolean
*/
public function isContributor($member) {
@ -161,6 +161,7 @@ class Blog extends Page implements PermissionProvider {
* E.g. `Hello $RoleOf($CurrentMember.ID)`
*
* @param Member|integer $member
*
* @return string|null Author, Editor, Writer, Contributor, or null if no role
*/
public function RoleOf($member) {
@ -178,6 +179,7 @@ class Blog extends Page implements PermissionProvider {
*
* @param Member $member
* @param DataList $list Relation to check
*
* @return boolean
*/
protected function isMemberOf($member, $list) {
@ -191,7 +193,6 @@ class Blog extends Page implements PermissionProvider {
}
public function canEdit($member = null) {
$member = $member ?: Member::currentUser();
if(is_numeric($member)) $member = Member::get()->byID($member);
@ -206,6 +207,7 @@ class Blog extends Page implements PermissionProvider {
* Determine if this user can edit the editors list
*
* @param Member $member
*
* @return boolean
*/
public function canEditEditors($member = null) {
@ -222,6 +224,7 @@ class Blog extends Page implements PermissionProvider {
* Determine if this user can edit writers list
*
* @param Member $member
*
* @return boolean
*/
public function canEditWriters($member = null) {
@ -242,6 +245,7 @@ class Blog extends Page implements PermissionProvider {
* Determines if this user can edit the contributors list
*
* @param type $member
*
* @return boolean
*/
public function canEditContributors($member = null) {
@ -356,7 +360,6 @@ class Blog extends Page implements PermissionProvider {
}
/**
* Returns blogs posts for a given date period.
*
@ -389,6 +392,7 @@ class Blog extends Page implements PermissionProvider {
* Get a link to a Member profile.
*
* @param urlSegment
*
* @return String
*/
public function ProfileLink($urlSegment) {
@ -406,7 +410,6 @@ class Blog extends Page implements PermissionProvider {
}
/**
* This overwrites lumberjacks default gridfield config.
*
@ -499,7 +502,6 @@ class Blog extends Page implements PermissionProvider {
}
/**
* Blog Controller
*
@ -539,7 +541,6 @@ class Blog_Controller extends Page_Controller {
protected $blogPosts;
public function index() {
$this->blogPosts = $this->getBlogPosts();
return $this->render();
@ -622,7 +623,6 @@ class Blog_Controller extends Page_Controller {
}
/**
* Renders the blog posts for a given tag.
*
@ -638,7 +638,6 @@ class Blog_Controller extends Page_Controller {
}
/**
* Renders the blog posts for a given category
*
@ -759,7 +758,6 @@ class Blog_Controller extends Page_Controller {
}
/**
* Returns a list of paginated blog posts based on the blogPost dataList
*
@ -783,7 +781,6 @@ class Blog_Controller extends Page_Controller {
}
/**
* Tag Getter for use in templates.
*
@ -800,7 +797,6 @@ class Blog_Controller extends Page_Controller {
}
/**
* Category Getter for use in templates.
*
@ -817,7 +813,6 @@ class Blog_Controller extends Page_Controller {
}
/**
* Fetches the archive year from the url
*
@ -832,7 +827,6 @@ class Blog_Controller extends Page_Controller {
}
/**
* Fetches the archive money from the url.
*
@ -852,7 +846,6 @@ class Blog_Controller extends Page_Controller {
}
/**
* Fetches the archive day from the url
*
@ -871,7 +864,6 @@ class Blog_Controller extends Page_Controller {
}
/**
* Returns the current archive date.
*
@ -896,7 +888,6 @@ class Blog_Controller extends Page_Controller {
}
/**
* Returns a link to the RSS feed.
*

View File

@ -7,24 +7,20 @@
/*
* Sprite maps & Icons
*/
/* line 48, blog-icon/*.png */
.blog-icon-sprite, .gridfield-icon .blog-icon-timer {
background-image: url('../images/blog-icon-s0a5ab5f851.png');
background-repeat: no-repeat;
}
/* line 84, ../../../../../Library/Ruby/Gems/2.0.0/gems/compass-core-1.0.3/stylesheets/compass/utilities/sprites/_base.scss */
.gridfield-icon .blog-icon-timer {
background-position: 0 0;
}
/* line 20, ../scss/cms.scss */
#FeaturedImage .middleColumn {
clear: none;
float: left;
}
/* line 25, ../scss/cms.scss */
.blog-admin-sidebar {
width: 280px;
border-right: none;
@ -34,11 +30,9 @@
bottom: 0px;
height: 100%;
}
/* line 34, ../scss/cms.scss */
.blog-admin-sidebar .cms-panel-toggle a {
text-align: left;
}
/* line 38, ../scss/cms.scss */
.blog-admin-sidebar ~ .blog-admin-outer {
width: 100%;
padding-right: 280px;
@ -48,59 +42,47 @@
overflow-x: hidden;
box-sizing: border-box;
}
/* line 47, ../scss/cms.scss */
.blog-admin-sidebar ~ .blog-admin-outer > .ss-tabset {
position: relative;
overflow: auto;
height: 100%;
width: 100%;
}
/* line 54, ../scss/cms.scss */
.blog-admin-sidebar ~ .blog-admin-outer > .ss-tabset #Title label {
float: none;
}
/* line 57, ../scss/cms.scss */
.blog-admin-sidebar ~ .blog-admin-outer > .ss-tabset #Title .middleColumn, .blog-admin-sidebar ~ .blog-admin-outer > .ss-tabset #Title input {
width: 100%;
max-width: 100%;
margin-left: 0;
}
/* line 68, ../scss/cms.scss */
.blog-admin-sidebar .cms-content-view > .field + .field {
margin-top: 10px;
}
/* line 73, ../scss/cms.scss */
.blog-admin-sidebar .cms-content-view > .field.urlsegment .preview {
padding-top: 0;
line-height: 25px;
}
/* line 78, ../scss/cms.scss */
.blog-admin-sidebar .cms-content-view > .field.urlsegment .edit {
float: right;
}
/* line 85, ../scss/cms.scss */
.blog-admin-sidebar .cms-content-view > .field.datetime > .middleColumn > .date {
width: 60%;
}
/* line 89, ../scss/cms.scss */
.blog-admin-sidebar .cms-content-view > .field.datetime > .middleColumn > .time {
width: 36%;
float: right;
}
/* line 94, ../scss/cms.scss */
.blog-admin-sidebar .cms-content-view > .field.datetime > .middleColumn .middleColumn, .blog-admin-sidebar .cms-content-view > .field.datetime > .middleColumn input {
width: 100%;
}
/* line 103, ../scss/cms.scss */
.blog-admin-sidebar.collapsed ~ .blog-admin-outer {
padding-right: 41px;
}
/* line 109, ../scss/cms.scss */
.blog-admin-sidebar.cms-content-tools .cms-panel-content {
width: auto;
}
/* line 115, ../scss/cms.scss */
.toggle-description {
text-indent: -1000000px;
display: inline-block;
@ -110,14 +92,12 @@
margin-left: 4px;
}
/* line 124, ../scss/cms.scss */
.middleColumn.toggle-description-correct-middle {
margin-left: 0;
float: left;
width: 416px;
}
/* line 130, ../scss/cms.scss */
label.right.toggle-description-correct-right {
display: inline-block;
margin-left: 0;
@ -125,22 +105,26 @@ label.right.toggle-description-correct-right {
float: left;
}
/* line 137, ../scss/cms.scss */
.description.toggle-description-correct-description {
width: 416px;
padding: 12px 0;
}
/* line 144, ../scss/cms.scss */
.custom-summary .ui-accordion-content .field {
margin: 0;
}
/* line 148, ../scss/cms.scss */
.custom-summary .ui-accordion-content,
.custom-summary .ui-accordion-content .field {
padding: 0;
}
/* line 153, ../scss/cms.scss */
.custom-summary .ui-icon-triangle-1-e {
background-position: -16px -128px;
}
.cms table.ss-gridfield-table tr td.MergeAction {
width: 150px;
}
.cms table.ss-gridfield-table tr td.MergeAction select {
width: 75px;
}

44
js/merge-action.js Normal file
View File

@ -0,0 +1,44 @@
/**
* Register expandable help text functions with fields.
*/
(function ($) {
$.entwine('ss', function ($) {
$('.MergeAction').entwine({
'onadd': function() {
var $this = $(this);
$this.on('click', 'select', function() {
return false;
});
$this.children('button').each(function(i, button) {
var $button = $(button);
var $select = $button.prev('select');
$button.before('<input type="hidden" name="' + $button.attr('data-target') + '" value="' + $select.val() + '" />');
});
$this.on('change', 'select', function(e) {
var $target = $(e.target);
$target.next('input').val($target.val());
});
$this.children('button, select').hide();
$this.on('click', '.MergeActionReveal', function(e) {
var $target = $(e.target);
$target.parent().children('button, select').show();
$target.hide();
return false;
});
}
})
});
}(jQuery));

View File

@ -1,12 +1,10 @@
/**
* CMS Styles
*/
/**
* Include Compass framework
*/
@import "compass";
/*
* Sprite maps & Icons
*/
@ -140,7 +138,6 @@ label.right.toggle-description-correct-right {
}
.custom-summary {
.ui-accordion-content .field {
margin: 0;
}
@ -153,5 +150,12 @@ label.right.toggle-description-correct-right {
.ui-icon-triangle-1-e {
background-position: -16px -128px;
}
}
.cms table.ss-gridfield-table tr td.MergeAction {
width: 150px;
}
.cms table.ss-gridfield-table tr td.MergeAction select {
width: 75px;
}