Skip to content

Commit

Permalink
Se han implementado librerias para exportar archivos word, excel y pd…
Browse files Browse the repository at this point in the history
…f, se han añadido y validado la clase de archivos.
  • Loading branch information
Sleon4 committed Mar 11, 2022
1 parent 10a6150 commit 9c1fd45
Show file tree
Hide file tree
Showing 9 changed files with 605 additions and 60 deletions.
42 changes: 26 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,47 +1,57 @@
# Lion-Framework-Backend

## Install
```
```powershell
composer create-project lion-framework/lion-backend
```

## Update packages
```
```powershell
composer install
```

### Libraries used
[Lion-SQL](https://github.com/Sleon4/Lion-SQL)
```
#####[Lion-SQL](https://github.com/Sleon4/Lion-SQL)
```powershell
composer require lion-framework/lion-sql
```

[Lion-Route](https://github.com/Sleon4/Lion-Route)
```
#####[Lion-Route](https://github.com/Sleon4/Lion-Route)
```powershell
composer require lion-framework/lion-route
```

[Lion-Mailer](https://github.com/Sleon4/Lion-Mailer)
```
#####[Lion-Mailer](https://github.com/Sleon4/Lion-Mailer)
```powershell
composer require lion-framework/lion-mailer
```

[Valitron](https://github.com/vlucas/valitron)
```
#####[Valitron](https://github.com/vlucas/valitron)
```powershell
composer require vlucas/valitron
```

[PHP dotenv](https://github.com/vlucas/phpdotenv)
```
#####[PHP dotenv](https://github.com/vlucas/phpdotenv)
```powershell
composer require vlucas/phpdotenv
```

[PHRoute](https://github.com/mrjgreen/phroute)
```
#####[PHRoute](https://github.com/mrjgreen/phroute)
```powershell
composer require phroute/phroute
```

[PHPMailer](https://github.com/PHPMailer/PHPMailer)
```
#####[PHPMailer](https://github.com/PHPMailer/PHPMailer)
```powershell
composer require phpmailer/phpmailer
```

#####[PHPSpreadsheet](https://github.com/PHPOffice/PhpSpreadsheet)
```powershell
composer require phpoffice/phpspreadsheet
```

#####[Dompdf](https://github.com/dompdf/dompdf)
```powershell
composer require dompdf/dompdf
```
18 changes: 17 additions & 1 deletion app/Http/Controllers/Example/ExampleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
use LionMailer\Attach;
use Valitron\Validator;
use App\Models\Example\ExampleModel;
use App\Http\Functions\Excel;
use App\Http\Functions\Word;
use App\Http\Functions\Pdf;
use App\Http\Functions\Files;

class ExampleController extends Controller {

Expand All @@ -23,4 +25,18 @@ public function methodExample(): Request {
return new Request('success', 'Welcome to example.', self::$form);
}

public function createMyWord() {
$this->content(true);

Word::loadTemplate('public/template/template.docx');
Word::add([
'user_data_email' => self::$form->user_data_email,
'user_data_password' => self::$form->user_data_password
]);

$word_file = Word::saveTemplate('public/', 'nuevo_data', true);

return new Request('success', 'documento creado.');
}

}
36 changes: 2 additions & 34 deletions app/Http/Functions/Excel.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ 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");
Expand Down Expand Up @@ -85,10 +81,10 @@ public static function push(string $value, string $char): void {

public static function add(string $index = null, string $value = null): void {
if ($index != null && $value != null) {
self::$worksheet->setCellValue($index, self::replace($value));
self::$worksheet->setCellValue($index, Files::replace($value));
} else {
foreach (self::$list_push as $key => $cell) {
self::$worksheet->setCellValue(((string) $key), self::replace($cell));
self::$worksheet->setCellValue(((string) $key), Files::replace($cell));
}
}
}
Expand All @@ -99,32 +95,4 @@ public static function save(string $path, string $file_name): string {
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("á", "á", $cell);
$cell = str_replace("é", "é", $cell);
$cell = str_replace("í", "í", $cell);
$cell = str_replace("ó", "ó", $cell);
$cell = str_replace("ú", "ú", $cell);
$cell = str_replace("ñ", "ñ", $cell);
return $cell;
}

}
48 changes: 42 additions & 6 deletions app/Http/Functions/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public static function rename(string $file): string {
}

public static function upload(array $tmps, array $names, string $path): bool {
if (self::folder($path)) {
foreach ($names as $key_name => $name) {
if (!move_uploaded_file($tmps[$key_name], $path . self::rename($name))) {
return false;
break;
}
self::folder($path);

foreach ($names as $key => $name) {
if (!move_uploaded_file($tmps[$key], $path . self::rename($name))) {
return false;
break;
}
}

Expand All @@ -62,6 +62,14 @@ public static function getExtension(string $url_path): string {
return (new \SplFileInfo($url_path))->getExtension();
}

public static function getName(string $url_path): string {
return (new \SplFileInfo($url_path))->getBasename("." . self::getExtension($url_path));
}

public static function getBasename(string $url_path): string {
return (new \SplFileInfo($url_path))->getBasename();
}

public static function folder(string $path): bool {
return !self::exist([$path]) ? mkdir($path, 0777, true) : true;
}
Expand All @@ -79,4 +87,32 @@ public static function validate(array $files, array $exts): bool {
return true;
}

public 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("á", "á", $cell);
$cell = str_replace("é", "é", $cell);
$cell = str_replace("í", "í", $cell);
$cell = str_replace("ó", "ó", $cell);
$cell = str_replace("ú", "ú", $cell);
$cell = str_replace("ñ", "ñ", $cell);
return $cell;
}

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

//require_once 'dompdf/autoload.inc.php';

namespace App\Http\Functions;

use Dompdf\Dompdf;
use App\Http\Functions\Files;

class Pdf {

private static Dompdf $dompdf;

public function __construct() {

}

public static function load(): void {
self::$dompdf = new Dompdf();
}

public static function convertToPdf(string $path, string $url): string {
Files::folder($url);
$file_name = $url . Files::getName($path) . ".pdf";
self::$dompdf->loadHtml(file_get_contents($path));
self::$dompdf->render();
file_put_contents($file_name, self::$dompdf->output());

return $file_name;
}

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

namespace App\Http\Functions;

use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Style\Font;
use PhpOffice\PhpWord\Element\Section;
use App\Http\Functions\Files;

class Word {

private static ?TemplateProcessor $templateProcessor = null;
private static ?PhpWord $phpWord = null;
private static ?Section $section = null;
private static ?Font $font = null;

public function __construct() {

}

public static function load(): void {
self::$phpWord = new PhpWord();
self::$font = new Font();
}

public static function save(string $path, string $file_name, array $type = [], bool $rename = false): array {
Files::folder($path);
$list_files = [];
$count = 0;

foreach ($type as $key => $ext) {
if ($count === 0) {
$file_name = !$rename ? "{$file_name}.{$ext}" : Files::rename("{$file_name}.{$ext}");
} else {
$file_name = Files::getName($file_name) . ".{$ext}";
}

$list_files[$ext] = "{$path}{$file_name}";
IOFactory::createWriter(self::$phpWord, $key)->save("{$path}{$file_name}");

$count++;
}

return $list_files;
}

public static function loadTemplate(string $path): void {
self::$font = new Font();
self::$templateProcessor = new TemplateProcessor($path);
}

public static function saveTemplate(string $path, string $file_name, bool $option = false): string {
$file_name = !$option ? "{$file_name}.docx" : Files::rename("{$file_name}.docx");
self::$templateProcessor->saveAs("{$path}{$file_name}");
self::$templateProcessor = null;
return "{$path}{$file_name}";
}

public static function convertToHtml(string $path, string $url, bool $option = false): string {
Files::folder($url);
$file_name = !$option ? Files::getName($path) : Files::getName($path);
$file_name = "{$url}{$file_name}.html";

IOFactory::createWriter(IOFactory::load($path), 'HTML')->save($file_name);
return $file_name;
}

public static function add(array $elements): void {
foreach ($elements as $key => $element) {
self::$templateProcessor->setValue($key, $element);
}
}

public static function section(array $options = []): void {
self::$section = self::$phpWord->addSection($options);
}

public static function text(string $text): void {
self::$section->addText($text)->setFontStyle(self::$font);
self::$font = new Font();
}

public static function bold(): void {
self::$font->setBold(true);
}

public static function name(string $name): void {
self::$font->setName($name);
}

public static function size(int $size): void {
self::$font->setSize($size);
}

}
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
"vlucas/phpdotenv": "^5.4",
"vlucas/valitron": "^1.4",
"phpmailer/phpmailer": "^6.6",
"phpoffice/phpspreadsheet": "^1.22"
"phpoffice/phpspreadsheet": "^1.22",
"phpoffice/phpword": "^0.18.3",
"dompdf/dompdf": "^1.2"
}
}
Loading

0 comments on commit 9c1fd45

Please sign in to comment.