added archive report extraction

This commit is contained in:
Amanmyrat 2022-09-16 00:49:54 +05:00
parent 2dff38d6dc
commit 00beadc789
11 changed files with 279 additions and 120 deletions

View File

@ -0,0 +1,141 @@
<?php namespace Tps\Shops\Classes;
/**
* Class Extractor
*
* Extract a archive (zip/gzip/rar) file.
*
* @author niceshipest
*
*/
class Extractor{
/**
* Checks file extension and calls suitable extractor functions.
*
* @param $archive
* @param $destination
*/
public static function extract($archive, $destination){
$ext = pathinfo($archive, PATHINFO_EXTENSION);
switch ($ext){
case 'zip':
$res = self::extractZipArchive($archive, $destination);
break;
case 'gz':
$res = self::extractGzipFile($archive, $destination);
break;
case 'rar':
$res = self::extractRarArchive($archive, $destination);
break;
}
return $res;
}
/**
* Decompress/extract a zip archive using ZipArchive.
*
* @param $archive
* @param $destination
*/
public static function extractZipArchive($archive, $destination){
// Check if webserver supports unzipping.
if(!class_exists('ZipArchive')){
$GLOBALS['status'] = array('error' => 'Your PHP version does not support unzip functionality.');
return false;
}
$zip = new \ZipArchive;
// Check if archive is readable.
if($zip->open($archive) === TRUE){
// Check if destination is writable
if(is_writeable($destination . '/')){
$zip->extractTo($destination);
$zip->close();
$GLOBALS['status'] = array('success' => 'Files unzipped successfully');
return true;
}else{
$GLOBALS['status'] = array('error' => 'Directory not writeable by webserver.');
return false;
}
}else{
$GLOBALS['status'] = array('error' => 'Cannot read .zip archive.');
return false;
}
}
/**
* Decompress a .gz File.
*
* @param $archive
* @param $destination
*/
public static function extractGzipFile($archive, $destination){
// Check if zlib is enabled
if(!function_exists('gzopen')){
$GLOBALS['status'] = array('error' => 'Error: Your PHP has no zlib support enabled.');
return false;
}
$filename = pathinfo($archive, PATHINFO_FILENAME);
$gzipped = gzopen($archive, "rb");
$file = fopen($filename, "w");
while ($string = gzread($gzipped, 4096)) {
fwrite($file, $string, strlen($string));
}
gzclose($gzipped);
fclose($file);
// Check if file was extracted.
if(file_exists($destination.'/'.$filename)){
$GLOBALS['status'] = array('success' => 'File unzipped successfully.');
return true;
}else{
$GLOBALS['status'] = array('error' => 'Error unzipping file.');
return false;
}
}
/**
* Decompress/extract a Rar archive using RarArchive.
*
* @param $archive
* @param $destination
*/
public static function extractRarArchive($archive, $destination){
// Check if webserver supports unzipping.
if(!class_exists('RarArchive')){
$GLOBALS['status'] = array('error' => 'Your PHP version does not support .rar archive functionality.');
return false;
}
// Check if archive is readable.
if($rar = \RarArchive::open($archive)){
// Check if destination is writable
if (is_writeable($destination . '/')) {
$entries = $rar->getEntries();
foreach ($entries as $entry) {
$entry->extract($destination);
}
$rar->close();
$GLOBALS['status'] = array('success' => 'File extracted successfully.');
return true;
}else{
$GLOBALS['status'] = array('error' => 'Directory not writeable by webserver.');
return false;
}
}else{
$GLOBALS['status'] = array('error' => 'Cannot read .rar archive.');
return false;
}
}
}
?>

View File

@ -2,6 +2,11 @@
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
{
@ -16,20 +21,42 @@ class Report extends Controller
BackendMenu::setContext('Tps.Shops', 'main-menu-item', 'side-menu-item6');
}
public function onZipExtract(){
dd("bcas");
public function onExtractReports($id)
{
$post = ModelsReport::where("id",$id)->get()->first();
$sessionKey = uniqid('session_key', true);
$path = $post->file()->withDeferred($sessionKey)->get()->first()->getPath();
$diviedPath = explode('storage/app', $path);
$extractor = new Extractor();
$storageDestinationPath= storage_path("app/extract/".hash('ripemd160', $post->name).'-'.$post->date);
if (!\File::exists( $storageDestinationPath)) {
\File::makeDirectory($storageDestinationPath, 0755, true);
}
$extractor->extract(storage_path('app/'. $diviedPath[1]), $storageDestinationPath);
$filenames = array_diff(scandir($storageDestinationPath), array('.', '..'));
foreach($filenames as $item){
$shop_number = explode(".", $item)[0];
$shop = Shop::where("shop_number", $shop_number)->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;
$report_detail->file = explode('storage\app', $storageDestinationPath)[1].'/'.$item;
Log::info(explode('storage\app', $storageDestinationPath)[1].'/'.$item);
$report_detail->save();
Log::info("saved succesfully");
}else Log::info("Record exist");
}else Log::info("Shop not found");
}
}
public function onSaveAndNew($context = null)
{
// parent::update_onSave($context);
// dump("bcas");
// dd("bcas");
$aa = $this->name;
echo '<script language="javascript">';
echo '<script type="text/javascript">alert("' . $aa . '")</script>';
echo 'alert("message successfully sent")';
echo '</script>';
// return \Backend::redirect('tps/shops/report');
}
}

