diff --git a/composer.json b/composer.json index 07b2a7c..6c1d832 100644 --- a/composer.json +++ b/composer.json @@ -3,10 +3,9 @@ "description": "Easy form builder from array", "require": { "php": "^8.1", - "khill/fontawesomephp": "^2.0", "goodby/csv": "1.2.0", "laravelcollective/html": "^6.1", - "laravel/framework": "8.x", + "laravel/framework": "8.x|9.x|10.x", "laravel/helpers": "^1.2" }, "authors": [ diff --git a/composer.lock b/composer.lock index d420d38..f188604 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "30daefbacb7d48dd3cbfa8f9e9e1986e", + "content-hash": "2390a0fcd102432d3de07ca8fe2d477b", "packages": [ { "name": "brick/math", @@ -560,59 +560,6 @@ ], "time": "2021-11-21T21:41:47+00:00" }, - { - "name": "khill/fontawesomephp", - "version": "2.0", - "source": { - "type": "git", - "url": "https://github.com/kevinkhill/FontAwesomePHP.git", - "reference": "324fdac57c319f7212d4be2e47f1d03c3f015e1a" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/kevinkhill/FontAwesomePHP/zipball/324fdac57c319f7212d4be2e47f1d03c3f015e1a", - "reference": "324fdac57c319f7212d4be2e47f1d03c3f015e1a", - "shasum": "" - }, - "require-dev": { - "phpunit/phpunit": "~4.8", - "satooshi/php-coveralls": "~1.0", - "squizlabs/php_codesniffer": "~2.5" - }, - "type": "library", - "extra": { - "laravel": { - "providers": [ - "Khill\\FontAwesome\\Laravel\\FontAwesomeServiceProvider" - ], - "aliases": { - "FA": "Khill\\FontAwesome\\Laravel\\FontAwesomeFacade" - } - } - }, - "autoload": { - "psr-4": { - "Khill\\FontAwesome\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Kevin Hill", - "email": "kevinkhill@gmail.com", - "role": "Developer" - } - ], - "description": "PHP wrapper library for the fantastic Font Awesome icon set.", - "support": { - "issues": "https://github.com/kevinkhill/FontAwesomePHP/issues", - "source": "https://github.com/kevinkhill/FontAwesomePHP/tree/2.0" - }, - "time": "2018-06-19T17:27:46+00:00" - }, { "name": "laravel/framework", "version": "v8.83.9", @@ -4707,5 +4654,5 @@ "php": "^8.1" }, "platform-dev": [], - "plugin-api-version": "2.2.0" + "plugin-api-version": "2.0.0" } diff --git a/config/tenants.php b/config/tenants.php index c0fab16..bd4dfa6 100644 --- a/config/tenants.php +++ b/config/tenants.php @@ -6,6 +6,10 @@ "DB_USERNAME" => env('DB_USERNAME' ,'root'), "DB_PASSWORD" => env('DB_PASSWORD' ,''), + 'db' => [ + 'connection' => env('DB_AUTH_CONNECTION', 'mysql'), + ], + "migration_paths" => [ "database/migrations/tenants", "database/migrations/tenants/stocks", @@ -15,4 +19,4 @@ TenantConfigSeeder::class, TenantProductsSeeder::class, ], -]; \ No newline at end of file +]; diff --git a/src/GrogServiceProvider.php b/src/GrogServiceProvider.php index 9c8a6ce..c8677c0 100644 --- a/src/GrogServiceProvider.php +++ b/src/GrogServiceProvider.php @@ -3,7 +3,6 @@ use Collective\Html\HtmlServiceProvider; use Illuminate\Support\ServiceProvider; use Illuminate\Foundation\AliasLoader; -use Khill\FontAwesome\Laravel\FontAwesomeServiceProvider; class GrogServiceProvider extends ServiceProvider { @@ -32,6 +31,6 @@ public function boot(){ public function register(){ $this->app->register(HtmlServiceProvider::class); - $this->app->register(FontAwesomeServiceProvider::class); + $this->mergeConfigFrom(__DIR__ . '/../config/tenants.php', 'tenants'); } } diff --git a/src/Helpers/Helpers.php b/src/Helpers/Helpers.php index 35e655e..d7c360a 100644 --- a/src/Helpers/Helpers.php +++ b/src/Helpers/Helpers.php @@ -3,7 +3,7 @@ use BadChoice\Grog\Services\RVConnection; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Config; - +use BadChoice\Grog\Services\ProvidesDatabaseConnection; /** * Creates a connection for the database of the $user * the name of the connection will be 'RevoRetail_{$user}' @@ -13,8 +13,15 @@ * @param $shouldConnect * @param bool $reports true to connect to the read only database insatance */ -function createDBConnection($user, $shouldConnect = false, $reports = false) { - (new RVConnection($user))->useReportsDatabase($reports)->create($shouldConnect); +function createDBConnection(string|ProvidesDatabaseConnection $object, $shouldConnect = false, $reports = false) { + if (is_string($object) && RVConnection::$provider) { + $object = RVConnection::$provider::databaseConnectionProviderByName($object); + } + + return (new RVConnection($object instanceof ProvidesDatabaseConnection ? $object->getDatabaseName() : $object)) + ->atInstance($object instanceof ProvidesDatabaseConnection ? $object->getDatabaseInstance() : null) + ->useReportsDatabase($reports) + ->create($shouldConnect); } /** diff --git a/src/Services/ProvidesDatabaseConnection.php b/src/Services/ProvidesDatabaseConnection.php new file mode 100644 index 0000000..e52ab7a --- /dev/null +++ b/src/Services/ProvidesDatabaseConnection.php @@ -0,0 +1,10 @@ +dbInstance = $instanceName; + return $this; + } + private function getDatabase(){ $prefix = config('tenants.DB_TENANTS_PREFIX'); return App::environment('testing') ? config('database.connections.'.config('database.default').'.database', ':memory:') : $prefix.$this->databaseName; @@ -70,6 +78,9 @@ protected function getUsername() { } protected function getHost() { + if ($this->dbInstance) { + return config('tenants.DB_INSTANCES.'.$this->dbInstance . '.' . ($this->useReportsDatabase ? 'reports' : 'main')); + } return ($this->useReportsDatabase) ? config('tenants.DB_REPORTS_HOST') : config('tenants.DB_HOST'); } diff --git a/src/Services/ResourceRoute.php b/src/Services/ResourceRoute.php index 1925521..f37e4cd 100644 --- a/src/Services/ResourceRoute.php +++ b/src/Services/ResourceRoute.php @@ -42,7 +42,7 @@ public static function link_to_edit($object) public static function link_to_delete($object) { - return link_to(route(object_route($object) . '.destroy', $object->id), '', ['id' => $object->id, 'class' => 'delete-resource', 'data-delete' => 'confirm resource']) ; + return link_to(route(object_route($object) . '.destroy', $object->id), '', ['id' => $object->id, 'class' => 'delete-resource', 'data-delete' => 'confirm resource', 'confirm-message' => __('admin.confirmDelete')]) ; } public static function route_to_update() diff --git a/src/Traits/AuthConnectionTrait.php b/src/Traits/AuthConnectionTrait.php index 26c3037..b2728f0 100644 --- a/src/Traits/AuthConnectionTrait.php +++ b/src/Traits/AuthConnectionTrait.php @@ -1,7 +1,11 @@ - $value) { - if (static::isKeyARelation($key) ) { - if($value != null) { - $toSaveNested[$key] = $value; + foreach ($nestedArray as $relationMethodName => $value) { + if (static::isARelationMethod($relationMethodName)) { + if ($value != null) { + $toSaveNested[$relationMethodName] = $value; } - unset($nestedArray[$key]); + unset($nestedArray[$relationMethodName]); } } if (isset($nestedArray['id']) && $nestedArray['id']) { - $object = static::find($nestedArray['id']); - if($object) $object->update($nestedArray); - else if($createIfNotFound) $object = static::create($nestedArray); + if ($object = static::find($nestedArray['id'])) { + $object->update($nestedArray); + } else if ($createIfNotFound) { + $object = static::withTrashed()->updateOrCreate([ + 'id' => $nestedArray['id'], + ], [ + 'deleted_at' => null, + ...$nestedArray + ]); + } } else { $object = static::create($nestedArray); } - foreach ($toSaveNested as $key => $array) { - $relatedModel = $object->$key()->getRelated(); - $foreignKey = $object->$key()->getForeignKeyName(); + foreach ($toSaveNested as $relationMethodName => $contents) { + $relation = $object->$relationMethodName(); + $relatedModel = $relation->getRelated(); + $foreignKey = $relation->getForeignKeyName(); + $foreignKeyType = $relation instanceof MorphOneOrMany + ? $relation->getMorphType() + : null; + $contents = is_array($contents) ? $contents : [$contents]; - if(is_array($array)) { - foreach ($array as $content) { - $content->$foreignKey = $object->id; - $relatedModel::saveNested((array)$content, $createIfNotFound); - } + if (static::isAnOverwritableMethodRelation($relationMethodName)) { + $object->$relationMethodName()->delete(); } - else{ - $array->$foreignKey = $object->id; - $relatedModel::saveNested((array)$array, $createIfNotFound); + foreach ($contents as $content) { + $content->$foreignKey = $object->id; + if ($foreignKeyType) { + $content->$foreignKeyType = get_class($object); + } + $relatedModel::saveNested((array) $content, $createIfNotFound); } } return $object; } - public static function isKeyARelation($key){ + public static function isARelationMethod(string $relationMethodName): bool + { + $object = new static(); + return method_exists($object, $relationMethodName) && ($object->$relationMethodName() instanceof Relation); + } + + public static function isAnOverwritableMethodRelation(string $relationMethodName): bool + { $object = new static(); - return method_exists( $object, $key) && ($object->$key() instanceof Relation); + return in_array($relationMethodName, $object->overwritableRelations ?? []); } } \ No newline at end of file