Completed
Push — development ( 67765c...7029e6 )
by Andrij
18:12
created

Mod_seo::_buildCategoryMeta()   D

Complexity

Conditions 20
Paths 34

Size

Total Lines 95
Code Lines 64

Duplication

Lines 40
Ratio 42.11 %

Importance

Changes 0
Metric Value
cc 20
eloc 64
nc 34
nop 1
dl 40
loc 95
rs 4.7294
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
use CMSFactory\Events;
4
use CMSFactory\MetaManipulator\GalleryMetaManipulator;
5
use CMSFactory\MetaManipulator\MetaManipulator;
6
use CMSFactory\MetaManipulator\MetaStorage;
7
use CMSFactory\MetaManipulator\PageCategoryMetaManipulator;
8
use CMSFactory\MetaManipulator\PageMetaManipulator;
9
use CMSFactory\MetaManipulator\ShopBrandMetaManipulator;
10
use CMSFactory\MetaManipulator\ShopCategoryMetaManipulator;
11
use CMSFactory\MetaManipulator\ShopProductMetaManipulator;
12
13
(defined('BASEPATH')) OR exit('No direct script access allowed');
14
15
/**
16
 *
17
 * @property Seoexpert_model $seoexpert_model
18
 * @property Seoexpert_model_products $seoexpert_model_products
19
 * @author <[email protected]>
20
 * @copyright ImageCMS (c) 2014
21
 */
22
class Mod_seo extends MY_Controller
23
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
24
25
    public function __construct() {
26
27
        parent::__construct();
28
        $this->load->model('seoexpert_model');
29
        $this->load->model('seoexpert_model_products');
30
        $this->load->helper('translit');
31
32
        $lang = new MY_Lang();
33
        $lang->load('mod_seo');
34
    }
35
36
    /**
37
     * Build Meta for Shop Brand
38
     * @param array $arg
39
     * @return false|null
40
     */
41
    public static function _buildBrandMeta($arg) {
42
43
        $local = MY_Controller::getCurrentLocale();
44
        /* @var $model SBrands */
45
        $model = $arg['model'];
46
        $settings = CI::$APP->seoexpert_model->getSettings($local);
47
48
        CI::$APP->core->setLastModified($model->getUpdated());
49
50
        if ($settings['useBrandPattern'] != 1) {
51
            return FALSE;
52
        }
53
54 View Code Duplication
        if ($settings['useBrandPatternForEmptyMeta'] == 1 && trim($model->getMetaTitle()) != '') {
55
            $templateTitle = trim($model->getMetaTitle());
56
        } else {
57
            $templateTitle = $settings['brandTemplate'];
58
        }
59
60 View Code Duplication
        if ($settings['useBrandPatternForEmptyMeta'] == 1 && trim($model->getMetaDescription()) != '') {
61
            $templateDesc = trim($model->getMetaDescription());
62
        } else {
63
            $templateDesc = $settings['brandTemplateDesc'];
64
        }
65
66 View Code Duplication
        if ($settings['useBrandPatternForEmptyMeta'] == 1 && trim($model->getMetaKeywords()) != '') {
67
            $templateKey = trim($model->getMetaKeywords());
68
        } else {
69
            $templateKey = $settings['brandTemplateKey'];
70
        }
71
72
        $metaStorage = new MetaStorage();
73
        $metaStorage->setTitleTemplate($templateTitle);
74
        $metaStorage->setDescriptionTemplate($templateDesc);
75
        $metaStorage->setKeywordsTemplate($templateKey);
76
77
        $metaManipulator = new ShopBrandMetaManipulator($model, $metaStorage);
78
        $metaManipulator->setDescLength($settings['brandTemplateDescCount']);
79
        $metaManipulator->setPageNumber($settings['brandPaginationTemplate']);
80
        $meta = $metaManipulator->render();
81
82
        // Set meta tags
83
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
84
    }
85
86
    /**
87
     * @param string $template
88
     * @param string $templateKey
89
     * @param string $templateDesc
90
     */
91
    private static function setMetaTags($template, $templateKey, $templateDesc) {
92
93
        CI::$APP->core->set_meta_tags($template, $templateKey, $templateDesc);
94
    }
95
96
    /**
97
     * Build Meta for Shop Brands page
98
     * @return false|null
99
     */
100
    public static function _buildBrandsMeta() {
101
102
        $local = MY_Controller::getCurrentLocale();
103
        $settings = CI::$APP->seoexpert_model->getSettings($local);
104
105
        if ($settings['useBrandsListPattern'] != 1) {
106
            return FALSE;
107
        }
108
109
        // Set meta tags
110
        self::setMetaTags($settings['brandsListTemplate'], $settings['brandsListTemplateKey'], $settings['brandsListTemplateDesc']);
111
    }
112
113
    /**
114
     * Build Meta for Shop Category
115
     * @param Category $categoryObj
116
     * @return false|null
117
     */
118
    public static function _buildCategoryMeta($categoryObj) {
119
120
        CI::$APP->load->helper('array_helper');
121
122
        $local = MY_Controller::getCurrentLocale();
123
        /* @var $model SCategory */
124
        $model = $categoryObj->data['category'];
125
126
        CI::$APP->core->setLastModified($model->getUpdated());
127
128
        $settings = ShopCore::$ci->seoexpert_model->getSettings($local);
129
130
        if ($model->getParentId() > 0) {
131
            if ($settings['usesubcategoryPattern'] != 1) {
132
                return FALSE;
133
            }
134
135 View Code Duplication
            if ($settings['usesubcategoryPatternForEmptyMeta'] == 1 && trim($model->getH1()) != '') {
136
                $templateH1 = trim($model->getH1());
137
            } else {
138
                $templateH1 = $settings['subcategoryTemplateH1'];
139
            }
140
141 View Code Duplication
            if ($settings['usesubcategoryPatternForEmptyMeta'] == 1 && trim($model->getMetaTitle()) != '') {
142
                $templateTitle = trim($model->getMetaTitle());
143
            } else {
144
                $templateTitle = $settings['subcategoryTemplate'];
145
            }
146
147 View Code Duplication
            if ($settings['usesubcategoryPatternForEmptyMeta'] == 1 && trim($model->getMetaDesc()) != '') {
148
                $templateDesc = trim($model->getMetaDesc());
149
            } else {
150
                $templateDesc = $settings['subcategoryTemplateDesc'];
151
            }
152
153 View Code Duplication
            if ($settings['usesubcategoryPatternForEmptyMeta'] == 1 && trim($model->getMetaKeywords()) != '') {
154
                $templateKey = trim($model->getMetaKeywords());
155
            } else {
156
                $templateKey = $settings['subcategoryTemplateKey'];
157
            }
158
            $brandsCount = $settings['subcategoryTemplateBrandsCount'];
159
            $paginationTemplate = $settings['subcategoryTemplatePaginationTemplate'];
160
            $descCount = $settings['subcategoryTemplateDescCount'];
161
        } else {
162
            if ($settings['useCategoryPattern'] != 1) {
163
                return FALSE;
164
            }
165
166 View Code Duplication
            if ($settings['useCategoryPatternForEmptyMeta'] == 1 && trim($model->getH1()) != '') {
167
                $templateH1 = trim($model->getH1());
168
            } else {
169
                $templateH1 = $settings['categoryTemplateH1'];
170
            }
171
172 View Code Duplication
            if ($settings['useCategoryPatternForEmptyMeta'] == 1 && trim($model->getMetaTitle()) != '') {
173
                $templateTitle = trim($model->getMetaTitle());
174
            } else {
175
                $templateTitle = $settings['categoryTemplate'];
176
            }
177
178 View Code Duplication
            if ($settings['useCategoryPatternForEmptyMeta'] == 1 && trim($model->getMetaDesc()) != '') {
179
                $templateDesc = trim($model->getMetaDesc());
180
            } else {
181
                $templateDesc = $settings['categoryTemplateDesc'];
182
            }
183
184 View Code Duplication
            if ($settings['useCategoryPatternForEmptyMeta'] == 1 && trim($model->getMetaKeywords()) != '') {
185
                $templateKey = trim($model->getMetaKeywords());
186
            } else {
187
                $templateKey = $settings['categoryTemplateKey'];
188
            }
189
            $brandsCount = $settings['categoryTemplateBrandsCount'];
190
            $paginationTemplate = $settings['categoryTemplatePaginationTemplate'];
191
            $descCount = $settings['categoryTemplateDescCount'];
192
        }
193
194
        $metaStorage = new MetaStorage();
195
        $metaStorage->setH1Template($templateH1);
196
        $metaStorage->setTitleTemplate($templateTitle);
197
        $metaStorage->setDescriptionTemplate($templateDesc);
198
        $metaStorage->setKeywordsTemplate($templateKey);
199
200
        $metaManipulator = new ShopCategoryMetaManipulator($model, $metaStorage);
201
        $metaManipulator->setPageNumber($paginationTemplate);
202
        $metaManipulator->setBrandsCount($brandsCount);
203
        $metaManipulator->setDescLength($descCount);
204
        $meta = $metaManipulator->render();
205
206
        $model->setH1($meta['metaH1']);
207
        $model->setVirtualColumn('title', $meta['metaH1']);
208
        $categoryObj->data['title'] = $meta['metaH1'];
209
210
        // Set meta tags
211
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
212
    }
