00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Adminhtml_Block_Widget_Grid extends Mage_Adminhtml_Block_Widget
00035 {
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050 protected $_columns = array();
00051
00052 protected $_lastColumnId;
00053
00054
00055
00056
00057
00058
00059 protected $_collection = null;
00060
00061
00062
00063
00064
00065
00066 protected $_varNameLimit = 'limit';
00067 protected $_varNamePage = 'page';
00068 protected $_varNameSort = 'sort';
00069 protected $_varNameDir = 'dir';
00070 protected $_varNameFilter = 'filter';
00071
00072 protected $_defaultLimit = 20;
00073 protected $_defaultPage = 1;
00074 protected $_defaultSort = false;
00075 protected $_defaultDir = 'desc';
00076 protected $_defaultFilter = array();
00077
00078
00079
00080
00081
00082
00083 protected $_isExport = false;
00084
00085
00086
00087
00088
00089
00090 protected $_emptyText;
00091
00092
00093
00094
00095
00096
00097 protected $_emptyTextCss = 'a-center';
00098
00099
00100
00101
00102
00103
00104 protected $_pagerVisibility = true;
00105
00106
00107
00108
00109
00110
00111 protected $_headersVisibility = true;
00112
00113
00114
00115
00116
00117
00118 protected $_filterVisibility = true;
00119
00120
00121
00122
00123
00124
00125 protected $_messageBlockVisibility = false;
00126
00127 protected $_saveParametersInSession = false;
00128
00129
00130
00131
00132
00133
00134 protected $_countTotals = false;
00135
00136
00137
00138
00139
00140
00141 protected $_varTotals;
00142
00143
00144
00145
00146
00147
00148 protected $_exportTypes = array();
00149
00150
00151
00152
00153
00154
00155 protected $_massactionIdField = null;
00156
00157
00158
00159
00160
00161
00162 protected $_massactionBlockName = 'adminhtml/widget_grid_massaction';
00163
00164
00165
00166
00167 protected $_rssLists = array();
00168
00169 public function __construct($attributes=array())
00170 {
00171 parent::__construct($attributes);
00172 $this->setTemplate('widget/grid.phtml');
00173 $this->setRowClickCallback('openGridRow');
00174 $this->_emptyText = Mage::helper('adminhtml')->__('No records found.');
00175 }
00176
00177 protected function _prepareLayout()
00178 {
00179 $this->setChild('export_button',
00180 $this->getLayout()->createBlock('adminhtml/widget_button')
00181 ->setData(array(
00182 'label' => Mage::helper('adminhtml')->__('Export'),
00183 'onclick' => $this->getJsObjectName().'.doExport()',
00184 'class' => 'task'
00185 ))
00186 );
00187 $this->setChild('reset_filter_button',
00188 $this->getLayout()->createBlock('adminhtml/widget_button')
00189 ->setData(array(
00190 'label' => Mage::helper('adminhtml')->__('Reset Filter'),
00191 'onclick' => $this->getJsObjectName().'.resetFilter()',
00192 ))
00193 );
00194 $this->setChild('search_button',
00195 $this->getLayout()->createBlock('adminhtml/widget_button')
00196 ->setData(array(
00197 'label' => Mage::helper('adminhtml')->__('Search'),
00198 'onclick' => $this->getJsObjectName().'.doFilter()',
00199 'class' => 'task'
00200 ))
00201 );
00202 return parent::_prepareLayout();
00203 }
00204
00205 public function getExportButtonHtml()
00206 {
00207 return $this->getChildHtml('export_button');
00208 }
00209
00210 public function getResetFilterButtonHtml()
00211 {
00212 return $this->getChildHtml('reset_filter_button');
00213 }
00214
00215 public function getSearchButtonHtml()
00216 {
00217 return $this->getChildHtml('search_button');
00218 }
00219
00220 public function getMainButtonsHtml()
00221 {
00222 $html = '';
00223 if($this->getFilterVisibility()){
00224 $html.= $this->getResetFilterButtonHtml();
00225 $html.= $this->getSearchButtonHtml();
00226 }
00227 return $html;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236 public function setCollection($collection)
00237 {
00238 $this->_collection = $collection;
00239 }
00240
00241
00242
00243
00244
00245
00246 public function getCollection()
00247 {
00248 return $this->_collection;
00249 }
00250
00251
00252
00253
00254
00255
00256
00257
00258 public function addColumn($columnId, $column)
00259 {
00260 if (is_array($column)) {
00261 $this->_columns[$columnId] = $this->getLayout()->createBlock('adminhtml/widget_grid_column')
00262 ->setData($column)
00263 ->setGrid($this);
00264 }
00265
00266
00267
00268 else {
00269 throw new Exception(Mage::helper('adminhtml')->__('Wrong column format'));
00270 }
00271
00272 $this->_columns[$columnId]->setId($columnId);
00273 $this->_lastColumnId = $columnId;
00274 return $this;
00275 }
00276
00277 public function getLastColumnId()
00278 {
00279 return $this->_lastColumnId;
00280 }
00281
00282 public function getColumnCount()
00283 {
00284 return count($this->getColumns());
00285 }
00286
00287
00288
00289
00290
00291
00292
00293 public function getColumn($columnId)
00294 {
00295 if (!empty($this->_columns[$columnId])) {
00296 return $this->_columns[$columnId];
00297 }
00298 return false;
00299 }
00300
00301
00302
00303
00304
00305
00306 public function getColumns()
00307 {
00308 return $this->_columns;
00309 }
00310
00311 protected function _setFilterValues($data)
00312 {
00313 foreach ($this->getColumns() as $columnId => $column) {
00314 if (isset($data[$columnId]) && (!empty($data[$columnId]) || strlen($data[$columnId]) > 0) && $column->getFilter()) {
00315 $column->getFilter()->setValue($data[$columnId]);
00316 $this->_addColumnFilterToCollection($column);
00317 }
00318 }
00319 return $this;
00320 }
00321
00322 protected function _addColumnFilterToCollection($column)
00323 {
00324 if ($this->getCollection()) {
00325 $field = ( $column->getFilterIndex() ) ? $column->getFilterIndex() : $column->getIndex();
00326 if ($column->getFilterConditionCallback()) {
00327 call_user_func($column->getFilterConditionCallback(), $this->getCollection(), $column);
00328 } else {
00329 $cond = $column->getFilter()->getCondition();
00330 if ($field && isset($cond)) {
00331 $this->getCollection()->addFieldToFilter($field , $cond);
00332 }
00333 }
00334 }
00335 return $this;
00336 }
00337
00338
00339
00340
00341
00342
00343 protected function _prepareCollection()
00344 {
00345 if ($this->getCollection()) {
00346
00347 $this->_preparePage();
00348
00349 $columnId = $this->getParam($this->getVarNameSort(), $this->_defaultSort);
00350 $dir = $this->getParam($this->getVarNameDir(), $this->_defaultDir);
00351 $filter = $this->getParam($this->getVarNameFilter(), null);
00352
00353 if (is_null($filter)) {
00354 $filter = $this->_defaultFilter;
00355 }
00356
00357 if (is_string($filter)) {
00358 $data = array();
00359 $filter = base64_decode($filter);
00360 parse_str(urldecode($filter), $data);
00361 $this->_setFilterValues($data);
00362 } else if ($filter && is_array($filter)) {
00363 $this->_setFilterValues($filter);
00364 } else if(0 !== sizeof($this->_defaultFilter)) {
00365 $this->_setFilterValues($this->_defaultFilter);
00366 }
00367
00368 if (isset($this->_columns[$columnId]) && $this->_columns[$columnId]->getIndex()) {
00369 $dir = (strtolower($dir)=='desc') ? 'desc' : 'asc';
00370 $this->_columns[$columnId]->setDir($dir);
00371 $column = $this->_columns[$columnId]->getFilterIndex() ?
00372 $this->_columns[$columnId]->getFilterIndex() : $this->_columns[$columnId]->getIndex();
00373 $this->getCollection()->setOrder($column , $dir);
00374 }
00375
00376 if ( !$this->_isExport ) {
00377 $this->getCollection()->load();
00378 $this->_afterLoadCollection();
00379 }
00380 }
00381
00382 return $this;
00383 }
00384
00385 protected function _preparePage()
00386 {
00387 $this->getCollection()->setPageSize($this->getParam($this->getVarNameLimit(), $this->_defaultLimit));
00388 $this->getCollection()->setCurPage($this->getParam($this->getVarNamePage(), $this->_defaultPage));
00389 }
00390
00391 protected function _prepareColumns()
00392 {
00393 return $this;
00394 }
00395
00396
00397
00398
00399
00400
00401 protected function _prepareMassactionBlock()
00402 {
00403 $this->setChild('massaction', $this->getLayout()->createBlock($this->getMassactionBlockName()));
00404 $this->_prepareMassaction();
00405 if($this->getMassactionBlock()->isAvailable()) {
00406 $this->_prepareMassactionColumn();
00407 }
00408 return $this;
00409 }
00410
00411
00412
00413
00414
00415
00416 protected function _prepareMassaction()
00417 {
00418
00419 return $this;
00420 }
00421
00422
00423
00424
00425
00426
00427 protected function _prepareMassactionColumn()
00428 {
00429 $columnId = 'massaction';
00430 $massactionColumn = $this->getLayout()->createBlock('adminhtml/widget_grid_column')
00431 ->setData(array(
00432 'index' => $this->getMassactionIdField(),
00433 'type' => 'massaction',
00434 'name' => $this->getMassactionBlock()->getFormFieldName(),
00435 'align' => 'center',
00436 'is_system' => true
00437 ));
00438
00439 if ($this->getNoFilterMassactionColumn()) {
00440 $massactionColumn->setData('filter', false);
00441 }
00442
00443 $massactionColumn->setSelected($this->getMassactionBlock()->getSelected())
00444 ->setGrid($this)
00445 ->setId($columnId);
00446
00447 $oldColumns = $this->_columns;
00448 $this->_columns = array();
00449 $this->_columns[$columnId] = $massactionColumn;
00450 $this->_columns = array_merge($this->_columns, $oldColumns);
00451 return $this;
00452 }
00453
00454 protected function _prepareGrid()
00455 {
00456 $this->_prepareColumns();
00457 $this->_prepareMassactionBlock();
00458 $this->_prepareCollection();
00459 return $this;
00460 }
00461
00462 protected function _beforeToHtml()
00463 {
00464 $this->_prepareGrid();
00465 return parent::_beforeToHtml();
00466 }
00467
00468 protected function _afterLoadCollection()
00469 {
00470 return $this;
00471 }
00472
00473 public function getVarNameLimit()
00474 {
00475 return $this->_varNameLimit;
00476 }
00477
00478 public function getVarNamePage()
00479 {
00480 return $this->_varNamePage;
00481 }
00482
00483 public function getVarNameSort()
00484 {
00485 return $this->_varNameSort;
00486 }
00487
00488 public function getVarNameDir()
00489 {
00490 return $this->_varNameDir;
00491 }
00492
00493 public function getVarNameFilter()
00494 {
00495 return $this->_varNameFilter;
00496 }
00497
00498 public function setVarNameLimit($name)
00499 {
00500 return $this->_varNameLimit = $name;
00501 }
00502
00503 public function setVarNamePage($name)
00504 {
00505 return $this->_varNamePage = $name;
00506 }
00507
00508 public function setVarNameSort($name)
00509 {
00510 return $this->_varNameSort = $name;
00511 }
00512
00513 public function setVarNameDir($name)
00514 {
00515 return $this->_varNameDir = $name;
00516 }
00517
00518 public function setVarNameFilter($name)
00519 {
00520 return $this->_varNameFilter = $name;
00521 }
00522
00523
00524
00525
00526
00527
00528 public function setHeadersVisibility($visible=true)
00529 {
00530 $this->_headersVisibility = $visible;
00531 }
00532
00533
00534
00535
00536
00537
00538 public function getHeadersVisibility()
00539 {
00540 return $this->_headersVisibility;
00541 }
00542
00543
00544
00545
00546
00547
00548 public function setPagerVisibility($visible=true)
00549 {
00550 $this->_pagerVisibility = $visible;
00551 }
00552
00553
00554
00555
00556
00557
00558 public function getPagerVisibility()
00559 {
00560 return $this->_pagerVisibility;
00561 }
00562
00563
00564
00565
00566
00567
00568 public function setFilterVisibility($visible=true)
00569 {
00570 $this->_filterVisibility = $visible;
00571 }
00572
00573
00574
00575
00576
00577
00578 public function getFilterVisibility()
00579 {
00580 return $this->_filterVisibility;
00581 }
00582
00583
00584
00585
00586
00587
00588 public function setMessageBlockVisibility($visible=true)
00589 {
00590 $this->_messageBlockVisibility = $visible;
00591 }
00592
00593
00594
00595
00596
00597
00598 public function getMessageBlockVisibility()
00599 {
00600 return $this->_messageBlockVisibility;
00601 }
00602
00603 public function setDefaultLimit($limit)
00604 {
00605 $this->_defaultLimit = $limit;
00606 return $this;
00607 }
00608
00609 public function setDefaultPage($page)
00610 {
00611 $this->_defaultPage = $page;
00612 return $this;
00613 }
00614
00615 public function setDefaultSort($sort)
00616 {
00617 $this->_defaultSort = $sort;
00618 return $this;
00619 }
00620
00621 public function setDefaultDir($dir)
00622 {
00623 $this->_defaultDir = $dir;
00624 return $this;
00625 }
00626
00627 public function setDefaultFilter($filter)
00628 {
00629 $this->_defaultFilter = $filter;
00630 return $this;
00631 }
00632
00633
00634
00635
00636
00637
00638 public function getExportTypes()
00639 {
00640 return empty($this->_exportTypes) ? false : $this->_exportTypes;
00641 }
00642
00643
00644
00645
00646
00647
00648
00649
00650 public function addExportType($url, $label)
00651 {
00652 $this->_exportTypes[] = new Varien_Object(
00653 array(
00654 'url' => $this->getUrl($url, array('_current'=>true)),
00655 'label' => $label
00656 )
00657 );
00658 return $this;
00659 }
00660
00661
00662
00663
00664
00665
00666 public function getRssLists()
00667 {
00668 return empty($this->_rssLists) ? false : $this->_rssLists;
00669 }
00670
00671
00672
00673
00674
00675
00676
00677
00678 public function addRssList($url, $label)
00679 {
00680 $this->_rssLists[] = new Varien_Object(
00681 array(
00682 'url' => Mage::getModel('core/url')->getUrl($url),
00683 'label' => $label
00684 )
00685 );
00686 return $this;
00687 }
00688
00689
00690
00691
00692
00693
00694 public function getHtml()
00695 {
00696 return $this->toHtml();
00697 }
00698
00699
00700
00701
00702
00703
00704 public function getCsv()
00705 {
00706 $csv = '';
00707 $this->_isExport = true;
00708 $this->_prepareGrid();
00709 $this->getCollection()->getSelect()->limit();
00710 $this->getCollection()->setPageSize(0);
00711 $this->getCollection()->load();
00712 $this->_afterLoadCollection();
00713
00714 $data = array();
00715 foreach ($this->_columns as $column) {
00716 if (!$column->getIsSystem()) {
00717 $data[] = '"'.$column->getExportHeader().'"';
00718 }
00719 }
00720 $csv.= implode(',', $data)."\n";
00721
00722 foreach ($this->getCollection() as $item) {
00723 $data = array();
00724 foreach ($this->_columns as $column) {
00725 if (!$column->getIsSystem()) {
00726 $data[] = '"'.str_replace(array('"', '\\'), array('""', '\\\\'), $column->getRowFieldExport($item)).'"';
00727 }
00728 }
00729 $csv.= implode(',', $data)."\n";
00730 }
00731
00732 if ($this->getCountTotals())
00733 {
00734 $data = array();
00735 foreach ($this->_columns as $column) {
00736 if (!$column->getIsSystem()) {
00737 $data[] = '"'.str_replace(array('"', '\\'), array('""', '\\\\'), $column->getRowFieldExport($this->getTotals())).'"';
00738 }
00739 }
00740 $csv.= implode(',', $data)."\n";
00741 }
00742
00743 return $csv;
00744 }
00745
00746 public function getXml()
00747 {
00748 $this->_isExport = true;
00749 $this->_prepareGrid();
00750 $this->getCollection()->getSelect()->limit();
00751 $this->getCollection()->setPageSize(0);
00752 $this->getCollection()->load();
00753 $this->_afterLoadCollection();
00754 $indexes = array();
00755 foreach ($this->_columns as $column) {
00756 if (!$column->getIsSystem()) {
00757 $indexes[] = $column->getIndex();
00758 }
00759 }
00760 $xml = '<?xml version="1.0" encoding="UTF-8"?>';
00761 $xml.= '<items>';
00762 foreach ($this->getCollection() as $item) {
00763 $xml.= $item->toXml($indexes);
00764 }
00765 if ($this->getCountTotals())
00766 {
00767 $xml.= $this->getTotals()->toXml($indexes);
00768 }
00769 $xml.= '</items>';
00770 return $xml;
00771 }
00772
00773 public function getExcel($filename = '')
00774 {
00775 $this->_isExport = true;
00776 $this->_prepareGrid();
00777 $this->getCollection()->getSelect()->limit();
00778 $this->getCollection()->setPageSize(0);
00779 $this->getCollection()->load();
00780 $this->_afterLoadCollection();
00781 $headers = array();
00782 $data = array();
00783 foreach ($this->_columns as $column) {
00784 if (!$column->getIsSystem()) {
00785 $headers[] = $column->getHeader();
00786 }
00787 }
00788 $data[] = $headers;
00789
00790 foreach ($this->getCollection() as $item) {
00791 $row = array();
00792 foreach ($this->_columns as $column) {
00793 if (!$column->getIsSystem()) {
00794 $row[] = $column->getRowField($item);
00795 }
00796 }
00797 $data[] = $row;
00798 }
00799
00800 if ($this->getCountTotals())
00801 {
00802 $row = array();
00803 foreach ($this->_columns as $column) {
00804 if (!$column->getIsSystem()) {
00805 $row[] = $column->getRowField($this->getTotals());
00806 }
00807 }
00808 $data[] = $row;
00809 }
00810
00811 $xmlObj = new Varien_Convert_Parser_Xml_Excel();
00812 $xmlObj->setVar('single_sheet', $filename);
00813 $xmlObj->setData($data);
00814 $xmlObj->unparse();
00815
00816 return $xmlObj->getData();
00817 }
00818
00819 public function canDisplayContainer()
00820 {
00821 if ($this->getRequest()->getQuery('ajax')) {
00822 return false;
00823 }
00824 return true;
00825 }
00826
00827 public function getGridUrl()
00828 {
00829 return $this->getCurrentUrl();
00830 }
00831
00832
00833
00834
00835
00836
00837
00838
00839 public function getParam($paramName, $default=null)
00840 {
00841 $session = Mage::getSingleton('adminhtml/session');
00842 $sessionParamName = $this->getId().$paramName;
00843 if ($this->getRequest()->has($paramName)) {
00844 $param = $this->getRequest()->getParam($paramName);
00845 if ($this->_saveParametersInSession) {
00846 $session->setData($sessionParamName, $param);
00847 }
00848 return $param;
00849 }
00850 elseif ($this->_saveParametersInSession && ($param = $session->getData($sessionParamName)))
00851 {
00852 return $param;
00853 }
00854
00855 return $default;
00856 }
00857
00858 public function setSaveParametersInSession($flag)
00859 {
00860 $this->_saveParametersInSession = $flag;
00861 return $this;
00862 }
00863
00864 public function getJsObjectName()
00865 {
00866 return $this->getId().'JsObject';
00867 }
00868
00869
00870
00871
00872
00873
00874 public function getRowId($row)
00875 {
00876 return $this->getRowUrl($row);
00877 }
00878
00879
00880
00881
00882
00883
00884 public function getMassactionIdField()
00885 {
00886 return $this->_massactionIdField;
00887 }
00888
00889
00890
00891
00892
00893
00894
00895 public function setMassactionIdField($idField)
00896 {
00897 $this->_massactionIdField = $idField;
00898 return $this;
00899 }
00900
00901
00902
00903
00904
00905
00906 public function getMassactionBlockName()
00907 {
00908 return $this->_massactionBlockName;
00909 }
00910
00911
00912
00913
00914
00915
00916
00917 public function setMassactionBlockName($blockName)
00918 {
00919 $this->_massactionBlockName = $blockName;
00920 return $this;
00921 }
00922
00923
00924
00925
00926
00927
00928 public function getMassactionBlock()
00929 {
00930 return $this->getChild('massaction');
00931 }
00932
00933 public function getMassactionBlockHtml()
00934 {
00935 return $this->getChildHtml('massaction');
00936 }
00937
00938
00939
00940
00941
00942
00943
00944 public function setEmptyText($text)
00945 {
00946 $this->_emptyText = $text;
00947 return $this;
00948 }
00949
00950
00951
00952
00953
00954
00955 public function getEmptyText()
00956 {
00957 return $this->_emptyText;
00958 }
00959
00960
00961
00962
00963
00964
00965
00966 public function setEmptyTextClass($cssClass)
00967 {
00968 $this->_emptyTextCss = $text;
00969 return $this;
00970 }
00971
00972
00973
00974
00975
00976
00977 public function getEmptyTextClass()
00978 {
00979 return $this->_emptyTextCss;
00980 }
00981
00982
00983
00984
00985
00986
00987 public function setCountTotals($count=true)
00988 {
00989 $this->_countTotals = $count;
00990 }
00991
00992
00993
00994
00995
00996
00997 public function getCountTotals()
00998 {
00999 return $this->_countTotals;
01000 }
01001
01002
01003
01004
01005
01006
01007 public function setTotals(Varien_Object $totals)
01008 {
01009 $this->_varTotals = $totals;
01010 }
01011
01012
01013
01014
01015
01016
01017 public function getTotals()
01018 {
01019 return $this->_varTotals;
01020 }
01021
01022 }