/app/code/community/Ess/M2ePro/controllers/Adminhtml/Ebay/Template/SellingFormatController.php

https://github.com/expressdecor/Expressdecor · PHP · 177 lines · 119 code · 41 blank · 17 comment · 7 complexity · 36c9cd3281357c74d419efebfa4630ca MD5 · raw file

  1. <?php
  2. /*
  3. * @copyright Copyright (c) 2011 by ESS-UA.
  4. */
  5. class Ess_M2ePro_Adminhtml_Ebay_Template_SellingFormatController extends Ess_M2ePro_Controller_Adminhtml_MainController
  6. {
  7. //#############################################
  8. protected function _initAction()
  9. {
  10. $this->loadLayout()
  11. ->_setActiveMenu('m2epro/templates')
  12. ->_title(Mage::helper('M2ePro')->__('M2E Pro'))
  13. ->_title(Mage::helper('M2ePro')->__('Templates'))
  14. ->_title(Mage::helper('M2ePro')->__('Selling Format Templates'));
  15. $this->getLayout()->getBlock('head')
  16. ->addJs('M2ePro/Template/AttributeSetHandler.js')
  17. ->addJs('M2ePro/Ebay/Template/SellingFormatHandler.js');
  18. return $this;
  19. }
  20. protected function _isAllowed()
  21. {
  22. return Mage::getSingleton('admin/session')->isAllowed('m2epro/templates/selling_format');
  23. }
  24. //#############################################
  25. public function indexAction()
  26. {
  27. return $this->_redirect('*/adminhtml_template_sellingFormat/index');
  28. }
  29. //#############################################
  30. public function newAction()
  31. {
  32. $this->_forward('edit');
  33. }
  34. public function editAction()
  35. {
  36. $id = $this->getRequest()->getParam('id');
  37. $model = Mage::helper('M2ePro/Component_Ebay')->getModel('Template_SellingFormat')->load($id);
  38. if (!$model->getId() && $id) {
  39. $this->_getSession()->addError(Mage::helper('M2ePro')->__('Template does not exist'));
  40. return $this->_redirect('*/adminhtml_template_sellingFormat/index');
  41. }
  42. $temp = Ess_M2ePro_Model_AttributeSet::OBJECT_TYPE_TEMPLATE_SELLING_FORMAT;
  43. $templateAttributeSetsCollection = Mage::getModel('M2ePro/AttributeSet')->getCollection();
  44. $templateAttributeSetsCollection->addFieldToFilter('object_id', $id)
  45. ->addFieldToFilter('object_type', $temp);
  46. $templateAttributeSetsCollection->getSelect()->reset(Zend_Db_Select::COLUMNS)
  47. ->columns('attribute_set_id');
  48. $model->setData('attribute_sets', $templateAttributeSetsCollection->getColumnValues('attribute_set_id'));
  49. Mage::helper('M2ePro')->setGlobalValue('temp_data', $model);
  50. $this->_initAction()
  51. ->_addContent($this->getLayout()->createBlock('M2ePro/adminhtml_ebay_template_sellingFormat_edit'))
  52. ->renderLayout();
  53. }
  54. public function saveAction()
  55. {
  56. if (!$post = $this->getRequest()->getPost()) {
  57. return $this->_redirect('*/adminhtml_template_sellingFormat/index');
  58. }
  59. $id = $this->getRequest()->getParam('id');
  60. // Base prepare
  61. //--------------------
  62. $data = array();
  63. $keys = array(
  64. 'title',
  65. 'listing_type',
  66. 'listing_type_attribute',
  67. 'listing_is_private',
  68. 'duration_mode',
  69. 'duration_attribute',
  70. 'qty_mode',
  71. 'qty_custom_value',
  72. 'qty_custom_attribute',
  73. 'currency',
  74. 'price_variation_mode',
  75. 'start_price_mode',
  76. 'start_price_coefficient',
  77. 'start_price_custom_attribute',
  78. 'reserve_price_mode',
  79. 'reserve_price_coefficient',
  80. 'reserve_price_custom_attribute',
  81. 'buyitnow_price_mode',
  82. 'buyitnow_price_coefficient',
  83. 'buyitnow_price_custom_attribute',
  84. 'best_offer_mode',
  85. 'best_offer_accept_mode',
  86. 'best_offer_accept_value',
  87. 'best_offer_accept_attribute',
  88. 'best_offer_reject_mode',
  89. 'best_offer_reject_value',
  90. 'best_offer_reject_attribute'
  91. );
  92. foreach ($keys as $key) {
  93. if (isset($post[$key])) {
  94. $data[$key] = $post[$key];
  95. }
  96. }
  97. $data['title'] = strip_tags($data['title']);
  98. $data['start_price_coefficient'] = str_replace(',', '.', $data['start_price_coefficient']);
  99. $data['reserve_price_coefficient'] = str_replace(',', '.', $data['reserve_price_coefficient']);
  100. $data['buyitnow_price_coefficient'] = str_replace(',', '.', $data['buyitnow_price_coefficient']);
  101. //--------------------
  102. // Add or update model
  103. //--------------------
  104. $model = Mage::helper('M2ePro/Component_Ebay')->getModel('Template_SellingFormat');
  105. is_null($id) && $model->setData($data);
  106. !is_null($id) && $model->load($id)->addData($data);
  107. $id = $model->save()->getId();
  108. //--------------------
  109. // Attribute sets
  110. //--------------------
  111. $oldAttributeSets = Mage::getModel('M2ePro/AttributeSet')
  112. ->getCollection()
  113. ->addFieldToFilter('object_type',Ess_M2ePro_Model_AttributeSet::OBJECT_TYPE_TEMPLATE_SELLING_FORMAT)
  114. ->addFieldToFilter('object_id',(int)$id)
  115. ->getItems();
  116. foreach ($oldAttributeSets as $oldAttributeSet) {
  117. /** @var $oldAttributeSet Ess_M2ePro_Model_AttributeSet */
  118. $oldAttributeSet->deleteInstance();
  119. }
  120. if (!is_array($post['attribute_sets'])) {
  121. $post['attribute_sets'] = explode(',', $post['attribute_sets']);
  122. }
  123. foreach ($post['attribute_sets'] as $newAttributeSet) {
  124. $dataForAdd = array(
  125. 'object_type' => Ess_M2ePro_Model_AttributeSet::OBJECT_TYPE_TEMPLATE_SELLING_FORMAT,
  126. 'object_id' => (int)$id,
  127. 'attribute_set_id' => (int)$newAttributeSet
  128. );
  129. Mage::getModel('M2ePro/AttributeSet')->setData($dataForAdd)->save();
  130. }
  131. //--------------------
  132. $this->_getSession()->addSuccess(Mage::helper('M2ePro')->__('Template was successfully saved'));
  133. $this->_redirectUrl(Mage::helper('M2ePro')->getBackUrl('list',array(),array('edit'=>array('id'=>$id))));
  134. }
  135. //#############################################
  136. }