View File

@ -33,14 +33,12 @@
class="btn btn-default">
<?= e(trans('backend::lang.form.save_and_close')) ?>
</button>
<button
type="button"
data-request="onSaveAndNew"
data-request-data="close:1"
data-hotkey="ctrl+n, cmd+n"
data-load-indicator="<?= e(trans('backend::lang.form.saving_name', ['name'=>$formRecordName])) ?>"
class="btn btn-default">
Extract Zip
<button
data-request="onExtractReports"
data-request-data="id:<?= $formModel->id?>"
data-load-indicator="<?= e(trans('indikator.news::lang.form.news_cloning', ['name'=>$formModel->title])) ?>"
class="oc-icon-clone btn-icon pull-right">
Extract
</button>
<button
@ -50,9 +48,6 @@
data-load-indicator="<?= e(trans('backend::lang.form.deleting')) ?>"
data-request-confirm="<?= e(trans('backend::lang.form.confirm_delete')) ?>">
</button>
<span class="btn-text">
<a href="{{route('zip.extract', '5')}}">Zip Extract</a>
</span>
<br>
<span class="btn-text">
<?= e(trans('backend::lang.form.or')) ?> <a href="<?= Backend::url('tps/shops/report') ?>"><?= e(trans('backend::lang.form.cancel')) ?></a>

View File

@ -39,55 +39,4 @@ class Report extends Model
public $translatable = ['name'];
// public function afterSave()
// public function beforeSave()
public function formAfterSave($model)
{
// $this->name = $this->file;
$filePath = \System\Models\File::find($model->id)->getLocalPath();
// $filePath = Input::file('file')->getPath();
Log::error("FILE PATH IS ------------------------".$filePath);
// throw new ApplicationException("FILE PATH IS ------------------------".$filePath);
// App::abort(403, "FILE PATH IS ------------------------".$filePath);
// $fileRecord = Input::file('file');
// $fileRecord = \System\Models\File::find($model->id);
// $files = \System\Models\File::find($model->file);
// $new = $this->file()->withDeferred($this->sessionKey)->first()->getPath();;
// $fileRecord = \October\Rain\Database\Models\DeferredBinding::where([
// 'master_type' => 'Tps\Shops\Models\Report', // <- REPLACE WITH YOUR MODEL(ModelName)
// "master_field" => "file", // <- REPLACE WITH ATTACHEMNT MODEL (gallery)
// "slave_type" => "System\Models\File",
// "session_key" => post('_session_key')
// ])->get()->first();
// $fileRecord = (new \System\Models\File)->fromData($model->file, 'logo.png');
// $fileRecord = $model->file->getPath();;
// $fileRecord = $model->file->getLocalPath();;
// if($fileRecord){
// echo "<script>console.log('Debug Objects: " . $fileRecord . "' );</script>";
// dd($fileRecord);
// $zip = new ZipArchive();
// $status = $zip->open($fileRecord);
// if ($status !== true) {
// throw new \Exception($status);
// }
// else{
// $storageDestinationPath= storage_path("app/uploads/unzip/");
// if (!\File::exists( $storageDestinationPath)) {
// \File::makeDirectory($storageDestinationPath, 0755, true);
// }
// $zip->extractTo($storageDestinationPath);
// $zip->close();
// // return back()
// // ->with('success','You have successfully extracted zip.');
// }
// }else{
// // dd("aaa");
// }
}
}

View File

@ -0,0 +1,37 @@
<?php namespace Tps\Shops\Models;
use Model;
/**
* Model
*/
class ReportDetail extends Model
{
use \October\Rain\Database\Traits\Validation;
/*
* Disable timestamps by default.
* Remove this line if timestamps are defined in the database table.
*/
public $timestamps = false;
/**
* @var string The database table used by the model.
*/
public $table = 'tps_shops_report_details';
/**
* @var array Validation rules
*/
public $rules = [];
public $belongsTo = [
'report' => [
'Tps\Shops\Models\Report'
],
'shop' => [
'RainLab\User\Models\Shop'
],
];
}

View File

@ -36,6 +36,12 @@ class Shop extends Model
'RainLab\User\Models\User'
],
];
public $hasMany = [
'reports' => [
'Tps\Shops\Models\ReportDetail',
]
];
public $translatable = ['name','description','open_time'];
}

View File

@ -0,0 +1,13 @@
columns:
id:
label: id
type: number
report_id:
label: report_id
type: number
shop_id:
label: shop_id
type: number
file:
label: file
type: text

View File

@ -11,6 +11,4 @@ use Tps\Shops\Controllers\Shops;
// Custom Routes
Route::name('update.shop')->any('update/shop', 'Tps\Shops\Controllers\Shops@updateShop');
Route::name('report.create')->any('add/report', 'Tps\Shops\Controllers\Reports@createReport');
Route::name('zip.extract')->any('zip/extract/{id}', 'Tps\Shops\Controllers\Reports@createReport');
Route::name('update.shop')->any('update/shop', 'Tps\Shops\Controllers\Shops@updateShop');

View File

@ -0,0 +1,24 @@
<?php namespace Tps\Shops\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class BuilderTableCreateTpsShopsReportDetails extends Migration
{
public function up()
{
Schema::create('tps_shops_report_details', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id')->unsigned();
$table->integer('report_id');
$table->integer('shop_id');
$table->text('file');
});
}
public function down()
{
Schema::dropIfExists('tps_shops_report_details');
}
}

View File

@ -72,3 +72,6 @@
1.0.25:
- 'Created table tps_shops_reports'
- builder_table_create_tps_shops_reports.php
1.0.26:
- 'Created table tps_shops_report_details'
- builder_table_create_tps_shops_report_details.php

View File

@ -7,6 +7,7 @@ is_hidden = 0
security = "user"
redirect = "home"
==
{% component 'session' %}
<!-- Breadcrumb ======================= -->
@ -14,7 +15,7 @@ redirect = "home"
<div class="auto_container">
<div class="crumb_wrap">
<div class="crumb_row">
<a href="index.html" class="crumb_title">
<a href="{{'home'|page }}" class="crumb_title">
<span>
<svg width="16" height="16" viewBox="0 0 16 16" fill="none"
xmlns="http://www.w3.org/2000/svg">
@ -51,19 +52,18 @@ redirect = "home"
Контактная информация
</h4>
</div>
<div class="bill_info tabItem active" id="bill-1">
{% for detail in user.shop.reports %}
<div class="bill_item downloaded wow fadeInUp" data-wow-duration=".3s" data-wow-delay=".1s">
<div>
<p>
23.02.2023
{{detail.report.date}}
</p>
<p>
Счет за электричество
{{detail.report.name}}
</p>
</div>
<a href="#" download="#" class="bill_item-link">
<a href="{{ url('/storage/app') }}{{detail.file}}" download="{{detail.report.name}}-{{detail.report.date}}" class="bill_item-link">
<span>
<svg width="24" height="24" viewBox="0 0 24 24" fill="none"
xmlns="http://www.w3.org/2000/svg">
@ -88,42 +88,8 @@ redirect = "home"
Скачать PDF файл
</a>
</div>
{% endfor %}
<div class="bill_item downloaded wow fadeInUp" data-wow-duration=".3s" data-wow-delay=".13s">
<div>
<p>
23.02.2023
</p>
<p>
Счет за свет
</p>
</div>
<a href="#" download="#" class="bill_item-link">
<span>
<svg width="24" height="24" viewBox="0 0 24 24" fill="none"
xmlns="http://www.w3.org/2000/svg">
<path
d="M19 18H5C4.44772 18 4 18.4477 4 19C4 19.5523 4.44772 20 5 20H19C19.5523 20 20 19.5523 20 19C20 18.4477 19.5523 18 19 18Z"
fill="#292929" />
<path
d="M4 17V19C4 19.5523 4.44772 20 5 20C5.55228 20 6 19.5523 6 19V17C6 16.4477 5.55228 16 5 16C4.44772 16 4 16.4477 4 17Z"
fill="#292929" />
<path
d="M18 17V19C18 19.5523 18.4477 20 19 20C19.5523 20 20 19.5523 20 19V17C20 16.4477 19.5523 16 19 16C18.4477 16 18 16.4477 18 17Z"
fill="#292929" />
<path
d="M11.9995 15C11.7921 15.0016 11.5895 14.9387 11.4195 14.82L7.41946 12C7.20387 11.8471 7.05761 11.615 7.01263 11.3545C6.96766 11.0941 7.02764 10.8264 7.17946 10.61C7.25525 10.5019 7.35171 10.4098 7.46327 10.3391C7.57483 10.2684 7.69928 10.2206 7.82945 10.1982C7.95961 10.1759 8.09291 10.1796 8.22164 10.2091C8.35037 10.2386 8.47198 10.2933 8.57946 10.37L11.9995 12.76L15.3995 10.2C15.6116 10.0409 15.8783 9.97255 16.1409 10.0101C16.4034 10.0476 16.6403 10.1878 16.7995 10.4C16.9586 10.6122 17.0269 10.8789 16.9894 11.1414C16.9519 11.404 16.8116 11.6409 16.5995 11.8L12.5995 14.8C12.4264 14.9298 12.2158 15 11.9995 15Z"
fill="#292929" />
<path
d="M12 13C11.7348 13 11.4804 12.8946 11.2929 12.7071C11.1054 12.5196 11 12.2652 11 12V4C11 3.73478 11.1054 3.48043 11.2929 3.29289C11.4804 3.10536 11.7348 3 12 3C12.2652 3 12.5196 3.10536 12.7071 3.29289C12.8946 3.48043 13 3.73478 13 4V12C13 12.2652 12.8946 12.5196 12.7071 12.7071C12.5196 12.8946 12.2652 13 12 13Z"
fill="#292929" />
</svg>
</span>
Скачать PDF файл
</a>
</div>
</div>
<div class="bill_info tabItem" id="bill-2">