213
214
    /**
215
     * Change page category meta tags
216
     *
217
     * @param array $model
218
     * @return false|null
219
     */
220 View Code Duplication
    public static function _buildPageCategoryMeta($model) {
221
222
        $local = MY_Controller::getCurrentLocale();
223
224
        CI::$APP->core->setLastModified($model['updated']);
225
226
        $settings = CI::$APP->seoexpert_model->getSettings($local);
227
228
        if ($settings['usePageCategoryPattern'] != 1) {
229
            return FALSE;
230
        }
231
232
        // Use for Empty meta
233
        if ($settings['usePageCategoryPatternForEmptyMeta'] == 1 && trim($model['title']) != '') {
234
            $templateTitle = trim($model['title']);
235
        } else {
236
            $templateTitle = $settings['pageCategoryTemplateTitle'];
237
        }
238
239
        if ($settings['usePageCategoryPatternForEmptyMeta'] == 1 && trim($model['description']) != '') {
240
            $templateDesc = trim($model['description']);
241
        } else {
242
            $templateDesc = $settings['pageCategoryTemplateDesc'];
243
        }
244
245
        if ($settings['usePageCategoryPatternForEmptyMeta'] == 1 && trim($model['keywords']) != '') {
246
            $templateKey = trim($model['keywords']);
247
        } else {
248
            $templateKey = $settings['pageCategoryTemplateKey'];
249
        }
250
251
        $metaStorage = new MetaStorage();
252
        $metaStorage->setTitleTemplate($templateTitle);
253
        $metaStorage->setDescriptionTemplate($templateDesc);
254
        $metaStorage->setKeywordsTemplate($templateKey);
255
256
        $metaManipulator = new PageCategoryMetaManipulator($model, $metaStorage);
257
        $metaManipulator->setPageNumber($settings['pageCategoryTemplatePaginationTemplate']);
258
        $metaManipulator->setDescLength($settings['pageCategoryTemplateDescCount']);
259
        $meta = $metaManipulator->render();
260
261
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
262
    }
263
264
    /**
265
     * Change simple page meta tags
266
     *
267
     * @param array $model
268
     * @return false|null
269
     */
270 View Code Duplication
    public static function _buildPageMeta($model) {
271
272
        CI::$APP->core->setLastModified($model['updated']);
273
274
        $local = MY_Controller::getCurrentLocale();
275
        $settings = CI::$APP->seoexpert_model->getSettings($local);
276
277
        if ($settings['usePagePattern'] != 1) {
278
            return FALSE;
279
        }
280
281
        // Use for Empty meta
282
        if ($settings['usePagePatternForEmptyMeta'] == 1 && trim($model['meta_title']) != '') {
283
            $templateTitle = trim($model['meta_title']);
284
        } else {
285
            $templateTitle = $settings['pageTemplateTitle'];
286
        }
287
288
        if ($settings['usePagePatternForEmptyMeta'] == 1 && trim($model['description']) != '') {
289
            $templateDesc = trim($model['description']);
290
        } else {
291
            $templateDesc = $settings['pageTemplateDesc'];
292
        }
293
294
        if ($settings['usePagePatternForEmptyMeta'] == 1 && trim($model['keywords']) != '') {
295
            $templateKey = trim($model['keywords']);
296
        } else {
297
            $templateKey = $settings['pageTemplateKey'];
298
        }
299
300
        $metaStorage = new MetaStorage();
301
        $metaStorage->setTitleTemplate($templateTitle);
302
        $metaStorage->setDescriptionTemplate($templateDesc);
303
        $metaStorage->setKeywordsTemplate($templateKey);
304
305
        $metaManipulator = new PageMetaManipulator($model, $metaStorage);
306
        $metaManipulator->setDescLength($settings['pageTemplateDescCount']);
307
        $meta = $metaManipulator->render();
308
309
        // Set meta tags
310
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
311
    }
