/** * Creates a new Posts model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Posts(); /** @var Categories[] $categories */ $categories = Categories::find()->all(); /** @var [] $items [category_id => 'name',category_id => 'name',category_id => 'name'] */ $items = []; foreach ($categories as $category) { $items[$category->id] = $category->title; } //var_dump($items); // exit; if ($model->load(Yii::$app->request->post()) && $model->save()) { foreach ($model->categoriesID as $categoryID) { $categoryPost = new Relatives(); $categoryPost->id_categories = $categoryID; $categoryPost->id_posts = $model->id; $categoryPost->save(); } //var_dump(Yii::$app->request->post()); // exit; return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'items' => $items]); } }
/** * Creates a new Posts model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Posts(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->post_id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new Posts model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Posts(); if ($model->load(Yii::$app->request->post())) { $model->user_id = Yii::$app->user->getId(); $model->create = date('Y-d-m h:i:s'); $model->save(); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->renderAjax('create', ['model' => $model]); } }
/** * @var $post Posts; * @return Posts|null */ public function createPost() { $post = new Posts(); $post->user_id = Yii::$app->user->id; $post->full_text = preg_replace('#<(/?(?:em|b|i|u|ul|li|ol|h2|blockquote))>#', '<\\1>', Html::encode($this->post)); $post->meta_keys = Html::encode($this->keys); $post->intro_text = preg_replace('#<(/?(?:em|b|i|u|ul|li|ol|h2|h3|blockquote))>#', '<\\1>', Html::encode(substr($this->post, 0, 32) . ' ...')); $post->meta_desc = Html::encode($this->desc); $post->title = Html::encode($this->title_post); /* todo allow user some interaction * // $str is the result of htmlspecialchars() preg_replace('#<(/?(?:pre|b|em|u|ul|li|ol))>#', '<\1>', $str); */ return $post->save() ? $post : null; }
/** * Creates a new Posts model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Posts(); $model->active = true; $model->allow_comments = true; $model->updated_at = date("Y-m-d H:i:s"); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { $upload_form_model = new UploadForm(); $upload_form_model->post_id = $model->id; return $this->render('create', ['upload_form_model' => $upload_form_model, 'model' => $model]); } }
/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { $post = new Posts(); $post->title = $request->get('title'); $post->body = $request->get('body'); $post->slug = str_slug($post->title); $post->author_id = $request->user()->id; if ($request->has('save')) { $post->active = 0; $message = 'Post saved successfully'; } else { $post->active = 1; $message = 'Post published successfully'; } $post->save(); return redirect('edit/' . $post->slug)->withMessage($message); }
/** * Store a newly created resource in storage. * * @param Request $request * @return Response */ public function store(Request $request) { $validator = \Validator::make($request->all(), ['title' => 'required', 'content' => 'required']); if ($validator->fails()) { return \Redirect::route('posts.create')->withErrors($validator)->withInput(); } else { $post = new Posts(); $post->title = $request->title; $post->slug = str_slug($request->title); $post->content = $request->content; $post->author_id = \Auth::id(); $post->post_type = 1; $post->created_at = date('Y-m-d H:i:s'); $post->updated_at = date('Y-m-d H:i:s'); $post->save(); } return \Redirect::route('posts.index')->with('success', 'You have successfully created a post.'); }
/** * @inheritdoc * @return Result parsing group. */ public static function parseAllSources() { $query = Sources::find(); $sources = $query->active()->all(); $i = 0; foreach ($sources as $source) { if ($source->ownerId) { $param['ownerId'] = $source->ownerId; } else { $param['ownerId'] = NULL; } $results = Yii::$app->vkApi->createRequest()->setUrl('wall.get')->setData(['owner_id' => $param['ownerId'], 'domain' => $source->domain, 'offset' => $source->postOffset, 'count' => $source->postCount, 'filter' => 'owner'])->send(); if ($results->isOk) { $responses = $results->data['response']; foreach ($responses as $response) { if (is_array($response)) { $post = new Posts(); $post->recordId = $response['id']; $post->sources_id = $source->id; $prepare_content = preg_replace("/([^&]#[a-zA-zа-яА-я0-9]*)/", "", stripcslashes($response['text'])); $prepare_content = preg_replace("/\\[(.+?)\\]/", "", $prepare_content); $post->content = $prepare_content; $title = mb_substr($post->content, 0, mb_stripos($post->content, "<br")); if (!$title) { $title = $post->content; } $post->title = $title ? $title : NULL; $post->slug = Inflector::slug($post->title); if ($response['post_type'] != 'post') { break; } try { if ($post->save()) { foreach ($response['attachments'] as $attachment) { if ($attachment['type'] == 'photo') { $photo = new Photos(); $photo->id_posts = $post->id; $photo->text = $attachment['photo']['text']; if (isset($attachment['photo']['src'])) { $photo->link = $attachment['photo']['src']; } if (isset($attachment['photo']['src_small'])) { $photo->link = $attachment['photo']['src_small']; } if (isset($attachment['photo']['src_big'])) { $photo->link = $attachment['photo']['src_big']; } if (isset($attachment['photo']['src_xbig'])) { $photo->link = $attachment['photo']['src_xbig']; } if (isset($attachment['photo']['src_xxbig'])) { $photo->link = $attachment['photo']['src_xxbig']; } if (isset($attachment['photo']['src_xxxbig'])) { $photo->link = $attachment['photo']['src_xxxbig']; } $photo->save(); } else { $post->delete(); $i--; } } $i++; } $post->slug = $post->id . "-" . $post->slug; $post->save(); } catch (yii\db\IntegrityException $e) { //nothing } } } } sleep(5); } if ($i < 0) { $i = 0; } return Yii::t("app", '{n, plural, =0{Not added new records} =1{# record added} few{# records added} other{# added records}}', ['n' => $i]); }