public function actionCreates() { $model = new News(); $file = new File(); $model->member_id = Yii::app()->user->id; $model->create_at = date('Y-m-d H:i:s'); $model->update_at = date('Y-m-d H:i:s'); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['News']) && isset($_POST['File'])) { $model->attributes = $_POST['News']; $file->attributes = $_POST['File']; $model->pic = 'noimage.jpg'; $model->validate(); $file->validate(); if ($model->getErrors() == null && $file->getErrors() == null) { $file->file = CUploadedFile::getInstance($file, 'file'); if ($file->file != null) { $filename = time() . '.' . $file->file->getExtensionName(); $file->file->saveAs(Yii::app()->params['pathUpload'] . $filename); $model->pic = $filename; } else { $model->pic = 'noimage.jpg'; } if ($model->save()) { $this->redirect(array('view', 'id' => $model->news_id)); } } } $this->render('create', array('model' => $model, 'file' => $file)); }
public function actionCreate() { $this->breadcrumbs = array_merge($this->breadcrumbs, array('Создание новости')); $this->pageTitle = Yii::t('app', 'Создание новости'); $model = new News(); if (Yii::app()->request->getPost('News')) { $model->attributes = Yii::app()->request->getPost('News'); if ($model->validate()) { if ($model->save()) { Yii::app()->user->setFlash('success', Yii::t('app', 'Новость успешно добавлена')); Yii::app()->request->redirect($this->createUrl('index')); } } } $this->render('create', array('model' => $model)); }
public function actionCreate() { if (Yii::app()->user->checkAccess('createNews') == false) { throw new CHttpException(403); } $cases = new News(); if (isset($_POST['News'])) { $cases->attributes = Yii::app()->request->getPost('News'); if ($cases->validate() && $cases->save()) { $this->setFlashMessage(strtr('<strong>{link}</strong> 成功案例添加成功', array('{link}' => CHtml::link($cases->title, array('view', 'id' => $cases->primaryKey))))); $this->redirect($this->getReturnUrl()); } } else { $cases->release_date = date('Y-m-d'); $cases->is_released = 1; } $this->breadcrumbs = array('成功案例' => array('index'), '添加'); $this->render('create', array('cases' => $cases, 'returnUrl' => $this->getReturnUrl())); }
public function actionIndex() { $model = new News(); // uncomment the following code to enable ajax-based validation /* if(isset($_POST['ajax']) && $_POST['ajax']==='news-index-form') { echo CActiveForm::validate($model); Yii::app()->end(); } */ if (isset($_POST['News'])) { $model->attributes = $_POST['News']; if ($model->validate()) { // form inputs are valid, do something here return; } } $this->render('index', array('model' => $model)); }
/** * Store a newly created resource in storage. * * @return Response */ public function store() { $data = Input::all(); $validation = News::validate($data); $news = new News(); if ($validation->fails()) { return Redirect::to('news/create')->withErrors($validation)->withInput(); } $file = Input::file('image'); $destinatonPath = 'public/storage/news'; $originalName = $file->getClientOriginalName(); $filename = rand(1, 10000) . $originalName; $file->move($destinatonPath, $filename); $news->title = $data['title']; $news->content = $data['content']; $news->image = $filename; $news->date_added = date('Y-m-d H:i:s'); $news->save(); return Redirect::route('news.index')->with('message', 'Succesfull added news'); }