Skip to content

Commit

Permalink
Merge branch '5.x' into feature/laravel-11
Browse files Browse the repository at this point in the history
  • Loading branch information
luanfreitasdev authored Dec 28, 2023
2 parents e5a3d43 + 5f4e786 commit 1b7fda4
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 68 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@
"require-dev": {
"composer/composer": "^2.6.6",
"laravel/pint": "^1.13.7",
"laradumps/laradumps-core": "^1.1",

"spaze/phpstan-disallowed-calls": "^2.16.1",
"larastan/larastan": "^2.7",
"pestphp/pest": "^2.28.1",
"orchestra/testbench": "^8.17|^9.0",
"laradumps/laradumps-core": "^1.0|dev-update-dependencies"
"orchestra/testbench": "^8.17|^9.0"
},
"suggest": {
"openspout/openspout": "Required to export XLS and CSV"
Expand Down
2 changes: 1 addition & 1 deletion dist/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"/powergrid.js": "/powergrid.js?id=495809d3974beb1d1566c20a3225b674",
"/powergrid.js": "/powergrid.js?id=d96096b65479becbf69b80ca0c298e90",
"/bootstrap5.css": "/bootstrap5.css?id=03aba1df82c23db07c1d1096efcd42ae",
"/tailwind.css": "/tailwind.css?id=479d85eb8b0b8341542e0b979c84f17d",
"/tom-select.css": "/tom-select.css?id=7af730d2c4bf937316d4002948b1571d",
Expand Down
2 changes: 1 addition & 1 deletion dist/powergrid.js

Large diffs are not rendered by default.

21 changes: 19 additions & 2 deletions resources/js/components/pg-flatpickr.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ export default (params) => ({
element: null,
selectedDates: null,
init() {
if(typeof flatpickr == "undefined") {
console.log('%c%s',
'color: #f59e0c; font-size: 1.2em; font-weight: bold; line-height: 1.5',
` PowerGrid`);

console.error('%c%s',
'font-size: 1em; line-height: 1.5',
`
Failed to mount filter: Filter::datetime('${this.dataField}') on table ['${this.tableName}']
• Install flatpickr: npm install flatpickr
• Add 'import flatpickr from "flatpickr"' in resources/js/app.js file
`
);
}

window.addEventListener(`pg:clear_flatpickr::${this.tableName}:${this.dataField}`, () => {
if (this.$refs.rangeInput && this.element) {
this.element.clear()
Expand All @@ -24,13 +41,13 @@ export default (params) => ({

const lang = this.locale.locale;

if (typeof lang !== 'undefined') {
if (typeof lang !== 'undefined' && typeof flatpickr != "undefined") {
this.locale.locale = require("flatpickr/dist/l10n/"+lang+".js").default[lang];
}

const options = this.getOptions()

if(this.$refs.rangeInput) {
if(this.$refs.rangeInput && typeof flatpickr != "undefined") {
this.element = flatpickr(this.$refs.rangeInput, options);

this.selectedDates = this.$wire.get(`filters.${this.type}.${this.dataField}.formatted`)
Expand Down
69 changes: 69 additions & 0 deletions src/Commands/Actions/DependenciesCheck.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace PowerComponents\LivewirePowerGrid\Commands\Actions;

use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;

class DependenciesCheck
{
/**
* Check if flatpickr is installed
*
*/
public static function flatpickr(): ?string
{
$filesToCheck = [
base_path('tailwind.config.js'),
base_path('resources/js/app.js'),
];

$message = "\n💡 It seems you are not using the <comment>flatpickr</comment> plugin.\n Please check: <comment>https://livewire-powergrid.com/table/column-filters.html#filter-datetimepicker</comment> for more information.";

foreach ($filesToCheck as $file) {
if (File::exists($file) && !Str::contains(File::get($file), "flatpickr")) {
return $message;
}
}

return null;
}

/**
* Check if openspout/openspout is installed
*
*/
public static function openspout(): ?string
{
$file = base_path() . '/' . 'composer.json';

if (File::exists($file)) {
$content = File::get($file);

if (!Str::contains($content, "openspout/openspout")) {
return("\n💡 It seems you are using the <comment>openspout/openspout</comment> package.\n Please check: <comment>https://livewire-powergrid.com/table/features-setup.html#exportable</comment> for more information.");
}
}

return null;
}

/**
* Check if TailwindForms is installed
*
*/
public static function tailwindForms(): ?string
{
$file = base_path() . '/' . 'tailwind.config.js';

if (File::exists($file)) {
$content = File::get($file);

if (Str::contains($content, "require('@tailwindcss/forms')") === true) {
return("\n💡 It seems you are using the plugin <comment>Tailwindcss/form</comment>.\n Please check: <comment>https://livewire-powergrid.com/get-started/configure.html#tailwind-forms</comment> for more information.");
}
}

return null;
}
}
12 changes: 10 additions & 2 deletions src/Commands/Actions/FillableTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static function eloquentBuilder(string $modelQualifiedName, string $model

$title = Str::of($field)->replace('_', ' ')->ucfirst();

if (in_array($columnType, ['datetime', 'date', 'timestamp'])) {
if (in_array($columnType, ['datetime', 'date'])) {
$columns .= ' Column::make(\'' . $title . '\', \'' . $field . '_formatted\', \'' . $field . '\')' . "\n" . ' ->sortable(),' . "\n\n";
}

Expand Down Expand Up @@ -144,7 +144,15 @@ public static function queryBuilder(string $databaseTableName, string $stubFile
$columns = "[\n";
$filters = "[\n";

foreach ($columnListing as $field) {

$filteredColumns = collect($columnListing)
->filter(function ($column) {
return !in_array($column, ['password', 'remember_token', 'email_verified_at']);
})
->toArray();

/** @var string $field */
foreach ($filteredColumns as $field) {
$columnType = Schema::getColumnType($databaseTableName, $field);

$title = Str::of($field)->replace('_', ' ')->ucfirst();
Expand Down
28 changes: 0 additions & 28 deletions src/Commands/Actions/TailwindForm.php

This file was deleted.

82 changes: 51 additions & 31 deletions src/Commands/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,32 @@
use Illuminate\Support\Facades\{File, Schema};
use Illuminate\Support\{Arr, Str};

use function Laravel\Prompts\{confirm, info, select, suggest, text};
use function Laravel\Prompts\{confirm, error, info, select, suggest, text};

use PowerComponents\LivewirePowerGrid\Commands\Actions\{DatabaseTables, FillableTable, Models, Stubs, TailwindForm};
use PowerComponents\LivewirePowerGrid\Commands\Actions\{DatabaseTables,
DependenciesCheck,
FillableTable,
Models,
Stubs};
use PowerComponents\LivewirePowerGrid\Commands\Concerns\RenderAscii;
use PowerComponents\LivewirePowerGrid\Commands\Exceptions\CreateCommandException;

class CreateCommand extends Command
{
use RenderAscii;

public const DATASOURCE_ELOQUENT_BUILDER = 'Eloquent Builder';

public const DATASOURCE_QUERY_BUILDER = 'Query Builder';

public const DATASOURCE_COLLECTION = 'Collection';

public const DATASOURCE_OPTIONS = [
self::DATASOURCE_ELOQUENT_BUILDER,
self::DATASOURCE_QUERY_BUILDER,
self::DATASOURCE_COLLECTION,
];

/** @var string */
protected $signature = 'powergrid:create {--template= : name of the file that will be used as a template}';

Expand Down Expand Up @@ -53,20 +69,18 @@ public function handle(): int

$this->call('powergrid:update');

$this->checkDependencies();

try {
$this->askTableName();
$this->askDatasource();
$this->askModel();
} catch (CreateCommandException $e) {
$this->error($e->getMessage());
error($e->getMessage());

return self::FAILURE;
}

$this->checkTailwindForms();

$this->showCreated();

return self::SUCCESS;
}

Expand Down Expand Up @@ -105,28 +119,18 @@ protected function askDataBaseTableName(): void
$exists = Schema::hasTable($this->dataBaseTableName);

if (!$exists && !app()->runningUnitTests()) {
$this->components->error('The table you provided does not exist!');
error('The table you provided does not exist!');
$this->askDataBaseTableName();
}
}

protected function askDatasource(): void
{
$datasourceOption = select(
$this->datasourceOption = strval(select(
label: 'What type of data source will you use?',
options: [
'Eloquent Builder',
'Query Builder',
'Collection',
],
default: 'Eloquent Builder'
);

$this->datasourceOption = match ($datasourceOption) {
'Eloquent Builder' => 'm',
'Query Builder' => 'qb',
default => 'c'
};
options: self::DATASOURCE_OPTIONS,
default: self::DATASOURCE_ELOQUENT_BUILDER
));
}

/**
Expand All @@ -136,7 +140,7 @@ protected function askModel(): void
{
$this->stub = Stubs::load($this->datasourceOption, strval($this->option('template')));

if (strtolower($this->datasourceOption) === 'm') {
if (strtolower($this->datasourceOption) === strtolower(self::DATASOURCE_ELOQUENT_BUILDER)) {
$this->model = suggest(
label: 'Enter your Model name or file path',
options: Models::list(),
Expand Down Expand Up @@ -164,11 +168,11 @@ protected function askModel(): void
}
}

if (in_array(strtolower($this->datasourceOption), ['m', 'qb'])) {
if (in_array(strtolower($this->datasourceOption), [strtolower(self::DATASOURCE_ELOQUENT_BUILDER), strtolower(self::DATASOURCE_QUERY_BUILDER)])) {
$this->useFillable = confirm('Create columns based on Model\'s fillable property?');

if ($this->useFillable) {
if (strtolower($this->datasourceOption) === 'qb') {
if (strtolower($this->datasourceOption) === strtolower(self::DATASOURCE_QUERY_BUILDER)) {
$this->askDataBaseTableName();

$this->stub = FillableTable::queryBuilder($this->dataBaseTableName, strval($this->option('template')));
Expand All @@ -194,14 +198,14 @@ protected function askModel(): void
$this->stub = str_replace('{{ subFolder }}', $subFolder, $this->stub);
$this->stub = str_replace('{{ componentName }}', $this->componentName, $this->stub);

if (strtolower($this->datasourceOption) === 'm') {
if (strtolower($this->datasourceOption) === strtolower(self::DATASOURCE_ELOQUENT_BUILDER)) {
$this->stub = str_replace('{{ modelName }}', $this->model, $this->stub);
$this->stub = str_replace('{{ modelLastName }}', $this->modelName, $this->stub);
$this->stub = str_replace('{{ modelLowerCase }}', Str::lower($this->modelName), $this->stub);
$this->stub = str_replace('{{ modelKebabCase }}', Str::kebab($this->modelName), $this->stub);
}

if (strtolower($this->datasourceOption) === 'qb') {
if (strtolower($this->datasourceOption) === strtolower(self::DATASOURCE_QUERY_BUILDER)) {
$this->stub = str_replace('{{ databaseTableName }}', $this->dataBaseTableName, $this->stub);
}

Expand Down Expand Up @@ -242,7 +246,13 @@ protected function askModel(): void

if ($createTable) {
File::put($path, $this->stub);

$this->showCreated();

return;
}

info("⭐ <comment>" . self::thanks() . "</comment> Please consider <comment>starring</comment> our repository at <comment>https://github.com/Power-Components/livewire-powergrid</comment> ⭐\n");
}

protected function showCreated(): void
Expand All @@ -259,12 +269,22 @@ protected function thanks(): string
return strval(str_replace(',', '!', strval(__('Thanks,'))));
}

protected function checkTailwindForms(): void
private function checkDependencies(): void
{
$tailwind = TailwindForm::check();
$flatpickr = DependenciesCheck::flatpickr();
$openspout = DependenciesCheck::openspout();
$tailwindForms = DependenciesCheck::tailwindForms();

if (!empty($flatpickr)) {
info($flatpickr);
}

if (!empty($openspout)) {
info($openspout);
}

if (!empty($tailwind)) {
$this->components->info($tailwind);
if (!empty($tailwindForms)) {
info($tailwindForms);
}
}
}
2 changes: 1 addition & 1 deletion src/Commands/UpdateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function handle(): int
}

if (!class_exists(\Composer\Factory::class)) {
\Laravel\Prompts\error('You need to install composer: <comment>composer require composer/composer --dev</comment>');
warning('UpdateCommand: You need to install composer: <info>composer require composer/composer --dev</info>');

return self::SUCCESS;
}
Expand Down

0 comments on commit 1b7fda4

Please sign in to comment.