Skip to content

Instantly share code, notes, and snippets.

@samrae
Created June 28, 2012 14:37
Show Gist options
  • Save samrae/3011716 to your computer and use it in GitHub Desktop.
Save samrae/3011716 to your computer and use it in GitHub Desktop.
<?php
class PeopleController extends AppController {
public $name = 'People';
public $uses = array('Person', 'Option', 'Location', 'Message', 'Staff', 'Tag', 'Group', 'Congregation', 'Attendance','MailchimpSubscriber');
public $components = array('Email', 'RequestHandler');
public $paginate = array(
'limit' => '1000',
'maxLimit' => '100',
'order' => array('Person.P_LastName' => 'asc','Person.P_FirstName' => 'asc'),
'fields' => array('id','P_FirstName','P_LastName','P_Email','P_FacebookID','P_Mobile','P_Suburb'),
'contain' => array(),
'conditions' => array('Person.Deleted' => '0')
);
public $helpers = array('Vcard.vcf', 'Html', 'Form', 'Js');
public function beforeFilter(){
parent::beforeFilter();
//$this->Session->SetFlash('Database is in process of being synchronised with camp database, any changes made to people\'s details will be lost... JR');
$this->RequestHandler->setContent('vcf', 'text/x-vcard'); //'text/x-vcard'
}
/**
* Form to subscribe the user to our newsletter
*/
function subscribe() {
debug($this->data);
if( isset( $this->data['MailchimpSubscriber']['emailaddress'] ) && $this->data['MailchimpSubscriber']['emailaddress'] != null ) {
$email = $this->data['MailchimpSubscriber']['emailaddress'];
} else {
$this->set( 'email', '' );
}
//if the page is posted
if( !empty( $this->data ) ) {
//lazy load the model to not do any unused http calls
$this->loadModel( 'MailchimpSubscriber' );
//load the data to ...
$this->MailchimpSubscriber->set( $this->data );
//check to see if the data validates
if( $this->MailchimpSubscriber->validates() ) {
//save the data
if( $this->MailchimpSubscriber->save( $this->data) ) {
$this->set( 'success', true );
} else {
//some error occured in the saving of the data
//do a request to see if the email adres already exists
$check = $this->MailchimpSubscriber->find( 'all', array( 'conditions' => array( 'emailaddress' => $this->data['MailchimpSubscriber']['emailaddress'])) );
//if the email address exists in the mailchimp db
if ( isset($check['email']) ) {
//flash msg here
$this->set( 'already_subscribed', true );
} else {
//some other error occured at the mailchimp side
$this->set( 'unknown_error', true );
}
}
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment