Skip to content
Snippets Groups Projects
Commit 034e79ee authored by Reinhold Kainhofer's avatar Reinhold Kainhofer
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
.svn
*~
cache
*.kate-swp
logs
tmp
<?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 {
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 hasBoughtItem ($uid, $customitem) {
}
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') {
// if ($this->hasBoughtItem ($userId, $field->
// TODO: check whether the logged-in user has bought the product or the correct ordernumber/password is given
$order_number = JRequest::getString('order_number', null);//JRequest::getString('username', null, 'GET', JREQUEST_ALLOWRAW);
$order_pass = JRequest::getString('order_pass', null);
$orderModel = VmModel::getModel('orders');
$order_id = $orderModel->getOrderIdByOrderPass ($order_number, $order_pass);
if (!order_id) {
JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_WRONG_PASSWD'), 'error');
return false;
}
return $order_id;
}
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;
}
// print("<pre>checkDownloadable</pre>");
if ($this->checkDownloadable ($field, $render)) {
// print("<pre>checkDownloadable SUCCESS</pre>");
$this->downloadFile ($field->media_id, $render);
// } else {
// print("<pre>checkDownloadable FAILURE</pre>");
}
}
/*
$dlkey = JRequest::getString('dlkey', 0);
$dloid = JRequest::getInt('oid', 0);
if ( !$order =$this->getOrderData( $dlkey, $dloid ) ) {
vmInfo(JText::_('VMCUSTOM_SPIRAL_DOWNLOAD_NO_FILE_FOUND'));
return ;
}
$order_status = $order['order_status'];
$params = $this->getMediaParams($custom_id, $order['virtuemart_product_id']);
$status = $params->orderstatus;
if(empty($status))
{
$paramStatus = array("C","S");
}
else
{
$paramStatus = (array)$params->orderstatus;
}
//if(empty($paramStatus))
//vmDebug('plgVmOnSelfCallFE - params',$params);
if (in_array($order_status,$paramStatus)) {
$orderdata = new stdClass();
$orderdata->virtuemart_order_id = $order['virtuemart_order_id'];
$orderdata->virtuemart_order_item_id = $dloid;
$orderdata->created_on = $order['created_on'];
//update downloads table
if ($this->checkDownloadTable($orderdata,$custom_id,$params))
{
$orderModel = VmModel::getModel('orders');
$orderdata->order_status = 'S';
$orderModel->updateSingleItem($dloid,$orderdata);
return $this->startDownload($params->media_id, $output);
}
else
{
vmInfo(JText::_('VMCUSTOM_SPIRAL_DOWNLOAD_EXPIRED'));
}
} else vmInfo(JText::_('VMCUSTOM_SPIRAL_DOWNLOAD_PAYMENT_NOT_CONFIRMED'));
return;
}
*/
/**
* plgVmOnSelfCallBE ... Called to execute some plugin action in the backend (e.g. set/reset dl counter 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', 'text'=>'VMCUSTOM_DLSALE_LINK_TEXT', 'image'=>'VMCUSTOM_DLSALE_LINK_IMAGE');
$html .= VmHTML::row('select', 'VMCUSTOM_DLSALE_DOWNLOAD_FILE', 'custom_param['.$row.'][media_id]', $download_files, $field->media_id,'','virtuemart_media_id', 'file_title', false);
$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');
switch ($field->download_type) {
case 'free_download': break;
case 'registered_download': break;
case 'paid_download':
if ($orderitem) {
$order = $this->orderDataFromItem ($orderitem->virtuemart_order_item_id);
} elseif ($cuid>0) {
$orderModel = VmModel::getModel('orders');
$orderList = $orderModel->getOrdersList($cuid, true);
foreach ($orderList as $ol) {
$o = $orderModel->getOrder($ol->virtuemart_order_id);
foreach ($o['items'] as $i) {
if ($i->virtuemart_product_id == $field->virtuemart_product_id) {
$order = $o['details']['BT'];
}
}
}
}
if (empty($order)) {
return false;
}
print("<pre>order= ".print_r($order,true)."</pre>");
$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($field, $media, $url));
}
/**
* 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,&$group){
// default return if it's not this plugin
if ($field->custom_element != $this->_name) return '';
$group->display .= "<h1>plgVmOnDisplayProductFE</h1>";
$this->getCustomParams($field);
$type = $field->product_link_type;
if ($type != 'none') {
$group->display .= $this->createDownloadLink($field, "product", $type);
return true;
} 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);
// print("<pre>plgVmDisplayInOrderFE, item=".print_r($item,true)."</pre>");
$type = $item->productCustom->product_link_type;
if ($type != 'none') {
$html .= $this->createDownloadLink($item->productCustom, "product", $type, $item);
return true;
} else {
return false;
}
}
/*
$_currentUser = JFactory::getUser();
$cuid = $_currentUser->get('id');
$orderModel = VmModel::getModel('orders');
$orderList = $orderModel->getOrdersList($cuid, true);
foreach ($orderList as $o) {
$order = $orderModel->getOrder($o->virtuemart_order_id);
foreach ($order['items'] as $i) {
if ($i->virtuemart_product_id == $field->virtuemart_product_id) {
$downloadInfo = $this->getDownloadInfo($order['details']['BT']->virtuemart_order_id, $i->virtuemart_order_item_id, $field->value, $order['details']['BT']->created_on, $field );
if(!$downloadInfo->downloadsExpired) {
$url = JURI::root().'index.php?option=com_virtuemart&view=plugin&name=downloads_for_sale&custom_id='.$field->value . '&dlkey='.base64_encode($order['details']['BT']-> ).'&oid='.$i->virtuemart_order_item_id ;
$html .= JHTML::link ( $url, JTEXT::_('VMCUSTOM_DLSALE_LINK') , array ('title' => $media->title, 'class' => 'dlink' ) );
} else {
$html .= '<p><strong>'.JText::_('VMCUSTOM_DLSALE_EXPIRED_INFO').'</strong></p>';
}
}
}
}
*/
/**
* 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
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8" ?>
<install version="1.5" type="plugin" group="vmcustom" >
<name>VMCUSTOM_DLSALE</name>
<creationDate>January 26, 2013</creationDate>
<author>Reinhold Kainhofer</author>
<authorUrl>http://www.kainhofer.com/</authorUrl>
<copyright>Copyright (C) 2013 Reinhold Kainhofer. All rights reserved.</copyright>
<license>http://www.gnu.org/licenses/gpl.html GNU/GPL v3+</license>
<version>0.0.1</version>
<description>VMCUSTOM_DLSALE_DESC</description>
<languages>
<language tag="en-GB">en-GB.plg_vmcustom_downloads_for_sale.ini</language>
</languages>
<files>
<filename plugin="downloads_for_sale">downloads_for_sale.php</filename>
</files>
<params addpath="/administrator/components/com_virtuemart/elements">
<param type="spacer" default="VMCUSTOM_DLSALE_USAGE" />
<param name="invoice_link_type" type="list" default="link" label="VMCUSTOM_DLSALE_INVOICE_LINK" description="VMCUSTOM_DLSALE_INVOICE_LINK_DESC" >
<option value="none">VMCUSTOM_DLSALE_LINK_NONE</option>
<option value="text">VMCUSTOM_DLSALE_LINK_TEXT</option>
<option value="image">VMCUSTOM_DLSALE_LINK_IMAGE</option>
</param>
<param name="product_link_type" type="list" default="image" label="VMCUSTOM_DLSALE_PRODUCT_LINK" description="VMCUSTOM_DLSALE_PRODUCT_LINK_DESC" >
<option value="none">VMCUSTOM_DLSALE_LINK_NONE</option>
<option value="text">VMCUSTOM_DLSALE_LINK_TEXT</option>
<option value="image">VMCUSTOM_DLSALE_LINK_IMAGE</option>
</param>
<param name="download_type" type="list" default="yes" label="VMCUSTOM_DLSALE_TYPE" description="VMCUSTOM_DLSALE_TYPE_DESC">
<option value="free_download">VMCUSTOM_DLSALE_TYPE_FREE</option>
<option value="registered_download">VMCUSTOM_DLSALE_TYPE_REGISTERED</option>
<option value="paid_download">VMCUSTOM_DLSALE_TYPE_PAID</option>
</param>
</params>
</install>
<?php
/**
*
* @author Reinhold Kainhofer
* @package VirtueMart
* @subpackage custom
* @copyright Copyright (C) 2013 RK - All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*
**/
defined('_JEXEC') or die();
?>
<h1>IMAGE LINK</h1>
<?php
/**
*
* @author Reinhold Kainhofer
* @package VirtueMart
* @subpackage custom
* @copyright Copyright (C) 2013 RK - All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*
**/
defined('_JEXEC') or die();
?>
<h1>TEXT LINK</h1>
<?php
/**
*
* @author Reinhold Kainhofer
* @package VirtueMart
* @subpackage custom
* @copyright Copyright (C) 2013 RK - All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*
**/
defined('_JEXEC') or die();
?>
<h1>IMAGE LINK</h1><pre>
<?php // print_r($viewData) ?></pre>
<a href="<?php echo $viewData[2]; ?>">Download</a>
<?php
/**
*
* @author Reinhold Kainhofer
* @package VirtueMart
* @subpackage custom
* @copyright Copyright (C) 2013 RK - All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*
**/
defined('_JEXEC') or die();
?>
<h1>TEXT LINK</h1>
<?php print_r($viewData) ?></pre>
<a href="<?php echo $viewData[2]; ?>">Download</a>
; Virtuemart! Project
; Copyright (C) 2011 Virtuemart Team. All rights reserved.
; License http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
; Note : All ini files need to be saved as UTF-8 - No BOM
VMCUSTOM_DLSALE="Downloadable products"
VMCUSTOM_DLSALE_DESC="Assign a downloadable media file to the product."
VMCUSTOM_DLSALE_USAGE="TODO"
VMCUSTOM_DLSALE_INVOICE_LINK="Link type in Invoices"
VMCUSTOM_DLSALE_INVOICE_LINK_DESC="Download link type displaye in the invoices and order views."
VMCUSTOM_DLSALE_PRODUCT_LINK="Link type on Product Page"
VMCUSTOM_DLSALE_PRODUCT_LINK_DESC="Download link type displayed on the product page for permitted users."
VMCUSTOM_DLSALE_LINK_NONE="No link displayed"
VMCUSTOM_DLSALE_LINK_TEXT="Text link"
VMCUSTOM_DLSALE_LINK_IMAGE="Image"
VMCUSTOM_DLSALE_TYPE="Type of download"
VMCUSTOM_DLSALE_TYPE_DESC="Select the default type of download"
VMCUSTOM_DLSALE_TYPE_FREE="Free download"
VMCUSTOM_DLSALE_TYPE_REGISTERED="Download after registration"
VMCUSTOM_DLSALE_TYPE_PAID="Paid download"
VMCUSTOM_DLSALE_NO_FILES="No media files found for download or sale."
VMCUSTOM_DLSALE_DOWNLOAD_FILE="Download file"
VMCUSTOM_DLSALE_ERROR_NOT_AUTHORIZED="Not authorized to download. Please log in."
VMCUSTOM_DLSALE_ERROR_CUSTOMFIELDID="Custom field ID missing in the URL."
VMCUSTOM_DLSALE_ERROR_LOAD_FAILURE="Unable to load custom field data."
VMCUSTOM_DLSALE_ERROR_NO_FILE_SET="Media file is not available."
VMCUSTOM_DLSALE_ERROR_ORDER_NOT_FOUND="Unable to find order data in database."
VMCUSTOM_DLSALE_ERROR_WRONG_PASSWD="Invalid combination of order number and password."
; Virtuemart! Project
; Copyright (C) 2011 Virtuemart Team. All rights reserved.
; License http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL, see LICENSE.php
; Note : All ini files need to be saved as UTF-8 - No BOM
VMCUSTOM_DLSALE="VM Custom Field for downloadable products"
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment