Ejemplo n.º 1
0
 public function post_add()
 {
     $model = Model_Blog::forge();
     $model->set(Input::post());
     $model->set(BLOG_CREATED_DATE, DB::expr('now()'));
     $model->set(DELETE_FLG, '0');
     $model->save();
     Response::redirect(Config::get('base_url'));
 }
Ejemplo n.º 2
0
 public function action_edit($blog_id = null)
 {
     $result = array('status' => false, 'error' => array());
     try {
         $input = Input::post();
         if (empty($input)) {
             throw new Exception('No data was posted');
         }
         $blog = null;
         $user_id = empty($input['user_id']) ? 0 : $input['user_id'];
         if (empty($blog_id)) {
             $blog = Model_Blog::forge();
             $blog->user_id = $user_id;
         } else {
             $blog = Model_Blog::find($blog_id);
             if (!$blog) {
                 throw new Exception('Blog not found.');
             }
             if ($user_id != $blog->user_id) {
                 throw new Exception('Wrong user id passed for this blog.');
             }
         }
         if (!\Access::can('edit_any_blog', $this->user) && !\Access::can('edit_own_blog', $this->user)) {
             throw new Exception('You are not authorized to edit this blog');
         }
         if (!\Access::can('publicize_any_blog', $this->user) && !\Access::can('publicize_own_blog', $this->user)) {
             $input['public_flag'] = $blog->public_flag;
             //dont allow public flag to change if user doesn't have permission
         } elseif (!isset($input['public_flag'])) {
             $input['public_flag'] = false;
         }
         if (!isset($input['publish_flag'])) {
             $input['publish_flag'] = false;
         }
         $blog->from_array($input);
         //populate the blog fields from the input
         try {
             $blog->save();
             $result['status'] = true;
             $result['data'] = $blog->to_array();
         } catch (\Orm\ValidationFailed $ex) {
             $result['error'] = $ex->getMessage();
         } catch (Exception $ex) {
             $msg = $ex->getMessage();
             $result['error'] = $msg ? $msg : 'Oops, something went wrong.';
         }
     } catch (Exception $ex) {
         $result['error'] = $ex->getMessage();
     }
     return $this->response($result);
 }
Ejemplo n.º 3
0
 public function action_create()
 {
     if (Input::method() == 'POST') {
         $file = Input::file('blog_cover_photo_file');
         $val = Model_Blog::validate('create');
         if ($val->run()) {
             $allowList = array(".jpeg", ".jpg", ".png");
             $error = false;
             $path = realpath(DOCROOT . "/../../uploads/blog_cover/") . DS;
             $blog_cover_photo = "";
             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()) . $ext;
                 if (@copy($file['tmp_name'], $path . $filename)) {
                     $blog_cover_photo = $filename;
                 } else {
                     Session::set_flash('error', 'ไม่สามารถอัพโหลดไฟล์ภาพได้ โปรดลองใหม่อีกครั้ง');
                     $error = true;
                 }
             }
             if (!$error) {
                 $blog = Model_Blog::forge(array('blog_title' => Input::post('blog_title'), 'blog_short_detail' => Input::post('blog_short_detail'), 'blog_detail' => Input::post('blog_detail'), 'blog_cover_photo' => $blog_cover_photo, 'blog_published' => Input::post('blog_published'), 'created_at' => time(), 'published_at' => Input::post('blog_published') == 1 ? time() : 0));
                 if ($blog and $blog->save()) {
                     Session::set_flash('success', 'Added blog #' . $blog->id . '.');
                     Response::redirect('blog/edit/' . $blog->id);
                 } else {
                     Session::set_flash('error', 'Could not save blog.');
                 }
             }
         } 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', "Blogs", 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' => "Blogs", 'icon' => "fa-rss", 'link' => Uri::create('blog/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_blog.js");
     $this->theme->set_partial('sidebar', 'common/sidebar');
     $this->theme->set_partial('left', 'blog/create');
 }
Ejemplo n.º 4
0
 public function action_create()
 {
     if (Input::method() == 'POST') {
         $val = Model_Blog::validate('create');
         if ($val->run()) {
             $blog = Model_Blog::forge(array('title' => Input::post('title'), 'content' => Input::post('content')));
             if ($blog and $blog->save()) {
                 Session::set_flash('success', 'Added blog #' . $blog->id . '.');
                 Response::redirect('blog');
             } else {
                 Session::set_flash('error', 'Could not save blog.');
             }
         } else {
             Session::set_flash('error', $val->error());
         }
     }
     $this->template->title = "Blogs";
     $this->template->content = View::forge('blog/create');
 }