Completed
Push — master ( dee795...c34ef8 )
by Vitaly
02:18
created

Application::isCMS()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php 
2
namespace samsoncms\cms;
3
4
use samson\activerecord\dbQuery;
5
use samson\core\CompressableExternalModule;
6
use samson\pager\Pager;
7
use samsonphp\event\Event;
8
9
/**
10
 * SamsonCMS external compressible application for integrating
11
 * @author Vitaly Iegorov <[email protected]>
12
 */
13
class Application extends CompressableExternalModule {
14
    /** @var string Module identifier */
15
    protected $id = 'cms';
16
17
    protected $isCMS = false;
18
19
    public function isCMS()
20
    {
21
        return $this->isCMS;
22
    }
23
24
    public function init( array $params = array() ) {
25
        Event::subscribe('core.security', array($this, 'updateTemplate'));
26
        // Old applications main page rendering
27
        Event::subscribe('template.main.rendered', array($this, 'oldMainRenderer'));
28
        // Old applications menu rendering
29
        Event::subscribe('template.menu.rendered', array($this, 'oldMenuRenderer'));
30
31
        Event::subscribe('samson.url.build', array($this, 'buildUrl'));
32
        // Call parent initialization
33
        return parent::init( $params );
34
    }
35
36
    public function buildUrl(& $urlObj, & $httpHost, & $urlParams)
37
    {
38
        $index = 0;
39
        if (isset($urlParams[$index]) && (\samson\core\SamsonLocale::current() == $urlParams[$index])) {
40
            $index = 1;
41
        }
42
        if ( isset( $urlParams[$index] ) && ( strpos($urlParams[$index], 'cms-') === 0 ) ) {
43
            $urlParams[$index] = str_replace('cms-', '', $urlParams[$index]);
44
            array_unshift($urlParams, 'cms');
45
        }
46
    }
47
48
    public function updateTemplate($core, $securityResult) {
0 ignored issues
show
Unused Code introduced by
The parameter $securityResult is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
49
        if ($this->isCMS) {
50
            $core->template($this->path().'app/view/index.php', true);
51
        }
52
    }
53
54
    public function initUrl( & $urlObj, & $urlArgs ) {
0 ignored issues
show
Unused Code introduced by
The parameter $urlObj is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
        if($urlArgs[0] == 'cms') {
56
            $this->isCMS = true;
57
            if (isset($urlArgs[1])) {
58
                if (strpos($urlArgs[1], 'samsoncms_') !== 0){
59
                    $urlArgs[1] = 'cms-'.$urlArgs[1];
60
                }
61
                unset($urlArgs[0]);
62
                $urlArgs = array_values($urlArgs);
63
            }
64
        }
65
    }
66
67
    public function initResources(& $resourceRouter, $moduleId, & $approve)
0 ignored issues
show
Unused Code introduced by
The parameter $resourceRouter is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
68
    {
69
        if ($moduleId == 'core') return true;
70
        if ($this->isCMS) {
71
            $approve = false;
72
            if (isset(m($moduleId)->composerParameters['composerName'])&&
73
                isset(m('cms')->composerParameters['required'])&&
74
                in_array(m($moduleId)->composerParameters['composerName'], m('cms')->composerParameters['required'])){
75
                $approve = true;
76
            }
77
        }
78
79
    }
80
81
    public function __base()
82
    {
83
        $this->active(m('template'));
0 ignored issues
show
Documentation Bug introduced by
The method active does not exist on object<samsoncms\cms\Application>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
84
85
        m('template')->__handler();
86
    }
87
88
    public function oldMainRenderer(&$html)
89
    {
90
        // Render application main page block
91
        foreach (\samsoncms\Application::loaded() as $app) {
0 ignored issues
show
Deprecated Code introduced by
The method samsoncms\Application::loaded() has been deprecated.

This method has been deprecated.

Loading history...
92
            // Show only visible apps
93
            if ($app->hide == false && $app->findView('sub_menu')) {
94
                $html .= $app->main();
95
            }
96
        }
97
    }
98
99
    /**
100
     * @deprecated All application should draw menu block via events
101
     */
102
    public function oldMenuRenderer(&$html, &$subMenu)
103
    {
104
        // Iterate loaded samson\cms\application
105
        foreach (\samsoncms\Application::loaded() as $app) {
0 ignored issues
show
Deprecated Code introduced by
The method samsoncms\Application::loaded() has been deprecated.

This method has been deprecated.

Loading history...
106
            // Show only visible apps
107
            if ($app->hide == false) {
108
                // Render application menu item
109
                $html .= m('template')
110
                    ->view('menu/item')
111
                    ->active(url()->module == $app->id() ? 'active' : '')
112
                    ->app($app)
113
                    ->icon($app->icon)
114
                    ->name(isset($app->name{0}) ? $app->name : '')
115
                    ->output();
116
            }
117
        }
118
        $subMenu = '';
119
        // Find current SamsonCMS application
120
        if (\samsoncms\Application::find(url()->module, $app/*@var $app App*/)) {
121
            // If module has sub_menu view - render it
122
            if ($app->findView('sub_menu')) {
123
                $subMenu .= $app->view('sub_menu')->output();
124
            }
125
        }
126
    }
127
128
    /**
129
     * @deprecated
130
     * @return string Page title
131
     */
132
    public function oldGetTitle()
133
    {
134
        $local = m('local');
135
        $current = m();
136
        return isset($current['title']) ? $current['title'] :
137
            (isset($local['title']) ? $local['title'] : '');
138
    }
139
}
140