ExportAllController.php 11.8 KB
<?php
    
    namespace backend\controllers;
    
    use artbox\catalog\models\Brand;
    use artbox\core\models\Image;
    use artbox\catalog\models\Category;
    use artbox\catalog\models\Product;
    use PHPExcel;
    use PHPExcel_Writer_Excel2007;
    use ZipArchive;
    
    /**
     * Class ExportController
     */
    class ExportAllController extends \artbox\catalog\controllers\ExportController
    {
        public function actionExport()
        {
            set_time_limit(0);
            $tempnameZip = tempnam(sys_get_temp_dir(), 'zip');
            $zip = new ZipArchive();
            $zip->open($tempnameZip, ZipArchive::CREATE | ZipArchive::OVERWRITE);
            \PHPExcel_Settings::setLibXmlLoaderOptions(LIBXML_COMPACT | LIBXML_PARSEHUGE);
            $xls = new PHPExcel();
            $xls->setActiveSheetIndex(0);
            $sheet = $xls->getActiveSheet();
            $cache = \Yii::$app->request->get('cache');
            $query = Product::find()
                            ->with(
                                [
                                    'variants' => function ($query) use ($cache) {
                                        /**
                                         * @var \yii\db\ActiveQuery $query
                                         */
                                        $query->with(
                                            [
                                                'variantOptionExcls' => function ($query) {
                                                    /**
                                                     * @var \yii\db\ActiveQuery $query
                                                     */
                                                    $query->with('variantOptionGroupExcl.lang')
                                                          ->with('lang');
                                                },
                                            ]
                                        )
                                              ->with('image')
                                              ->with('images');
                                    },
                                ]
                            )
                            ->with('lang')//                ->with('images')
                            ->with(
                    [
                        'productOptionExcls' => function ($query) {
                            /**
                             * @var \yii\db\ActiveQuery $query
                             */
                            $query->with('lang')
                                  ->with('productOptionGroupExcl.lang');
                        },
                    ]
                );
            if (!$cache) {
                $query->with('brand.lang')
                      ->with('category.lang');
            } else {
                $query->leftJoin(
                    'product_to_category',
                    'product.id = product_to_category.product_id'
                );
            }
            if ($cache) {
                $categories = Category::find()
                                      ->with('lang')
                                      ->indexBy('id')
                                      ->all();
                $brands = Brand::find()
                               ->with('lang')
                               ->indexBy('id')
                               ->all();
            }
            foreach ($query->each(1000) as $index => $product) {
                /**
                 * @var Product    $product
                 * @var Brand[]    $brands
                 * @var Category[] $categories
                 */
                $num = $index;
                if ($cache) {
                    if ($product->brand_id && array_key_exists($product->brand_id, $brands)) {
                        $sheet->setCellValue('B' . $num, $brands[ $product->brand_id ]->lang->title);
                    }
                    $category_id = $product->category_id;
                    if ($category_id && array_key_exists($category_id, $categories)) {
                        $sheet->setCellValue('A' . $num, $categories[ $category_id ]->lang->title);
                    }
                } else {
                    if (!empty($product->category)) {
                        $sheet->setCellValue('A' . $num, $product->category->lang->title);
                    }
                    if (!empty($product->brand)) {
                        $sheet->setCellValue('B' . $num, $product->brand->lang->title);
                    }
                }
                $sheet->setCellValue('C' . $num, $product->lang->title);
                $sheet->setCellValue(
                    'D' . $num,
                    ( isset($product->variants[ 0 ]->sku) ? $product->variants[ 0 ]->sku : 'default' )
                );
                $sheet->setCellValue('E' . $num, $product->lang->description);
                $sheet->setCellValue(
                    'F' . $num,
                    ( isset($product->variants[ 0 ]->price) ? $product->variants[ 0 ]->price : 0 )
                );
                $sheet->setCellValue(
                    'G' . $num,
                    ( isset($product->variants[ 0 ]->price_old) ? $product->variants[ 0 ]->price_old : 0 )
                );
                
                if ($product->is('top')) {
                    $sheet->setCellValue('H' . $num, 1);
                } else {
                    $sheet->setCellValue('H' . $num, 0);
                }
                if ($product->is('new')) {
                    $sheet->setCellValue('I' . $num, 1);
                } else {
                    $sheet->setCellValue('I' . $num, 0);
                }
                if ($product->is('akcia')) {
                    $sheet->setCellValue('J' . $num, 1);
                } else {
                    $sheet->setCellValue('J' . $num, 0);
                }
                
                $sheet->setCellValue('K' . $num, $this->writeImages($product, $zip));
                $sheet->setCellValue(
                    'M' . $num,
                    ( isset($product->variants[ 0 ]->stock) ? $product->variants[ 0 ]->stock : 0 )
                );
                $sheet->setCellValue('N' . $num, $this->writeProps($product));
                //                if (!empty($product->video)) {
                //                    $videos = json_decode($product->video);
                //                    $sheet->setCellValue('O' . $num, implode(';', $videos));
                //                }
                //                if(!empty($product->variants[0])) {
                //                    $sheet->setCellValue('Q' . $num, $this->writeVariant($product->variants[0], $zip));
                //                }
                //                if(!empty($product->variants[1])) {
                //                    $sheet->setCellValue('R' . $num, $this->writeVariant($product->variants[1], $zip));
                //                }
                //                if(!empty($product->variants[2])) {
                //                    $sheet->setCellValue('S' . $num, $this->writeVariant($product->variants[2], $zip));
                //                }
                //                if(!empty($product->variants[3])) {
                //                    $sheet->setCellValue('T' . $num, $this->writeVariant($product->variants[3], $zip));
                //                }
                //                if(!empty($product->variants[4])) {
                //                    $sheet->setCellValue('U' . $num, $this->writeVariant($product->variants[4], $zip));
                //                }
                //                if(!empty($product->variants[5])) {
                //                    $sheet->setCellValue('V' . $num, $this->writeVariant($product->variants[5], $zip));
                //                }
                //                if(!empty($product->variants[6])) {
                //                    $sheet->setCellValue('W' . $num, $this->writeVariant($product->variants[6], $zip));
                //                }
            }
            $response = \Yii::$app->response;
            $objWriter = new PHPExcel_Writer_Excel2007($xls);
            $tempnameExcel = tempnam(sys_get_temp_dir(), 'export');
            $objWriter->save($tempnameExcel);
            $zip->addFile($tempnameExcel, 'export.xlsx');
            $zip->close();
            $response->sendFile($tempnameZip, 'export.zip');
        }
        
        protected function writeImages(Product $product, ZipArchive $zip)
        {
            $imageNames = [];
            /**
             * @var Image[] $images
             */
            foreach ($product->images as $image) {
                /**
                 * @var Image $image
                 */
                $imageNames[] = $image->fileName;
                if (\Yii::$app->request->get('images')) {
                    $zip->addFile($image->getPath(), '/sync/' . $image->fileName);
                }
            }
            return implode(',', $imageNames);
        }
        
        protected function writeVImages(\artbox\catalog\models\Variant $variant, ZipArchive $zip)
        {
            $imageNames = [];
            if (!empty($variant->image)) {
                $imageNames[] = $variant->image->fileName;
                if (\Yii::$app->request->get('images')) {
                    $zip->addFile($variant->image->getPath(), '/sync/' . $variant->image->fileName);
                }
            }
            /**
             * @var Image[] $images
             */
            foreach ($variant->images as $image) {
                /**
                 * @var Image $image
                 */
                $imageNames[] = $image->fileName;
                if (\Yii::$app->request->get('images')) {
                    $zip->addFile($image->getPath(), '/sync/' . $image->fileName);
                }
            }
            return implode(',', $imageNames);
        }
        
        protected function writeProps(Product $product)
        {
            $groups = [];
            foreach ($product->productOptionExcls as $productOptionExcl) {
                if (!array_key_exists($productOptionExcl->productOptionGroupExcl->lang->title, $groups)) {
                    $groups[ $productOptionExcl->productOptionGroupExcl->lang->title ] = [];
                }
                $groups[ $productOptionExcl->productOptionGroupExcl->lang->title ][] = $productOptionExcl->lang->value;
            }
            $groupStrings = [];
            foreach ($groups as $group => $options) {
                $groupStrings[] = $group . ':' . implode('~', $options);
            }
            return implode(';', $groupStrings);
        }
        
        protected function writeVProps(\artbox\catalog\models\Variant $variant)
        {
            $groups = [];
            foreach ($variant->variantOptionExcls as $variantOptionExcl) {
                if (!array_key_exists($variantOptionExcl->variantOptionGroupExcl->lang->title, $groups)) {
                    $groups[ $variantOptionExcl->variantOptionGroupExcl->lang->title ] = [];
                }
                $groups[ $variantOptionExcl->variantOptionGroupExcl->lang->title ][] = $variantOptionExcl->lang->value;
            }
            $groupStrings = [];
            foreach ($groups as $group => $options) {
                $groupStrings[] = $group . ':' . implode('~', $options);
            }
            return implode('*', $groupStrings);
        }
        
        protected function writeVariant(\artbox\catalog\models\Variant $variant, $zip)
        {
            $data = [];
            $data[] = $variant->sku;
            $data[] = $this->writeVProps($variant);
            $data[] = $this->writeVImages($variant, $zip);
            $data[] = $variant->stock;
            $result = implode('=', $data);
            return $result;
        }
    }