diff --git a/plugins/tps/shops/classes/Extractor.php b/plugins/tps/shops/classes/Extractor.php index 052c7fe..b8f9b28 100755 --- a/plugins/tps/shops/classes/Extractor.php +++ b/plugins/tps/shops/classes/Extractor.php @@ -1,141 +1,145 @@ '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; - } - } - + return $res; } -?> \ No newline at end of file + + /** + * 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')) { + Log::info('Your PHP version does not support .rar archive functionality.'); + $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; + } + } +} diff --git a/plugins/tps/shops/controllers/Report.php b/plugins/tps/shops/controllers/Report.php index 139df20..8eea3b3 100755 --- a/plugins/tps/shops/controllers/Report.php +++ b/plugins/tps/shops/controllers/Report.php @@ -11,7 +11,7 @@ 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'; @@ -23,30 +23,30 @@ class Report extends Controller public function onExtractReports($id) { - $post = ModelsReport::where("id",$id)->get()->first(); - + $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(); + $dividedPath = explode('storage/app', $path); $storageDestinationPath= storage_path("app/extract/".hash('ripemd160', $post->name).'-'.$post->date); - if (!\File::exists( $storageDestinationPath)) { + + if (!\File::exists($storageDestinationPath)) { \File::makeDirectory($storageDestinationPath, 0755, true); } - - $extractor->extract(storage_path('app/'. $diviedPath[1]), $storageDestinationPath); - + + $extractor = new Extractor(); + $extractor->extract(storage_path('app/'. $dividedPath[1]), $storageDestinationPath); + $filenames = array_diff(scandir($storageDestinationPath), array('.', '..')); - foreach($filenames as $item){ + foreach ($filenames as $item) { $shop_number = explode(".", $item)[0]; $shop = Shop::where("shop_number", $shop_number)->get()->first(); - if($shop){ + if ($shop) { $exist = ReportDetail::where('report_id', $id)->where("shop_id", $shop->id)->get(); - if($exist->isEmpty()){ + if ($exist->isEmpty()) { $report_detail = new ReportDetail(); $report_detail->report_id = $id; $report_detail->shop_id = $shop->id; @@ -54,9 +54,12 @@ class Report extends Controller 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"); + }else { + Log::info("Record exist"); + } + }else { + Log::info("Shop not found"); + } } } - } diff --git a/plugins/tps/shops/controllers/ReportDetails.php b/plugins/tps/shops/controllers/ReportDetails.php new file mode 100644 index 0000000..1bfdc4b --- /dev/null +++ b/plugins/tps/shops/controllers/ReportDetails.php @@ -0,0 +1,19 @@ + + + diff --git a/plugins/tps/shops/controllers/reportdetails/config_form.yaml b/plugins/tps/shops/controllers/reportdetails/config_form.yaml new file mode 100644 index 0000000..acc7b3e --- /dev/null +++ b/plugins/tps/shops/controllers/reportdetails/config_form.yaml @@ -0,0 +1,10 @@ +name: ReportDetails +form: $/tps/shops/models/reportdetail/fields.yaml +modelClass: Tps\Shops\Models\ReportDetail +defaultRedirect: tps/shops/reportdetails +create: + redirect: 'tps/shops/reportdetails/update/:id' + redirectClose: tps/shops/reportdetails +update: + redirect: tps/shops/reportdetails + redirectClose: tps/shops/reportdetails diff --git a/plugins/tps/shops/controllers/reportdetails/config_list.yaml b/plugins/tps/shops/controllers/reportdetails/config_list.yaml new file mode 100644 index 0000000..4dc3055 --- /dev/null +++ b/plugins/tps/shops/controllers/reportdetails/config_list.yaml @@ -0,0 +1,12 @@ +list: $/tps/shops/models/reportdetail/columns.yaml +modelClass: Tps\Shops\Models\ReportDetail +title: ReportDetails +noRecordsMessage: 'backend::lang.list.no_records' +showSetup: true +showCheckboxes: true +recordsPerPage: 20 +toolbar: + buttons: list_toolbar + search: + prompt: 'backend::lang.list.search_prompt' +recordUrl: 'tps/shops/reportdetails/update/:id' diff --git a/plugins/tps/shops/controllers/reportdetails/index.htm b/plugins/tps/shops/controllers/reportdetails/index.htm new file mode 100644 index 0000000..ea43a36 --- /dev/null +++ b/plugins/tps/shops/controllers/reportdetails/index.htm @@ -0,0 +1 @@ +listRender() ?> diff --git a/plugins/tps/shops/controllers/reportdetails/preview.htm b/plugins/tps/shops/controllers/reportdetails/preview.htm new file mode 100644 index 0000000..ac39535 --- /dev/null +++ b/plugins/tps/shops/controllers/reportdetails/preview.htm @@ -0,0 +1,22 @@ + + + + +fatalError): ?> + +
+ formRenderPreview() ?> +
+ + +

fatalError) ?>

+ + +

+ + + +

\ No newline at end of file diff --git a/plugins/tps/shops/controllers/reportdetails/update.htm b/plugins/tps/shops/controllers/reportdetails/update.htm new file mode 100644 index 0000000..81cee0d --- /dev/null +++ b/plugins/tps/shops/controllers/reportdetails/update.htm @@ -0,0 +1,54 @@ + + + + +fatalError): ?> + + 'layout']) ?> + +
+ formRender() ?> +
+ +
+
+ + + + + + + +
+
+ + + +

fatalError)) ?>

+

