PageRenderTime 63ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/app/code/core/Mage/Adminhtml/Block/Widget/Grid.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 1626 lines | 873 code | 178 blank | 575 comment | 65 complexity | e6fc43f4d7bdf539096637f06f2bf579 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Adminhtml
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Adminhtml grid widget block
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_Block_Widget_Grid extends Mage_Adminhtml_Block_Widget
  34. {
  35. /**
  36. * Columns array
  37. *
  38. * array(
  39. * 'header' => string,
  40. * 'width' => int,
  41. * 'sortable' => bool,
  42. * 'index' => string,
  43. * //'renderer' => Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Interface,
  44. * 'format' => string
  45. * 'total' => string (sum, avg)
  46. * )
  47. * @var array
  48. */
  49. protected $_columns = array();
  50. protected $_lastColumnId;
  51. /**
  52. * Collection object
  53. *
  54. * @var Varien_Data_Collection
  55. */
  56. protected $_collection = null;
  57. /**
  58. * Page and sorting var names
  59. *
  60. * @var string
  61. */
  62. protected $_varNameLimit = 'limit';
  63. protected $_varNamePage = 'page';
  64. protected $_varNameSort = 'sort';
  65. protected $_varNameDir = 'dir';
  66. protected $_varNameFilter = 'filter';
  67. protected $_defaultLimit = 20;
  68. protected $_defaultPage = 1;
  69. protected $_defaultSort = false;
  70. protected $_defaultDir = 'desc';
  71. protected $_defaultFilter = array();
  72. /**
  73. * Export flag
  74. *
  75. * @var bool
  76. */
  77. protected $_isExport = false;
  78. /**
  79. * Empty grid text
  80. *
  81. * @var sting|null
  82. */
  83. protected $_emptyText;
  84. /**
  85. * Empty grid text CSS class
  86. *
  87. * @var sting|null
  88. */
  89. protected $_emptyTextCss = 'a-center';
  90. /**
  91. * Pager visibility
  92. *
  93. * @var boolean
  94. */
  95. protected $_pagerVisibility = true;
  96. /**
  97. * Column headers visibility
  98. *
  99. * @var boolean
  100. */
  101. protected $_headersVisibility = true;
  102. /**
  103. * Filter visibility
  104. *
  105. * @var boolean
  106. */
  107. protected $_filterVisibility = true;
  108. /**
  109. * Massage block visibility
  110. *
  111. * @var boolean
  112. */
  113. protected $_messageBlockVisibility = false;
  114. protected $_saveParametersInSession = false;
  115. /**
  116. * Count totals
  117. *
  118. * @var boolean
  119. */
  120. protected $_countTotals = false;
  121. /**
  122. * Count subtotals
  123. *
  124. * @var boolean
  125. */
  126. protected $_countSubTotals = false;
  127. /**
  128. * Totals
  129. *
  130. * @var Varien_Object
  131. */
  132. protected $_varTotals;
  133. /**
  134. * SubTotals
  135. *
  136. * @var array
  137. */
  138. protected $_subtotals = array();
  139. /**
  140. * Grid export types
  141. *
  142. * @var array
  143. */
  144. protected $_exportTypes = array();
  145. /**
  146. * Rows per page for import
  147. *
  148. * @var int
  149. */
  150. protected $_exportPageSize = 1000;
  151. /**
  152. * Massaction row id field
  153. *
  154. * @var string
  155. */
  156. protected $_massactionIdField = null;
  157. /**
  158. * Massaction block name
  159. *
  160. * @var string
  161. */
  162. protected $_massactionBlockName = 'adminhtml/widget_grid_massaction';
  163. /**
  164. * RSS list
  165. *
  166. * @var array
  167. */
  168. protected $_rssLists = array();
  169. /**
  170. * Columns view order
  171. *
  172. * @var array
  173. */
  174. protected $_columnsOrder = array();
  175. /**
  176. * Columns to group by
  177. *
  178. * @var array
  179. */
  180. protected $_groupedColumn = array();
  181. /**
  182. * Label for empty cell
  183. *
  184. * @var string
  185. */
  186. protected $_emptyCellLabel = '';
  187. public function __construct($attributes=array())
  188. {
  189. parent::__construct($attributes);
  190. $this->setTemplate('widget/grid.phtml');
  191. $this->setRowClickCallback('openGridRow');
  192. $this->_emptyText = Mage::helper('adminhtml')->__('No records found.');
  193. }
  194. protected function _prepareLayout()
  195. {
  196. $this->setChild('export_button',
  197. $this->getLayout()->createBlock('adminhtml/widget_button')
  198. ->setData(array(
  199. 'label' => Mage::helper('adminhtml')->__('Export'),
  200. 'onclick' => $this->getJsObjectName().'.doExport()',
  201. 'class' => 'task'
  202. ))
  203. );
  204. $this->setChild('reset_filter_button',
  205. $this->getLayout()->createBlock('adminhtml/widget_button')
  206. ->setData(array(
  207. 'label' => Mage::helper('adminhtml')->__('Reset Filter'),
  208. 'onclick' => $this->getJsObjectName().'.resetFilter()',
  209. ))
  210. );
  211. $this->setChild('search_button',
  212. $this->getLayout()->createBlock('adminhtml/widget_button')
  213. ->setData(array(
  214. 'label' => Mage::helper('adminhtml')->__('Search'),
  215. 'onclick' => $this->getJsObjectName().'.doFilter()',
  216. 'class' => 'task'
  217. ))
  218. );
  219. return parent::_prepareLayout();
  220. }
  221. public function getExportButtonHtml()
  222. {
  223. return $this->getChildHtml('export_button');
  224. }
  225. public function getResetFilterButtonHtml()
  226. {
  227. return $this->getChildHtml('reset_filter_button');
  228. }
  229. public function getSearchButtonHtml()
  230. {
  231. return $this->getChildHtml('search_button');
  232. }
  233. public function getMainButtonsHtml()
  234. {
  235. $html = '';
  236. if($this->getFilterVisibility()){
  237. $html.= $this->getResetFilterButtonHtml();
  238. $html.= $this->getSearchButtonHtml();
  239. }
  240. return $html;
  241. }
  242. /**
  243. * set collection object
  244. *
  245. * @param Varien_Data_Collection $collection
  246. */
  247. //public function setCollection(Varien_Data_Collection $collection)
  248. public function setCollection($collection)
  249. {
  250. $this->_collection = $collection;
  251. }
  252. /**
  253. * get collection object
  254. *
  255. * @return Varien_Data_Collection
  256. */
  257. public function getCollection()
  258. {
  259. return $this->_collection;
  260. }
  261. /**
  262. * Add column to grid
  263. *
  264. * @param string $columnId
  265. * @param array || Varien_Object $column
  266. * @return Mage_Adminhtml_Block_Widget_Grid
  267. */
  268. public function addColumn($columnId, $column)
  269. {
  270. if (is_array($column)) {
  271. $this->_columns[$columnId] = $this->getLayout()->createBlock('adminhtml/widget_grid_column')
  272. ->setData($column)
  273. ->setGrid($this);
  274. }
  275. /*elseif ($column instanceof Varien_Object) {
  276. $this->_columns[$columnId] = $column;
  277. }*/
  278. else {
  279. throw new Exception(Mage::helper('adminhtml')->__('Wrong column format.'));
  280. }
  281. $this->_columns[$columnId]->setId($columnId);
  282. $this->_lastColumnId = $columnId;
  283. return $this;
  284. }
  285. /**
  286. * Add column to grid after specified column.
  287. *
  288. * @param string $columnId
  289. * @param array|Varien_Object $column
  290. * @param string $after
  291. * @return Mage_Adminhtml_Block_Widget_Grid
  292. */
  293. public function addColumnAfter($columnId, $column, $after)
  294. {
  295. $this->addColumn($columnId, $column);
  296. $this->addColumnsOrder($columnId, $after);
  297. return $this;
  298. }
  299. /**
  300. * Add column view order
  301. *
  302. * @param string $columnId
  303. * @param string $after
  304. * @return Mage_Adminhtml_Block_Widget_Grid
  305. */
  306. public function addColumnsOrder($columnId, $after)
  307. {
  308. $this->_columnsOrder[$columnId] = $after;
  309. return $this;
  310. }
  311. /**
  312. * Retrieve columns order
  313. *
  314. * @return array
  315. */
  316. public function getColumnsOrder()
  317. {
  318. return $this->_columnsOrder;
  319. }
  320. /**
  321. * Sort columns by predefined order
  322. *
  323. * @return Mage_Adminhtml_Block_Widget_Grid
  324. */
  325. public function sortColumnsByOrder()
  326. {
  327. $keys = array_keys($this->_columns);
  328. $values = array_values($this->_columns);
  329. foreach ($this->getColumnsOrder() as $columnId => $after) {
  330. if (array_search($after, $keys) !== false) {
  331. // Moving grid column
  332. $positionCurrent = array_search($columnId, $keys);
  333. $key = array_splice($keys, $positionCurrent, 1);
  334. $value = array_splice($values, $positionCurrent, 1);
  335. $positionTarget = array_search($after, $keys) + 1;
  336. array_splice($keys, $positionTarget, 0, $key);
  337. array_splice($values, $positionTarget, 0, $value);
  338. $this->_columns = array_combine($keys, $values);
  339. }
  340. }
  341. end($this->_columns);
  342. $this->_lastColumnId = key($this->_columns);
  343. return $this;
  344. }
  345. public function getLastColumnId()
  346. {
  347. return $this->_lastColumnId;
  348. }
  349. public function getColumnCount()
  350. {
  351. return count($this->getColumns());
  352. }
  353. /**
  354. * Retrieve grid column by column id
  355. *
  356. * @param string $columnId
  357. * @return Varien_Object || false
  358. */
  359. public function getColumn($columnId)
  360. {
  361. if (!empty($this->_columns[$columnId])) {
  362. return $this->_columns[$columnId];
  363. }
  364. return false;
  365. }
  366. /**
  367. * Retrieve all grid columns
  368. *
  369. * @return array
  370. */
  371. public function getColumns()
  372. {
  373. return $this->_columns;
  374. }
  375. protected function _setFilterValues($data)
  376. {
  377. foreach ($this->getColumns() as $columnId => $column) {
  378. if (isset($data[$columnId]) && (!empty($data[$columnId]) || strlen($data[$columnId]) > 0) && $column->getFilter()) {
  379. $column->getFilter()->setValue($data[$columnId]);
  380. $this->_addColumnFilterToCollection($column);
  381. }
  382. }
  383. return $this;
  384. }
  385. protected function _addColumnFilterToCollection($column)
  386. {
  387. if ($this->getCollection()) {
  388. $field = ( $column->getFilterIndex() ) ? $column->getFilterIndex() : $column->getIndex();
  389. if ($column->getFilterConditionCallback()) {
  390. call_user_func($column->getFilterConditionCallback(), $this->getCollection(), $column);
  391. } else {
  392. $cond = $column->getFilter()->getCondition();
  393. if ($field && isset($cond)) {
  394. $this->getCollection()->addFieldToFilter($field , $cond);
  395. }
  396. }
  397. }
  398. return $this;
  399. }
  400. /**
  401. * Sets sorting order by some column
  402. *
  403. * @param Mage_Adminhtml_Block_Widget_Grid_Column $column
  404. * @return Mage_Adminhtml_Block_Widget_Grid
  405. */
  406. protected function _setCollectionOrder($column)
  407. {
  408. $collection = $this->getCollection();
  409. if ($collection) {
  410. $columnIndex = $column->getFilterIndex() ?
  411. $column->getFilterIndex() : $column->getIndex();
  412. $collection->setOrder($columnIndex, $column->getDir());
  413. }
  414. return $this;
  415. }
  416. /**
  417. * Prepare grid collection object
  418. *
  419. * @return this
  420. */
  421. protected function _prepareCollection()
  422. {
  423. if ($this->getCollection()) {
  424. $this->_preparePage();
  425. $columnId = $this->getParam($this->getVarNameSort(), $this->_defaultSort);
  426. $dir = $this->getParam($this->getVarNameDir(), $this->_defaultDir);
  427. $filter = $this->getParam($this->getVarNameFilter(), null);
  428. if (is_null($filter)) {
  429. $filter = $this->_defaultFilter;
  430. }
  431. if (is_string($filter)) {
  432. $data = $this->helper('adminhtml')->prepareFilterString($filter);
  433. $this->_setFilterValues($data);
  434. }
  435. else if ($filter && is_array($filter)) {
  436. $this->_setFilterValues($filter);
  437. }
  438. else if(0 !== sizeof($this->_defaultFilter)) {
  439. $this->_setFilterValues($this->_defaultFilter);
  440. }
  441. if (isset($this->_columns[$columnId]) && $this->_columns[$columnId]->getIndex()) {
  442. $dir = (strtolower($dir)=='desc') ? 'desc' : 'asc';
  443. $this->_columns[$columnId]->setDir($dir);
  444. $this->_setCollectionOrder($this->_columns[$columnId]);
  445. }
  446. if (!$this->_isExport) {
  447. $this->getCollection()->load();
  448. $this->_afterLoadCollection();
  449. }
  450. }
  451. return $this;
  452. }
  453. /**
  454. * Decode URL encoded filter value recursive callback method
  455. *
  456. * @var string $value
  457. */
  458. protected function _decodeFilter(&$value)
  459. {
  460. $value = $this->helper('adminhtml')->decodeFilter($value);
  461. }
  462. protected function _preparePage()
  463. {
  464. $this->getCollection()->setPageSize($this->getParam($this->getVarNameLimit(), $this->_defaultLimit));
  465. $this->getCollection()->setCurPage($this->getParam($this->getVarNamePage(), $this->_defaultPage));
  466. }
  467. protected function _prepareColumns()
  468. {
  469. $this->sortColumnsByOrder();
  470. return $this;
  471. }
  472. /**
  473. * Prepare grid massaction block
  474. *
  475. * @return Mage_Adminhtml_Block_Widget_Grid
  476. */
  477. protected function _prepareMassactionBlock()
  478. {
  479. $this->setChild('massaction', $this->getLayout()->createBlock($this->getMassactionBlockName()));
  480. $this->_prepareMassaction();
  481. if($this->getMassactionBlock()->isAvailable()) {
  482. $this->_prepareMassactionColumn();
  483. }
  484. return $this;
  485. }
  486. /**
  487. * Prepare grid massaction actions
  488. *
  489. * @return Mage_Adminhtml_Block_Widget_Grid
  490. */
  491. protected function _prepareMassaction()
  492. {
  493. return $this;
  494. }
  495. /**
  496. * Prepare grid massaction column
  497. *
  498. * @return unknown
  499. */
  500. protected function _prepareMassactionColumn()
  501. {
  502. $columnId = 'massaction';
  503. $massactionColumn = $this->getLayout()->createBlock('adminhtml/widget_grid_column')
  504. ->setData(array(
  505. 'index' => $this->getMassactionIdField(),
  506. 'type' => 'massaction',
  507. 'name' => $this->getMassactionBlock()->getFormFieldName(),
  508. 'align' => 'center',
  509. 'is_system' => true
  510. ));
  511. if ($this->getNoFilterMassactionColumn()) {
  512. $massactionColumn->setData('filter', false);
  513. }
  514. $massactionColumn->setSelected($this->getMassactionBlock()->getSelected())
  515. ->setGrid($this)
  516. ->setId($columnId);
  517. $oldColumns = $this->_columns;
  518. $this->_columns = array();
  519. $this->_columns[$columnId] = $massactionColumn;
  520. $this->_columns = array_merge($this->_columns, $oldColumns);
  521. return $this;
  522. }
  523. protected function _prepareGrid()
  524. {
  525. $this->_prepareColumns();
  526. $this->_prepareMassactionBlock();
  527. $this->_prepareCollection();
  528. return $this;
  529. }
  530. protected function _beforeToHtml()
  531. {
  532. $this->_prepareGrid();
  533. return parent::_beforeToHtml();
  534. }
  535. protected function _afterLoadCollection()
  536. {
  537. return $this;
  538. }
  539. public function getVarNameLimit()
  540. {
  541. return $this->_varNameLimit;
  542. }
  543. public function getVarNamePage()
  544. {
  545. return $this->_varNamePage;
  546. }
  547. public function getVarNameSort()
  548. {
  549. return $this->_varNameSort;
  550. }
  551. public function getVarNameDir()
  552. {
  553. return $this->_varNameDir;
  554. }
  555. public function getVarNameFilter()
  556. {
  557. return $this->_varNameFilter;
  558. }
  559. public function setVarNameLimit($name)
  560. {
  561. return $this->_varNameLimit = $name;
  562. }
  563. public function setVarNamePage($name)
  564. {
  565. return $this->_varNamePage = $name;
  566. }
  567. public function setVarNameSort($name)
  568. {
  569. return $this->_varNameSort = $name;
  570. }
  571. public function setVarNameDir($name)
  572. {
  573. return $this->_varNameDir = $name;
  574. }
  575. public function setVarNameFilter($name)
  576. {
  577. return $this->_varNameFilter = $name;
  578. }
  579. /**
  580. * Set visibility of column headers
  581. *
  582. * @param boolean $visible
  583. */
  584. public function setHeadersVisibility($visible=true)
  585. {
  586. $this->_headersVisibility = $visible;
  587. }
  588. /**
  589. * Return visibility of column headers
  590. *
  591. * @return boolean
  592. */
  593. public function getHeadersVisibility()
  594. {
  595. return $this->_headersVisibility;
  596. }
  597. /**
  598. * Set visibility of pager
  599. *
  600. * @param boolean $visible
  601. */
  602. public function setPagerVisibility($visible=true)
  603. {
  604. $this->_pagerVisibility = $visible;
  605. }
  606. /**
  607. * Return visibility of pager
  608. *
  609. * @return boolean
  610. */
  611. public function getPagerVisibility()
  612. {
  613. return $this->_pagerVisibility;
  614. }
  615. /**
  616. * Set visibility of filter
  617. *
  618. * @param boolean $visible
  619. */
  620. public function setFilterVisibility($visible=true)
  621. {
  622. $this->_filterVisibility = $visible;
  623. }
  624. /**
  625. * Return visibility of filter
  626. *
  627. * @return boolean
  628. */
  629. public function getFilterVisibility()
  630. {
  631. return $this->_filterVisibility;
  632. }
  633. /**
  634. * Set visibility of filter
  635. *
  636. * @param boolean $visible
  637. */
  638. public function setMessageBlockVisibility($visible=true)
  639. {
  640. $this->_messageBlockVisibility = $visible;
  641. }
  642. /**
  643. * Return visibility of filter
  644. *
  645. * @return boolean
  646. */
  647. public function getMessageBlockVisibility()
  648. {
  649. return $this->_messageBlockVisibility;
  650. }
  651. public function setDefaultLimit($limit)
  652. {
  653. $this->_defaultLimit = $limit;
  654. return $this;
  655. }
  656. public function setDefaultPage($page)
  657. {
  658. $this->_defaultPage = $page;
  659. return $this;
  660. }
  661. public function setDefaultSort($sort)
  662. {
  663. $this->_defaultSort = $sort;
  664. return $this;
  665. }
  666. public function setDefaultDir($dir)
  667. {
  668. $this->_defaultDir = $dir;
  669. return $this;
  670. }
  671. public function setDefaultFilter($filter)
  672. {
  673. $this->_defaultFilter = $filter;
  674. return $this;
  675. }
  676. /**
  677. * Retrieve grid export types
  678. *
  679. * @return array
  680. */
  681. public function getExportTypes()
  682. {
  683. return empty($this->_exportTypes) ? false : $this->_exportTypes;
  684. }
  685. /**
  686. * Add new export type to grid
  687. *
  688. * @param string $url
  689. * @param string $label
  690. * @return Mage_Adminhtml_Block_Widget_Grid
  691. */
  692. public function addExportType($url, $label)
  693. {
  694. $this->_exportTypes[] = new Varien_Object(
  695. array(
  696. 'url' => $this->getUrl($url, array('_current'=>true)),
  697. 'label' => $label
  698. )
  699. );
  700. return $this;
  701. }
  702. /**
  703. * Retrieve rss lists types
  704. *
  705. * @return array
  706. */
  707. public function getRssLists()
  708. {
  709. return empty($this->_rssLists) ? false : $this->_rssLists;
  710. }
  711. /**
  712. * Returns url for RSS
  713. * Can be overloaded in descendant classes to perform custom changes to url passed to addRssList()
  714. *
  715. * @param string $url
  716. * @return string
  717. */
  718. protected function _getRssUrl($url)
  719. {
  720. $urlModel = Mage::getModel('core/url');
  721. if (Mage::app()->getStore()->getStoreInUrl()) {
  722. // Url in 'admin' store view won't be accessible, so form it in default store view frontend
  723. $urlModel->setStore(Mage::app()->getDefaultStoreView());
  724. }
  725. return $urlModel->getUrl($url);
  726. }
  727. /**
  728. * Add new rss list to grid
  729. *
  730. * @param string $url
  731. * @param string $label
  732. * @return Mage_Adminhtml_Block_Widget_Grid
  733. */
  734. public function addRssList($url, $label)
  735. {
  736. $this->_rssLists[] = new Varien_Object(
  737. array(
  738. 'url' => $this->_getRssUrl($url),
  739. 'label' => $label
  740. )
  741. );
  742. return $this;
  743. }
  744. /**
  745. * Retrieve grid HTML
  746. *
  747. * @return string
  748. */
  749. public function getHtml()
  750. {
  751. return $this->toHtml();
  752. }
  753. /**
  754. * Retrieve file content from file container array
  755. *
  756. * @param array $fileData
  757. * @return string
  758. */
  759. protected function _getFileContainerContent(array $fileData)
  760. {
  761. $io = new Varien_Io_File();
  762. $path = $io->dirname($fileData['value']);
  763. $io->open(array('path' => $path));
  764. return $io->read($fileData['value']);
  765. }
  766. /**
  767. * Retrieve Headers row array for Export
  768. *
  769. * @return array
  770. */
  771. protected function _getExportHeaders()
  772. {
  773. $row = array();
  774. foreach ($this->_columns as $column) {
  775. if (!$column->getIsSystem()) {
  776. $row[] = $column->getExportHeader();
  777. }
  778. }
  779. return $row;
  780. }
  781. /**
  782. * Retrieve Totals row array for Export
  783. *
  784. * @return array
  785. */
  786. protected function _getExportTotals()
  787. {
  788. $totals = $this->getTotals();
  789. $row = array();
  790. foreach ($this->_columns as $column) {
  791. if (!$column->getIsSystem()) {
  792. $row[] = ($column->hasTotalsLabel()) ? $column->getTotalsLabel() : $column->getRowFieldExport($totals);
  793. }
  794. }
  795. return $row;
  796. }
  797. /**
  798. * Iterate collection and call callback method per item
  799. * For callback method first argument always is item object
  800. *
  801. * @param string $callback
  802. * @param array $args additional arguments for callback method
  803. * @return Mage_Adminhtml_Block_Widget_Grid
  804. */
  805. public function _exportIterateCollection($callback, array $args)
  806. {
  807. $originalCollection = $this->getCollection();
  808. $count = null;
  809. $page = 1;
  810. $lPage = null;
  811. $break = false;
  812. while ($break !== true) {
  813. $collection = clone $originalCollection;
  814. $collection->setPageSize($this->_exportPageSize);
  815. $collection->setCurPage($page);
  816. $collection->load();
  817. if (is_null($count)) {
  818. $count = $collection->getSize();
  819. $lPage = $collection->getLastPageNumber();
  820. }
  821. if ($lPage == $page) {
  822. $break = true;
  823. }
  824. $page ++;
  825. foreach ($collection as $item) {
  826. call_user_func_array(array($this, $callback), array_merge(array($item), $args));
  827. }
  828. }
  829. }
  830. /**
  831. * Write item data to csv export file
  832. *
  833. * @param Varien_Object $item
  834. * @param Varien_Io_File $adapter
  835. */
  836. protected function _exportCsvItem(Varien_Object $item, Varien_Io_File $adapter)
  837. {
  838. $row = array();
  839. foreach ($this->_columns as $column) {
  840. if (!$column->getIsSystem()) {
  841. $row[] = $column->getRowFieldExport($item);
  842. }
  843. }
  844. $adapter->streamWriteCsv($row);
  845. }
  846. /**
  847. * Retrieve a file container array by grid data as CSV
  848. *
  849. * Return array with keys type and value
  850. *
  851. * @return array
  852. */
  853. public function getCsvFile()
  854. {
  855. $this->_isExport = true;
  856. $this->_prepareGrid();
  857. $io = new Varien_Io_File();
  858. $path = Mage::getBaseDir('var') . DS . 'export' . DS;
  859. $name = md5(microtime());
  860. $file = $path . DS . $name . '.csv';
  861. $io->setAllowCreateFolders(true);
  862. $io->open(array('path' => $path));
  863. $io->streamOpen($file, 'w+');
  864. $io->streamLock(true);
  865. $io->streamWriteCsv($this->_getExportHeaders());
  866. $this->_exportIterateCollection('_exportCsvItem', array($io));
  867. if ($this->getCountTotals()) {
  868. $io->streamWriteCsv($this->_getExportTotals());
  869. }
  870. $io->streamUnlock();
  871. $io->streamClose();
  872. return array(
  873. 'type' => 'filename',
  874. 'value' => $file,
  875. 'rm' => true // can delete file after use
  876. );
  877. }
  878. /**
  879. * Retrieve Grid data as CSV
  880. *
  881. * @return string
  882. */
  883. public function getCsv()
  884. {
  885. $csv = '';
  886. $this->_isExport = true;
  887. $this->_prepareGrid();
  888. $this->getCollection()->getSelect()->limit();
  889. $this->getCollection()->setPageSize(0);
  890. $this->getCollection()->load();
  891. $this->_afterLoadCollection();
  892. $data = array();
  893. foreach ($this->_columns as $column) {
  894. if (!$column->getIsSystem()) {
  895. $data[] = '"'.$column->getExportHeader().'"';
  896. }
  897. }
  898. $csv.= implode(',', $data)."\n";
  899. foreach ($this->getCollection() as $item) {
  900. $data = array();
  901. foreach ($this->_columns as $column) {
  902. if (!$column->getIsSystem()) {
  903. $data[] = '"'.str_replace(array('"', '\\'), array('""', '\\\\'), $column->getRowFieldExport($item)).'"';
  904. }
  905. }
  906. $csv.= implode(',', $data)."\n";
  907. }
  908. if ($this->getCountTotals())
  909. {
  910. $data = array();
  911. foreach ($this->_columns as $column) {
  912. if (!$column->getIsSystem()) {
  913. $data[] = '"'.str_replace(array('"', '\\'), array('""', '\\\\'), $column->getRowFieldExport($this->getTotals())).'"';
  914. }
  915. }
  916. $csv.= implode(',', $data)."\n";
  917. }
  918. return $csv;
  919. }
  920. public function getXml()
  921. {
  922. $this->_isExport = true;
  923. $this->_prepareGrid();
  924. $this->getCollection()->getSelect()->limit();
  925. $this->getCollection()->setPageSize(0);
  926. $this->getCollection()->load();
  927. $this->_afterLoadCollection();
  928. $indexes = array();
  929. foreach ($this->_columns as $column) {
  930. if (!$column->getIsSystem()) {
  931. $indexes[] = $column->getIndex();
  932. }
  933. }
  934. $xml = '<?xml version="1.0" encoding="UTF-8"?>';
  935. $xml.= '<items>';
  936. foreach ($this->getCollection() as $item) {
  937. $xml.= $item->toXml($indexes);
  938. }
  939. if ($this->getCountTotals())
  940. {
  941. $xml.= $this->getTotals()->toXml($indexes);
  942. }
  943. $xml.= '</items>';
  944. return $xml;
  945. }
  946. /**
  947. * Write item data to Excel 2003 XML export file
  948. *
  949. * @param Varien_Object $item
  950. * @param Varien_Io_File $adapter
  951. * @param Varien_Convert_Parser_Xml_Excel $parser
  952. */
  953. protected function _exportExcelItem(Varien_Object $item, Varien_Io_File $adapter, $parser = null)
  954. {
  955. if (is_null($parser)) {
  956. $parser = new Varien_Convert_Parser_Xml_Excel();
  957. }
  958. $row = array();
  959. foreach ($this->_columns as $column) {
  960. if (!$column->getIsSystem()) {
  961. $row[] = $column->getRowFieldExport($item);
  962. }
  963. }
  964. $data = $parser->getRowXml($row);
  965. $adapter->streamWrite($data);
  966. }
  967. /**
  968. * Retrieve a file container array by grid data as MS Excel 2003 XML Document
  969. *
  970. * Return array with keys type and value
  971. *
  972. * @return string
  973. */
  974. public function getExcelFile($sheetName = '')
  975. {
  976. $this->_isExport = true;
  977. $this->_prepareGrid();
  978. $parser = new Varien_Convert_Parser_Xml_Excel();
  979. $io = new Varien_Io_File();
  980. $path = Mage::getBaseDir('var') . DS . 'export' . DS;
  981. $name = md5(microtime());
  982. $file = $path . DS . $name . '.xml';
  983. $io->setAllowCreateFolders(true);
  984. $io->open(array('path' => $path));
  985. $io->streamOpen($file, 'w+');
  986. $io->streamLock(true);
  987. $io->streamWrite($parser->getHeaderXml($sheetName));
  988. $io->streamWrite($parser->getRowXml($this->_getExportHeaders()));
  989. $this->_exportIterateCollection('_exportExcelItem', array($io, $parser));
  990. if ($this->getCountTotals()) {
  991. $io->streamWrite($parser->getRowXml($this->_getExportTotals()));
  992. }
  993. $io->streamWrite($parser->getFooterXml());
  994. $io->streamUnlock();
  995. $io->streamClose();
  996. return array(
  997. 'type' => 'filename',
  998. 'value' => $file,
  999. 'rm' => true // can delete file after use
  1000. );
  1001. }
  1002. /**
  1003. * Retrieve grid data as MS Excel 2003 XML Document
  1004. *
  1005. * @param string $filename the Workbook sheet name
  1006. * @return string
  1007. */
  1008. public function getExcel($filename = '')
  1009. {
  1010. $this->_isExport = true;
  1011. $this->_prepareGrid();
  1012. $this->getCollection()->getSelect()->limit();
  1013. $this->getCollection()->setPageSize(0);
  1014. $this->getCollection()->load();
  1015. $this->_afterLoadCollection();
  1016. $headers = array();
  1017. $data = array();
  1018. foreach ($this->_columns as $column) {
  1019. if (!$column->getIsSystem()) {
  1020. $headers[] = $column->getHeader();
  1021. }
  1022. }
  1023. $data[] = $headers;
  1024. foreach ($this->getCollection() as $item) {
  1025. $row = array();
  1026. foreach ($this->_columns as $column) {
  1027. if (!$column->getIsSystem()) {
  1028. $row[] = $column->getRowField($item);
  1029. }
  1030. }
  1031. $data[] = $row;
  1032. }
  1033. if ($this->getCountTotals())
  1034. {
  1035. $row = array();
  1036. foreach ($this->_columns as $column) {
  1037. if (!$column->getIsSystem()) {
  1038. $row[] = $column->getRowField($this->getTotals());
  1039. }
  1040. }
  1041. $data[] = $row;
  1042. }
  1043. $xmlObj = new Varien_Convert_Parser_Xml_Excel();
  1044. $xmlObj->setVar('single_sheet', $filename);
  1045. $xmlObj->setData($data);
  1046. $xmlObj->unparse();
  1047. return $xmlObj->getData();
  1048. }
  1049. public function canDisplayContainer()
  1050. {
  1051. if ($this->getRequest()->getQuery('ajax')) {
  1052. return false;
  1053. }
  1054. return true;
  1055. }
  1056. /**
  1057. * Grid url getter
  1058. *
  1059. * @deprecated after 1.3.2.3 Use getAbsoluteGridUrl() method instead
  1060. *
  1061. * @return string current grid url
  1062. */
  1063. public function getGridUrl()
  1064. {
  1065. return $this->getCurrentUrl();
  1066. }
  1067. /**
  1068. * Grid url getter
  1069. * Version of getGridUrl() but with parameters
  1070. *
  1071. * @param array $params url parameters
  1072. * @return string current grid url
  1073. */
  1074. public function getAbsoluteGridUrl($params = array())
  1075. {
  1076. return $this->getCurrentUrl($params);
  1077. }
  1078. /**
  1079. * Retrieve grid
  1080. *
  1081. * @param string $paramName
  1082. * @param mixed $default
  1083. * @return mixed
  1084. */
  1085. public function getParam($paramName, $default=null)
  1086. {
  1087. $session = Mage::getSingleton('adminhtml/session');
  1088. $sessionParamName = $this->getId().$paramName;
  1089. if ($this->getRequest()->has($paramName)) {
  1090. $param = $this->getRequest()->getParam($paramName);
  1091. if ($this->_saveParametersInSession) {
  1092. $session->setData($sessionParamName, $param);
  1093. }
  1094. return $param;
  1095. }
  1096. elseif ($this->_saveParametersInSession && ($param = $session->getData($sessionParamName)))
  1097. {
  1098. return $param;
  1099. }
  1100. return $default;
  1101. }
  1102. public function setSaveParametersInSession($flag)
  1103. {
  1104. $this->_saveParametersInSession = $flag;
  1105. return $this;
  1106. }
  1107. public function getJsObjectName()
  1108. {
  1109. return $this->getId().'JsObject';
  1110. }
  1111. /**
  1112. * Deprecated since 1.1.7
  1113. *
  1114. * @return string
  1115. */
  1116. public function getRowId($row)
  1117. {
  1118. return $this->getRowUrl($row);
  1119. }
  1120. /**
  1121. * Retrive massaction row identifier field
  1122. *
  1123. * @return string
  1124. */
  1125. public function getMassactionIdField()
  1126. {
  1127. return $this->_massactionIdField;
  1128. }
  1129. /**
  1130. * Set massaction row identifier field
  1131. *
  1132. * @param string $idField
  1133. * @return Mage_Adminhtml_Block_Widget_Grid
  1134. */
  1135. public function setMassactionIdField($idField)
  1136. {
  1137. $this->_massactionIdField = $idField;
  1138. return $this;
  1139. }
  1140. /**
  1141. * Retrive massaction block name
  1142. *
  1143. * @return string
  1144. */
  1145. public function getMassactionBlockName()
  1146. {
  1147. return $this->_massactionBlockName;
  1148. }
  1149. /**
  1150. * Set massaction block name
  1151. *
  1152. * @param string $blockName
  1153. * @return Mage_Adminhtml_Block_Widget_Grid
  1154. */
  1155. public function setMassactionBlockName($blockName)
  1156. {
  1157. $this->_massactionBlockName = $blockName;
  1158. return $this;
  1159. }
  1160. /**
  1161. * Retrive massaction block
  1162. *
  1163. * @return Mage_Adminhtml_Block_Widget_Grid_Massaction_Abstract
  1164. */
  1165. public function getMassactionBlock()
  1166. {
  1167. return $this->getChild('massaction');
  1168. }
  1169. public function getMassactionBlockHtml()
  1170. {
  1171. return $this->getChildHtml('massaction');
  1172. }
  1173. /**
  1174. * Set empty text for grid
  1175. *
  1176. * @param string $text
  1177. * @return Mage_Adminhtml_Block_Widget_Grid
  1178. */
  1179. public function setEmptyText($text)
  1180. {
  1181. $this->_emptyText = $text;
  1182. return $this;
  1183. }
  1184. /**
  1185. * Return empty text for grid
  1186. *
  1187. * @return string
  1188. */
  1189. public function getEmptyText()
  1190. {
  1191. return $this->_emptyText;
  1192. }
  1193. /**
  1194. * Set empty text CSS class
  1195. *
  1196. * @param string $cssClass
  1197. * @return Mage_Adminhtml_Block_Widget_Grid
  1198. */
  1199. public function setEmptyTextClass($cssClass)
  1200. {
  1201. $this->_emptyTextCss = $cssClass;
  1202. return $this;
  1203. }
  1204. /**
  1205. * Return empty text CSS class
  1206. *
  1207. * @return string
  1208. */
  1209. public function getEmptyTextClass()
  1210. {
  1211. return $this->_emptyTextCss;
  1212. }
  1213. /**
  1214. * Set count totals
  1215. *
  1216. * @param boolean $visible
  1217. */
  1218. public function setCountTotals($count=true)
  1219. {
  1220. $this->_countTotals = $count;
  1221. }
  1222. /**
  1223. * Return count totals
  1224. *
  1225. * @return boolean
  1226. */
  1227. public function getCountTotals()
  1228. {
  1229. return $this->_countTotals;
  1230. }
  1231. /**
  1232. * Set totals
  1233. *
  1234. * @param boolean $visible
  1235. */
  1236. public function setTotals(Varien_Object $totals)
  1237. {
  1238. $this->_varTotals = $totals;
  1239. }
  1240. /**
  1241. * Retrieve totals
  1242. *
  1243. * @return Varien_Object
  1244. */
  1245. public function getTotals()
  1246. {
  1247. return $this->_varTotals;
  1248. }
  1249. /**
  1250. * Set subtotals
  1251. *
  1252. * @param boolean $flag
  1253. * @return Mage_Adminhtml_Block_Widget_Grid
  1254. */
  1255. public function setCountSubTotals($flag = true)
  1256. {
  1257. $this->_countSubTotals = $flag;
  1258. return $this;
  1259. }
  1260. /**
  1261. * Return count subtotals
  1262. *
  1263. * @return boolean
  1264. */
  1265. public function getCountSubTotals()
  1266. {
  1267. return $this->_countSubTotals;
  1268. }
  1269. /**
  1270. * Set subtotal items
  1271. *
  1272. * @param array $items
  1273. * @return Mage_Adminhtml_Block_Widget_Grid
  1274. */
  1275. public function setSubTotals(array $items)
  1276. {
  1277. $this->_subtotals = $items;
  1278. return $this;
  1279. }
  1280. /**
  1281. * Retrieve subtotal item
  1282. *
  1283. * @param Varien_Object $item
  1284. * @return Varien_Object
  1285. */
  1286. public function getSubTotalItem($item)
  1287. {
  1288. foreach ($this->_subtotals as $subtotalItem) {
  1289. foreach ($this->_groupedColumn as $groupedColumn) {
  1290. if ($subtotalItem->getData($groupedColumn) == $item->getData($groupedColumn)) {
  1291. return $subtotalItem;
  1292. }
  1293. }
  1294. }
  1295. return '';
  1296. }
  1297. /**
  1298. * Retrieve subtotal items
  1299. *
  1300. * @return array
  1301. */
  1302. public function getSubTotals()
  1303. {
  1304. return $this->_subtotals;
  1305. }
  1306. /**
  1307. * Check whether subtotal should be rendered
  1308. *
  1309. * @param Varien_Object $item
  1310. * @return boolean
  1311. */
  1312. public function shouldRenderSubTotal($item) {
  1313. return ($this->_countSubTotals && count($this->_subtotals) > 0 && count($this->getMultipleRows($item)) > 0);
  1314. }
  1315. /**
  1316. * Retrieve columns to render
  1317. *
  1318. * @return unknown
  1319. */
  1320. public function getSubTotalColumns() {
  1321. return $this->getColumns();
  1322. }
  1323. /**
  1324. * Retrieve rowspan number
  1325. *
  1326. * @param Varien_Object $item
  1327. * @param Mage_Adminhtml_Block_Widget_Grid_Column $column
  1328. * @return integer|boolean
  1329. */
  1330. public function getRowspan($item, $column)
  1331. {
  1332. if ($this->isColumnGrouped($column)) {
  1333. return count($this->getMultipleRows($item)) + count($this->_groupedColumn);
  1334. }
  1335. return false;
  1336. }
  1337. /**
  1338. * Enter description here...
  1339. *
  1340. * @param string|object $column
  1341. * @param string $value
  1342. * @return boolean|Mage_Adminhtml_Block_Widget_Grid
  1343. */
  1344. public function isColumnGrouped($column, $value = null)
  1345. {
  1346. if (null === $value) {
  1347. if (is_object($column)) {
  1348. return in_array($column->getIndex(), $this->_groupedColumn);
  1349. }
  1350. return in_array($column, $this->_groupedColumn);
  1351. }
  1352. $this->_groupedColumn[] = $column;
  1353. return $this;
  1354. }
  1355. /**
  1356. * Get children of specified item
  1357. *
  1358. * @param Varien_Object $item
  1359. * @return array
  1360. */
  1361. public function getMultipleRows($item)
  1362. {
  1363. return $item->getChildren();
  1364. }
  1365. /**
  1366. * Retrieve columns for multiple rows
  1367. *
  1368. * @param Varien_Object $item
  1369. * @return array
  1370. */
  1371. public function getMultipleRowColumns()
  1372. {
  1373. $columns = $this->getColumns();
  1374. foreach ($this->_groupedColumn as $column) {
  1375. unset($columns[$column]);
  1376. }
  1377. return $columns;
  1378. }
  1379. /**
  1380. * Check whether should render cell
  1381. *
  1382. * @param Varien_Object $item
  1383. * @param Mage_Adminhtml_Block_Widget_Grid_Column $column
  1384. * @return boolean
  1385. */
  1386. public function shouldRenderCell($item, $column)
  1387. {
  1388. if ($this->isColumnGrouped($column) && $item->getIsEmpty()) {
  1389. return true;
  1390. }
  1391. if (!$item->getIsEmpty()) {
  1392. return true;
  1393. }
  1394. return false;
  1395. }
  1396. /**
  1397. * Check whether should render empty cell
  1398. *
  1399. * @param Varien_Object $item
  1400. * @param Mage_Adminhtml_Block_Widget_Grid_Column $column
  1401. * @return boolean
  1402. */
  1403. public function shouldRenderEmptyCell($item, $column)
  1404. {
  1405. return ($item->getIsEmpty() && in_array($column['index'], $this->_groupedColumn));
  1406. }
  1407. /**
  1408. * Retrieve colspan for empty cell
  1409. *
  1410. * @param Varien_Object $item
  1411. * @return integer
  1412. */
  1413. public function getEmptyCellColspan()
  1414. {
  1415. return $this->getColumnCount() - count($this->_groupedColumn);
  1416. }
  1417. /**
  1418. * Retrieve label for empty cell
  1419. *
  1420. * @return string
  1421. */
  1422. public function getEmptyCellLabel()
  1423. {
  1424. return $this->_emptyCellLabel;
  1425. }
  1426. /**
  1427. * Set label for empty cell
  1428. *
  1429. * @param string $label
  1430. * @return Mage_Adminhtml_Block_Widget_Grid
  1431. */
  1432. public function setEmptyCellLabel($label)
  1433. {
  1434. $this->_emptyCellLabel = $label;
  1435. return $this;
  1436. }
  1437. /**
  1438. * Return row url for js event handlers
  1439. *
  1440. * @param Mage_Catalog_Model_Product|Varien_Object
  1441. * @return string
  1442. */
  1443. public function getRowUrl($item)
  1444. {
  1445. $res = parent::getRowUrl($item);
  1446. return ($res ? $res : '#');
  1447. }
  1448. }