Completed
Push — development ( eb9524...db4517 )
by Andrij
28:49 queued 02:09
created

PageCategories   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 80
rs 10
c 4
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getGroupName() 0 3 1
B getResultsFor() 0 27 4
A getPagentUrl() 0 13 2
1
<?php
2
3
namespace xbanners\src\UrlFinder\Finders;
4
5
use xbanners\src\UrlFinder\Results\Result;
6
7
final class PageCategories extends BaseFinder
8
{
0 ignored issues
show
introduced by
Opening brace of a class must be on the same line as the definition
Loading history...
9
10
    protected $table = 'category';
11
12
    protected $translations = 'category_translate';
13
14
    protected $nameColumn = 'category_translate.name';
15
16
    protected $urlColumn = 'category.url';
17
18
    protected $langColumn = 'category_translate.lang';
19
20
    protected $parentId = 'category.parent_id';
21
22
    /**
23
     * @return string
24
     */
25
    public function getGroupName() {
26
        return lang('Page Categories', 'xbanners');
27
    }
28
29
    /**
30
     * @param string $word
31
     * @param string $language
32
     * @param integer $limit
33
     * @return Result
34
     */
35
    public function getResultsFor($word, $language, $limit = 10) {
36
        $languageId = $this->correctLocale($language);
37
        $db = \CI::$APP->db;
38
        if ($language == \MY_Controller::defaultLocale()) {
39
            $this->nameColumn = 'category.name';
40
        } else {
41
            $db->join($this->translations, "{$this->table}.id = {$this->translations}.alias");
42
            $db->where($this->langColumn, $languageId);
43
        };
44
        /* @var $db \CI_DB_active_record */
45
        $results = $db->select("$this->nameColumn, $this->urlColumn, $this->parentId")
46
            ->from($this->table)
47
            ->like($this->nameColumn, $word)
48
            ->limit($limit)
49
            ->get()
50
            ->result_array();
51
52
        $result = new Result($this->getGroupName());
53
        foreach ($results as $oneResult) {
54
            if ($oneResult['parent_id'] > 0) {
55
                $oneResult['url'] = $this->getPagentUrl($oneResult['parent_id']) . $oneResult['url'];
56
            }
57
            $result->addResult($oneResult['name'], $this->formUrl($oneResult['url']));
58
        }
59
60
        return $result;
61
    }
62
63
    /**
64
     * Returns url segments
65
     * from all parent categories
66
     *
67
     * @param integer $id
68
     * @return string parent url with last "/" symbol
69
     */
70
    protected function getPagentUrl($id) {
71
        /* @var $db \CI_DB_active_record */
72
        $db = \CI::$APP->db;
73
        $res = $db->select("$this->urlColumn, $this->parentId")
74
            ->where('id', $id)
75
            ->get($this->table)
76
            ->first_row();
77
        $url = $res->url;
78
        if ($res->parent_id > 0) {
79
            $url = $this->getPagentUrl($res->parent_id) . $url;
80
        }
81
        return $url . '/';
82
    }
83
84
    /**
85
     * @param string $url
86
     * @return string
87
     */
88
    public function formUrl($url) {
89
        return '/' . $url;
90
    }
91
92
}