Skip to content

Commit

Permalink
Converted to PSR-2
Browse files Browse the repository at this point in the history
  • Loading branch information
helpfulrobot committed Dec 27, 2015
1 parent af6b9ee commit 8ea6973
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 133 deletions.
118 changes: 63 additions & 55 deletions code/forms/GridFieldPaginatorWithShowAll.php
Original file line number Diff line number Diff line change
@@ -1,68 +1,76 @@
<?php

class GridFieldPaginatorWithShowAll extends GridFieldPaginator {
class GridFieldPaginatorWithShowAll extends GridFieldPaginator
{

protected $itemClass = 'GridFieldPaginatorWithShowAll_Row';
protected $itemClass = 'GridFieldPaginatorWithShowAll_Row';

public function getTemplateParameters(GridField $gridField){
$data = parent::getTemplateParameters($gridField);
public function getTemplateParameters(GridField $gridField)
{
$data = parent::getTemplateParameters($gridField);

//Inject Requirements
Requirements::css('gridfieldpaginatorwithshowall/css/GridFieldPaginatorWithShowAll.css');
Requirements::javascript('gridfieldpaginatorwithshowall/javascript/GridFieldPaginatorWithShowAll.js');
//Inject Requirements
Requirements::css('gridfieldpaginatorwithshowall/css/GridFieldPaginatorWithShowAll.css');
Requirements::javascript('gridfieldpaginatorwithshowall/javascript/GridFieldPaginatorWithShowAll.js');

if(is_a($data, "ArrayData")){
$showAllMode = $showAllMode =$this->getShowAllMode();
if($showAllMode) $checked = ' checked="checked"';
else $checked = '';
if (is_a($data, "ArrayData")) {
$showAllMode = $showAllMode =$this->getShowAllMode();
if ($showAllMode) {
$checked = ' checked="checked"';
} else {
$checked = '';
}

$data->setField("ID", $gridField->ID());
$data->setField("Checked", $checked);
return $data;
}else{
return;
}
}
$data->setField("ID", $gridField->ID());
$data->setField("Checked", $checked);
return $data;
} else {
return;
}
}

/**
* Manipulate the datalist as needed by this grid modifier.
* @param GridField $gridField Grid Field Reference
* @param SS_List $dataList Data List to adjust
* @return DataList Modified Data List
*/
public function getManipulatedData(GridField $gridField, SS_List $dataList) {
$this->setShowAllMode($gridField);
/**
* Manipulate the datalist as needed by this grid modifier.
* @param GridField $gridField Grid Field Reference
* @param SS_List $dataList Data List to adjust
* @return DataList Modified Data List
*/
public function getManipulatedData(GridField $gridField, SS_List $dataList)
{
$this->setShowAllMode($gridField);

$dataList = parent::getManipulatedData($gridField, $dataList);
$showAllMode =$this->getShowAllMode();
if($showAllMode){
$dataList = $dataList->limit(999, 0);
$component = $gridField->getConfig()->getComponentByType('GridFieldPaginatorWithShowAll');
$component->setItemsPerPage(1+$dataList->count());
}
$dataList = parent::getManipulatedData($gridField, $dataList);
$showAllMode =$this->getShowAllMode();
if ($showAllMode) {
$dataList = $dataList->limit(999, 0);
$component = $gridField->getConfig()->getComponentByType('GridFieldPaginatorWithShowAll');
$component->setItemsPerPage(1+$dataList->count());
}

return $dataList;
}
return $dataList;
}

protected function setShowAllMode(GridField $gridField){
$state =$gridField->State->GridFieldShowAll;
protected function setShowAllMode(GridField $gridField)
{
$state =$gridField->State->GridFieldShowAll;

$sessionMode = Session::get("GridField.PaginatorWithShowAll.Mode");
if(isset($sessionMode)){
if(is_bool($state->showAllMode)) {
if($sessionMode != $state->showAllMode){
Session::set("GridField.PaginatorWithShowAll.Mode",$state->showAllMode);
}
}
}else{
if(is_bool($state->showAllMode)) {
Session::set("GridField.PaginatorWithShowAll.Mode",$state->showAllMode);
}
}
}
$sessionMode = Session::get("GridField.PaginatorWithShowAll.Mode");
if (isset($sessionMode)) {
if (is_bool($state->showAllMode)) {
if ($sessionMode != $state->showAllMode) {
Session::set("GridField.PaginatorWithShowAll.Mode", $state->showAllMode);
}
}
} else {
if (is_bool($state->showAllMode)) {
Session::set("GridField.PaginatorWithShowAll.Mode", $state->showAllMode);
}
}
}

protected function getShowAllMode(){
$sessionMode = Session::get("GridField.PaginatorWithShowAll.Mode");
return isset($sessionMode) && $sessionMode;
}
}
protected function getShowAllMode()
{
$sessionMode = Session::get("GridField.PaginatorWithShowAll.Mode");
return isset($sessionMode) && $sessionMode;
}
}
161 changes: 83 additions & 78 deletions tests/GridFieldPaginatorWithShowAllTest.php
Original file line number Diff line number Diff line change
@@ -1,94 +1,99 @@
<?php

