blue-twilight/app/PhotoComment.php

91 lines
2.1 KiB
PHP
Raw Permalink Normal View History

<?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 authorEmail()
{
return is_null($this->createdBy) ? $this->email : $this->createdBy->email;
}
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_at) && is_null($this->rejected_at));
}
public function isModerated()
{
return (!is_null($this->approved_at) || !is_null($this->rejected_at));
}
public function isRejected()
{
return (!is_null($this->rejected_at) && is_null($this->approved_at));
}
public function parent()
{
return $this->belongsTo(PhotoComment::class, 'parent_comment_id');
}
public function photo()
{
return $this->belongsTo(Photo::class);
}
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);
}
}