1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Adminhtml_Block_Widget_Grid extends Mage_Adminhtml_Block_Widget
35: {
36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49:
50: protected $_columns = array();
51:
52: protected $_lastColumnId;
53:
54: 55: 56: 57: 58:
59: protected $_collection = null;
60:
61: 62: 63: 64: 65:
66: protected $_varNameLimit = 'limit';
67: protected $_varNamePage = 'page';
68: protected $_varNameSort = 'sort';
69: protected $_varNameDir = 'dir';
70: protected $_varNameFilter = 'filter';
71:
72: protected $_defaultLimit = 20;
73: protected $_defaultPage = 1;
74: protected $_defaultSort = false;
75: protected $_defaultDir = 'desc';
76: protected $_defaultFilter = array();
77:
78: 79: 80: 81: 82:
83: protected $_isExport = false;
84:
85: 86: 87: 88: 89:
90: protected $_emptyText;
91:
92: 93: 94: 95: 96:
97: protected $_emptyTextCss = 'a-center';
98:
99: 100: 101: 102: 103:
104: protected = true;
105:
106: 107: 108: 109: 110:
111: protected = true;
112:
113: 114: 115: 116: 117:
118: protected $_filterVisibility = true;
119:
120: 121: 122: 123: 124:
125: protected $_messageBlockVisibility = false;
126:
127: protected $_saveParametersInSession = false;
128:
129: 130: 131: 132: 133:
134: protected $_countTotals = false;
135:
136: 137: 138: 139: 140:
141: protected $_countSubTotals = false;
142:
143: 144: 145: 146: 147:
148: protected $_varTotals;
149:
150: 151: 152: 153: 154:
155: protected $_subtotals = array();
156:
157: 158: 159: 160: 161:
162: protected $_exportTypes = array();
163:
164: 165: 166: 167: 168:
169: protected $_exportPageSize = 1000;
170:
171: 172: 173: 174: 175:
176: protected $_massactionIdField = null;
177:
178: 179: 180: 181: 182:
183: protected $_massactionIdFilter = null;
184:
185: 186: 187: 188: 189:
190: protected $_massactionBlockName = 'adminhtml/widget_grid_massaction';
191:
192: 193: 194: 195: 196:
197: protected = array();
198:
199: 200: 201: 202: 203:
204: protected $_columnsOrder = array();
205:
206: 207: 208: 209: 210:
211: protected $_groupedColumn = array();
212:
213: 214: 215: 216: 217:
218: protected $_emptyCellLabel = '';
219:
220: public function __construct($attributes=array())
221: {
222: parent::__construct($attributes);
223: $this->setTemplate('widget/grid.phtml');
224: $this->setRowClickCallback('openGridRow');
225: $this->_emptyText = Mage::helper('adminhtml')->__('No records found.');
226: }
227:
228: protected function _prepareLayout()
229: {
230: $this->setChild('export_button',
231: $this->getLayout()->createBlock('adminhtml/widget_button')
232: ->setData(array(
233: 'label' => Mage::helper('adminhtml')->__('Export'),
234: 'onclick' => $this->getJsObjectName().'.doExport()',
235: 'class' => 'task'
236: ))
237: );
238: $this->setChild('reset_filter_button',
239: $this->getLayout()->createBlock('adminhtml/widget_button')
240: ->setData(array(
241: 'label' => Mage::helper('adminhtml')->__('Reset Filter'),
242: 'onclick' => $this->getJsObjectName().'.resetFilter()',
243: ))
244: );
245: $this->setChild('search_button',
246: $this->getLayout()->createBlock('adminhtml/widget_button')
247: ->setData(array(
248: 'label' => Mage::helper('adminhtml')->__('Search'),
249: 'onclick' => $this->getJsObjectName().'.doFilter()',
250: 'class' => 'task'
251: ))
252: );
253: return parent::_prepareLayout();
254: }
255:
256: public function getExportButtonHtml()
257: {
258: return $this->getChildHtml('export_button');
259: }
260:
261: public function getResetFilterButtonHtml()
262: {
263: return $this->getChildHtml('reset_filter_button');
264: }
265:
266: public function getSearchButtonHtml()
267: {
268: return $this->getChildHtml('search_button');
269: }
270:
271: public function getMainButtonsHtml()
272: {
273: $html = '';
274: if($this->getFilterVisibility()){
275: $html.= $this->getResetFilterButtonHtml();
276: $html.= $this->getSearchButtonHtml();
277: }
278: return $html;
279: }
280:
281: 282: 283: 284: 285:
286:
287: public function setCollection($collection)
288: {
289: $this->_collection = $collection;
290: }
291:
292: 293: 294: 295: 296:
297: public function getCollection()
298: {
299: return $this->_collection;
300: }
301:
302: 303: 304: 305: 306: 307: 308:
309: public function addColumn($columnId, $column)
310: {
311: if (is_array($column)) {
312: $this->_columns[$columnId] = $this->getLayout()->createBlock('adminhtml/widget_grid_column')
313: ->setData($column)
314: ->setGrid($this);
315: }
316: 317: 318:
319: else {
320: throw new Exception(Mage::helper('adminhtml')->__('Wrong column format.'));
321: }
322:
323: $this->_columns[$columnId]->setId($columnId);
324: $this->_lastColumnId = $columnId;
325: return $this;
326: }
327:
328: 329: 330: 331: 332: 333:
334: public function removeColumn($columnId)
335: {
336: if (isset($this->_columns[$columnId])) {
337: unset($this->_columns[$columnId]);
338: if ($this->_lastColumnId == $columnId) {
339: $this->_lastColumnId = key($this->_columns);
340: }
341: }
342: return $this;
343: }
344:
345: 346: 347: 348: 349: 350: 351: 352:
353: public function addColumnAfter($columnId, $column, $after)
354: {
355: $this->addColumn($columnId, $column);
356: $this->addColumnsOrder($columnId, $after);
357: return $this;
358: }
359:
360: 361: 362: 363: 364: 365: 366:
367: public function addColumnsOrder($columnId, $after)
368: {
369: $this->_columnsOrder[$columnId] = $after;
370: return $this;
371: }
372:
373: 374: 375: 376: 377:
378: public function getColumnsOrder()
379: {
380: return $this->_columnsOrder;
381: }
382:
383: 384: 385: 386: 387:
388: public function sortColumnsByOrder()
389: {
390: $keys = array_keys($this->_columns);
391: $values = array_values($this->_columns);
392:
393: foreach ($this->getColumnsOrder() as $columnId => $after) {
394: if (array_search($after, $keys) !== false) {
395:
396: $positionCurrent = array_search($columnId, $keys);
397:
398: $key = array_splice($keys, $positionCurrent, 1);
399: $value = array_splice($values, $positionCurrent, 1);
400:
401: $positionTarget = array_search($after, $keys) + 1;
402:
403: array_splice($keys, $positionTarget, 0, $key);
404: array_splice($values, $positionTarget, 0, $value);
405:
406: $this->_columns = array_combine($keys, $values);
407: }
408: }
409:
410: end($this->_columns);
411: $this->_lastColumnId = key($this->_columns);
412: return $this;
413: }
414:
415: public function getLastColumnId()
416: {
417: return $this->_lastColumnId;
418: }
419:
420: public function getColumnCount()
421: {
422: return count($this->getColumns());
423: }
424:
425: 426: 427: 428: 429: 430:
431: public function getColumn($columnId)
432: {
433: if (!empty($this->_columns[$columnId])) {
434: return $this->_columns[$columnId];
435: }
436: return false;
437: }
438:
439: 440: 441: 442: 443:
444: public function getColumns()
445: {
446: return $this->_columns;
447: }
448:
449: protected function _setFilterValues($data)
450: {
451: foreach ($this->getColumns() as $columnId => $column) {
452: if (isset($data[$columnId])
453: && (!empty($data[$columnId]) || strlen($data[$columnId]) > 0)
454: && $column->getFilter()
455: ) {
456: $column->getFilter()->setValue($data[$columnId]);
457: $this->_addColumnFilterToCollection($column);
458: }
459: }
460: return $this;
461: }
462:
463: protected function _addColumnFilterToCollection($column)
464: {
465: if ($this->getCollection()) {
466: $field = ( $column->getFilterIndex() ) ? $column->getFilterIndex() : $column->getIndex();
467: if ($column->getFilterConditionCallback()) {
468: call_user_func($column->getFilterConditionCallback(), $this->getCollection(), $column);
469: } else {
470: $cond = $column->getFilter()->getCondition();
471: if ($field && isset($cond)) {
472: $this->getCollection()->addFieldToFilter($field , $cond);
473: }
474: }
475: }
476: return $this;
477: }
478:
479: 480: 481: 482: 483: 484:
485: protected function _setCollectionOrder($column)
486: {
487: $collection = $this->getCollection();
488: if ($collection) {
489: $columnIndex = $column->getFilterIndex() ?
490: $column->getFilterIndex() : $column->getIndex();
491: $collection->setOrder($columnIndex, strtoupper($column->getDir()));
492: }
493: return $this;
494: }
495:
496: 497: 498: 499: 500:
501: protected function _prepareCollection()
502: {
503: if ($this->getCollection()) {
504:
505: $this->_preparePage();
506:
507: $columnId = $this->getParam($this->getVarNameSort(), $this->_defaultSort);
508: $dir = $this->getParam($this->getVarNameDir(), $this->_defaultDir);
509: $filter = $this->getParam($this->getVarNameFilter(), null);
510:
511: if (is_null($filter)) {
512: $filter = $this->_defaultFilter;
513: }
514:
515: if (is_string($filter)) {
516: $data = $this->helper('adminhtml')->prepareFilterString($filter);
517: $this->_setFilterValues($data);
518: }
519: else if ($filter && is_array($filter)) {
520: $this->_setFilterValues($filter);
521: }
522: else if(0 !== sizeof($this->_defaultFilter)) {
523: $this->_setFilterValues($this->_defaultFilter);
524: }
525:
526: if (isset($this->_columns[$columnId]) && $this->_columns[$columnId]->getIndex()) {
527: $dir = (strtolower($dir)=='desc') ? 'desc' : 'asc';
528: $this->_columns[$columnId]->setDir($dir);
529: $this->_setCollectionOrder($this->_columns[$columnId]);
530: }
531:
532: if (!$this->_isExport) {
533: $this->getCollection()->load();
534: $this->_afterLoadCollection();
535: }
536: }
537:
538: return $this;
539: }
540:
541: 542: 543: 544: 545:
546: protected function _decodeFilter(&$value)
547: {
548: $value = $this->helper('adminhtml')->decodeFilter($value);
549: }
550:
551: protected function _preparePage()
552: {
553: $this->getCollection()->setPageSize((int) $this->getParam($this->getVarNameLimit(), $this->_defaultLimit));
554: $this->getCollection()->setCurPage((int) $this->getParam($this->getVarNamePage(), $this->_defaultPage));
555: }
556:
557: protected function _prepareColumns()
558: {
559: $this->sortColumnsByOrder();
560: return $this;
561: }
562:
563: 564: 565: 566: 567:
568: protected function _prepareMassactionBlock()
569: {
570: $this->setChild('massaction', $this->getLayout()->createBlock($this->getMassactionBlockName()));
571: $this->_prepareMassaction();
572: if($this->getMassactionBlock()->isAvailable()) {
573: $this->_prepareMassactionColumn();
574: }
575: return $this;
576: }
577:
578: 579: 580: 581: 582:
583: protected function _prepareMassaction()
584: {
585:
586: return $this;
587: }
588:
589: 590: 591: 592: 593:
594: protected function _prepareMassactionColumn()
595: {
596: $columnId = 'massaction';
597: $massactionColumn = $this->getLayout()->createBlock('adminhtml/widget_grid_column')
598: ->setData(array(
599: 'index' => $this->getMassactionIdField(),
600: 'filter_index' => $this->getMassactionIdFilter(),
601: 'type' => 'massaction',
602: 'name' => $this->getMassactionBlock()->getFormFieldName(),
603: 'align' => 'center',
604: 'is_system' => true
605: ));
606:
607: if ($this->getNoFilterMassactionColumn()) {
608: $massactionColumn->setData('filter', false);
609: }
610:
611: $massactionColumn->setSelected($this->getMassactionBlock()->getSelected())
612: ->setGrid($this)
613: ->setId($columnId);
614:
615: $oldColumns = $this->_columns;
616: $this->_columns = array();
617: $this->_columns[$columnId] = $massactionColumn;
618: $this->_columns = array_merge($this->_columns, $oldColumns);
619: return $this;
620: }
621:
622: protected function _prepareGrid()
623: {
624: $this->_prepareColumns();
625: $this->_prepareMassactionBlock();
626: $this->_prepareCollection();
627: return $this;
628: }
629:
630: protected function _beforeToHtml()
631: {
632: $this->_prepareGrid();
633: return parent::_beforeToHtml();
634: }
635:
636: protected function _afterLoadCollection()
637: {
638: return $this;
639: }
640:
641: public function getVarNameLimit()
642: {
643: return $this->_varNameLimit;
644: }
645:
646: public function getVarNamePage()
647: {
648: return $this->_varNamePage;
649: }
650:
651: public function getVarNameSort()
652: {
653: return $this->_varNameSort;
654: }
655:
656: public function getVarNameDir()
657: {
658: return $this->_varNameDir;
659: }
660:
661: public function getVarNameFilter()
662: {
663: return $this->_varNameFilter;
664: }
665:
666: public function setVarNameLimit($name)
667: {
668: return $this->_varNameLimit = $name;
669: }
670:
671: public function setVarNamePage($name)
672: {
673: return $this->_varNamePage = $name;
674: }
675:
676: public function setVarNameSort($name)
677: {
678: return $this->_varNameSort = $name;
679: }
680:
681: public function setVarNameDir($name)
682: {
683: return $this->_varNameDir = $name;
684: }
685:
686: public function setVarNameFilter($name)
687: {
688: return $this->_varNameFilter = $name;
689: }
690:
691: 692: 693: 694: 695:
696: public function ($visible=true)
697: {
698: $this->_headersVisibility = $visible;
699: }
700:
701: 702: 703: 704: 705:
706: public function ()
707: {
708: return $this->_headersVisibility;
709: }
710:
711: 712: 713: 714: 715:
716: public function ($visible=true)
717: {
718: $this->_pagerVisibility = $visible;
719: }
720:
721: 722: 723: 724: 725:
726: public function ()
727: {
728: return $this->_pagerVisibility;
729: }
730:
731: 732: 733: 734: 735:
736: public function setFilterVisibility($visible=true)
737: {
738: $this->_filterVisibility = $visible;
739: }
740:
741: 742: 743: 744: 745:
746: public function getFilterVisibility()
747: {
748: return $this->_filterVisibility;
749: }
750:
751: 752: 753: 754: 755:
756: public function setMessageBlockVisibility($visible=true)
757: {
758: $this->_messageBlockVisibility = $visible;
759: }
760:
761: 762: 763: 764: 765:
766: public function getMessageBlockVisibility()
767: {
768: return $this->_messageBlockVisibility;
769: }
770:
771: public function setDefaultLimit($limit)
772: {
773: $this->_defaultLimit = $limit;
774: return $this;
775: }
776:
777: public function setDefaultPage($page)
778: {
779: $this->_defaultPage = $page;
780: return $this;
781: }
782:
783: public function setDefaultSort($sort)
784: {
785: $this->_defaultSort = $sort;
786: return $this;
787: }
788:
789: public function setDefaultDir($dir)
790: {
791: $this->_defaultDir = $dir;
792: return $this;
793: }
794:
795: public function setDefaultFilter($filter)
796: {
797: $this->_defaultFilter = $filter;
798: return $this;
799: }
800:
801: 802: 803: 804: 805:
806: public function getExportTypes()
807: {
808: return empty($this->_exportTypes) ? false : $this->_exportTypes;
809: }
810:
811: 812: 813: 814: 815: 816: 817:
818: public function addExportType($url, $label)
819: {
820: $this->_exportTypes[] = new Varien_Object(
821: array(
822: 'url' => $this->getUrl($url, array('_current'=>true)),
823: 'label' => $label
824: )
825: );
826: return $this;
827: }
828:
829: 830: 831: 832: 833:
834: public function ()
835: {
836: return empty($this->_rssLists) ? false : $this->_rssLists;
837: }
838:
839: 840: 841: 842: 843: 844: 845:
846: protected function ($url)
847: {
848: $urlModel = Mage::getModel('core/url');
849: if (Mage::app()->getStore()->getStoreInUrl()) {
850:
851: $urlModel->setStore(Mage::app()->getDefaultStoreView());
852: }
853: return $urlModel->getUrl($url);
854: }
855:
856: 857: 858: 859: 860: 861: 862:
863: public function ($url, $label)
864: {
865: $this->_rssLists[] = new Varien_Object(
866: array(
867: 'url' => $this->_getRssUrl($url),
868: 'label' => $label
869: )
870: );
871: return $this;
872: }
873:
874: 875: 876: 877: 878:
879: public function getHtml()
880: {
881: return $this->toHtml();
882: }
883:
884: 885: 886: 887: 888: 889:
890: protected function _getFileContainerContent(array $fileData)
891: {
892: $io = new Varien_Io_File();
893: $path = $io->dirname($fileData['value']);
894: $io->open(array('path' => $path));
895: return $io->read($fileData['value']);
896: }
897:
898: 899: 900: 901: 902:
903: protected function ()
904: {
905: $row = array();
906: foreach ($this->_columns as $column) {
907: if (!$column->getIsSystem()) {
908: $row[] = $column->getExportHeader();
909: }
910: }
911: return $row;
912: }
913:
914: 915: 916: 917: 918:
919: protected function _getExportTotals()
920: {
921: $totals = $this->getTotals();
922: $row = array();
923: foreach ($this->_columns as $column) {
924: if (!$column->getIsSystem()) {
925: $row[] = ($column->hasTotalsLabel()) ? $column->getTotalsLabel() : $column->getRowFieldExport($totals);
926: }
927: }
928: return $row;
929: }
930:
931: 932: 933: 934: 935: 936: 937: 938:
939: public function _exportIterateCollection($callback, array $args)
940: {
941: $originalCollection = $this->getCollection();
942: $count = null;
943: $page = 1;
944: $lPage = null;
945: $break = false;
946:
947: while ($break !== true) {
948: $collection = clone $originalCollection;
949: $collection->setPageSize($this->_exportPageSize);
950: $collection->setCurPage($page);
951: $collection->load();
952: if (is_null($count)) {
953: $count = $collection->getSize();
954: $lPage = $collection->getLastPageNumber();
955: }
956: if ($lPage == $page) {
957: $break = true;
958: }
959: $page ++;
960:
961: foreach ($collection as $item) {
962: call_user_func_array(array($this, $callback), array_merge(array($item), $args));
963: }
964: }
965: }
966:
967: 968: 969: 970: 971: 972:
973: protected function _exportCsvItem(Varien_Object $item, Varien_Io_File $adapter)
974: {
975: $row = array();
976: foreach ($this->_columns as $column) {
977: if (!$column->getIsSystem()) {
978: $row[] = $column->getRowFieldExport($item);
979: }
980: }
981: $adapter->streamWriteCsv($row);
982: }
983:
984: 985: 986: 987: 988: 989: 990:
991: public function getCsvFile()
992: {
993: $this->_isExport = true;
994: $this->_prepareGrid();
995:
996: $io = new Varien_Io_File();
997:
998: $path = Mage::getBaseDir('var') . DS . 'export' . DS;
999: $name = md5(microtime());
1000: $file = $path . DS . $name . '.csv';
1001:
1002: $io->setAllowCreateFolders(true);
1003: $io->open(array('path' => $path));
1004: $io->streamOpen($file, 'w+');
1005: $io->streamLock(true);
1006: $io->streamWriteCsv($this->_getExportHeaders());
1007:
1008: $this->_exportIterateCollection('_exportCsvItem', array($io));
1009:
1010: if ($this->getCountTotals()) {
1011: $io->streamWriteCsv($this->_getExportTotals());
1012: }
1013:
1014: $io->streamUnlock();
1015: $io->streamClose();
1016:
1017: return array(
1018: 'type' => 'filename',
1019: 'value' => $file,
1020: 'rm' => true
1021: );
1022: }
1023:
1024: 1025: 1026: 1027: 1028:
1029: public function getCsv()
1030: {
1031: $csv = '';
1032: $this->_isExport = true;
1033: $this->_prepareGrid();
1034: $this->getCollection()->getSelect()->limit();
1035: $this->getCollection()->setPageSize(0);
1036: $this->getCollection()->load();
1037: $this->_afterLoadCollection();
1038:
1039: $data = array();
1040: foreach ($this->_columns as $column) {
1041: if (!$column->getIsSystem()) {
1042: $data[] = '"'.$column->getExportHeader().'"';
1043: }
1044: }
1045: $csv.= implode(',', $data)."\n";
1046:
1047: foreach ($this->getCollection() as $item) {
1048: $data = array();
1049: foreach ($this->_columns as $column) {
1050: if (!$column->getIsSystem()) {
1051: $data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'),
1052: $column->getRowFieldExport($item)) . '"';
1053: }
1054: }
1055: $csv.= implode(',', $data)."\n";
1056: }
1057:
1058: if ($this->getCountTotals())
1059: {
1060: $data = array();
1061: foreach ($this->_columns as $column) {
1062: if (!$column->getIsSystem()) {
1063: $data[] = '"' . str_replace(array('"', '\\'), array('""', '\\\\'),
1064: $column->getRowFieldExport($this->getTotals())) . '"';
1065: }
1066: }
1067: $csv.= implode(',', $data)."\n";
1068: }
1069:
1070: return $csv;
1071: }
1072:
1073: public function getXml()
1074: {
1075: $this->_isExport = true;
1076: $this->_prepareGrid();
1077: $this->getCollection()->getSelect()->limit();
1078: $this->getCollection()->setPageSize(0);
1079: $this->getCollection()->load();
1080: $this->_afterLoadCollection();
1081: $indexes = array();
1082: foreach ($this->_columns as $column) {
1083: if (!$column->getIsSystem()) {
1084: $indexes[] = $column->getIndex();
1085: }
1086: }
1087: $xml = '<?xml version="1.0" encoding="UTF-8"?>';
1088: $xml.= '<items>';
1089: foreach ($this->getCollection() as $item) {
1090: $xml.= $item->toXml($indexes);
1091: }
1092: if ($this->getCountTotals())
1093: {
1094: $xml.= $this->getTotals()->toXml($indexes);
1095: }
1096: $xml.= '</items>';
1097: return $xml;
1098: }
1099:
1100: 1101: 1102: 1103: 1104: 1105: 1106:
1107: protected function _exportExcelItem(Varien_Object $item, Varien_Io_File $adapter, $parser = null)
1108: {
1109: if (is_null($parser)) {
1110: $parser = new Varien_Convert_Parser_Xml_Excel();
1111: }
1112:
1113: $row = array();
1114: foreach ($this->_columns as $column) {
1115: if (!$column->getIsSystem()) {
1116: $row[] = $column->getRowFieldExport($item);
1117: }
1118: }
1119: $data = $parser->getRowXml($row);
1120: $adapter->streamWrite($data);
1121: }
1122:
1123: 1124: 1125: 1126: 1127: 1128: 1129:
1130: public function getExcelFile($sheetName = '')
1131: {
1132: $this->_isExport = true;
1133: $this->_prepareGrid();
1134:
1135: $parser = new Varien_Convert_Parser_Xml_Excel();
1136: $io = new Varien_Io_File();
1137:
1138: $path = Mage::getBaseDir('var') . DS . 'export' . DS;
1139: $name = md5(microtime());
1140: $file = $path . DS . $name . '.xml';
1141:
1142: $io->setAllowCreateFolders(true);
1143: $io->open(array('path' => $path));
1144: $io->streamOpen($file, 'w+');
1145: $io->streamLock(true);
1146: $io->streamWrite($parser->getHeaderXml($sheetName));
1147: $io->streamWrite($parser->getRowXml($this->_getExportHeaders()));
1148:
1149: $this->_exportIterateCollection('_exportExcelItem', array($io, $parser));
1150:
1151: if ($this->getCountTotals()) {
1152: $io->streamWrite($parser->getRowXml($this->_getExportTotals()));
1153: }
1154:
1155: $io->streamWrite($parser->getFooterXml());
1156: $io->streamUnlock();
1157: $io->streamClose();
1158:
1159: return array(
1160: 'type' => 'filename',
1161: 'value' => $file,
1162: 'rm' => true
1163: );
1164: }
1165:
1166: 1167: 1168: 1169: 1170: 1171:
1172: public function getExcel($filename = '')
1173: {
1174: $this->_isExport = true;
1175: $this->_prepareGrid();
1176: $this->getCollection()->getSelect()->limit();
1177: $this->getCollection()->setPageSize(0);
1178: $this->getCollection()->load();
1179: $this->_afterLoadCollection();
1180: $headers = array();
1181: $data = array();
1182: foreach ($this->_columns as $column) {
1183: if (!$column->getIsSystem()) {
1184: $headers[] = $column->getHeader();
1185: }
1186: }
1187: $data[] = $headers;
1188:
1189: foreach ($this->getCollection() as $item) {
1190: $row = array();
1191: foreach ($this->_columns as $column) {
1192: if (!$column->getIsSystem()) {
1193: $row[] = $column->getRowField($item);
1194: }
1195: }
1196: $data[] = $row;
1197: }
1198:
1199: if ($this->getCountTotals())
1200: {
1201: $row = array();
1202: foreach ($this->_columns as $column) {
1203: if (!$column->getIsSystem()) {
1204: $row[] = $column->getRowField($this->getTotals());
1205: }
1206: }
1207: $data[] = $row;
1208: }
1209:
1210: $xmlObj = new Varien_Convert_Parser_Xml_Excel();
1211: $xmlObj->setVar('single_sheet', $filename);
1212: $xmlObj->setData($data);
1213: $xmlObj->unparse();
1214:
1215: return $xmlObj->getData();
1216: }
1217:
1218: public function canDisplayContainer()
1219: {
1220: if ($this->getRequest()->getQuery('ajax')) {
1221: return false;
1222: }
1223: return true;
1224: }
1225:
1226: 1227: 1228: 1229: 1230: 1231: 1232:
1233: public function getGridUrl()
1234: {
1235: return $this->getCurrentUrl();
1236: }
1237:
1238: 1239: 1240: 1241: 1242: 1243: 1244:
1245: public function getAbsoluteGridUrl($params = array())
1246: {
1247: return $this->getCurrentUrl($params);
1248: }
1249:
1250: 1251: 1252: 1253: 1254: 1255: 1256:
1257: public function getParam($paramName, $default=null)
1258: {
1259: $session = Mage::getSingleton('adminhtml/session');
1260: $sessionParamName = $this->getId().$paramName;
1261: if ($this->getRequest()->has($paramName)) {
1262: $param = $this->getRequest()->getParam($paramName);
1263: if ($this->_saveParametersInSession) {
1264: $session->setData($sessionParamName, $param);
1265: }
1266: return $param;
1267: }
1268: elseif ($this->_saveParametersInSession && ($param = $session->getData($sessionParamName)))
1269: {
1270: return $param;
1271: }
1272:
1273: return $default;
1274: }
1275:
1276: public function setSaveParametersInSession($flag)
1277: {
1278: $this->_saveParametersInSession = $flag;
1279: return $this;
1280: }
1281:
1282: public function getJsObjectName()
1283: {
1284: return $this->getId().'JsObject';
1285: }
1286:
1287: 1288: 1289: 1290: 1291:
1292: public function getRowId($row)
1293: {
1294: return $this->getRowUrl($row);
1295: }
1296:
1297: 1298: 1299: 1300: 1301:
1302: public function getMassactionIdField()
1303: {
1304: return $this->_massactionIdField;
1305: }
1306:
1307: 1308: 1309: 1310: 1311: 1312:
1313: public function setMassactionIdField($idField)
1314: {
1315: $this->_massactionIdField = $idField;
1316: return $this;
1317: }
1318:
1319: 1320: 1321: 1322: 1323:
1324: public function getMassactionIdFilter()
1325: {
1326: return $this->_massactionIdFilter;
1327: }
1328:
1329: 1330: 1331: 1332: 1333: 1334:
1335: public function setMassactionIdFilter($idFilter)
1336: {
1337: $this->_massactionIdFilter = $idFilter;
1338: return $this;
1339: }
1340:
1341: 1342: 1343: 1344: 1345:
1346: public function getMassactionBlockName()
1347: {
1348: return $this->_massactionBlockName;
1349: }
1350:
1351: 1352: 1353: 1354: 1355: 1356:
1357: public function setMassactionBlockName($blockName)
1358: {
1359: $this->_massactionBlockName = $blockName;
1360: return $this;
1361: }
1362:
1363: 1364: 1365: 1366: 1367:
1368: public function getMassactionBlock()
1369: {
1370: return $this->getChild('massaction');
1371: }
1372:
1373: public function getMassactionBlockHtml()
1374: {
1375: return $this->getChildHtml('massaction');
1376: }
1377:
1378: 1379: 1380: 1381: 1382: 1383:
1384: public function setEmptyText($text)
1385: {
1386: $this->_emptyText = $text;
1387: return $this;
1388: }
1389:
1390: 1391: 1392: 1393: 1394:
1395: public function getEmptyText()
1396: {
1397: return $this->_emptyText;
1398: }
1399:
1400: 1401: 1402: 1403: 1404: 1405:
1406: public function setEmptyTextClass($cssClass)
1407: {
1408: $this->_emptyTextCss = $cssClass;
1409: return $this;
1410: }
1411:
1412: 1413: 1414: 1415: 1416:
1417: public function getEmptyTextClass()
1418: {
1419: return $this->_emptyTextCss;
1420: }
1421:
1422: 1423: 1424: 1425: 1426:
1427: public function setCountTotals($count=true)
1428: {
1429: $this->_countTotals = $count;
1430: }
1431:
1432: 1433: 1434: 1435: 1436:
1437: public function getCountTotals()
1438: {
1439: return $this->_countTotals;
1440: }
1441:
1442: 1443: 1444: 1445: 1446:
1447: public function setTotals(Varien_Object $totals)
1448: {
1449: $this->_varTotals = $totals;
1450: }
1451:
1452: 1453: 1454: 1455: 1456:
1457: public function getTotals()
1458: {
1459: return $this->_varTotals;
1460: }
1461:
1462: 1463: 1464: 1465: 1466: 1467:
1468: public function setCountSubTotals($flag = true)
1469: {
1470: $this->_countSubTotals = $flag;
1471: return $this;
1472: }
1473:
1474: 1475: 1476: 1477: 1478:
1479: public function getCountSubTotals()
1480: {
1481: return $this->_countSubTotals;
1482: }
1483:
1484: 1485: 1486: 1487: 1488: 1489:
1490: public function setSubTotals(array $items)
1491: {
1492: $this->_subtotals = $items;
1493: return $this;
1494: }
1495:
1496: 1497: 1498: 1499: 1500: 1501:
1502: public function getSubTotalItem($item)
1503: {
1504: foreach ($this->_subtotals as $subtotalItem) {
1505: foreach ($this->_groupedColumn as $groupedColumn) {
1506: if ($subtotalItem->getData($groupedColumn) == $item->getData($groupedColumn)) {
1507: return $subtotalItem;
1508: }
1509: }
1510: }
1511: return '';
1512: }
1513:
1514: 1515: 1516: 1517: 1518:
1519: public function getSubTotals()
1520: {
1521: return $this->_subtotals;
1522: }
1523:
1524: 1525: 1526: 1527: 1528: 1529:
1530: public function shouldRenderSubTotal($item) {
1531: return ($this->_countSubTotals && count($this->_subtotals) > 0 && count($this->getMultipleRows($item)) > 0);
1532: }
1533:
1534: 1535: 1536: 1537: 1538:
1539: public function getSubTotalColumns() {
1540: return $this->getColumns();
1541: }
1542:
1543: 1544: 1545: 1546: 1547: 1548: 1549:
1550: public function getRowspan($item, $column)
1551: {
1552: if ($this->isColumnGrouped($column)) {
1553: return count($this->getMultipleRows($item)) + count($this->_groupedColumn);
1554: }
1555: return false;
1556: }
1557:
1558: 1559: 1560: 1561: 1562: 1563: 1564:
1565: public function isColumnGrouped($column, $value = null)
1566: {
1567: if (null === $value) {
1568: if (is_object($column)) {
1569: return in_array($column->getIndex(), $this->_groupedColumn);
1570: }
1571: return in_array($column, $this->_groupedColumn);
1572: }
1573: $this->_groupedColumn[] = $column;
1574: return $this;
1575: }
1576:
1577: 1578: 1579: 1580: 1581: 1582:
1583: public function getMultipleRows($item)
1584: {
1585: return $item->getChildren();
1586: }
1587:
1588: 1589: 1590: 1591: 1592: 1593:
1594: public function getMultipleRowColumns()
1595: {
1596: $columns = $this->getColumns();
1597: foreach ($this->_groupedColumn as $column) {
1598: unset($columns[$column]);
1599: }
1600: return $columns;
1601: }
1602:
1603: 1604: 1605: 1606: 1607: 1608: 1609:
1610: public function shouldRenderCell($item, $column)
1611: {
1612: if ($this->isColumnGrouped($column) && $item->getIsEmpty()) {
1613: return true;
1614: }
1615: if (!$item->getIsEmpty()) {
1616: return true;
1617: }
1618: return false;
1619: }
1620:
1621: 1622: 1623: 1624: 1625: 1626: 1627:
1628: public function shouldRenderEmptyCell($item, $column)
1629: {
1630: return ($item->getIsEmpty() && in_array($column['index'], $this->_groupedColumn));
1631: }
1632:
1633: 1634: 1635: 1636: 1637: 1638:
1639: public function getEmptyCellColspan()
1640: {
1641: return $this->getColumnCount() - count($this->_groupedColumn);
1642: }
1643:
1644: 1645: 1646: 1647: 1648:
1649: public function getEmptyCellLabel()
1650: {
1651: return $this->_emptyCellLabel;
1652: }
1653:
1654: 1655: 1656: 1657: 1658: 1659:
1660: public function setEmptyCellLabel($label)
1661: {
1662: $this->_emptyCellLabel = $label;
1663: return $this;
1664: }
1665:
1666: 1667: 1668: 1669: 1670: 1671:
1672: public function getRowUrl($item)
1673: {
1674: $res = parent::getRowUrl($item);
1675: return ($res ? $res : '#');
1676: }
1677:
1678: }
1679: