public function beforeDelete() { //DebugBreak(); $tagposts = Tagpost::model()->findAll('post_id = :post_id', array(':post_id' => $this->post_id)); if (!empty($tagposts)) { foreach ($tagposts as $tagpost) { //delete all tags relations $tagpost->delete(); } } return parent::beforeDelete(); //run parent event handler... IT MUST BE!!! }
public function actionDelete() { if (Yii::app()->request->isPostRequest) { // we only allow deletion via POST request $criteria = new CDbCriteria(); $criteria->condition = 'tag_id=:tag_id'; $criteria->params = array(':tag_id' => $_GET['id']); $tagposts = Tagpost::model()->findAll($criteria); foreach ($tagposts as $tagpost) { $tagpost->delete(); } $model = $this->deleteLoadModel(); $model->delete(); // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser if (!Yii::app()->request->isAjaxRequest) { $this->redirect(array('/tag')); } } else { throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.'); } }
/** * action for update * * @param mixed $id */ public function actionUpdate($id = null) { Yii::import('application.modules.portfolio.widgets.PhotoPortfolio.PhotoPortfolioWidget'); $news = $this->loadModel($this->_modelclass, $id); $post = isset($news->post) ? $news->post : new Posting(); if ($post->scenario == 'insert') { $this->setPostType($post); if (empty($news->publication_date)) { $news->publication_date = CTimestamp::formatDate('Y-m-d H:i:s'); } } $portfolio = isset($news->post->portfolio) ? $news->post->portfolio : new Portfolio(); $portfolio->gallery_type = 1; if (!empty($_POST[$this->_modelclass])) { $post->attributes = $_POST['Posting']; //set attributes $news->attributes = $_POST[$this->_modelclass]; $posttitle = $post->title; //save name of posting to buffer $portfolioWidget = new PhotoPortfolioWidget(); $galleries = $portfolioWidget->validatePortfolio($_POST); if (!empty($galleries)) { $portfolio = $galleries[0]; } $post->validate(); $news->validate(); $success = !$post->hasErrors() && !$news->hasErrors() && is_object($portfolio) && !$portfolio->hasErrors(); if ($success && isset($post->tags)) { //search for deleted tags foreach ($post->tags as $tag) { if (!in_array($tag->tag_id, $post->postedTags)) { $post->deletedTags[] = $tag; } } } $strMessage = ($post->isNewRecord ? Yii::t('main', 'New article was created successfully') : Yii::t('main', 'Article was saved successfully')) . ' (' . Yii::t('main', 'Title') . ': ' . $posttitle . ')'; if ($success) { $transaction = Yii::app()->db->beginTransaction(); if ($success = $portfolio->savePortfolio()) { $post->gallery_id = $portfolio->gallery_id; if ($success = $post->save()) { $news->post_id = $post->post_id; if ($success = $news->save()) { //if exists deleted tags - process its foreach ($post->deletedTags as $tag) { $countDeleted = Tagpost::model()->deleteAll('tag_id = :tag_id AND post_id = :post_id', array(':tag_id' => $tag->tag_id, ':post_id' => $post->post_id)); if (!($success = $countDeleted > 0)) { break; } } foreach ($post->postedTags as $tag) { $exists = Tagpost::model()->exists('tag_id = :tag_id AND post_id = :post_id', array(':tag_id' => $tag, ':post_id' => $post->post_id)); if (!$exists) { $tagpost = new Tagpost(); $tagpost->tag_id = $tag; $tagpost->post_id = $post->post_id; $tagpost->save(); } } } } } if ($success) { $transaction->commit(); $resultMode = $success ? 'success' : 'error'; Yii::app()->user->setFlash($resultMode, $strMessage); //show flash message $this->redirect($this->createUrl('index')); } else { $transaction->rollback(); } } } $this->render('application.modules.news.views.default.form', array('news' => $news, 'post' => $post, 'portfolio' => $portfolio)); }