コード例 #1
0
 public function actionCreate($post_type)
 {
     $model = new Post();
     $postType = $this->findPostType($post_type);
     $model->post_comment_status = Option::get('default_comment_status');
     if (!Yii::$app->user->can($postType->post_type_permission)) {
         throw new ForbiddenHttpException(Yii::t('content', 'You are not allowed to perform this action.'));
     }
     if ($model->load(Yii::$app->request->post())) {
         $model->post_type = $postType->id;
         $model->post_date = Yii::$app->formatter->asDatetime($model->post_date, 'php:Y-m-d H:i:s');
         if ($model->save()) {
             if ($termIds = Yii::$app->request->post('termIds')) {
                 foreach ($termIds as $termId) {
                     $termRelationship = new TermRelationship();
                     $termRelationship->term_id = $termId;
                     $termRelationship->post_id = $model->id;
                     if ($termRelationship->save() && ($term = $this->findTerm($termId))) {
                         $term->term_count++;
                         $term->save();
                     }
                 }
             }
             if ($meta = Yii::$app->request->post('meta')) {
                 foreach ($meta as $meta_name => $meta_value) {
                     $model->setMeta($meta_name, $meta_value);
                 }
             }
             Yii::$app->getSession()->setFlash('success', Yii::t('content', '{post_type} successfully saved.', ['post_type' => $postType->post_type_sn]));
             return $this->redirect(['update', 'id' => $model->id]);
         }
     }
     return $this->render('create', ['model' => $model, 'postType' => $postType]);
 }
コード例 #2
0
 /**
  * Create term taxonomy non-hierarchical
  */
 public function actionAjaxCreateHierarchical()
 {
     $term = new Term();
     $termRelationship = new TermRelationship();
     if ($term->load(Yii::$app->request->post())) {
         if ($termRelationship->load(Yii::$app->request->post()) && $termRelationship->post_id) {
             $term->term_count = 1;
             if ($term->save()) {
                 $termRelationship->term_id = $term->id;
                 if ($termRelationship->save()) {
                     echo '<br />';
                     echo Html::label(Html::checkbox('termIds', true, ['value' => $term->id, 'class' => 'new-i-check']) . ' ' . $term->term_name);
                 }
             }
         } else {
             if ($term->save()) {
                 echo '<br />';
                 echo Html::label(Html::checkbox('termIds', true, ['value' => $term->id]) . $term->term_name);
             }
         }
     }
 }
コード例 #3
0
ファイル: Term.php プロジェクト: fbarrento/yii2-content
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getTermRelationships()
 {
     return $this->hasMany(TermRelationship::className(), ['term_id' => 'id']);
 }
コード例 #4
0
 /**
  * Finds the TermRelationship model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  *
  * @param integer $post_id
  * @param integer $term_id
  *
  * @return TermRelationship the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($post_id, $term_id)
 {
     if (($model = TermRelationship::findOne(['post_id' => $post_id, 'term_id' => $term_id])) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }