Last active
August 29, 2015 14:03
-
-
Save valdiney/3885a567d45c90645b24 to your computer and use it in GitHub Desktop.
Códigos da página function-artigos.class.php
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
<?php | |
require_once('conecxao.php'); | |
////////////////////////////////////////////// | |
// Class 'CadastrarArtigo'... | |
///////////////////////////////////////////// | |
class CadastrarArtigo { | |
protected $titulo; | |
protected $categoria; | |
protected $artigo; | |
protected $data; | |
public function setTitulo($valor) { | |
$this->titulo = $valor; | |
} | |
public function getTitulo() { | |
return $this->titulo; | |
} | |
public function setCategoria($valor) { | |
$this->categoria = $valor; | |
} | |
public function getCategoria() { | |
return $this->categoria; | |
} | |
public function setArtigo($valor) { | |
$this->artigo = $valor; | |
} | |
public function getArtigo() { | |
return $this->artigo; | |
} | |
public function setData($valor) { | |
$this->data = $valor; | |
} | |
public function getData() { | |
return $this->data; | |
} | |
} | |
///////////////////////////////////////////// | |
////////////////////////////////////////////// | |
// Class 'Categoria'... | |
///////////////////////////////////////////// | |
class Categoria { | |
protected $nomeCategoria; | |
protected $data; | |
public function setCategoria($valor) { | |
$this->nomeCategoria = $valor; | |
} | |
public function getCategoria() { | |
return $this->nomeCategoria; | |
} | |
public function setData($valor) { | |
$this->data = $valor; | |
} | |
public function getData() { | |
return $this->data; | |
} | |
} | |
/////////////////////////////////////////// | |
$pdo = conecxao(); | |
////////////////////////////////////////////// | |
// Cadastra um Artigo... | |
///////////////////////////////////////////// | |
if ($_GET['funcao'] === 'cadastrar') { | |
$artigo = new CadastrarArtigo(); | |
$titulo = strtolower($_POST['titulo']); | |
$titulo = ucwords($titulo); | |
$artigo->setTitulo($titulo); | |
$artigo->setCategoria($_POST['categoria']); | |
$artigo->setArtigo($_POST['artigo']); | |
$artigo->setData(Date("d/m/Y")); | |
if ($artigo->getCategoria() == '') { | |
$backFaltaCategoria = "Não é possível cadastrar um artigo se não existir uma Categoria."; | |
header("Location:admin_professor.php?backFaltaCategoria=$backFaltaCategoria"); | |
} | |
else { | |
$insert = $pdo->prepare("insert into artigo(titulo,categoria,artigo,data) values (?,?,?,?)"); | |
$insert->execute(array($artigo->getTitulo(),$artigo->getCategoria(),$artigo->getArtigo(),$artigo->getData())); | |
header("Location:admin_professor.php"); | |
} | |
} | |
///////////////////////////////////////////// | |
////////////////////////////////////////////// | |
// Editando um Artigo... | |
///////////////////////////////////////////// | |
if ($_GET['funcao'] === 'editandoArtigo') { | |
$idArtigo = $_GET['id']; | |
$titulo = strtolower($_POST['titulo']); | |
$titulo = ucwords($titulo); | |
$editar = $pdo->prepare("update artigo set titulo=?,categoria=?,artigo=? where id=?"); | |
$editar->execute(array($titulo,$_POST['categoria'],$_POST['artigo'],$idArtigo)); | |
header("Location:function-mostrar-artigos.php?funcao=chamaArtigo&idDoArtigo=$idArtigo"); | |
} | |
////////////////////////////////////////////// | |
// Deletando um artigo... | |
///////////////////////////////////////////// | |
if ($_GET['funcao'] === 'deletarArtigo') { | |
$idArtigo = $_GET['id']; | |
$deletar = $pdo->prepare("delete from artigo where id=:id"); | |
$deletar->bindValue(":id",$idArtigo); | |
$deletar->execute(); | |
header("Location:function-mostrar-artigos.php"); | |
} | |
//////////////////////////////////////////// | |
////////////////////////////////////////////// | |
// Cadastra uma Categoria... | |
///////////////////////////////////////////// | |
if ($_GET['funcao'] === 'cadastrarCategoria') { | |
$categoria = new Categoria(); | |
$novaCategoria = strtolower($_POST['novaCategoria']); | |
$novaCategoria = ucwords($novaCategoria); | |
////////////////////////////////////////////// | |
// Verifica se a categoria já existe, | |
// se já existe, manda uma mensagem para a página | |
// e não executa o cadatro... | |
///////////////////////////////////////////// | |
$verifica = $pdo->prepare("select * from categoria where nomeCategoria=:nome"); | |
$verifica->bindValue(':nome',$novaCategoria); | |
$verifica->execute(); | |
$linha = $verifica->fetchAll(PDO::FETCH_OBJ); | |
if ($verifica->rowCount() != 0) { | |
foreach($linha as $listar) { | |
$data = $listar->data; | |
} | |
$back = "Essa categoria já existe e foi criada em: ".$data; | |
header("Location:admin_professor.php?back=$back"); | |
} | |
else { | |
$categoria->setCategoria($novaCategoria); | |
$categoria->setData(Date("d/m/Y")); | |
$insert = $pdo->prepare("insert into categoria(nomeCategoria,data) values(?,?)"); | |
$insert->execute(array($categoria->getCategoria(),$categoria->getData())); | |
header("Location:admin_professor.php"); | |
} | |
} | |
//////////////////////////////////////////// | |
/////////////////////////////////////////// | |
// Editando Categoria... | |
////////////////////////////////////////// | |
if ($_GET['funcao'] === 'editarCategoria') { | |
$idCategoria = $_GET['id']; | |
$nome = strtolower($_POST['novaCategoria']); | |
$nome = ucwords($nome); | |
////////////////////////////////////////////// | |
// Verifica se a categoria já existe | |
// se já existe, manda uma mensagem para a página | |
// e não executa o cadastro... | |
///////////////////////////////////////////// | |
$verifica = $pdo->prepare("select * from categoria where nomeCategoria=:nome"); | |
$verifica->bindValue(':nome',$nome); | |
$verifica->execute(); | |
$linha = $verifica->fetchAll(PDO::FETCH_OBJ); | |
if ($verifica->rowCount() != 0) { | |
foreach($linha as $listar) { | |
$data = $listar->data; | |
} | |
$back = "Essa categoria já existe e foi criada em: ".$data; | |
header("Location:function-artigo-mostrar-categorias.php?funcao=editarCategoria&id=$idCategoria&back=$back"); | |
} | |
else { | |
$editar = $pdo->prepare('update categoria set nomeCategoria=? where id=?'); | |
$editar->execute(array($nome,$idCategoria)); | |
header("Location:function-artigo-mostrar-categorias.php"); | |
} | |
} | |
//////////////////////////////////////////// | |
/////////////////////////////////////////// | |
// Deletando Categoria... | |
////////////////////////////////////////// | |
if ($_GET['funcao'] === 'deletarCategoria') { | |
$idCategoria = $_GET['id']; | |
$deletar = $pdo->prepare('delete from categoria where id=:id'); | |
$deletar->bindValue(':id',$idCategoria); | |
$deletar->execute(); | |
header("Location:function-artigo-mostrar-categorias.php"); | |
} | |
////////////////////////////////////////// | |
/* create table artigo( | |
id int(30) AUTO_INCREMENT, | |
titulo varchar(50) not null, | |
categoria int(30) not null, | |
artigo text not null, | |
PRIMARY KEY (id) | |
) | |
create table categoria( | |
id int(30) AUTO_INCREMENT, | |
nomeCategoria varchar(50) not null, | |
PRIMARY KEY (id) | |
) | |
Alter table artigo Add constraint chave_estrangeira_categoria | |
foreign key (categoria) references categoria (id); | |
Alter table artigo Add constraint chave_estrangeira_nomeCategoria | |
foreign key (nomeCategoria) references categoria (nomeCategoria); | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment