Example #1
0
 /**
  * @test
  */
 public function create()
 {
     $model = NewsCategory::model();
     $this->assertInstanceOf('NewsCategory', $model);
     $model = new NewsCategory();
     $model->attributes = array('name' => 'test');
     $this->assertTrue($model->save());
 }
Example #2
0
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new NewsCategory();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['NewsCategory'])) {
         $model->attributes = $_POST['NewsCategory'];
         if ($model->save()) {
             $this->redirect(array('view', 'id' => $model->id));
         }
     }
     $this->render('create', array('model' => $model));
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new NewsCategory();
     // Uncomment the following line if AJAX validation is needed
     // $this->performAjaxValidation($model);
     if (isset($_POST['NewsCategory'])) {
         $model->attributes = $_POST['NewsCategory'];
         if ($model->save()) {
             Yii::app()->user->setFlash('success', Yii::t('main', 'Данные успешно сохранены!'));
             $this->redirect(array('update', 'id' => $model->id));
         } else {
             Yii::app()->user->setFlash('error', Yii::t('main', 'Ошибка!'));
         }
     }
     $this->render('create', array('model' => $model));
 }
 /**
  * Creates a new model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  */
 public function actionCreate()
 {
     $model = new NewsCategory();
     if (isset($_POST['NewsCategory'])) {
         /** @var myWebUser $webUser */
         $webUser = Yii::app()->user;
         $model->attributes = $_POST['NewsCategory'];
         if ($model->save()) {
             $webUser->setFlash('success', Yii::t('msg', 'Object saved'));
             if (isset($_POST['backToList'])) {
                 // On a cliqué le bouton 'revenir à la liste' et le formulaire a bien été traité
                 $this->redirect(array('admin'));
             } else {
                 $this->redirect(array('update', 'id' => $model->id));
             }
         } else {
             $webUser->setFlash('error', Yii::t('msg', 'There are errors. Please check the form'));
         }
     }
     $this->render('create', array('model' => $model));
 }
Example #5
0
 public function action_cat_create()
 {
     $this->template->title = __("Thêm mới mục bài viết");
     $this->template->section_title = __("Thêm mới mục bài viết");
     $data = array();
     if (Request::$method == "POST") {
         $cat = new NewsCategory();
         $active = isset($_POST['active']);
         $post = $cat->validate_create($_POST);
         if ($post->check()) {
             $post = $post->as_array();
             $cat->name = $post['name'];
             $cat->slug = $post['slug'];
             $cat->active = $active;
             $cat->User = Auth::instance()->get_user();
             $cat->save();
             Request::instance()->redirect('admin/news/cat_index');
         } else {
             $data['errors'] = $post->errors('admin/newscategory');
             $_POST = $post->as_array();
         }
     }
     $this->template->content = View::factory('admin/news/cat_create', $data);
 }
Example #6
0
 /**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function postUpdate($id)
 {
     if (Session::get('user_level') < Config::get('cms.editNews')) {
         return Redirect::to(_l(URL::action('AdminHomeController@getIndex')))->with('message', Lang::get('admin.notPermitted'))->with('notif', 'warning');
     }
     $rules = array('title' => 'Required', 'shortContent' => 'Required');
     $validator = Validator::make(Input::all(), $rules);
     if ($validator->fails()) {
         return Redirect::to(_l(URL::action('NewsController@getEdit') . "/" . $id))->withErrors($validator)->withInput();
     } else {
         try {
             $news = News::findOrFail($id);
             $news->title = Input::get('title');
             if (!Input::get('permalink')) {
                 $news->permalink = slugify(Input::get('title'));
             } else {
                 $news->permalink = slugify(Input::get('permalink'));
             }
             if (Input::get('createdAt')) {
                 $news->created_at = date("Y-m-d H:i:s", strtotime(Input::get('createdAt')));
             } else {
                 $news->created_at = date("Y-m-d H:i:s", strtotime('now'));
             }
             $news->short_content = Input::get('shortContent', null, false);
             if (!Input::get('longContent')) {
                 $news->long_content = Input::get('shortContent', null, false);
             } else {
                 $news->long_content = Input::get('longContent', null, false);
             }
             $news->featured = Input::get('featured');
             $news->last_modified_by = Session::get('id');
             $news->published_by = Input::get('publishedBy');
             $news->featured_image = Input::get('featuredImage');
             $news->image_caption = Input::get('imageCaption');
             $news->post_type = 1;
             $news->published = Input::get('published');
             $news->save();
             $news->newsCategories()->delete();
             if (Input::get('categories')) {
                 foreach (Input::get('categories') as $catid) {
                     $newsCategory = new NewsCategory();
                     $newsCategory->news_id = $news->id;
                     $newsCategory->category_id = $catid;
                     $newsCategory->save();
                 }
             }
             return Redirect::to(_l(URL::action('NewsController@getEdit') . "/" . $id))->with('message', Lang::get('admin.newsUpdated'))->with('notif', 'success');
         } catch (Exception $e) {
             return Redirect::to(_l(URL::action('NewsController@getIndex')))->with('message', Lang::get('admin.noSuchNews'))->with('notif', 'danger');
         }
     }
 }
Example #7
0
 /**
  * Set organization categories 
  * @param array $categories ids.
  */
 public function setCategories(array $categories, $add = false)
 {
     $dontDelete = array();
     if (!empty($categories)) {
         foreach ($categories as $c) {
             if (empty($c)) {
                 continue;
             }
             $found = NewsCategory::model()->findByAttributes(array('category' => $c, 'news' => $this->id));
             // если не было категории - делаем
             if (!$found) {
                 $record = new NewsCategory();
                 $record->category = (int) $c;
                 $record->news = $this->id;
                 $record->save(false);
             }
             $dontDelete[] = $c;
         }
     }
     if ($add === false) {
         // Удаляем все категории, которых не было в массиве
         if (sizeof($dontDelete) > 0) {
             $cr = new CDbCriteria();
             $cr->addNotInCondition('category', $dontDelete);
             NewsCategory::model()->deleteAllByAttributes(array('news' => $this->id), $cr);
         } else {
             // удаляем все категории, т.к. пустой массив
             // Delete all relations
             NewsCategory::model()->deleteAllByAttributes(array('news' => $this->id));
         }
     }
 }