Source of file DownloadableProduct.php
Size: 3,501 Bytes - Last Modified: 2021-12-24T05:16:37+00:00
/var/www/docs.ssmods.com/process/src/code/model/DownloadableProduct.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 | <?php class DownloadableProduct extends Product { /** * A list of statuses that an order containing this product must * have in order to allow this product to be downloaded. * * @config */ private static $allowed_order_statuses = array( "paid", "processing", "dispatched" ); /** * @config */ private static $description = "A product that can be downloaded"; private static $db = array( 'LinkLife' => 'Int' ); private static $has_one = array( "File" => "File" ); private static $casting = array( "DownloadLink" => "Varchar", "Deliverable" => "Boolean" ); private static $defaults = array( 'LinkLife' => 7 ); /** * Is this product deliverable? Largely this is used * by the shopping cart when adding to cart. * * @return boolean */ public function getDeliverable() { return false; } /** * Get the link to download the file associated with this product * */ public function getDownloadLink() { $link = ""; if($this->FileID) $link = $this->File()->Link(); return $link; } public function getCMSFields() { $fields = parent::getCMSFields(); $fields->removeByName("Weight"); $fields->removeByName("PackSize"); $fields->addFieldsToTab( "Root.Settings", array( TextField::create('LinkLife','Life of download link (in days)'), UploadField::create("File") ->setFolderName("downloadable") ) ); return $fields; } public function requireDefaultRecords() { parent::requireDefaultRecords(); // See if we need to create downloadable postage $records = PostageArea::get() ->filter( "Title", _t( "DownloadableProduct.DownloadableGoods", "Downloadable Goods" ) ); if(!$records->exists()) { $config = SiteConfig::current_site_config(); $postage = PostageArea::create(); $postage->Title = _t( "DownloadableProduct.DownloadableGoods", "Downloadable Goods" ); $postage->Country = "*"; $postage->ZipCode = "*"; $postage->Calculation = "Weight"; $postage->Unit = 0.0; $postage->Cost = 0.0; $postage->Tax = 0.0; $postage->SiteID = $config->ID; $postage->write(); DB::alteration_message(_t("DownloadableProduct.AddedPostage", "Added downloadable postage"), 'created'); } } public function onBeforeWrite() { parent::onBeforeWrite(); // Downloadable products have 0 weight and Pack Size $this->Weight = 0; $this->PackSize = 0; } public function canDownload($member = null) { if(!$member || !$member instanceof Member) $member = Member::currentUser(); if($member) { $items = $member ->Orders() ->filter(array( "Status" => $this->config()->allowed_order_statuses, "Items.StockID" => $this->StockID )); return $items->exists(); } return false; } } |