diff --git a/.gitignore b/.gitignore index 81d6905..439d232 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .idea .env -vendor \ No newline at end of file +vendor +/public/images/uploads/ \ No newline at end of file diff --git a/app/Controller/Admin/CategoryController.php b/app/Controller/Admin/CategoryController.php index fa4851d..fb19da7 100644 --- a/app/Controller/Admin/CategoryController.php +++ b/app/Controller/Admin/CategoryController.php @@ -5,7 +5,7 @@ use Core\Controller; use Slim\Http\Request; use Slim\Http\Response; -use App\Model\Category; +use App\Model\Categories; use Core\Validate; use Core\Redirect; @@ -15,7 +15,7 @@ class CategoryController extends Controller { */ public function index() { - $category = new Category; + $category = new Categories; $categories = $category->select()->busca("name")->paginate(3)->get(); $this->view("admin/categories/index", [ "template_admin" => $this->templateAdmin, @@ -37,19 +37,16 @@ public function create() { */ public function store(Request $request, Response $response) { //$data = filter_input_array(($_SERVER['REQUEST_METHOD'] == "POST") ? INPUT_POST : INPUT_GET, FILTER_SANITIZE_STRING); - $validate = new Validate(); + $validate = new Validate($_POST); $data = $validate->validate([ "name" => "required" ]); if($validate->hasErros()){ - foreach($data as $field => $value){ - flash("post_".$field,$data[$field]); - } - back(); + return $this->view("admin/categories/create", ["template_admin" => $this->templateAdmin,"post" => $_POST]); } - $category = new Category; + $category = new Categories; $category->create($data); if($category->lastCreated > 0){ flash("name",success("Cadastrado com sucesso")); @@ -68,7 +65,7 @@ public function show($id) { * Exibe o formulário de edição */ public function edit(Request $request, Response $response, $args) { - $category = new Category; + $category = new Categories; $category = $category->select()->where2(["id","=",$args["id"]])->first(); return $response->write($this->view("admin/categories/edit", [ "template_admin" => $this->templateAdmin, @@ -82,30 +79,33 @@ public function edit(Request $request, Response $response, $args) { */ public function update(Request $request, Response $response, $args) { - $validate = new Validate(); + $validate = new Validate($_POST); $data = $validate->validate([ "name" => "required" ]); - + $category = new Categories; if($validate->hasErros()){ - foreach($data as $field => $value){ - flash("post_".$field,$data[$field]); - } - back(); - } - $category = new Category; - $category->update2(["name" => $data["name"]])->where2(["id","=",$args["id"]])->exec(); - return Redirect::redirect("/painel/admin/category"); + + $category = $category->select()->where2(["id","=",$args["id"]])->first(); + return $response->write($this->view("admin/categories/edit", [ + "template_admin" => $this->templateAdmin, + "category" => $category, + "post" => $_POST + ])); + + } + $category->update2(["name" => $data["name"]])->where2(["id","=",$args["id"]])->exec(); + return Redirect::redirect("/painel/admin/category"); } /** * Remove dados do Banco */ public function destroy(Request $request, Response $response, $args) { - $category = new Category(); + $category = new Categories(); $category->delete2()->where2(["id","=",$args["id"]])->exec(); return Redirect::redirect("/painel/admin/category"); diff --git a/app/Controller/Admin/LoginController.php b/app/Controller/Admin/LoginController.php index 76dca0c..6dcbdfd 100644 --- a/app/Controller/Admin/LoginController.php +++ b/app/Controller/Admin/LoginController.php @@ -37,7 +37,7 @@ public function create() */ public function store(Request $request, Response $response) { - $validate = new Validate(); + $validate = new Validate($_POST); $data = $validate->validate([ "email" => "required:email", "password" => "required" diff --git a/app/Controller/Admin/PostController.php b/app/Controller/Admin/PostController.php index 433c217..58f557a 100644 --- a/app/Controller/Admin/PostController.php +++ b/app/Controller/Admin/PostController.php @@ -2,9 +2,15 @@ namespace App\Controller\Admin; +use App\Model\Categories; +use App\Model\User; use Core\Controller; +use Core\Image; +use Core\Redirect; +use Core\Validate; use Slim\Http\Request; use Slim\Http\Response; +use App\Model\Post; class PostController extends Controller { /** @@ -12,7 +18,13 @@ class PostController extends Controller { */ public function index() { - $this->view("admin/post/index", ["template_admin" => $this->templateAdmin]); + $posts = new Post(); + + $this->view("admin/post/index", [ + "template_admin" => $this->templateAdmin, + "posts" => $posts->list(), + "links" => $posts->links() + ]); } @@ -20,14 +32,45 @@ public function index() { * Exibe o formulário de criação */ public function create() { - $this->view("admin/post/create", ["template_admin" => $this->templateAdmin]); + + $categories = new Categories(); + $categories = $categories->getAll(); + + $this->view("admin/post/create", + [ + "template_admin" => $this->templateAdmin, + "categories" => $categories + ]); } /** * Processa Formulário de criação */ public function store(Request $request, Response $response) { - echo 'store'; + + + $validate = new Validate($_POST); + $data = $validate->validate([ + "title" => "required", + "content" => "required", + "category_id" => "required" + ]); + if($validate->hasErros()){ + foreach($data as $field => $value){ + flash("post_".$field,$data[$field]); + } + back(); + } + + $post = new Post; + $image = new Image("thumbnail"); + $data["thumbnail"] = $image->size("capa")->upload(); + $data["user_id"] = (new User)->user()->id; + $post->create($data); + if($post->lastCreated > 0){ + flash("name",success("Cadastrado com sucesso")); + Redirect::redirect("/painel/admin/posts"); + } } /** @@ -40,22 +83,68 @@ public function show($id) { /** * Exibe o formulário de edição */ - public function edit($id) { - echo 'edit'; + public function edit(Request $request, Response $response, $args) { + + $bodyRequest = $request->getParsedBody(); + $categories = new Categories(); + $categories = $categories->getAll(); + + $post = new Post(); + $post = $post->select()->where2(["id","=",$args["id"]])->first(); + + if(!is_null($bodyRequest)){ + $post = get_object_vars($post); + foreach ($post as $key => $value){ + if(!key_exists($key,$bodyRequest)){ + $bodyRequest[$key] = $value; + } + } + } + + $this->view("admin/post/edit", + [ + "template_admin" => $this->templateAdmin, + "categories" => $categories, + "post" => (is_null($bodyRequest)) ? $post : $bodyRequest + ]); } /** * Processa o formulário de edição */ public function update(Request $request, Response $response, $args) { - echo 'update'; + $validate = new Validate($request->getParsedBody()); + $data = $validate->validate([ + "title" => "required", + "content" => "required", + "category_id" => "required" + ]); + if($validate->hasErros()){ + $this->edit($request,$response,$args); + exit; + } + + $post = new Post; + if(!$_FILES["thumbnail"]["error"]){ + $image = new Image("thumbnail"); + $data["thumbnail"] = $image->size("capa")->upload(); + } + $data["user_id"] = (new User)->user()->id; + $result = $post->update2($data)->where2(["id","=", $args["id"]])->exec(); + if($result){ + Redirect::redirect("/painel/admin/posts"); + } + } /** * Remove dados do Banco */ - public function destroy($id) { - echo 'destroy'; + public function destroy(Request $request, Response $response, $args) { + $post = new Post; + $post->delete2()->where2(["id","=",$args["id"]])->exec(); + + return Redirect::redirect("/painel/admin/posts"); } } \ No newline at end of file diff --git a/app/Functions/twig.php b/app/Functions/twig.php index 7df93db..a7e18cf 100755 --- a/app/Functions/twig.php +++ b/app/Functions/twig.php @@ -12,7 +12,6 @@ $sent = new TwigFunction("sent",function($index){ echo Flash::get("post_".$index); - }); $message = new TwigFunction("message",function($index) { diff --git a/app/Model/Category.php b/app/Model/Categories.php similarity index 71% rename from app/Model/Category.php rename to app/Model/Categories.php index c03ce66..de35511 100644 --- a/app/Model/Category.php +++ b/app/Model/Categories.php @@ -4,7 +4,7 @@ use Core\Model; -class Category extends Model { +class Categories extends Model { protected $table = "categories"; } \ No newline at end of file diff --git a/app/Model/Post.php b/app/Model/Post.php new file mode 100644 index 0000000..379207d --- /dev/null +++ b/app/Model/Post.php @@ -0,0 +1,26 @@ +select("posts.id, + posts.title, + posts.content, + posts.thumbnail as capa, + posts.status as status, + categories.name as category, + users.name as user, + posts.created_at as criacao") + ->join("categories","posts.category_id","categories.id") + ->join("users","users.id","posts.user_id") + ->order("order by posts.id desc") + ->busca("title,content,status") + ->paginate(1) + ->get(); + } +} \ No newline at end of file diff --git a/app/traits/Read.php b/app/traits/Read.php index b6aa2e9..5a4b596 100755 --- a/app/traits/Read.php +++ b/app/traits/Read.php @@ -17,7 +17,7 @@ trait Read * @return User */ public function select($fields = "*"){ - $this->sql = "SELECT {$fields} FROM {$this->table}"; + $this->sql = "SELECT {$fields} FROM {$this->table} "; return $this; } @@ -31,11 +31,16 @@ public function bindAndExecute():\PDOStatement{ return $select; } + public function getAll(){ + return $this->select()->get(); + } + /** * Método responsável por Obter a quantidade de registros para a paginação. * @return Int */ public function count():int { + $select = $this->connection->prepare($this->sql); $select->execute($this->binds); return $select->rowCount(); @@ -56,6 +61,7 @@ public function first(){ * @return array */ public function get():array{ + $select = $this->bindAndExecute(); return $select->fetchAll(); } @@ -63,8 +69,8 @@ public function get():array{ public function exec(){ $select = $this->connection->prepare($this->sql); - $select->execute($this->binds); - return $this; + + return $select->execute($this->binds); } /** * Melhorar este método para que possa aceitar a o operador AND, por exemplo: @@ -140,16 +146,52 @@ public function busca($fields){ $fields = explode(",",$fields); - $this->sql .= " WHERE "; + if(!empty(busca())){ + if(substr_count($this->sql,"WHERE") > 0){ + $this->sql .= " AND "; + }else{ + $this->sql .= " WHERE "; + } + + foreach($fields as $field){ + /* + if(substr_count($field,".") > 0){ + + list($table,$column) = explode(".",$field); + $tabela = $table; + $table = "\\App\\Model\\".ucfirst($table); + $model = new $table; + $id = $model->select("id")->where2(["name","LIKE", "%".busca()."%"])->first(); + $this->sql .= " ".$tabela.".id = :".$tabela.".id"." OR "; + $this->binds[$column] = $id; + + }else{ + $this->sql .= " {$field} LIKE :{$field} OR "; + $this->binds[$field] = "%".busca()."%"; + } + */ + + $this->sql .= " {$field} LIKE :{$field} OR "; + $this->binds[$field] = "%".busca()."%"; + } + $this->sql = rtrim($this->sql,"OR "); - foreach($fields as $field){ - $this->sql .= " {$field} LIKE :{$field} OR "; - $this->binds[$field] = "%".busca()."%"; } - $this->sql = rtrim($this->sql,"OR "); - return $this; } + + public function join($table,$column1,$column2){ + + $this->sql .= " INNER JOIN {$table} ON({$column1} = {$column2}) "; + + return $this; + + } + + public function order($order){ + $this->sql .= " {$order} "; + return $this; + } } \ No newline at end of file diff --git a/app/traits/Sanitize.php b/app/traits/Sanitize.php index 34eef78..0900582 100755 --- a/app/traits/Sanitize.php +++ b/app/traits/Sanitize.php @@ -9,7 +9,7 @@ protected function sanitize(array $data):array { $sanitezed = []; foreach ($data as $field => $value){ - $sanitezed[$field] = filter_var($value,FILTER_SANITIZE_STRING); + $sanitezed[$field] = rtrim(filter_var($value,FILTER_SANITIZE_STRING)); } return $sanitezed; diff --git a/app/traits/Validations.php b/app/traits/Validations.php index b2ad2ea..462cfba 100755 --- a/app/traits/Validations.php +++ b/app/traits/Validations.php @@ -15,8 +15,8 @@ trait Validations */ protected function required($field){ - if(empty($_POST[$field]) OR !isset($_POST[$field])){ - $_POST[$field] = ""; + + if(empty($this->data[$field])){ $this->erros[$field][] = flash($field, error("Favor preencha esse campo")); } } diff --git a/composer.json b/composer.json index 03f3a9d..f05b1e2 100755 --- a/composer.json +++ b/composer.json @@ -16,7 +16,8 @@ "intervention/image": "^2.5", "phpmailer/phpmailer": "^6.1", "illuminate/database": "^6.16", - "illuminate/pagination": "^6.16" + "illuminate/pagination": "^6.16", + "nesbot/carbon": "^2.31" }, "autoload": { "psr-4": { diff --git a/composer.lock b/composer.lock index 123e9b4..be64134 100755 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "0793e010c3dc76b1e8f8ddd5b223af1d", + "content-hash": "732e971894558cbee9eb5c2509ad7212", "packages": [ { "name": "cakephp/cache", @@ -948,16 +948,16 @@ }, { "name": "nesbot/carbon", - "version": "2.30.0", + "version": "2.31.0", "source": { "type": "git", "url": "https://github.com/briannesbitt/Carbon.git", - "reference": "912dff66d2690ca66abddb9b291a1df5f371d3b4" + "reference": "bbc0ab53f41a4c6f223c18efcdbd9bc725eb5d2d" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/912dff66d2690ca66abddb9b291a1df5f371d3b4", - "reference": "912dff66d2690ca66abddb9b291a1df5f371d3b4", + "url": "https://api.github.com/repos/briannesbitt/Carbon/zipball/bbc0ab53f41a4c6f223c18efcdbd9bc725eb5d2d", + "reference": "bbc0ab53f41a4c6f223c18efcdbd9bc725eb5d2d", "shasum": "" }, "require": { @@ -1014,7 +1014,7 @@ "datetime", "time" ], - "time": "2020-02-07T15:25:46+00:00" + "time": "2020-03-01T11:11:58+00:00" }, { "name": "nikic/fast-route", diff --git a/public/assets/images/uploads/08ebeb5d49dfe96d15de66c76c7773ff_19032020-210429.jpg b/public/assets/images/uploads/08ebeb5d49dfe96d15de66c76c7773ff_19032020-210429.jpg new file mode 100644 index 0000000..1532d30 Binary files /dev/null and b/public/assets/images/uploads/08ebeb5d49dfe96d15de66c76c7773ff_19032020-210429.jpg differ diff --git a/public/assets/images/uploads/095251b015d0e326ffae5fd4bfb0e5e6_19032020-210332.png b/public/assets/images/uploads/095251b015d0e326ffae5fd4bfb0e5e6_19032020-210332.png new file mode 100644 index 0000000..95730c3 Binary files /dev/null and b/public/assets/images/uploads/095251b015d0e326ffae5fd4bfb0e5e6_19032020-210332.png differ diff --git a/public/assets/images/uploads/76a66b1a61d04c16aa34b777395cc8ac_19032020-203122.jpg b/public/assets/images/uploads/76a66b1a61d04c16aa34b777395cc8ac_19032020-203122.jpg new file mode 100644 index 0000000..1532d30 Binary files /dev/null and b/public/assets/images/uploads/76a66b1a61d04c16aa34b777395cc8ac_19032020-203122.jpg differ diff --git a/public/assets/images/uploads/788825f9e902d250f19e7b78641c4039_05042020-164830.jpg b/public/assets/images/uploads/788825f9e902d250f19e7b78641c4039_05042020-164830.jpg new file mode 100644 index 0000000..dbd1d56 Binary files /dev/null and b/public/assets/images/uploads/788825f9e902d250f19e7b78641c4039_05042020-164830.jpg differ diff --git a/public/assets/images/uploads/d1713a232fadbb418589934876a2f73c_20032020-121810.png b/public/assets/images/uploads/d1713a232fadbb418589934876a2f73c_20032020-121810.png new file mode 100644 index 0000000..f13d294 Binary files /dev/null and b/public/assets/images/uploads/d1713a232fadbb418589934876a2f73c_20032020-121810.png differ diff --git a/public/assets/images/uploads/d5fff5ece8378f23418d843c4e7c4f55_19032020-203021.png b/public/assets/images/uploads/d5fff5ece8378f23418d843c4e7c4f55_19032020-203021.png new file mode 100644 index 0000000..e4eabb0 Binary files /dev/null and b/public/assets/images/uploads/d5fff5ece8378f23418d843c4e7c4f55_19032020-203021.png differ diff --git a/public/assets/js/categories.js b/public/assets/js/categories.js new file mode 100644 index 0000000..29691e1 --- /dev/null +++ b/public/assets/js/categories.js @@ -0,0 +1,35 @@ +console.log("fetch" in window); +var form = document.querySelector(".form-edit-category"); + +form.addEventListener("submit",function(ev){ + ev.preventDefault(); + + url = this.getAttribute("action"); + let formData = new FormData(this); + + + xmlHttpPOST(url,function () { + success(function(){ + let response = JSON.parse(xmlHttp.responseText); + }); + },formData); + +}); + +function ajaxRequest(url,form){ + + fetch(url,{ + method: "POST", + body: form + }) + .then(response => response.json()) + .then(data => mostraRetorno(data)) + .catch(erro => console.log(erro.message)); +} + +function mostraRetorno(d) { + console.log(d); +} + + + diff --git a/public/assets/js/xhttp.js b/public/assets/js/xhttp.js new file mode 100644 index 0000000..f87289f --- /dev/null +++ b/public/assets/js/xhttp.js @@ -0,0 +1,48 @@ +var xmlHttp = new XMLHttpRequest(); + + +function xmlHttpGET(url,callback,parameters = ""){ + + xmlHttp.open("GET",url+parameters,true); + + xmlHttp.onerror = function(){console.log ("** Ocorreu um erro durante a transação ***")}; + + xmlHttp.send(); + + xmlHttp.onreadystatechange = callback; + +}; + +function xmlHttpPOST(url,callback,parameters = ""){ + + xmlHttp.open("POST",url,true); + + xmlHttp.onerror = function(){console.log ("** Ocorreu um erro durante a transação ***")}; + + if(typeof parameters != "object"){ + xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + } + + xmlHttp.send(parameters); + + xmlHttp.onreadystatechange = callback; + + + +}; + +function beforeSend(callback){ + + if(xmlHttp.readyState == 3){ + callback(); + } + +}; + +function success(callback){ + + if(xmlHttp.readyState == 4 && xmlHttp.status == 200){ + callback(); + } + +}; diff --git a/public/index.php b/public/index.php index f7ae534..553c2d6 100755 --- a/public/index.php +++ b/public/index.php @@ -21,6 +21,13 @@ $app->post("/category/create", "CategoryController:store"); $app->post("/category/{id}", "CategoryController:update"); $app->get("/category/delete/{id}", "CategoryController:destroy"); + + $app->get("/posts[/]", "PostController:index"); + $app->get("/post/create", "PostController:create"); + $app->get("/post/{id}", "PostController:edit"); + $app->post("/post/create", "PostController:store"); + $app->post("/post/{id}", "PostController:update"); + $app->get("/post/delete/{id}", "PostController:destroy"); })->add($middleware->auth(1)); $app->group("/painel/professor", function () use ($app) { diff --git a/src/Image.php b/src/Image.php index 482d875..3560347 100755 --- a/src/Image.php +++ b/src/Image.php @@ -20,6 +20,8 @@ class Image protected $type; + protected $path = "assets/images/uploads/"; + public function __construct($imageNameInput) { $this->intervention = new ImageManager(); @@ -83,9 +85,11 @@ private function doUpload(){ if($this->type == "avatar"){ $background = $this->intervention->canvas(90,90); $background->insert($image,"center"); - $background->save("assets/images/uploads/{$this->getName()}"); + $background->save($this->path.$this->getName()); + return $this->path.$this->getName(); }else{ - $image->save("assets/images/uploads/{$this->getName()}"); + $image->save($this->path.$this->getName()); + return $this->path.$this->getName(); } } @@ -96,6 +100,6 @@ public function delete($photo){ public function upload(){ $this->rename(); - $this->doUpload(); + return $this->doUpload(); } } \ No newline at end of file diff --git a/src/Validate.php b/src/Validate.php index 6f5fb22..96c7977 100755 --- a/src/Validate.php +++ b/src/Validate.php @@ -14,6 +14,16 @@ class Validate { use Validations,Sanitize; + protected $data; + + public function __construct($data) + { + foreach ($data as $field => $value){ + $this->data[$field] = trim(filter_var($value,FILTER_SANITIZE_STRING)); + + } + } + public function validate($rules){ @@ -33,7 +43,7 @@ public function validate($rules){ } } - return $this->sanitize($_POST); + return $this->data; } diff --git a/views/admin/categories/create.html b/views/admin/categories/create.html index 6f9885d..c9723e6 100644 --- a/views/admin/categories/create.html +++ b/views/admin/categories/create.html @@ -6,7 +6,7 @@

Categorias

- + {{ erros("name") }} {{ message("name") }}
diff --git a/views/admin/categories/edit.html b/views/admin/categories/edit.html index 7cb7bca..624f00b 100644 --- a/views/admin/categories/edit.html +++ b/views/admin/categories/edit.html @@ -3,10 +3,10 @@

Categorias

- +
- + {{ erros("name") }} {{ message("name") }}
@@ -15,4 +15,10 @@

Categorias

+{% endblock %} +{% block script %} + {% endblock %} \ No newline at end of file diff --git a/views/admin/master.html b/views/admin/master.html index 1cb5b9c..71a7b43 100755 --- a/views/admin/master.html +++ b/views/admin/master.html @@ -68,10 +68,10 @@
- User Image + User Image
@@ -142,5 +142,31 @@
Title
+ + + + + + +{% block script %} +{% endblock %} \ No newline at end of file diff --git a/views/admin/painel.html b/views/admin/painel.html index 0e6b713..c87c175 100644 --- a/views/admin/painel.html +++ b/views/admin/painel.html @@ -11,7 +11,7 @@