class GridFieldPaginatorWithShowAllTest extends SapphireTest{
/** @var ArrayList */
protected $list;

/** @var GridField */
protected $gridField1, $gridField2;

/** @var Form */
protected $form;

/** @var string */
public static $fixture_file = 'GridFieldPaginatorWithShowAllTest.yml';
protected $extraDataObjects = array('GridFieldPaginator_ShowAll_Item');
class GridFieldPaginatorWithShowAllTest extends SapphireTest
{
/** @var ArrayList */
protected $list;

/** @var GridField */
protected $gridField1, $gridField2;

/** @var Form */
protected $form;

/** @var string */
public static $fixture_file = 'GridFieldPaginatorWithShowAllTest.yml';
protected $extraDataObjects = array('GridFieldPaginator_ShowAll_Item');

public function setUp() {
parent::setUp();
$this->list = GridFieldPaginator_ShowAll_Item::get();
$config = GridFieldConfig::create()->addComponents(
new GridFieldDataColumns(), new GridFieldPaginatorWithShowAll(2)
);
$this->gridField1 = new GridField('testfield1', 'testfield1', $this->list, $config);
$this->gridField2 = new GridField('testfield2', 'testfield2', $this->list, $config);
$this->form = new Form(new Controller(), 'mockform', new FieldList(array($this->gridField1, $this->gridField2)), new FieldList());
}
public function setUp()
{
parent::setUp();
$this->list = GridFieldPaginator_ShowAll_Item::get();
$config = GridFieldConfig::create()->addComponents(
new GridFieldDataColumns(), new GridFieldPaginatorWithShowAll(2)
);
$this->gridField1 = new GridField('testfield1', 'testfield1', $this->list, $config);
$this->gridField2 = new GridField('testfield2', 'testfield2', $this->list, $config);
$this->form = new Form(new Controller(), 'mockform', new FieldList(array($this->gridField1, $this->gridField2)), new FieldList());
}

public function testTurnOnShowAllMode() {
Session::clear('GridField.PaginatorWithShowAll.Mode');
$fieldHolder1 = $this->gridField1->FieldHolder();
$content1 = new CSSContentParser($fieldHolder1);
$this->assertEquals(2, count($content1->getBySelector('tr.ss-gridfield-item')));
$this->gridField1->State->GridFieldShowAll->showAllMode = true;

/*$stateID = 'testGridStateWithShowAllTurnedOn';
Session::set($stateID, array('grid'=>'', 'actionName'=>'setShowAllMode',
'args'=>array(
'GridFieldShowAll'=>array('showAllMode'=>true),
'GridFieldPaginator'=> array('currentPage1'=>1)
)
));
$request = new SS_HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID='.$stateID=>true));
$this->gridField1->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request);*/
public function testTurnOnShowAllMode()
{
Session::clear('GridField.PaginatorWithShowAll.Mode');
$fieldHolder1 = $this->gridField1->FieldHolder();
$content1 = new CSSContentParser($fieldHolder1);
$this->assertEquals(2, count($content1->getBySelector('tr.ss-gridfield-item')));
$this->gridField1->State->GridFieldShowAll->showAllMode = true;

/*$stateID = 'testGridStateWithShowAllTurnedOn';
Session::set($stateID, array('grid'=>'', 'actionName'=>'setShowAllMode',
'args'=>array(
'GridFieldShowAll'=>array('showAllMode'=>true),
'GridFieldPaginator'=> array('currentPage1'=>1)
)
));
$request = new SS_HTTPRequest('POST', 'url', array(), array('action_gridFieldAlterAction?StateID='.$stateID=>true));
$this->gridField1->gridFieldAlterAction(array('StateID'=>$stateID), $this->form, $request);*/

$fieldHolder1 = $this->gridField1->FieldHolder();
$content1 = new CSSContentParser($fieldHolder1);
$this->assertEquals(6, count($content1->getBySelector('tr.ss-gridfield-item')));
Session::clear('GridField.PaginatorWithShowAll.Mode');
}
$fieldHolder1 = $this->gridField1->FieldHolder();
$content1 = new CSSContentParser($fieldHolder1);
$this->assertEquals(6, count($content1->getBySelector('tr.ss-gridfield-item')));
Session::clear('GridField.PaginatorWithShowAll.Mode');
}

public function testTurnOffShowAllMode() {
Session::clear('GridField.PaginatorWithShowAll.Mode');
$this->gridField1->State->GridFieldShowAll->showAllMode = true;
public function testTurnOffShowAllMode()
{
Session::clear('GridField.PaginatorWithShowAll.Mode');
$this->gridField1->State->GridFieldShowAll->showAllMode = true;

$fieldHolder1 = $this->gridField1->FieldHolder();
$content1 = new CSSContentParser($fieldHolder1);
$this->assertEquals(6, count($content1->getBySelector('tr.ss-gridfield-item')));
$fieldHolder1 = $this->gridField1->FieldHolder();
$content1 = new CSSContentParser($fieldHolder1);
$this->assertEquals(6, count($content1->getBySelector('tr.ss-gridfield-item')));

$this->gridField1->State->GridFieldShowAll->showAllMode = false;
$this->gridField1->getConfig()->getComponentByType('GridFieldPaginatorWithShowAll')->setItemsPerPage(2);
$this->gridField1->State->GridFieldShowAll->showAllMode = false;
$this->gridField1->getConfig()->getComponentByType('GridFieldPaginatorWithShowAll')->setItemsPerPage(2);

$fieldHolder2 = $this->gridField1->FieldHolder();
$content1 = new CSSContentParser($fieldHolder2);
$this->assertEquals(2, count($content1->getBySelector('tr.ss-gridfield-item')));
$fieldHolder2 = $this->gridField1->FieldHolder();
$content1 = new CSSContentParser($fieldHolder2);
$this->assertEquals(2, count($content1->getBySelector('tr.ss-gridfield-item')));

Session::clear('GridField.PaginatorWithShowAll.Mode');
}
Session::clear('GridField.PaginatorWithShowAll.Mode');
}

public function testGoballyEffected() {
Session::clear('GridField.PaginatorWithShowAll.Mode');
$this->gridField1->State->GridFieldShowAll->showAllMode = true;
$this->gridField1->FieldHolder();
public function testGoballyEffected()
{
Session::clear('GridField.PaginatorWithShowAll.Mode');
$this->gridField1->State->GridFieldShowAll->showAllMode = true;
$this->gridField1->FieldHolder();

$fieldHolder21 = $this->gridField2->FieldHolder();
$content21 = new CSSContentParser($fieldHolder21);
$this->assertEquals(6, count($content21->getBySelector('tr.ss-gridfield-item')));
$fieldHolder21 = $this->gridField2->FieldHolder();
$content21 = new CSSContentParser($fieldHolder21);
$this->assertEquals(6, count($content21->getBySelector('tr.ss-gridfield-item')));

$this->gridField1->State->GridFieldShowAll->showAllMode = false;
$this->gridField1->FieldHolder();
$this->gridField1->State->GridFieldShowAll->showAllMode = false;
$this->gridField1->FieldHolder();

$this->gridField2->getConfig()->getComponentByType('GridFieldPaginatorWithShowAll')->setItemsPerPage(2);
$fieldHolder22 = $this->gridField2->FieldHolder();
$content22 = new CSSContentParser($fieldHolder22);
$this->assertEquals(2, count($content22->getBySelector('tr.ss-gridfield-item')));
Session::clear('GridField.PaginatorWithShowAll.Mode');
}

$this->gridField2->getConfig()->getComponentByType('GridFieldPaginatorWithShowAll')->setItemsPerPage(2);
$fieldHolder22 = $this->gridField2->FieldHolder();
$content22 = new CSSContentParser($fieldHolder22);
$this->assertEquals(2, count($content22->getBySelector('tr.ss-gridfield-item')));
Session::clear('GridField.PaginatorWithShowAll.Mode');
}
}

class GridFieldPaginator_ShowAll_Item extends DataObject implements TestOnly {
static $db = array(
'Title' => 'Varchar',
);
}
class GridFieldPaginator_ShowAll_Item extends DataObject implements TestOnly
{
public static $db = array(
'Title' => 'Varchar',
);
}

0 comments on commit 8ea6973

Please sign in to comment.