-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add and Deleting Articles is now working using javascript and ajax
- Loading branch information
1 parent
61e40fa
commit ab93c69
Showing
29 changed files
with
1,089 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
class PostsController extends AppController { | ||
public $helpers = array('Html', 'Form', 'Session', 'Js'); | ||
public $components = array('Session'); | ||
|
||
public function index() { | ||
$this->set('posts', $this->Post->find('all')); | ||
} | ||
|
||
public function view($id = null) { | ||
if (!$id) { | ||
throw new NotFoundException(__('Invalid post')); | ||
} | ||
|
||
$post = $this->Post->findById($id); | ||
if (!$post) { | ||
throw new NotFoundException(__('Invalid post')); | ||
} | ||
$this->set('post', $post); | ||
} | ||
public function add() { | ||
if ($this->request->is('post')) { | ||
$this->Post->create(); | ||
if ($this->Post->save($this->request->data)) { | ||
$this->Session->setFlash(__('Your post has been saved.')); | ||
return $this->redirect(array('action' => 'index')); | ||
} | ||
$this->Session->setFlash(__('Unable to add your post.')); | ||
} | ||
} | ||
public function edit($id = null) { | ||
if (!$id) { | ||
throw new NotFoundException(__('Invalid post')); | ||
} | ||
|
||
$post = $this->Post->findById($id); | ||
if (!$post) { | ||
throw new NotFoundException(__('Invalid post')); | ||
} | ||
|
||
if ($this->request->is(array('post', 'put'))) { | ||
$this->Post->id = $id; | ||
if ($this->Post->save($this->request->data)) { | ||
$this->Session->setFlash(__('Your post has been updated.')); | ||
return $this->redirect(array('action' => 'index')); | ||
} | ||
$this->Session->setFlash(__('Unable to update your post.')); | ||
} | ||
|
||
if (!$this->request->data) { | ||
$this->request->data = $post; | ||
} | ||
} | ||
public function delete($id) { | ||
if ($this->request->is('get')) { | ||
throw new MethodNotAllowedException(); | ||
} | ||
|
||
if ($this->Post->delete($id)) { | ||
$this->Session->setFlash(__('The post with id: %s has been deleted.', h($id))); | ||
return $this->redirect(array('action' => 'index')); | ||
} | ||
} | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
class Post extends AppModel { | ||
public $validate = array( | ||
'title' => array( | ||
'rule' => 'notEmpty' | ||
), | ||
'body' => array( | ||
'rule' => 'notEmpty' | ||
) | ||
); | ||
} | ||
|
||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
<?php | ||
echo $success; | ||
if($success){ | ||
echo $this->element('article', array("article" => $article ['Article'])); | ||
}else{ | ||
echo "Something went wrong"; | ||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<?php | ||
echo $success; | ||
echo json_encode($success); | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
/** | ||
* Application level Controller | ||
* | ||
* This file is application-wide controller file. You can put all | ||
* application-wide controller-related methods here. | ||
* | ||
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice. | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) | ||
* @link http://cakephp.org CakePHP(tm) Project | ||
* @package app.Controller | ||
* @since CakePHP(tm) v 0.2.9 | ||
* @license http://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
App::uses('Controller', 'Controller'); | ||
|
||
/** | ||
* Application Controller | ||
* | ||
* Add your application-wide methods in the class below, your controllers | ||
* will inherit them. | ||
* | ||
* @package app.Controller | ||
* @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller | ||
*/ | ||
class AppController extends Controller { | ||
public $components = array('DebugKit.Toolbar', | ||
'Session', | ||
'Auth' => array( | ||
'loginRedirect' => array('controller' => 'sections', 'action' => 'index'), | ||
'logoutRedirect' => array('controller' => 'sections', 'action' => 'index') | ||
) | ||
); | ||
|
||
public function beforeFilter() { | ||
$this->Auth->allow('index', 'view'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
class ArticlesController extends AppController { | ||
public $helpers = array('Html', 'Form', 'Session', 'Js'); | ||
public $components = array('Session', 'Auth'); | ||
|
||
public function edit($id = null) { | ||
$this->layout = 'ajax'; | ||
|
||
if (!$id) { | ||
$this->set('success', "<strong>FAIL!</strong> The article you tried to save was invalid"); | ||
return; | ||
//throw new NotFoundException(__('Invalid post')); | ||
} | ||
|
||
$Article = $this->Article->findById($id); | ||
if (!$Article) { | ||
$this->set('success', "<strong>FAIL!</strong> The article you tried to save was invalid"); | ||
return; | ||
//throw new NotFoundException(__('Invalid post')); | ||
} | ||
|
||
if ($this->request->is(array('post', 'put'))) { | ||
$this->Article->id = $id; | ||
if ($this->Article->save($this->request->data)) { | ||
$this->set('success', "<strong>SUCCES!</strong> All your changes are saved!"); | ||
$this->Session->setFlash(__('Your post has been updated.')); | ||
return; | ||
//return $this->redirect(array('action' => 'index')); | ||
} | ||
$this->Session->setFlash(__('Unable to update your post.')); | ||
$this->set('success', "<strong>FAIL!</strong> Unable to save your article!"); | ||
} | ||
} | ||
public function delete($id) { | ||
if ($this->request->is('get')) { | ||
$this->set('success', "<strong>FAIL!</strong> The article was not found"); | ||
} | ||
|
||
if ($this->Article->delete($id)) { | ||
$this->set('success', "<strong>SUCCES!</strong> The article was deleted"); | ||
} | ||
} | ||
public function add() { | ||
if ($this->request->is('post')) { | ||
$this->Article->create(); | ||
if ($this->Article->save($this->request->data)) { | ||
$this->set('success', "<strong>SUCCES!</strong> New Article Created"); | ||
} | ||
$this->set('success', "<strong>FAIL!</strong> Can't Create Article"); | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
?> |
Empty file.
Oops, something went wrong.