Completed
Push — development ( 227e3a...98bb7e )
by Andrij
14:37
created

Mod_link::_updateProducts()   F

Complexity

Conditions 9
Paths 256

Size

Total Lines 35
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 9
eloc 26
c 1
b 0
f 0
nc 256
nop 1
dl 0
loc 35
rs 3
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
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
14
15
    public function __construct() {
16
        parent::__construct();
17
        $lang = new MY_Lang();
18
        $lang->load('mod_link');
19
    }
20
21
    /**
22
     * @param $pageId
0 ignored issues
show
introduced by
Missing parameter type
Loading history...
23
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be PageLink?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
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\
0 ignored issues
show
introduced by
Missing parameter type
Loading history...
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
0 ignored issues
show
introduced by
Comment missing or not on the next line for @throws tag in function comment
Loading history...
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
0 ignored issues
show
introduced by
Missing parameter type
Loading history...
101
     * @throws \Propel\Runtime\Exception\PropelException
0 ignored issues
show
introduced by
Comment missing or not on the next line for @throws tag in function comment
Loading history...
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 */