Beispiel #1
0
 public function action_create()
 {
     if (Input::method() == 'POST') {
         $val = Model_News::validate('create');
         if ($val->run()) {
             $news = Model_News::forge(array('title' => Input::post('title'), 'article' => Input::post('article'), 'call_center_id' => Input::post('call_center_id'), 'user_id' => Input::post('user_id')));
             if ($news and $news->save()) {
                 Session::set_flash('success', 'Added news #' . $news->id . '.');
                 Response::redirect('news');
             } else {
                 Session::set_flash('error', 'Could not save news.');
             }
         } else {
             Session::set_flash('error', $val->error());
         }
     }
     $this->template->title = "News";
     $this->template->content = View::forge('news/create');
 }
Beispiel #2
0
 public function action_create()
 {
     if (!Auth::has_access('news.create')) {
         Session::set_flash('warning', 'You don\'t have the right to create a news');
         Response::redirect('admin');
     }
     if (Input::method() == 'POST') {
         $val = Model_News::validate('create');
         if ($val->run()) {
             $news = Model_News::forge(array('title' => Input::post('title'), 'body' => Input::post('body')));
             if ($news and $news->save()) {
                 Session::set_flash('success', 'Added news #' . $news->id . '.');
                 Response::redirect('news');
             } else {
                 Session::set_flash('error', 'Could not save news.');
             }
         } else {
             Session::set_flash('error', $val->error());
         }
     }
     $this->template->title = "News";
     $this->template->content = View::forge('news/create');
 }
Beispiel #3
0
 public function action_create()
 {
     try {
         if (Input::method() == 'POST') {
             $file = Input::file('news_photo_file');
             $val = Model_News::validate('create');
             if ($val->run()) {
                 $config = array('path' => "/var/www/html/uploads/news_photo/", 'ext_whitelist' => array('jpg', 'jpeg', 'png'), 'file_chmod' => 0777, 'auto_rename' => true, 'overwrite' => true, 'randomize' => true, 'create_path' => true);
                 //  $allowList = array(".jpeg", ".jpg", ".png");
                 //  $error = false;
                 //  $path = realpath(DOCROOT."/../../uploads/news_photo/").DS;
                 $news_photo = "";
                 Upload::process($config);
                 if (Upload::is_valid()) {
                     Upload::save();
                     $news_photo = Upload::get_files()[0];
                     $news = Model_News::forge(array('news_title' => Input::post('news_title'), 'news_short_detail' => Input::post('news_short_detail'), 'news_detail' => Input::post('news_detail'), 'news_photo' => $news_photo['saved_as'], 'news_published' => Input::post('news_published'), 'created_at' => time(), 'published_at' => Input::post('news_published') == 1 ? time() : 0));
                     if ($news and $news->save()) {
                         Session::set_flash('success', 'Added news #' . $news->id . '.');
                         Response::redirect('news/edit/' . $news->id);
                     } else {
                         Session::set_flash('error', 'Could not save news.');
                     }
                     if ($file and $file->save()) {
                         DB::commit_transaction();
                         \Fuel\Core\Session::set_flash('success', 'Upload success');
                     }
                 }
                 /*if($file['size'] > 0){
                 
                                         $ext = strtolower(substr($file['name'],strrpos($file['name'],".")));
                 
                                         if(!in_array($ext,$allowList)){
                                             Session::set_flash('error', 'ชนิดของไฟล์ภาพไม่ถูกต้อง');
                                             $error = true;
                                         }
                 
                                         $filename = md5(time());
                 
                                         if(@copy($file['tmp_name'],$path.$filename.$ext)){
                 
                                             $news_photo = $filename.$ext;
                 
                                             /* small thumbnail */
                 #   parent::create_cropped_thumbnail($path.$filename.$ext,64,64,"-s");
                 #   parent::create_cropped_thumbnail($path.$filename.$ext,128,128,"-s@2x");
                 /* */
                 /* medium thumbnail */
                 #  parent::create_cropped_thumbnail($path.$filename.$ext,360,240,"-m");
                 #  parent::create_cropped_thumbnail($path.$filename.$ext,720,480,"-m@2x");
                 /*
                 
                                 } else {
                                     Session::set_flash('error', 'ไม่สามารถอัพโหลดไฟล์ภาพได้ โปรดลองใหม่อีกครั้ง');
                                     $error = true;
                                 }
                 
                             }*/
                 /*if(!$error){
                 
                                         $news = Model_News::forge(array(
                                             'news_title' => Input::post('news_title'),
                                             'news_short_detail' => Input::post('news_short_detail'),
                                             'news_detail' => Input::post('news_detail'),
                                             'news_photo' => $news_photo,
                                             'news_published' => Input::post('news_published'),
                                             'created_at' => time(),
                                             'published_at' => (Input::post('news_published')==1)?time():0
                                         ));
                 
                                         if ($news and $news->save()){
                                             Session::set_flash('success', 'Added news #' . $news->id . '.');
                                             Response::redirect('news/edit/'.$news->id);
                                         } else {
                                             Session::set_flash('error', 'Could not save news.');
                                         }
                 
                                     }*/
             } else {
                 $msg = '<ul>';
                 foreach ($val->error() as $field => $error) {
                     $msg .= '<li>' . $error->get_message() . '</li>';
                 }
                 $msg .= '</ul>';
                 Session::set_flash('error', $msg);
             }
         }
         $this->theme->set_template('edit');
         $this->theme->get_template()->set_global('current_menu', "News", false);
         $this->theme->get_template()->set_global('current_menu_desc', "จัดการข่าวทั้งหมดในระบบ", false);
         $this->theme->get_template()->set('breadcrumb', array(array('title' => "Home", 'icon' => "fa-home", 'link' => Uri::create('home'), 'active' => false), array('title' => "News", 'icon' => "eicon-newspaper", 'link' => Uri::create('news/index'), 'active' => false), array('title' => "Create", 'icon' => "", 'link' => "", 'active' => true)));
         $this->theme->get_template()->set_global('mode', "create", false);
         $this->theme->get_template()->set('page_specific_js', "form_news.js");
         $this->theme->set_partial('sidebar', 'common/sidebar');
         $this->theme->set_partial('left', 'news/create');
     } catch (Exception $e) {
         die($e->getMessage());
     }
 }
