Skip to content

Commit

Permalink
Se ha implementado PHPSpreadSheet con exito, se ha creado clase exclu…
Browse files Browse the repository at this point in the history
…siva para el trabajo de funciones de excel dentro del framework.
  • Loading branch information
Sleon4 committed Mar 10, 2022
1 parent 43363f2 commit 10a6150
Show file tree
Hide file tree
Showing 6 changed files with 756 additions and 27 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function __construct() {

public static function content(bool $option = false): void {
if (!$option) {
self::$form = (object) ($_POST + $_FILES + $_ENV + $_GET + $_SESSION);
self::$form = (object) ($_POST + $_FILES + $_GET + $_SESSION + $_ENV);
} else {
self::$form = (object) json_decode(file_get_contents("php://input"), true);
}
Expand Down
24 changes: 6 additions & 18 deletions app/Http/Controllers/Example/ExampleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,20 @@
use LionMailer\Mailer;
use LionMailer\Attach;
use Valitron\Validator;
use App\Models\Class\Example;
use App\Models\Example\ExampleModel;
use App\Http\Functions\Excel;

class ExampleController extends Controller {

private ExampleModel $exampleModel;

public function __construct() {

$this->exampleModel = new ExampleModel();
}

public function methodExample(): Request {
$this->content(true);

$validator = $this->make(new Validator((array) self::$form), [
'email' => [
['user_data_email']
],
'required' => [
['user_data_email'],
['user_data_password']
]
]);

if($validator) {
return new Request('success', 'Welcome to example.', self::$form);
} else {
return new Request('error', 'All fields are required and must meet the requested characteristics.');
}
return new Request('success', 'Welcome to example.', self::$form);
}

}
130 changes: 130 additions & 0 deletions app/Http/Functions/Excel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

namespace App\Http\Functions;

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use App\Http\Functions\Files;


class Excel {

private static Spreadsheet $spreadsheet;
private static Worksheet $worksheet;
private static array $list_push = [];

public function __construct() {

}

public static function info(): array {
return self::$list_push;
}

public static function new(string $path, string $file_name): string {
Files::folder($path);
$file_name = Files::rename("{$file_name}.xlsx");
(new Xlsx(new Spreadsheet()))->save("{$path}{$file_name}");
return $file_name;
}

public static function load(string $path): void {
self::$spreadsheet = IOFactory::createReader("Xlsx")->load($path);
self::$worksheet = self::$spreadsheet->getActiveSheet();
}

public static function merge(string $columns): void {
self::$worksheet->mergeCells($columns);
}

public static function image(string $column, string $path, ?int $height = null): void {
$drawing = new Drawing();
$drawing->setCoordinates($column);
$drawing->setPath($path);
if ($height != null) $drawing->setHeight($height);
$drawing->setWorksheet(self::$worksheet);
}

public static function size(string $columns, int $size): void {
self::$worksheet->getStyle($columns)->getFont()->setSize($size);
}

public static function bold(string $column): void {
self::$worksheet->getStyle($column)->getFont()->setBold(true);
}

public static function color(string $column, string $color): void {
self::$worksheet->getStyle($column)->getFont()->getColor()->setARGB($color);
}

public static function background(string $column, string $color, ?string $type_color = null): void {
if (strtoupper($type_color) === 'FILL_SOLID') {
$setType = Fill::FILL_SOLID;
} elseif (strtoupper($type_color) === 'FILL_GRADIENT_LINEAR') {
$setType = Fill::FILL_GRADIENT_LINEAR;
} else {
$setType = Fill::FILL_SOLID;
}

self::$worksheet->getStyle($column)->getFill()->setFillType($setType)->getStartColor()->setARGB($color);
}

public static function insert(int $number): void {
self::$worksheet->insertNewRowBefore($number);
}

public static function push(string $value, string $char): void {
self::$list_push[$char] = $value;
}

public static function add(string $index = null, string $value = null): void {
if ($index != null && $value != null) {
self::$worksheet->setCellValue($index, self::replace($value));
} else {
foreach (self::$list_push as $key => $cell) {
self::$worksheet->setCellValue(((string) $key), self::replace($cell));
}
}
}

public static function save(string $path, string $file_name): string {
$file_name = Files::rename("{$file_name}.xlsx");
(new Xlsx(self::$spreadsheet))->save("{$path}{$file_name}");
return $file_name;
}

private static function replace($cell): string {
$cell = str_replace("á", "á", $cell);
$cell = str_replace("é", "é", $cell);
$cell = str_replace("í", "í", $cell);
$cell = str_replace("ó", "ó", $cell);
$cell = str_replace("ú", "ú", $cell);
$cell = str_replace("ñ", "ñ", $cell);
$cell = str_replace("á", "á", $cell);
$cell = str_replace("é", "é", $cell);
$cell = str_replace("Ã", "í", $cell);
$cell = str_replace("ó", "ó", $cell);
$cell = str_replace("ú", "ú", $cell);
$cell = str_replace("ñ", "ñ", $cell);
$cell = str_replace("Á", "á", $cell);
$cell = str_replace("É", "é", $cell);
$cell = str_replace("Í", "í", $cell);
$cell = str_replace("Ó", "ó", $cell);
$cell = str_replace("Ú", "ú", $cell);
$cell = str_replace("Ñ", "ñ", $cell);
$cell = str_replace("&aacute;", "á", $cell);
$cell = str_replace("&eacute;", "é", $cell);
$cell = str_replace("&iacute;", "í", $cell);
$cell = str_replace("&oacute;", "ó", $cell);
$cell = str_replace("&uacute;", "ú", $cell);
$cell = str_replace("&ntilde;", "ñ", $cell);
return $cell;
}

}
6 changes: 6 additions & 0 deletions app/Models/Example/ExampleModel.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace App\Models\Example;

use App\Models\Model;
use LionSql\Sql\QueryBuilder as Builder;

Expand All @@ -9,4 +11,8 @@ public function __construct() {
$this->init();
}

public function readComponentsDB(): array {
return Builder::select('fetchAll', 'components', null);
}

}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
"phroute/phroute": "^2.1",
"vlucas/phpdotenv": "^5.4",
"vlucas/valitron": "^1.4",
"phpmailer/phpmailer": "^6.6"
"phpmailer/phpmailer": "^6.6",
"phpoffice/phpspreadsheet": "^1.22"
}
}
Loading

0 comments on commit 10a6150

Please sign in to comment.