PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Downloadable/Block/Adminhtml/Catalog/Product/Edit/Tab/Downloadable/Links.php

https://bitbucket.org/acidel/buykoala
PHP | 305 lines | 173 code | 22 blank | 110 comment | 16 complexity | 948a82ee24708a2f0bcd13a0ebfe3814 MD5 | raw file
  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_Downloadable
  23. * @copyright Copyright (c) 2011 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 catalog product downloadable items tab links section
  28. *
  29. * @category Mage
  30. * @package Mage_Downloadable
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Downloadable_Block_Adminhtml_Catalog_Product_Edit_Tab_Downloadable_Links
  34. extends Mage_Adminhtml_Block_Template
  35. {
  36. /**
  37. * Purchased Separately Attribute cache
  38. *
  39. * @var Mage_Catalog_Model_Resource_Eav_Attribute
  40. */
  41. protected $_purchasedSeparatelyAttribute = null;
  42. /**
  43. * Class constructor
  44. *
  45. */
  46. public function __construct()
  47. {
  48. parent::__construct();
  49. $this->setTemplate('downloadable/product/edit/downloadable/links.phtml');
  50. $this->setCanEditPrice(true);
  51. $this->setCanReadPrice(true);
  52. }
  53. /**
  54. * Get product that is being edited
  55. *
  56. * @return Mage_Catalog_Model_Product
  57. */
  58. public function getProduct()
  59. {
  60. return Mage::registry('product');
  61. }
  62. /**
  63. * Retrieve Purchased Separately Attribute object
  64. *
  65. * @return Mage_Catalog_Model_Resource_Eav_Attribute
  66. */
  67. public function getPurchasedSeparatelyAttribute()
  68. {
  69. if (is_null($this->_purchasedSeparatelyAttribute)) {
  70. $_attributeCode = 'links_purchased_separately';
  71. $this->_purchasedSeparatelyAttribute = Mage::getModel('eav/entity_attribute')
  72. ->loadByCode(Mage_Catalog_Model_Product::ENTITY, $_attributeCode);
  73. }
  74. return $this->_purchasedSeparatelyAttribute;
  75. }
  76. /**
  77. * Retrieve Purchased Separately HTML select
  78. *
  79. * @return string
  80. */
  81. public function getPurchasedSeparatelySelect()
  82. {
  83. $select = $this->getLayout()->createBlock('adminhtml/html_select')
  84. ->setName('product[links_purchased_separately]')
  85. ->setId('downloadable_link_purchase_type')
  86. ->setOptions(Mage::getSingleton('adminhtml/system_config_source_yesno')->toOptionArray())
  87. ->setValue($this->getProduct()->getLinksPurchasedSeparately());
  88. return $select->getHtml();
  89. }
  90. /**
  91. * Retrieve Add button HTML
  92. *
  93. * @return string
  94. */
  95. public function getAddButtonHtml()
  96. {
  97. $addButton = $this->getLayout()->createBlock('adminhtml/widget_button')
  98. ->setData(array(
  99. 'label' => Mage::helper('downloadable')->__('Add New Row'),
  100. 'id' => 'add_link_item',
  101. 'class' => 'add'
  102. ));
  103. return $addButton->toHtml();
  104. }
  105. /**
  106. * Retrieve default links title
  107. *
  108. * @return string
  109. */
  110. public function getLinksTitle()
  111. {
  112. return Mage::getStoreConfig(Mage_Downloadable_Model_Link::XML_PATH_LINKS_TITLE);
  113. }
  114. /**
  115. * Check exists defined links title
  116. *
  117. * @return bool
  118. */
  119. public function getUsedDefault()
  120. {
  121. return $this->getProduct()->getAttributeDefaultValue('links_title') === false;
  122. }
  123. /**
  124. * Return true if price in website scope
  125. *
  126. * @return bool
  127. */
  128. public function getIsPriceWebsiteScope()
  129. {
  130. $scope = (int) Mage::app()->getStore()->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE);
  131. if ($scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE) {
  132. return true;
  133. }
  134. return false;
  135. }
  136. /**
  137. * Return array of links
  138. *
  139. * @return array
  140. */
  141. public function getLinkData()
  142. {
  143. $linkArr = array();
  144. $links = $this->getProduct()->getTypeInstance(true)->getLinks($this->getProduct());
  145. $priceWebsiteScope = $this->getIsPriceWebsiteScope();
  146. foreach ($links as $item) {
  147. $tmpLinkItem = array(
  148. 'link_id' => $item->getId(),
  149. 'title' => $item->getTitle(),
  150. 'price' => $this->getCanReadPrice() ? $this->getPriceValue($item->getPrice()) : '',
  151. 'number_of_downloads' => $item->getNumberOfDownloads(),
  152. 'is_shareable' => $item->getIsShareable(),
  153. 'link_url' => $item->getLinkUrl(),
  154. 'link_type' => $item->getLinkType(),
  155. 'sample_file' => $item->getSampleFile(),
  156. 'sample_url' => $item->getSampleUrl(),
  157. 'sample_type' => $item->getSampleType(),
  158. 'sort_order' => $item->getSortOrder(),
  159. );
  160. $file = Mage::helper('downloadable/file')->getFilePath(
  161. Mage_Downloadable_Model_Link::getBasePath(), $item->getLinkFile()
  162. );
  163. if ($item->getLinkFile() && !is_file($file)) {
  164. Mage::helper('core/file_storage_database')->saveFileToFilesystem($file);
  165. }
  166. if ($item->getLinkFile() && is_file($file)) {
  167. $name = '<a href="'
  168. . $this->getUrl('*/downloadable_product_edit/link', array(
  169. 'id' => $item->getId(),
  170. '_secure' => true
  171. )) . '">' . Mage::helper('downloadable/file')->getFileFromPathFile($item->getLinkFile()) . '</a>';
  172. $tmpLinkItem['file_save'] = array(
  173. array(
  174. 'file' => $item->getLinkFile(),
  175. 'name' => $name,
  176. 'size' => filesize($file),
  177. 'status' => 'old'
  178. ));
  179. }
  180. $sampleFile = Mage::helper('downloadable/file')->getFilePath(
  181. Mage_Downloadable_Model_Link::getBaseSamplePath(), $item->getSampleFile()
  182. );
  183. if ($item->getSampleFile() && is_file($sampleFile)) {
  184. $tmpLinkItem['sample_file_save'] = array(
  185. array(
  186. 'file' => $item->getSampleFile(),
  187. 'name' => Mage::helper('downloadable/file')->getFileFromPathFile($item->getSampleFile()),
  188. 'size' => filesize($sampleFile),
  189. 'status' => 'old'
  190. ));
  191. }
  192. if ($item->getNumberOfDownloads() == '0') {
  193. $tmpLinkItem['is_unlimited'] = ' checked="checked"';
  194. }
  195. if ($this->getProduct()->getStoreId() && $item->getStoreTitle()) {
  196. $tmpLinkItem['store_title'] = $item->getStoreTitle();
  197. }
  198. if ($this->getProduct()->getStoreId() && $priceWebsiteScope) {
  199. $tmpLinkItem['website_price'] = $item->getWebsitePrice();
  200. }
  201. $linkArr[] = new Varien_Object($tmpLinkItem);
  202. }
  203. return $linkArr;
  204. }
  205. /**
  206. * Return formated price with two digits after decimal point
  207. *
  208. * @param decimal $value
  209. * @return decimal
  210. */
  211. public function getPriceValue($value)
  212. {
  213. return number_format($value, 2, null, '');
  214. }
  215. /**
  216. * Retrieve max downloads value from config
  217. *
  218. * @return int
  219. */
  220. public function getConfigMaxDownloads()
  221. {
  222. return Mage::getStoreConfig(Mage_Downloadable_Model_Link::XML_PATH_DEFAULT_DOWNLOADS_NUMBER);
  223. }
  224. /**
  225. * Prepare block Layout
  226. *
  227. */
  228. protected function _prepareLayout()
  229. {
  230. $this->setChild(
  231. 'upload_button',
  232. $this->getLayout()->createBlock('adminhtml/widget_button')->addData(array(
  233. 'id' => '',
  234. 'label' => Mage::helper('adminhtml')->__('Upload Files'),
  235. 'type' => 'button',
  236. 'onclick' => 'Downloadable.massUploadByType(\'links\');Downloadable.massUploadByType(\'linkssample\')'
  237. ))
  238. );
  239. }
  240. /**
  241. * Retrieve Upload button HTML
  242. *
  243. * @return string
  244. */
  245. public function getUploadButtonHtml()
  246. {
  247. return $this->getChild('upload_button')->toHtml();
  248. }
  249. /**
  250. * Retrive config json
  251. *
  252. * @return string
  253. */
  254. public function getConfigJson($type='links')
  255. {
  256. $this->getConfig()->setUrl(Mage::getModel('adminhtml/url')->addSessionParam()
  257. ->getUrl('*/downloadable_file/upload', array('type' => $type, '_secure' => true)));
  258. $this->getConfig()->setParams(array('form_key' => $this->getFormKey()));
  259. $this->getConfig()->setFileField($type);
  260. $this->getConfig()->setFilters(array(
  261. 'all' => array(
  262. 'label' => Mage::helper('adminhtml')->__('All Files'),
  263. 'files' => array('*.*')
  264. )
  265. ));
  266. $this->getConfig()->setReplaceBrowseWithRemove(true);
  267. $this->getConfig()->setWidth('32');
  268. $this->getConfig()->setHideUploadButton(true);
  269. return Mage::helper('core')->jsonEncode($this->getConfig()->getData());
  270. }
  271. /**
  272. * Retrive config object
  273. *
  274. * @return Varien_Config
  275. */
  276. public function getConfig()
  277. {
  278. if(is_null($this->_config)) {
  279. $this->_config = new Varien_Object();
  280. }
  281. return $this->_config;
  282. }
  283. }