From c7ef3848ed8df74d6f4dd178c9d3710092a6b717 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Thu, 6 Oct 2022 20:57:38 +0200 Subject: [PATCH] Add vcard to member --- app/Member/Member.php | 71 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 70 insertions(+), 1 deletion(-) diff --git a/app/Member/Member.php b/app/Member/Member.php index ffac1fa7..79b29912 100644 --- a/app/Member/Member.php +++ b/app/Member/Member.php @@ -14,12 +14,14 @@ use App\Payment\Subscription; use App\Region; use App\Setting\NamiSettings; use App\Subactivity; +use Cviebrock\EloquentSluggable\Sluggable; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Notifications\Notifiable; +use Sabre\VObject\Component\VCard; use Zoomyboy\LaravelNami\Api; use Zoomyboy\LaravelNami\Data\MembershipEntry; @@ -35,10 +37,11 @@ class Member extends Model { use Notifiable; use HasFactory; + use Sluggable; public $guarded = []; - public static array $namiFields = ['firstname', 'lastname', 'joined_at', 'birthday', 'send_newspaper', 'address', 'zip', 'location', 'nickname', 'other_country', 'further_address', 'main_phone', 'mobile_phone', 'work_phone', 'fax', 'email', 'email_parents', 'gender_id', 'confession_id', 'region_id', 'country_id', 'fee_id', 'nationality_id']; + public static array $namiFields = ['firstname', 'lastname', 'joined_at', 'birthday', 'send_newspaper', 'address', 'zip', 'location', 'nickname', 'other_country', 'further_address', 'main_phone', 'mobile_phone', 'work_phone', 'fax', 'email', 'email_parents', 'gender_id', 'confession_id', 'region_id', 'country_id', 'fee_id', 'nationality_id', 'slug']; public $dates = ['try_created_at', 'joined_at', 'birthday']; @@ -59,6 +62,13 @@ class Member extends Model 'is_leader' => 'boolean', ]; + public function sluggable(): array + { + return [ + 'slug' => ['source' => ['firstname', 'lastname']], + ]; + } + public function scopeSearch(Builder $q, ?string $text): Builder { if (is_null($text)) { @@ -93,6 +103,24 @@ class Member extends Model return $this->firstname.' '.$this->lastname; } + public function getPreferredPhoneAttribute(): ?string + { + if ($this->mobile_phone) { + return $this->mobile_phone; + } + + if ($this->main_phone) { + return $this->main_phone; + } + + return null; + } + + public function getEtagAttribute(): string + { + return $this->updated_at->timestamp.'_'.$this->version; + } + public function getFullAddressAttribute(): string { return $this->address.', '.$this->zip.' '.$this->location; @@ -324,4 +352,45 @@ class Member extends Model ->where('activities.is_try', true), ]); } + + public function toVcard(): VCard + { + $card = new VCard([ + 'FN' => $this->fullname, + 'TEL' => $this->mobile_phone, + 'N' => [$this->lastname, $this->firstname, '', '', ''], + 'BDAY' => $this->birthday->format('Ymd'), + ]); + + if ($this->preferred_phone) { + $card->add('TEL', $this->preferred_phone, ['type' => 'pref,voice']); + } + + if ($this->mobile_phone) { + $card->add('TEL', $this->mobile_phone, ['type' => 'cell']); + } + + if ($this->work_phone) { + $card->add('TEL', $this->work_phone, ['type' => 'work']); + } + + if ($this->email) { + $card->add('EMAIL', $this->email, ['type' => 'pref']); + } + if ($this->email_parents) { + $card->add('EMAIL', $this->email_parents, ['type' => 'internet']); + } + + $card->add('ADDR', [ + $this->fullname, + $this->fullAddress, + $this->address ?: '', + $this->location ?: '', + $this->region?->name ?: '', + $this->zip ?: '', + $this->country?->name ?: '', + ], ['type' => 'postal']); + + return $card; + } }