Beispiel #4
0
 public function action_edit($id = 0)
 {
     if (Input::post('action') == 'confirm') {
         foreach ($this->fields as $field) {
             Session::set_flash($field, Input::post($field));
         }
         $data = array();
         foreach ($this->fields as $field) {
             $data[$field] = Session::get_flash($field);
             Session::keep_flash($field);
         }
         $this->template->content = View::forge('admin/news/confirm', $data);
     } else {
         $data["news"] = Model_News::find($id);
         if ($data["news"] == null) {
             $data["news"] = Model_News::forge();
         }
         // add
         if (Session::get_flash('title') != null and Security::check_token()) {
             // save
             $news = $data["news"];
             $news->for_teachers = Session::get_flash("for_teachers", 0);
             $news->for_students = Session::get_flash("for_students", 0);
             $news->title = Session::get_flash("title");
             $news->body = Session::get_flash("body");
             if ($news->for_students == null) {
                 $news->for_students = 0;
             }
             if ($news->for_teachers == null) {
                 $news->for_teachers = 0;
             }
             $news->save();
             $body = View::forge("email/news");
             $body->set("title", $news->title);
             $body->set("body", $news->body);
             if ($news->for_teachers == 1) {
                 $teachers = Model_User::find("all", ["where" => [["group_id", 10], ["deleted_at", 0]], "order_by" => [["id", "desc"]]]);
                 foreach ($teachers as $teacher) {
                     $sendmail = Email::forge("JIS");
                     $sendmail->from(Config::get("statics.info_email"), Config::get("statics.info_name"));
                     $sendmail->to($teacher->email);
                     $sendmail->subject("{$news->title} / Game-bootcamp");
                     $sendmail->html_body("Dear {$teacher->firstname},<br><br>" . htmlspecialchars_decode($body) . "If you are no longer interested, you can " . "<a href=" . Uri::base() . "?" . md5('id') . "={$teacher->id}/unsubscribe=" . md5($teacher->email) . ">Unsubscribe.</a>");
                     $sendmail->send();
                 }
             }
             if ($news->for_students == 1) {
                 $students = Model_User::find("all", ["where" => [["group_id", 1], ["deleted_at", 0]], "order_by" => [["id", "desc"]]]);
                 foreach ($students as $student) {
                     $sendmail = Email::forge("JIS");
                     $sendmail->from(Config::get("statics.info_email"), Config::get("statics.info_name"));
                     $sendmail->to($student->email);
                     $sendmail->subject("{$news->title} / Game-bootcamp");
                     $sendmail->html_body("Dear {$student->firstname},<br><br>" . htmlspecialchars_decode($body) . "If you are no longer interested, you can " . "<a href=" . Uri::base() . "?" . md5('id') . "={$student->id}/unsubscribe=" . md5($student->email) . ">Unsubscribe.</a>");
                     $sendmail->send();
                 }
             }
             Response::redirect("/admin/news/");
         }
         $view = View::forge("admin/news/edit", $data);
         $this->template->content = $view;
     }
 }
