Rapid Application Development with Open Source Software

Next: Creating Reusable View Components in CakePHP UP: TOC

The following requirement must be implemented: Anybody can open a new book. He or she must provide an email address where the password is sent to.

In this chapter we will show the implementation of
a) a model
b) a controller action
c) a view

The model User.php has this logic:

 array(
            'className' => 'Book',
            'foreignKey' => 'user_id'
         )
   );

function set_pw( $pemail, $pw = '') {

   // generate a new password if not provided to this function:
   if (empty($pw)) {
      $tw = md5 (uniqid (rand()));
      $pw = '';
      $noval = array("0","o","i","l","1");
      for ($j = 0; (strlen($pw) < 6 and $j < 16); $j++) {
         $i = substr($tw,$j,1);
         if (!in_array($i, $noval)) $pw .= $i;
      }
   }

   $this->log('pw: ' . $pw);

   // save the encrypted password
   $q = "update bo_user set pw = '" . sha1($pw) . "'";
   $q .= ", modified = current_timestamp";
   $q .= " where lower(email) = '" . strtolower($pemail) . "'";
   $ret = $this->query($q);
   return $pw;
}


// generate a new password and send it to users email
function sendpw($email) {
   $urec = $this->findByEmail($email);  // check if email exists
   if (empty($urec)) {
      return __('Email not found');
   }

   // generate a new password for this email:
   $pw = $this->set_pw($email);
 
   App::uses('CakeEmail', 'Network/Email');
   $e = new CakeEmail('default');   // take default config in Config/email.php
   $e->emailFormat('text');
   $e->replyTo('admin@logikfabrik.com');
   $e->domain('logikfabrik.com');
   
   // take the users language
   Configure::write('Config.language',$urec['User']['lang']);  

   // prepare and send email
   $e->to($email);
   $e->subject(__('Your Password')); 

   $m = __('Please find below the password you have requested') . ":\n";
   $m .=  $pw . "\n\n\n";
   $m .= __('Use this link to login') . ":\n";
   $m .=  'http://www.logikfabrik.com/users/login';
   $e->send($m);
}

}
?>

The Controller Users.php has this action:

Session = $this->Components->load('Session');

   $this->set('title_for_layout',__('Registration for a new Book'));

   if ($this->request->is('post')) {

       if (!empty($this->request->data)) {

           // Check if User already exists:
           $user = $this->User->findByEmail($this->request->data['User']['email']);
           if (!empty($user['User']['id']))
              $this->request->data['User']['id'] = $user['User']['id'];

           $this->request->data['User']['nick'] = substr($this->request->data['User']['email'],0,4);  // generate nickname
           if ($this->User->saveAssociated($this->request->data)) {
               $this->Session->setFlash(__('Thank you for registering.'),'default');
               $this->redirect('/');
           }
       }
   }
}
}
?>

The View "View/User/openbook.ctp" has this content:

Form->create('Book', 
        array('inputDefaults' => array('div' => 'form-group','label' => false,
          'wrapInput' => false, 'class' => 'form-control'), 
          'class' => 'well','type'=>'file','url' => $this->request->here)); 

// a user can have many books, there the 0 in Book.0.name
echo $this->Form->input('Book.0.name',
   array('placeholder' => __('Title of the Book'),
   'required' => true,'error' => array('minlength' => __('Minimum 3 Zeichen'))
));


echo $this->Form->input('User.email',array(
   'placeholder' => __('Your Email'),'required' => true,
   'before' => '' . __('Your Email for the Login') . ''
));

echo $this->Form->end(array('label' => __('Open your book'), 'class'=> 'btn btn-primary'));

// best practice: position the cursor to the first input field:
echo '';
Next: Creating Reusable View Components in CakePHP UP: TOC