Blame view

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