Skip to content

Instantly share code, notes, and snippets.

@manviny
Created March 2, 2014 09:41
Show Gist options
  • Save manviny/9304158 to your computer and use it in GitHub Desktop.
Save manviny/9304158 to your computer and use it in GitHub Desktop.
Create Subscribers on PW
<?php
/**
*
* Añade el cliente para Newsletters de un determinado municipio
* Si el cliente existe lo añade a la suscripcion del municipio seleccionado
* Si el cliente NO existe crea el usuario y la suscripcion
*
* IN:
* email, municipio
*
*/
if($input->post->email && $input->post->municipio) {
// Datos enviados por POST
$email = $input->post->email;
$idMunicipio = $input->post->municipio;
// Datos de configuracion
$carpetaSuscriptores = '/contactos/newsletters-clients/';
$plantillaSuscriptores = 'newsletters-client';
/**
* 1.- Comprueba si el Suscriptor existe
* @var [type]
*/
$suscriptores = wire('pages')->get($carpetaSuscriptores)->children;
$suscriptor = $suscriptores->find('email='.$email); // ej:15324
/**
* 2.- Si el Suscriptor es Nuevo, crealo
* @var [type]
*/
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 = $sanitizer->text($email); //titulo
$suscriptor->name = $sanitizer->pageName($email,true); //url
$suscriptor->email= $sanitizer->email($email); //email
$suscriptor->save();
}
/**
* 3.- Añade el Suscriptor (existente o recien creado) al municipio
* @var [type]
*/
$municipio = wire('pages')->get($idMunicipio);
$municipio->setOutputFormatting(false);
$municipio->suscriptores->add( $pages->get('id='.$suscriptor));
$municipio->save();
/**
* 4.- Vuelve a la pagina que llama
* @var string
*/
$session->mensaje = " nos comprometemos a mantenerte informado."
$session->redirect($session->paginaActual);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment