Documentation/Index/MigratePHPExcelToPhpSpreadsheet: ProjectsImportDialog.inc

Plik ProjectsImportDialog.inc, 13.9 KB (dodany przez TS, 3 years temu)
xx
Line 
1<?php
2require_once(MOD_PATH.'Dictionaries/Projects/services/ProjectsImportService.inc');
3
4/**
5 * ProjectsImportDialog
6 *
7 * @author Tomasz Świenty
8 * @version 2.0
9 * @copyright Copyright (c) eDokumenty
10 */
11class ProjectsImportDialog extends DialogForm {
12
13
14    /**
15     * @var string[]
16     */
17    protected static $cMethods = array(
18        'import',
19    );
20
21
22    /**
23     * @var SimpleUploader
24     */
25    private $importFile;
26
27
28    /**
29     * @var int|string
30     */
31    private $HWND;
32
33
34    /**
35     * ProjectsImportDialog 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 projektów z szablonu'), DS_POPUP);
43
44        $this->path = Application::makeRelativePath(__FILE__);
45        $this->HWND = $this->createHWND();
46
47        $this->width = '470px';
48        $this->height = '210px';
49
50        if ($this->isOpened() AND !$this->noNeedForCreate() AND !Request::hasMethodCall($this->HWND, array_keys(self::$cMethods))) {
51            $this->create();
52        }
53       
54    }
55
56
57    /**
58     * @return string[]
59     */
60    public function getCallableMethods(): array {
61
62        return self::$cMethods;
63
64    }
65
66
67    /**
68     * @return bool
69     */
70    public function import(): bool {
71
72        $sData = $this->getSData();
73
74        $importFileData = $sData['importFile'] ?? NULL;
75
76        $options = [
77            'ifFound' => ($sData['ifFound'] ?? NULL),
78            'ifFoundAndDeleted' => ($sData['ifFoundAndDeleted'] ?? NULL)
79        ];
80
81        $progressBar = new ProgressBar($this->name.'PB');
82        $progressBar->init();
83        $progressBar->updateEvery(1000);
84
85        $db = PgManager::getInstance();
86        $counter = 0;
87
88        ErrorHandler::tryBegin();
89        try {
90            ProjectsImportService::checkFileExtension($importFileData[1]);
91            $counter = ProjectsImportService::importFromFile($sData['file'], $options, $progressBar);
92        } catch (Exception $exception) {
93            $db->rollback();
94
95            (new WidgetException($this->name.'file', $exception->getMessage()))->show();
96        }
97        ErrorHandler::tryEnd();
98
99        $progressBar->close();
100
101        if (!isset($exception)) {
102            if ($counter) {
103                JScript::sweetAlert(sprintf(Translator::translate('Zaimportowano projektów: %d'), $counter), ['type' => 'success']);
104            }
105
106            $this->jsClose(TRUE, FALSE);
107        }
108
109        return TRUE;
110
111    }
112
113
114    /**
115     * @param array $params
116     * @param array $data
117     * @return bool|void
118     * @throws UserRightsException
119     */
120    public function open($params = [], &$data = []) {
121
122        require_once('./classes/FieldTools/RightCheckers/ControlPanelRightsChecker.inc');
123        $rightsChecker = new ControlPanelRightsChecker();
124        if (!UserRights::checkSysAcc('bswfms.settings.level2') AND !$rightsChecker->isPermitted('PROJECTS', FieldsAccessLevel::READ, TRUE)) {
125            throw new UserRightsException(NULL, 'bswfms.settings.level2');
126        }
127
128        require_once('./classes/Calendars/services/CalendarsDefService.inc');
129        $calendar = (new CalendarsDefService)->getDefaultCalendar();
130        if (!$calendar) {
131            if (!UserRights::checkSysAcc('bswfms.settings.level2')) {
132                throw new UserRightsException(NULL, 'bswfms.settings.level2');
133            }
134
135            JScript::registerOnLoad('App.openDialogEx(\''.App::registerDialog(App::getShortName('Calendars'), 'Calendars', './classes/Calendars/forms/Calendars.inc').'\', null, null);');
136            JScript::info(Translator::translate('Brak domyślnego kalendarza'));
137
138            return FALSE;
139        }
140
141        parent::open($params, $data);
142        $this->setData($data);
143        $this->create();
144       
145    }
146
147
148    /**
149     * @return bool
150     * @throws WidgetException
151     */
152    public function save(): bool {
153
154        if (!$this->isOpened()) {
155            return FALSE;
156        }
157
158        $wData = $this->getWData();
159        $importFileData = $this->importFile->getUploadedFile();
160
161        if (!is_array($importFileData) OR !$importFileData OR !array_key_exists('file', $wData) OR bs_empty_str($wData['file'])) {
162            throw new WidgetException($this->name.'file_l', Translator::translate('Wskaż plik do importu'), 'NOTICE', ['highlightFieldsWithLabel' => [$this->name.'file'], 'highlightDelay' => 5000]);
163        }
164
165        $this->setSData(array_merge(['importFile' => $importFileData], $wData));
166
167        $progressBar = new ProgressBar($this->name.'PB');
168
169        JScript::add('
170            App.'.$progressBar->getName().'showProgressBar = function() {               
171                var box = $(\''.$this->name.'progHandlerBox\');
172                box.style.left = \'0px\';
173
174                Tooltip.hide();
175                BalloonHint.hide();
176                ProgressBar.show(\''.$progressBar->getName().'\', unescape(\''.Translator::translate('Import projektów').'\'), 100, function(text){return App.'.$progressBar->getName().'handle(text);}, false, {interval:1200, retries:30, width:'.((int)$this->width + 6).', height:96, inline:box});
177            }
178
179            App.'.$progressBar->getName().'handle = function(text) {
180                if (text == \'\') {
181                    return false;
182                }
183
184                var ss = text.split(\':\');
185                $(\''.$progressBar->getName().'_dlg\').lastChild.firstChild.innerHTML = \''.Translator::translate('Import projektów').'<br><br>\';
186                $(\''.$progressBar->getName().'_txt\').innerHTML = ss[1]+\' / \'+ss[0] + \' %\';
187                return ss.join(\':\');
188            }
189        ');
190
191        JScript::addAfter('
192            Dialog.disable(\''.$this->name.'\');
193           
194            App.'.$progressBar->getName().'showProgressBar();
195            $A1(\''.$this->HWND.'\', \''.$this->name.'\', '.(int)array_search('import', self::$cMethods).', null, \'$(\\\''.$this->name.'progHandlerBox\\\').style.left = \\\'-100%\\\'; Dialog.enable(\\\''.$this->name.'\\\');\', 0, false);
196        ');
197
198        return TRUE;
199
200    }
201
202
203    /**
204     * @return bool
205     */
206    protected function create(): bool {
207       
208        if (!$this->isOpened()) {
209            return FALSE;
210        }
211       
212        $composer = new DialogComposer($this);
213        $composer->addModernHeader(Translator::translate('Wybierz plik, który zawiera listę projektów.'));
214       
215        $this->importFile = new SimpleUploader($this->name.'file');
216        $this->importFile->bindDropZoneTo = $this->name.'rightPanelInner';
217        $this->importFile->dropZoneStyle = 'background-color: white;';
218        $this->importFile->position = 'absolute';
219        $this->importFile->width = '';
220
221        $label = $this->importFile->setLabel(Translator::translate('Wybierz plik do importu'));
222        $label->setRequired(TRUE);
223
224        $templateDownloadJS = '';
225        FileManager::getJSForFileDownloadByURI('./share/import/templates/projects.xlsx', $templateDownloadJS, 'projects.xlsx');
226
227        $optionsPanel = new FieldsSet($this->name.'optionsPanel', FieldsSet::STYLE4);
228        $optionsPanel->clearPositionString(TRUE);
229        $optionsPanel->setLabelText(Translator::translate('Opcje importu'));
230        $optionsPanel->class = 'b-s-b';
231        $optionsPanel->height = 'auto';
232        $optionsPanel->top = '80px';
233        $optionsPanel->width = 'calc(100% - '.(Dialog::SPACE_LEFT + Dialog::SPACE_RIGHT).'px)';
234
235        $labelIfFound = new Label($this->name.'labelIfFound');
236        $labelIfFound->top = (Dialog::SPACE_TOP + Dialog::SPACE1).'px';
237        $labelIfFound->text = Translator::translate('Jeśli projekt zostanie znaleziony:');
238        $labelIfFound->helpText = Translator::translate('Wykrywanie czy projekt istnieje w systemie następuje poprzez porównanie nazwy projektu oraz numeru (wystarczy, że wartość jednego z tych atrybutów zostanie znaleziona w bazie).');
239        $labelIfFound->width = 'calc(100% - 140px - '.(Dialog::SPACE_LEFT + Dialog::SPACE_RIGHT).'px)';
240
241        $optionsIfFound = new ExtSelect($this->name.'optionsIfFound');
242        $optionsIfFound->width = '170px';
243        $optionsIfFound->addItem(ProjectsImportService::IF_FOUND_OPTION_BREAK, Translator::translate('Przerwij cały import'));
244        $optionsIfFound->addItem(ProjectsImportService::IF_FOUND_OPTION_SKIP, Translator::translate('Pomiń projekt i kontynuuj import'));
245        $optionsIfFound->addItem(ProjectsImportService::IF_FOUND_OPTION_UPDATE, Translator::translate('Aktualizuj projekt'));
246        $optionsIfFound->onChange = 'if (this.value == \'update\') {$(\''.$this->name.'\').style.height = \'468px\';} else {$(\''.$this->name.'\').style.height = \'414px\';}';
247
248        $ifFoundAndDeletedPanel = new Panel($this->name.'ifFoundAndDeletedPanel');
249        $ifFoundAndDeletedPanel->height = '50px';
250        $ifFoundAndDeletedPanel->width = '100%';
251
252        $labelIfFoundAndDeleted = new Label($this->name.'labelIfFoundAndDeleted');
253        $labelIfFoundAndDeleted->text = Translator::translate('Jeśli znaleziony projekt jest usunięty:');
254        $labelIfFoundAndDeleted->width = 'calc(100% - 140px - '.(Dialog::SPACE_LEFT + Dialog::SPACE_RIGHT).'px)';
255
256        $optionsIfFoundAndDeleted = new ExtSelect($this->name.'optionsIfFoundAndDeleted');
257        $optionsIfFoundAndDeleted->width = '170px';
258        $optionsIfFoundAndDeleted->addItem(ProjectsImportService::IF_FOUND_AND_DELETED_OPTION_BREAK, Translator::translate('Przerwij cały import'));
259        $optionsIfFoundAndDeleted->addItem(ProjectsImportService::IF_FOUND_AND_DELETED_OPTION_SKIP, Translator::translate('Pomiń projekt i kontynuuj import'));
260        $optionsIfFoundAndDeleted->addItem(ProjectsImportService::IF_FOUND_AND_DELETED_OPTION_RESTORE, Translator::translate('Przywróć projekt i zaktualizuj'));
261
262        $ifFoundAndDeletedPanel->addNext($labelIfFoundAndDeleted, NULL, Widget::P_TO_LEFT_L);
263        $ifFoundAndDeletedPanel->addNext($optionsIfFoundAndDeleted, 'ifFoundAndDeleted', Widget::P_TO_RIGHT_R);
264
265        $optionsPanel->addNext($labelIfFound, NULL, Widget::P_TO_LEFT_L);
266        $optionsPanel->addNext($optionsIfFound, 'ifFound', Widget::P_TO_RIGHT_R);
267        $optionsPanel->addNext($ifFoundAndDeletedPanel, NULL, Widget::P_TO_LEFT_L);
268
269        $infoPanel = new Panel($this->name.'infoPanel');
270        $infoPanel->class = 'b-s-b';
271        $infoPanel->height = '140px';
272        $infoPanel->left = '0px';
273        $infoPanel->width = '100%';
274        $infoPanel->display = 'flex';
275        $infoPanel->top = 'auto';
276        $infoPanel->bottom = '0px';
277        $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;';
278        $infoPanel->addHtml('
279                    <div style="flex-shrink: 0; align-self: center; height: 100%; display: flex; width: 80px; align-items: center; justify-content: center;">
280                        <i class="eic eic-microsoft-excel-filled eic-ef-xls" style="font-size: 48px; align-items: baseline; position: relative;"></i>
281                    </div>
282                    <div style="padding: 0 10px; color: #002627 !important;">
283                        <div class="rbos-font-block-header" style="line-height: 22px; margin-bottom: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">Szablon importu</div>
284                        <div class="rbos-font-list-header" style="line-height: 16px;">
285                            <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>
286                            <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>
287                            <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>                                                                                                                                                               
288                        </div>
289                     </div>                   
290                    ');
291
292        $progHandlerBox = new Panel($this->name.'progHandlerBox');
293        $progHandlerBox->position = 'absolute';
294        $progHandlerBox->bottom = '51px';
295        $progHandlerBox->height = '139px';
296        $progHandlerBox->left = '-100%';
297        $progHandlerBox->class = 'b-s-b';
298        $progHandlerBox->width = '100%';
299        $progHandlerBox->style = 'transition: all 200ms; flex-direction: row; align-items: center; background-color: #f1f1f1;';
300        $progHandlerBox->display = 'flex';
301
302        $composer->addNext($this->importFile, 'file');
303        $composer->addNext($optionsPanel);
304        $composer->addNext($infoPanel);
305
306        $composer->addModernButtonsPanel(array('bSave', 'bCancel'));
307
308        $this->height = '460px';
309
310        $this->bSave->caption = Translator::translate('Importuj');
311        $this->bSave->title = Translator::translate('Importuj z pliku do systemu');
312        $this->onbSaveClick = 'if (!ROS.RG.canInvoke(this)) {return false;} asyncLibrary.doAsyncWait = false; ROS.RG.Request = ';
313
314        $this->add($composer);
315        $this->add($progHandlerBox);
316
317        return TRUE;
318
319    }
320
321}