public function executeRemove() { $this->validAjaxRequest(); $res = new \AjaxResponse(); if (!($postImg = \PostImages::retrieveById($this->request()->post('id')))) { $res->type = \AjaxResponse::ERROR; $res->message = t("Image not found"); return $this->renderText($res->toString()); } if ($postImg->delete()) { $otherImages = []; if (($otherImages = \PostPeer::getPostImg($postImg->getPostId())) && $postImg->getIsMain()) { $otherImages[0]->setIsMain(true); $otherImages[0]->save(); } $result = []; foreach ($otherImages as $image) { $t = $image->toArray(); $t['thumb_url'] = $image->getThumbs(96, 96); $t['url'] = $image->getUrl(); $result[] = $t; } $res->images = $result; $res->postImg = $postImg->toArray(); $res->type = \AjaxResponse::SUCCESS; return $this->renderText($res->toString()); } $res->type = \AjaxResponse::ERROR; $res->message = t("Unknown error"); return $this->renderText($res->toString()); }
/** * @param $postId * @return \PostImages[] | null */ public static function getPostImg($postId) { return \PostImages::select()->where('`post_id` = :post_id')->setParameter(':post_id', $postId, \PDO::PARAM_INT)->orderBy('ordering')->execute(); }
protected function _afterDelete() { parent::_afterDelete(); \PostImages::write()->delete(PostImages::getTableName())->where('`post_id`=:post_id')->setParameter(':post_id', $this->getId(), \PDO::PARAM_INT)->execute(); \PostCustomFields::write()->delete(PostCustomFields::getTableName())->where('`post_id`=:post_id')->setParameter(':post_id', $this->getId(), \PDO::PARAM_INT)->execute(); \PostAttachments::write()->delete(PostAttachments::getTableName())->where('`post_id`=:post_id')->setParameter(':post_id', $this->getId(), \PDO::PARAM_INT)->execute(); \PostProperty::write()->delete(PostProperty::getTableName())->where('`post_id`=:post_id')->setParameter(':post_id', $this->getId(), \PDO::PARAM_INT)->execute(); }