PageRenderTime 91ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/community/Devinc/Groupdeals/Block/Adminhtml/Groupdeals/Edit.php

https://bitbucket.org/acidel/buykoala
PHP | 231 lines | 177 code | 28 blank | 26 comment | 37 complexity | aa8e341d54c4bf820829b0458ed9258f MD5 | raw file
  1. <?php
  2. class Devinc_Groupdeals_Block_Adminhtml_Groupdeals_Edit extends Mage_Adminhtml_Block_Widget_Form_Container
  3. {
  4. public function __construct()
  5. {
  6. parent::__construct();
  7. $this->_objectId = 'id';
  8. $this->_blockGroup = 'groupdeals';
  9. $this->_controller = 'adminhtml_groupdeals';
  10. $this->_updateButton('save', 'label', Mage::helper('groupdeals')->__('Save Deal'));
  11. $this->_updateButton('save', 'onclick', 'save()');
  12. $this->_updateButton('delete', 'label', Mage::helper('groupdeals')->__('Delete Deal'));
  13. $this->_updateButton('delete', 'onclick', 'deleteConfirm(\'Are you sure you want to do this?\', \''.$this->getUrl('*/*/delete', array('groupdeals_id' => $this->getRequest()->getParam('groupdeals_id'), 'id' => $this->getProductId())).'\')');
  14. $this->_removeButton('reset');
  15. $this->_addButton('saveandcontinue', array(
  16. 'label' => Mage::helper('adminhtml')->__('Save And Continue Edit'),
  17. 'onclick' => 'saveAndContinueEdit()',
  18. 'class' => 'save',
  19. ), -100);
  20. $storeId = $this->getRequest()->getParam('store', 0);
  21. if (($this->getRequest()->getParam('groupdeals_id')!=null || $this->getRequest()->getParam('groupdeals_id')!=0) && $this->getProduct()->getTypeId()=='virtual') {
  22. $data = Mage::registry('groupdeals_data')->getData();
  23. $_groupdeals = Mage::getModel('groupdeals/groupdeals')->load($this->getRequest()->getParam('groupdeals_id'));
  24. $_product = $this->getProduct();
  25. // check deal's status for all stores
  26. $stores = Mage::app()->getStores();
  27. $finished = true;
  28. foreach ($stores as $_eachStoreId => $val)
  29. {
  30. $_storeId = Mage::app()->getStore($_eachStoreId)->getId();
  31. $product = Mage::getModel('catalog/product')->setStoreId($_storeId)->load($this->getProductId());
  32. if ($product->getGroupdealStatus()==1 || $product->getGroupdealStatus()==0) {
  33. $finished = false;
  34. }
  35. }
  36. $sold_qty = Mage::getModel('groupdeals/groupdeals')->getGroupdealsSoldQty($_groupdeals);
  37. if (($_product->getGroupdealStatus()==4 || $_product->getGroupdealStatus()==2) && $sold_qty>=$data['minimum_qty']) {
  38. if ($finished) {
  39. $emailMessage = 'Are you sure you want to email the coupons to your customers?';
  40. } else {
  41. $emailMessage = 'The deal has not ended yet on all of the stores. Are you sure you want to email the coupons to your customers?';
  42. }
  43. } elseif (($_product->getGroupdealStatus()==4 || $_product->getGroupdealStatus()==2) && $sold_qty<$data['minimum_qty']) {
  44. if ($finished) {
  45. $emailMessage = 'The target was not met! Are you sure you want to email the coupons to your customers?';
  46. } else {
  47. $emailMessage = 'The target was not met and the deal has not ended yet on all of the stores. Are you sure you want to email the coupons to your customers?';
  48. }
  49. } else {
  50. $emailMessage = 'The deal has not ended yet! Are you sure you want to email the coupons to your customers?';
  51. }
  52. $this->_addButton('email_coupons', array(
  53. 'label' => Mage::helper('adminhtml')->__('Email Coupons'),
  54. 'onclick' => 'deleteConfirm(\''.$emailMessage.'\', \''.$this->getUrl('*/*/email', array('groupdeals_id' => $this->getRequest()->getParam('groupdeals_id'), 'id' => $this->getProductId())).'\')',
  55. ));
  56. $this->_addButton('email_merchants', array(
  57. 'label' => Mage::helper('adminhtml')->__('Email Coupons CSV to Merchant'),
  58. 'onclick' => 'emailMerchantCoupons()',
  59. ));
  60. $this->_addButton('preview_coupon', array(
  61. 'label' => Mage::helper('adminhtml')->__('Preview Coupon'),
  62. 'onclick' => 'previewCoupon()',
  63. ));
  64. $groupdeals_id = 'groupdeals_id/'.$this->getRequest()->getParam('groupdeals_id').'/';
  65. $product_id = 'id/'.$this->getProductId().'/';
  66. } elseif (($this->getRequest()->getParam('groupdeals_id')!=null || $this->getRequest()->getParam('groupdeals_id')!=0) && ($this->getProduct()->getTypeId()=='simple' || $this->getProduct()->getTypeId()=='configurable')) {
  67. $data = Mage::registry('groupdeals_data')->getData();
  68. $groupdeals_id = 'groupdeals_id/'.$this->getRequest()->getParam('groupdeals_id').'/';
  69. $product_id = 'id/'.$this->getProductId().'/';
  70. } else {
  71. $groupdeals_id = '';
  72. $product_id = '';
  73. }
  74. //$attribute_set_id = Mage::getStoreConfig('groupdeals/attribute_set_id');
  75. $attribute_set_id = Mage::getModel('eav/entity_attribute_set')->getCollection()->addFieldToFilter('attribute_set_name','Group Deal')->getFirstItem()->getId();
  76. $type = $this->getRequest()->getParam('type', null);
  77. $this->_formScripts[] = "
  78. function setDealSettings() {
  79. setLocation('".$this->getUrl('*/*/new')."'+'set/".$attribute_set_id."/type/'+document.getElementById('product_type').value+'/');
  80. }
  81. function setSettings(urlTemplate, setElement, typeElement) {
  82. var template = new Template(urlTemplate, productTemplateSyntax);
  83. setLocation(template.evaluate({attribute_set:$(setElement),type:$(typeElement)}));
  84. }
  85. function setSuperSettings(urlTemplate, attributesClass, validateField) {
  86. var attributesFields = $$('.' + attributesClass);
  87. var attributes = Form.serializeElements(attributesFields, true).attribute;
  88. if(typeof attributes == 'string') {
  89. attributes = [attributes];
  90. }
  91. if(!attributes) {
  92. $(validateField).value = 'no-attributes';
  93. } else {
  94. $(validateField).value = 'has-attributes';
  95. }
  96. if (editForm.validator.validate()) {
  97. setLocation('".$this->getUrl('*/*/new')."'+'attributes/'+encode_base64(attributes.join(',')).replace(new RegExp('/','g'),'%2F').replace(new RegExp('=','g'),'%3D')+'/set/".$this->getRequest()->getParam('set', $attribute_set_id)."/type/".$this->getRequest()->getParam('type', 'configurable')."/');
  98. }
  99. }
  100. function save(){
  101. editForm.submit('".$this->getUrl('*/*/save').$groupdeals_id.$product_id."type/".$type."/set/".$attribute_set_id."/store/".$storeId."/');
  102. }
  103. function saveAndContinueEdit(){
  104. editForm.submit('".$this->getUrl('*/*/save').$groupdeals_id.$product_id."type/".$type."/set/".$attribute_set_id."/store/".$storeId."/back/edit/');
  105. }
  106. function emailMerchantCoupons(){
  107. editForm.submit('".$this->getUrl('groupdeals/adminhtml_groupdeals/emailMerchant').$groupdeals_id.$product_id."');
  108. }
  109. function previewCoupon(){
  110. return window.open('".$this->getUrl('groupdeals/adminhtml_groupdeals/preview').$groupdeals_id.$product_id."store/".$storeId."/');
  111. }
  112. function checkMaxLength(Object, MaxLen)
  113. {
  114. if (Object.value.length > MaxLen-1) {
  115. Object.value = Object.value.substr(0, MaxLen);
  116. }
  117. return 1;
  118. }
  119. function regionReload(value)
  120. {
  121. new Ajax.Updater('region_container', '".$this->getUrl('groupdealsadmin/adminhtml_groupdeals/region')."?country='+value, {
  122. asynchronous:true, evalScripts:true,
  123. onComplete:function(request, json){
  124. if (document.getElementById('country_id').value!='') { document.getElementById('city').disabled = '';document.getElementById('city').value = ''; }
  125. else { document.getElementById('city').value = 'Select a country to enter city'; document.getElementById('city').disabled = 'disabled'; document.getElementById('region').value = 'Select a country to enter state/province'; document.getElementById('region').disabled = 'disabled'; }
  126. },
  127. onLoading:function(request, json){}});
  128. return false;
  129. }
  130. ";
  131. if ($this->getRequest()->getParam('groupdeals_id')!=null || $this->getRequest()->getParam('groupdeals_id')!=0) {
  132. if ($data['country_id']=='') {
  133. $this->_formScripts[] .= "
  134. document.getElementById('region').value = 'Select a country to enter state/province';
  135. document.getElementById('city').value = 'Select a country to enter city';
  136. document.getElementById('region').disabled = 'disabled';
  137. document.getElementById('city').disabled = 'disabled';
  138. ";
  139. }
  140. if ($data['coupon_expiration_date']=='0000-00-00') {
  141. $this->_formScripts[] .= "
  142. document.getElementById('coupon_expiration_date').value = '';
  143. ";
  144. }
  145. } else {
  146. $this->_formScripts[] .= "
  147. document.getElementById('region').value = 'Select a country to enter state/province';
  148. document.getElementById('city').value = 'Select a country to enter city';
  149. document.getElementById('region').disabled = 'disabled';
  150. document.getElementById('city').disabled = 'disabled';
  151. ";
  152. }
  153. if (Mage::getModel('groupdeals/merchants')->isMerchant() && Mage::getModel('groupdeals/merchants')->getPermission('add_edit')==0) {
  154. $this->_removeButton('save');
  155. $this->_removeButton('saveandcontinue');
  156. $this->_removeButton('email_coupons');
  157. $this->_removeButton('email_merchants');
  158. $this->_removeButton('preview_coupon');
  159. }
  160. if (Mage::getModel('groupdeals/merchants')->isMerchant() && Mage::getModel('groupdeals/merchants')->getPermission('delete')==0) {
  161. $this->_removeButton('delete');
  162. }
  163. }
  164. public function getHeaderText()
  165. {
  166. if(Mage::registry('groupdeals_data') && Mage::registry('groupdeals_data')->getId()) {
  167. if (!$this->getProduct()->getGroupdealType()) {
  168. $dealType = 'DEAL';
  169. } else {
  170. switch ($this->getProduct()->getGroupdealType()) {
  171. case 1:
  172. $dealType = 'MAIN DEAL';
  173. break;
  174. case 2:
  175. $dealType = 'SIDE DEAL';
  176. break;
  177. case 3:
  178. $dealType = 'RECENT DEAL';
  179. break;
  180. case 0:
  181. $dealType = 'FUTURE DEAL';
  182. break;
  183. }
  184. }
  185. return $dealType.' - '.$this->htmlEscape($this->getProduct()->getName());
  186. } else {
  187. return Mage::helper('groupdeals')->__('New Deal');
  188. }
  189. }
  190. public function getProduct()
  191. {
  192. return Mage::registry('current_product');
  193. }
  194. public function getProductId()
  195. {
  196. return $this->getProduct()->getId();
  197. }
  198. }