Sample.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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\SampleInterface;
  8. /**
  9. * Downloadable sample model
  10. *
  11. * @method int getProductId()
  12. *
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Sample extends \Magento\Framework\Model\AbstractExtensibleModel implements ComponentInterface, SampleInterface
  17. {
  18. const XML_PATH_SAMPLES_TITLE = 'catalog/downloadable/samples_title';
  19. /**#@+
  20. * Constants for field names
  21. */
  22. const KEY_TITLE = 'title';
  23. const KEY_SORT_ORDER = 'sort_order';
  24. const KEY_SAMPLE_TYPE = 'sample_type';
  25. const KEY_SAMPLE_FILE = 'sample_file';
  26. const KEY_SAMPLE_FILE_CONTENT = 'sample_file_content';
  27. const KEY_SAMPLE_URL = 'sample_url';
  28. /**#@-*/
  29. /**
  30. * @param \Magento\Framework\Model\Context $context
  31. * @param \Magento\Framework\Registry $registry
  32. * @param \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory
  33. * @param \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory
  34. * @param \Magento\Framework\Model\ResourceModel\AbstractResource $resource
  35. * @param \Magento\Framework\Data\Collection\AbstractDb $resourceCollection
  36. * @param array $data
  37. */
  38. public function __construct(
  39. \Magento\Framework\Model\Context $context,
  40. \Magento\Framework\Registry $registry,
  41. \Magento\Framework\Api\ExtensionAttributesFactory $extensionFactory,
  42. \Magento\Framework\Api\AttributeValueFactory $customAttributeFactory,
  43. \Magento\Framework\Model\ResourceModel\AbstractResource $resource = null,
  44. \Magento\Framework\Data\Collection\AbstractDb $resourceCollection = null,
  45. array $data = []
  46. ) {
  47. parent::__construct(
  48. $context,
  49. $registry,
  50. $extensionFactory,
  51. $customAttributeFactory,
  52. $resource,
  53. $resourceCollection,
  54. $data
  55. );
  56. }
  57. /**
  58. * Initialize resource
  59. *
  60. * @return void
  61. */
  62. protected function _construct()
  63. {
  64. $this->_init(\Magento\Downloadable\Model\ResourceModel\Sample::class);
  65. parent::_construct();
  66. }
  67. /**
  68. * After save process
  69. *
  70. * @return $this
  71. */
  72. public function afterSave()
  73. {
  74. $this->getResource()->saveItemTitle($this);
  75. return parent::afterSave();
  76. }
  77. /**
  78. * Retrieve sample URL
  79. *
  80. * @return string
  81. */
  82. public function getUrl()
  83. {
  84. if ($this->getSampleUrl()) {
  85. return $this->getSampleUrl();
  86. } else {
  87. return $this->getSampleFile();
  88. }
  89. }
  90. /**
  91. * Retrieve base tmp path
  92. *
  93. * @return string
  94. */
  95. public function getBaseTmpPath()
  96. {
  97. return 'downloadable/tmp/samples';
  98. }
  99. /**
  100. * Retrieve sample files path
  101. *
  102. * @return string
  103. */
  104. public function getBasePath()
  105. {
  106. return 'downloadable/files/samples';
  107. }
  108. /**
  109. * Retrieve links searchable data
  110. *
  111. * @param int $productId
  112. * @param int $storeId
  113. * @return array
  114. */
  115. public function getSearchableData($productId, $storeId)
  116. {
  117. return $this->_getResource()->getSearchableData($productId, $storeId);
  118. }
  119. /**
  120. * {@inheritdoc}
  121. * @codeCoverageIgnore
  122. */
  123. public function getTitle()
  124. {
  125. return $this->getData(self::KEY_TITLE);
  126. }
  127. /**
  128. * {@inheritdoc}
  129. * @codeCoverageIgnore
  130. */
  131. public function getSortOrder()
  132. {
  133. return $this->getData(self::KEY_SORT_ORDER);
  134. }
  135. /**
  136. * {@inheritdoc}
  137. * @codeCoverageIgnore
  138. */
  139. public function getSampleType()
  140. {
  141. return $this->getData(self::KEY_SAMPLE_TYPE);
  142. }
  143. /**
  144. * {@inheritdoc}
  145. * @codeCoverageIgnore
  146. */
  147. public function getSampleFile()
  148. {
  149. return $this->getData(self::KEY_SAMPLE_FILE);
  150. }
  151. /**
  152. * {@inheritdoc}
  153. * @codeCoverageIgnore
  154. */
  155. public function getSampleFileContent()
  156. {
  157. return $this->getData(self::KEY_SAMPLE_FILE_CONTENT);
  158. }
  159. /**
  160. * {@inheritdoc}
  161. * @codeCoverageIgnore
  162. */
  163. public function getSampleUrl()
  164. {
  165. return $this->getData(self::KEY_SAMPLE_URL);
  166. }
  167. /**
  168. * Set sample title
  169. *
  170. * @param string $title
  171. * @return $this
  172. */
  173. public function setTitle($title)
  174. {
  175. return $this->setData(self::KEY_TITLE, $title);
  176. }
  177. /**
  178. * Set sort order index for sample
  179. *
  180. * @param int $sortOrder
  181. * @return $this
  182. */
  183. public function setSortOrder($sortOrder)
  184. {
  185. return $this->setData(self::KEY_SORT_ORDER, $sortOrder);
  186. }
  187. /**
  188. * @param string $sampleType
  189. * @return $this
  190. */
  191. public function setSampleType($sampleType)
  192. {
  193. return $this->setData(self::KEY_SAMPLE_TYPE, $sampleType);
  194. }
  195. /**
  196. * Set file path or null when type is 'url'
  197. *
  198. * @param string $sampleFile
  199. * @return $this
  200. */
  201. public function setSampleFile($sampleFile)
  202. {
  203. return $this->setData(self::KEY_SAMPLE_FILE, $sampleFile);
  204. }
  205. /**
  206. * Set sample file content
  207. *
  208. * @param \Magento\Downloadable\Api\Data\File\ContentInterface $sampleFileContent
  209. * @return $this
  210. */
  211. public function setSampleFileContent(\Magento\Downloadable\Api\Data\File\ContentInterface $sampleFileContent = null)
  212. {
  213. return $this->setData(self::KEY_SAMPLE_FILE_CONTENT, $sampleFileContent);
  214. }
  215. /**
  216. * Set sample URL
  217. *
  218. * @param string $sampleUrl
  219. * @return $this
  220. */
  221. public function setSampleUrl($sampleUrl)
  222. {
  223. return $this->setData(self::KEY_SAMPLE_URL, $sampleUrl);
  224. }
  225. /**
  226. * {@inheritdoc}
  227. *
  228. * @return \Magento\Downloadable\Api\Data\SampleExtensionInterface|null
  229. */
  230. public function getExtensionAttributes()
  231. {
  232. return $this->_getExtensionAttributes();
  233. }
  234. /**
  235. * {@inheritdoc}
  236. *
  237. * @param \Magento\Downloadable\Api\Data\SampleExtensionInterface $extensionAttributes
  238. * @return $this
  239. */
  240. public function setExtensionAttributes(\Magento\Downloadable\Api\Data\SampleExtensionInterface $extensionAttributes)
  241. {
  242. return $this->_setExtensionAttributes($extensionAttributes);
  243. }
  244. }