From d3966e2daaf52c9c453c6893536b1c417a76f48b Mon Sep 17 00:00:00 2001 From: Fredric N Date: Wed, 22 Sep 2010 22:22:50 +0200 Subject: [PATCH] Added user profile --- application/Bootstrap.php | 26 ++++- application/languages/se.ini | 7 ++ application/models/Mapper/User.php | 14 +++ application/models/User.php | 15 +++ .../default/controllers/ProfileController.php | 100 ++++++++++++++++++ .../default/views/scripts/profile/index.phtml | 12 +++ .../Fiktiv/Model/Mapper/DbTableAbstract.php | 6 ++ library/Fiktiv/Translate/Adapter.php | 16 ++- library/Fiktiv/View/Helper/AuthLink.php | 7 +- 9 files changed, 193 insertions(+), 10 deletions(-) create mode 100644 application/modules/default/controllers/ProfileController.php create mode 100644 application/modules/default/views/scripts/profile/index.phtml diff --git a/application/Bootstrap.php b/application/Bootstrap.php index 2785953..d69631f 100644 --- a/application/Bootstrap.php +++ b/application/Bootstrap.php @@ -66,7 +66,6 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap $view->headScript()->appendFile('http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'); Zend_Locale::setDefault('sv_SE'); - return $view; } @@ -106,7 +105,9 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap $router = $this->getResource('front')->getRouter(); - + /* + * Default routes to the default module + */ $router->addRoute('default', new Zend_Controller_Router_Route(':lang/:controller/:action', array( 'lang' => 'sv', @@ -116,8 +117,13 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap ) )); - + /* + * This route allows direct access to IndexController + * without specifying "index" as controller. + * + * Allowed actions need to be specified! + */ $router->addRoute('index', new Zend_Controller_Router_Route( ':lang/:action', array( @@ -125,9 +131,17 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 'module' => 'default', 'controller' => 'index', 'action' => 'index' + ), + array( + 'action' => '(about)' ) )); + + /* + * Route to the blog module + * TODO: This prob. needs changing. + */ $router->addRoute('blog', new Zend_Controller_Router_Route( ':lang/blog/:action/:id', array( @@ -136,9 +150,12 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 'controller' => 'index', 'action' => 'latest', 'id' => '' + ), + array( + 'lang' => '(en|sv)' ) )); - + $router->addRoute('portfolio', new Zend_Controller_Router_Route( ':lang/portfolio/:action', array( @@ -148,6 +165,7 @@ class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 'action' => 'index' ) )); + $router->addRoute('admin', new Zend_Controller_Router_Route( ':lang/admin/:action', diff --git a/application/languages/se.ini b/application/languages/se.ini index cc8710e..bf8a896 100644 --- a/application/languages/se.ini +++ b/application/languages/se.ini @@ -3,6 +3,13 @@ blog = blog about = om fiktiv example = "Det här är ett exempel på %s och %s" +profile information = "profil information" +user information = "användarinformation" +change password = "ändra lösenord" + +firstname = förnamn +lastname = efternamn + email = e-post password = lösenord login = logga in diff --git a/application/models/Mapper/User.php b/application/models/Mapper/User.php index 49ad3f6..35ea3ab 100644 --- a/application/models/Mapper/User.php +++ b/application/models/Mapper/User.php @@ -97,4 +97,18 @@ class Mapper_User extends Fiktiv_Model_Mapper_DbTableAbstract return false; } + + + /** + * Change password + */ + public function changePassword($userId, $password) + { + return $this->_dbTable->update( + array( + 'password' => hash('sha256',$password) + ), + $this->_dbTable->getAdapter()->quoteInto('id = ?',$userId) + ); + } } \ No newline at end of file diff --git a/application/models/User.php b/application/models/User.php index 6a6adf0..7ed6bf9 100644 --- a/application/models/User.php +++ b/application/models/User.php @@ -115,4 +115,19 @@ class User extends Fiktiv_Model_Abstract { return '(' . __CLASS__ . '){' . $this->_email . ' ' . $this->_firstName . ' ' . $this->_lastName . '}'; } + + /** + * Convert object to array + */ + public function toArray() + { + return array( + 'id' => $this->_id, + 'email' => $this->_email, + 'firstName' => $this->_firstName, + 'lastName' => $this->_lastName, + 'isDeleted' => $this->_isDeleted, + 'regDate' => $this->_regDate + ); + } } \ No newline at end of file diff --git a/application/modules/default/controllers/ProfileController.php b/application/modules/default/controllers/ProfileController.php new file mode 100644 index 0000000..476cad5 --- /dev/null +++ b/application/modules/default/controllers/ProfileController.php @@ -0,0 +1,100 @@ +view->form = new Zend_Form(); + + $form->addElement('text', 'email', array( + 'label' => 'u:email', + 'validators' => array(new Zend_Validate_EmailAddress()) + )); + + $form->addElement('text', 'firstName', array( + 'label' => 'u:firstname', + 'validators' => array(new Zend_Validate_Alpha()) + )); + + $form->addElement('text', 'lastName', array( + 'label' => 'u:lastname', + 'validators' => array(new Zend_Validate_Alpha()) + )); + + $form->addElement('password', 'password', array( + 'label' => 'u:password', + 'validators' => array(new Zend_Validate_Alnum()) + )); + + $form->addElement('password', 'passwordConfirm', array( + 'label' => 'u:confirm password', + 'validators' => array(new Zend_Validate_Alnum()) + )); + + $form->addElement('Submit', 'save', array( + 'label' => 'u:save' + )); + + $form->addDisplayGroup( + array( + 'email', + 'firstName', + 'lastName' + ), + 'partOne', + array( + 'legend' => 'u:user information' + ) + ); + + $form->addDisplayGroup( + array( + 'password', + 'passwordConfirm' + ), + 'partTwo', + array( + 'legend' => 'u:change password' + ) + ); + + $form->addDisplayGroup( + array( + 'save' + ), + 'partThree' + ); + + + if ($this->_request->isPost()) { + + $data = $this->_request->getPost(); + + if ($data['password'] !== $data['passwordConfirm']) { + + if (!empty($data['password'])) + $this->view->messages = 'passwords dont match'; + + + } else { + + if ($this->dataService->User->changePassword(Zend_Auth::getInstance()->getIdentity()->id, $data['password'])) + $this->view->messages = 'Lösenordet är ändrat!'; + + unset($data['password']); + } + unset($data['passwordConfirm']); + unset($data['save']); + + $this->dataService->User->update($data, 'id = '.Zend_Auth::getInstance()->getIdentity()->id); + + + } + + + $user = $this->dataService->User->findById(Zend_Auth::getInstance()->getIdentity()->id); + $form->populate($user->toArray()); + + } +} \ No newline at end of file diff --git a/application/modules/default/views/scripts/profile/index.phtml b/application/modules/default/views/scripts/profile/index.phtml new file mode 100644 index 0000000..4669b6d --- /dev/null +++ b/application/modules/default/views/scripts/profile/index.phtml @@ -0,0 +1,12 @@ +

