File size: 9,993 Bytes
07c3cdd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php

namespace ProcessMaker\Plugins\Adapters;

use G;
use PluginsRegistry;
use PMPluginRegistry;

/**
 * Adapts the plugin singleton file and converts it to table
 * Class PluginAdapter
 * @package ProcessMaker\Plugins\Adapters
 */
class PluginAdapter
{
    /**
     * This array is a map to change the old key to the new, corresponding of each class
     * @var array
     */
    private $keyNames = [
        'sNamespace' => 'Namespace',
        //MenuDetail
        'sMenuId' => 'MenuId',
        'sFilename' => 'Filename',
        //FolderDetail
        'sFolderId' => 'FolderId',
        'sFolderName' => 'FolderName',
        //TriggerDetail
        'sTriggerId' => 'TriggerId',
        'sTriggerName' => 'TriggerName',
        //RedirectDetail
        'sRoleCode' => 'RoleCode',
        'sPathMethod' => 'PathMethod',
        //StepDetail
        'sStepId' => 'StepId',
        'sStepName' => 'StepName',
        'sStepTitle' => 'StepTitle',
        'sSetupStepPage' => 'SetupStepPage',
        //CssFile->_aCSSStyleSheets
        'sCssFile' => 'CssFile',
        //ToolbarDetail->_aToolbarFiles
        'sToolbarId' => 'ToolbarId',
        //CaseSchedulerPlugin->_aCaseSchedulerPlugin
        'sActionId' => 'ActionId',
        'sActionForm' => 'ActionForm',
        'sActionSave' => 'ActionSave',
        'sActionExecute' => 'ActionExecute',
        'sActionGetFields' => 'ActionGetFields',
        //TaskExtendedProperty->_aTaskExtendedProperties
        //DashboardPage->_aDashboardPages
        'sPage' => 'Page',
        'sName' => 'Name',
        'sIcon' => 'Icon',
        //CronFile->_aCronFiles
        'namespace' => 'Namespace',
        'cronFile' => 'CronFile',
        //ImportCallBack->_aImportProcessCallbackFile
        //OpenReassignCallback->_aOpenReassignCallback
        'callBackFile' => 'CallBackFile',
        //JsFile->_aJavascripts
        'sCoreJsFile' => 'CoreJsFile',
        'pluginJsFile' => 'PluginJsFile',
    ];

    /**
     * Map the fields of the table with their type
     * @var array $attributes
     */
    private $attributes = [
        'sNamespace' => ['name' => 'PLUGIN_NAMESPACE', 'type' => 'string'],
        'sDescription' => ['name' => 'PLUGIN_DESCRIPTION', 'type' => 'string'],
        'sClassName' => ['name' => 'PLUGIN_CLASS_NAME', 'type' => 'string'],
        'sFriendlyName' => ['name' => 'PLUGIN_FRIENDLY_NAME', 'type' => 'string'],
        'sFilename' => ['name' => 'PLUGIN_FILE', 'type' => 'string'],
        'sPluginFolder' => ['name' => 'PLUGIN_FOLDER', 'type' => 'string'],
        'sSetupPage' => ['name' => 'PLUGIN_SETUP_PAGE', 'type' => 'string'],
        'aWorkspaces' => ['name' => 'PLUGIN_WORKSPACES', 'type' => 'array'],
        'sCompanyLogo' => ['name' => 'PLUGIN_COMPANY_LOGO', 'type' => 'string'],
        'iVersion' => ['name' => 'PLUGIN_VERSION', 'type' => 'int'],
        'enabled' => ['name' => 'PLUGIN_ENABLE', 'type' => 'bool'],
        'bPrivate' => ['name' => 'PLUGIN_PRIVATE', 'type' => 'bool'],
        '_aMenus' => ['name' => 'PLUGIN_MENUS', 'type' => 'array'],
        '_aFolders' => ['name' => 'PLUGIN_FOLDERS', 'type' => 'array'],
        '_aTriggers' => ['name' => 'PLUGIN_TRIGGERS', 'type' => 'array'],
        '_aPmFunctions' => ['name' => 'PLUGIN_PM_FUNCTIONS', 'type' => 'array'],
        '_aRedirectLogin' => ['name' => 'PLUGIN_REDIRECT_LOGIN', 'type' => 'array'],
        '_aSteps' => ['name' => 'PLUGIN_STEPS', 'type' => 'array'],
        '_aCSSStyleSheets' => ['name' => 'PLUGIN_CSS', 'type' => 'array'],
        '_aCss' => ['name' => 'PLUGIN_CSS', 'type' => 'array'],
        '_aJavascripts' => ['name' => 'PLUGIN_JS', 'type' => 'array'],
        '_aJs' => ['name' => 'PLUGIN_JS', 'type' => 'array'],
        '_restServices' => ['name' => 'PLUGIN_REST_SERVICE', 'type' => 'array'],
        '_aCronFiles' => ['name' => 'PLUGIN_CRON_FILES', 'type' => 'array'],
        '_aTaskExtendedProperties' => ['name' => 'PLUGIN_TASK_EXTENDED_PROPERTIES', 'type' => 'array'],
    ];

    /**
     * Data of the plugin singleton in array structure
     * @var array $PMPluginRegistry
     */
    private $PMPluginRegistry;

    /**
     * Returns the structure of the table in attributes
     * @return array
     */
    public function getAttributes()
    {
        return $this->attributes;
    }

