ExportController.php 6.68 KB
<?php
    
    namespace artbox\catalog\controllers;

    use artbox\catalog\models\Product;
    use common\models\ExportLog;
    use yii\db\ActiveQuery;
    use yii\filters\AccessControl;
    use yii\web\Controller;
    use PHPExcel;
    use PHPExcel_Writer_Excel2007;

    /**
     * Class ExportController is web wrapper for console ExportXmlController
     */
    class ExportController extends Controller
    {
        /**
         * @inheritdoc
         */
        public function getViewPath()
        {
            return '@artbox/catalog/views/export';
        }
    
        public function actionTest()
        {
            $exportLog = ExportLog::findOne(1);
        
            $products = Product::find()
                               ->with(
                                   [
                                       'variant.lang',
                                       'lang',
                                       'brand.lang',
                                       'category.lang',
                                       'productOptionExcls' => function (ActiveQuery $query) {
                                           $query->with(
                                               [
                                                   'lang',
                                                   'group.lang',
                                               ]
                                           );
                                       },
                                   ]
                               );
            $data = [];
            $j = 0;
            $exportLog->message = 'Staeted';
            $exportLog->percent = '';
            $exportLog->is_running = true;
            foreach ($products->batch(1000) as $productsBatch) {
                foreach ($productsBatch as $product) {
                    $j++;
                    if (empty($product->variant) || empty($product->category) || empty($product->brand)) {
                        continue;
                    }
                    $filters = [];
                
                    foreach ($product->productOptionExcls as $optionExcl) {
                        $filters[] = trim($optionExcl->group->lang->title, ':') . ':' . $optionExcl->lang->value;
                    }
                
                    $data[] = [
                        'A' => $product->category->lang->title,
                        //1
                        'B' => $product->brand->lang->title,
                        //2
                        'C' => $product->lang->title,
                        //3
                        'D' => $product->variant->sku,
                        //4
                        'E' => $product->lang->description,
                        //5
                        'F' => $product->variant->price,
                        //6
                        'G' => $product->variant->price_old,
                        //7
                        'H' => '',
                        //8
                        'I' => '',
                        //9
                        'J' => '',
                        //10
                        'K' => '',
                        //11
                        'L' => '',
                        //12
                        'M' => $product->variant->stock,
                        //13
                        'N' => implode('*', $filters),
                        //14
                    ];
                }
            }
    
            $objPHPExcel = new PHPExcel();
        
            $objPHPExcel->getProperties()
                        ->setCreator("Runnable.com");
            $objPHPExcel->getProperties()
                        ->setLastModifiedBy("Runnable.com");
            $objPHPExcel->getProperties()
                        ->setTitle("Office 2007 XLSX Test Document");
            $objPHPExcel->getProperties()
                        ->setSubject("Office 2007 XLSX Test Document");
            $objPHPExcel->getProperties()
                        ->setDescription(
                            "Test document for Office 2007 XLSX, generated using PHP classes."
                        );
        
            $objPHPExcel->setActiveSheetIndex(0);
            $i = 1;
            foreach ($data as $values) {
                foreach ($values as $key => $value) {
                    $objPHPExcel->getActiveSheet()
                                ->SetCellValue($key . $i, $value);
                }
                $i++;
            }
        
            $objPHPExcel->getActiveSheet()
                        ->setTitle('Simple');
        
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $objWriter->save(\Yii::getAlias('@storage/test2.xlsx'));
        }
    
        /**
         * @inheritdoc
         */
        public function behaviors()
        {
            return [
                'access' => [
                    'class' => AccessControl::className(),
                    'rules' => [
                        [
                            'actions' => [
                                'login',
                                'error',
                            ],
                            'allow'   => true,
                        ],
                        [
                            'allow' => true,
                            'roles' => [ '@' ],
                        ],
                    ],
                ],
            ];
        }
    
        public function actionIndex()
        {
            return $this->render('index');
        }
    
        public function actionHotline()
        {
            $response = \Yii::$app->response;
            $response->format = $response::FORMAT_JSON;
            $rootDir = dirname(\Yii::getAlias('@common'));
            exec("cd $rootDir && php yii export-xml/hotline", $output);
            if (count($output) === 1) {
                return [
                    'error'  => true,
                    'output' => $output[ 0 ],
                ];
            } else {
                return [
                    'success' => true,
                    'output'  => $output,
                ];
            }
        }
    
        public function actionNadavi()
        {
            $response = \Yii::$app->response;
            $response->format = $response::FORMAT_JSON;
            $rootDir = dirname(\Yii::getAlias('@common'));
            exec("cd $rootDir && php yii export-xml/nadavi", $output);
            if (count($output) === 1) {
                return [
                    'error'  => true,
                    'output' => $output[ 0 ],
                ];
            } else {
                return [
                    'success' => true,
                    'output'  => $output,
                ];
            }
        }
    }