Claim Form * @copyright Tamaranga */ class ClaimBlock extends Form { /** @var int item id */ public $itemId; /** @var bool item owner is current user */ public $itemOwner = false; /** @var string captcha key */ public $captcha_key = 'listings-item-claim'; public function init() { parent::init(); $this->setTemplate('item/claim', 'listings'); $this->setKey('view.claim'); $this->setTitle(_t('listings', 'View Claim')); } public function data() { $data = parent::data(); if (! $this->isBlockVisible()) { return false; } $claims = $this->claims(); $data['claims'] = $claims; $data['reasons'] = $claims->getReasons($this->language); $data['reason_other_id'] = $claims::WITH_MESSAGE; $data['captcha_on'] = $this->hasCaptcha(); return $data; } public function isBlockVisible() { if (! $this->itemId) { return false; } if ($this->itemOwner) { return false; } return true; } public function hasCaptcha() { return ! $this->request->user(); } public function onClaimAction() { do { if (! $this->isBlockVisible() || ! $this->isRequestValid()) { $this->errors->reloadPage(); break; } if ($this->hasCaptcha()) { if (! $this->isCaptchaValid($this->captcha_key)) { break; } } $success = $this->claims()->add( $this->itemId, $this->request->userId(), $this->request->post('reason', TYPE_ARRAY_UINT), $this->request->post('comment', [TYPE_TEXT, 'len' => 1000]), [ 'throttle' => true, ] ); if ($success) { $this->respond('success_message', _t('item-claim', 'The complaint was successfully accepted')); } } while (false); return $this->getActionResponse(); } public function claims() { return Listings::itemClaims(); } }