--- /dev/null
+<?php
+
+namespace App\Nova\Actions;
+
+use App\Organization;
+use App\User;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Support\Collection;
+use Laravel\Nova\Actions\Action;
+use Laravel\Nova\Actions\DestructiveAction;
+use Laravel\Nova\Fields\ActionFields;
+
+class SetOrganizationMembersType extends DestructiveAction
+{
+ use InteractsWithQueue, Queueable;
+
+
+ protected $type;
+ protected $action;
+
+ public $showOnTableRow = true;
+
+ public function __construct(int $type)
+ {
+ $this->type = $type;
+
+ $this->action = "Action inconnue";
+ $more = "";
+
+ switch($this->type) {
+ case User::TYPE_PROSPECT:
+ $this->action = "prospection";
+ break;
+ case User::TYPE_DISCOVER:
+ $this->action = "découverte";
+ $more = "Cette action n'activera pas de période découverte.";
+ break;
+ }
+
+ $this->name = "Passer en $this->action";
+
+ $this->confirmText = "Êtes vous sur de vouloir passer tous les membres de cette organisation en $this->action ? Cela désactivera l'abonnement pour tous les membres. ".$more;
+
+
+
+ }
+
+
+
+ /**
+ * Perform the action on the given models.
+ *
+ * @param \Laravel\Nova\Fields\ActionFields $fields
+ * @param \Illuminate\Support\Collection $models
+ * @return mixed
+ */
+ public function handle(ActionFields $fields, Collection $models)
+ {
+ $models->map(function(Organization $orga) {
+ $orga->update(['subscription_active' => 0]);
+
+ $orga->members()->update(['type' => $this->type]);
+ });
+
+ return Action::message("Membres passés en $this->action avec succès");
+ }
+
+ /**
+ * Get the fields available on the action.
+ *
+ * @return array
+ */
+ public function fields()
+ {
+ return [];
+ }
+}
namespace App\Nova\Actions;
+use App\Organization;
+use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Collection;
use Laravel\Nova\Actions\Action;
use Laravel\Nova\Fields\ActionFields;
+use Laravel\Nova\Fields\Boolean;
class SetSubscriptionStatus extends Action
{
private $setActive;
public $showOnTableRow = true;
- public $withoutConfirmation = true;
+// public $withoutConfirmation = true;
public function __construct($setActive)
{
{
$models->map->update(['subscription_active' => $this->setActive]);
+ if($this->setActive) {
+ $models->map(function (Organization $orga) use($fields){
+
+ /** @see User::prepareForSubscription() */
+ $orga->members()->update([
+ 'discover_ends_at' => null,
+ 'trial_ends_at' => null,
+ 'type' => User::TYPE_SUBSCRIBER,
+ ]);
+
+ if(! $fields->dont_notify) {
+ $orga->members->map->sendSubscribedNotification();
+ }
+ });
+ }
+
+
+
$message = $this->setActive ?
"Abonnement activé avec succès" :
"Abonnement désactivé avec succès";
*/
public function fields()
{
- return [];
+ return $this->setActive ? [
+ Boolean::make("Ne pas notifier", 'dont_notify')
+ ] : [];
}
}
$user->organization()->associate(\App\Organization::query()->findOrFail($fields->org_id));
$user->save();
if(! $fields->dont_notify) {
- $template = $user->reg_complete ?
- 'just_subscribed_reg_ok' :
- 'just_subscribed_need_reg';
- $user->sendEmailFromTemplate($template);
+ $user->sendSubscribedNotification();
}
});
use App\Nova\Actions\SendCustomNotification;
use App\Nova\Actions\SendNotification;
+use App\Nova\Actions\SetOrganizationMembersType;
use App\Nova\Actions\SetSubscriptionStatus;
use App\Nova\Filters\SubscriptionStatusFilter;
use App\Nova\Filters\TypeFilter;
public function fields(Request $request)
{
return [
- ID::make()->sortable(),
+// ID::make()->sortable(),
Text::make('Nom', 'name'),
HasMany::make('Membres', 'members', User::class),
BelongsTo::make('Type', 'type', OrganizationType::class),
{
return array_merge([
new SetSubscriptionStatus(true),
- new SetSubscriptionStatus(false),
+ new SetOrganizationMembersType(\App\User::TYPE_PROSPECT),
new SendNotification,
+ new SetOrganizationMembersType(\App\User::TYPE_DISCOVER),
+ new SetSubscriptionStatus(false),
], SendCustomNotification::getActions());
}
}
$this->update(['unsubscribed' => 1]);
}
+ /**
+ * Prepares model for subscription (disables trial, discover, sets type)
+ */
+ public function prepareForSubscription()
+ {
+ $this->update([
+ 'discover_ends_at' => null,
+ 'trial_ends_at' => null,
+ 'type' => self::TYPE_SUBSCRIBER,
+ ]);
+ }
+
+ /**
+ * Sends correct notification to a just subscribed user
+ */
+ public function sendSubscribedNotification()
+ {
+ $template = $this->reg_complete ?
+ 'just_subscribed_reg_ok' :
+ 'just_subscribed_need_reg';
+ $this->sendEmailFromTemplate($template);
+ }
/**
* @return MailgunValidation