translate('u:profile') ?>

+ +messages)) + echo '

' . $this->messages . '

'; +?> + +

Lite text

+ +

translate('u:profile information') ?>

+ +form ?> \ No newline at end of file diff --git a/library/Fiktiv/Model/Mapper/DbTableAbstract.php b/library/Fiktiv/Model/Mapper/DbTableAbstract.php index 649918a..f4abd00 100644 --- a/library/Fiktiv/Model/Mapper/DbTableAbstract.php +++ b/library/Fiktiv/Model/Mapper/DbTableAbstract.php @@ -21,4 +21,10 @@ abstract class Fiktiv_Model_Mapper_DbTableAbstract throw new Fiktiv_Exception('Invalid database table supplied to ' . __CLASS__); } } + + public function update($data, $where) + { + return $this->_dbTable->update($data, $where); + } + } \ No newline at end of file diff --git a/library/Fiktiv/Translate/Adapter.php b/library/Fiktiv/Translate/Adapter.php index 44e0769..6a684ab 100644 --- a/library/Fiktiv/Translate/Adapter.php +++ b/library/Fiktiv/Translate/Adapter.php @@ -37,21 +37,27 @@ abstract class Fiktiv_Translate_Adapter extends Zend_Translate_Adapter */ protected function transform($flag, $text) { + $encoding = mb_detect_encoding($text); + switch ($flag) { case 'u': - return ucfirst($text); + return mb_strtoupper(mb_substr($text, 0, 1, $encoding), $encoding) . mb_substr($text, 1, strlen($text), $encoding); break; case 'uw': - return ucwords($text); + $split = mb_split(' ', $text); + foreach ($split as &$word) { + $word = mb_strtoupper(mb_substr($word, 0, 1, $encoding), $encoding) . mb_substr($word, 1, strlen($word), $encoding); + } + return join(' ', $split); break; case 'ua': - return strtoupper($text); + return mb_strtoupper($text, $encoding); break; case 'l': - return strtolower($text[0]); + return mb_strtolower(mb_substr($text, 0, 1, $encoding), $encoding) . mb_substr($text, 1, strlen($text), $encoding); break; case 'la': - return strtolower($text); + return mb_strtolower($text, $encoding); break; } } diff --git a/library/Fiktiv/View/Helper/AuthLink.php b/library/Fiktiv/View/Helper/AuthLink.php index 70ce110..2bcd6ca 100644 --- a/library/Fiktiv/View/Helper/AuthLink.php +++ b/library/Fiktiv/View/Helper/AuthLink.php @@ -11,14 +11,19 @@ class Fiktiv_View_Helper_AuthLink extends Zend_View_Helper_Abstract 'controller' => 'auth' ); + $prefix = ''; + if ($auth->hasIdentity()) { $options['action'] = 'logout'; $display = 'u:logout'; + + + $prefix = $auth->getIdentity()->firstName.' (Profile) '; } else { $options['action'] = 'login'; $display = 'u:login'; } - return '' . $this->view->translate($display) . ''; + return $prefix.'' . $this->view->translate($display) . ''; } } \ No newline at end of file