Skip to content

Instantly share code, notes, and snippets.

@SET001
Created December 23, 2011 01:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SET001/1512629 to your computer and use it in GitHub Desktop.
Save SET001/1512629 to your computer and use it in GitHub Desktop.
<?php
class Controller_Article extends Layout{
public $secure_actions = array(
'delete' => array('moderator'),
'save' => array('moderator'));
public function action_save(){
$id = $this->request->param('id');
$article = ORM::factory('article', $id);
if (!$article->loaded()){
$article = new Model_Article();
$article->added = date('Y-m-d H:i:s');
$article->user_id = $this->user->id;
}
if (!empty($_POST)){
$article->name = arr::get($_POST, 'name');
$article->text = arr::get($_POST, 'text');
try{
$article->save();
$image = Upload::save($_FILES['cover'], $article->id . '.jpg', 'images/articles');
$this->request->redirect('article/index');
}
catch(Exception $e){
$errors = $e->getMessage();
print_r($errors);
die;
}
}
$this->template->content = View::factory('article_save')
->set('article', $article)
->render();
}
public function action_view($id){
$article = new Model_Article($id);
if (!$article->loaded()) die('wrong ig');
$this->template->content = View::factory('article_view')->set('article', $article);
}
public function action_delete($id){
$article = new Model_Article($id);
if (!$article->loaded()) die('wrong ig');
$article->delete();
$this->request->redirect('article');
}
public function action_index(){
$query = ORM::factory('article');
if ($q = arr::get($_GET, 'q')){
$query->where('name', 'like', '%'.strip_tags($q).'%');
}
$articles = $query->find_all();
$this->template->content = View::factory('insider_2col')
->set('sidebar', View::factory('article_sidebar'))
->set('main', View::factory('article_index')
->set('articles', $articles));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment