From ade4a10520ff1fb22f1ca8b78c25c17ef31cc8be Mon Sep 17 00:00:00 2001 From: Poonam Baravkar Date: Mon, 14 Oct 2024 20:20:13 +0530 Subject: [PATCH 1/2] Task #228793 chore: Logs implenetation for import ucm functionality --- .../languages/site/en-GB/en-GB.com_tjucm.ini | 5 + .../com_tjucm/site/controllers/items.php | 94 ++++++++++++++++++- 2 files changed, 97 insertions(+), 2 deletions(-) diff --git a/src/components/com_tjucm/languages/site/en-GB/en-GB.com_tjucm.ini b/src/components/com_tjucm/languages/site/en-GB/en-GB.com_tjucm.ini index e51f8b45..c42c2f70 100644 --- a/src/components/com_tjucm/languages/site/en-GB/en-GB.com_tjucm.ini +++ b/src/components/com_tjucm/languages/site/en-GB/en-GB.com_tjucm.ini @@ -223,3 +223,8 @@ COM_TJUCM_ITEM_COPY_TO_QUEUE_SUCCESSFULLY="Item Successfully saved to queue for ; Since 1.2.5 COM_TJUCM_FILTER_SELECT_CATEGORY_LABEL="Select Category" +COM_TJUCM_INVALID_IMPORT_RECORD_ROW="Invalid import record on row number %s" +COM_TJUCM_IMPORT_FIELD_VALUE_EMPTY="Field value of required field is empty or Invalid %s" +COM_TJUCM_IMPORT_FIELD_VALUE_EMPTY_ON_ROW_NO="Invalid import record on row number %s" +COM_TJUCM_ITEMS_LOG_FILE_READY="The log file is ready for download " +COM_TJUCM_DOWNLOAD_LOG_FILE="Download Log File" diff --git a/src/components/com_tjucm/site/controllers/items.php b/src/components/com_tjucm/site/controllers/items.php index 0eb6fa14..78a1a3ff 100644 --- a/src/components/com_tjucm/site/controllers/items.php +++ b/src/components/com_tjucm/site/controllers/items.php @@ -19,6 +19,8 @@ use Joomla\CMS\Table\Table; use Joomla\CMS\Uri\Uri; use Joomla\CMS\Factory; +use Joomla\CMS\Log\Log; + /** * Items list controller class. @@ -124,6 +126,25 @@ public function importCsv() $fieldsArray[$field->name] = $field; } + // Logs implemetation + header('Cache-Control: no-cache, must-revalidate'); + header('Content-type: application/json'); + + // Format the date for the log file name + $date = Factory::getDate()->format('Y-m-d_H-i-s'); + $logFileName = 'com_tjucm.import_records_' . $date . '.log.php'; // Ensuring valid filename + + // Register the logger with a valid log file name + Log::addLogger( + array('text_file' => $logFileName), + Log::ALL, + array('com_tjucm') + ); + + // Set log file name to session + $session = Factory::getSession(); + $session->set('import_filename', $logFileName); + // Read the CSV file $file = fopen($uploadPath, 'r'); $headerRow = true; @@ -243,16 +264,27 @@ public function importCsv() else { $itemData[$fieldName] = trim($value); + + } } } - + // Check if all the required values are present in the row $isValid = (count(array_intersect_key($itemData, $requiredFieldsName)) == count($requiredFieldsName)); + $missingFields = array_diff_key($requiredFieldsName, $itemData); + $missingValues = array_values($missingFields); + $missingvaluesString = implode(', ', $missingValues); + + if (!$isValid || empty($itemData)) { $invalidRows++; + + Log::add(Text::sprintf("COM_TJUCM_IMPORT_FIELD_VALUE_EMPTY_ON_ROW_NO", $invalidRows), Log::INFO, 'com_tjucm'); + Log::add(Text::sprintf("COM_TJUCM_IMPORT_FIELD_VALUE_EMPTY", $missingvaluesString), Log::INFO, 'com_tjucm'); + } else { @@ -305,7 +337,11 @@ public function importCsv() } if ($invalidRows) - { + { + + Log::add(Text::sprintf("COM_TJUCM_INVALID_IMPORT_RECORD_ROW", $invalidRows), Log::INFO, 'com_tjucm'); + + $app->enqueueMessage(Text::sprintf('COM_TJUCM_ITEMS_IMPORT_REJECTED_RECORDS', $invalidRows), 'warning'); } @@ -314,6 +350,19 @@ public function importCsv() $app->enqueueMessage(Text::_('COM_TJUCM_ITEMS_NO_RECORDS_TO_IMPORT'), 'error'); } + $session = Factory::getSession(); + + $logFilePath = JPATH_ADMINISTRATOR . '/logs/' . $logFileName; + + $filename = $session->get('import_filename'); + + $downloadUrl = Uri::root() . 'index.php?option=com_tjucm&task=items.downloadLog&file=' . urlencode($filename); + + $app->enqueueMessage(Text::_('COM_TJUCM_ITEMS_LOG_FILE_READY'). + ''.Text::_('COM_TJUCM_DOWNLOAD_LOG_FILE').'', + 'message' + ); + $app->redirect(Uri::root() . 'index.php?option=com_tjucm&view=items&layout=importitems&tmpl=component&client=' . $client); } @@ -412,4 +461,45 @@ private function processErrors($errors) $app->enqueueMessage(implode("
", $msg), 'error'); } } + + + /** + * Method to download log file + * + * @return void + * + * @since 1.0 + */ + public function downloadLog() + { + $logFileName = Factory::getApplication()->input->getString('file'); + + // Full path to the log file + $logFilePath = JPATH_ADMINISTRATOR . '/logs/' . $logFileName; + + // Check if the file exists + if (file_exists($logFilePath)) { + // Set headers for file download + header('Content-Description: File Transfer'); + header('Content-Type: application/octet-stream'); + header('Content-Disposition: attachment; filename="' . basename($logFilePath) . '"'); + header('Expires: 0'); + header('Cache-Control: must-revalidate'); + header('Pragma: public'); + header('Content-Length: ' . filesize($logFilePath)); + + // Clear output buffer + ob_clean(); + flush(); + + // Read and output the file content + readfile($logFilePath); + exit; + } else { + // If file not found, set an error message + Factory::getApplication()->enqueueMessage('Log file not found.', 'error'); + $this->setRedirect('index.php?option=com_yourcomponent'); + } + } + } From fa7c68f056f34c816da67d5a1a98864336159a9c Mon Sep 17 00:00:00 2001 From: Poonam Baravkar Date: Wed, 16 Oct 2024 16:43:42 +0530 Subject: [PATCH 2/2] Task #228793 chore: Logs implenetation for import ucm functionality #426 --- .../com_tjucm/site/controllers/items.php | 61 +++++++++++-------- 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/src/components/com_tjucm/site/controllers/items.php b/src/components/com_tjucm/site/controllers/items.php index ff6f852f..c1c5f5f9 100644 --- a/src/components/com_tjucm/site/controllers/items.php +++ b/src/components/com_tjucm/site/controllers/items.php @@ -126,24 +126,23 @@ public function importCsv() $fieldsArray[$field->name] = $field; } - // Logs implemetation - header('Cache-Control: no-cache, must-revalidate'); - header('Content-type: application/json'); - - // Format the date for the log file name - $date = Factory::getDate()->format('Y-m-d_H-i-s'); - $logFileName = 'com_tjucm.import_records_' . $date . '.log.php'; // Ensuring valid filename - - // Register the logger with a valid log file name - Log::addLogger( - array('text_file' => $logFileName), - Log::ALL, - array('com_tjucm') - ); + header('Cache-Control: no-cache, must-revalidate'); + header('Content-type: application/json'); + + // Format the date for the log file name + $date = Factory::getDate()->format('Y-m-d_H-i-s'); + $logFileName = 'com_tjucm.import_records_' . $date . '.log.php'; // Ensuring valid filename + + // Register the logger with a valid log file name + Log::addLogger( + array('text_file' => $logFileName), + Log::ALL, + array('com_tjucm') + ); - // Set log file name to session - $session = Factory::getSession(); - $session->set('import_filename', $logFileName); + // Set log file name to session + $session = Factory::getSession(); + $session->set('import_filename', $logFileName); // Read the CSV file $file = fopen($uploadPath, 'r'); @@ -462,7 +461,6 @@ private function processErrors($errors) } } - /** * Method to download log file * @@ -472,30 +470,41 @@ private function processErrors($errors) */ public function downloadLog() { + // Get the file name from the request $logFileName = Factory::getApplication()->input->getString('file'); + $safeFileName = basename($logFileName); // Full path to the log file - $logFilePath = JPATH_ADMINISTRATOR . '/logs/' . $logFileName; + $logFilePath = JPATH_ADMINISTRATOR . '/logs/' . $safeFileName; + $fullPath = realpath($baseDir . $safeFileName); + + // Validate: Ensure the path is within the intended directory + if (strpos($fullPath, $baseDir) !== 0 || !$fullPath) + { + JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR')); + return false; + } + // Check if the file exists - if (file_exists($logFilePath)) { + if (file_exists($fullPath)) + { // Set headers for file download header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); - header('Content-Disposition: attachment; filename="' . basename($logFilePath) . '"'); + header('Content-Disposition: attachment; filename="' . $safeFileName . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); - header('Content-Length: ' . filesize($logFilePath)); + header('Content-Length: ' . filesize($fullPath)); // Clear output buffer ob_clean(); flush(); // Read and output the file content - readfile($logFilePath); + readfile($fullPath); exit; - } - } - + } + } }