1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Downloadable_Block_Adminhtml_Catalog_Product_Edit_Tab_Downloadable_Links
35: extends Mage_Adminhtml_Block_Template
36: {
37: 38: 39: 40: 41:
42: protected $_purchasedSeparatelyAttribute = null;
43:
44: 45: 46: 47:
48: public function __construct()
49: {
50: parent::__construct();
51: $this->setTemplate('downloadable/product/edit/downloadable/links.phtml');
52: $this->setCanEditPrice(true);
53: $this->setCanReadPrice(true);
54: }
55:
56: 57: 58: 59: 60:
61: public function getProduct()
62: {
63: return Mage::registry('product');
64: }
65:
66: 67: 68: 69: 70:
71: public function getPurchasedSeparatelyAttribute()
72: {
73: if (is_null($this->_purchasedSeparatelyAttribute)) {
74: $_attributeCode = 'links_purchased_separately';
75:
76: $this->_purchasedSeparatelyAttribute = Mage::getModel('eav/entity_attribute')
77: ->loadByCode(Mage_Catalog_Model_Product::ENTITY, $_attributeCode);
78: }
79:
80: return $this->_purchasedSeparatelyAttribute;
81: }
82:
83: 84: 85: 86: 87:
88: public function getPurchasedSeparatelySelect()
89: {
90: $select = $this->getLayout()->createBlock('adminhtml/html_select')
91: ->setName('product[links_purchased_separately]')
92: ->setId('downloadable_link_purchase_type')
93: ->setOptions(Mage::getSingleton('adminhtml/system_config_source_yesno')->toOptionArray())
94: ->setValue($this->getProduct()->getLinksPurchasedSeparately());
95:
96: return $select->getHtml();
97: }
98:
99: 100: 101: 102: 103:
104: public function getAddButtonHtml()
105: {
106: $addButton = $this->getLayout()->createBlock('adminhtml/widget_button')
107: ->setData(array(
108: 'label' => Mage::helper('downloadable')->__('Add New Row'),
109: 'id' => 'add_link_item',
110: 'class' => 'add'
111: ));
112: return $addButton->toHtml();
113: }
114:
115: 116: 117: 118: 119:
120: public function getLinksTitle()
121: {
122: return Mage::getStoreConfig(Mage_Downloadable_Model_Link::XML_PATH_LINKS_TITLE);
123: }
124:
125: 126: 127: 128: 129:
130: public function getUsedDefault()
131: {
132: return $this->getProduct()->getAttributeDefaultValue('links_title') === false;
133: }
134:
135: 136: 137: 138: 139:
140: public function getIsPriceWebsiteScope()
141: {
142: $scope = (int) Mage::app()->getStore()->getConfig(Mage_Core_Model_Store::XML_PATH_PRICE_SCOPE);
143: if ($scope == Mage_Core_Model_Store::PRICE_SCOPE_WEBSITE) {
144: return true;
145: }
146: return false;
147: }
148:
149: 150: 151: 152: 153:
154: public function getLinkData()
155: {
156: $linkArr = array();
157: $links = $this->getProduct()->getTypeInstance(true)->getLinks($this->getProduct());
158: $priceWebsiteScope = $this->getIsPriceWebsiteScope();
159: foreach ($links as $item) {
160: $tmpLinkItem = array(
161: 'link_id' => $item->getId(),
162: 'title' => $this->escapeHtml($item->getTitle()),
163: 'price' => $this->getCanReadPrice() ? $this->getPriceValue($item->getPrice()) : '',
164: 'number_of_downloads' => $item->getNumberOfDownloads(),
165: 'is_shareable' => $item->getIsShareable(),
166: 'link_url' => $item->getLinkUrl(),
167: 'link_type' => $item->getLinkType(),
168: 'sample_file' => $item->getSampleFile(),
169: 'sample_url' => $item->getSampleUrl(),
170: 'sample_type' => $item->getSampleType(),
171: 'sort_order' => $item->getSortOrder(),
172: );
173: $file = Mage::helper('downloadable/file')->getFilePath(
174: Mage_Downloadable_Model_Link::getBasePath(), $item->getLinkFile()
175: );
176:
177: if ($item->getLinkFile() && !is_file($file)) {
178: Mage::helper('core/file_storage_database')->saveFileToFilesystem($file);
179: }
180:
181: if ($item->getLinkFile() && is_file($file)) {
182: $name = '<a href="'
183: . $this->getUrl('*/downloadable_product_edit/link', array(
184: 'id' => $item->getId(),
185: '_secure' => true
186: )) . '">' . Mage::helper('downloadable/file')->getFileFromPathFile($item->getLinkFile()) . '</a>';
187: $tmpLinkItem['file_save'] = array(
188: array(
189: 'file' => $item->getLinkFile(),
190: 'name' => $name,
191: 'size' => filesize($file),
192: 'status' => 'old'
193: ));
194: }
195: $sampleFile = Mage::helper('downloadable/file')->getFilePath(
196: Mage_Downloadable_Model_Link::getBaseSamplePath(), $item->getSampleFile()
197: );
198: if ($item->getSampleFile() && is_file($sampleFile)) {
199: $tmpLinkItem['sample_file_save'] = array(
200: array(
201: 'file' => $item->getSampleFile(),
202: 'name' => Mage::helper('downloadable/file')->getFileFromPathFile($item->getSampleFile()),
203: 'size' => filesize($sampleFile),
204: 'status' => 'old'
205: ));
206: }
207: if ($item->getNumberOfDownloads() == '0') {
208: $tmpLinkItem['is_unlimited'] = ' checked="checked"';
209: }
210: if ($this->getProduct()->getStoreId() && $item->getStoreTitle()) {
211: $tmpLinkItem['store_title'] = $item->getStoreTitle();
212: }
213: if ($this->getProduct()->getStoreId() && $priceWebsiteScope) {
214: $tmpLinkItem['website_price'] = $item->getWebsitePrice();
215: }
216: $linkArr[] = new Varien_Object($tmpLinkItem);
217: }
218: return $linkArr;
219: }
220:
221: 222: 223: 224: 225: 226:
227: public function getPriceValue($value)
228: {
229: return number_format($value, 2, null, '');
230: }
231:
232: 233: 234: 235: 236:
237: public function getConfigMaxDownloads()
238: {
239: return Mage::getStoreConfig(Mage_Downloadable_Model_Link::XML_PATH_DEFAULT_DOWNLOADS_NUMBER);
240: }
241:
242: 243: 244: 245:
246: protected function _prepareLayout()
247: {
248: $this->setChild(
249: 'upload_button',
250: $this->getLayout()->createBlock('adminhtml/widget_button')->addData(array(
251: 'id' => '',
252: 'label' => Mage::helper('adminhtml')->__('Upload Files'),
253: 'type' => 'button',
254: 'onclick' => 'Downloadable.massUploadByType(\'links\');Downloadable.massUploadByType(\'linkssample\')'
255: ))
256: );
257: }
258:
259: 260: 261: 262: 263:
264: public function getUploadButtonHtml()
265: {
266: return $this->getChild('upload_button')->toHtml();
267: }
268:
269: 270: 271: 272: 273:
274: public function getConfigJson($type='links')
275: {
276: $this->getConfig()->setUrl(Mage::getModel('adminhtml/url')->addSessionParam()
277: ->getUrl('*/downloadable_file/upload', array('type' => $type, '_secure' => true)));
278: $this->getConfig()->setParams(array('form_key' => $this->getFormKey()));
279: $this->getConfig()->setFileField($type);
280: $this->getConfig()->setFilters(array(
281: 'all' => array(
282: 'label' => Mage::helper('adminhtml')->__('All Files'),
283: 'files' => array('*.*')
284: )
285: ));
286: $this->getConfig()->setReplaceBrowseWithRemove(true);
287: $this->getConfig()->setWidth('32');
288: $this->getConfig()->setHideUploadButton(true);
289: return Mage::helper('core')->jsonEncode($this->getConfig()->getData());
290: }
291:
292: 293: 294: 295: 296:
297: public function getConfig()
298: {
299: if(is_null($this->_config)) {
300: $this->_config = new Varien_Object();
301: }
302:
303: return $this->_config;
304: }
305: }
306: