PageRenderTime 41ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/local/Ced/CsMarketplace/Block/Adminhtml/Vproducts/Grid.php

https://gitlab.com/vincent.perdereau/picandparts
PHP | 314 lines | 269 code | 29 blank | 16 comment | 15 complexity | e59cb02a10dc12b8e0541099b6344086 MD5 | raw file
  1. <?php
  2. /**
  3. * CedCommerce
  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. *
  12. * @category Ced
  13. * @package Ced_CsMarketplace
  14. * @author CedCommerce Core Team <coreteam@cedcommerce.com>
  15. * @copyright Copyright CedCommerce (http://cedcommerce.com/)
  16. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  17. */
  18. class Ced_CsMarketplace_Block_Adminhtml_Vproducts_Grid extends Mage_Adminhtml_Block_Widget_Grid
  19. {
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. $this->setId('productGrid');
  24. $this->setDefaultSort('entity_id');
  25. $this->setDefaultDir('DESC');
  26. $this->setSaveParametersInSession(true);
  27. $this->setUseAjax(true);
  28. }
  29. protected function _prepareMassaction()
  30. {
  31. $this->setMassactionIdField('entity_id');
  32. $this->getMassactionBlock()->setFormFieldName('entity_id');
  33. $this->getMassactionBlock()->addItem('delete', array(
  34. 'label' => Mage::helper('csmarketplace')->__('Delete'),
  35. 'url' => $this->getUrl('*/*/massDelete'),
  36. 'confirm' => Mage::helper('csmarketplace')->__('Are you sure?')
  37. ));
  38. $statuses = Mage::getSingleton('csmarketplace/vproducts')->getMassActionArray();
  39. $this->getMassactionBlock()->addItem('status', array(
  40. 'label'=> Mage::helper('csmarketplace')->__('Change status'),
  41. 'url' => $this->getUrl('*/*/massStatus/', array('_current'=>true)),
  42. 'additional' => array(
  43. 'visibility' => array(
  44. 'name' => 'status',
  45. 'type' => 'select',
  46. 'class' => 'required-entry',
  47. 'label' => Mage::helper('csmarketplace')->__('Status'),
  48. 'default'=>'-1',
  49. 'values' =>$statuses,
  50. )
  51. )
  52. ));
  53. return $this;
  54. }
  55. protected function _getStore()
  56. {
  57. $storeId = (int) $this->getRequest()->getParam('store', 0);
  58. return Mage::app()->getStore($storeId);
  59. }
  60. protected function _prepareCollection()
  61. {
  62. $vendor_id = $this->getRequest()->getParam('vendor_id',0);
  63. $allowedIds = array();
  64. if(Mage::registry('usePendingProductFilter')){
  65. $vproducts = Mage::getModel('csmarketplace/vproducts')->getVendorProducts(Ced_CsMarketplace_Model_Vproducts::PENDING_STATUS,0,0,-1);
  66. Mage::unregister('usePendingProductFilter');
  67. Mage::unregister('useApprovedProductFilter');
  68. } elseif(Mage::registry('useApprovedProductFilter') ){
  69. $vproducts = Mage::getModel('csmarketplace/vproducts')->getVendorProducts(Ced_CsMarketplace_Model_Vproducts::APPROVED_STATUS,0,0,-1);
  70. Mage::unregister('useApprovedProductFilter');
  71. Mage::unregister('usePendingProductFilter');
  72. } else {
  73. $vproducts = Mage::getModel('csmarketplace/vproducts')->getVendorProducts('',0,0,-1);
  74. }
  75. foreach($vproducts as $vproduct) {
  76. $allowedIds[] = $vproduct->getProductId();
  77. }
  78. $store = $this->_getStore();
  79. $collection = Mage::getModel('catalog/product')->getCollection()
  80. ->addAttributeToSelect('sku')
  81. ->addAttributeToSelect('name')
  82. ->addAttributeToSelect('attribute_set_id')
  83. ->addAttributeToSelect('type_id')
  84. ->addAttributeToFilter('entity_id',array('in'=>$allowedIds));
  85. if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
  86. $collection->joinField('qty',
  87. 'cataloginventory/stock_item',
  88. 'qty',
  89. 'product_id=entity_id',
  90. '{{table}}.stock_id=1',
  91. 'left');
  92. }
  93. if ($store->getId()) {
  94. $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
  95. $collection->addStoreFilter($store);
  96. $collection->joinAttribute(
  97. 'name',
  98. 'catalog_product/name',
  99. 'entity_id',
  100. null,
  101. 'inner',
  102. $adminStore
  103. );
  104. $collection->joinAttribute(
  105. 'custom_name',
  106. 'catalog_product/name',
  107. 'entity_id',
  108. null,
  109. 'inner',
  110. $store->getId()
  111. );
  112. $collection->joinAttribute(
  113. 'price',
  114. 'catalog_product/price',
  115. 'entity_id',
  116. null,
  117. 'left',
  118. $store->getId()
  119. );
  120. }
  121. else {
  122. $collection->addAttributeToSelect('price');
  123. }
  124. $collection->joinField('check_status','csmarketplace/vproducts', 'check_status','product_id=entity_id',null,'left');
  125. $collection->joinField('vendor_id','csmarketplace/vproducts', 'vendor_id','product_id=entity_id',null,'left');
  126. if($vendor_id) {
  127. $collection->addFieldToFilter('vendor_id',array('eq'=>$vendor_id));
  128. }
  129. $this->setCollection($collection);
  130. parent::_prepareCollection();
  131. $this->getCollection()->addWebsiteNamesToResult();
  132. return $this;
  133. }
  134. protected function _addColumnFilterToCollection($column)
  135. {
  136. if ($this->getCollection()) {
  137. if ($column->getId() == 'websites') {
  138. $this->getCollection()->joinField('websites',
  139. 'catalog/product_website',
  140. 'website_id',
  141. 'product_id=entity_id',
  142. null,
  143. 'left');
  144. }
  145. }
  146. return parent::_addColumnFilterToCollection($column);
  147. }
  148. protected function _prepareColumns()
  149. {
  150. $this->addColumn('entity_id',
  151. array(
  152. 'header'=> Mage::helper('catalog')->__('ID'),
  153. 'width' => '10px',
  154. 'type' => 'number',
  155. 'align' => 'left',
  156. 'index' => 'entity_id',
  157. ));
  158. $this->addColumn('name',
  159. array(
  160. 'header'=> Mage::helper('catalog')->__('Name'),
  161. 'index' => 'name',
  162. ));
  163. $this->addColumn('vendor_id',
  164. array(
  165. 'header' => Mage::helper('csmarketplace')->__('Vendor Name'),
  166. 'align' => 'left',
  167. 'width' => '100px',
  168. 'index' => 'vendor_id',
  169. 'renderer' => 'Ced_CsMarketplace_Block_Adminhtml_Vorders_Grid_Renderer_Vendorname',
  170. 'filter_condition_callback' => array($this, '_vendornameFilter'),
  171. ));
  172. $this->addColumn('type_id',
  173. array(
  174. 'header'=> Mage::helper('catalog')->__('Type'),
  175. 'width' => '60px',
  176. 'index' => 'type_id',
  177. 'type' => 'options',
  178. 'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
  179. ));
  180. $sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
  181. ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
  182. ->load()
  183. ->toOptionHash();
  184. $this->addColumn('set_name',
  185. array(
  186. 'header'=> Mage::helper('catalog')->__('Attrib. Set Name'),
  187. 'width' => '60px',
  188. 'index' => 'attribute_set_id',
  189. 'type' => 'options',
  190. 'options' => $sets,
  191. ));
  192. $this->addColumn('sku',
  193. array(
  194. 'header'=> Mage::helper('catalog')->__('SKU'),
  195. 'index' => 'sku',
  196. ));
  197. $store = $this->_getStore();
  198. $this->addColumn('price',
  199. array(
  200. 'header'=> Mage::helper('catalog')->__('Price'),
  201. 'type' => 'price',
  202. 'currency_code' => $store->getBaseCurrency()->getCode(),
  203. 'index' => 'price',
  204. ));
  205. if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
  206. $this->addColumn('qty',
  207. array(
  208. 'header'=> Mage::helper('catalog')->__('Qty'),
  209. 'width' => '50px',
  210. 'type' => 'number',
  211. 'index' => 'qty',
  212. ));
  213. }
  214. if (!Mage::app()->isSingleStoreMode()) {
  215. $this->addColumn('websites',
  216. array(
  217. 'header'=> Mage::helper('catalog')->__('Websites'),
  218. 'width' => '100px',
  219. 'sortable' => false,
  220. 'index' => 'websites',
  221. 'type' => 'options',
  222. 'options' => Mage::getModel('core/website')->getCollection()->toOptionHash(),
  223. ));
  224. }
  225. if(!Mage::registry('usePendingProductFilter')&&!Mage::registry('useApprovedProductFilter')){
  226. $this->addColumn('check_status',
  227. array(
  228. 'header'=> Mage::helper('catalog')->__('Status'),
  229. 'width' => '70px',
  230. 'index' => 'check_status',
  231. 'type' => 'options',
  232. 'options' => Mage::getSingleton('csmarketplace/vproducts')->getOptionArray(),
  233. ));
  234. }
  235. $this->addColumn('action',
  236. array(
  237. 'header' => Mage::helper('catalog')->__('Action'),
  238. 'type' => 'text',
  239. 'align' => 'center',
  240. 'filter' => false,
  241. 'sortable' => false,
  242. 'renderer'=>'csmarketplace/adminhtml_vproducts_renderer_action',
  243. 'index' => 'action',
  244. ));
  245. $this->addColumn('view',
  246. array(
  247. 'header' => Mage::helper('catalog')->__('View'),
  248. 'type' => 'text',
  249. 'align' => 'center',
  250. 'filter' => false,
  251. 'sortable' => false,
  252. 'renderer'=>'csmarketplace/adminhtml_vproducts_renderer_view',
  253. 'index' => 'view',
  254. ));
  255. return parent::_prepareColumns();
  256. }
  257. protected function _prepareLayout()
  258. {
  259. $this->getLayout()->getBlock('head')->addJs('ced/csmarketplace/adminhtml/popup.js');
  260. return parent::_prepareLayout();
  261. }
  262. protected function _vendornameFilter($collection, $column){
  263. if (!$value = $column->getFilter()->getValue()) {
  264. return $this;
  265. }
  266. $vendorIds = Mage::getModel('csmarketplace/vendor')->getCollection()
  267. ->addAttributeToFilter('name', array('like' => '%'.$column->getFilter()->getValue().'%'))
  268. ->getAllIds();
  269. if(count($vendorIds)>0)
  270. $this->getCollection()->addFieldToFilter('vendor_id', array('in', $vendorIds));
  271. else{
  272. $this->getCollection()->addFieldToFilter('vendor_id');
  273. }
  274. return $this;
  275. }
  276. public function getGridUrl() {
  277. if($this->getRequest()->getActionName()=="approved"){
  278. return $this->getUrl('*/adminhtml_vproducts/gridapproved', array('_secure'=>true, '_current'=>true));
  279. }
  280. elseif($this->getRequest()->getActionName()=="pending"){
  281. return $this->getUrl('*/adminhtml_vproducts/gridpending', array('_secure'=>true, '_current'=>true));
  282. }
  283. return $this->getUrl('*/adminhtml_vproducts/grid', array('_secure'=>true, '_current'=>true));
  284. }
  285. }