diff --git a/Makefile b/Makefile index 6e9f64cb6435d00beaae789aa2de7711e5314242..729711b97309dc7f137d60dc1f9f4fea6e401265 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ BASE=downloads_for_sale PLUGINTYPE=vmcustom -VERSION=1.0 +VERSION=1.1 PLUGINFILES=$(BASE).php $(BASE).script.php $(BASE).xml index.html diff --git a/downloads_for_sale.php b/downloads_for_sale.php index e9dea2620c577dd50bf7f20ff21b004c780af6bd..f25416ea5767dfb39ea533239249df586379ac00 100644 --- a/downloads_for_sale.php +++ b/downloads_for_sale.php @@ -23,6 +23,8 @@ class plgVmCustomDownloads_for_Sale extends vmCustomPlugin { function __construct(& $subject, $config) { parent::__construct($subject, $config); + $this->_tablepkey = 'id'; + $this->tableFields = array_keys($this->getTableSQLFields()); $varsToPush = array( 'media_id'=>array(0,'char'), 'invoice_link_type'=>array('text', 'char'), @@ -30,6 +32,7 @@ class plgVmCustomDownloads_for_Sale extends vmCustomPlugin { 'download_type'=>array('free_download','char'), ); $this->setConfigParameterable('custom_params',$varsToPush); + $this->onStoreInstallPluginTable($this->_psType); } function getDownloadFiles () { @@ -43,39 +46,76 @@ class plgVmCustomDownloads_for_Sale extends vmCustomPlugin { 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'); + function getCustomfieldOrderitems ($field, $user, $show_warnings) { + $orderModel = VmModel::getModel('orders'); + $order_number = JRequest::getString('order_number', null); + $order_pass = JRequest::getString('order_pass', null); + $orders = array(); + if ($order_number && $order_pass) { $order_id = $orderModel->getOrderIdByOrderPass ($order_number, $order_pass); + if ($order_id) { + $orders[] = $orderModel->getOrder($order_id); + } + if (empty($orders) && $show_warnings) { + JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_WRONG_PASSWD'), 'error'); + } + } elseif ($order_number) { + // Check if user has access + $order_id = $orderModel->getOrderIdByOrderNumber ($order_number); 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'); - } + if ($order['details']['BT']->virtuemart_user_id == $user) { + $orders[] = $order; + } elseif ($show_warnings) { + JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_NOT_AUTHORIZED'), 'error'); + } + } elseif ($show_warnings) { + JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_ORDER_NOT_FOUND'), 'error'); + } + } else { + if ($user>0) { + $os = $orderModel->getOrdersList($user, true); + foreach ($os as $o) { + $orders[] = $orderModel->getOrder($o->virtuemart_order_id); + } + } + } + $prodid = $field->virtuemart_product_id; + if (empty($prodid)) { + JFactory::getApplication()->enqueueMessage(JText::sprintf("WARNING: no product ID given in custom field. Please contact the plugin developer and copy this message: <pre>%s</pre>", print_r($field,1)), 'error'); + } + $orderitems = array(); + foreach ($orders as $order) { + if ($order && in_array($order['details']['BT']->order_status, $this->status_allowed)) { + foreach ($order['items'] as $i) { + if ($i->virtuemart_product_id == $prodid) { + if (in_array($i->order_status, $this->status_allowed)) { + $orderitems[] = $i; + } elseif ($show_warnings) { + 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 $orderitems; + } + + function checkDownloadable ($field, $orderitem=null, $show_warnings=false) { + // Free downloads are always allowed + if ($field->download_type == 'free_download') { + return true; + } + // For registered downloads it suffices to be logged in (that's not neccessary, giving + // the order nr/pwd should also work, so don't return false here!) + $userId = (int) JFactory::getUser()->get('id'); + if (($field->download_type == 'registered_download') && ($userId>0)) { + return ($userId>0); + } + // In all other cases, check that the orderitem has the correct state + if ($orderitem && in_array($orderitem->order_status, $this->status_allowed)) { + return true; + } return false; } @@ -117,31 +157,66 @@ class plgVmCustomDownloads_for_Sale extends vmCustomPlugin { } + function updateDownloadCounterSQL ($field, $orderitem) { + $db = JFactory::getDBO(); + $sql = "SELECT `id` FROM `" . $this->_tablename . "` WHERE `virtuemart_customfield_id`=" . (int)$field->virtuemart_customfield_id . + " AND `virtuemart_order_item_id`=" . (int)($orderitem?$orderitem->virtuemart_order_item_id:0); + $user = (int)JFactory::getUser()->get('id'); + $date = JFactory::getDate()->toMySQL(); + + $db->setQuery($sql); + $id = $db->loadResult(); + + if ($id) { + // Entry found, update downloaded + $sql = "UPDATE `" . $this->_tablename . "` SET `downloaded`=(`downloaded`+1), `modified_on` = '" . $date . "', `modified_by`=" . (int)$user . " WHERE `id`=" . (int)$id; + $db->setQuery($sql); + $db->query(); + } else { + // New entry, insert with value 1 + $sql = "INSERT INTO `" . $this->_tablename . + "` (`virtuemart_customfield_id`, `virtuemart_order_item_id`, `downloaded`, `created_on`, `created_by`) " . + "VALUES (" . (int)$field->virtuemart_customfield_id . ", " . (int)($orderitem?($orderitem->virtuemart_order_item_id):0) . + ", 1, '" . $date . "', " . (int)$user . ")"; + $db->setQuery($sql); + $db->query(); + } + + } + function plgVmOnSelfCallFE($type,$name,&$render) { if ($name != $this->_name || $type != 'vmcustom') return false; $handled = false; $field_id = JRequest::getInt('customfield_id',0); + $customModel = VmModel::getModel('customfields'); if ($field_id) { + $field = $this->loadCustomfieldData ($field_id); + $this->parseCustomParams($field); + $orderitems = $this->getCustomfieldOrderitems ($field, JFactory::getUser()->get('id'), true); + $orderitem = empty($orderitems)?null:$orderitems[0]; if ($field) { - if ($this->checkDownloadable ($field, $render)) { + if ($this->checkDownloadable ($field, $orderitem, true)) { + // TODO: Update download counter in the mysql table + $this->updateDownloadCounterSQL ($field, $orderitem); $handled = true; $this->downloadFile ($field->media_id, $render); return true; } else { - JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_NOT_AUTHORIZED_UNKNOWN'), 'error'); +// JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_NOT_AUTHORIZED_UNKNOWN'), 'error'); } } else { JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_LOAD_FAILURE'), 'error'); } } if (!$handled) { + $user = JFactory::getUser()->get('id'); // If we have an order number and password, display all downloads of that order, even if the user is not logged in: $field_id = JRequest::getInt('customfield_id',0); $order_number = JRequest::getString('order_number', null); $order_pass = JRequest::getString('order_pass', null); $orders = array(); - if ($order_number || $order_pass) { + if ($order_number && $order_pass) { $orderModel = VmModel::getModel('orders'); $order_id = $orderModel->getOrderIdByOrderPass ($order_number, $order_pass); if ($order_id) { @@ -150,12 +225,22 @@ class plgVmCustomDownloads_for_Sale extends vmCustomPlugin { if (empty($orders)) { JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_WRONG_PASSWD'), 'error'); } + } elseif ($order_number) { + $orderModel = VmModel::getModel('orders'); + $order_id = $orderModel->getOrderIdByOrderNumber ($order_number); + if ($order_id) { + $order = $orderModel->getOrder($order_id); + if ($order['details']['BT']->virtuemart_user_id == $user) { + $orders[] = $order; + } + } + if (empty($orders)) { + JFactory::getApplication()->enqueueMessage(JText::_('VMCUSTOM_DLSALE_ERROR_NOT_AUTHORIZED'), 'error'); + } } else { - $_currentUser = JFactory::getUser(); - $cuid = $_currentUser->get('id'); - if ($cuid>0) { + if ($user>0) { $orderModel = VmModel::getModel('orders'); - $os = $orderModel->getOrdersList($cuid, true); + $os = $orderModel->getOrdersList($user, true); foreach ($os as $o) { $orders[] = $orderModel->getOrder($o->virtuemart_order_id); } @@ -163,7 +248,6 @@ class plgVmCustomDownloads_for_Sale extends vmCustomPlugin { } - $customModel = VmModel::getModel('customfields'); $orderdownloads = array(); foreach ($orders as $order) { if (!in_array($order['details']['BT']->order_status, $this->status_allowed)) { @@ -177,7 +261,7 @@ class plgVmCustomDownloads_for_Sale extends vmCustomPlugin { $productinfo->downloads = array(); $customs = (array)$customModel->getproductCustomslist ($i->virtuemart_product_id); foreach ($customs as $field) { - if ($field->custom_element == $this->_name) { + if (($field->custom_element == $this->_name) && ($this->checkDownloadable ($field, $i, false))) { $productinfo->downloads[] = $this->createDownloadLink ($field, 'order', $field->invoice_link_type, $i); } } @@ -190,7 +274,7 @@ class plgVmCustomDownloads_for_Sale extends vmCustomPlugin { } } - print $this->renderByLayout('downloads', array($orderdownloads)); + print $this->renderByLayout('downloads', array($orderdownloads, $order_number)); return true; } } @@ -251,6 +335,7 @@ class plgVmCustomDownloads_for_Sale extends vmCustomPlugin { $_currentUser = JFactory::getUser(); $cuid = $_currentUser->get('id'); $this->parseCustomParams($field); + // TODO: Extract download statistics switch ($field->download_type) { case 'free_download': break; case 'registered_download': if ($cuid<=0) return false; break; @@ -278,6 +363,30 @@ class plgVmCustomDownloads_for_Sale extends vmCustomPlugin { return $this->renderByLayout($view.'_'.$type, array($url, $field, $media)); } + function createProductDownloadLinks ($field, $type) { + $return = ''; + if ($type != 'none') { + $items = $this->getCustomfieldOrderitems ($field, JFactory::getUser()->get('id'), false); + + foreach ($items as $i) { + if ($this->checkDownloadable ($field, $i, false)) { + $dllink = $this->createDownloadLink($field, "product", $type, $i); + if (!empty($dllink)) { + $return .= $dllink; + } + } + } + // Handle downloads without a corresponding order + if (empty($items) && $this->checkDownloadable ($field, null, false)) { + $dllink = $this->createDownloadLink($field, "product", $type, null); + if (!empty($dllink)) { + $return .= $dllink; + } + } + } + return $return; + } + /** * plgVmOnDisplayProductVariantFE ... Called for product variant custom fields to display on the product details page */ @@ -285,15 +394,10 @@ class plgVmCustomDownloads_for_Sale extends vmCustomPlugin { // 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; - } + $output = $this->createProductDownloadLinks ($field, $field->product_link_type); + if (!empty($output)) { + $group->display .= $output; + return true; } else { return false; } @@ -305,17 +409,12 @@ class plgVmCustomDownloads_for_Sale extends vmCustomPlugin { function plgVmOnDisplayProductFE( $product, &$idx,&$field){ // default return if it's not this plugin if ($field->custom_element != $this->_name) return ''; + $field->virtuemart_product_id = $product->virtuemart_product_id; $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; + $output = $this->createProductDownloadLinks ($field, $field->product_link_type); + if (!empty($output)) { + $field->display .= $output; + return true; } else { return false; } @@ -343,23 +442,28 @@ class plgVmCustomDownloads_for_Sale extends vmCustomPlugin { */ 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; + $type = $item->productCustom->invoice_link_type; + + $return = false; if ($type != 'none') { - $html .= $this->createDownloadLink($item->productCustom, "order", $type, $item); - return true; - } else { - return false; + if ($this->checkDownloadable ($item->productCustom, $item, false)) { + $dllink = $this->createDownloadLink($item->productCustom, "order", $type, $item); + if (!empty($dllink)) { + $html .= $dllink; + $return = true; + } + } } + return $return; } /** * We must reimplement this triggers for joomla 1.7 * vmplugin triggers note by Max Milbers */ - public function plgVmOnStoreInstallPluginTable($psType) { - return $this->onStoreInstallPluginTable($psType); + public function plgVmOnStoreInstallPluginTable($psType, $name) { + return $this->onStoreInstallPluginTable($psType, $name); } function plgVmDeclarePluginParamsCustom($psType,$name,$id, &$data){ @@ -374,23 +478,36 @@ class plgVmCustomDownloads_for_Sale extends vmCustomPlugin { return $this->onDisplayEditBECustom($virtuemart_custom_id,$customPlugin); } + /** + * This function is called, when the order is confirmed by the shopper. + * + * Here are the last checks done by payment plugins. + * The mails are created and send to vendor and shopper + * will show the orderdone page (thank you page) + * + */ + function plgVmConfirmedOrder($cart, $order) { + // TODO: Add record to the database to store # of downloads etc. + // TODO +JFactory::getApplication()->enqueueMessage("plgVmConfirmedOrder", 'error'); + + } /** - * Create the database table for this plugin. So far, we don't store - * anything, so don't create the table, either. + * Create the database table for this plugin. */ public function getVmPluginCreateTableSQL() { - //return $this->createTableSQL('Spiral Downloads tracking table'); + return $this->createTableSQL('Downloads for Sale tracking'); } function getTableSQLFields() { -/* $SQLfields = array( + $SQLfields = array( 'id' => 'int(1) UNSIGNED NOT NULL AUTO_INCREMENT', 'virtuemart_customfield_id' => 'int(11) UNSIGNED NOT NULL DEFAULT 0', + 'virtuemart_order_item_id' => 'int(11) UNSIGNED NULL DEFAULT 0', 'downloaded' => 'int(11) UNSIGNED NOT NULL DEFAULT 0', ); - return $SQLfields;*/ - return array(); + return $SQLfields; } } diff --git a/downloads_for_sale.xml b/downloads_for_sale.xml index 3b325a6518c3f4fc1af3ea5b5af7cf0c948052cb..2e6bc016c5737bc65a8148942f913b51ea731c54 100644 --- a/downloads_for_sale.xml +++ b/downloads_for_sale.xml @@ -6,7 +6,7 @@ <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>1.0.0</version> + <version>1.1.0</version> <description>VMCUSTOM_DLSALE_DESC</description> <files> <filename plugin="downloads_for_sale">downloads_for_sale.php</filename> diff --git a/downloads_for_sale/tmpl/downloads.php b/downloads_for_sale/tmpl/downloads.php index 566f801e4f2680371b153796de0ac816a6dadbae..b1c2277ce0e6fd4597badd47826887c4dc397226 100644 --- a/downloads_for_sale/tmpl/downloads.php +++ b/downloads_for_sale/tmpl/downloads.php @@ -42,8 +42,12 @@ if(JFile::exists(JPATH_SITE.DS.'plugins'.DS.'vmcustom'.DS.'downloads_for_sale'.D </form> </p> -<?php if (empty($viewData[0])) { ?> - <h2><?php echo JText::_('VMCUSTOM_DLSALE_DLPAGE_NO_DOWNLOADS'); ?></h2> +<?php if (empty($viewData[0])) { + if (empty($viewData[1])) { ?> + <h2><?php echo JText::_('VMCUSTOM_DLSALE_DLPAGE_NO_DOWNLOADS'); ?></h2> + <?php } else { ?> + <h2><?php echo JText::sprintf('VMCUSTOM_DLSALE_DLPAGE_ORDER_NO_DOWNLOADS', $viewData[1]); ?></h2> + <?php } ?> <?php } else { ?> <h2><?php echo JText::_('VMCUSTOM_DLSALE_DLPAGE_DOWNLOADS'); ?></h2> diff --git a/en-GB.plg_vmcustom_downloads_for_sale.ini b/en-GB.plg_vmcustom_downloads_for_sale.ini index 2084039679bd22094558a8e93a1cf442e2640794..f713c6354753a88422188c36af37857396fd4d6d 100644 --- a/en-GB.plg_vmcustom_downloads_for_sale.ini +++ b/en-GB.plg_vmcustom_downloads_for_sale.ini @@ -35,6 +35,7 @@ VMCUSTOM_DLSALE_ERROR_NOT_AUTHORIZED_UNKNOWN="Not authorized to download." VMCUSTOM_DLSALE_DLPAGE_TITLE="Downloads for Sale" VMCUSTOM_DLSALE_DLPAGE_BUTTON_VIEW="See Downloads" VMCUSTOM_DLSALE_DLPAGE_NO_DOWNLOADS="No Downloads Available" +VMCUSTOM_DLSALE_DLPAGE_ORDER_NO_DOWNLOADS="No Downloads Available for order %s" VMCUSTOM_DLSALE_DLPAGE_DOWNLOADS="Available Downloads" VMCUSTOM_DLSALE_DLPAGE_ORDERPRODUCT="Order number %s: %s (%s)" diff --git a/images/downloads_for_sale_-_downloadaread_menuitem1.png b/images/downloads_for_sale_-_downloadaread_menuitem1.png new file mode 100644 index 0000000000000000000000000000000000000000..bba3c940e055103f2014b1c59b3311281cd4ac63 Binary files /dev/null and b/images/downloads_for_sale_-_downloadaread_menuitem1.png differ diff --git a/releases/plg_vmcustom_downloads_for_sale_v1.1.zip b/releases/plg_vmcustom_downloads_for_sale_v1.1.zip new file mode 100644 index 0000000000000000000000000000000000000000..0d52bf8024e634522aab62cd8609e961967c5703 Binary files /dev/null and b/releases/plg_vmcustom_downloads_for_sale_v1.1.zip differ