/**
  * Check is link shareable or not
  *
  * @param Faett_Package_Model_Link $link
  * @return bool
  */
 public function getIsShareable($link)
 {
     $shareable = false;
     switch ($link->getIsShareable()) {
         case Faett_Package_Model_Link::LINK_SHAREABLE_YES:
         case Faett_Package_Model_Link::LINK_SHAREABLE_NO:
             $shareable = (bool) $link->getIsShareable();
             break;
         case Faett_Package_Model_Link::LINK_SHAREABLE_CONFIG:
             $shareable = (bool) Mage::getStoreConfigFlag(Faett_Package_Model_Link::XML_PATH_CONFIG_IS_SHAREABLE);
     }
     return $shareable;
 }
 /**
  * This method opens and validates the uploaded PEAR package
  * and assembles the content of the package file itself, the
  * dependencies and the release notes.
  *
  * @param Faett_Package_Model_Link $link The link with the PEAR package file
  * @return void
  */
 public function generatePEARInfos(Faett_Package_Model_Link $link)
 {
     // initialize the PEAR service implementation
     $service = Faett_Core_Factory::get(Mage::getBaseDir());
     // initialize the PEAR_PackageFile_v2 instance
     $pf = $service->packageFile($packageFile = Faett_Package_Model_Link::getBasePath() . $link->getLinkFile());
     // initialize the archive
     $tar = new Archive_Tar($packageFile);
     // try to load the content of the package2.xml file
     $contents = $tar->extractInString('package2.xml');
     // if not available, try to load from package.xml file
     if (!$contents) {
         $contents = $tar->extractInString('package.xml');
     }
     // load the data to assemble the link with
     $link->setPackageFile($contents);
     $link->setPackageName($pf->getName());
     $link->setPackageSize(filesize($packageFile));
     $link->setDependencies(serialize($pf->getDependencies()));
     $link->setState($pf->getState());
     $link->setVersion($pf->getVersion());
     $link->setReleaseDate($pf->getDate());
     $link->setLicence($pf->getLicense());
     $link->setSummary($pf->getSummary());
     $link->setDescription($pf->getDescription());
     $link->setNotes($pf->getNotes());
     // set the licence URI
     if (is_array($loc = $pf->getLicenseLocation())) {
         if (array_key_exists('uri', $loc)) {
             $link->setLicenceUri($loc['uri']);
         } elseif (array_key_exists('filesource', $loc)) {
             $link->setLicenceUri($loc['filesource']);
         }
     }
     // save the completed link
     $link->save();
 }
 /**
  * Upload file controller action
  */
 public function uploadAction()
 {
     $type = $this->getRequest()->getParam('type');
     $tmpPath = '';
     if ($type == 'samples') {
         $tmpPath = Faett_Package_Model_Sample::getBaseTmpPath();
     } elseif ($type == 'links') {
         $tmpPath = Faett_Package_Model_Link::getBaseTmpPath();
     } elseif ($type == 'link_samples') {
         $tmpPath = Faett_Package_Model_Link::getBaseSampleTmpPath();
     }
     $result = array();
     try {
         $uploader = new Varien_File_Uploader($type);
         $uploader->setAllowRenameFiles(true);
         $uploader->setFilesDispersion(true);
         $result = $uploader->save($tmpPath);
         $result['cookie'] = array('name' => session_name(), 'value' => $this->_getSession()->getSessionId(), 'lifetime' => $this->_getSession()->getCookieLifetime(), 'path' => $this->_getSession()->getCookiePath(), 'domain' => $this->_getSession()->getCookieDomain());
     } catch (Exception $e) {
         $result = array('error' => $e->getMessage(), 'errorcode' => $e->getCode());
     }
     $this->getResponse()->setBody(Zend_Json::encode($result));
 }
 /**
  * Return array of links
  *
  * @return array
  */
 public function getLinkData()
 {
     $linkArr = array();
     $links = $this->getProduct()->getTypeInstance(true)->getLinks($this->getProduct());
     $priceWebsiteScope = $this->getIsPriceWebsiteScope();
     foreach ($links as $item) {
         $tmpLinkItem = array('link_id' => $item->getId(), 'title' => $item->getTitle(), 'price' => $this->getPriceValue($item->getPrice()), 'number_of_downloads' => $item->getNumberOfDownloads(), 'is_shareable' => $item->getIsShareable(), 'link_url' => $item->getLinkUrl(), 'link_type' => $item->getLinkType(), 'sample_file' => $item->getSampleFile(), 'sample_url' => $item->getSampleUrl(), 'sample_type' => $item->getSampleType(), 'sort_order' => $item->getSortOrder(), 'version' => $item->getVersion(), 'stability' => $item->getStability());
         $file = Mage::helper('package/file')->getFilePath(Faett_Package_Model_Link::getBasePath(), $item->getLinkFile());
         if ($item->getLinkFile() && is_file($file)) {
             $name = '<a href="' . $this->getUrl('packageadmin/product_edit/link', array('id' => $item->getId(), '_secure' => true)) . '">' . Mage::helper('package/file')->getFileFromPathFile($item->getLinkFile()) . '</a>';
             $tmpLinkItem['file_save'] = array(array('file' => $item->getLinkFile(), 'name' => $name, 'size' => filesize($file), 'status' => 'old'));
         }
         $sampleFile = Mage::helper('package/file')->getFilePath(Faett_Package_Model_Link::getBaseSamplePath(), $item->getSampleFile());
         if ($item->getSampleFile() && is_file($sampleFile)) {
             $tmpLinkItem['sample_file_save'] = array(array('file' => $item->getSampleFile(), 'name' => Mage::helper('package/file')->getFileFromPathFile($item->getSampleFile()), 'size' => filesize($sampleFile), 'status' => 'old'));
         }
         if ($item->getNumberOfDownloads() == '0') {
             $tmpLinkItem['is_unlimited'] = ' checked="checked"';
         }
         if ($this->getProduct()->getStoreId() && $item->getStoreTitle()) {
             $tmpLinkItem['store_title'] = $item->getStoreTitle();
         }
         if ($this->getProduct()->getStoreId() && $priceWebsiteScope) {
             $tmpLinkItem['website_price'] = $item->getWebsitePrice();
         }
         $linkArr[] = new Varien_Object($tmpLinkItem);
     }
     return $linkArr;
 }
 /**
  * Enter description here...
  *
  * @param Mage_Catalog_Model_Product $product
  * @return Faett_Package_Model_Product_Type
  */
 public function save($product = null)
 {
     parent::save($product);
     $product = $this->getProduct($product);
     /* @var Mage_Catalog_Model_Product $product */
     if ($data = $product->getPackageData()) {
         if (isset($data['sample'])) {
             $_deleteItems = array();
             foreach ($data['sample'] as $sampleItem) {
                 if ($sampleItem['is_delete'] == '1') {
                     if ($sampleItem['sample_id']) {
                         $_deleteItems[] = $sampleItem['sample_id'];
                     }
                 } else {
                     unset($sampleItem['is_delete']);
                     if (!$sampleItem['sample_id']) {
                         unset($sampleItem['sample_id']);
                     }
                     $sampleModel = Mage::getModel('package/sample');
                     $files = array();
                     if (isset($sampleItem['file'])) {
                         $files = Zend_Json::decode($sampleItem['file']);
                         unset($sampleItem['file']);
                     }
                     $sampleModel->setData($sampleItem)->setSampleType($sampleItem['type'])->setProductId($product->getId())->setStoreId($product->getStoreId());
                     if ($sampleModel->getSampleType() == Faett_Package_Helper_Download::LINK_TYPE_FILE) {
                         $sampleFileName = Mage::helper('package/file')->moveFileFromTmp(Faett_Package_Model_Sample::getBaseTmpPath(), Faett_Package_Model_Sample::getBasePath(), $files);
                         $sampleModel->setSampleFile($sampleFileName);
                     }
                     $sampleModel->save();
                 }
             }
             if ($_deleteItems) {
                 Mage::getResourceModel('package/sample')->deleteItems($_deleteItems);
             }
         }
         if (isset($data['link'])) {
             $_deleteItems = array();
             foreach ($data['link'] as $linkItem) {
                 if ($linkItem['is_delete'] == '1') {
                     if ($linkItem['link_id']) {
                         $_deleteItems[] = $linkItem['link_id'];
                     }
                 } else {
                     unset($linkItem['is_delete']);
                     if (!$linkItem['link_id']) {
                         unset($linkItem['link_id']);
                     }
                     $files = array();
                     if (isset($linkItem['file'])) {
                         $files = Zend_Json::decode($linkItem['file']);
                         unset($linkItem['file']);
                     }
                     $sample = array();
                     if (isset($linkItem['sample'])) {
                         $sample = $linkItem['sample'];
                         unset($linkItem['sample']);
                     }
                     $linkModel = Mage::getModel('package/link')->setData($linkItem)->setLinkType($linkItem['type'])->setProductId($product->getId())->setStoreId($product->getStoreId())->setWebsiteId($product->getStore()->getWebsiteId());
                     if (null === $linkModel->getPrice()) {
                         $linkModel->setPrice(0);
                     }
                     if ($linkModel->getIsUnlimited()) {
                         $linkModel->setNumberOfDownloads(0);
                     }
                     $sampleFile = array();
                     if ($sample && isset($sample['type'])) {
                         if ($sample['type'] == 'url' && $sample['url'] != '') {
                             $linkModel->setSampleUrl($sample['url']);
                         }
                         $linkModel->setSampleType($sample['type']);
                         $sampleFile = Zend_Json::decode($sample['file']);
                     }
                     if ($linkModel->getLinkType() == Faett_Package_Helper_Download::LINK_TYPE_FILE) {
                         $linkFileName = Mage::helper('package/file')->moveFileFromTmp(Faett_Package_Model_Link::getBaseTmpPath(), Faett_Package_Model_Link::getBasePath(), $files);
                         $linkModel->setLinkFile($linkFileName);
                     }
                     if ($linkModel->getSampleType() == Faett_Package_Helper_Download::LINK_TYPE_FILE) {
                         $linkSampleFileName = Mage::helper('package/file')->moveFileFromTmp(Faett_Package_Model_Link::getBaseSampleTmpPath(), Faett_Package_Model_Link::getBaseSamplePath(), $sampleFile);
                         $linkModel->setSampleFile($linkSampleFileName);
                     }
                     $linkModel->save();
                     Mage::helper('package/file')->generatePEARInfos($linkModel);
                 }
             }
             if ($_deleteItems) {
                 Mage::getResourceModel('package/link')->deleteItems($_deleteItems);
             }
         }
     }
     return $this;
 }
 /**
  * Download link action
  *
  */
 public function linkAction()
 {
     $linkId = $this->getRequest()->getParam('id', 0);
     $link = Mage::getModel('package/link')->load($linkId);
     if ($link->getId()) {
         $resource = '';
         $resourceType = '';
         if ($link->getLinkType() == Faett_Package_Helper_Download::LINK_TYPE_URL) {
             $resource = $link->getLinkUrl();
             $resourceType = Faett_Package_Helper_Download::LINK_TYPE_URL;
         } elseif ($link->getLinkType() == Faett_Package_Helper_Download::LINK_TYPE_FILE) {
             $resource = Mage::helper('package/file')->getFilePath(Faett_Package_Model_Link::getBasePath(), $link->getLinkFile());
             $resourceType = Faett_Package_Helper_Download::LINK_TYPE_FILE;
         }
         try {
             $this->_processDownload($resource, $resourceType);
         } catch (Mage_Core_Exception $e) {
             $this->_getCustomerSession()->addError(Mage::helper('package')->__('Sorry, there was an error getting requested content'));
         }
     }
 }
 /**
  * Download link action.
  *
  * @return void
  */
 public function linkAction()
 {
     // load the serialz from the request
     $id = $this->getRequest()->getParam('id', 0);
     // load the purchased link
     $linkPurchased = Mage::getModel('package/link_purchased')->load($id, 'serialz');
     // check if a valid link was found
     if (!$linkPurchased->getId()) {
         $this->_getCustomerSession()->addNotice(Mage::helper('package')->__("Requested link doesn't exist."));
         return $this->_redirect('*/*/index');
     }
     // load the customer and the product ID
     $customerId = $this->_getCustomerSession()->getCustomerId();
     $productId = $linkPurchased->getProductId();
     // check if a valid customer id was found
     if (!$customerId) {
         // if not load the product
         $product = Mage::getModel('catalog/product')->load($productId);
         // load the product ID
         if ($product->getId()) {
             $notice = Mage::helper('package')->__('Please log in to download your product or purchase <a href="%s">%s</a>.', $product->getProductUrl(), $product->getName());
         } else {
             $notice = Mage::helper('package')->__('Please log in to download your product.');
         }
         // log the notice
         $this->_getCustomerSession()->addNotice($notice);
         // redirect to the index page
         return $this->_redirect('*/*/index');
     }
     // check if the customer ID's are equal, means the customer has bought the package
     if ($linkPurchased->getCustomerId() != $customerId) {
         $this->_getCustomerSession()->addNotice(Mage::helper('package')->__("Requested link doesn't exist."));
         return $this->_redirect('*/*/index');
     }
     // check the package state
     if ($linkPurchased->getState() == Faett_Package_Model_Link_Purchased::LINK_STATE_AVAILABLE) {
         // initialize the resource
         $resource = '';
         $resourceType = '';
         // load the package
         $product = Mage::getModel('catalog/product')->load($productId);
         // load the available links
         $links = $product->getTypeInstance(true)->getLinks($product);
         // check the serialz
         $serialz = Mage::getModel('package/serialz')->init($linkPurchased)->decrypt($linkPurchased->getSerialz());
         // iterate over the links and check the customers serialz
         foreach ($links as $link) {
             // if the serialz is valid for the link
             if ($link->getReleaseDate() <= $serialz->getValidThru()) {
                 // load the resource
                 $resource = Mage::helper('package/file')->getFilePath(Faett_Package_Model_Link::getBasePath(), $link->getLinkFile());
                 // set the resource type
                 $resourceType = Faett_Package_Helper_Download::LINK_TYPE_FILE;
                 try {
                     // process the download
                     $this->_processDownload($resource, $resourceType);
                     exit(0);
                 } catch (Exception $e) {
                     Mage::logException($e);
                     $this->_getCustomerSession()->addNotice(Mage::helper('package')->__('Sorry, there was an error getting requested content. Please contact store owner.'));
                 }
             }
         }
     } elseif ($linkPurchased->getState() == Faett_Package_Model_Link_Purchased::LINK_STATE_EXPIRED) {
         $this->_getCustomerSession()->addNotice(Mage::helper('package')->__('Link has expired.'));
     } elseif ($linkPurchased->getState() == Faett_Package_Model_Link_Purchased::LINK_STATE_PENDING) {
         $this->_getCustomerSession()->addNotice(Mage::helper('package')->__('Link is not available.'));
     } else {
         $this->_getCustomerSession()->addNotice(Mage::helper('package')->__('Sorry, there was an error getting requested content. Please contact store owner.'));
     }
     // return to the main page
     return $this->_redirect('*/customer/products');
 }
 /**
  * Delete data by item(s)
  *
  * @param Faett_Package_Model_Link|array|int $items
  * @return Faett_Package_Model_Mysql4_Link
  */
 public function deleteItems($items)
 {
     $where = '';
     if ($items instanceof Faett_Package_Model_Link) {
         $where = $this->_getReadAdapter()->quoteInto('link_id = ?', $items->getId());
     } elseif (is_array($items)) {
         $where = $this->_getReadAdapter()->quoteInto('link_id in (?)', $items);
     } else {
         $where = $this->_getReadAdapter()->quoteInto('sample_id = ?', $items);
     }
     if ($where) {
         $this->_getWriteAdapter()->delete($this->getTable('package/link'), $where);
         $this->_getWriteAdapter()->delete($this->getTable('package/link_title'), $where);
         $this->_getWriteAdapter()->delete($this->getTable('package/link_price'), $where);
     }
     return $this;
 }
 /**
  * Enter description here...
  *
  * @param Faett_Package_Model_Link $link
  * @return string
  */
 public function getFormattedLinkPrice($link)
 {
     $price = $link->getPrice();
     if (0 == $price) {
         return '';
     }
     $_priceInclTax = Mage::helper('tax')->getPrice($link->getProduct(), $price, true);
     $_priceExclTax = Mage::helper('tax')->getPrice($link->getProduct(), $price);
     $priceStr = '<span class="price-notice">+';
     if (Mage::helper('tax')->displayPriceIncludingTax()) {
         $priceStr .= $this->helper('core')->currency($_priceInclTax, true, true);
     } elseif (Mage::helper('tax')->displayPriceExcludingTax()) {
         $priceStr .= $this->helper('core')->currency($_priceExclTax, true, true);
     } elseif (Mage::helper('tax')->displayBothPrices()) {
         $priceStr .= $this->helper('core')->currency($_priceExclTax, true, true);
         if ($_priceInclTax != $_priceExclTax) {
             $priceStr .= ' (+' . $this->helper('core')->currency($_priceInclTax, true, true) . ' ' . $this->__('Incl. Tax') . ')';
         }
     }
     $priceStr .= '</span>';
     return $priceStr;
 }