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
{
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));
}
}
--- /dev/null
+<?php
+
+namespace App\Jobs;
+
+use App\Models\FluidbookPublication;
+use App\Models\User;
+use App\Notifications\ToolboxNotification;
+use Cubist\Backpack\Magic\Fields\Hidden;
+use Cubist\Backpack\Magic\Fields\UnstoredField;
+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;
+
+class FluidbookSettingsExport extends Base
+{
+ protected $listID;
+
+ protected $userID;
+
+ public function __construct($ids, $userID)
+ {
+ $this->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));
+ }
+}