Skip to content

Commit e033a4c

Browse files
committed
[Software maintenance - Perfective]
* C.R.U.D implemented * TODO - Verify post and put request. Not working
1 parent 7ea6e15 commit e033a4c

File tree

10 files changed

+197
-110
lines changed

10 files changed

+197
-110
lines changed

app/routes.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
$app->group('/painel', function() use ($app) {
77
$app->get('',App\Action\CMSAction::class . ':page')->setName('homepage');
8-
$app->map(['GET', 'POST'], '/menu', App\Action\CMSAction::class . ':page');
8+
$app->map(['GET', 'POST','DELETE','PUT'], '/menu', App\Action\CMSAction::class . ':page');
99
$app->get('/page',App\Action\CMSAction::class . ':page');
1010
$app->get('/page-details',App\Action\CMSAction::class . ':page');
1111
$app->get('/img-video',App\Action\CMSAction::class . ':page');

app/src/Action/CMSAction.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,19 @@ public function __invoke(Request $request, Response $response, $args) {
4747
*/
4848
public function page(Request $request, Response $response, $args) {
4949
$this->logger->info("Page action dispatched");
50+
5051
$page = $request->getUri()->getPath() == '/painel' ? explode("/", $request->getUri()->getPath())[1] : explode("/", $request->getUri()->getPath())[2];
5152

5253
$data = $this->resource($page,$request,$response,$args);
5354

54-
if($request->getParam('id')){
55+
if($request->getParam('id') && !$request->isPost()){
5556
return $response->withStatus(200)
5657
->withHeader('Content-Type', 'application/json')
5758
->write(json_encode($data));
5859
}else{
60+
5961
$this->view->render($response, $page.'.twig', array("partials" => $page,"data" =>$data));
62+
6063
return $response;
6164
}
6265
}

app/src/Action/DefaultAction.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,21 @@ public function isAuthorized(Request $request){
2020
}
2121
}
2222

23+
public function object_to_array($data) {
24+
if ((! is_array($data)) and (! is_object($data))) return 'xxx'; //$data;
25+
26+
$result = array();
27+
28+
$data = (array) $data;
29+
foreach ($data as $key => $value) {
30+
if (is_object($value)) $value = (array) $value;
31+
if (is_array($value))
32+
$result[$key] = self::object_to_array($value);
33+
else
34+
$result[$key] = $value;
35+
}
36+
37+
return $result;
38+
}
39+
2340
}

app/src/Entity/AbstractEntity.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Created by IntelliJ IDEA.
4+
* User: bruno
5+
* Date: 04/03/18
6+
* Time: 21:35
7+
*/
8+
9+
namespace App\Entity;
10+
11+
12+
abstract class AbstractEntity {
13+
14+
public function getAvoidedFields() {
15+
return array ();
16+
}
17+
18+
public function toArray() {
19+
$temp = ( array ) $this;
20+
21+
$array = array ();
22+
23+
foreach ( $temp as $k => $v ) {
24+
$k = preg_match ( '/^\x00(?:.*?)\x00(.+)/', $k, $matches ) ? $matches [1] : $k;
25+
if (in_array ( $k, $this->getAvoidedFields () )) {
26+
$array [$k] = "";
27+
} else {
28+
29+
// if it is an object recursive call
30+
if (is_object ( $v ) && $v instanceof AbstractEntity) {
31+
$array [$k] = $v->toArray();
32+
}
33+
// if its an array pass por each item
34+
if (is_array ( $v )) {
35+
36+
foreach ( $v as $key => $value ) {
37+
if (is_object ( $value ) && $value instanceof AbstractEntity) {
38+
$arrayReturn [$key] = $value->toArray();
39+
} else {
40+
$arrayReturn [$key] = $value;
41+
}
42+
}
43+
$array [$k] = $arrayReturn;
44+
}
45+
// if it is not a array and a object return it
46+
if (! is_object ( $v ) && !is_array ( $v )) {
47+
$array [$k] = $v;
48+
}
49+
}
50+
}
51+
52+
return $array;
53+
}
54+
}

app/src/Entity/Menu.php

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @ORM\Table(name="menu", indexes={@ORM\Index(name="menu_menu", columns={"menu_id"})})
1212
* @ORM\Entity
1313
*/
14-
class Menu
14+
class Menu extends AbstractEntity
1515
{
1616
/**
1717
* @var integer
@@ -36,13 +36,6 @@ class Menu
3636
*/
3737
private $enabled;
3838

39-
/**
40-
* @var \DateTime
41-
*
42-
* @ORM\Column(name="data_created", type="datetime", nullable=false)
43-
*/
44-
private $dataCreated = 'CURRENT_TIMESTAMP';
45-
4639
/**
4740
* @var \App\Entity\Menu
4841
*
@@ -76,17 +69,21 @@ public function __construct()
7669
$this->pages = new ArrayCollection();
7770
}
7871

79-
8072
/**
81-
* Get id
82-
*
83-
* @return integer
73+
* @return int
8474
*/
85-
public function getId()
86-
{
75+
public function getId() {
8776
return $this->id;
8877
}
8978

79+
/**
80+
* @param int $id
81+
*/
82+
public function setId($id) {
83+
$this->id = $id;
84+
}
85+
86+
9087
/**
9188
* Set description
9289
*
@@ -135,30 +132,6 @@ public function getEnabled()
135132
return $this->enabled;
136133
}
137134

138-
/**
139-
* Set dataCreated
140-
*
141-
* @param \DateTime $dataCreated
142-
*
143-
* @return Menu
144-
*/
145-
public function setDataCreated($dataCreated)
146-
{
147-
$this->dataCreated = $dataCreated;
148-
149-
return $this;
150-
}
151-
152-
/**
153-
* Get dataCreated
154-
*
155-
* @return \DateTime
156-
*/
157-
public function getDataCreated()
158-
{
159-
return $this->dataCreated;
160-
}
161-
162135
/**
163136
* Set menu
164137
*

app/src/Resource/MenuResource.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use App\Entity\Menu;
1313
use Doctrine\ORM\EntityManager;
14+
use Doctrine\ORM\Query;
1415
use Slim\Http\Request;
1516
use Slim\Http\Response;
1617

@@ -67,7 +68,7 @@ function get(Request $request, $args) {
6768

6869
if($request->getParam("id")!=null){
6970
$menuEdit = $this->entityManager->getRepository($this->REPOSITORY)->findOneBy(array('id'=>$request->getParam('id')));
70-
$data->menuEdit = $menuEdit;
71+
$data->menuEdit = $menuEdit->toArray();
7172
}
7273
$menus = $this->entityManager->getRepository($this->REPOSITORY)->findAll();
7374
$data->menus = $menus;
@@ -82,9 +83,14 @@ function get(Request $request, $args) {
8283
*/
8384
function put(Request $request, $args) {
8485
$objMenu = new Menu();
86+
$objMenu->setId($request->getParam("txtMenuEdit"));
8587
$objMenu->setDescription($request->getParam('txtDescricao'));
86-
$objMenu->setEnabled(true);
87-
$objMenu->setMenu($request->getParam('chkStatus'));
88+
$objMenu->setEnabled((bool)$request->getParam('chkStatus') ? 1 : 0);
89+
90+
if($request->getParam('txtMenu')){
91+
$menuEdit = $this->entityManager->getRepository($this->REPOSITORY)->findOneBy(array('id'=>$request->getParam('txtMenu')));
92+
$objMenu->setMenu($menuEdit);
93+
}
8894

8995
$this->entityManager->merge($objMenu);
9096
$this->entityManager->flush();
@@ -100,7 +106,6 @@ function post(Request $request, $args) {
100106
$objMenu = new Menu();
101107
$objMenu->setDescription($request->getParam('txtDescricao'));
102108
$objMenu->setEnabled($request->getParam('chkStatus') == 'A' ? true : false );
103-
$objMenu->setDataCreated(new \DateTime());
104109
if(intval($request->getParam('txtMenu')) != 0){
105110
$parent = $this->entityManager->getRepository($this->REPOSITORY)->findOneBy(array('id' => $request->getParam('txtMenu')));
106111
$objMenu->setMenu($parent);
@@ -119,7 +124,7 @@ function post(Request $request, $args) {
119124
* @throws \Doctrine\ORM\OptimisticLockException
120125
*/
121126
function delete(Request $request, $args) {
122-
$objMenu = $this->entityManager->getRepository($this->REPOSITORY)->findOneBy(array('id' => $request->getParam('txtMenu')));
127+
$objMenu = $this->entityManager->getRepository($this->REPOSITORY)->findOneBy(array('id' => $request->getParam('id')));
123128
$this->entityManager->remove($objMenu);
124129
$this->entityManager->flush();
125130
}

app/templates/admin-cms/default/base.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!DOCTYPE html>
2-
<html prefix="og: http://ogp.me/ns#" itemscope itemtype="http://schema.org/Article">
2+
<html prefix="og: http://ogp.me/ns#" itemscope itemtype="http://schema.org/Article" id="mainHtml">
33
<!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7"><![endif]-->
44
<!--[if IE 7]><html class="no-js lt-ie9 lt-ie8"><![endif]-->
55
<!--[if IE 8]><html class="no-js lt-ie9"><![endif]-->

app/templates/admin-cms/partials/menu.twig

Lines changed: 35 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,52 @@
11
{% extends "base.html.twig" %}
22

33
{% block content %}
4-
<div class="row">
4+
<div class="row" id="mainMenu">
55
<div class="col-lg-12">
66

77
<section class="panel">
88
<div class="panel-body">
9-
<button class="btn btn-success" data-toggle="modal" id="btnNovoRegistro">Novo Registro</button>
10-
</div>
9+
<header class="panel-heading ">
10+
Novo Registro
11+
<span class="tools pull-right">
12+
<a class="fa fa-repeat box-refresh" href="javascript:;" onclick="resetForm('formMenu')"></a>
13+
<a class="t-collapse fa fa-chevron-down" href="javascript:;"></a>
14+
</span>
15+
</header>
1116

12-
<!-- Modal -->
13-
<div class="modal fade" id="menuModal" tabindex="-1" role="dialog" aria-labelledby="menuModal"
14-
aria-hidden="true">
15-
<div class="modal-dialog">
16-
<div class="modal-content">
17-
<div class="modal-header">
18-
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;
19-
</button>
20-
<h4 class="modal-title">Novo registro</h4>
17+
<div class="panel-body">
18+
19+
<form role="form" id="formMenu" enctype="multipart/form-data">
20+
<div class="form-group">
21+
<label for="txtMenu">Menu</label>
22+
<select class="form-control select2" id="txtMenu" name="txtMenu">
23+
<option value="">Selecione uma opção</option>
24+
{% for menu in data.menus %}
25+
<option value="{{ menu.id }}" {{ selected }}>{{ menu.description }}</option>
26+
{% endfor %}
27+
</select>
28+
</div>
29+
30+
<div class="form-group">
31+
<label for="menu">Descri&ccedil;&atilde;o</label>
32+
<input type="text" class="form-control" id="txtDescricao" name="txtDescricao" placeholder="Informe uma descrição. Se informar menu, este será sub-menu">
2133
</div>
22-
<div class="modal-body">
23-
<form role="form" enctype="multipart/form-data" id="form-menu-modal">
24-
<div class="form-group">
25-
<label for="txtMenu">Menu</label>
26-
<select class="form-control select2" id="txtMenu" name="txtMenu">
27-
<option value="">Selecione uma opção</option>
28-
{% for menu in data.menus %}
29-
{% set selected = '' %}
30-
{% if (menu.id == menuEdit.id) %}
31-
{% set selected = 'selected' %}
32-
{% endif %}
3334

34-
<option value="{{ menu.id }}" {{ selected }}>{{ menu.description }}</option>
35-
{% endfor %}
36-
</select>
37-
</div>
35+
<div class="form-group">
36+
<input id="chkStatus" type="checkbox" name="chkStatus" class="form-control js-switch-blue"
37+
checked/>
38+
<label for="chkStatus" class="control-label">Ativar/Inativar Registro</label>
39+
</div>
3840

39-
<div class="form-group">
40-
<label for="menu">Descri&ccedil;&atilde;o</label>
41-
<input type="text" class="form-control" id="menu" name="txtDescricao"
42-
placeholder="Informe uma descrição. Se informar menu, este será sub-menu">
43-
</div>
41+
<input type="text" id="txtMenuEdit" name="txtMenuEdit"/>
4442

45-
<div class="form-group">
46-
<input id="chkStatus" type="checkbox" name="chkStatus" class="form-control js-switch-blue"
47-
checked/>
48-
<label for="chkStatus" class="control-label">Ativar/Inativar Registro</label>
49-
</div>
43+
<button class="btn btn-success" type="submit" id="btnSalvar">Salvar</button>
5044

51-
<div class="modal-footer">
52-
<button data-dismiss="modal" class="btn btn-default" type="button">Cancelar</button>
53-
<button class="btn btn-success" type="submit" id="btnSalvar">Salvar</button>
54-
</div>
55-
</form>
45+
</form>
5646

57-
</div>
58-
</div>
5947
</div>
48+
6049
</div>
61-
<!-- modal -->
6250

6351
<!-- Modal -->
6452
<div class="modal fade" id="removeData" data-toggle="modal" tabindex="-1" role="dialog" aria-labelledby="removeData"
@@ -97,7 +85,7 @@
9785
<table class="table colvis-data-table data-table">
9886
<thead>
9987
<tr>
100-
<th width="5%">&nbsp;</th>
88+
<th width="5%">#</th>
10189
<th width="40%">MENU</th>
10290
<th width="50%">SUB-MENU</th>
10391
<th width="5%">AÇÃO</th>
@@ -107,8 +95,8 @@
10795
{% for menu in data.menus %}
10896
<tr>
10997
<td>{{ menu.id }}</td>
98+
<td>{{ menu.menu.description }}</td>
11099
<td>{{ menu.description }}</td>
111-
<td>{{ menu.menu.id }} - {{ menu.menu.description }}</td>
112100
<td>
113101
<button class="btn btn-primary btn-xs" id="btnEditMenu" data-id="{{ menu.id }}"><i class="fa fa-pencil"></i></button>
114102
<button class="btn btn-danger btn-xs" id="btnUpdateMenu" data-id="{{ menu.id }}"><i class="fa fa-trash-o "></i></button>

0 commit comments

Comments
 (0)