    /**
     * Migrate the singleton plugin to tables
     * @param PMPluginRegistry $PMPluginsSingleton
     */
    public function migrate($PMPluginsSingleton)
    {
        $this->PMPluginRegistry = G::json_decode(G::json_encode($PMPluginsSingleton->getAttributes()), true);
        $this->parserNameKey();
        foreach ($this->PMPluginRegistry['_aPluginDetails'] as $nameSpace => $value) {
            $this->saveInTable($nameSpace, $this->PMPluginRegistry);
        }
    }

    /**
     * Change to new key property $keyNames
     * @return mixed
     */
    private function parserNameKey()
    {
        $aPluginDetails = $this->PMPluginRegistry['_aPluginDetails'];
        unset($this->PMPluginRegistry['_aPluginDetails']);
        $aPlugins = $this->PMPluginRegistry['_aPlugins'];
        unset($this->PMPluginRegistry['_aPlugins']);
        foreach ($this->PMPluginRegistry as $propertyKey => $propertyValue) {
            foreach ($propertyValue as $attKey => $attributes) {
                if (is_array($attributes)) {
                    foreach ($attributes as $index => $attribute) {
                        if (array_key_exists($index, $this->keyNames)) {
                            $newKey = $this->keyNames[$index];
                            $value = $this->PMPluginRegistry[$propertyKey][$attKey][$index];
                            $this->PMPluginRegistry[$propertyKey][$attKey][$newKey] = $value;
                            unset($this->PMPluginRegistry[$propertyKey][$attKey][$index]);
                        }
                    }
                }
            }
        }
        $this->PMPluginRegistry['_aPluginDetails'] = $aPluginDetails;
        $this->PMPluginRegistry['_aPlugins'] = $aPlugins;
        return $this->PMPluginRegistry;
    }

    /**
     * Save plugin in table PLUGINS_REGISTRY
     * @param string $Namespace Name of plugin
     * @param array $PMPluginRegistry
     */
    public function saveInTable($Namespace, $PMPluginRegistry)
    {
        $newStructurePlugin = $this->getAllAttributes($Namespace, $PMPluginRegistry);
        $plugin = $this->convertFieldTable($newStructurePlugin);
        if ($plugin['PLUGIN_NAMESPACE'] && $plugin['PLUGIN_CLASS_NAME'] && $plugin['PLUGIN_FILE']) {
            $fieldPlugin = PluginsRegistry::loadOrCreateIfNotExists(md5($plugin['PLUGIN_NAMESPACE']), $plugin);
            PluginsRegistry::update($fieldPlugin);
        }
    }

    /**
     * Extracts all attributes corresponding to a plugin
     * @param string $Namespace Name Plugin
     * @param array $PMPluginRegistry
     * @return array
     */
    private function getAllAttributes($Namespace, $PMPluginRegistry)
    {
        $PluginDetails = $PMPluginRegistry['_aPluginDetails'][$Namespace];
        unset($PMPluginRegistry['_aPluginDetails']);
        $Plugin = isset($PMPluginRegistry['_aPlugins'][$Namespace]) ? $PMPluginRegistry['_aPlugins'][$Namespace] : [];
        unset($PMPluginRegistry['_aPlugins']);
        $newStructurePlugin = array_merge($PluginDetails, $Plugin);
        foreach ($PMPluginRegistry as $propertyName => $propertyValue) {
            foreach ($propertyValue as $key => $plugin) {
                if (is_array($plugin) &&
                    (array_key_exists('Namespace', $plugin) && $plugin['Namespace'] == $Namespace)
                ) {
                    $newStructurePlugin[$propertyName][] = $plugin;
                } elseif (is_array($plugin) &&
                    array_key_exists('pluginName', $plugin) &&
                    $plugin['pluginName'] == $Namespace
                ) {
                    $newStructurePlugin[$propertyName][] = $plugin;
                } elseif (!is_int($key) && $key == $Namespace) {
                    $newStructurePlugin[$propertyName][$key] = $plugin;
                } elseif (is_string($plugin) && $plugin == $Namespace) {
                    $newStructurePlugin[$propertyName][] = $plugin;
                }
            }
        }
        return $newStructurePlugin;
    }

    /**
     * Convert de attributes to field of table PLUGINS_REGISTRY
     * @param array $plugin
     * @return array
     */
    private function convertFieldTable($plugin)
    {
        $fields = [];
        $extraAttributes = array_diff_key($plugin, $this->attributes);
        $fieldsInTable = array_intersect_key($plugin, $this->attributes);
        foreach ($this->attributes as $name => $property) {
            switch ($property['type']) {
                case 'string':
                    $valueField = array_key_exists($name, $fieldsInTable) ? $fieldsInTable[$name] : '';
                    break;
                case 'array':
                    $valueField = (array_key_exists($name, $fieldsInTable) && $fieldsInTable[$name]) ?
                        $fieldsInTable[$name] :
                        [];
                    $valueField = G::json_encode($valueField);
                    break;
                case 'int':
                    $valueField = array_key_exists($name, $fieldsInTable) ? $fieldsInTable[$name] : 0;
                    break;
                case 'bool':
                    $valueField = array_key_exists($name, $fieldsInTable) ?
                        ($fieldsInTable[$name] ? true : false) :
                        false;
                    break;
                default:
                    $valueField = array_key_exists($name, $fieldsInTable) ?
                        $fieldsInTable[$name] :
                        [];
                    break;
            }
            $fields[$property['name']] = $valueField;
        }
        $fields['PLUGIN_ATTRIBUTES'] = G::json_encode($extraAttributes);
        return $fields;
    }
}