00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 class Mage_Downloadable_Model_Product_Type extends Mage_Catalog_Model_Product_Type_Virtual
00035 {
00036 const TYPE_DOWNLOADABLE = 'downloadable';
00037
00038
00039
00040
00041
00042
00043
00044 public function getLinks($product = null)
00045 {
00046 $product = $this->getProduct($product);
00047
00048 if (is_null($product->getDownloadableLinks())) {
00049 $_linkCollection = Mage::getModel('downloadable/link')->getCollection()
00050 ->addProductToFilter($product->getId())
00051 ->addTitleToResult($product->getStoreId())
00052 ->addPriceToResult($product->getStore()->getWebsiteId());
00053 $linksCollectionById = array();
00054 foreach ($_linkCollection as $link) {
00055
00056
00057 $link->setProduct($product);
00058 $linksCollectionById[$link->getId()] = $link;
00059 }
00060 $product->setDownloadableLinks($linksCollectionById);
00061 }
00062 return $product->getDownloadableLinks();
00063 }
00064
00065
00066
00067
00068
00069
00070
00071 public function hasLinks($product = null)
00072 {
00073 return count($this->getLinks($product)) > 0;
00074 }
00075
00076
00077
00078
00079
00080
00081
00082 public function hasOptions($product = null)
00083 {
00084
00085 return $this->getProduct($product)->getLinksPurchasedSeparately()
00086 || parent::hasOptions($product);
00087 }
00088
00089
00090
00091
00092
00093
00094
00095 public function hasRequiredOptions($product = null)
00096 {
00097 if (parent::hasRequiredOptions($product) || $this->getProduct($product)->getLinksPurchasedSeparately()) {
00098 return true;
00099 }
00100 return false;
00101 }
00102
00103
00104
00105
00106
00107
00108
00109 public function getLinkSelectionRequired($product = null)
00110 {
00111 return $this->getProduct($product)->getLinksPurchasedSeparately();
00112 }
00113
00114
00115
00116
00117
00118
00119
00120 public function getSamples($product = null)
00121 {
00122 $product = $this->getProduct($product);
00123
00124 if (is_null($product->getDownloadableSamples())) {
00125 $_sampleCollection = Mage::getModel('downloadable/sample')->getCollection()
00126 ->addProductToFilter($product->getId())
00127 ->addTitleToResult($product->getStoreId());
00128 $product->setDownloadableSamples($_sampleCollection);
00129 }
00130
00131 return $product->getDownloadableSamples();
00132 }
00133
00134
00135
00136
00137
00138
00139
00140 public function hasSamples($product = null)
00141 {
00142 return count($this->getSamples($product)) > 0;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151 public function save($product = null)
00152 {
00153 parent::save($product);
00154
00155 $product = $this->getProduct($product);
00156
00157
00158 if ($data = $product->getDownloadableData()) {
00159 if (isset($data['sample'])) {
00160 $_deleteItems = array();
00161 foreach ($data['sample'] as $sampleItem) {
00162 if ($sampleItem['is_delete'] == '1') {
00163 if ($sampleItem['sample_id']) {
00164 $_deleteItems[] = $sampleItem['sample_id'];
00165 }
00166 } else {
00167 unset($sampleItem['is_delete']);
00168 if (!$sampleItem['sample_id']) {
00169 unset($sampleItem['sample_id']);
00170 }
00171 $sampleModel = Mage::getModel('downloadable/sample');
00172 $files = array();
00173 if (isset($sampleItem['file'])) {
00174 $files = Zend_Json::decode($sampleItem['file']);
00175 unset($sampleItem['file']);
00176 }
00177
00178 $sampleModel->setData($sampleItem)
00179 ->setSampleType($sampleItem['type'])
00180 ->setProductId($product->getId())
00181 ->setStoreId($product->getStoreId());
00182
00183 if ($sampleModel->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
00184 $sampleFileName = Mage::helper('downloadable/file')->moveFileFromTmp(
00185 Mage_Downloadable_Model_Sample::getBaseTmpPath(),
00186 Mage_Downloadable_Model_Sample::getBasePath(),
00187 $files
00188 );
00189 $sampleModel->setSampleFile($sampleFileName);
00190 }
00191 $sampleModel->save();
00192 }
00193 }
00194 if ($_deleteItems) {
00195 Mage::getResourceModel('downloadable/sample')->deleteItems($_deleteItems);
00196 }
00197 }
00198 if (isset($data['link'])) {
00199 $_deleteItems = array();
00200 foreach ($data['link'] as $linkItem) {
00201 if ($linkItem['is_delete'] == '1') {
00202 if ($linkItem['link_id']) {
00203 $_deleteItems[] = $linkItem['link_id'];
00204 }
00205 } else {
00206 unset($linkItem['is_delete']);
00207 if (!$linkItem['link_id']) {
00208 unset($linkItem['link_id']);
00209 }
00210 $files = array();
00211 if (isset($linkItem['file'])) {
00212 $files = Zend_Json::decode($linkItem['file']);
00213 unset($linkItem['file']);
00214 }
00215 $sample = array();
00216 if (isset($linkItem['sample'])) {
00217 $sample = $linkItem['sample'];
00218 unset($linkItem['sample']);
00219 }
00220 $linkModel = Mage::getModel('downloadable/link')
00221 ->setData($linkItem)
00222 ->setLinkType($linkItem['type'])
00223 ->setProductId($product->getId())
00224 ->setStoreId($product->getStoreId())
00225 ->setWebsiteId($product->getStore()->getWebsiteId());
00226 if (null === $linkModel->getPrice()) {
00227 $linkModel->setPrice(0);
00228 }
00229 if ($linkModel->getIsUnlimited()) {
00230 $linkModel->setNumberOfDownloads(0);
00231 }
00232 $sampleFile = array();
00233 if ($sample && isset($sample['type'])) {
00234 if ($sample['type'] == 'url' && $sample['url'] != '') {
00235 $linkModel->setSampleUrl($sample['url']);
00236 }
00237 $linkModel->setSampleType($sample['type']);
00238 $sampleFile = Zend_Json::decode($sample['file']);
00239 }
00240 if ($linkModel->getLinkType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
00241 $linkFileName = Mage::helper('downloadable/file')->moveFileFromTmp(
00242 Mage_Downloadable_Model_Link::getBaseTmpPath(),
00243 Mage_Downloadable_Model_Link::getBasePath(),
00244 $files
00245 );
00246 $linkModel->setLinkFile($linkFileName);
00247 }
00248 if ($linkModel->getSampleType() == Mage_Downloadable_Helper_Download::LINK_TYPE_FILE) {
00249 $linkSampleFileName = Mage::helper('downloadable/file')->moveFileFromTmp(
00250 Mage_Downloadable_Model_Link::getBaseSampleTmpPath(),
00251 Mage_Downloadable_Model_Link::getBaseSamplePath(),
00252 $sampleFile
00253 );
00254 $linkModel->setSampleFile($linkSampleFileName);
00255 }
00256 $linkModel->save();
00257 }
00258 }
00259 if ($_deleteItems) {
00260 Mage::getResourceModel('downloadable/link')->deleteItems($_deleteItems);
00261 }
00262 }
00263 }
00264
00265 return $this;
00266 }
00267
00268
00269
00270
00271
00272
00273
00274
00275 public function prepareForCart(Varien_Object $buyRequest, $product = null)
00276 {
00277 $result = parent::prepareForCart($buyRequest, $product);
00278
00279 if (is_string($result)) {
00280 return $result;
00281 }
00282
00283 if ($this->getProduct($product)->getSkipCheckRequiredOption()) {
00284 $this->getProduct($product)->setLinksPurchasedSeparately(false);
00285 }
00286 $preparedLinks = array();
00287 if ($this->getProduct($product)->getLinksPurchasedSeparately()) {
00288 if ($links = $buyRequest->getLinks()) {
00289 foreach ($this->getLinks($product) as $link) {
00290 if (in_array($link->getId(), $links)) {
00291 $preparedLinks[] = $link->getId();
00292 }
00293 }
00294 }
00295 } else {
00296 foreach ($this->getLinks($product) as $link) {
00297 $preparedLinks[] = $link->getId();
00298 }
00299 }
00300 if ($preparedLinks) {
00301 $this->getProduct($product)->addCustomOption('downloadable_link_ids', implode(',', $preparedLinks));
00302 return $result;
00303 }
00304 if ($this->getLinkSelectionRequired($product)) {
00305 return Mage::helper('downloadable')->__('Please specify product link(s).');
00306 }
00307 return $result;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317 public function getOrderOptions($product = null)
00318 {
00319 $options = parent::getOrderOptions($product);
00320 if ($linkIds = $this->getProduct($product)->getCustomOption('downloadable_link_ids')) {
00321 $linkOptions = array();
00322 $links = $this->getLinks($product);
00323 foreach (explode(',', $linkIds->getValue()) as $linkId) {
00324 if (isset($links[$linkId])) {
00325 $linkOptions[] = $linkId;
00326 }
00327 }
00328 $options = array_merge($options, array('links' => $linkOptions));
00329 }
00330 $options = array_merge($options, array(
00331 'is_downloadable' => true,
00332 'real_product_type' => self::TYPE_DOWNLOADABLE
00333 ));
00334 return $options;
00335 }
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345 public function beforeSave($product = null)
00346 {
00347 parent::beforeSave($product);
00348
00349
00350 if ($this->getLinkSelectionRequired($product)) {
00351 $this->getProduct($product)->setTypeHasOptions(true);
00352 $this->getProduct($product)->setTypeHasRequiredOptions(true);
00353 } else {
00354 $this->getProduct($product)->setTypeHasOptions(false);
00355 $this->getProduct($product)->setTypeHasRequiredOptions(false);
00356 }
00357 }
00358
00359
00360
00361
00362
00363
00364
00365
00366 public function getSearchableData($product = null)
00367 {
00368 $searchData = parent::getSearchableData($product);
00369 $product = $this->getProduct($product);
00370
00371 $linkSearchData = Mage::getSingleton('downloadable/link')
00372 ->getSearchableData($product->getId(), $product->getStoreId());
00373 if ($linkSearchData) {
00374 $searchData = array_merge($searchData, $linkSearchData);
00375 }
00376
00377 $sampleSearchData = Mage::getSingleton('downloadable/sample')
00378 ->getSearchableData($product->getId(), $product->getStoreId());
00379 if ($sampleSearchData) {
00380 $searchData = array_merge($searchData, $sampleSearchData);
00381 }
00382
00383 return $searchData;
00384 }
00385 }