Last active
August 29, 2015 14:21
-
-
Save manviny/f21598ad1aa08276ea5c to your computer and use it in GitHub Desktop.
Busqueda en ProcessWire
This file contains hidden or 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
/** | |
* Funciones disponibles | |
* | |
* buscar($termino) | |
* login($userf, $passf) | |
* logout() | |
* registerUser($email2, $name, $email, $password, $password2 ) | |
* suscribir() | |
*/ | |
/** | |
* Buscar terminos separados por espacios | |
*/ | |
function buscar($termino){ | |
$q = wire(sanitizer)->text($termino); | |
if($q) { | |
wire(input)->whitelist('q', $q); | |
$q = wire(sanitizer)->selectorValue($q); | |
$selector = "body|title~=$q, limit=50"; // añadir los campos en que queremos buscar | |
if(wire(user)->isLoggedin()) $selector .= ", has_parent!=2"; | |
$matches = wire(pages)->find($selector); | |
if($matches->count) { | |
return $matches; | |
} else { | |
return "No hay resultados"; | |
} | |
} else { | |
return "Pon algún termino para buscar"; | |
} | |
} | |
/** | |
* LOGIN | |
*/ | |
function login($userf, $passf){ | |
// --IMPORTANTE!! PONER EN LA CABECERA ESTA LINEA (donde queramos que aparezca el nombre del usuario logeado) | |
//echo '',($user->isLoggedin() ? $user->name : 'entrar'); | |
// check for login before outputting markup | |
if(wire(user)->isLoggedin()) { | |
// user is already logged in, so they don't need to be here | |
wire(session)->redirect(wire(session)->paginaActual); | |
} | |
$user = wire(sanitizer)->username($userf); | |
$pass = $passf; | |
$u = wire(session)->login($user, $pass); | |
if($u) { | |
$user = $u; | |
if($user->hasRole('rolquequeremos')) { | |
wire(session)->redirect("/admin/page/"); | |
} | |
// login successful | |
// wire(session)->redirect($config->urls->root); | |
wire(session)->redirect(wire(session)->paginaActual); | |
} | |
} | |
/** | |
* Salir de la sesión y llevar a la raiz | |
*/ | |
function logout(){ | |
wire(session)->logout(); | |
wire(session)->redirect($config->urls->root); | |
} | |
/** | |
* Se registra con el role guest pero podemos crear otros | |
*/ | |
function registerUser($email2, $name, $email, $password, $password2 ){ | |
/** | |
* Check for spam and last 2 lines to the code | |
*/ | |
if (trim($email2) != '') return 'spam'; | |
// <input type="text" name="email2" id="email2"> | |
// <style type="text/css">#email2 { display: none; }</style> | |
// el usuario ya existe | |
if(wire('users')->get("email=$email")->id || wire('users')->get("name=$name")->id) { | |
return "El usuario ya existe"; | |
} | |
// las constraseña no coinciden | |
if($password!=$password2) | |
return '<div class="alert alert-danger"><i class="fa fa-frown-o"></i> <strong>Las contraseñas</strong> no coinciden</div>'; | |
// | |
$pass = $password; | |
$u = new User(); | |
// $u->$first_name = $sanitizer->text($input->post->first_name); | |
// $u->$last_name = $sanitizer->text($input->post->last_name); | |
$u->name= wire('sanitizer')->username($name); | |
$u->email = wire('sanitizer')->email($email); | |
$u->pass = $pass; | |
$u->addRole("guest"); | |
// $u->addRole("registrado"); | |
$u->language = wire('languages')->get("default"); | |
$u->save(); | |
return 'usuario '.$u->name.' creado'; | |
} | |
/** | |
* | |
* | |
*/ | |
function suscribir($solicitud, $email2) { | |
if (trim($email2) != '') return 'spam'; | |
/** Datos de configuracion | |
* 1.- debemos crear 2 plantillas: newsletters-clients.php y newsletters-client.php | |
* 2.- ahora creamos una página y le asignamos la plantilla newsletters-clients | |
* 3.- a la plantilla newsletters-client debemos añadirle el campo email ( ya definido en el sistema) | |
* 4.- ya estamos en disposición de crear páginas de tipo newsletters-client bajo newsletters-cliens | |
*/ | |
$carpetaSuscriptores = '/newsletters-clients/'; // padre donde se crea el nuevo suscriptor | |
$plantillaSuscriptores = 'newsletters-client'; // tipo de plantilla para el suscriptor | |
$email = wire(sanitizer)->email($solicitud); | |
$suscriptores = wire(pages)->find('template='.$plantillaSuscriptores); | |
$suscriptor = $suscriptores->find('email='.$email); // ej:15324 | |
wire(session)->mensaje = "Este email ya está suscrito"; | |
if($suscriptor=="") { | |
$suscriptor = new Page(); // create new suscriptor page | |
// $suscriptor->of(false); | |
// echo "else".$suscriptor."<br>"; | |
$suscriptor->template = $plantillaSuscriptores; | |
$suscriptor->parent = wire('pages')->get($carpetaSuscriptores); // set carpeta donde guardar al suscriptor | |
$suscriptor->title = date("Y.m.d-H:i:s"); //titulo | |
$suscriptor->name = wire(sanitizer)->pageName($email,true); //url | |
$suscriptor->email= wire(sanitizer)->email($email); //email | |
$suscriptor->save(); | |
wire(session)->mensaje = "Te has suscrito satisfactoriamente"; | |
} | |
} |
This file contains hidden or 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 | |
if($input->get->q) { | |
$encontrado = buscar($input->get->q); | |
foreach($encontrado as $match){ | |
echo $match->title. "<br>"; | |
} | |
} | |
?> | |
<form action='./' method='get'> | |
<input type='text' name='q' /> | |
<input type='submit' name='submit' value='Buscar' /> | |
</form> | |
<!-- | |
Si queremos llamar a una pagina diferente, por ej busqueda cambiaremos action por: | |
action="<?php echo $config->urls->root?>busqueda/<?php echo '?q=$input->get->q' ?>" | |
--> |
This file contains hidden or 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 | |
// llama a la funcion login | |
if($input->post->user && $input->post->pass) { | |
// guarda la pagina en la que nos encontramos para volver a ella tras el login | |
$session->paginaActual = $page->url; | |
login($input->post->user, $input->post->pass); | |
} | |
?> | |
<form action="./" method="post"> | |
<input required type="text" name="user" placeholder="usuario o email"> | |
<input required type="password" name="pass" placeholder="Contraseña"> | |
<input type="submit" value="submit" > | |
</form> |
This file contains hidden or 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
// debemos poner la siguiente linea en algún lugar para deslogearnos | |
// <a href="<?=$config->urls->root?>logout/"><i class="fa fa-sign-out"></i></a> | |
<?php | |
$session->logout(); | |
$session->redirect($config->urls->root); | |
?> |
This file contains hidden or 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 | |
/** | |
* Crea Usuario nuevo | |
*/ | |
if ($input->post->submit){ | |
$message = registerUser($input->post->email2, $input->post->user_name, $input->post->user_email, $input->post->user_password, $input->post->user_password2 ); | |
//sendEmail('[email protected]', [email protected]', 'usuario registrado en mi página', $message); | |
} | |
?> | |
<form class="white-row" method="post" action="./"> | |
<input type="text" value="" name="user_name" placeholder="Nombre"> | |
<input type="text" value="" name="user_email" placeholder="Email"> | |
<input type="password" name="user_password" value="" placeholder="contraseña"> | |
<input type="password" value="" name="user_password2" placeholder="repetir contraseña"> | |
<input type="submit" name="submit" value="Registrarse" > | |
<style type="text/css"> #email2 { display: none; } </style> | |
<input type="text" name="email2" id="email2"> | |
</form> |
This file contains hidden or 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 | |
if($input->post->suscriptor) { | |
suscribir($input->post->suscriptor, $input->post->email2); | |
} | |
?> | |
<!-- para un suscriptor solo necesitamos su email --> | |
<?php echo $session->mensaje; ?> | |
<form class="form-inline pull-right" action='./' method='post'> | |
<div class="input-append row-fluid"> | |
<input type="email" name="suscriptor" class="" placeholder="[email protected]"> | |
<button type="submit" class="btn btn-danger">suscribirme</button> | |
<p><?php echo $session->mensaje ?> </p> | |
<style type="text/css"> #email2 { display: none; } </style> | |
<input type="text" name="email2" id="email2"> | |
</div> | |
</form> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment