Link.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Downloadable\Model;
  7. use Magento\Downloadable\Api\Data\LinkInterface;
  8. use Magento\Downloadable\Model\ResourceModel\Link as Resource;
  9. /**
  10. * Downloadable link model
  11. *
  12. * @api
  13. * @method int getProductId()
  14. * @method Link setProductId(int $value)
  15. *
  16. * @api
  17. * @since 100.0.2
  18. */
  19. class Link extends \Magento\Framework\Model\AbstractExtensibleModel implements ComponentInterface, LinkInterface
  20. {
  21. const XML_PATH_LINKS_TITLE = 'catalog/downloadable/links_title';
  22. const XML_PATH_DEFAULT_DOWNLOADS_NUMBER = 'catalog/downloadable/downloads_number';
  23. const XML_PATH_TARGET_NEW_WINDOW = 'catalog/downloadable/links_target_new_window';
  24. const XML_PATH_CONFIG_IS_SHAREABLE = 'catalog/downloadable/shareable';
  25. const LINK_SHAREABLE_YES = 1;
  26. const LINK_SHAREABLE_NO = 0;
  27. const LINK_SHAREABLE_CONFIG = 2;
  28. /**#@+
  29. * Constants for field names
  30. */
  31. const KEY_TITLE = 'title';
  32. const KEY_SORT_ORDER = 'sort_order';
  33. const KEY_IS_SHAREABLE = 'is_shareable';
  34. const KEY_PRICE = 'price';
  35. const KEY_NUMBER_OF_DOWNLOADS = 'number_of_downloads';
  36. const KEY_LINK_TYPE = 'link_type';
  37. const KEY_LINK_FILE = 'link_file';
  38. const KEY_LINK_FILE_CONTENT = 'link_file_content';
  39. const KEY_LINK_URL = 'link_url';
  40. const KEY_SAMPLE_TYPE = 'sample_type';
  41. const KEY_SAMPLE_FILE = 'sample_file';
  42. const KEY_SAMPLE_FILE_CONTENT = 'sample_file_content';
  43. const KEY_SAMPLE_URL = 'sample_url';
  44. /**#@-*/
  45. /**
  46. * @param \Magento\Framework\Model\Context $context
  47. * @param \Magento\Framework\Registry $registry
  48. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  49. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  50. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  51. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  52. * @param array $data
  53. */
  54. public function __construct(
  55. \Magento\Framework\Model\Context $context,
  56. \Magento\Framework\Registry $registry,
  57. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  58. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  59. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  60. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  61. array $data = []
  62. ) {
  63. parent::__construct(
  64. $context,
  65. $registry,
  66. $extensionFactory,
  67. $customAttributeFactory,
  68. $resource,
  69. $resourceCollection,
  70. $data
  71. );
  72. }
  73. /**
  74. * Initialize resource model
  75. *
  76. * @return void
  77. */
  78. protected function _construct()
  79. {
  80. $this->_init(\Magento\Downloadable\Model\ResourceModel\Link::class);
  81. parent::_construct();
  82. }
  83. /**
  84. * @return $this
  85. */
  86. public function afterSave()
  87. {
  88. $this->getResource()->saveItemTitleAndPrice($this);
  89. return parent::afterSave();
  90. }
  91. /**
  92. * Retrieve base temporary path
  93. *
  94. * @return string
  95. */
  96. public function getBaseTmpPath()
  97. {
  98. return 'downloadable/tmp/links';
  99. }
  100. /**
  101. * Retrieve Base files path
  102. *
  103. * @return string
  104. */
  105. public function getBasePath()
  106. {
  107. return 'downloadable/files/links';
  108. }
  109. /**
  110. * Retrieve base sample temporary path
  111. *
  112. * @return string
  113. */
  114. public function getBaseSampleTmpPath()
  115. {
  116. return 'downloadable/tmp/link_samples';
  117. }
  118. /**
  119. * Retrieve base sample path
  120. *
  121. * @return string
  122. */
  123. public function getBaseSamplePath()
  124. {
  125. return 'downloadable/files/link_samples';
  126. }
  127. /**
  128. * Retrieve links searchable data
  129. *
  130. * @param int $productId
  131. * @param int $storeId
  132. * @return array
  133. */
  134. public function getSearchableData($productId, $storeId)
  135. {
  136. return $this->_getResource()->getSearchableData($productId, $storeId);
  137. }
  138. /**
  139. * {@inheritdoc}
  140. * @codeCoverageIgnore
  141. */
  142. public function getTitle()
  143. {
  144. return $this->getData(self::KEY_TITLE);
  145. }
  146. /**
  147. * {@inheritdoc}
  148. * @codeCoverageIgnore
  149. */
  150. public function getPrice()
  151. {
  152. return $this->getData(self::KEY_PRICE);
  153. }
  154. /**
  155. * {@inheritdoc}
  156. * @codeCoverageIgnore
  157. */
  158. public function getIsShareable()
  159. {
  160. return $this->getData(self::KEY_IS_SHAREABLE);
  161. }
  162. /**
  163. * {@inheritdoc}
  164. * @codeCoverageIgnore
  165. */
  166. public function getSortOrder()
  167. {
  168. return $this->getData(self::KEY_SORT_ORDER);
  169. }
  170. /**
  171. * {@inheritdoc}
  172. * @codeCoverageIgnore
  173. */
  174. public function getNumberOfDownloads()
  175. {
  176. return $this->getData(self::KEY_NUMBER_OF_DOWNLOADS);
  177. }
  178. /**
  179. * {@inheritdoc}
  180. * @codeCoverageIgnore
  181. */
  182. public function getLinkType()
  183. {
  184. return $this->getData(self::KEY_LINK_TYPE);
  185. }
  186. /**
  187. * {@inheritdoc}
  188. * @codeCoverageIgnore
  189. */
  190. public function getLinkFile()
  191. {
  192. return $this->getData(self::KEY_LINK_FILE);
  193. }
  194. /**
  195. * Return file content
  196. *
  197. * @return \Magento\Downloadable\Api\Data\File\ContentInterface|null
  198. */
  199. public function getLinkFileContent()
  200. {
  201. return $this->getData(self::KEY_LINK_FILE_CONTENT);
  202. }
  203. /**
  204. * {@inheritdoc}
  205. * @codeCoverageIgnore
  206. */
  207. public function getLinkUrl()
  208. {
  209. return $this->getData(self::KEY_LINK_URL);
  210. }
  211. /**
  212. * {@inheritdoc}
  213. * @codeCoverageIgnore
  214. */
  215. public function getSampleType()
  216. {
  217. return $this->getData(self::KEY_SAMPLE_TYPE);
  218. }
  219. /**
  220. * {@inheritdoc}
  221. * @codeCoverageIgnore
  222. */
  223. public function getSampleFile()
  224. {
  225. return $this->getData(self::KEY_SAMPLE_FILE);
  226. }
  227. /**
  228. * Return sample file content when type is 'file'
  229. *
  230. * @return \Magento\Downloadable\Api\Data\File\ContentInterface|null relative file path
  231. */
  232. public function getSampleFileContent()
  233. {
  234. return $this->getData(self::KEY_SAMPLE_FILE_CONTENT);
  235. }
  236. /**
  237. * {@inheritdoc}
  238. * @codeCoverageIgnore
  239. */
  240. public function getSampleUrl()
  241. {
  242. return $this->getData(self::KEY_SAMPLE_URL);
  243. }
  244. //@codeCoverageIgnoreStart
  245. /**
  246. * @param string $title
  247. * @return $this
  248. */
  249. public function setTitle($title)
  250. {
  251. return $this->setData(self::KEY_TITLE, $title);
  252. }
  253. /**
  254. * @param int $sortOrder
  255. * @return $this
  256. */
  257. public function setSortOrder($sortOrder)
  258. {
  259. return $this->setData(self::KEY_SORT_ORDER, $sortOrder);
  260. }
  261. /**
  262. * @param int $isShareable
  263. * @return $this
  264. */
  265. public function setIsShareable($isShareable)
  266. {
  267. return $this->setData(self::KEY_IS_SHAREABLE, $isShareable);
  268. }
  269. /**
  270. * Set link price
  271. *
  272. * @param float $price
  273. * @return $this
  274. */
  275. public function setPrice($price)
  276. {
  277. return $this->setData(self::KEY_PRICE, $price);
  278. }
  279. /**
  280. * Set number of downloads per user
  281. * Null for unlimited downloads
  282. *
  283. * @param int $numberOfDownloads
  284. * @return $this
  285. */
  286. public function setNumberOfDownloads($numberOfDownloads)
  287. {
  288. return $this->setData(self::KEY_NUMBER_OF_DOWNLOADS, $numberOfDownloads);
  289. }
  290. /**
  291. * @param string $linkType
  292. * @return $this
  293. */
  294. public function setLinkType($linkType)
  295. {
  296. return $this->setData(self::KEY_LINK_TYPE, $linkType);
  297. }
  298. /**
  299. * Set file path or null when type is 'url'
  300. *
  301. * @param string $linkFile
  302. * @return $this
  303. */
  304. public function setLinkFile($linkFile)
  305. {
  306. return $this->setData(self::KEY_LINK_FILE, $linkFile);
  307. }
  308. /**
  309. * Set file content
  310. *
  311. * @param \Magento\Downloadable\Api\Data\File\ContentInterface $linkFileContent
  312. * @return $this
  313. */
  314. public function setLinkFileContent(\Magento\Downloadable\Api\Data\File\ContentInterface $linkFileContent = null)
  315. {
  316. return $this->setData(self::KEY_LINK_FILE_CONTENT, $linkFileContent);
  317. }
  318. /**
  319. * Set URL
  320. *
  321. * @param string $linkUrl
  322. * @return $this
  323. */
  324. public function setLinkUrl($linkUrl)
  325. {
  326. return $this->setData(self::KEY_LINK_URL, $linkUrl);
  327. }
  328. /**
  329. * @param string $sampleType
  330. * @return $this
  331. */
  332. public function setSampleType($sampleType)
  333. {
  334. return $this->setData(self::KEY_SAMPLE_TYPE, $sampleType);
  335. }
  336. /**
  337. * Set file path
  338. *
  339. * @param string $sampleFile
  340. * @return $this
  341. */
  342. public function setSampleFile($sampleFile)
  343. {
  344. return $this->setData(self::KEY_SAMPLE_FILE, $sampleFile);
  345. }
  346. /**
  347. * Set sample file content
  348. *
  349. * @param \Magento\Downloadable\Api\Data\File\ContentInterface $sampleFileContent
  350. * @return $this
  351. */
  352. public function setSampleFileContent(
  353. \Magento\Downloadable\Api\Data\File\ContentInterface $sampleFileContent = null
  354. ) {
  355. return $this->setData(self::KEY_SAMPLE_FILE_CONTENT, $sampleFileContent);
  356. }
  357. /**
  358. * Set URL
  359. *
  360. * @param string $sampleUrl
  361. * @return $this
  362. */
  363. public function setSampleUrl($sampleUrl)
  364. {
  365. return $this->setData(self::KEY_SAMPLE_URL, $sampleUrl);
  366. }
  367. /**
  368. * {@inheritdoc}
  369. *
  370. * @return \Magento\Downloadable\Api\Data\LinkExtensionInterface|null
  371. */
  372. public function getExtensionAttributes()
  373. {
  374. return $this->_getExtensionAttributes();
  375. }
  376. /**
  377. * {@inheritdoc}
  378. *
  379. * @param \Magento\Downloadable\Api\Data\LinkExtensionInterface $extensionAttributes
  380. * @return $this
  381. */
  382. public function setExtensionAttributes(\Magento\Downloadable\Api\Data\LinkExtensionInterface $extensionAttributes)
  383. {
  384. return $this->_setExtensionAttributes($extensionAttributes);
  385. }
  386. //@codeCoverageIgnoreEnd
  387. }