Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
5b53ce7
Merge pull request #4 from revosystems/update-font-awesome-to-2.0
BadChoice Apr 21, 2022
b925a2e
Refactor saveNestedTrait.
PauRevo Apr 26, 2022
f3d7a7c
Refactor saveNestedTrait.
PauRevo Apr 26, 2022
63c8d8f
Merge pull request #7 from MartiSalaMorral/master
BadChoice Apr 28, 2022
118f681
Merge pull request #5 from revosystems/hotfix/master-php7-refactor
BadChoice May 2, 2022
390392c
Merge pull request #6 from revosystems/hotfix/allow-polymorph-to-save…
BadChoice May 2, 2022
cdc9025
Merge pull request #8 from revosystems/hotfix/master-php7-refactor
PauBenetPrat May 2, 2022
2dab4ba
Added the confirm delete message
alexrevo May 4, 2022
9d7cda0
Merge pull request #10 from alexrevo/master
BadChoice May 4, 2022
fbe872c
Remove font-awesome-dependency.
PauRevo May 4, 2022
9df6172
Merge pull request #11 from revosystems/hotfix/remove-font-awesome-de…
PauBenetPrat May 4, 2022
37be676
Merge pull request #9 from revosystems/master-php7
BadChoice May 30, 2022
5d3b943
Add overwritable methode relation logic.
PauRevo Nov 14, 2022
2c961ff
Merge pull request #12 from revosystems/feature/add-overwritable-meth…
BadChoice Nov 18, 2022
55175f5
Added compatibility with laravel 9
MartiSalaMorral Jan 4, 2023
f68808b
Merge pull request #13 from MartiSalaMorral/improvement/laravel-9
BadChoice Jan 4, 2023
da417f8
Improved the auth connection trait by requesting the data from the co…
alexrevo Feb 8, 2023
c57b722
Merge pull request #14 from revosystems/bugfix/auth-connection
BadChoice Feb 9, 2023
2583e4b
Merge branch 'master' of https://github.com/revosystems/grog
MartiSalaMorral Feb 10, 2023
fc2f242
REV-10678 removes khill fontawesome reference
MartiSalaMorral Feb 10, 2023
8000ec8
Merge pull request #15 from revosystems/bugfix/REV-10678
BadChoice Feb 10, 2023
412b9b4
Undo deletes when updating overwritableMethodRelation.
PauRevo Jun 5, 2023
936a24f
Merge pull request #17 from revosystems/hotfix/undo-delete-with-trashed
BadChoice Jun 6, 2023
f2d87ea
Added compatibility with Laravel 10
MartiSalaMorral Aug 22, 2023
cd61964
Merge pull request #18 from revosystems/laravel-10
BadChoice Aug 22, 2023
a0c15fc
Adds option to connect to multiple instances
Oct 5, 2023
927080b
Fixed minor typo
Oct 5, 2023
8de4458
Auto find user
Oct 5, 2023
3811ad9
Reducing complexity
Oct 5, 2023
aa5154e
PR Fixes
Oct 9, 2023
aa91227
Merge
Oct 9, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
57 changes: 2 additions & 55 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion config/tenants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -15,4 +19,4 @@
TenantConfigSeeder::class,
TenantProductsSeeder::class,
],
];
];
3 changes: 1 addition & 2 deletions src/GrogServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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');
}
}
13 changes: 10 additions & 3 deletions src/Helpers/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}'
Expand All @@ -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);
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/Services/ProvidesDatabaseConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace BadChoice\Grog\Services;

interface ProvidesDatabaseConnection
{
public function getDatabaseInstance() : ?string;
public function getDatabaseName(): string;
static function databaseConnectionProviderByName($string) : ?ProvidesDatabaseConnection;
}
17 changes: 14 additions & 3 deletions src/Services/RVConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

class RVConnection {

protected $useReportsDatabase = false;
protected $databaseName;
protected $connectionName;
static ?string $provider;

protected bool $useReportsDatabase = false;
protected string $databaseName;
protected string $connectionName;
protected ?string $dbInstance;

public function __construct($database)
{
Expand All @@ -29,6 +32,11 @@ public function useReportsDatabase($useReportsDatabase = true)
return $this;
}

public function atInstance(?string $instanceName) {
$this->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;
Expand Down Expand Up @@ -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');
}

Expand Down
2 changes: 1 addition & 1 deletion src/Services/ResourceRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 9 additions & 5 deletions src/Traits/AuthConnectionTrait.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?php namespace BadChoice\Grog\Traits;
<?php

trait AuthConnectionTrait{
public function getConnectionName(){
return env('DB_AUTH_CONNECTION','mysql');
namespace BadChoice\Grog\Traits;

trait AuthConnectionTrait
{
public function getConnectionName()
{
return config('tenants.db.connection');
}
}
}
73 changes: 48 additions & 25 deletions src/Traits/SaveNestedTrait.php
Original file line number Diff line number Diff line change
@@ -1,52 +1,75 @@
<?php namespace BadChoice\Grog\Traits;
<?php

namespace BadChoice\Grog\Traits;

use Illuminate\Database\Eloquent\Relations\MorphOneOrMany;
use Illuminate\Database\Eloquent\Relations\Relation;

/**
* Class SaveNestedTrait
* @package BadChoice\Grog\Traits
*/
trait SaveNestedTrait{

public static function saveNested(array $nestedArray, $createIfNotFound = false)
trait SaveNestedTrait
{
public static function saveNested(array $nestedArray, bool $createIfNotFound = false)
{
$toSaveNested = [];
foreach ($nestedArray as $key => $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 ?? []);
}
}