Rapid Application Development with Open Source Software

Next: Mobile websites with CakePHP and Bootstrap UP: TOC

We would like to get back to interested users by informing them about new created content on our site. Therefore we need a newsletter system.

These elements need to be implemented:
a) database table for PostgreSQL
b) For CakePHP 2.x
b1) Model
b2) Controller
b3) reuseable form
b4) a shell

The relational database table and the according sequence are defined as follows:

create sequence bo_newsletter_id_seq;

create table bo_newsletter (
   id integer primary key default nextval('bo_newsletter_id_seq'::regclass),    -- Id of email contact
   email character varying (255) not null,
   lang character(2) default 'en',         -- the language
   last_sent timestamp with time zone,     -- last time an email has been sent
   created  timestamp with time zone DEFAULT ('now'::text)::timestamp with time zone
) with (oids = false);

The CakePHP 2.x Model has this appearance

 array(
         'isUnique' => array('rule' => 'isUnique', 'message' => 'Email already exists'),
         'not a valid email' => array('rule' => 'email','message' => 'Enter a valid email','required' => true)
      ),
   );
 
}
?>

CakePHP 2.x Controller Action for Newsletter

log('CNewsletter'. print_r($this->params,true));

    if (!empty($this->params->query['email'])) {

        if ($this->Newsletter->save(array('email' =>  $this->params->query['email']))) {
            $this->Session->setFlash(__('Email for Newsletter saved').': '. $this->params->query['email'],
                'default', array('class' => 'alert alert-success'));
            $this->redirect('/');
        } else {
            $this->log('CNewsletter::add validation Errors ' . 
                print_r($this->Newsletter->validationErrors,true) . "\n". print_r($this->params->query,true));
            $this->Session->setFlash(__('Please enter a valid email to subscribe our newsletter').': '.
                $this->params->query['email'],'default', array('class' => 'alert alert-danger'));
        }
    }
}
}
Next: Mobile websites with CakePHP and Bootstrap UP: TOC