75 lines
2.3 KiB
PHP
75 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace RomhackPlaza\Master\Entity;
|
|
|
|
use XF\Mvc\Entity\Entity;
|
|
use XF\Mvc\Entity\Structure;
|
|
|
|
class ClubRequest extends Entity
|
|
{
|
|
public static function getStructure(Structure $structure): Structure
|
|
{
|
|
$structure->table = 'xf_club_request';
|
|
$structure->shortName = 'RomhackPlaza\Master:ClubRequest';
|
|
$structure->primaryKey = 'request_id';
|
|
$structure->columns = [
|
|
'request_id' => ['type' => self::UINT, 'autoIncrement' => true],
|
|
'user_id' => ['type' => self::UINT, 'required' => true],
|
|
'title' => ['type' => self::STR, 'maxLength' => 100, 'required' => true],
|
|
'description' => ['type' => self::STR, 'required' => true],
|
|
'request_state' => ['type' => self::STR, 'default' => 'moderated',
|
|
'allowedValues' => ['visible', 'moderated', 'rejected']
|
|
],
|
|
'request_date' => ['type' => self::UINT, 'default' => \XF::$time]
|
|
];
|
|
$structure->relations = [
|
|
'User' => [
|
|
'entity' => 'XF:User',
|
|
'type' => self::TO_ONE,
|
|
'conditions' => 'user_id',
|
|
'primary' => true
|
|
]
|
|
];
|
|
|
|
return $structure;
|
|
}
|
|
|
|
protected function _postSave()
|
|
{
|
|
if( $this->isInsert() && $this->request_state == 'moderated' ){
|
|
$this->inQueue();
|
|
} else if( $this->isUpdate() && $this->isChanged('request_state') ){
|
|
if( $this->request_state === 'moderated' ){
|
|
$this->inQueue();
|
|
} else {
|
|
$this->removeQueue();
|
|
}
|
|
}
|
|
}
|
|
|
|
protected function _postDelete()
|
|
{
|
|
if( $this->request_state === 'moderated' ){
|
|
$this->removeQueue();
|
|
}
|
|
}
|
|
|
|
protected function inQueue()
|
|
{
|
|
$queue = $this->em()->find('XF:ApprovalQueue', ['club_request', $this->request_id ] );
|
|
if( !$queue )
|
|
{
|
|
$newQueue = $this->em()->create('XF:ApprovalQueue');
|
|
$newQueue->content_type = 'club_request';
|
|
$newQueue->content_id = $this->request_id;
|
|
$newQueue->save();
|
|
}
|
|
}
|
|
|
|
protected function removeQueue()
|
|
{
|
|
$queue = $this->em()->find('XF:ApprovalQueue', ['club_request', $this->request_id ] );
|
|
if( $queue )
|
|
$queue->delete();
|
|
}
|
|
} |