Skip to content

Commit

Permalink
experimental : columns can now be used for filtering only (saves plac…
Browse files Browse the repository at this point in the history
…e in the results table)
  • Loading branch information
mage-eag committed Jul 30, 2013
1 parent 2c484d6 commit b655097
Show file tree
Hide file tree
Showing 17 changed files with 372 additions and 129 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,6 @@ public function getCustomColumnConfigUrl()
{
return $this->getUrl('customgrid/custom_column_config/index');
}

// @todo restore getErrorText() from where it was lost
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* @category BL
* @package BL_CustomGrid
* @copyright Copyright (c) 2013 Benoît Leulliette <[email protected]>
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

class BL_CustomGrid_Block_Widget_Grid_Columns_Filters
extends Mage_Adminhtml_Block_Template
{
protected function _construct()
{
parent::_construct();
$this->setTemplate('bl/customgrid/widget/grid/columns/filters.phtml');
}

public function getColumns()
{
$columns = array();

if ($gridBlock = $this->getGridBlock()) {
foreach ($gridBlock->getColumns() as $column) {
if ($column->getBlcgFilterOnly()) {
$columns[] = $column;
}
}
}

return $columns;
}

protected function _toHtml()
{
if (!$this->getIsNewGridModel()
&& ($gridBlock = $this->getGridBlock())
&& $gridBlock->getFilterVisibility()) {
return parent::_toHtml();
}
return '';
}

// @todo next step: either enable sortability, either only apply filters, do not select values when it's not needed (eg for attribute columns and custom columns with sub-queries)
}
43 changes: 30 additions & 13 deletions app/code/community/BL/CustomGrid/Model/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,7 @@ protected function _addColumnFromBlock(Mage_Adminhtml_Block_Widget_Grid_Column $
'order' => $order,
'origin' => self::GRID_COLUMN_ORIGIN_GRID,
'is_visible' => 1,
'filter_only' => 0,
'is_system' => ($column->getIsSystem() ? 1 : 0),
'missing' => 0,
'store_id' => null,
Expand Down Expand Up @@ -1029,6 +1030,7 @@ protected function _addColumnFromCollection($key, $order)
'order' => $order,
'origin' => self::GRID_COLUMN_ORIGIN_COLLECTION,
'is_visible' => 0,
'filter_only' => 0,
'is_system' => 0,
'missing' => 0,
'store_id' => null,
Expand Down Expand Up @@ -1740,7 +1742,7 @@ public function applyColumnsToGridBlock(Mage_Adminhtml_Block_Widget_Grid $grid,
$columnsOrders = array();
$columns = $this->getColumns(false, true);
uasort($columns, array($this, '_sortColumns'));
$attributes = $this->getAvailableAttributes();
$attributes = $this->getAvailableAttributes();

foreach ($columns as $column) {
if (!in_array($column['id'], $gridIds, true)) {
Expand Down Expand Up @@ -1877,6 +1879,16 @@ public function applyColumnsToGridBlock(Mage_Adminhtml_Block_Widget_Grid $grid,
$grid->blcg_removeColumn($column['id']);
}
}

if ($column['filter_only']
&& ($columnBlock = $grid->getColumn($column['id']))) {
$columnBlock->setBlcgFilterOnly(true);

if ($grid->blcg_isExport()) {
// Columns with is_system flag on won't be exported, so forcing it will save us two overloads
$columnBlock->setIsSystem(true);
}
}
}

// Apply columns orders
Expand Down Expand Up @@ -1918,7 +1930,10 @@ protected function _extractColumnValues(array $column, $allowStore=false, $allow
if (isset($column['header'])) {
$values['header'] = $column['header'];
}
$values['is_visible'] = (isset($column['is_visible']) && $column['is_visible'] ? 1 : 0);

$values['is_visible'] = (isset($column['is_visible']) && $column['is_visible'] ? 1 : 0);
$values['filter_only'] = ($values['is_visible'] && isset($column['filter_only']) && $column['filter_only'] ? 1 : 0);

if (isset($column['order'])) {
$values['order'] = intval($column['order']);
}
Expand Down Expand Up @@ -2013,17 +2028,18 @@ public function updateColumns(array $columns, $mustSave=true)

$this->_columns[$newColumnId] = array_merge(
array(
'grid_id' => $this->getId(),
'id' => $newColumnId,
'index' => $column['index'],
'width' => '',
'align' => self::GRID_COLUMN_ALIGNMENT_LEFT,
'header' => '',
'order' => 0,
'origin' => self::GRID_COLUMN_ORIGIN_ATTRIBUTE,
'is_visible' => 1,
'is_system' => 0,
'missing' => 0,
'grid_id' => $this->getId(),
'id' => $newColumnId,
'index' => $column['index'],
'width' => '',
'align' => self::GRID_COLUMN_ALIGNMENT_LEFT,
'header' => '',
'order' => 0,
'origin' => self::GRID_COLUMN_ORIGIN_ATTRIBUTE,
'is_visible' => 1,
'filter_only' => 0,
'is_system' => 0,
'missing' => 0,
),
$this->_extractColumnValues($column, true, true, false, $allowEditable)
);
Expand Down Expand Up @@ -2109,6 +2125,7 @@ public function updateCustomColumns(array $columns, $mustSave=true)
'order' => $this->_getNextOrder(),
'origin' => self::GRID_COLUMN_ORIGIN_CUSTOM,
'is_visible' => 1,
'filter_only' => 0,
'is_system' => 0,
'missing' => 0,
'store_id' => null,
Expand Down
11 changes: 11 additions & 0 deletions app/code/community/BL/CustomGrid/Model/Mysql4/Grid.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ protected function _saveColumns(Mage_Core_Model_Abstract $object)
$columnsIds = array();

foreach ($object->getColumns() as $column) {
if (isset($column['filter_only'])) {
if ($column['filter_only']
&& isset($column['is_visible'])
&& $column['is_visible']) {
$column['is_visible'] = 2;
}
unset($column['filter_only']); // No new database field to avoid a new setup
}
if (isset($column['column_id']) && ($column['column_id'] > 0)) {
// Update existing columns
$write->update($columnsTable, $column, $write->quoteInto('column_id = ?', $column['column_id']));
Expand Down Expand Up @@ -127,6 +135,9 @@ public function getGridColumns($gridId)

return $read->fetchAll($read->select()
->from($columnsTable)
->columns('*')
->columns(array('is_visible' => new Zend_Db_Expr('IF(is_visible=2, 1, is_visible)')))
->columns(array('filter_only' => new Zend_Db_Expr('IF(is_visible=2, 1, 0)')))
->where('grid_id = ?', $gridId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,12 @@ public function addColumnsToResult()
}

if (!empty($gridIds)) {
$read = $this->getResource()->getReadConnection();
$read = $this->getResource()->getReadConnection();
$columns = $read->fetchAll($read->select()
->from($this->getTable('customgrid/grid_column')
->columns('*')
->columns(array('is_visible' => new Zend_Db_Expr('IF(is_visible=2, 1, is_visible)')))
->columns(array('filter_only' => new Zend_Db_Expr('IF(is_visible=2, 1, 0)')))
->where('grid_id IN ?', $gridIds))
->order(array('order', 'asc')));

Expand Down
25 changes: 12 additions & 13 deletions app/code/community/BL/CustomGrid/Model/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,8 @@ public function getCollection()
&& ($collection instanceof Mage_Eav_Model_Entity_Collection_Abstract)
&& count($this->_blcg_additionalAttributes)) {
$this->_blcg_mustSelectAdditionalAttributes = false;
foreach ($this->_blcg_additionalAttributes as $attribute) {
$collection->joinAttribute(
$attribute[\'alias\'],
$attribute[\'attribute\'],
$attribute[\'bind\'],
$attribute[\'filter\'],
$attribute[\'join_type\'],
$attribute[\'store_id\']
);
foreach ($this->_blcg_additionalAttributes as $attr) {
$collection->joinAttribute($attr[\'alias\'], $attr[\'attribute\'], $attr[\'bind\'], $attr[\'filter\'], $attr[\'join_type\'], $attr[\'store_id\']);
}
}
return $collection;
Expand Down Expand Up @@ -203,14 +196,14 @@ protected function _prepareCollection()
}
if ($this->_blcg_prepareEventsEnabled) {
Mage::getSingleton(\'customgrid/observer\')->beforeGridPrepareCollection($this);
$this->_blcg_launchCollectionCallbacks(\'before_prepare\', array($this, $this->_collection, $this->_blcg_prepareEventsEnabled));
$this->_blcg_launchCollectionCallbacks(\'before_prepare\', array($this, $this->_collection, true));
$return = parent::_prepareCollection();
$this->_blcg_launchCollectionCallbacks(\'after_prepare\', array($this, $this->_collection, $this->_blcg_prepareEventsEnabled));
$this->_blcg_launchCollectionCallbacks(\'after_prepare\', array($this, $this->_collection, true));
Mage::getSingleton(\'customgrid/observer\')->afterGridPrepareCollection($this);
} else {
$this->_blcg_launchCollectionCallbacks(\'before_prepare\', array($this, $this->_collection, $this->_blcg_prepareEventsEnabled));
$this->_blcg_launchCollectionCallbacks(\'before_prepare\', array($this, $this->_collection, false));
$return = parent::_prepareCollection();
$this->_blcg_launchCollectionCallbacks(\'after_prepare\', array($this, $this->_collection, $this->_blcg_prepareEventsEnabled));
$this->_blcg_launchCollectionCallbacks(\'after_prepare\', array($this, $this->_collection, false));
}
if (!is_null($this->_blcg_typeModel)) {
$this->_blcg_typeModel->afterGridPrepareCollection($this, $this->_blcg_prepareEventsEnabled);
Expand Down Expand Up @@ -866,6 +859,12 @@ public function beforeBlockToHtml($observer)
->setGridBlock($grid)
->setGridModel($model)
->setIsNewGridModel($newModel)
)->setChild(
'bl_custom_grid_grid_columns_filters',
$grid->getLayout()->createBlock('customgrid/widget_grid_columns_filters')
->setGridBlock($grid)
->setGridModel($model)
->setIsNewGridModel($newModel)
);

// Replace grid template with our own one
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,16 @@
* getPagerVisibility()
* getVarNamePage()
*/
$numColumns = sizeof($this->getColumns());
$_columns = $this->getColumns();

// Remove columns that should not be displayed
foreach ($_columns as $_key => $_column):
if ($_column->getBlcgFilterOnly()):
unset($_columns[$_key]);
endif;
endforeach;

$_numColumns = count($_columns);
?>
<?php if($this->getCollection()): ?>
<?php if($this->canDisplayContainer()): ?>
Expand Down Expand Up @@ -128,13 +137,14 @@ $numColumns = sizeof($this->getColumns());
</tr>
</table>
<?php endif; ?>
<?php echo $this->getChildHtml('bl_custom_grid_grid_columns_filters') ?>
<?php if($this->getMassactionBlock()->isAvailable()): ?>
<?php echo $this->getMassactionBlockHtml() ?>
<?php endif ?>
<div class="grid">
<div class="hor-scroll">
<table cellspacing="0" class="data" id="<?php echo $this->getId() ?>_table">
<?php foreach ($this->getColumns() as $_column): ?>
<?php foreach ($_columns as $_column): ?>
<col <?php echo $_column->getHtmlProperty() ?> />
<?php if ($_column->getEditable() && !$_column->getEditOnly()) : ?>
<col <?php echo $_column->getHtmlProperty() ?> />
Expand All @@ -144,14 +154,14 @@ $numColumns = sizeof($this->getColumns());
<thead>
<?php if ($this->getHeadersVisibility()): ?>
<tr class="headings">
<?php foreach ($this->getColumns() as $_column): ?>
<?php foreach ($_columns as $_column): ?>
<th<?php echo $_column->getHeaderHtmlProperty() ?>><span class="nobr"><?php echo $_column->getHeaderHtml() ?></span></th>
<?php endforeach; ?>
</tr>
<?php endif; ?>
<?php if ($this->getFilterVisibility()): ?>
<tr class="filter">
<?php $i=0;foreach ($this->getColumns() as $_column): ?>
<?php $i=0;foreach ($_columns as $_column): ?>
<th<?php echo $_column->getHeaderHtmlProperty() ?>><?php echo $_column->getFilterHtml() ?></th>
<?php endforeach; ?>
</tr>
Expand All @@ -161,7 +171,7 @@ $numColumns = sizeof($this->getColumns());
<?php if ($this->getCountTotals()): ?>
<tfoot>
<tr class="totals">
<?php foreach ($this->getColumns() as $_column): ?>
<?php foreach ($_columns as $_column): ?>
<th class="<?php echo $_column->getCssProperty() ?>"><?php echo ($_column->hasTotalsLabel()) ? $_column->getTotalsLabel() : $_column->getRowField($_column->getGrid()->getTotals()) ?>&nbsp;</th>
<?php endforeach; ?>
</tr>
Expand All @@ -172,11 +182,11 @@ $numColumns = sizeof($this->getColumns());
<?php if (($this->getCollection()->getSize()>0) && (!$this->getIsCollapsed())): ?>
<?php foreach ($this->getCollection() as $_index=>$_item): ?>
<tr title="<?php echo $this->getRowUrl($_item) ?>"<?php if ($_class = $this->getRowClass($_item)):?> class="<?php echo $_class; ?>"<?php endif;?> >
<?php $i=0;foreach ($this->getColumns() as $_column): ?>
<?php $i=0;foreach ($_columns as $_column): ?>

<?php if ($this->shouldRenderCell($_item, $_column)):?>
<?php $_rowspan = $this->getRowspan($_item, $_column);?>
<td <?php echo ($_rowspan ? 'rowspan="' . $_rowspan . '" ' : '') ?>class="<?php echo $_column->getCssProperty() ?> <?php echo ++$i==$numColumns?'last':'' ?>">
<td <?php echo ($_rowspan ? 'rowspan="' . $_rowspan . '" ' : '') ?>class="<?php echo $_column->getCssProperty() ?> <?php echo ++$i==$_numColumns?'last':'' ?>">
<?php echo (($_html = $_column->getRowField($_item)) != '' ? $_html : '&nbsp;') ?>
</td>
<?php if ($this->shouldRenderEmptyCell($_item, $_column)):?>
Expand All @@ -190,7 +200,7 @@ $numColumns = sizeof($this->getColumns());
<?php foreach ($_multipleRows as $_i):?>
<tr>
<?php $i=0;foreach ($this->getMultipleRowColumns($_i) as $_column): ?>
<td class="<?php echo $_column->getCssProperty() ?> <?php echo ++$i==$numColumns-1?'last':'' ?>">
<td class="<?php echo $_column->getCssProperty() ?> <?php echo ++$i==$_numColumns-1?'last':'' ?>">
<?php echo (($_html = $_column->getRowField($_i)) != '' ? $_html : '&nbsp;') ?>
</td>
<?php endforeach; ?>
Expand All @@ -201,7 +211,7 @@ $numColumns = sizeof($this->getColumns());
<?php if ($this->shouldRenderSubTotal($_item)): ?>
<tr class="subtotals">
<?php $i = 0; foreach ($this->getSubTotalColumns() as $_column): ?>
<td class="<?php echo $_column->getCssProperty() ?> <?php echo ++$i == $numColumns ? 'last' : '' ?>">
<td class="<?php echo $_column->getCssProperty() ?> <?php echo ++$i == $_numColumns ? 'last' : '' ?>">
<?php echo ($_column->hasSubtotalsLabel() ? $_column->getSubtotalsLabel() :
$_column->getRowField($this->getSubTotalItem($_item))
);
Expand Down
Loading

0 comments on commit b655097

Please sign in to comment.