312
313
    /**
314
     * Build Meta tags for Shop Product
315
     * @param array $arg
316
     * @return false|null
317
     */
318
    public static function _buildProductsMeta($arg) {
319
320
        /* @var $model SProducts */
321
        $model = $arg['model'];
322
        $local = MY_Controller::getCurrentLocale();
323
324
        CI::$APP->core->setLastModified($model->getUpdated());
325
326
        // Get categories ids which has unique settings
327
        $uniqueCategories = CI::$APP->seoexpert_model_products->getCategoriesArray();
328
        // Check is common categories or unique
329
        if (in_array($model->getCategoryId(), $uniqueCategories)) {
330
            $settings = CI::$APP->seoexpert_model_products->getProductCategory($model->getCategoryId(), $local);
331
            $settings = $settings['settings'];
332
        } else {
333
            $settings = CI::$APP->seoexpert_model->getSettings($local);
334
        }
335
336
        // Is active
337
        if ($settings['useProductPattern'] != 1) {
338
            return FALSE;
339
        }
340
341
        // Use for Empty meta
342 View Code Duplication
        if ($settings['useProductPatternForEmptyMeta'] == 1 && trim($model->getMetaTitle()) != '') {
343
            $templateTitle = trim($model->getMetaTitle());
344
        } else {
345
            $templateTitle = $settings['productTemplate'];
346
        }
347
348 View Code Duplication
        if ($settings['useProductPatternForEmptyMeta'] == 1 && trim($model->getMetaDescription()) != '') {
349
            $templateDesc = trim($model->getMetaDescription());
350
        } else {
351
            $templateDesc = $settings['productTemplateDesc'];
352
        }
353
354 View Code Duplication
        if ($settings['useProductPatternForEmptyMeta'] == 1 && trim($model->getMetaKeywords()) != '') {
355
            $templateKey = trim($model->getMetaKeywords());
356
        } else {
357
            $templateKey = $settings['productTemplateKey'];
358
        }
359
360
        $descCount = $settings['productTemplateDescCount'];
361
362
        $metaStorage = new MetaStorage();
363
        $metaStorage->setTitleTemplate($templateTitle);
364
        $metaStorage->setDescriptionTemplate($templateDesc);
365
        $metaStorage->setKeywordsTemplate($templateKey);
366
367
        $metaManipulator = new ShopProductMetaManipulator($model, $metaStorage);
368
        $metaManipulator->setDescLength($descCount);
369
        $meta = $metaManipulator->render();
370
371
        // Set meta tags
372
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
373
    }
374
375
    /**
376
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

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...
377
     */
378 View Code Duplication
    public static function _buildSearchMeta() {
379
380
        $local = MY_Controller::getCurrentLocale();
381
        $settings = CI::$APP->seoexpert_model->getSettings($local);
382
383
        if ($settings['useSearchPattern'] != 1) {
384
            return FALSE;
385
        }
386
387
        $metaStorage = new MetaStorage();
388
        $metaStorage->setTitleTemplate($settings['searchTemplate']);
389
        $metaStorage->setDescriptionTemplate($settings['searchTemplateDesc']);
390
        $metaStorage->setKeywordsTemplate($settings['searchTemplateKey']);
391
392
        $metaManipulator = new MetaManipulator([], $metaStorage);
393
        $meta = $metaManipulator->render();
394
395
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
396
    }
397
398
    /**
399
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

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...
400
     */
401 View Code Duplication
    public static function _buildGalleryMeta() {
402
403
        $local = MY_Controller::getCurrentLocale();
404
        $settings = CI::$APP->seoexpert_model->getSettings($local);
405
406
        if ($settings['useGalleryPattern'] != 1) {
407
            return FALSE;
408
        }
409
410
        $metaStorage = new MetaStorage();
411
        $metaStorage->setTitleTemplate($settings['galleryTemplate']);
412
        $metaStorage->setDescriptionTemplate($settings['galleryTemplateDesc']);
413
        $metaStorage->setKeywordsTemplate($settings['galleryTemplateKey']);
414
415
        $metaManipulator = new MetaManipulator([], $metaStorage);
416
        $meta = $metaManipulator->render();
417
418
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
419
    }
420
421
    /**
422
     * @param array $data
423
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

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...
424
     */
