Skip to content

Export, import, lengthy process

Lubomir Andrisek edited this page Jul 11, 2019 · 1 revision

final class MyComponent extends \Masala\IBuilderFactory implements IMyComponentFactory {
    public function __construct(string $wwwDir, string $folder)
        $this->folder = $wwwDir . $folder;
        $this->path = $folder;
    }
    public function handleProcess(): void {
        $this->state();
        if(1 == $this->state->_paginator->current) {
            ...application logic for first page...
        }
        foreach($this->database->query($this->query, ...$this->arguments)->fetchAll() as $key => $row) {
            ...application logic...
            $this->state->rows->$key = $row;
        }
        $this->state->_paginator->current++;
        if($this->state->_paginator->current == $this->state->_paginator->last) {
            ...application logic for last page...
        }
        $this->presenter->sendResponse(
            new \Nette\Application\Responses\JsonResponse($this->state));
    }
    public function handleImport(): void {
        $this->state();
        ...application logic...
        if($this->state->_paginator->current == $this->state->_paginator->last) {
            ...application logic for last page...
            $this->state->upload = [];
        }
        $this->state->_paginator->current++;
        $this->presenter->sendResponse(new \Nette\Application\Responses\JsonResponse($this->state));
    }
    public function handleUpload(): void {
        file_put_contents('path/to/file', file_get_contents('php://input'));
        $this->presenter->sendResponse(new \Nette\Application\Responses\JsonResponse(($this->state));
    }
    public function props(): array {
        parent::props();
        $this->prop('download', ['label' => 'myLabel'])
        ->prop('export', ['label' => 'myLabel','link' => $this->link('export'),
        ->prop('import', ['label' => 'myLabel','link' => $this->link('import'),
        ->prop('upload', ['label' => 'myLabel', 'link' => $this->link('upload')])
        return $this->props;
    }
    protected function file(): string {
        return 'MyFile';
    }
    protected function row(stdClass $row): array {
        ...application logic for export row...
        return [$this->translatorRepository->translate('myHeader') => $row->myColumn];
    }
}