Completed
Push — master ( 805de4...131e3d )
by Vitaly
02:35
created

Application::createDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
//[PHPCOMPRESSOR(remove,start)]
3
/**
4
 * Created by Vitaly Iegorov <[email protected]>.
5
 * on 23.03.16 at 14:51
6
 */
7
namespace samsoncms\api\generator;
8
9
use samsonphp\generator\Generator;
10
11
/**
12
 * SamsonCMS application classes generator.
13
 *
14
 * @package samsoncms\application\generator
15
 */
16
class Application extends \samsoncms\api\generator\Generic
17
{
18
    /** Default control class name */
19
    const DEFAULT_GENERIC_CONTROL_TYPE = 'Control';
20
21
    /** Path to index view of main page */
22
    const MAIN_INDEX_VIEW = 'www/main_application/index';
23
24
    /** Path to item view of main page */
25
    const MAIN_ITEM_VIEW = 'www/main_application/row';
26
27
    /**
28
     * Query constructor.
29
     *
30
     * @param Generator $generator
31
     * @param           $metadata
32
     */
33
    public function __construct(Generator $generator, $metadata)
34
    {
35
        parent::__construct($generator, $metadata);
36
37
        $this->className .= 'Application';
38
    }
39
40
    /**
41
     * Class definition generation part.
42
     *
43
     * @param \samsoncms\api\generator\metadata\Application $metadata Entity metadata
44
     */
45
    protected function createDefinition($metadata)
46
    {
47
        $this->generator
48
            ->multiComment(array('Class application for "'.$metadata->name.'"'))
49
            ->defClass($this->className, '\\'.\samsoncms\app\material\Application::class)
50
        ;
51
    }
52
53
    /**
54
     * Class fields generation part.
55
     *
56
     * @param \samsoncms\api\generator\metadata\Application $metadata Entity metadata
57
     */
58
    protected function createFields($metadata)
59
    {
60
        $this->generator
61
            ->commentVar('string', 'Application name')
62
            ->defVar('public $name', $metadata->name)
63
            ->commentVar('string', 'Application description')
64
            ->defVar('public $description', $metadata->description)
65
            ->commentVar('string', 'Identifier')
66
            ->defVar('protected $id', $metadata->entity)
67
            ->commentVar('string', 'Icon class')
68
            ->defVar('public $icon', $metadata->iconApplication)
69
            ->commentVar('bool', 'Flag for hiding Application icon in main menu')
70
            ->defClassVar('$hide', 'public', !$metadata->showApplication ? 1 : 0)
71
            ->commentVar('string', 'Path to rendering index view in main page')
72
            ->defClassVar('$mainIndexView', 'public', self::MAIN_INDEX_VIEW)
73
            ->commentVar('string', 'Path to rendering item view in main page')
74
            ->defClassVar('$mainItemView', 'public', self::MAIN_ITEM_VIEW)
75
            ->commentVar('array', 'Collection of structures related to entity')
76
            ->defClassVar('$navigation', 'public static', (int)$metadata->identifier)
77
            ->commentVar('array', 'All structures which have to have material at creation')
78
            ->defClassVar('$structures', 'public static', array_merge(array($metadata->entityID), $metadata->childNavigationIDs))
79
            ->commentVar('string', 'Collection class name for rendering entities collection')
80
            ->defClassVar('$collectionClass', 'protected', $metadata->entityClassName.'Collection')
81
        ;
82
    }
83
84
    /**
85
     * Class constructor generation part.
86
     *
87
     * @param \samsoncms\api\generator\metadata\Virtual $metadata Entity metadata
88
     */
89
    protected function createConstructor($metadata)
90
    {
91
        $constructorCode = <<<'EOD'
92
    /**
93
     * Render materials list with pager
94
     *
95
     * @param string $navigationId Structure identifier
96
     * @param string $search Keywords to filter table
97
     * @param int $page Current table page
98
     * @return array Asynchronous response containing status and materials list with pager on success
99
     * or just status on asynchronous controller failure
100
     */
101
    public function __async_collection($navigationId = '0', $search = '', $page = 1)
102
    {
103
        return parent::__async_collection(self::$navigation, $search, $page);
104
    }
105
EOD;
106
107
        $this->generator->text($constructorCode);
108
    }
109
110
    public function createSubMenuView(Metadata $metadata, $namespace = __NAMESPACE__)
0 ignored issues
show
Unused Code introduced by
The parameter $metadata 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...
Unused Code introduced by
The parameter $namespace 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...
111
    {
112
113
        $code = <<<'EOD'
114
115
    <li>
116
        <a class="sub_menu_a <?php if(isv('all_materials')):?>active<?php endif?>" href="<?php module_url()?>">
117
            <i class="icon2 icon2-list"></i>
118
            <?=$appName?>
119
        </a>
120
    </li>
121
    <li>
122
        <a class="sub_menu_a <?php if(isv('new_material')):?>active<?php endif?>" href="<?php module_url('new');?>">
123
            <i class="icon2 icon2-plus"></i> <?php t('Add'); ?> <?=lcfirst($appName);?> <?php t('item')?>
124
        </a>
125
    </li>
126
127
EOD;
128
129
        return str_replace('{{fields}}', implode(',', array_merge(array(), array("\n\t\t\t" . 'new Control()'. "\n\t\t"))), $code);
130
    }
131
132
    /**
133
     * Render method for rendering view of entity on main page
134
     * @param $entityName
135
     * @return string
136
     */
137
    public function renderViewsOnMainPage($entityName)
138
    {
139
140
        $code = <<<'EOD'
141
142
    /** Output for main page1 */
143
    public function main()
144
    {
145
        // Get module id
146
        $moduleId = $this->id;
147
148
        // Get module description
149
        $navName = $this->description;
150
151
        // Get path to views
152
        $mainIndexView = $this->mainIndexView;
153
        $mainItemView = $this->mainItemView;
154
155
        // Return material block HTML on main page
156
        /*return (new \samsoncms\api\generated\{{collection_name}}Collection($this))
157
            // Render index
158
            ->indexView(function($html, $renderer) use ($navName, $mainIndexView) {
159
                return $renderer->view($mainIndexView)
160
                    ->set($html, \samsoncms\api\renderable\Collection::ITEMS_VIEW_VARIABLE)
161
                    ->navName($navName)
162
                    ->output();
163
            })
164
            // Render item
165
            ->itemView(function($item, $renderer) use ($moduleId, $mainItemView) {
166
                return $renderer->view($mainItemView)
167
                    ->set($item, \samsoncms\api\renderable\Collection::ITEM_VIEW_VARIABLE)
168
                    //->user(m('social')->user())
169
                    ->moduleId($moduleId)
170
                    ->output();
171
            })
172
            ->output();*/
173
    }
174
EOD;
175
176
        return str_replace(array('{{collection_name}}'), array($entityName), $code);
177
    }
178
}
179
//[PHPCOMPRESSOR(remove,end)]
180