Skip to content

Instantly share code, notes, and snippets.

@ThatGirlSlays
Last active December 11, 2015 21:59
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 ThatGirlSlays/2b90e5af2518a81672fb to your computer and use it in GitHub Desktop.
Save ThatGirlSlays/2b90e5af2518a81672fb to your computer and use it in GitHub Desktop.
ArticlesController for CakePHP 2.2.4
<?php
App::uses('AppController', 'Controller');
/**
* Articles Controller
*
* @property Article $Article
*/
class ArticlesController extends AppController {
// CustomPage component is used in AppController
function beforeFilter(){
parent::beforeFilter();
$this->set('module_id', '3');
$this->Auth->allow('topic', 'edit', 'add', 'delete');
}
public $paginate = array(
'Article' => array(
'limit' => 10,
'order' => array('Article.id' => 'DESC'),
'contain' => array(
'ArticleTopic' => array(
'fields' => array(
'ArticleTopic.title'
),
),
'User' => array(
'fields' => array(
'User.username', 'User.online', 'User.signature'
),
'UserAvatar' => array(
'fields' => array(
'UserAvatar.file'
)
)
)
),
),
'Comment' => array(
'limit' => 10,
'order' => array('Comment.id' => 'DESC'),
'contain' => array(
'User' => array(
'fields' => array(
'User.username', 'User.online', 'User.signature'
),
'UserAvatar' => array(
'fields' => array(
'UserAvatar.file'
)
)
)
),
),
);
public function index($page = null) {
if($page){
$this->set('articles', $this->paginate('Article', array('Article.page_id' => $page, 'Article.status <= 5')
) // end paginate
); // end set
}
else{
$this->set('articles', $this->paginate('Article', array('Article.status <= 5')));
}
} // end index function
public function topic($page = null, $id = null) {
$topic_title = $this->Article->ArticleTopic->field('title', array('id =' => $id));
$this->set('articles', $this->paginate('Article', array('Article.topic_id' => $id)));
$this->set('topic_id', $id);
$this->set('topic_title', $topic_title);
$this->set('title_for_layout', $topic_title);
} // end topic function
// Redirect works fine in view() if article does not exist or if page_id != $page
public function view($page = null, $id = null) {
$this->Article->id = $id;
$article_page = $this->Article->field('page_id', array('id =' => $id));
if (!$this->Article->exists()) {
$this->Session->setFlash('This article does not exist');
$this->redirect(array('controller'=>'articles', 'action' => 'index', 'page' => $page), null, true);
}
elseif ($article_page != $page) {
$this->Session->setFlash('Invalid article for this page');
$this->redirect(array('action' => 'index', 'page' => $page), null, true);
}
else{
$this->set('title_for_layout', $this->Article->field('title', array('id =' => $id)));
// CONTAIN
$this->Article->contain(array(
'ArticleTopic' => array(
'fields' => array(
'ArticleTopic.title'
),
),
'User' => array(
'fields' => array(
'User.username', 'User.signature', 'User.online'
),
'UserAvatar' => array(
'fields' => array(
'UserAvatar.file'
)
)
)
));
// END CONTAIN
$this->set('article', $this->Article->read(null, $id));
$this->set('comments', $this->paginate($this->Article->Comment, array('Comment.module_id' => $this->viewVars['module_id'], 'Comment.post_id' => $id)));
} // end else
} // end view function
// Redirect works fine if user has correct access and user posts article meeting validation rules.
// Redirect does not work when user does not have correct access (as in $access_message is not null)
public function add($page = null) {
$access = $this->viewVars['access'];
if($this->CustomPage->AccessMessage(4, $access)){
$this->Session->setFlash(__($this->CustomPage->AccessMessage(4, $access)));
$this->redirect(array('action' => 'index', 'page' => $page));
} else
{
$pages = $this->Article->Page->find('list');
$topics = $this->Article->ArticleTopic->find('list', array('conditions' => array('ArticleTopic.page_id' => $page)));
$users = $this->Article->User->find('list');
$this->set(compact('pages', 'topics', 'users'));
if ($this->request->is('post')) {
$this->Article->create();
if ($this->Article->save($this->request->data)) {
$article_id = $this->Article->getInsertId();
$activity_data = array(
'Activity' => array(
'description' => 'Posted the article',
'post_id' => $article_id,
'page_id' => $page,
'user_id' => $this->request->data['Article']['user_id'],
'module_id' => $this->viewVars['module_id'],
)
);
$this->Article->Page->Activity->create();
$this->Article->Page->Activity->save($activity_data);
$points_data = array(
'Point' => array(
'description' => 'Posted the article',
'post_id' => $article_id,
'page_id' => $page,
'user_id' => $this->request->data['Article']['user_id'],
'module_id' => $this->viewVars['module_id'],
'amount' => Configure::read('Articles.AddPoints'),
)
);
$this->Article->Page->Point->create();
$this->Article->Page->Point->save($points_data);
$this->Session->setFlash(__('The article has been saved'));
$this->redirect(array('action' => 'view', 'id' => $article_id, 'page' => $page));
} else {
$this->Session->setFlash(__('The article could not be saved. Please, try again.'));
} // else if can't save article
} // if method is post
} // end if user has access
} // end add action
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment