<?php
class AccountController extends CommonCustomControllerAction
{
public function init()
{
parent::init();
$this->logger= Zend_Registry::get('logger');
$this->view->extra_styles= array(
'/css/main.css',
'/css/formstyle.css',
'/css/windowstyle.css'
);
$this->view->extra_scripts= array(
'/js/main.js',
'/js/mylib.inlineframe.class.js'
);
}
public function indexAction()
{
// nothing to do here, index.tpl will be displayed
}
public function loginAction()
{
$logger= Zend_Registry::get('logger');
$auth = Zend_Auth::getInstance();
//user already logged in. We might want to redirect to home page
if ($auth->hasIdentity()){
$this->_redirect('/');
}
$request = $this->getRequest();
// determine the page the user was originally trying to request
$redirect = $request->getPost('redirect');
if (strlen($redirect) == 0) {
$redirect = $request->getServer('REQUEST_URI');
}
if (strlen($redirect) == 0)
$redirect = $this->getUrl();
// initialize errors
$errors = array();
// process login if request method is post
if ($request->isPost()) {
// fetch login details from form and validate the
$username = $request->getPost('username');
$password = $request->getPost('password');
if (strlen($username) == 0)
$errors['username'] = 'Required field must not be blank';
if (strlen($password) == 0)
$errors['password'] = 'Required field must not be blank';
if (count($errors) == 0) {
// setup the authentication adapter
$adapter = new Zend_Auth_Adapter_DbTable($this->db,
'users',
'username',
'password',
'md5(?)');
$adapter->setIdentity($username);
$adapter->setCredential($password);
// try and authenticate the user
$result = $auth->authenticate($adapter);
if ($result->isValid()) {
$user = new DatabaseObject_User($this->db);
$user->load($adapter->getResultRowObject()->user_id);
if ($user->account_status == 'A') {
// record login attempt
$user->loginSuccess();
// create identity data and write it to session
$identity = $user->createAuthIdentity();
$auth->getStorage()->write($identity);
// send user to page they originally request
$this->_redirect($redirect);
}
else if ($user->account_status == 'P') {
Zend_Auth::getInstance()->clearIdentity();
$errors['username'] = 'Your account is still not activate,please activate the account';
}
else if ($user->account_status == 'S') {
Zend_Auth::getInstance()->clearIdentity();
$errors['username'] = 'Your account has been susupended, please contact the website administrator';
}
}
}
// record failed login attempt
DatabaseObject_User::LoginFailure($username,$result->getCode());
if (!isset($errors['username'])){
$errors['username'] = 'Your login details were invalid';
}
}
$this->view->errors = $errors;
$this->view->redirect = $redirect;
}
public function logoutAction()
{
Zend_Auth::getInstance()->clearIdentity();
DatabaseObject_TrackingData::noteLogoutTs($this->db, $this->user_id, time());
$this->_redirect($this->getUrl('login'));
}
public function fetchpasswordAction()
{
// if a user's already logged in, send them to their account home page
if (Zend_Auth::getInstance()->hasIdentity())
$this->_redirect($this->getUrl());
$errors = array();
$action = $this->getRequest()->getQuery('action');
if ($this->getRequest()->isPost())
$action = 'submit';
switch ($action) {
case 'submit':
$username = trim($this->getRequest()->getPost('username'));
if (strlen($username) == 0) {
$errors['username'] = 'Required field must not be blank';
}
else {
$user = new DatabaseObject_User($this->db);
if ($user->load($username, 'username')) {
$user->fetchPassword();
$url = $this->getUrl('fetchpassword') . '?action=complete';
$this->_redirect($url);
}
else
$errors['username'] = 'Specified user "'. $username .'" not found';
}
break;
case 'complete':
// nothing to do
break;
case 'confirm':
$id = $this->getRequest()->getQuery('id');
$key = $this->getRequest()->getQuery('key');
$user = new DatabaseObject_User($this->db);
if (!$user->load($id))
$errors['confirm'] = 'User not found';
else if (!$user->confirmNewPassword($key))
$errors['confirm'] = 'Unknow error';
break;
}
$this->view->errors = $errors;
$this->view->action = $action;
}
public function registerAction()
{
$request = $this->getRequest();
$fp = new FormProcessor_UserRegistration($this->db);
$validate = $request->isXmlHttpRequest();
if ($request->isPost()) {
if ($validate) {
$fp->validateOnly(true);
$fp->process($request);
}
else if ($fp->process($request)) {
$session = new Zend_Session_Namespace('registration');
$session->user_id = $fp->user->getId();
$this->_redirect($this->getUrl('registercomplete'));
}
}
if ($validate) {
$json= array( 'errors' => $fp->getErrors());
$this->sendJson($json);
}
else {
$this->view->fp= $fp;
}
}
public function activateAction()
{
$logger= Zend_Registry::get('logger');
$request= $this->getRequest();
$user_id= $request->getParam('userid');
$user= new DatabaseObject_User($this->db);
if ($this->user_type == 'administrator') {
if ($user->load($user_id)){
if (!$user->activate()) {
$error= 'Unknown reason';
}
}
$options= array('status'=> 'P');
$users= DatabaseObject_User::GetUsers($this->db, $options);
$this->view->users= $users;
$this->view->pagetype="admin";
}
else {
//check for the activation key
$key= $request->getParam('key');
if ($user->load($user_id)){
if($user->user_settings->new_password_key == $key) {
if (!$user->activate()) {
$error= 'Unknown reason';
}
}
else {
$error= "Invalid key";
}
}
$this->view->pagetype="user";
}
if (isset($error)) {
$this->view->error= $error;
}
}
public function registercompleteAction()
{
// retrieve the same session namespace used in register
$session = new Zend_Session_Namespace('registration');
// load the user record based on the stored user ID
$user = new DatabaseObject_User($this->db);
if (!$user->load($session->user_id)) {
$this->_forward('register');
return;
}
$this->view->user = $user;
}
public function deleteAction() //mainly for testing purpose
{
//fixme:need to disable after testing
$request= $this->getRequest();
if ($request->isPost()) {
$user_id= $request->getPost('userid');
$user= new DatabaseObject_User($this->db);
if ($user->load($user_id) && ($user->user_type == 'member')) {
$user->delete();
}
}
$options= array('type'=> 'member');
$users= DatabaseObject_User::GetUsers($this->db, $options);
$this->view->users= $users;
}
public function suspendAction()
{
$request= $this->getRequest();
if ($request->isPost()) {
$user_id= $request->getPost('userid');
$user = new DatabaseObject_User($this->db);
if ($user->load($user_id)) {
$user->account_status= 'S';
if (!$user->save()) {
$this->view->error= "Unable to suspend the user account";
}
}
}
$options= array('type'=> 'member');
$users= DatabaseObject_User::GetUsers($this->db, $options);
$this->view->users= $users;
}
public function resumeAction()
{
$request= $this->getRequest();
if ($request->isPost()) {
$user_id= $request->getPost('userid');
$user= new DatabaseObject_User($this->db);
if ($user->load($user_id)){
$user->account_status= 'A';
if (!$user->save()) {
$this->view->error= "Unable to resume the user account";
}
}
}
$options= array('status'=> 'S');
$users= DatabaseObject_User::GetUsers($this->db, $options);
$this->view->users= $users;
}
public function detailsAction()
{
$auth = Zend_Auth::getInstance();
$fp = new FormProcessor_UserDetails($this->db, $auth->getIdentity()->user_id);
if ($this->getRequest()->isPost()){
if ($fp->process($this->getRequest())) {
//$auth->getStorage()->write($fp->user->createAuthIdentity());
$this->_redirect($this->getUrl('detailscomplete'));
}
}
$this->view->fp = $fp;
$this->dropdown['0']= 'default';
$this->dropdown['1']= '1';
$this->dropdown['2']= '2';
$this->dropdown['3']= '3';
$this->dropdown['4']= '4';
$this->dropdown['5']= '5';
$this->dropdown['10']= '10';
$this->view->dropdown= $this->dropdown;
$this->addUserMenu();
$this->selected_usermenu_item= "My Account";
$this->fillViewVariables();
}
public function detailscompleteAction()
{
$this->view->username= $this->username;
$this->addUserMenu();
$this->fillViewVariables();
}
}
?>