Documentation/Index/MigratePHPExcelToPhpSpreadsheet: AdditionalAnalyticsImportDialog.inc

Plik AdditionalAnalyticsImportDialog.inc, 10.1 KB (dodany przez TS, 3 years temu)
xx
Line 
1<?php
2require_once(MOD_PATH.'Dictionaries/VatNote/services/AdditionalAnalyticsImportService.inc');
3
4/**
5 * AdditionalAnalyticsImportDialog
6 *
7 * @author Tomasz Świenty
8 * @version 2.0
9 * @copyright Copyright (c) eDokumenty
10 */
11class AdditionalAnalyticsImportDialog extends DialogForm {
12
13
14    /**
15     * @var string[]
16     */
17    protected static $cMethods = array(
18        'import',
19    );
20
21
22    /**
23     * @var int|string
24     */
25    private $HWND;
26
27
28    /**
29     * @var SimpleUploader
30     */
31    private $importFile;
32
33
34    /**
35     * AdditionalAnalyticsImportDialog constructor.
36     * @param $name
37     * @param null $caption
38     * @param int $dstyle
39     */
40    public function __construct($name, $caption = NULL, $dstyle = BS_DIALOG) {
41       
42        parent::__construct($name, Translator::translate('Import dodatkowych analityk z szablonu'), DS_POPUP);
43        $this->path = Application::makeRelativePath(__FILE__);
44        $this->HWND = $this->createHWND();
45
46        $this->width = '470px';
47        $this->height = '210px';
48
49        if ($this->isOpened() AND !$this->noNeedForCreate() AND !Request::hasMethodCall($this->HWND, array_keys(self::$cMethods))) {
50            $this->create();
51        }
52       
53    }
54
55
56    /**
57     * @return string[]
58     */
59    public function getCallableMethods(): array {
60
61        return self::$cMethods;
62
63    }
64
65
66    /**
67     * @return bool
68     */
69    public function import(): bool {
70
71        $sData = $this->getSData();
72
73        $importFileData = $sData['importFile'] ?? NULL;
74
75        $progressBar = new ProgressBar($this->name.'PB');
76        $progressBar->init();
77        $progressBar->updateEvery(1000);
78
79        $db = PgManager::getInstance();
80        $counter = 0;
81
82        ErrorHandler::tryBegin();
83        try {
84            AdditionalAnalyticsImportService::checkFileExtension($importFileData[1]);
85            $counter = AdditionalAnalyticsImportService::importFromFile($sData['file'], $sData, $progressBar);
86        } catch (Exception $exception) {
87            $db->rollback();
88
89            (new WidgetException($this->name.'file', $exception->getMessage()))->show();
90        }
91        ErrorHandler::tryEnd();
92
93        $progressBar->close();
94
95        if (!isset($exception)) {
96            if ($counter) {
97                JScript::sweetAlert(sprintf(Translator::translate('Zaimportowano dodatkowych analityk: %d'), $counter), ['type' => 'success']);
98            }
99
100            $this->jsClose(TRUE, FALSE);
101        }
102
103        return TRUE;
104
105    }
106
107
108    /**
109     * @param array $params
110     * @param array $data
111     * @throws CustomErrorException
112     * @throws UserRightsException
113     */
114    public function open($params = [], &$data = []) {
115
116        if (!UserRights::checkSysAcc('bswfms.settings.level1')) {
117            throw new UserRightsException(NULL, 'bswfms.settings.level1');
118        }
119
120        $pData = JSON::toArray($params);
121        if (!isset($pData['adantp'])) {
122            throw new CustomErrorException(Translator::translate('Brak klucza adantp - typ dodatkowej analityki'));
123        }
124
125        parent::open($params, $data);
126
127        $this->setSData($data);
128        $this->create();
129       
130    }
131
132
133    /**
134     * @return bool
135     * @throws WidgetException
136     */
137    public function save(): bool {
138
139        if (!$this->isOpened()) {
140            return FALSE;
141        }
142
143        $sData = $this->getSData();
144        if (!isset($sData['adantp'])) {
145            throw new CustomErrorException(Translator::translate('Brak klucza adantp - typ dodatkowej analityki'));
146        }
147
148        $wData = $this->getWData();
149        $importFileData = $this->importFile->getUploadedFile();
150
151        if (!is_array($importFileData) OR !$importFileData OR !array_key_exists('file', $wData) OR bs_empty_str($wData['file'])) {
152            throw new WidgetException($this->name.'file', Translator::translate('Wskaż plik do importu'));
153        }
154
155        $this->setSData(array_merge(['importFile' => $importFileData], $wData));
156
157        $progressBar = new ProgressBar($this->name.'PB');
158
159        JScript::registerOnLoad('
160            App.'.$progressBar->getName().'showProgressBar = function() {               
161                var box = $(\''.$this->name.'progHandlerBox\');
162                box.style.left = \'0px\';
163
164                Tooltip.hide();
165                BalloonHint.hide();
166                ProgressBar.show(\''.$progressBar->getName().'\', unescape(\''.Translator::translate('Import dodatkowych analityk').'\'), 100, function(text){return App.'.$progressBar->getName().'handle(text);}, false, {interval:1200, retries:30, width:'.((int)$this->width + 6).', height:96, inline:box});
167            }
168
169            App.'.$progressBar->getName().'handle = function(text) {
170                if (text == \'\') {
171                    return false;
172                }
173
174                var ss = text.split(\':\');
175                $(\''.$progressBar->getName().'_dlg\').lastChild.firstChild.innerHTML = \''.Translator::translate('Import dodatkowych analityk').'<br><br>\';
176                $(\''.$progressBar->getName().'_txt\').innerHTML = ss[1]+\' / \'+ss[0] + \' %\';
177                return ss.join(\':\');
178            }
179        ');
180
181        JScript::addAfter('
182            Dialog.disable(\''.$this->name.'\');
183       
184            App.'.$progressBar->getName().'showProgressBar();
185            $A1(\''.$this->HWND.'\', \''.$this->name.'\', '.(int)array_search('import', self::$cMethods).', null, \'\'$(\\\''.$this->name.'progHandlerBox\\\').style.left = \\\'-100%\\\'; Dialog.enable(\\\''.$this->name.'\\\');\'\', 0, true);');
186
187        return TRUE;
188
189    }
190
191
192    /**
193     * @return bool
194     */
195    protected function create(): bool {
196       
197        if (!$this->isOpened()) {
198            return FALSE;
199        }
200
201        $composer = new DialogComposer($this);
202        $composer->addModernHeader(Translator::translate('Wybierz plik, który zawiera listę dodatkowych analityk'));
203
204        $this->importFile = new SimpleUploader($this->name.'file');
205        $this->importFile->bindDropZoneTo = $this->name.'rightPanelInner';
206        $this->importFile->dropZoneStyle = 'background-color: white;';
207        $this->importFile->position = 'absolute';
208        $label = $this->importFile->setLabel(Translator::translate('Wybierz plik do importu'));
209        $label->setRequired(TRUE);
210        $this->importFile->width = '';
211
212        $templateDownloadJS = '';
213        FileManager::getJSForFileDownloadByURI('./share/import/templates/additional_analytics.xlsx', $templateDownloadJS, 'additional_analytics.xlsx');
214
215        $infoPanel = new Panel($this->name.'info');
216        $infoPanel->class = 'b-s-b';
217        $infoPanel->height = '140px';
218        $infoPanel->left = '0px';
219        $infoPanel->width = '100%';
220        $infoPanel->display = 'flex';
221        $infoPanel->top = 'auto';
222        $infoPanel->bottom = '0px';
223        $infoPanel->style = 'border: 1px solid rgba(0,0,0,.04); border-width: 1px 0 0 0; background-color: #f1f1f1; display: flex; flex-direction: row; align-items: center;';
224        $infoPanel->addHtml('
225                    <div style="flex-shrink: 0; align-self: center; height: 100%; display: flex; width: 80px; align-items: center; justify-content: center;">
226                        <i class="eic eic-microsoft-excel-filled eic-ef-xls" style="font-size: 48px; align-items: baseline; position: relative;"></i>
227                    </div>
228                    <div style="padding: 0 10px; color: #002627 !important;">
229                        <div class="rbos-font-block-header" style="line-height: 22px; margin-bottom: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">Szablon importu</div>
230                        <div class="rbos-font-list-header" style="line-height: 16px;">
231                            <div style="font-weight: normal !important; padding-left: 24px; position: relative;"><div style="width: 16px; height: 16px; display: inline-block; color: white; background-color: #2980b9; text-align: center; line-height: 16px; position: absolute; left: 0;">1</div>Pobierz szablon importu'.($templateDownloadJS ? '<span class="ActionLink" style="line-height: 16px; font-weight: normal !important; margin-left: 10px;" onclick="'.$templateDownloadJS.'">Pobierz</span>' : '').'</div>
232                            <div style="margin-top: 8px; font-weight: normal !important; padding-left: 24px; position: relative;"><div style="width: 16px; height: 16px; display: inline-block; color: white; background-color: #2980b9; text-align: center; line-height: 16px; position: absolute; left: 0;">2</div>Przygotuj Twój plik zgodnie z pobranym szablonem</div>
233                            <div style="margin-top: 8px; font-weight: normal !important; padding-left: 24px; position: relative;"><div style="width: 16px; height: 16px; display: inline-block; color: white; background-color: #2980b9; text-align: center; line-height: 16px; position: absolute; left: 0;">3</div>Wybierz przygotowany plik do importu i kliknij przycisk Importuj</div>                                                                                                                                                               
234                        </div>
235                     </div>                   
236                    ');
237
238        $progHandlerBox = new Panel($this->name.'progHandlerBox');
239        $progHandlerBox->position = 'absolute';
240        $progHandlerBox->bottom = '51px';
241        $progHandlerBox->height = '139px';
242        $progHandlerBox->left = '-100%';
243        $progHandlerBox->class = 'b-s-b';
244        $progHandlerBox->width = '100%';
245        $progHandlerBox->style = 'transition: all 200ms; flex-direction: row; align-items: center; background-color: #f1f1f1;';
246        $progHandlerBox->display = 'flex';
247
248        $composer->addNext($this->importFile, 'file');
249        $composer->addNext($infoPanel);
250
251        $composer->addModernButtonsPanel(array('bSave', 'bCancel'));
252
253        $this->height = '336px';
254
255        $this->bSave->caption = Translator::translate('Importuj');
256        $this->bSave->title = Translator::translate('Importuj z pliku do systemu');
257        $this->onbSaveClick = 'if (!ROS.RG.canInvoke(this)) {return false;} asyncLibrary.doAsyncWait = false; ROS.RG.Request = ';
258
259        $this->add($composer);
260        $this->add($progHandlerBox);
261
262        return TRUE;
263
264    }
265
266}