Berkarar/plugins/tps/shops/controllers/Report.php

67 lines
2.3 KiB
PHP
Executable File

<?php namespace Tps\Shops\Controllers;
use Backend\Classes\Controller;
use BackendMenu;
use Illuminate\Support\Facades\Log;
use Tps\Shops\Models\Report as ModelsReport;
use Tps\Shops\Classes\Extractor;
use Tps\Shops\Models\ReportDetail;
use Tps\Shops\Models\Shop;
class Report extends Controller
{
public $implement = [ 'Backend\Behaviors\ListController', 'Backend\Behaviors\FormController' ];
public $listConfig = 'config_list.yaml';
public $formConfig = 'config_form.yaml';
public function __construct()
{
parent::__construct();
BackendMenu::setContext('Tps.Shops', 'main-menu-item', 'side-menu-item6');
}
public function onExtractReports($id)
{
$post = ModelsReport::where("id", $id)->get()->first();
$sessionKey = uniqid('session_key', true);
$path = $post->file()->withDeferred($sessionKey)->get()->first()->getPath();
$dividedPath = explode('storage/app', $path);
$storageDestinationPath= storage_path("app/extract/".hash('ripemd160', $post->name).'-'.$post->date);
if (!\File::exists($storageDestinationPath)) {
\File::makeDirectory($storageDestinationPath, 0755, true);
}
$extractor = new Extractor();
$extractor->extract(storage_path('app/'. $dividedPath[1]), $storageDestinationPath);
$filenames = array_diff(scandir($storageDestinationPath), array('.', '..'));
foreach ($filenames as $item) {
$shop_id = explode(".", $item)[0];
$shop = Shop::where("shop_id", $shop_id)->get()->first();
if ($shop) {
$exist = ReportDetail::where('report_id', $id)->where("shop_id", $shop->id)->get();
if ($exist->isEmpty()) {
$report_detail = new ReportDetail();
$report_detail->report_id = $id;
$report_detail->shop_id = $shop->id;
// on local explode storage\app on live server storage/app
$report_detail->file = '/extract'.explode('extract', $storageDestinationPath)[1].'/'.$item;
$report_detail->save();
Log::info("saved succesfully");
}else {
Log::info("Record exist");
}
}else {
Log::info("Shop not found");
}
}
}
}