Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Training mahdy #3

Open
wants to merge 18 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
edit category
mohamedMahdyCc committed Jul 5, 2018
commit 71ed690e94748078c7bb2e6be61eb8ea3d06f1e8
22 changes: 21 additions & 1 deletion module/Category/config/module.config.php
Original file line number Diff line number Diff line change
@@ -29,6 +29,16 @@
],
],
],
'category-edit' => [
'type' => Segment::class,
'options' => [
'route' => '/category/edit[/:id]',
'defaults' => [
'controller' => Controller\CategoryController::class,
'action' => 'edit',
],
],
],
'category-add-ajax' => [
'type' => Segment::class,
'options' => [
@@ -39,6 +49,16 @@
],
],
],
'category-edit-ajax' => [
'type' => Segment::class,
'options' => [
'route' => '/category/update',
'defaults' => [
'controller' => Controller\CategoryController::class,
'action' => 'updateAjax',
],
],
],
],
],
'controllers' => [
@@ -56,7 +76,7 @@
'layout/layout' => __DIR__ . '/../../../themes/default/layout/layout.phtml',
'category/category/index' => __DIR__ . '/../view/category/index.phtml',
'category/category/add' => __DIR__ . '/../view/category/add.phtml',
// 'catalog/catalog/edit' => __DIR__ . '/../view/catalog/edit.phtml',
'category/category/edit' => __DIR__ . '/../view/category/edit.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
],
20 changes: 18 additions & 2 deletions module/Category/src/Controller/CategoryController.php
Original file line number Diff line number Diff line change
@@ -35,13 +35,29 @@ public function addAjaxAction()
// Fill in the form with POST data
$data = $this->params()->fromPost();
// var_dump($data);
$category->save($data);
$category->create($data);
}
die;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not die inside a controller. Please see this URL about ways of returning different output (i.e. not HTML):
https://olegkrivtsov.github.io/using-zend-framework-3-book/html/en/Model_View_Controller/View_Rendering_Strategies.html

}

public function editAction()
{
return new ViewModel();
// Get post ID.
$id = (int)$this->params()->fromRoute('id', -1);
$category = new CategoryRepository($this->entityManager);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above re: using service instead of business logic directly inside controller.

$data = $category->find($id);

return new ViewModel(['category' => $data]);
}

public function updateAjaxAction()
{
if($this->getRequest()->isPost()) {
$category = new CategoryRepository($this->entityManager);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above re: using service instead of business logic directly inside controller.

// Fill in the form with POST data
$data = $this->params()->fromPost();
$category->update($data);
}
die;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above re: die.

}
}
30 changes: 28 additions & 2 deletions module/Category/src/Repository/CategoryRepository.php
Original file line number Diff line number Diff line change
@@ -27,8 +27,19 @@ public function findAll()
return $categoryRepository->findAll();
}

// add - update method
public function save($data){
public function find($id){
$row = $this->entityManager->getRepository(Category::class)
->findOneById($id);
return $row;
}

public function findObj($id){
$row = $this->entityManager->getRepository(Category::class)->find($id);
return $row;
}

// add method
public function create($data){
// Create new Post entity.
$category = new Category();
// $category->name = $data['name'];
@@ -42,6 +53,21 @@ public function save($data){

}

// update method
public function update($data){

$category = new Category();
$category->setId($data['id']);
$category->setName($data['name']);

// Add the entity to entity manager.
$this->entityManager->merge($category);

// Apply changes to database.
$this->entityManager->flush();

}

// add - update method
public function delete(){

26 changes: 26 additions & 0 deletions module/Category/view/category/edit.phtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<div class="jumbotron">
<h2>Edit Category Information</h2>
</div>
<div class="row col-md-6 col-md-offset-2 custyle">
<a href="<?php echo $this->url('category-list') ?>" class="btn btn-primary"><b>+</b> All Categories</a>
<br><br>
<form role="form">
<div class="alert hidden"></div>
<div class="form-group">
<input type="text" class="form-control" id='name' name="name" value='<?php echo $category->name ?>' placeholder="Name" required>
</div>
<button type="button" id="submit" name="submit" class="btn btn-primary pull-right">Update</button>
</form>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#submit').click(submit);

function submit(){
$.post( "<?php echo $this->url('category-edit-ajax') ?>",{name:$('#name').val(),id:'<?php echo $category->id ?>'} ,function( data ) {
var response = '<i class="glyphicon glyphicon-check"></i> Data Saved, Thanks! ';
$('.alert').html(response).removeClass('hidden').addClass('alert-success');
});
}
});
</script>
2 changes: 1 addition & 1 deletion module/Category/view/category/index.phtml
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@
<td><?php echo $cat->name ?></td>
<td class="text-center">
<a class='btn btn-default btn-xs' href="javascript:alert('in progress...')"><span class="glyphicon glyphicon-eye-open"></span> View Products </a>
<a class='btn btn-info btn-xs' href="#"><span class="glyphicon glyphicon-edit"></span> Edit</a>
<a class='btn btn-info btn-xs' href="<?php echo $this->url('category-edit').'/'.$cat->id ?>"><span class="glyphicon glyphicon-edit"></span> Edit</a>
<a href="#" class="btn btn-danger btn-xs"><span class="glyphicon glyphicon-remove"></span> Delete</a>
</td>
</tr>