425 View Code Duplication
    public static function _buildGalleryCategoryMeta($data) {
426
427
        $local = MY_Controller::getCurrentLocale();
428
        $settings = CI::$APP->seoexpert_model->getSettings($local);
429
430
        if ($settings['useGalleryCategoryPattern'] != 1) {
431
            return FALSE;
432
        }
433
434
        $metaStorage = new MetaStorage();
435
        $metaStorage->setTitleTemplate($settings['galleryCategoryTemplate']);
436
        $metaStorage->setDescriptionTemplate($settings['galleryCategoryTemplateDesc']);
437
        $metaStorage->setKeywordsTemplate($settings['galleryCategoryTemplateKey']);
438
439
        $metaManipulator = new GalleryMetaManipulator($data['current_category'], $metaStorage);
440
        $metaManipulator->setDescLength($settings['galleryCategoryTemplateDescCount']);
441
        $meta = $metaManipulator->render();
442
443
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
444
    }
445
446
    /**
447
     * @param array $data
448
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

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...
449
     */
450
    public static function _buildGalleryAlbumMeta($data) {
451
452
        /** Передаем имя категории в дату, которая передается в качестве модели*/
453
        $data['album']['category_name'] = $data['current_category']['name'];
454
455
        $local = MY_Controller::getCurrentLocale();
456
        $settings = CI::$APP->seoexpert_model->getSettings($local);
457
458
        if ($settings['useGalleryAlbumPattern'] != 1) {
459
            return FALSE;
460
        }
461
462
        $metaStorage = new MetaStorage();
463
        $metaStorage->setTitleTemplate($settings['galleryAlbumTemplate']);
464
        $metaStorage->setDescriptionTemplate($settings['galleryAlbumTemplateDesc']);
465
        $metaStorage->setKeywordsTemplate($settings['galleryAlbumTemplateKey']);
466
467
        $metaManipulator = new GalleryMetaManipulator($data['album'], $metaStorage);
468
        $metaManipulator->setDescLength($settings['galleryAlbumTemplateDescCount']);
469
        $meta = $metaManipulator->render();
470
471
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
472
    }
473
474
    /**
475
     * @param array $data
476
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|null?

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...
477
     */
478 View Code Duplication
    public static function _buildActionSearchMeta($data) {
479
480
        $local = MY_Controller::getCurrentLocale();
481
        $settings = CI::$APP->seoexpert_model->getSettings($local);
482
483
        $new_setting = CI::$APP->seoexpert_model->Actions_settings($settings, $data['type']);
484
485
        if ($new_setting['usePattern'] != 1) {
486
            return FALSE;
487
        }
488
489
        $metaStorage = new MetaStorage();
490
        $metaStorage->setTitleTemplate($new_setting['TitleTemplate']);
491
        $metaStorage->setDescriptionTemplate($new_setting['TemplateDesc']);
492
        $metaStorage->setKeywordsTemplate($new_setting['KeywordsTemplate']);
493
494
        $metaManipulator = new MetaManipulator([], $metaStorage);
495
        $meta = $metaManipulator->render();
496
497
        self::setMetaTags($meta['metaTitle'], $meta['metaKeywords'], $meta['metaDescription']);
498
    }
499
500
    public function _deinstall() {
501
502
        CI::$APP->seoexpert_model->deinstall();
503
    }
504
505
    public function _install() {
506
507
        CI::$APP->seoexpert_model->install();
508
    }
509
510
    public function autoload() {
511
512
        // Shop
513
        Events::create()->onSearchPageLoad()->setListener('_buildSearchMeta');
514
        Events::create()->onBrandPageLoad()->setListener('_buildBrandMeta');
515
        Events::create()->onBrandsPageLoad()->setListener('_buildBrandsMeta');
516
        Events::create()->onProductPageLoad()->setListener('_buildProductsMeta');
517
        Events::create()->onCategoryPageLoad()->setListener('_buildCategoryMeta');
518
        Events::create()->onActionTypeSearch()->setListener('_buildActionSearchMeta');
519
520
        // Core
521
        Events::create()->onPageLoad()->setListener('_buildPageMeta');
522
        Events::create()->onPageCategoryLoad()->setListener('_buildPageCategoryMeta');
523
        Events::create()->onGalleryLoad()->setListener('_buildGalleryMeta');
524
        Events::create()->onGalleryCategoryLoad()->setListener('_buildGalleryCategoryMeta');
525
        Events::create()->onGalleryAlbumLoad()->setListener('_buildGalleryAlbumMeta');
526
527
    }
528
}