/** * Download an ordered file * * @return ObjectFileResponse * @return ActionRedirectResponse * @role login */ public function download() { // get and validate OrderedItem instance first $f = new ARSelectFilter(new EqualsCond(new ARFieldHandle('OrderedItem', 'ID'), $this->request->get('id'))); $f->mergeCondition(new EqualsCond(new ARFieldHandle('CustomerOrder', 'userID'), $this->user->getID())); $s = ActiveRecordModel::getRecordSet('OrderedItem', $f, array('CustomerOrder', 'Product')); // OrderedItem does not exist if (!$s->size()) { return new ActionRedirectResponse('user', 'index'); } $item = $s->get(0); $file = $item->getFileByID($this->request->get('fileID')); // file does not exist for OrderedItem if (!$file) { return new ActionRedirectResponse('user', 'index'); } OrderedFile::getInstance($item, $file)->registerDownload(); // download expired if (!$item->isDownloadable($file)) { $this->setMessage($this->translate('_download_limit_reached')); return new ActionRedirectResponse('user', 'index'); } return new ObjectFileResponse($file); }
/** * Determine if the file download period hasn't expired yet * * @return ProductFile */ public function isDownloadable(ProductFile $file) { $allow = true; if ($file->allowDownloadDays->get()) { $orderDate = $this->customerOrder->get()->dateCompleted->get(); if (!(abs($orderDate->getDayDifference(new DateTime())) <= $file->allowDownloadDays->get() || !$file->allowDownloadDays->get())) { $allow = false; } } if ($file->allowDownloadCount->get()) { $orderFile = OrderedFile::getInstance($this, $file); if ($orderFile->timesDownloaded->get() > $file->allowDownloadCount->get() + 1) { $allow = false; } } return $allow; }