Example #1
0
 /**
  * @return bool
  */
 public function actionGenerate()
 {
     $config = new Yml();
     if (false === $config->loadConfig()) {
         return false;
     }
     $yml = new \app\modules\shop\components\yml\Yml($config);
     return true === $yml->generate() ? 0 : 1;
 }
 /**
  * @return string|\yii\web\Response
  * @throws \yii\web\ServerErrorHttpException
  */
 public function actionSettings()
 {
     if (Yii::$app->request->isPost) {
         $model = new Yml();
         if ($model->load(Yii::$app->request->post()) && $model->validate()) {
             $model->saveConfig();
         } elseif ($model->hasErrors()) {
             Yii::$app->session->setFlash('error', Helper::formatModelErrors($model, '<br />'));
         }
         return $this->refresh();
     }
     $model = new Yml();
     $model->loadConfig();
     return $this->render('settings', ['model' => $model]);
 }
Example #3
0
    public function actionGenerate()
    {
        $ymlConfig = new Yml();
        if (!$ymlConfig->loadConfig()) {
            return false;
        }
        static::$noimg = Yii::$app->getModule('image')->noImageSrc;
        if (1 == $ymlConfig->offer_param) {
            $this->prepareProperties();
        }
        \Yii::$app->urlManager->setHostInfo($ymlConfig->shop_url);
        $filePath = \Yii::getAlias('@webroot') . '/' . $ymlConfig->general_yml_filename;
        $tpl = <<<'TPL'
        <name>%s</name>
        <company>%s</company>
        <url>%s</url>
        <currencies>
            <currency id="%s" rate="1" plus="0"/>
        </currencies>
        <categories>
            %s
        </categories>
        <store>%s</store>
        <pickup>%s</pickup>
        <delivery>%s</delivery>
        <local_delivery_cost>%s</local_delivery_cost>
        <adult>%s</adult>
TPL;
        $section_categories = '';
        $categories = Category::find()->where(['active' => 1])->asArray();
        /** @var Category $row */
        foreach ($categories->each(500) as $row) {
            $section_categories .= '<category id="' . $row['id'] . '" ' . (0 != $row['parent_id'] ? 'parentId="' . $row['parent_id'] . '"' : '') . '>' . htmlspecialchars(trim(strip_tags($row['name']))) . '</category>' . PHP_EOL;
        }
        unset($row, $categories);
        $section_shop = sprintf($tpl, $ymlConfig->shop_name, $ymlConfig->shop_company, $ymlConfig->shop_url, $ymlConfig->currency_id, $section_categories, 1 == $ymlConfig->shop_store ? 'true' : 'false', 1 == $ymlConfig->shop_pickup ? 'true' : 'false', 1 == $ymlConfig->shop_delivery ? 'true' : 'false', $ymlConfig->shop_local_delivery_cost, 1 == $ymlConfig->shop_adult ? 'true' : 'false');
        $section_offers = '';
        //        $offer_type = ('simplified' === $ymlConfig->general_yml_type) ? '' : 'type="'.$ymlConfig->general_yml_type.'"';
        $offer_type = '';
        // временно, пока не будет окончательно дописан механизм для разных типов
        $products = Product::find()->where(['active' => 1]);
        /** @var Product $row */
        foreach ($products->each(100) as $row) {
            $price = $this->getByYmlParam($ymlConfig, 'offer_price', $row, 0);
            $price = intval($price);
            if ($price <= 0 || $price >= 1000000000) {
                continue;
            }
            $offer = '<offer id="' . $row->id . '" ' . $offer_type . ' available="true">' . PHP_EOL;
            /** @var Category $category */
            $category = $row->category;
            $category = empty($category) ? 1 : $category->category_group_id;
            $offer .= '<url>' . Url::to(['/shop/product/show', 'model' => $row, 'category_group_id' => $category], true) . '</url>' . PHP_EOL;
            $offer .= $this->wrapByYmlParam($ymlConfig, 'offer_price', $row, '<price>%s</price>' . PHP_EOL);
            $offer .= '<currencyId>' . $ymlConfig->currency_id . '</currencyId>' . PHP_EOL;
            $offer .= $this->wrapByYmlParam($ymlConfig, 'offer_category', $row, '<categoryId>%s</categoryId>' . PHP_EOL);
            $offer .= $this->wrapByYmlParam($ymlConfig, 'offer_picture', $row, function ($value) use($ymlConfig) {
                if (empty($value)) {
                    return $value;
                }
                $value = '<picture>' . rtrim($ymlConfig->shop_url, '/') . $value . '</picture>' . PHP_EOL;
                return $value;
            });
            $offer .= $this->wrapByYmlParam($ymlConfig, 'offer_name', $row, function ($value) use($ymlConfig) {
                if (mb_strlen($value) > 120) {
                    $value = mb_substr($value, 0, 120);
                    $value = mb_substr($value, 0, mb_strrpos($value, ' '));
                }
                $value = '<name>' . htmlspecialchars(trim(strip_tags($value))) . '</name>' . PHP_EOL;
                return $value;
            });
            $offer .= $this->wrapByYmlParam($ymlConfig, 'offer_description', $row, function ($value) use($ymlConfig) {
                if (mb_strlen($value) > 175) {
                    $value = mb_substr($value, 0, 175);
                    $value = mb_substr($value, 0, mb_strrpos($value, ' '));
                }
                $value = '<description>' . htmlspecialchars(trim(strip_tags($value))) . '</description>' . PHP_EOL;
                return $value;
            });
            if (1 == $ymlConfig->offer_param) {
                $offer .= $this->getValues($row);
            }
            $offer .= '</offer>';
            $section_offers .= $offer . PHP_EOL;
        }
        unset($row, $products);
        $ymlFileTpl = <<<'TPL'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE yml_catalog SYSTEM "shops.dtd">
<yml_catalog date="%s">
    <shop>
        %s
        <offers>
            %s
        </offers>
    </shop>
</yml_catalog>
TPL;
        $fileString = sprintf($ymlFileTpl, date('Y-m-d H:i'), $section_shop, $section_offers);
        if (1 == $ymlConfig->use_gzip) {
            file_put_contents($filePath . '.gz', gzencode($fileString, 5));
        }
        file_put_contents($filePath, $fileString);
    }