Select Git revision
downloads_for_sale.php
downloads_for_sale.php 12.57 KiB
<?php
defined('_JEXEC') or die( 'Direct Access to ' . basename( __FILE__ ) . ' is not allowed.' ) ;
/**
* @version $Id: downloads_for_sale.php,v 1.4 2005/05/27 19:33:57 ei
*
* A custom field plugin for downloadable files
* @author Reinhold Kainhofer
* @package VirtueMart
* @subpackage vmcustom
* @copyright Copyright (C) 2013 Reinhold Kainhofer - All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
* VirtueMart is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
*
* http://kainhofer.com
*/
if (!class_exists('vmCustomPlugin')) require(JPATH_VM_PLUGINS . DS . 'vmcustomplugin.php');
if(!class_exists('VmTable'))require(JPATH_VM_ADMINISTRATOR.DS.'helpers'.DS.'vmtable.php');
class plgVmCustomDownloads_for_Sale extends vmCustomPlugin {
var $status_allowed = array ('S', 'C');
function __construct(& $subject, $config) {
parent::__construct($subject, $config);
$varsToPush = array(
'media_id'=>array(0,'char'),
'invoice_link_type'=>array('text', 'char'),
'product_link_type'=>array('image','char'),
'download_type'=>array('free_download','char'),
);
$this->setConfigParameterable('custom_params',$varsToPush);
}
function getDownloadFiles () {
$db = JFactory::getDBO();
$db->setQuery('SELECT * FROM `#__virtuemart_medias` WHERE `file_is_downloadable`=1 OR `file_is_forSale`=1 ');
return $db->loadObjectList();
}
function getDownloadFile ($media_id) {
$db = JFactory::getDBO();
$db->setQuery('SELECT * FROM `#__virtuemart_medias` WHERE `virtuemart_media_id` = '.(int)$media_id);
return $db->loadObject();
}
function checkDownloadable ($field, &$output) {
$userId = (int) JFactory::getUser()->get('id');
if ($field->download_type == 'free_download') {
return true;
} elseif ($field->download_type == 'registered_download') {
if ($userId<=0) {
JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_NOT_AUTHORIZED'), 'error');
}
return ($userId>0);
} elseif ($field->download_type == 'paid_download') {
$order_number = JRequest::getString('order_number', null);
$order_pass = JRequest::getString('order_pass', null);
$orderModel = VmModel::getModel('orders');
$order_id = $orderModel->getOrderIdByOrderPass ($order_number, $order_pass);
if ($order_id) {
$order = $orderModel->getOrder($order_id);
if ($order && in_array($order['details']['BT']->order_status, $this->status_allowed)) {
foreach ($order['items'] as $i) {
if ($i->virtuemart_product_id == $field->virtuemart_product_id) {
if (in_array($i->order_status, $this->status_allowed)) {
return $i->virtuemart_order_item_id;
} else {
JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_STATUS_NOT_AUTHORIZED'), 'error'); }
}
}
} else {
JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_STATUS_NOT_AUTHORIZED'), 'error');
}
} else {
JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_WRONG_PASSWD'), 'error');
}
}
return false;
}
function loadCustomfieldData ($customfield_id) {
$db = JFactory::getDbo();
$q = 'SELECT * FROM `#__virtuemart_product_customfields` WHERE `virtuemart_customfield_id` = '.(int)$customfield_id . ' AND `custom_value`=\''.$db->getEscaped($this->_name).'\'';
$db->setQuery($q);
$field = $db->loadObject();
if(!$field) return false;
$err = $db->getErrorMsg();
if (!empty($err)) {
JError::raiseWarning(500, 'Downloads for sale plugin, loadCustomfieldData error '.$err);
return false;
}
$field->custom_element = $this->_name;
$this->parseCustomParams($field);
return $field;
}
function downloadFile($media_id, &$output){
if ($media_id) {
$media = $this->getDownloadFile($media_id);
if (!$media) {
JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_NO_FILE_SET'), 'error');
return false;
}
header("Content-Type: " . $media->file_mimetype);
header("Content-Disposition: attachment; filename=\"".JFile::getName($media->file_url)."\"");
if (!@readfile($media->file_url)) {
header_remove("Content-Type");
header_remove("Content-Disposition");
JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_NO_FILE_SET'), 'error');
return false;
}
JExit();
} else {
$output = JText::_('VMCUSTOM_DLSALE_NO_FILE_FOUND');
}
}
function plgVmOnSelfCallFE($type,$name,&$render) {
if ($name != $this->_name || $type != 'vmcustom') return false;
$field_id = JRequest::getInt('customfield_id',0);
if (!$field_id) {
JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_CUSTOMFIELDID'), 'error');
return false;
}
$field = $this->loadCustomfieldData ($field_id);
if (!$field) {
JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_LOAD_FAILURE'), 'error');
return false;
}
if ($this->checkDownloadable ($field, $render)) {
$this->downloadFile ($field->media_id, $render);
}
}
/** * plgVmOnSelfCallBE ... Called to execute some plugin action in the backend (e.g. set/reset dl counter, show statistics etc.)
*/
/* function plgVmOnSelfCallBE($type, $name, &$output) {
if ($name != $this->_name || $type != 'vmcustom') return false;
vmDebug('plgVmOnSelfCallBE');
$db = JFactory::getDBO();
$nullDate = $db->getNullDate();
}*/
/**
* @see Form displayed in the product edit page in the BE, configure the download file
* @author Reinhold Kainhofer
*/
function plgVmOnProductEdit($field, $product_id, &$row,&$retValue) {
if ($field->custom_element != $this->_name) return '';
$this->parseCustomParams($field);
$html = '';
$html .='<fieldset>
<legend>'. JText::_('VMCUSTOM_DLSALE') .'</legend>
<table class="admintable">
';
$download_files = $this->getDownloadFiles();
if (!$download_files) {
$html .= '<tr><td>'.JText::_('VMCUSTOM_DLSALE_NO_FILES').'</td></tr>';
$html .= '</table></fieldset>';
$retValue .= $html;
return true;
}
$link_types = array('none' => 'VMCUSTOM_DLSALE_LINK_NONE', 'compact'=>'VMCUSTOM_DLSALE_LINK_COMPACT', 'long'=>'VMCUSTOM_DLSALE_LINK_LONG');
$html .= VmHTML::row('select', 'VMCUSTOM_DLSALE_DOWNLOAD_FILE', 'custom_param['.$row.'][media_id]', $download_files, $field->media_id,'','virtuemart_media_id', 'file_title', false);
if ($field->is_cart_attribute) {
$html .= VmHTML::row ('select','VMCUSTOM_DLSALE_INVOICE_LINK', 'custom_param['.$row.'][invoice_link_type]',$link_types,$field->invoice_link_type, '', '', '', false);
}
$html .= VmHTML::row ('select','VMCUSTOM_DLSALE_PRODUCT_LINK', 'custom_param['.$row.'][product_link_type]',$link_types,$field->product_link_type, '', '', '', false);
$html .= VmHTML::row('select', 'VMCUSTOM_DLSALE_TYPE', 'custom_param['.$row.'][download_type]',
array('free_download'=>'VMCUSTOM_DLSALE_TYPE_FREE', 'registered_download'=>'VMCUSTOM_DLSALE_TYPE_REGISTERED', 'paid_download'=>'VMCUSTOM_DLSALE_TYPE_PAID'),
$field->download_type,'','', '', false);
$html .= '</table></fieldset>';
$retValue .= $html;
$row++;
return true ;
}
function createDownloadLink ($field, $view='order', $type='none', $orderitem=null) {
$url = JURI::root().'index.php?option=com_virtuemart&view=plugin&name=downloads_for_sale&customfield_id='.(int)$field->virtuemart_customfield_id;
$_currentUser = JFactory::getUser();
$cuid = $_currentUser->get('id');
$this->parseCustomParams($field);
switch ($field->download_type) {
case 'free_download': break;
case 'registered_download': if ($cuid<=0) return false; break;
case 'paid_download':
if ($orderitem) {
$order = $this->orderDataFromItem ($orderitem->virtuemart_order_item_id);
} elseif ($cuid>0) {
$orderModel = VmModel::getModel('orders');
foreach ($orderModel->getOrdersList($cuid, true) as $ol) {
$o = $orderModel->getOrder($ol->virtuemart_order_id);
foreach ($o['items'] as $i) {
if ($i->virtuemart_product_id == $field->virtuemart_product_id && in_array($i->order_status, $this->status_allowed)) {
$order = $o['details']['BT'];
}
} }
}
if (empty($order)) {
return false;
}
$url .= '&order_number='.urlencode($order->order_number).'&order_pass='.urlencode($order->order_pass);
break;
}
$media = $this->getDownloadFile ($field->media_id);
return $this->renderByLayout($view.'_'.$type, array($url, $field, $media));
}
/**
* plgVmOnDisplayProductVariantFE ... Called for product variant custom fields to display on the product details page
*/
function plgVmOnDisplayProductVariantFE($field,&$row,&$group) {
// default return if it's not this plugin
if ($field->custom_element != $this->_name) return '';
$this->parseCustomParams($field);
$type = $field->product_link_type;
if ($type != 'none') {
$dllink = $this->createDownloadLink($field, "product", $type);
if (!empty($dllink)) {
$group->display .= $dllink;
return true;
} else {
return false;
}
} else {
return false;
}
}
/**
* plgVmOnDisplayProductFE ... Called for NON-product variant custom fields to display on the product details page
*/
function plgVmOnDisplayProductFE( $product, &$idx,&$field){
// default return if it's not this plugin
if ($field->custom_element != $this->_name) return '';
$this->parseCustomParams($field);
$type = $field->product_link_type;
if ($type != 'none') {
// The $field for non-cart-variant fields does NOT have the product_id set!
$field->virtuemart_product_id = $product->virtuemart_product_id;
$ret = $this->createDownloadLink($field, "product", $type);
if (!empty($ret)) {
$field->display .= $ret;
return true;
} else
return false;
} else {
return false;
}
}
/**
* plgVmDisplayInOrderBE ... Called to display product variants in the order list in the BE
*/
function plgVmDisplayInOrderBE($item, $row, &$html) {
if (empty($item->productCustom->custom_element) or $item->productCustom->custom_element != $this->_name) return '';
// MODIFY $html to display info in the backend order
}
function orderDataFromItem ($orderitem) {
$db = JFactory::getDBO();
$q = "SELECT *
FROM `#__virtuemart_order_items` JOIN `#__virtuemart_orders` USING (`virtuemart_order_id`)
WHERE `virtuemart_order_item_id` = ".(int)$orderitem;
$db->setQuery($q);
return $db->loadObject(); }
/**
* plgVmDisplayInOrderFE ... Called to display the dl link in the order in the frontend
*/
function plgVmDisplayInOrderFE($item, $row, &$html) {
if (empty($item->productCustom->custom_element) or $item->productCustom->custom_element != $this->_name) return '';
$this->parseCustomParams($item->productCustom);
$type = $item->productCustom->product_link_type;
if ($type != 'none') {
$html .= $this->createDownloadLink($item->productCustom, "order", $type, $item);
return true;
} else {
return false;
}
}
/**
* We must reimplement this triggers for joomla 1.7
* vmplugin triggers note by Max Milbers
*/
public function plgVmOnStoreInstallPluginTable($psType) {
return $this->onStoreInstallPluginTable($psType);
}
function plgVmDeclarePluginParamsCustom($psType,$name,$id, &$data){
return $this->declarePluginParams('custom', $name, $id, $data);
}
function plgVmSetOnTablePluginParamsCustom($name, $id, &$table){
return $this->setOnTablePluginParams($name, $id, $table);
}
function plgVmOnDisplayEdit($virtuemart_custom_id,&$customPlugin){
return $this->onDisplayEditBECustom($virtuemart_custom_id,$customPlugin);
}
/**
* Create the database table for this plugin. So far, we don't store
* anything, so don't create the table, either.
*/
public function getVmPluginCreateTableSQL() {
//return $this->createTableSQL('Spiral Downloads tracking table');
}
function getTableSQLFields() {
/* $SQLfields = array(
'id' => 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT',
'virtuemart_customfield_id' => 'int(11) UNSIGNED NOT NULL DEFAULT 0',
'downloaded' => 'int(11) UNSIGNED NOT NULL DEFAULT 0',
);
return $SQLfields;*/
return array();
}
}
// No closing tag