Commit 731adb96b0831f87d3042526938619776bdc8060
1 parent
f3bc9c4b
remarketing
Showing
1 changed file
with
64 additions
and
0 deletions
Show diff stats
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Created by PhpStorm. | |
| 4 | + * User: Alex Savenko | |
| 5 | + * Date: 05.12.2016 | |
| 6 | + * Time: 18:36 | |
| 7 | + */ | |
| 8 | + | |
| 9 | +namespace console\controllers; | |
| 10 | + | |
| 11 | +use common\modules\product\models\Product; | |
| 12 | +use Yii; | |
| 13 | +use yii\helpers\Url; | |
| 14 | +use yii\console\Controller; | |
| 15 | + | |
| 16 | + | |
| 17 | +class FeedController extends Controller | |
| 18 | +{ | |
| 19 | + private $urlList = []; | |
| 20 | + private $count = 1; | |
| 21 | + | |
| 22 | + public function getProducts() { | |
| 23 | + return Product::find()->all(); | |
| 24 | + } | |
| 25 | + | |
| 26 | + public function checkUrl($url){ | |
| 27 | + if(!in_array($url, $this->urlList)){ | |
| 28 | + $this->urlList[] = $url; | |
| 29 | + return true; | |
| 30 | + } else { | |
| 31 | + return false; | |
| 32 | + } | |
| 33 | + } | |
| 34 | + | |
| 35 | + public function createRow( $product, &$content ){ | |
| 36 | + $url = Url::to(['catalog/product', 'product' => $product]); | |
| 37 | + if($this->checkUrl($url)){ | |
| 38 | + print $this->count++ . "\n"; | |
| 39 | + $content .= $url."\r\n"; | |
| 40 | + } | |
| 41 | + } | |
| 42 | + | |
| 43 | + public function actionProcess() { | |
| 44 | + | |
| 45 | + $dirName = Yii::getAlias('@frontend').'/web'; | |
| 46 | + $filename = 'feed.csv'; | |
| 47 | + setlocale(LC_ALL, 'ru_RU.CP1251'); | |
| 48 | + | |
| 49 | + $handle = fopen($dirName .'/'. $filename, "w"); | |
| 50 | + | |
| 51 | + $content = 'ID, ID2, Item Category, Item title, Item description, Price'."\r\n"; | |
| 52 | + | |
| 53 | + foreach ($this->getProducts() as $product) { | |
| 54 | + $this->createRow($product, $content); | |
| 55 | + } | |
| 56 | + | |
| 57 | + $content .= '</urlset>'; | |
| 58 | + | |
| 59 | + fwrite($handle, $content); | |
| 60 | + fclose($handle); | |
| 61 | + | |
| 62 | + print $dirName .'/'. $filename; | |
| 63 | + } | |
| 64 | +} | |
| 0 | 65 | \ No newline at end of file | ... | ... |