|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\shop\components\GoogleMerchants; |
|
4
|
|
|
|
|
5
|
|
|
use app\modules\shop\models\Currency; |
|
6
|
|
|
use app\modules\shop\models\GoogleFeed; |
|
7
|
|
|
use app\modules\shop\models\Product; |
|
8
|
|
|
use Yii; |
|
9
|
|
|
use yii\base\Component; |
|
10
|
|
|
use yii\helpers\Json; |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class GoogleMerchants |
|
15
|
|
|
* This implementation uses a rss 2.0 template |
|
16
|
|
|
* @package app\modules\shop\components\GoogleMerchants |
|
17
|
|
|
*/ |
|
18
|
|
|
class GoogleMerchants extends Component |
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
protected static $modelSetting; |
|
22
|
|
|
|
|
23
|
|
|
public $host = null; |
|
24
|
|
|
public $title = null; |
|
25
|
|
|
public $description = null; |
|
26
|
|
|
public $fileName = null; |
|
27
|
|
|
|
|
28
|
|
|
public $handlers = [ |
|
29
|
|
|
'app\modules\shop\components\GoogleMerchants\DefaultHandler' |
|
30
|
|
|
]; |
|
31
|
|
|
public $brand_property = 'manufacturer'; |
|
32
|
|
|
|
|
33
|
|
|
public $mainCurrency = null; |
|
34
|
|
|
protected $data = []; |
|
35
|
|
|
|
|
36
|
|
|
const MODIFICATION_DATA = 'modification_data'; |
|
37
|
|
|
|
|
38
|
|
|
public function init() |
|
39
|
|
|
{ |
|
40
|
|
|
self::$modelSetting = new GoogleFeed(); |
|
41
|
|
|
self::$modelSetting->loadConfig(); |
|
42
|
|
|
|
|
43
|
|
|
$this->handlers = Json::decode(self::$modelSetting->feed_handlers); |
|
44
|
|
|
|
|
45
|
|
|
foreach ($this->handlers as $handler) { |
|
46
|
|
|
if (is_subclass_of($handler, ModificationDataInterface::class)) { |
|
|
|
|
|
|
47
|
|
|
$this->on(self::MODIFICATION_DATA, [$handler, 'processData']); |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
parent::init(); |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
if ($this->mainCurrency === null) { |
|
54
|
|
|
$this->mainCurrency = Currency::findOne(['iso_code' => self::$modelSetting->shop_main_currency]); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if ($this->host === null) { |
|
58
|
|
|
$this->host = self::$modelSetting->shop_host; |
|
59
|
|
|
} |
|
60
|
|
|
if ($this->title === null) { |
|
61
|
|
|
$this->title = self::$modelSetting->shop_name; |
|
62
|
|
|
} |
|
63
|
|
|
if ($this->description === null) { |
|
64
|
|
|
$this->description = self::$modelSetting->shop_description; |
|
65
|
|
|
} |
|
66
|
|
|
if ($this->fileName === null) { |
|
67
|
|
|
$this->fileName = self::$modelSetting->feed_file_name; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
public function saveFeedInFs() |
|
74
|
|
|
{ |
|
75
|
|
|
file_put_contents(Yii::getAlias('@webroot/' . $this->fileName), $this->generateFeedByArray($this->getData())); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function getData() |
|
79
|
|
|
{ |
|
80
|
|
|
if ($this->data === []) { |
|
81
|
|
|
$event = new ModificationDataEvent(); |
|
82
|
|
|
$query = Product::find() |
|
83
|
|
|
->where(['active' => 1]); |
|
84
|
|
|
|
|
85
|
|
|
foreach ($query->each() as $product) { |
|
86
|
|
|
$event->model = $product; |
|
87
|
|
|
$this->trigger(self::MODIFICATION_DATA, $event); |
|
88
|
|
|
$this->data[] = $event->dataArray; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
return $this->data; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function generateFeedByArray($data) |
|
95
|
|
|
{ |
|
96
|
|
|
$string = '<?xml version="1.0"?><rss xmlns:g="http://base.google.com/ns/1.0" version="2.0"></rss>'; |
|
97
|
|
|
$xml = simplexml_load_string($string); |
|
98
|
|
|
$channel = $xml->addChild('channel'); |
|
99
|
|
|
$channel->addChild('title', $this->title); |
|
100
|
|
|
$channel->addChild('link', $this->host); |
|
101
|
|
|
$channel->addChild('description', $this->description); |
|
102
|
|
|
foreach ($data as $item) { |
|
103
|
|
|
$currentItem = $channel->addChild("item"); |
|
104
|
|
|
foreach ($item as $key => $value) { |
|
105
|
|
|
$currentItem->$key = $value; |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
return $xml->asXML(); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|