+ \ No newline at end of file diff --git a/plugins/tps/shops/models/Category.php b/plugins/tps/shops/models/Category.php index a319d8f..f755c78 100755 --- a/plugins/tps/shops/models/Category.php +++ b/plugins/tps/shops/models/Category.php @@ -34,7 +34,7 @@ class Category extends Model 'Tps\Shops\Models\Shop', ] ]; - + public $belongsTo = [ 'top_category' => [ 'Tps\Shops\Models\TopCategory', @@ -46,4 +46,9 @@ class Category extends Model public function getShops(){ return Shop::where('category_id', $this->id)->limit(6)->get(); } + + public function getTopCategoryFieldAttribute() { + return $this->top_category->name; + } + } diff --git a/plugins/tps/shops/models/Report.php b/plugins/tps/shops/models/Report.php index cdc500e..87d3720 100755 --- a/plugins/tps/shops/models/Report.php +++ b/plugins/tps/shops/models/Report.php @@ -34,7 +34,6 @@ class Report extends Model public $attachOne = [ 'file' => 'System\Models\File', - // 'file' => 'Tps\Shops\Classes\FileAttachment', ]; public $translatable = ['name']; diff --git a/plugins/tps/shops/models/ReportDetail.php b/plugins/tps/shops/models/ReportDetail.php index b1621b1..9163c57 100755 --- a/plugins/tps/shops/models/ReportDetail.php +++ b/plugins/tps/shops/models/ReportDetail.php @@ -8,7 +8,7 @@ use 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. @@ -31,7 +31,15 @@ class ReportDetail extends Model 'Tps\Shops\Models\Report' ], 'shop' => [ - 'RainLab\User\Models\Shop' + 'Tps\Shops\Models\Shop' ], ]; + + public function getShopFieldAttribute() { + return $this->shop->name; + } + + public function getReportFieldAttribute() { + return $this->report->name; + } } diff --git a/plugins/tps/shops/models/Shop.php b/plugins/tps/shops/models/Shop.php index 8608b97..105200a 100755 --- a/plugins/tps/shops/models/Shop.php +++ b/plugins/tps/shops/models/Shop.php @@ -8,7 +8,7 @@ use Model; class Shop extends Model { use \October\Rain\Database\Traits\Validation; - + public $implement = ['RainLab.Translate.Behaviors.TranslatableModel']; /** * @var string The database table used by the model. @@ -20,7 +20,7 @@ class Shop extends Model */ public $rules = [ ]; - + public $fillable = ["name", "floor", "phone", "instagram_name", "instagram_link"]; public $attachOne = [ @@ -40,7 +40,7 @@ class Shop extends Model 'RainLab\User\Models\User' ], ]; - + public $hasMany = [ 'reports' => [ 'Tps\Shops\Models\ReportDetail', @@ -48,4 +48,8 @@ class Shop extends Model ]; public $translatable = ['name','description','open_time']; + + public function getCategoryFieldAttribute() { + return $this->category->name; + } } diff --git a/plugins/tps/shops/models/category/columns.yaml b/plugins/tps/shops/models/category/columns.yaml index d9e9d44..4cc7c8b 100755 --- a/plugins/tps/shops/models/category/columns.yaml +++ b/plugins/tps/shops/models/category/columns.yaml @@ -6,9 +6,9 @@ columns: name: label: ' Name' type: text - top_category_id: + top_category_field: label: 'Top Category' - type: text + disabled: true sort_order: label: sort_order type: number diff --git a/plugins/tps/shops/models/reportdetail/columns.yaml b/plugins/tps/shops/models/reportdetail/columns.yaml index 3a07b6c..cf52bbe 100755 --- a/plugins/tps/shops/models/reportdetail/columns.yaml +++ b/plugins/tps/shops/models/reportdetail/columns.yaml @@ -2,12 +2,12 @@ columns: id: label: id type: number - report_id: - label: report_id - type: number - shop_id: - label: shop_id - type: number + report_field: + label: report + disabled: true + shop_field: + label: shop + disabled: true file: label: file type: text diff --git a/plugins/tps/shops/models/reportdetail/fields.yaml b/plugins/tps/shops/models/reportdetail/fields.yaml new file mode 100644 index 0000000..d143697 --- /dev/null +++ b/plugins/tps/shops/models/reportdetail/fields.yaml @@ -0,0 +1,17 @@ +fields: + shop: + label: Shop + nameFrom: name + descriptionFrom: description + span: auto + type: relation + report: + label: Report + nameFrom: name + descriptionFrom: description + span: auto + type: relation + file: + label: File + span: auto + type: text diff --git a/plugins/tps/shops/models/shop/columns.yaml b/plugins/tps/shops/models/shop/columns.yaml index 1bccc0d..9ee9a1d 100755 --- a/plugins/tps/shops/models/shop/columns.yaml +++ b/plugins/tps/shops/models/shop/columns.yaml @@ -17,10 +17,9 @@ columns: instagram_link: label: instagram_link type: text - category_id: + category_field: label: category - type: number - relation: category + disabled: true created_at: label: created_at type: datetime diff --git a/plugins/tps/shops/plugin.yaml b/plugins/tps/shops/plugin.yaml index d9b37ce..5e0c3f1 100755 --- a/plugins/tps/shops/plugin.yaml +++ b/plugins/tps/shops/plugin.yaml @@ -34,3 +34,7 @@ navigation: label: Report url: tps/shops/report icon: icon-file-pdf-o + side-menu-item7: + label: 'Shop reports' + url: tps/shops/reportdetails + icon: icon-sitemap