Skip to content

Instantly share code, notes, and snippets.

@eamexicano
Created May 19, 2012 04:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eamexicano/2729027 to your computer and use it in GitHub Desktop.
Save eamexicano/2729027 to your computer and use it in GitHub Desktop.
Funciona para crear las vistas (y SQL) de un recurso en PHP. Esta relacionado con setupPHP. Después de crear el proyecto con setup se utiliza este archivo para crear recursos dentro del proyecto. Reescrito en PHP https://github.com/eamexicano/setup
#!bin/bash
echo "Nombre del script: " $0;
echo "Número de args: " $#;
TABLA="$1";
DB="${PWD##*/}";
echo $DB;
echo $TABLA;
shift;
# Atributos para HTML
SHOW="echo \$resultado['id'];\n";
NEW_INPUT="";
SENT_PARAMS="";
INSERT_ATTRS="";
INSERT_VALS="";
EDIT_INPUT="";
for x in $@
do
SHOW="$SHOW""echo \$resultado['${x%%:*}']; ";
NEW_INPUT="$NEW_INPUT""<input type='text' name='${x%%:*}' placeholder='${x%%:*}' /><br />\n";
SENT_PARAMS="$SENT_PARAMS""\$${x%%:*} = \$_POST['${x%%:*}'];\n";
INSERT_ATTRS="$INSERT_ATTRS""${x%%:*},";
INSERT_VALS="$INSERT_VALS""'\$${x%%:*}',";
EDIT_INPUT="$EDIT_INPUT""echo \"<input type='text' name='${x%%:*}' value='<?php echo \$resultado['${x%%:*}'] ?>'/><br />\"; ";
done
INSERT_ATTRS=${INSERT_ATTRS%","}
INSERT_VALS=${INSERT_VALS%","}
if [ -n "$TABLA" ]; then
echo "Creando directorio.";
mkdir "$TABLA";
cd "$TABLA"
tee index.php > /dev/null <<SOURCE
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<link rel="stylesheet" href="../assets/css/$TABLA.css" type="text/css" />
</head>
<body>
<?php require '../config/conexion.php'; ?>
<div class='container'>
<div class='header'>
<h1>$TABLA</h1>
</div>
<a href="new.php">Agregar $TABLA</a>
<div class='content'>
<?php
\$query = "SELECT * FROM $TABLA";
\$resultados = mysql_query(\$query) or die ("No se pudo realizar la consulta. " . mysql_error());
while (\$resultado = mysql_fetch_array(\$resultados)) {
$SHOW
echo "<a href='show.php?id=\$resultado[\'id\']'>Ver</a>";
echo "<a href='edit.php?id=\$resultado[\'id\']'>Editar</a>";
echo "<a href='destroy.php?id=\$resultado[\'id\']'>Eliminar</a>";
}
?>
</div>
<div class='footer'>
<p>
&copy; $TABLA
</p>
</div>
</div>
</body>
</html>
SOURCE
tee show.php > /dev/null <<SOURCE
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<link rel="stylesheet" href="../assets/css/$TABLA.css" type="text/css" />
</head>
<body>
<?php require '../config/conexion.php'; ?>
<div class='container'>
<div class='header'>
<h1>$TABLA</h1>
</div>
<a href="index.php">Ver todos</a>
<div class='content'>
<?php
\$id = \$_GET['id'];
\$query = "SELECT * FROM $TABLA WHERE id = '\$id'";
\$resultados = mysql_query(\$query) or die ("No se pudo realizar la consulta. " . mysql_error());
while (\$resultado = mysql_fetch_array(\$resultados)) {
$SHOW;
}
?>
</div>
<div class='footer'>
<p>
&copy; $TABLA
</p>
</div>
</div>
</body>
</html>
SOURCE
tee new.php > /dev/null <<SOURCE
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<link rel="stylesheet" href="../assets/css/$TABLA.css" type="text/css" />
</head>
<body>
<div class='container'>
<div class='header'>
<h1>$TABLA</h1>
</div>
<a href="index.php">Ver todos</a>
<div class='content'>
<form action='create.php' method='post'>
$NEW_INPUT;
<input type='submit' value='Crear' />
</form>
</div>
<div class='footer'>
<p>
&copy; $TABLA
</p>
</div>
</div>
</body>
</html>
SOURCE
tee create.php > /dev/null <<SOURCE
<?php
require '../config/conexion.php';
$SENT_PARAMS
\$query = "INSERT INTO $TABLA ($INSERT_ATTRS) VALUES ($INSERT_VALS)";
\$completado = mysql_query(\$query) or die ("No se pudo realizar la consulta. " . mysql_error());
if (\$completado) {
header("location: ./index.php");
} else {
echo "Problema con el query.";
}
?>
SOURCE
tee edit.php > /dev/null <<SOURCE
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<link rel="stylesheet" href="../assets/css/$TABLA.css" type="text/css" />
</head>
<body>
<div class='container'>
<div class='header'>
<h1>$TABLA</h1>
</div>
<a href="index.php">Ver todos</a>
<div class='content'>
<?php require '../config/conexion.php'; ?>
<form action='update.php' method='post'>
<?php
\$id = \$_GET['id'];
\$query = "SELECT * FROM $TABLA WHERE id = '\$id'";
\$resultados = mysql_query(\$query) or die ("No se pudo realizar la consulta. " . mysql_error());
while (\$resultado = mysql_fetch_array(\$resultados)) {
$EDIT_INPUT
echo "<input type='hidden' name='id' value='\$id'>";
}
?>
<input type='submit' value='Actualizar' />
</form>
</div>
<div class='footer'>
<p>
&copy; $TABLA
</p>
</div>
</div>
</body>
</html>
SOURCE
UPDATE_ATTRS="";
for x in $@
do
UPDATE_ATTRS="$UPDATE_ATTRS"" ${x%%:*} = '\$${x%%:*}',";
done
tee update.php > /dev/null <<SOURCE
<?php
require '../config/conexion.php';
$SENT_PARAMS
\$query = "UPDATE $TABLA SET $UPDATE_ATTRS WHERE id = '\$id'";
\$completado = mysql_query(\$query) or die ("No se pudo realizar la consulta. " . mysql_error());
if (\$completado) {
header("location: ./index.php");
} else {
echo "Problema con el query.";
}
?>
SOURCE
tee destroy.php > /dev/null <<SOURCE
<?php
require '../config/conexion.php';
\$id = \$_POST['id'];
\$query = "DELETE FROM $TABLA WHERE id = '\$id'";
\$completado = mysql_query(\$query) or die ("No se pudo realizar la consulta. " . mysql_error());
if (\$completado) {
header("location: ./index.php");
} else {
echo "Problema con el query.";
}
?>
SOURCE
# Crear tabla sql
STR="USE $DB;"
STR="$STR""CREATE TABLE IF NOT EXISTS $TABLA (";
STR="$STR""id int(11) NOT NULL AUTO_INCREMENT,";
for x in $@
do
case ${x#*:} in
'string') STR="$STR""${x%%:*}"" varchar(255)"" ,"
;;
'varchar') STR="$STR""${x%%:*}"" varchar(255)"" ,"
;;
'int') STR="$STR""${x%%:*}"" int(11)"" ,"
;;
'text' ) STR="$STR""${x%%:*}"" text"" ,"
;;
*) STR="$STR""${x%%:*}"" ${x#*:}"" ,"
;;
esac
done
STR="$STR""PRIMARY KEY (id)";
STR="$STR"") ENGINE=MyISAM DEFAULT CHARSET=UTF8;"
tee ../db/"$TABLA".sql > /dev/null <<SOURCE
$STR
SOURCE
mysql -u root < ../db/"$TABLA".sql
else
echo "Intenta ejecutar resource.sh con un nombre un atributo y al menos un tipo de dato para el atributo".
echo "sh resource.sh Usuario nombre:string";
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment