Skip to content

Commit

Permalink
Add and Delete is Now Working
Browse files Browse the repository at this point in the history
Add and Deleting Articles is now working using javascript and ajax
  • Loading branch information
joewashear007 committed Jan 10, 2014
1 parent 61e40fa commit ab93c69
Show file tree
Hide file tree
Showing 29 changed files with 1,089 additions and 213 deletions.
60 changes: 33 additions & 27 deletions Controller/ArticlesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,62 @@ class ArticlesController extends AppController {

public function edit($id = null) {
$this->layout = 'ajax';
Debugger::log("The ID: ".$id);
if (!$id) {
$this->set('success', false);
return;
}

$Article = $this->Article->findById($id);

if (!$Article) {
Debugger::log("The Article Loaded: NOT ");
$this->set('success', false);
return;
}
Debugger::log("Request Type: ".$this->request->method());
//if ($this->request->is('post') or $this->request->is('put')) {
if (!$this->request->is('get')) {
Debugger::log("Request Type: POST OR PUT");
$this->Article->id = $id;
Debugger::log("Article ID: ".$id);
if ($this->Article->save($this->request->data)) {
Debugger::log("Save: Success");
$this->set('success', true);
return;
}else{
Debugger::log("Save: FAIL!");
$this->set('success', false);
}
}else{
Debugger::log("Request Type: must be GET");
$this->set('success', false);
}
}
$this->set('success', false);
}
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");
}
$this->layout = "ajax";
if (!$this->request->is('get')) {
$this->Article->id = $id;
Debugger::log("The ID: ".$id);
if ($this->Article->delete($id)) {
Debugger::log("Delete");
$this->set('success', true);
// return $this->redirect(
// array('controller' => 'sections', 'action' => 'draw', $secId)
// );
}else{
Debugger::log("Failed to Delete: ");
$this->set('success', false);
}
}
}
public function add($sectionId) {
if ($this->request->is('post')) {
$this->layout = "ajax";
Configure::write('SiteEng.Run.Edit', true);
Configure::write('SiteEng.Run.EditClass', Configure::read('SiteEng.Site.EditClass'));
Debugger::log("Starting Add");
if (!$this->request->is('get')) {
Debugger::log("Creating ARTICLE");
$this->Article->create();
$this->Article->set('section-id' , $section-id);
if ($this->Article->save($this->request->data)) {
$this->set('success', "<strong>SUCCES!</strong> New Article Created");
$data = array( 'title' => 'My new article', 'section_id' => $sectionId, 'text' => "Write Some Cool Stuff");
if ($this->Article->save($data)) {
Debugger::log("Saved Data:".$this->Article->id);
$this->set('success', true);
$this->set('article', $this->Article->findById($this->Article->id));
return;
}
$this->set('success', "<strong>FAIL!</strong> Can't Create Article");
Debugger::log("Didn;t Save Data");
}else{
Debugger::log("Request not post?");
}
$this->set('success', false);
}


Expand Down
65 changes: 65 additions & 0 deletions Controller/PostsController.php
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'));
}
}
}
?>
58 changes: 44 additions & 14 deletions Controller/SectionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ class SectionsController extends AppController {
public $helpers = array('Html', 'Form', 'Session', 'Js');
public $components = array('Session', 'Auth');


public function index() {
$this->layout = 'siteeng';
$sec = $this->Section->find('all', array('order' => array('order' => 'asc')));
Expand Down Expand Up @@ -40,31 +39,62 @@ public function editsite(){

public function edit($id = null) {
$this->layout = 'ajax';

if (!$id) {
$this->set('success', "<strong>FAIL!</strong> The section you tried to save was invalid");
$this->set('success', false);
return;
//throw new NotFoundException(__('Invalid post'));
}

$Section = $this->Section->findById($id);
if (!$Section) {
$this->set('success', "<strong>FAIL!</strong> The section you tried to save was invalid");
$this->set('success', false);
return;
//throw new NotFoundException(__('Invalid post'));
}

if ($this->request->is(array('post', 'put'))) {
if (!$this->request->is('get')) {
$this->Section->id = $id;
if ($this->Section->save($this->request->data)) {
$this->set('success', "<strong>SUCCES!</strong> All your changes are saved!");
$this->Session->setFlash(__('Your post has been updated.'));
$this->set('success', true);
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 section!");
}
$this->set('success', false);
}

public function draw($id) {
$this->layout = 'ajax';
$Section = $this->Section->findById($id);
if (!$Section) {
$this->set('success', $Sections);
return;
}
Configure::write('SiteEng.Run.Edit', true);
Configure::write('SiteEng.Run.EditClass', Configure::read('SiteEng.Site.EditClass'));
$this->set('editmode', true );
$this->set('sections', array($Section));
$this->render('index');
}

public function deleteArticle($id) {
$this->layout = 'ajax';
Debugger::log("Log Message");
if ($this->request->is('post')) {
$articleId = $this->passedArgs['articleId'];
if($articleId){
$this->Section->Article->delete($articleId);
$this->set('success', true);

}else{
Debugger::log("Skipping Article create");
}
}
$Section = $this->Section->findById($id);
if (!$Section) {
$this->set('success', $Sections);
return;
}
Configure::write('SiteEng.Run.Edit', true);
Configure::write('SiteEng.Run.EditClass', Configure::read('SiteEng.Site.EditClass'));
$this->set('editmode', true );
$this->set('sections', array($Section));
$this->render('index');
}
}

Expand Down
Empty file added Model/Behavior/empty
Empty file.
Empty file added Model/Datasource/empty
Empty file.
14 changes: 14 additions & 0 deletions Model/Post.php
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'
)
);
}


?>
6 changes: 5 additions & 1 deletion View/Articles/add.ctp
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";
}
?>
2 changes: 1 addition & 1 deletion View/Articles/delete.ctp
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php
echo $success;
echo json_encode($success);
?>
44 changes: 44 additions & 0 deletions View/Controller/AppController.php
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');
}
}
57 changes: 57 additions & 0 deletions View/Controller/ArticlesController.php
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 added View/Controller/Component/empty
Empty file.
Loading

0 comments on commit ab93c69

Please sign in to comment.