diff --git a/plugins/tps/shops/classes/Extractor.php b/plugins/tps/shops/classes/Extractor.php new file mode 100644 index 0000000..052c7fe --- /dev/null +++ b/plugins/tps/shops/classes/Extractor.php @@ -0,0 +1,141 @@ + '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; + } + } + + } +?> \ No newline at end of file diff --git a/plugins/tps/shops/controllers/Report.php b/plugins/tps/shops/controllers/Report.php index d775b1c..139df20 100644 --- a/plugins/tps/shops/controllers/Report.php +++ b/plugins/tps/shops/controllers/Report.php @@ -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 ''; - echo 'alert("message successfully sent")'; - echo ''; - // return \Backend::redirect('tps/shops/report'); - } } diff --git a/plugins/tps/shops/controllers/report/update.htm b/plugins/tps/shops/controllers/report/update.htm index a5017c7..f764069 100644 --- a/plugins/tps/shops/controllers/report/update.htm +++ b/plugins/tps/shops/controllers/report/update.htm @@ -33,14 +33,12 @@ class="btn btn-default"> - - - Zip Extract -
diff --git a/plugins/tps/shops/models/Report.php b/plugins/tps/shops/models/Report.php index d6179ea..cdc500e 100644 --- a/plugins/tps/shops/models/Report.php +++ b/plugins/tps/shops/models/Report.php @@ -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 ""; - // 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"); - // } - - } } diff --git a/plugins/tps/shops/models/ReportDetail.php b/plugins/tps/shops/models/ReportDetail.php new file mode 100644 index 0000000..b1621b1 --- /dev/null +++ b/plugins/tps/shops/models/ReportDetail.php @@ -0,0 +1,37 @@ + [ + 'Tps\Shops\Models\Report' + ], + 'shop' => [ + 'RainLab\User\Models\Shop' + ], + ]; +} diff --git a/plugins/tps/shops/models/Shop.php b/plugins/tps/shops/models/Shop.php index 09589bf..42b0e5f 100644 --- a/plugins/tps/shops/models/Shop.php +++ b/plugins/tps/shops/models/Shop.php @@ -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']; } diff --git a/plugins/tps/shops/models/reportdetail/columns.yaml b/plugins/tps/shops/models/reportdetail/columns.yaml new file mode 100644 index 0000000..3a07b6c --- /dev/null +++ b/plugins/tps/shops/models/reportdetail/columns.yaml @@ -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 diff --git a/plugins/tps/shops/routes.php b/plugins/tps/shops/routes.php index 582f958..65c092c 100644 --- a/plugins/tps/shops/routes.php +++ b/plugins/tps/shops/routes.php @@ -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'); \ No newline at end of file +Route::name('update.shop')->any('update/shop', 'Tps\Shops\Controllers\Shops@updateShop'); \ No newline at end of file diff --git a/plugins/tps/shops/updates/builder_table_create_tps_shops_report_details.php b/plugins/tps/shops/updates/builder_table_create_tps_shops_report_details.php new file mode 100644 index 0000000..05700e8 --- /dev/null +++ b/plugins/tps/shops/updates/builder_table_create_tps_shops_report_details.php @@ -0,0 +1,24 @@ +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'); + } +} diff --git a/plugins/tps/shops/updates/version.yaml b/plugins/tps/shops/updates/version.yaml index b0f6dc7..4c2b586 100644 --- a/plugins/tps/shops/updates/version.yaml +++ b/plugins/tps/shops/updates/version.yaml @@ -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 diff --git a/themes/berkarar/pages/profile.htm b/themes/berkarar/pages/profile.htm index 563c499..788a5cf 100644 --- a/themes/berkarar/pages/profile.htm +++ b/themes/berkarar/pages/profile.htm @@ -7,6 +7,7 @@ is_hidden = 0 security = "user" redirect = "home" == + {% component 'session' %} @@ -14,7 +15,7 @@ redirect = "home"