Skip to content

Commit

Permalink
review organizations (#277)
Browse files Browse the repository at this point in the history
* wip organizations

* wip

* init export

* cleanup

* fix , bug

* Review/organizations add counties (#278)

* Add counties to OrganizationResource

* Fix return type

* wip

* Add request for update organization

* Fix update in vue

* Add actions for approve / reject changes on organizations

* Php-cs-fixer

* Remove old changes when you change the same attribute and the old changes are not approved

* Refactoring move where login in a scope for reusing

* Add waring in edit ong view for changes in pending

* Add  widget for pending changes

* cs-fixer

* fix: activitylog config

* refactor

* renaming organizations action

* wip

* wip

* wip

* Add org resource (#279)

* add resource

* cleanup

* wip

* wip

* wip

* fix org logo in admin

* add org filters

* fix filter display in org list

* wip

* wip

* wip

---------

Co-authored-by: Lupu Gheorghe <[email protected]>
Co-authored-by: Lupu Gheorghe <[email protected]>
  • Loading branch information
3 people committed Sep 1, 2023
1 parent 54dda91 commit 582156b
Show file tree
Hide file tree
Showing 95 changed files with 3,635 additions and 1,126 deletions.
18 changes: 3 additions & 15 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,10 @@
'align_multiline_comment' => true,
'array_indentation' => true,
'array_syntax' => ['syntax' => 'short'],

'binary_operator_spaces' => [
'default' => 'single_space',
'operators' => ['=>' => null],
],
'blank_line_after_namespace' => true,
'blank_line_after_opening_tag' => true,
'blank_line_before_statement' => [
'statements' => ['return'],
],
'braces' => true,
'cast_spaces' => true,
'class_attributes_separation' => [
'elements' => ['method'],
],
'class_definition' => true,
'concat_space' => [
'spacing' => 'one',
],
Expand Down Expand Up @@ -96,8 +84,7 @@
'no_singleline_whitespace_before_semicolons' => true,
'no_spaces_after_function_name' => true,
'no_spaces_inside_parenthesis' => true,
'no_trailing_comma_in_list_call' => true,
'no_trailing_comma_in_singleline_array' => true,
'no_trailing_comma_in_singleline' => true,
'no_trailing_whitespace' => true,
'no_trailing_whitespace_in_comment' => true,
'no_unreachable_default_argument_value' => true,
Expand Down Expand Up @@ -155,7 +142,7 @@
// php-cs-fixer 3: Changed options
'binary_operator_spaces' => [
'default' => 'single_space',
'operators' => ['=>' => null],
'operators' => [],
],
'blank_line_before_statement' => [
'statements' => ['return'],
Expand Down Expand Up @@ -194,6 +181,7 @@
__DIR__ . '/app',
__DIR__ . '/config',
__DIR__ . '/database',
__DIR__ . '/lang',
__DIR__ . '/resources',
__DIR__ . '/routes',
__DIR__ . '/tests',
Expand Down
95 changes: 95 additions & 0 deletions app/Concerns/LogsActivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

declare(strict_types=1);

namespace App\Concerns;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pipeline\Pipeline;
use Spatie\Activitylog\ActivityLogger;
use Spatie\Activitylog\EventLogBag;
use Spatie\Activitylog\Traits\LogsActivity as SpatieLogsActivity;

trait LogsActivity
{
use SpatieLogsActivity;

protected static function bootLogsActivity(): void
{
// Hook into eloquent events that only specified in $eventToBeRecorded array,
// checking for "updated" event hook explicitly to temporary hold original
// attributes on the model as we'll need them later to compare against.

static::eventsToBeRecorded()->each(function ($eventName) {
if ($eventName === 'updated') {
static::updating(function (Model $model) {
$oldValues = (new static())->setRawAttributes($model->getRawOriginal());
$model->oldAttributes = static::logChanges($oldValues);
});
}

static::$eventName(function (Model $model) use ($eventName) {
$model->activitylogOptions = $model->getActivitylogOptions();

if (! $model->shouldLogEvent($eventName)) {
return;
}

$changes = $model->attributeValuesToBeLogged($eventName);

$description = $model->getDescriptionForEvent($eventName);

$logName = $model->getLogNameToUse();

// Submitting empty description will cause place holder replacer to fail.
if ($description == '') {
return;
}

if ($model->isLogEmpty($changes) && ! $model->activitylogOptions->submitEmptyLogs) {
return;
}

// User can define a custom pipelines to mutate, add or remove from changes
// each pipe receives the event carrier bag with changes and the model in
// question every pipe should manipulate new and old attributes.
$event = app(Pipeline::class)
->send(new EventLogBag($eventName, $model, $changes, $model->activitylogOptions))
->through(static::$changesPipes)
->thenReturn();

if ($eventName === 'updated' || $eventName === 'updating') {
foreach ($event->changes['attributes'] as $key => $value) {
static::actuallyLog($logName, $eventName, $model, [
$key => [
'old' => $event->changes['old'][$key],
'new' => $value,
],
], $description);
}
} else {
static::actuallyLog($logName, $eventName, $model, $event->changes, $description);
}

// Reset log options so the model can be serialized.
$model->activitylogOptions = null;
});
});
}

protected static function actuallyLog(?string $logName, string $eventName, Model $model, array $properties, string $description): void
{
// Actual logging
$logger = app(ActivityLogger::class)
->useLog($logName)
->event($eventName)
->performedOn($model)
->withProperties($properties);

if (method_exists($model, 'tapActivity')) {
$logger->tap([$model, 'tapActivity'], $eventName);
}

$logger->log($description);
}
}
89 changes: 89 additions & 0 deletions app/Concerns/LogsActivityForApproval.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

declare(strict_types=1);

namespace App\Concerns;

use App\Models\Activity;
use Spatie\Activitylog\ActivityLogger;
use Spatie\Activitylog\Traits\LogsActivity as SpatieLogsActivity;

trait LogsActivityForApproval
{
use SpatieLogsActivity;

public function saveForApproval(): void
{
$changes = collect($this->getDirty())
->map(fn ($value, $key) => [
'old' => $this->getOriginal($key),
'new' => $value,
]);

if ($changes->isEmpty()) {
return;
}

Activity::pendingChangesFor($this, $changes->keys()->all())->delete();

$eventName = 'updated';

$description = $this->getDescriptionForEvent($eventName) ?: $eventName;

// Actual logging
app(ActivityLogger::class)
->useLog('pending')
->event($eventName)
->performedOn($this)
->withProperties($changes)
->log($description);
}

public function tapActivity(Activity $activity, string $eventName)
{
// Flip properties
$properties = [];

foreach ($activity->properties->get('attributes', []) as $key => $value) {
$properties[$key] = [
'old' => data_get($activity->properties, "old.{$key}"),
'new' => $value,
];
}

if (! empty($properties)) {
$activity->properties = $properties;
}

if (auth()->guest()) {
return;
}

if (auth()->user()->isBbAdmin() || auth()->user()->isBbManager()) {
activity()->disableLogging();

if (! $activity->description) {
$activity->description = $this->getDescriptionForEvent($eventName);
}

$activity->properties->each(
fn ($values, $key) => $activity
->replicate()
->fill([
'properties' => [$key => $values],
'approved_at' => now(),
])
->save()
);
} else {
if (
$activity->properties->count() === 1 &&
property_exists($this, 'requiresApproval') &&
! \in_array($activity->properties->keys()->first(), $this->requiresApproval)
) {
$activity->log_name = 'auto_approved';
$activity->approved_at = now();
}
}
}
}
7 changes: 3 additions & 4 deletions app/Concerns/MustSetInitialPassword.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ protected static function bootMustSetInitialPassword(): void

static::created(function (self $user) {
if (! app()->runningInConsole()) {
if (!empty($user->created_by))
{
if (! empty($user->created_by)) {
$user->sendWelcomeNotification();
}
}
Expand All @@ -44,9 +43,9 @@ public function markPasswordAsSet(): bool

public function sendWelcomeNotification(): void
{
if ($this->role===UserRole::ngo_admin)
{
if ($this->role === UserRole::ngo_admin) {
$this->notify(new WelcomeNotification());

return;
}
$this->notify(new AdminWelcomeNotification());
Expand Down
2 changes: 2 additions & 0 deletions app/Enums/OrganizationStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ enum OrganizationStatus: string
case approved = 'active';
case rejected = 'disabled';

public const pending_changes = 'pending_changes';

protected function translationKeyPrefix(): ?string
{
return 'organization.status_arr';
Expand Down
1 change: 0 additions & 1 deletion app/Enums/UserRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,4 @@ public function translationKeyPrefix(): string
{
return 'user.roles';
}

}
58 changes: 58 additions & 0 deletions app/Filament/Forms/Components/Download.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace App\Filament\Forms\Components;

use Closure;
use Filament\Forms\Components\Component;
use Filament\Forms\Components\Concerns;
use Illuminate\Support\Collection;
use Spatie\MediaLibrary\MediaCollections\Models\Media;

class Download extends Component
{
use Concerns\HasHelperText;
use Concerns\HasHint;
use Concerns\HasName;

protected string $view = 'forms.components.download';

protected string | Closure $collectionName = 'default';

final public function __construct(string $collectionName)
{
$this->collectionName($collectionName);
$this->statePath($collectionName);
}

public static function make(string $collectionName): static
{
$static = app(static::class, ['collectionName' => $collectionName]);
$static->configure();

return $static;
}

public function collectionName(string | Closure $collectionName): static
{
$this->collectionName = $collectionName;

return $this;
}

public function getCollectionName(): string
{
return $this->evaluate($this->collectionName);
}

public function getDownloadItems(): Collection
{
return $this->getRecord()
->getMedia($this->getCollectionName())
->map(fn (Media $media) => [
'url' => $media->getFullUrl(),
'name' => $media->name . '.' . $media->extension,
]);
}
}
2 changes: 1 addition & 1 deletion app/Filament/Forms/Components/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Value extends Component
use Concerns\HasHint;
use Concerns\HasName;

protected string $view = 'components.forms.value';
protected string $view = 'forms.components.value';

protected bool $empty = false;

Expand Down
Loading

0 comments on commit 582156b

Please sign in to comment.