Passed
Push — develop ( 94894c...636f36 )
by Nikolay
06:22
created

ModulesModelsBase::getRepresent()   A

Complexity

Conditions 6
Paths 14

Size

Total Lines 34
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 19
dl 0
loc 34
rs 9.0111
c 1
b 0
f 0
cc 6
nc 14
nop 1
1
<?php
2
/**
3
 * Copyright (C) MIKO LLC - All Rights Reserved
4
 * Unauthorized copying of this file, via any medium is strictly prohibited
5
 * Proprietary and confidential
6
 * Written by Nikolay Beketov, 7 2020
7
 *
8
 */
9
10
namespace MikoPBX\Modules\Models;
11
12
use MikoPBX\Common\Models\ModelsBase;
13
use Phalcon\Text;
14
use Phalcon\Url;
15
use ReflectionClass as ReflectionClassAlias;
16
17
class ModulesModelsBase extends ModelsBase
18
{
19
    protected bool $initialized = false;
20
    protected string $moduleUniqueId;
21
22
    /**
23
     * Class initialization and create DB connection by Class and DB name,
24
     * it uses on src/Common/Providers/ModulesDBConnectionsProvider.php
25
     */
26
    public function initialize(): void
27
    {
28
        // Get child class parameters and define module UniqueID
29
        $reflector        = new ReflectionClassAlias(static::class);
30
        $partsOfNameSpace = explode('\\', $reflector->getNamespaceName());
31
        if (count($partsOfNameSpace) === 3 && $partsOfNameSpace[0] === 'Modules') {
32
            $this->moduleUniqueId = $partsOfNameSpace[1];
33
            $this->setConnectionService("{$this->moduleUniqueId}_module_db");
34
        }
35
        parent::initialize();
36
        $this->initialized=true;
37
    }
38
39
    /**
40
     *  Returns module name in human readable form for error and notification messages
41
     *
42
     * @param bool $needLink
43
     *
44
     * @return string
45
     */
46
    public function getRepresent($needLink = false): string
47
    {
48
        if (!$this->initialized){
49
            $this->initialize();
50
        }
51
52
        if ($this->readAttribute('id') === null) {
53
            return $this->t('mo_NewElement');
54
        }
55
56
        if (isset($this->moduleUniqueId)) {
57
            $name = '<i class="puzzle piece icon"></i> '
58
                . $this->t('mo_' . $this->moduleUniqueId);
59
        } else {
60
            $name = 'Unknown';
61
        }
62
63
        if ($needLink) {
64
            if (empty($name)) {
65
                $name = $this->t('repLink');
66
            }
67
            $link = $this->getWebInterfaceLink();
68
69
            $result = $this->t(
70
                'rep' . $this->moduleUniqueId,
71
                [
72
                    'represent' => "<a href='{$link}'>{$name}</a>",
73
                ]
74
            );
75
        } else {
76
            $result = $name;
77
        }
78
79
        return $result;
80
    }
81
82
    /**
83
     * Return link on database record in web interface
84
     *
85
     * @return string
86
     */
87
    public function getWebInterfaceLink(): string
88
    {
89
        if (!$this->initialized){
90
            $this->initialize();
91
        }
92
        if (isset($this->moduleUniqueId)) {
93
            $url  = new Url();
94
            $link = $url->get(Text::uncamelize($this->moduleUniqueId, '-'));
95
        } else {
96
            $link = '#';
97
        }
98
99
        return $link;
100
    }
101
102
}