File: /home/imensosw/www/mpl.imenso.co/app/Models/Notification.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
class Notification extends Model
{
public function user()
{
return $this->belongsTo(\App\Models\User::class);
}
public static function addNew($data)
{
$notification = new self;
$notification->content = self::parseNotification($data['content']);
$notification->user_id = $data['user_id'];
$notification->type_id = $data['type_id'];
$notification->save();
return $notification;
}
public static function parseNotification($value)
{
$matches = [];
preg_match_all('/\{\!(.*?)\!\}/', $value, $matches);
foreach ($matches[0] as $match) {
$match_start = $match;
$match = str_replace('{!', '', $match);
$match = str_replace('!}', '', $match);
$match_array = explode('::', $match);
if (strpos($match_array[2], '()')) {
$match_array[2] = str_replace('()', '', $match_array[2]);
if ($match_array[0]::where('id', $match_array[1])->count() == 0) {
return '';
} else {
$replace = $match_array[0]::findOrFail($match_array[1])->{$match_array[2]}();
}
} else {
if ($match_array[0]::where('id', $match_array[1])->count() == 0) {
return '';
} else {
$replace = $match_array[0]::findOrFail($match_array[1])->{$match_array[2]};
}
$replace = $match_array[0]::findOrFail($match_array[1])->{$match_array[2]};
}
$value = str_replace($match_start, $replace, $value);
}
return $value;
}
// public function getContentAttribute($value)
// {
// return self::parseNotification($value);
// }
public function markAsDismissed()
{
$this->dismissed = 1;
$this->dismissed_at = new \Carbon\Carbon;
$this->save();
}
public function markAsActioned()
{
$this->actioned = 1;
$this->save();
}
public function getActions()
{
switch ($this->type_id) {
//Global Promoter Joins MPL
case 104:
if ($this->getData('promoter')) {
return '<a
onclick="actionNotification('.$this->id.', \''.url('venue/give-promoter-all-avails/'.$this->getData('promoter')).'\')"
href="javascript: void(0);"
class="btn primary action">
Share all availabilities
</a>';
}
break;
//Artist Recommends You
case 201:
if ($this->getData('recommender_id')) {
if (! $this->user->linkedToArtist($this->getData('recommender_id'))) {
return '<a onclick="actionNotification('.$this->id.', \''.url('add-artist-artist-connection/'.$this->user->id.'/'.$this->getData('recommender_id')).'\')" href="javascript: void(0);" class="btn primary action">Recommend them back</a>';
}
}
break;
//Fan Friend Requests You
case 301:
if ($this->getData('requester_id')) {
if (! $this->user->isConnected(User::findOrFail($this->getData('requester_id')), '', 1)) {
return '<a href="javascript:void(0);" onclick="actionNotification('.$this->id.', \''.url('approve-member-member-connection/'.User::find($this->getData('requester_id')))->id.'/'.$this->user->id.'\');" class="btn primary action">Accept</a>';
}
}
break;
//Artist You Follow Recommends an Artist
case 305:
$html = '';
if ($this->getData('recommendee_id')) {
$recomendee = User::find($this->getData('recommendee_id'));
if (! $this->user->linkedToArtist($recomendee->id) && ! $this->dismissed) {
$html .= '<p>Do you want to follow them?</p>';
$html .= '<a href="javascript:void(0);" onclick="actionNotification('.$this->id.', \''.url('add-member-artist-connection/'.$this->user->id.'/'.$recomendee->id).'\');" class="btn primary action">Yes</a>';
$html .= '<a href="javascript:void(0);" onclick="actionNotification('.$this->id.', \''.url('ignore-checkout-artist/'.$this->user->id.'/'.$recomendee->id).'\');" class="btn primary action">No</a>';
}
}
return $html;
break;
}
return false;
}
public function getData($key)
{
if (NotificationData::where([
'field_name' => $key,
'notification_id' => $this->id,
])->count() > 0) {
$data = NotificationData::where([
'field_name' => $key,
'notification_id' => $this->id,
])->first();
return $data->value;
} else {
return false;
}
}
public function addData($data)
{
foreach ($data as $key => $value) {
if ($this->getData($key) === false) {
$notification_data = NotificationData::addNew([
'field_name' => $key,
'value' => $value,
'notification_id' => $this->id,
]);
} else {
$notification_data = NotificationData::where([
'field_name' => $key,
'notification_id' => $this->id,
])->first();
$notification_data->update([
'value' => $value,
]);
}
}
}
public function remove()
{
DB::table('notification_data')->where([
'notification_id' => $this->id,
])->delete();
$this->delete();
}
}