1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use mod_link\models\PageLink; |
4
|
|
|
use mod_link\models\PageLinkProduct; |
5
|
|
|
use mod_link\models\PageLinkProductQuery; |
6
|
|
|
use mod_link\models\PageLinkQuery; |
7
|
|
|
use Propel\Runtime\ActiveQuery\Criteria; |
8
|
|
|
use Propel\Runtime\Collection\ObjectCollection; |
9
|
|
|
|
10
|
|
|
(defined('BASEPATH')) OR exit('No direct script access allowed'); |
11
|
|
|
|
12
|
|
|
class Mod_link extends MY_Controller |
13
|
|
|
{ |
|
|
|
|
14
|
|
|
|
15
|
|
|
public function __construct() { |
16
|
|
|
parent::__construct(); |
17
|
|
|
$lang = new MY_Lang(); |
18
|
|
|
$lang->load('mod_link'); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param $pageId |
|
|
|
|
23
|
|
|
* @return array |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
public function getLinkByPage($pageId) { |
26
|
|
|
$pageLink = PageLinkQuery::create()->findOneActiveByPageId($pageId); |
27
|
|
|
return $pageLink; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param int $productId |
32
|
|
|
* @return array |
33
|
|
|
*/ |
34
|
|
|
public function getLinksByProduct($productId) { |
35
|
|
|
$pageLinkProducts = PageLinkProductQuery::create() |
36
|
|
|
->joinWithPageLink() |
37
|
|
|
->usePageLinkQuery() |
38
|
|
|
->filterByaActive() |
39
|
|
|
->filterByShowOn(true) |
40
|
|
|
->endUse() |
41
|
|
|
->filterByProductId($productId) |
42
|
|
|
->find(); |
43
|
|
|
|
44
|
|
|
$links = []; |
45
|
|
|
/** @var PageLinkProduct $pageLinkProduct */ |
46
|
|
|
foreach ($pageLinkProducts as $pageLinkProduct) { |
47
|
|
|
$link = $pageLinkProduct->getPageLink(); |
48
|
|
|
$page = $this->getPage($link->getPageId()); |
49
|
|
|
$page = $this->load->module('cfcm')->connect_fields($page, 'page'); |
50
|
|
|
$link->setPageData($page); |
51
|
|
|
array_push($links, $link); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $links; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param $id\ |
|
|
|
|
59
|
|
|
* @return mixed page data |
60
|
|
|
*/ |
61
|
|
|
private function getPage($id) { |
62
|
|
|
$query = $this->db->select('content.*') |
63
|
|
|
->select('CONCAT(content.cat_url,content.url ) as full_url') |
64
|
|
|
->where('id', $id) |
65
|
|
|
->where('lang', $this->config->item('cur_lang')) |
66
|
|
|
->get('content'); |
67
|
|
|
|
68
|
|
|
if ($query->num_rows() > 0) { |
69
|
|
|
$page = $query->row_array(); |
70
|
|
|
return $page; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string locale |
77
|
|
|
*/ |
78
|
|
|
private static function getPageEditLocale() { |
79
|
|
|
$langId = CI::$APP->uri->segment(5); |
80
|
|
|
$db = CI::$APP->db; |
81
|
|
|
$lang = MY_Controller::defaultLocale(); |
82
|
|
|
$query = $db->select('identif')->where('id', $langId)->get('languages'); |
83
|
|
|
if ($query->num_rows() > 0) { |
84
|
|
|
$lang = $query->row_array()['identif']; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $lang; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Register page extension tab content |
92
|
|
|
* @throws Exception |
|
|
|
|
93
|
|
|
*/ |
94
|
|
|
public static function adminAutoload() { |
95
|
|
|
\CMSFactory\Events::create()->onAdminPagePreEdit()->setListener('_extendPageAdmin'); |
96
|
|
|
\CMSFactory\Events::create()->onAdminPageUpdate()->setListener('_updateProducts'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param $page |
|
|
|
|
101
|
|
|
* @throws \Propel\Runtime\Exception\PropelException |
|
|
|
|
102
|
|
|
*/ |
103
|
|
|
public static function _updateProducts($page) { |
104
|
|
|
$ci = CI::$APP; |
105
|
|
|
$linkedData = $ci->input->post('linked') ?: []; |
106
|
|
|
|
107
|
|
|
$pageId = $page['id']; |
108
|
|
|
$productIds = array_key_exists('products', $linkedData) ? $linkedData['products'] : []; |
109
|
|
|
$showOn = array_key_exists('show_on', $linkedData) ? true : false; |
110
|
|
|
$activeFrom = array_key_exists('active_from', $linkedData) ? strtotime($linkedData['active_from']) : null; |
111
|
|
|
$activeTo = array_key_exists('active_to', $linkedData) ? strtotime($linkedData['active_to']) : null; |
112
|
|
|
$permanent = array_key_exists('permanent', $linkedData) ? true : false; |
113
|
|
|
|
114
|
|
|
$pageLink = PageLinkQuery::create()->findOneByPageId($pageId); |
115
|
|
|
$pageLink = $pageLink ?: new PageLink(); |
116
|
|
|
|
117
|
|
|
$pageLink->setPageId($pageId); |
118
|
|
|
$pageLink->setShowOn($showOn); |
119
|
|
|
$pageLink->setActiveFrom($activeFrom); |
120
|
|
|
$pageLink->setActiveTo($activeTo); |
121
|
|
|
$pageLink->setPermanent($permanent); |
122
|
|
|
|
123
|
|
|
$products = SProductsQuery::create()->filterById($productIds, Criteria::IN)->find(); |
124
|
|
|
|
125
|
|
|
$pageLinkProductsCollection = new ObjectCollection(); |
126
|
|
|
PageLinkProductQuery::create()->filterByLinkId($pageLink->getId())->delete(); |
127
|
|
|
foreach ($products as $product) { |
128
|
|
|
$pageLinkProduct = new PageLinkProduct(); |
129
|
|
|
$pageLinkProduct->setPageLink($pageLink); |
130
|
|
|
$pageLinkProduct->setProductId($product->getId()); |
131
|
|
|
$pageLinkProductsCollection->append($pageLinkProduct); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
$pageLink->setPageLinkProducts($pageLinkProductsCollection); |
135
|
|
|
$pageLink->save(); |
136
|
|
|
|
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Display module template on tab "Modules additions" when edit page. |
141
|
|
|
* @param array $data |
142
|
|
|
*/ |
143
|
|
|
public static function _extendPageAdmin($data) { |
144
|
|
|
|
145
|
|
|
$pageId = $data['pageId']; |
146
|
|
|
|
147
|
|
|
$locale = self::getPageEditLocale(); |
148
|
|
|
|
149
|
|
|
$pageLink = PageLinkQuery::create()->findOneByPageId($pageId); |
150
|
|
|
|
151
|
|
|
$pageLink = $pageLink ?: new PageLink(); |
152
|
|
|
|
153
|
|
|
$view = \CMSFactory\assetManager::create() |
154
|
|
|
->setData(compact('pageLink', 'locale')) |
155
|
|
|
->registerScript('script') |
156
|
|
|
->fetchTemplate('/admin/pageTab'); |
157
|
|
|
|
158
|
|
|
\CMSFactory\assetManager::create() |
159
|
|
|
->appendData('moduleAdditions', $view); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function _install() { |
163
|
|
|
$this->load->dbforge(); |
164
|
|
|
|
165
|
|
|
$page_link_fields = [ |
166
|
|
|
'id' => [ |
167
|
|
|
'type' => 'INT', |
168
|
|
|
'constraint' => 11, |
169
|
|
|
'auto_increment' => true, |
170
|
|
|
], |
171
|
|
|
'page_id' => [ |
172
|
|
|
'type' => 'INT', |
173
|
|
|
'constraint' => 11, |
174
|
|
|
], |
175
|
|
|
'active_from' => [ |
176
|
|
|
'type' => 'int', |
177
|
|
|
'constraint' => 11, |
178
|
|
|
'null' => true, |
179
|
|
|
], |
180
|
|
|
'active_to' => [ |
181
|
|
|
'type' => 'int', |
182
|
|
|
'constraint' => 11, |
183
|
|
|
'null' => true, |
184
|
|
|
], |
185
|
|
|
'show_on' => [ |
186
|
|
|
'type' => 'TINYINT', |
187
|
|
|
'default' => 1, |
188
|
|
|
], |
189
|
|
|
'permanent' => [ |
190
|
|
|
'type' => 'TINYINT', |
191
|
|
|
'default' => 1, |
192
|
|
|
], |
193
|
|
|
]; |
194
|
|
|
|
195
|
|
|
$this->dbforge->add_field($page_link_fields); |
196
|
|
|
$this->dbforge->add_key('id', TRUE); |
197
|
|
|
$this->dbforge->create_table('page_link'); |
198
|
|
|
|
199
|
|
|
$page_link_products_fields = [ |
200
|
|
|
'link_id' => [ |
201
|
|
|
'type' => 'INT', |
202
|
|
|
'constraint' => 11, |
203
|
|
|
], |
204
|
|
|
'product_id' => [ |
205
|
|
|
'type' => 'INT', |
206
|
|
|
'constraint' => 11, |
207
|
|
|
], |
208
|
|
|
]; |
209
|
|
|
$this->dbforge->add_field($page_link_products_fields); |
210
|
|
|
$this->dbforge->add_key(['link_id', 'product_id']); |
211
|
|
|
|
212
|
|
|
$this->dbforge->create_table('page_link_product', TRUE); |
213
|
|
|
|
214
|
|
|
$this->db->where('name', 'mod_link') |
215
|
|
|
->update('components', ['autoload' => '1']); |
216
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function _deinstall() { |
220
|
|
|
$this->load->dbforge(); |
221
|
|
|
$this->dbforge->drop_table('page_link'); |
222
|
|
|
$this->dbforge->drop_table('page_link_product'); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/* End of file sample_module.php */ |