blue-twilight/app/PhotoComment.php

84 lines
2.0 KiB
PHP

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class PhotoComment extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'comment'
];
public function authorDisplayName()
{
return is_null($this->createdBy) ? $this->name : $this->createdBy->name;
}
public function children()
{
return $this->hasMany(PhotoComment::class, 'parent_comment_id');
}
public function createdBy()
{
return $this->belongsTo(User::class, 'created_user_id');
}
public function depth()
{
$depth = 0;
$current = $this;
while (!is_null($current->parent))
{
$current = $current->parent;
$depth++;
}
return $depth;
}
public function isApproved()
{
return (
!is_null($this->approved_user_id) &&
!is_null($this->approved_at) &&
is_null($this->rejected_user_id) &&
is_null($this->rejected_at)
);
}
public function isModerated()
{
return (
(!is_null($this->approved_user_id) && !is_null($this->approved_at)) ||
(!is_null($this->rejected_user_id) && !is_null($this->rejected_at))
);
}
public function parent()
{
return $this->belongsTo(PhotoComment::class, 'parent_comment_id');
}
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);
}
}