From: soufiane Date: Fri, 16 Feb 2024 17:27:04 +0000 (+0100) Subject: wait #6717 @1:00 refacto, ajout du job X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=9823d6f48f762ca22173a879c759e7487db62fbf;p=fluidbook-toolbox.git wait #6717 @1:00 refacto, ajout du job --- diff --git a/app/Console/Commands/FluidbookSettingsExport.php b/app/Console/Commands/FluidbookSettingsExport.php index ff40449e0..672b56f4d 100644 --- a/app/Console/Commands/FluidbookSettingsExport.php +++ b/app/Console/Commands/FluidbookSettingsExport.php @@ -3,20 +3,6 @@ namespace App\Console\Commands; use App\Console\Commands\Base\ToolboxCommand; -use App\Models\FluidbookPublication; -use App\Models\User; -use App\Notifications\ToolboxNotification; -use Cubist\Util\ArrayUtil; -use Cubist\Util\Files\Files; -use PhpOffice\PhpSpreadsheet\Spreadsheet; -use PhpOffice\PhpSpreadsheet\Style\Alignment; -use PhpOffice\PhpSpreadsheet\Style\Fill; -use PhpOffice\PhpSpreadsheet\Style\NumberFormat; -use PhpOffice\PhpSpreadsheet\Style\Protection; -use PhpOffice\PhpSpreadsheet\Writer\Xlsx; -use Cubist\Backpack\Magic\Fields\Hidden; -use Cubist\Backpack\Magic\Fields\UnstoredField; -use PhpOffice\PhpSpreadsheet\Cell\Coordinate; class FluidbookSettingsExport extends ToolboxCommand { @@ -40,121 +26,6 @@ class FluidbookSettingsExport extends ToolboxCommand public function handle() { $userID = $this->getUser()->id; - $user = User::withoutGlobalScopes()->find($userID); //5908 - - // - $listID = ArrayUtil::parseRange($this->argument('id')); - $model = FluidbookPublication::whereIn('id',$listID); - - $all = $model->get()->toArray(); - $settings = $model->get()->map(function($i,$k) use($all){ - unset($all[$k]['settings']); - return array_merge($all[$k],json_decode($i->settings, true)); - })->toArray(); - - //$editable = ['type' => 'text', 'editable' => true]; - $noteditable = ['noteditable' => true, 'hidden' => false]; - $fields = [ - 'id' => $noteditable - ]; - - $instance = new FluidbookPublication(); - foreach ($instance->getFields() as $field) { - $name = $field->getName(); - /** @var $field Field */ - if($field instanceof UnstoredField || in_array($name,['deleted_at','videoPath'])) { - - }else { - $fields[$name] = [ - 'default' => $field->getAttribute('default'), - 'noteditable' => $field instanceof Hidden - ]; - } - } - - $keys = array_keys($fields); - - $excel = new Spreadsheet(); - $excel->getDefaultStyle() - ->getNumberFormat() - ->setFormatCode( - NumberFormat::FORMAT_TEXT - ); - $sheet = $excel->getActiveSheet(); - $sheetname = "Export"; - $sheet->setTitle($sheetname); - - $excel->getActiveSheet()->getProtection()->setSheet(true); - $excel->getDefaultStyle()->getProtection()->setLocked(false); - - $line = 1; - $columns = 0; - $c = 0; - - foreach ($keys as $key) { - $c++; - $columns = max($columns, $c); - $cell = $sheet->getCellByColumnAndRow($c, $line); - $cell->setValue($key); - $style = $sheet->getStyleByColumnAndRow($c, $line); - if($fields[$key]['noteditable']) { - $style->getProtection()->setLocked(Protection::PROTECTION_PROTECTED); - $style->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('dddddd'); - } - $style->getFont()->setBold(true); - $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); - } - - $maxColSize = []; - - for ($i = 0; $i < sizeof($settings); $i++) { - $c = 1; - $line++; - foreach ($keys as $k => $key) { - $j = $k+1; - $columns = max($columns, $c); - $value = $settings[$i][$key] ?? $fields[$key]['default'] ?? ""; - $value = is_array($value) ? json_encode($value) : $value; - $maxColSize[$j][] = strlen($value); - $sheet->getCellByColumnAndRow($c, $line)->setValue($value); - $style = $sheet->getStyleByColumnAndRow($c, $line); - if($fields[$key]['noteditable']) { - $style->getProtection()->setLocked(Protection::PROTECTION_PROTECTED); - $style->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('dddddd'); - } - $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); - $style->getAlignment()->setWrapText(true); - $c++; - } - } - - $j = 1; - for ($i = 0; $i <= $columns; $i++) { - if(array_key_exists($j, $maxColSize)) { - if (max($maxColSize[$j]) > 50) { - $sheet->getColumnDimensionByColumn($j)->setWidth(100); - } else { - $sheet->getColumnDimensionByColumn($j)->setAutoSize(true); - } - } else { - $sheet->getColumnDimensionByColumn($j)->setAutoSize(true); - } - $j++; - } - - $tmpfile = Files::tempnam() . '.xlsx'; - $writer = new Xlsx($excel); - $writer->save($tmpfile); - - $hash = explode('cubist',$tmpfile)[1]; - - $url = route('download_settings', ['file' => base64_encode($hash)]); - $subject = __("Export groupé des paramètres des fluidbooks prêt au téléchargement"); - $notification = ''; - $action = [ - __('Télécharger') => $url, - ]; - - $user->notify(new ToolboxNotification($subject, $notification, $action, true)); + dispatch_sync(new \App\Jobs\FluidbookSettingsExport($this->argument('id'), $userID)); } } diff --git a/app/Jobs/FluidbookSettingsExport.php b/app/Jobs/FluidbookSettingsExport.php new file mode 100644 index 000000000..ec1165fcf --- /dev/null +++ b/app/Jobs/FluidbookSettingsExport.php @@ -0,0 +1,148 @@ +listID = ArrayUtil::parseRange($ids); + $this->userID = $userID; + } + public function handle() + { + $user = User::withoutGlobalScopes()->find($this->userID); //5908 + + // + $model = FluidbookPublication::whereIn('id', $this->listID); + + $all = $model->get()->toArray(); + $settings = $model->get()->map(function ($i, $k) use ($all) { + unset($all[$k]['settings']); + return array_merge($all[$k], json_decode($i->settings, true)); + })->toArray(); + + //$editable = ['type' => 'text', 'editable' => true]; + $noteditable = ['noteditable' => true, 'hidden' => false]; + $fields = [ + 'id' => $noteditable + ]; + + $instance = new FluidbookPublication(); + foreach ($instance->getFields() as $field) { + $name = $field->getName(); + /** @var $field Field */ + if ($field instanceof UnstoredField || in_array($name, ['deleted_at', 'videoPath'])) { + + } else { + $fields[$name] = [ + 'default' => $field->getAttribute('default'), + 'noteditable' => $field instanceof Hidden + ]; + } + } + + $keys = array_keys($fields); + + $excel = new Spreadsheet(); + $excel->getDefaultStyle() + ->getNumberFormat() + ->setFormatCode( + NumberFormat::FORMAT_TEXT + ); + $sheet = $excel->getActiveSheet(); + $sheetname = "Export"; + $sheet->setTitle($sheetname); + + $excel->getActiveSheet()->getProtection()->setSheet(true); + $excel->getDefaultStyle()->getProtection()->setLocked(false); + + $line = 1; + $columns = 0; + $c = 0; + + foreach ($keys as $key) { + $c++; + $columns = max($columns, $c); + $cell = $sheet->getCellByColumnAndRow($c, $line); + $cell->setValue($key); + $style = $sheet->getStyleByColumnAndRow($c, $line); + if ($fields[$key]['noteditable']) { + $style->getProtection()->setLocked(Protection::PROTECTION_PROTECTED); + $style->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('dddddd'); + } + $style->getFont()->setBold(true); + $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); + } + + $maxColSize = []; + + for ($i = 0; $i < sizeof($settings); $i++) { + $c = 1; + $line++; + foreach ($keys as $k => $key) { + $j = $k + 1; + $columns = max($columns, $c); + $value = $settings[$i][$key] ?? $fields[$key]['default'] ?? ""; + $value = is_array($value) ? json_encode($value) : $value; + $maxColSize[$j][] = strlen($value); + $sheet->getCellByColumnAndRow($c, $line)->setValue($value); + $style = $sheet->getStyleByColumnAndRow($c, $line); + if ($fields[$key]['noteditable']) { + $style->getProtection()->setLocked(Protection::PROTECTION_PROTECTED); + $style->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('dddddd'); + } + $style->getAlignment()->setVertical(Alignment::VERTICAL_CENTER); + $style->getAlignment()->setWrapText(true); + $c++; + } + } + + $j = 1; + for ($i = 0; $i <= $columns; $i++) { + if (array_key_exists($j, $maxColSize)) { + if (max($maxColSize[$j]) > 50) { + $sheet->getColumnDimensionByColumn($j)->setWidth(100); + } else { + $sheet->getColumnDimensionByColumn($j)->setAutoSize(true); + } + } else { + $sheet->getColumnDimensionByColumn($j)->setAutoSize(true); + } + $j++; + } + + $tmpfile = Files::tempnam() . '.xlsx'; + $writer = new Xlsx($excel); + $writer->save($tmpfile); + + $hash = explode('cubist', $tmpfile)[1]; + + $url = route('download_settings', ['file' => base64_encode($hash)]); + $subject = __("Export groupé des paramètres des fluidbooks prêt au téléchargement"); + $notification = ''; + $action = [ + __('Télécharger') => $url, + ]; + + $user->notify(new ToolboxNotification($subject, $notification, $action, true)); + } +}