Beispiel #5
0
 public function action_create()
 {
     \View::set_global('title', 'Add New');
     if (\Input::post()) {
         $val = Model_News::validate('create');
         // Upload image and display errors if there are any
         $image = $this->upload_image();
         if (!$image['exists'] && \Config::get('details.image.required', false) && empty($item->image)) {
             // No previous images and image is not selected and it is required
             \Messages::error('<strong>There was an error while trying to upload news image</strong>');
             \Messages::error('You have to select image');
         } elseif ($image['errors']) {
             \Messages::error('<strong>There was an error while trying to upload news image</strong>');
             foreach ($image['errors'] as $error) {
                 \Messages::error($error);
             }
         }
         if ($val->run() && $image['is_valid'] && !(!$image['exists'] && \Config::get('details.image.required', false))) {
             // Get POST values
             $insert = \Input::post();
             // Prepare some values
             $insert['published_at'] = !empty($insert['published_at']) ? strtotime($insert['published_at']) : \Date::forge()->get_timestamp();
             $insert['active_from'] = !empty($insert['active_from']) ? strtotime($insert['active_from']) : NULL;
             $insert['active_to'] = !empty($insert['active_to']) ? strtotime($insert['active_to']) : NULL;
             if ($insert['status'] != 2) {
                 unset($insert['active_from']);
                 unset($insert['active_to']);
             }
             $item = Model_News::forge($insert);
             try {
                 $item->save();
                 // Validate and insert news slug into SEO database table
                 $val_seo = Model_Seo::validate('create_seo');
                 $insert_seo = array('content_id' => $item->id, 'slug' => \Inflector::friendly_title($item->title, '-', true));
                 while (!$val_seo->run($insert_seo)) {
                     $insert_seo['slug'] = \Str::increment($insert_seo['slug'], 1, '-');
                 }
                 $item_seo = Model_Seo::forge($insert_seo);
                 $item_seo->save();
                 // END OF: SEO
                 // Insert news images
                 if ($this->_image_data) {
                     $item_image = array(array('id' => 0, 'data' => array('content_id' => $item->id, 'image' => $this->_image_data[0]['saved_as'], 'alt_text' => \Input::post('alt_text', ''), 'cover' => 1, 'sort' => 1)));
                     Model_News::bind_images($item_image);
                 }
                 \Messages::success('News successfully created.');
                 \Response::redirect(\Input::post('update', false) ? \Uri::create('admin/news/update/' . $item->id) : \Uri::admin('current'));
             } catch (\Database_Exception $e) {
                 // show validation errors
                 \Messages::error('<strong>There was an error while trying to create news</strong>');
                 // Uncomment lines below to show database errors
                 $errors = $e->getMessage();
                 \Messages::error($errors);
             }
         } else {
             if ($val->error() != array()) {
                 // show validation errors
                 \Messages::error('<strong>There was an error while trying to create news</strong>');
                 foreach ($val->error() as $e) {
                     \Messages::error($e->get_message());
                 }
             }
         }
         // Delete uploaded image if there is news saving error
         if (isset($this->_image_data)) {
             $this->delete_image($this->_image_data[0]['saved_as']);
         }
     }
     \Theme::instance()->set_partial('content', $this->view_dir . 'create');
 }