Last active
September 13, 2018 09:02
-
-
Save Kyslik/66d3cf40bfbf4353cffd1d76663079bf to your computer and use it in GitHub Desktop.
New version of CW Reporter Controller file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Oliverbj\Cwreporter\Http\Controllers; | |
use Illuminate\Support\Facades\Storage; | |
use File; | |
use Orchestra\Parser\Xml\Facade as XmlParser; | |
use Illuminate\Support\Facades\DB; | |
use Illuminate\Support\Facades\Log; | |
use Illuminate\Routing\Controller; | |
use Oliverbj\Cwreporter\Http\Helper; | |
use Illuminate\Support\Facades\Schema; | |
class CwreporterController extends Controller | |
{ | |
protected $filetype = 'xml'; | |
public function process($reportName) | |
{ | |
//load our configration file for easy use. | |
$config = config('cwreporter.name.' . $reportName); | |
$filetype = config('cwreporter.filetype'); | |
if (!in_array($reportName, array_keys(config('cwreporter.name')))) { | |
Log::error('report:process - The entered report ('.$reportName.') does not exist in config/cwreporter file! Please make sure it\'s filled out correctly or create the report.'); | |
return; | |
} | |
//Check if the table exists in our database. | |
if (!Schema::hasTable($config['table'])) { | |
Log::error('report:process - The table ' . $config['table'] . ' does not exist in your database. Report: ' . $reportName); | |
return; | |
} | |
//Get the config filter for the spcific report (XML element => database element) | |
$mapping = $config['columns']; | |
//Set the XML elements we need from our XML file. | |
$xmlFilters = array_keys($config['columns']); | |
//Find our file on the ftp server. | |
$this->findFile(); | |
//If the file is found, get the info from the file. | |
$file = $this->getFile($this->findFile()); | |
//Fetch the XML file from the FTP server. | |
$xml = XmlParser::extract(Storage::disk('ftp')->get('' . $file['dirname'] . '/' . $file['basename'] . '')); | |
//Parse the XML data from our XML file. | |
$data = $xml->parse([ | |
'report' => ['uses' => '' . $config['element'] . '[' . implode(',', $xmlFilters) . ']', 'default' => null] | |
]); | |
//Apply custom functions to our array. | |
//Functions can be enabled in the config file. | |
//Helper functions can be found in Helpers/helpers.php | |
if (count($config['functions']) > 0) { | |
foreach ($config['functions'] as $function) { | |
//$data['report'] = Helper::removeHigher($data['report'], 'MilestoneSequenceNo', 299); | |
$data['report'] = Helper::{$function['name']}($data['report'], $function['filterKey'], $function['filterValue']); | |
} | |
} | |
//Switch the columns from the config, so our array keys is our database columns names, instead of our element (XML) names. | |
$insert = []; | |
foreach ($data['report'] as $subarray) { | |
$new_subarray = []; | |
foreach ($subarray as $k => $v) { | |
//If this is enabled, the script will automatically search all values with "commas ," | |
//and replace them (remove them). | |
//This is so we can insert numbers to our DB, as it does not support comma seperated values. | |
if (isset($config['convertInteger'])) { | |
$v = str_replace(',', '', $v); | |
} | |
//If any of the array values is empty, we need to set them as NULL values | |
//this is done, so MySQL will handle it as NULL. | |
if (empty($v)) { | |
$v = null; | |
} | |
$new_subarray[$mapping[$k]] = $v; | |
} | |
$insert[] = $new_subarray; | |
} | |
if (empty($insert)) { | |
Log::error('report:process - This report (' . $reportName . ') file does not contain any elements for processing.'); | |
return; | |
} | |
$insertData = DB::table($config['table'])->insert($insert); | |
if ($insertData) { | |
Log::info('report:process - Report (' . $reportName . ') data has been successfully imported. Date: ' . date('Y-m-d H:i:s', time())); | |
//Delete the file from the FTP server. | |
//$delete_file = Storage::disk("ftp")->delete(''.$fileName["dirname"].'/'.$fileName["basename"].''); | |
} else { | |
Log::error('report:process - the data could not be inserted into the database. (Report name: ' . $reportName . ')'); | |
return; | |
} | |
} | |
/** | |
* Function to find a file on the FTP server | |
* Returns @array (folder/filename.ext) | |
*/ | |
public function findFile() | |
{ | |
$filesInFolder = array_filter(Storage::disk('ftp')->files(config('cwreporter.folder')), function ($file) { | |
//global $filetype; | |
//This get filename from the FTP server (/reports folder), that includes the current date (YYYY-MM-DD) in the file name. | |
return preg_match('/' . date('Y', time()) . '\-' . date('m', time()) . '\-' . date('d', time()) . '(.*)\.(?i)' . $this->filetype . '/ms', $file); | |
}); | |
if (empty($filesInFolder)) { | |
Log::error('report:process - No reports found on the FTP server by this filename.'); | |
throw new Exception; | |
} | |
return $filesInFolder; | |
} | |
public function getFile($files) | |
{ | |
foreach ($files as $file) { | |
$fileInfo = pathinfo($file); | |
} | |
return $fileInfo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment