2018-09-17 14:15:06 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
class PhotoComment extends Model
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $fillable = [
|
2018-09-19 09:44:20 +01:00
|
|
|
'name',
|
|
|
|
'email',
|
|
|
|
'comment'
|
2018-09-17 14:15:06 +01:00
|
|
|
];
|
2018-09-17 22:30:27 +01:00
|
|
|
|
2018-09-19 09:44:20 +01:00
|
|
|
public function authorDisplayName()
|
|
|
|
{
|
|
|
|
return is_null($this->createdBy) ? $this->name : $this->createdBy->name;
|
|
|
|
}
|
|
|
|
|
2018-10-05 21:08:14 +01:00
|
|
|
public function authorEmail()
|
|
|
|
{
|
|
|
|
return is_null($this->createdBy) ? $this->email : $this->createdBy->email;
|
|
|
|
}
|
|
|
|
|
2018-09-19 09:44:20 +01:00
|
|
|
public function children()
|
|
|
|
{
|
|
|
|
return $this->hasMany(PhotoComment::class, 'parent_comment_id');
|
|
|
|
}
|
|
|
|
|
2018-09-17 22:30:27 +01:00
|
|
|
public function createdBy()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(User::class, 'created_user_id');
|
|
|
|
}
|
2018-09-18 10:19:47 +01:00
|
|
|
|
|
|
|
public function depth()
|
|
|
|
{
|
|
|
|
$depth = 0;
|
|
|
|
$current = $this;
|
|
|
|
|
|
|
|
while (!is_null($current->parent))
|
|
|
|
{
|
|
|
|
$current = $current->parent;
|
|
|
|
$depth++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $depth;
|
|
|
|
}
|
|
|
|
|
2018-09-19 19:54:59 +01:00
|
|
|
public function isApproved()
|
|
|
|
{
|
2018-09-19 20:35:43 +01:00
|
|
|
return (!is_null($this->approved_at) && is_null($this->rejected_at));
|
2018-09-19 19:54:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isModerated()
|
|
|
|
{
|
2018-09-19 20:35:43 +01:00
|
|
|
return (!is_null($this->approved_at) || !is_null($this->rejected_at));
|
2018-09-19 19:54:59 +01:00
|
|
|
}
|
|
|
|
|
2018-10-05 22:57:39 +01:00
|
|
|
public function isRejected()
|
|
|
|
{
|
|
|
|
return (!is_null($this->rejected_at) && is_null($this->approved_at));
|
|
|
|
}
|
|
|
|
|
2018-09-18 10:19:47 +01:00
|
|
|
public function parent()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(PhotoComment::class, 'parent_comment_id');
|
|
|
|
}
|
2018-09-19 09:44:20 +01:00
|
|
|
|
2018-09-21 15:00:07 +01:00
|
|
|
public function photo()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Photo::class);
|
|
|
|
}
|
|
|
|
|
2018-09-19 09:44:20 +01:00
|
|
|
public function textAsHtml()
|
|
|
|
{
|
|
|
|
$start = '<p>';
|
|
|
|
$end = '</p>';
|
|
|
|
$isHtml = (
|
|
|
|
strlen($this->comment) > (strlen($start) + strlen($end)) && // text contains both our start + end string
|
|
|
|
strtolower(substr($this->comment, 0, strlen($start))) == strtolower($start) && // text starts with our start string
|
|
|
|
strtolower(substr($this->comment, strlen($this->comment) - strlen($end))) == strtolower($end) // text ends with our end string
|
|
|
|
);
|
|
|
|
|
|
|
|
return $isHtml ? $this->comment : sprintf('<p>%s</p>', $this->comment);
|
|
|
|
}
|
2018-09-17 14:15:06 +01:00
|
|
|
}
|