Exemplo n.º 1
0
 /**
  * Creates a new Post model.
  * For ajax request will return json object
  * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $request = Yii::$app->request;
     $model = new Post();
     if ($request->isAjax) {
         /*
          *   Process for ajax request
          */
         Yii::$app->response->format = Response::FORMAT_JSON;
         if ($request->isGet) {
             return ['code' => '200', 'message' => 'OK', 'data' => $this->renderPartial('create', ['model' => $model])];
         } else {
             if ($model->load($request->post()) && $model->save()) {
                 return ['code' => '200', 'message' => 'Create Post success'];
             } else {
                 return ['code' => '400', 'message' => 'Validate error', 'data' => $this->renderPartial('create', ['model' => $model])];
             }
         }
     } else {
         /*
          *   Process for non-ajax request
          */
         if ($model->load($request->post()) && $model->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         } else {
             return $this->render('create', ['model' => $model]);
         }
     }
 }
Exemplo n.º 2
0
 /**
  * Creates a new Post model.
  * If creation is successful, the browser will be redirected to the 'update' page.
  *
  * @param integer $post_type post_type_id
  *
  * @throws \yii\web\ForbiddenHttpException
  * @throws \yii\web\NotFoundHttpException
  * @return mixed
  */
 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('writesdown', '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->setAttributes(['term_id' => $termId, 'post_id' => $model->id]);
                     if ($termRelationship->save() && ($term = $this->findTerm($termId))) {
                         $term->updateAttributes(['term_count' => $term->term_count++]);
                     }
                 }
             }
             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('writesdown', '{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]);
 }
Exemplo n.º 3
0
 /**
  * Creates a new Post model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Post();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
 /**
  * Creates a new Post model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Post();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         $redirect = yii::$app->request->post('goto') == 'list' ? ['index'] : ['update', 'id' => $model->id];
         return $this->redirect($redirect);
     }
     return $this->render('create', ['model' => $model]);
 }
Exemplo n.º 5
0
 /**
  * Создание поста.
  * @return string|Response
  */
 public function actionCreate()
 {
     $model = new Post();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         $model->author_id = Yii::$app->user->id;
         return $this->render('create', ['model' => $model, 'category' => Category::find()->all(), 'tags' => Tags::find()->all(), 'authors' => User::find()->all()]);
     }
 }
Exemplo n.º 6
0
 /**
  * Creates a new Post model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Post();
     //$authot = new User();
     //$authot->attributes='selected';
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'category' => Category::find()->all(), 'autor' => User::find()->all()]);
     }
 }
Exemplo n.º 7
0
 /**
  * Creates a new Post model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Post();
     $user_id = yii::$app->getUser()->id;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         //return $this->redirect(['view', 'model' => $model]);
         return $this->redirect(['index', 'user_id' => $user_id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Exemplo n.º 8
0
 /**
  * Creates a new Post model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Post();
     $categoryModel = new CategoryPost();
     $treeParents = TreeHelper::build($categoryModel->find()->addOrderBy('tree')->addOrderBy('lft')->all());
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         return $this->redirect(['index']);
     } else {
         return $this->render('create', ['model' => $model, 'treeParents' => $treeParents]);
     }
 }
Exemplo n.º 9
0
 /**
  * Creates a new Post model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Post();
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         foreach (Category::getCategoriesById($model->categories_id) as $category) {
             $model->link('categories', $category);
         }
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Exemplo n.º 10
0
 /**
  * Creates a new Post model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Post();
     if ($model->load(Yii::$app->request->post())) {
         $model->created_at = time();
         $model->updated_at = time();
         if ($model->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         }
     } else {
         return $this->render('create', ['model' => $model, 'category' => Category::find()->all()]);
     }
 }
Exemplo n.º 11
0
 /**
  * Creates a new Post model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Post();
     $model->type = 'post';
     $model->user_id = \Yii::$app->user->id;
     if ($model->load(Yii::$app->request->post())) {
         $model->category_id = implode(',', $model->category_id);
         if ($model->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         }
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }
Exemplo n.º 12
0
 public function actionAddPost()
 {
     $post = new Post();
     $user = Yii::$app->user->identity;
     $post->load(Yii::$app->request->post(), '');
     $post->user_id = $user->id;
     $post->img = UploadedFile::getInstanceByName('image');
     if ($post->save()) {
         if ($post->img) {
             $post->saveImage();
         }
         return $post;
     } else {
         return $post->getErrors();
     }
 }
Exemplo n.º 13
0
 /**
  * Creates a new Content model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Post();
     $model->allowComment = true;
     $model->allowFeed = true;
     $model->allowPing = true;
     if (Yii::$app->request->isPost) {
         if ($model->load(Yii::$app->request->post())) {
             $model->inputCategories = Yii::$app->request->post('inputCategories', []);
             $model->inputTags = Yii::$app->request->post('inputTags', []);
             $model->inputAttachments = Yii::$app->request->post('inputAttachments', []);
             if ($model->save()) {
                 return $this->redirect(['index']);
             }
         }
     }
     return $this->render('create', ['model' => $model]);
 }
Exemplo n.º 14
0
 /**
  * Creates a new Post model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Post();
     $parents = [];
     $root = Category::findOne(['parent_id' => null, 'title' => 'post', 'module' => 'post']);
     if (empty($root)) {
         throw new NotFoundHttpException('Module không tồn tại');
     }
     $parents = $root->children()->all();
     $parent_id = $this->buildTree($parents);
     if ($model->load(Yii::$app->request->post())) {
         $model['slug'] = $this->slugAlias($model);
         if ($model->save()) {
             return $this->redirect(['index']);
         }
     } else {
         return $this->render('create', ['model' => $model, 'parent_id' => $parent_id]);
     }
 }
Exemplo n.º 15
0
 /**
  * Creates a new Post model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Post();
     $model->user_id = Yii::$app->user->id;
     if ($model->load(Yii::$app->request->post())) {
         $postData = Yii::$app->request->post('Post');
         if (!empty($postData['categories'])) {
             foreach ($postData['categories'] as $categoryId) {
                 $category = Category::find()->where(['id' => $categoryId])->one();
                 $model->link('categories', $category);
             }
         }
         $model->setScenario('create');
         $model->poster = \yii\web\UploadedFile::getInstance($model, 'poster');
         if ($model->save() && $model->upload()) {
             return $this->redirect(['view', 'id' => $model->id]);
         }
     }
     return $this->render('create', ['model' => $model]);
 }
Exemplo n.º 16
0
 /**
  * Creates a new Post model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new Post();
     $model->tags = [];
     // default values
     $model->allow_comment = 1;
     $model->is_public = 1;
     $model->content_category_id = Post::CATEGORY_NEWS;
     $model->user_id = Yii::$app->user->id;
     $model->is_pin = 0;
     $model->with_photo = 0;
     $model->with_video = 0;
     $matchModel = new \common\models\MatchSearch();
     $relation = new Relation();
     $relation->relationable_type = Relation::RELATIONABLE_POST;
     $matches = $matchModel::find()->orderBy(['date' => SORT_DESC])->limit(10)->all();
     $matchesList = [];
     foreach ($matches as $match) {
         $matchDate = date('d.m.Y', strtotime($match->date));
         $matchesList[$match->id] = $match->name . ' (' . $matchDate . ')';
     }
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         // Save the model to have a record number
         if (!$model->save()) {
             return $this->render('create', ['model' => $model]);
         }
         // Adding new tags
         if (is_array($model->tags)) {
             foreach ($model->tags as $id) {
                 $model->addTag($id);
             }
         }
         $cached_tag_list = [];
         $newTags = $model->getTags();
         foreach ($newTags as $newTag) {
             $cached_tag_list[] = $newTag->name;
         }
         $model->cached_tag_list = implode(', ', $cached_tag_list);
         // Set image
         $uploadedFile = UploadedFile::getInstance($model, 'image');
         if ($uploadedFile) {
             // Save origionals
             $asset = new Asset();
             $asset->assetable_type = Asset::ASSETABLE_POST;
             $asset->assetable_id = $model->id;
             $asset->uploadedFile = $uploadedFile;
             $asset->saveAsset();
             // Save thumbnails
             $imageID = $asset->id;
             $thumbnails = Asset::getThumbnails(Asset::ASSETABLE_POST);
             foreach ($thumbnails as $thumbnail) {
                 $asset = new Asset();
                 $asset->parent_id = $imageID;
                 $asset->thumbnail = $thumbnail;
                 $asset->assetable_type = Asset::ASSETABLE_POST;
                 $asset->assetable_id = $model->id;
                 $asset->uploadedFile = $uploadedFile;
                 $asset->saveAsset();
             }
         }
         $relation->relationable_id = $model->id;
         if ($relation->load(Yii::$app->request->post()) && $model->validate()) {
             if ($relation->parent_id != '' && is_array($relation->parent_id)) {
                 $relation->parent_id = $relation->parent_id[0];
             }
             if ($relation->parent_id && is_numeric($relation->parent_id)) {
                 $relation->save();
             }
         }
         $model->save();
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('create', ['model' => $model, 'relation' => $relation, 'matchModel' => $matchModel, 'matchesList' => $matchesList]);
     }
 }
Exemplo n.º 17
0
 /**
  * Url: /post/add
  * @return mixed
  * @throws ForbiddenHttpException
  */
 public function actionPostAdd()
 {
     if (Yii::$app->user->isGuest) {
         throw new ForbiddenHttpException("Вы не можете выполнить это действие.");
     }
     $model = new Post();
     $model->content_category_id = Post::CATEGORY_BLOG;
     $model->tags = [];
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         $model->allow_comment = 1;
         $model->is_public = 1;
         $model->content_category_id = Post::CATEGORY_BLOG;
         $model->user_id = Yii::$app->user->id;
         // Set slug
         $model->slug = $model->genSlug($model->title);
         // Save the model to have a record number
         if ($model->save()) {
             // Adding new tags
             if (is_array($model->tags)) {
                 foreach ($model->tags as $id) {
                     $model->addTag($id);
                 }
             }
             $cached_tag_list = [];
             $newTags = $model->getTags();
             foreach ($newTags as $newTag) {
                 $cached_tag_list[] = $newTag->name;
             }
             $model->cached_tag_list = implode(', ', $cached_tag_list);
             // Set image
             $uploadedFile = UploadedFile::getInstance($model, 'image');
             if ($uploadedFile) {
                 // Save origionals
                 $asset = new Asset();
                 $asset->assetable_type = Asset::ASSETABLE_POST;
                 $asset->assetable_id = $model->id;
                 $asset->uploadedFile = $uploadedFile;
                 $asset->saveAsset();
                 // Save thumbnails
                 $imageID = $asset->id;
                 $thumbnails = Asset::getThumbnails(Asset::ASSETABLE_POST);
                 foreach ($thumbnails as $thumbnail) {
                     $asset = new Asset();
                     $asset->parent_id = $imageID;
                     $asset->thumbnail = $thumbnail;
                     $asset->assetable_type = Asset::ASSETABLE_POST;
                     $asset->assetable_id = $model->id;
                     $asset->uploadedFile = $uploadedFile;
                     $asset->saveAsset();
                 }
             }
             $model->save(false);
             return $this->redirect($model->getUrl());
         }
         var_dump($model->getErrors());
         die;
     }
     $title = 'Добавить запись в блог';
     return $this->render('@frontend/views/site/index', ['templateType' => 'col2', 'title' => 'Dynamomania.com | ' . $title, 'columnFirst' => ['blog_form' => ['view' => '@frontend/views/forms/blog_form', 'data' => compact('model', 'tags', 'title')]], 'columnSecond' => ['blog' => SiteBlock::getBlogPosts(), 'banner1' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner2' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner3' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner4' => SiteBlock::getBanner(Banner::REGION_NEWS), 'banner5' => SiteBlock::getBanner(Banner::REGION_NEWS)]]);
 }