public
saveOrderDownloads
(mixed $orderProductId, mixed $productId, mixed $catId, mixed $orderId)
public function saveOrderDownloads($orderProductId, $productId, $catId, $orderId)
{
$app = Factory::getApplication();
$db = Factory::getDbo();
$pC = PhocacartUtils::getComponentParameters();
$download_product_attribute_options = $pC->get('download_product_attribute_options', 0);
$isDownloadableProduct = 0;
$forceOnlyDownloadFileAttribute = 0;
$row = Table::getInstance('PhocacartOrderDownloads', 'Table', array());
//$productItem = new PhocacartProduct();
$product = PhocacartProduct::getProduct((int) $productId, (int) $catId, $this->type);
// Additional download files
$additionalDownloadFiles = PhocacartFileAdditional::getProductFilesByProductId((int) $productId, 1);
// Attribute Option Download Files
$attributeDownloadFiles = PhocacartAttribute::getAttributeOptionDownloadFilesByOrder($orderId, $productId, $orderProductId);
// 1) download_file for ordered product
// 2) download_file for ordered attribute option of each product
$d = array();
$d['order_id'] = (int) $orderId;
$d['product_id'] = (int) $productId;
$d['order_product_id'] = (int) $orderProductId;
$d['title'] = $product->title;
$d['download_hits'] = 0;
$d['download_days'] = $product->download_days;
$d['published'] = 0;
$d['date'] = gmdate('Y-m-d H:i:s');
$d['ordering'] = 0;
// If Product includes attribute option download file, this means there can be two different products:
// a) produt without any attribute selected
// b) product with attribute selected
// So if set in options and there is a download file for attribute option - the main product download file will be skipped
if ($download_product_attribute_options == 1 && !empty($attributeDownloadFiles)) {
foreach ($attributeDownloadFiles as $k => $v) {
if (isset($v['download_file']) && $v['download_file'] != '' && isset($v['download_folder']) && $v['download_folder'] != '' && isset($v['download_token']) && isset($v['attribute_id']) && $v['attribute_id'] > 0 && isset($v['option_id']) && $v['option_id'] > 0 && isset($v['order_option_id']) && $v['order_option_id'] > 0) {
// !!! Both conditions are OK
// 1) we don't want provide a download file for main product in case that the product including attribute option is ordered (PARAMETER SET)
// 2) and yes there is selected attribute option including download file ordered (CUSTOMER ORDERED PRODUCT WITH ATTRIBUTE OPTION)
$forceOnlyDownloadFileAttribute = 1;
}
}
}
// 1)
if ($forceOnlyDownloadFileAttribute == 0 && isset($product->download_file) && $product->download_file != '') {
$d['download_token'] = $product->download_token;
$d['download_folder'] = $product->download_folder;
$d['download_file'] = $product->download_file;
$d['type'] = 1;
$db = Factory::getDbo();
//$db->setQuery('SELECT MAX(ordering) FROM #__phocacart_order_downloads WHERE catid = '.(int)$orderId);
$db->setQuery('SELECT MAX(ordering) FROM #__phocacart_order_downloads');
$max = $db->loadResult();
$d['ordering'] = $max + 1;
if (!$row->bind($d)) {
//throw new Exception($row->getError());
$msg = Text::_($row->getError());
$app->enqueueMessage($msg, 'error');
return false;
}
if (!$row->check()) {
//throw new Exception($row->getError());
$msg = Text::_($row->getErrorMsg());
$app->enqueueMessage($msg, 'error');
return false;
}
if (!$row->store()) {
//throw new Exception($row->getError());
$msg = Text::_($row->getErrorMsg());
$app->enqueueMessage($msg, 'error');
return false;
}
$isDownloadableProduct = 1;
}
// 2)
if ($forceOnlyDownloadFileAttribute == 0 && !empty($additionalDownloadFiles)) {
$d['ordering'] = $d['ordering'] + 1;
$d['download_token'] = '';
$d['download_folder'] = $product->download_folder;
$d['download_file'] = '';
$d['download_days'] = -1;
$d['type'] = 2;
foreach ($additionalDownloadFiles as $k => $v) {
if (isset($v['download_file']) && $v['download_file'] != '' && isset($d['download_folder']) && $d['download_folder'] != '' && isset($v['download_token']) && isset($v['download_days'])) {
$d['download_token'] = $v['download_token'];
$d['download_file'] = $v['download_file'];
$d['download_days'] = $v['download_days'];
$d['title'] = $product->title;
$row = Table::getInstance('PhocacartOrderDownloads', 'Table', array());
if (!$row->bind($d)) {
//throw new Exception($row->getError());
$msg = Text::_($row->getError());
$app->enqueueMessage($msg, 'error');
return false;
}
if (!$row->check()) {
//throw new Exception($row->getError());
$msg = Text::_($row->getErrorMsg());
$app->enqueueMessage($msg, 'error');
return false;
}
if (!$row->store()) {
//throw new Exception($row->getError());
$msg = Text::_($row->getErrorMsg());
$app->enqueueMessage($msg, 'error');
return false;
}
$isDownloadableProduct = 1;
$d['ordering'] = $d['ordering'] + 1;
}
}
}
// 3)
if (!empty($attributeDownloadFiles)) {
$d['ordering'] = $d['ordering'] + 1;
$d['type'] = 3;
foreach ($attributeDownloadFiles as $k => $v) {
if (isset($v['download_file']) && $v['download_file'] != '' && isset($v['download_folder']) && $v['download_folder'] != '' && isset($v['download_token']) && isset($v['attribute_id']) && $v['attribute_id'] > 0 && isset($v['option_id']) && $v['option_id'] > 0 && isset($v['order_option_id']) && $v['order_option_id'] > 0) {
$d['download_file'] = $v['download_file'];
$d['download_folder'] = $v['download_folder'];
$d['download_token'] = $v['download_token'];
$d['attribute_id'] = $v['attribute_id'];
$d['option_id'] = $v['option_id'];
$d['order_option_id'] = $v['order_option_id'];
$d['title'] = $product->title . ' (' . $v['attribute_title'] . ': ' . $v['option_title'] . ')';
$row = Table::getInstance('PhocacartOrderDownloads', 'Table', array());
if (!$row->bind($d)) {
//throw new Exception($row->getError());
$msg = Text::_($row->getError());
$app->enqueueMessage($msg, 'error');
return false;
}
if (!$row->check()) {
//throw new Exception($row->getError());
$msg = Text::_($row->getErrorMsg());
$app->enqueueMessage($msg, 'error');
return false;
}
if (!$row->store()) {
//throw new Exception($row->getError());
$msg = Text::_($row->getErrorMsg());
$app->enqueueMessage($msg, 'error');
return false;
}
$isDownloadableProduct = 1;
$d['ordering'] = $d['ordering'] + 1;
}
}
}
$this->downloadable_product = $isDownloadableProduct;
return true;
}