Created
January 11, 2019 13:24
-
-
Save scc-agnitio/1cd668c80de59beed52c93934f733e69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace App\Call; | |
| use App\User\User; | |
| use Carbon\Carbon; | |
| use App\Account\Account; | |
| use App\Contact\Contact; | |
| use App\Content\Content; | |
| use App\Core\Model\BaseModel; | |
| use App\Organization\Organization; | |
| use Illuminate\Database\Eloquent\SoftDeletes; | |
| use Prettus\Repository\Contracts\Transformable; | |
| use Prettus\Repository\Traits\TransformableTrait; | |
| use App\Call\Exception\CannotAssociateCallWithUserException; | |
| /** | |
| * Class Call | |
| * | |
| * @package App\Call | |
| * | |
| * @property integer id | |
| * @property integer organization_id | |
| * @property string activity_name | |
| * @property MeetingVersion currentVersion | |
| */ | |
| class Call extends BaseModel implements Transformable | |
| { | |
| use SoftDeletes, TransformableTrait; | |
| /** | |
| * Valid Call activity types | |
| */ | |
| const CALL_TYPES = ['f2f']; | |
| /** | |
| * @var string | |
| */ | |
| protected $table = 'Meeting'; | |
| /** | |
| * The attributes that aren't mass assignable. | |
| * | |
| * @var array | |
| */ | |
| protected $guarded = []; | |
| /** | |
| * The attributes that should be mutated to dates. | |
| * | |
| * @var array | |
| */ | |
| protected $dates = [ | |
| 'start_time', | |
| 'end_time', | |
| ]; | |
| /** | |
| * The accessors to append to the model's array form. | |
| * | |
| * @var array | |
| */ | |
| protected $appends = ['type']; | |
| /** | |
| * @param array $data | |
| * @return Call | |
| */ | |
| public static function createNew(array $data) { | |
| $call = new static([ | |
| 'organization_id' => $data['organization_id'], | |
| 'activity_name' => $data['type'], | |
| 'creator_id' => $data['creator_id'], | |
| ]); | |
| $call->save(); | |
| $versionData = array_only($data, ['source', 'source_date', 'start_time', 'end_time', 'notes', 'location', 'objective']); | |
| $version = MeetingVersion::makeInitial($versionData); | |
| $call->versions()->save($version); | |
| return $call; | |
| } | |
| /** | |
| * Update the Call model with allowed attributes only. | |
| * | |
| * @param array $attributes | |
| * @param array $options | |
| * @return self | |
| * @throws \Exception | |
| */ | |
| public function update(array $attributes = [], array $options = []): self | |
| { | |
| $versionData = array_only($attributes, ['version', 'start_time', 'end_time', 'notes', 'location', 'objective']); | |
| $version = $this->currentVersion->updateVersion($versionData); | |
| $this->versions()->save($version); | |
| return $this; | |
| } | |
| /** | |
| * Assign a call to a user | |
| * | |
| * @param User $user | |
| * @return Call | |
| */ | |
| public function assignToUser(User $user): self | |
| { | |
| if (! $user->organizations->contains($this->organization)) { | |
| throw new CannotAssociateCallWithUserException(sprintf("User id %d must belong to the same organization as the call.", $user->getId())); | |
| } | |
| $this->users()->save($user); | |
| $this->save(); | |
| return $this; | |
| } | |
| /** | |
| * @param array $accounts | |
| * @return $this | |
| */ | |
| public function syncAccounts(array $accounts) | |
| { | |
| $this->accounts()->sync($accounts); | |
| return $this; | |
| } | |
| /** | |
| * @param array $contacts | |
| * @return $this | |
| */ | |
| public function syncContacts(array $contacts) | |
| { | |
| $this->contacts()->sync($contacts); | |
| return $this; | |
| } | |
| /** | |
| * @param array $content | |
| * @return $this | |
| */ | |
| public function syncContent(array $content) | |
| { | |
| $this->content()->sync($content); | |
| return $this; | |
| } | |
| /* | |
| |-------------------------------------------------------------------------- | |
| | Setters | |
| |-------------------------------------------------------------------------- | |
| | | |
| */ | |
| /** | |
| * Set the call type. | |
| * | |
| * @param string $value | |
| * @return void | |
| */ | |
| public function setTypeAttribute($value) | |
| { | |
| $this->attributes['activity_name'] = strtolower($value); | |
| } | |
| /* | |
| |-------------------------------------------------------------------------- | |
| | Getters | |
| |-------------------------------------------------------------------------- | |
| | | |
| */ | |
| /** | |
| * Convert the model instance to an array. | |
| * | |
| * @return array | |
| */ | |
| public function toArray() | |
| { | |
| $array = parent::toArray(); | |
| unset($array['activity_name']); | |
| return $array; | |
| } | |
| /** | |
| * Get the call type. | |
| * | |
| * @return string | |
| */ | |
| public function getTypeAttribute() | |
| { | |
| return $this->attributes['activity_name']; | |
| } | |
| /** | |
| * @return int | |
| */ | |
| public function getId(): int | |
| { | |
| return $this->id; | |
| } | |
| /** | |
| * @return int | |
| */ | |
| public function getVersion(): int | |
| { | |
| return $this->currentVersion->getVersion(); | |
| } | |
| /** | |
| * @return string | |
| */ | |
| public function getSource(): string | |
| { | |
| return $this->currentVersion->getSource(); | |
| } | |
| /** | |
| * @return Carbon|null | |
| */ | |
| public function getSourceDate():? Carbon | |
| { | |
| return $this->currentVersion->getSourceDate(); | |
| } | |
| /** | |
| * @return int | |
| */ | |
| public function getOrganizationId(): int | |
| { | |
| return $this->organization_id; | |
| } | |
| /** | |
| * @return string | |
| */ | |
| public function getType(): string | |
| { | |
| return $this->activity_name; | |
| } | |
| /** | |
| * @return Carbon | |
| */ | |
| public function getStartTime(): Carbon | |
| { | |
| return $this->currentVersion->getStartTime(); | |
| } | |
| /** | |
| * @return Carbon | |
| */ | |
| public function getEndTime(): Carbon | |
| { | |
| return $this->currentVersion->end_time; | |
| } | |
| /** | |
| * @return int | |
| */ | |
| public function getCreatorId(): ?int | |
| { | |
| return $this->creator_id; | |
| } | |
| /** | |
| * @return string|null | |
| */ | |
| public function getNotes(): ?string | |
| { | |
| return $this->currentVersion->notes; | |
| } | |
| /** | |
| * @return string|null | |
| */ | |
| public function getLocation(): ?string | |
| { | |
| return $this->currentVersion->location; | |
| } | |
| /** | |
| * @return string|null | |
| */ | |
| public function getObjective(): ?string | |
| { | |
| return $this->currentVersion->objective; | |
| } | |
| /* | |
| |-------------------------------------------------------------------------- | |
| | Relations | |
| |-------------------------------------------------------------------------- | |
| | | |
| */ | |
| /** | |
| * @return \Illuminate\Database\Eloquent\Relations\HasMany | |
| */ | |
| public function versions() | |
| { | |
| return $this->hasMany(MeetingVersion::class, 'meeting_id'); | |
| } | |
| /** | |
| * @return \Illuminate\Database\Eloquent\Relations\HasOne | |
| */ | |
| public function currentVersion() | |
| { | |
| return $this->hasOne(MeetingVersion::class, 'meeting_id')->orderByDesc('version'); | |
| } | |
| /** | |
| * @return \Illuminate\Database\Eloquent\Relations\BelongsTo | |
| */ | |
| public function organization() | |
| { | |
| return $this->belongsTo(Organization::class); | |
| } | |
| /** | |
| * @return \Illuminate\Database\Eloquent\Relations\BelongsTo | |
| */ | |
| public function creator() | |
| { | |
| return $this->belongsTo(User::class); | |
| } | |
| /** | |
| * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | |
| */ | |
| public function accounts() | |
| { | |
| return $this->belongsToMany(Account::class, 'MeetingToAccount', 'meeting_id', 'account_id'); | |
| } | |
| /** | |
| * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | |
| */ | |
| public function contacts() | |
| { | |
| return $this->belongsToMany(Contact::class, 'MeetingToContact', 'meeting_id', 'contact_id'); | |
| } | |
| /** | |
| * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | |
| */ | |
| public function content() | |
| { | |
| return $this->belongsToMany(Content::class, 'MeetingToContent', 'meeting_id', 'content_id'); | |
| } | |
| /** | |
| * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany | |
| */ | |
| public function users() | |
| { | |
| return $this->belongsToMany(User::class, 'MeetingToUser', 'meeting_id', 'user_id'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment