From 5275a1a9d6b8c4a8796d6b5b50a44bcb7348aae1 Mon Sep 17 00:00:00 2001 From: Loic d'Anterroches Date: Tue, 12 Aug 2008 22:17:49 +0200 Subject: [PATCH] Fixed issue 5, add a way for user to manage their account. Also added for each user a small public profile. --- src/IDF/Form/UserAccount.php | 136 ++++++++++++++++++++++++++ src/IDF/Views/User.php | 78 +++++++++++++++ src/IDF/conf/views.php | 12 +++ src/IDF/templates/base-simple.html | 2 +- src/IDF/templates/base.html | 2 +- src/IDF/templates/issues/view.html | 20 ++-- src/IDF/templates/project-home.html | 8 +- src/IDF/templates/user/myaccount.html | 59 +++++++++++ src/IDF/templates/user/public.html | 28 ++++++ www/media/idf/css/style.css | 4 + 10 files changed, 333 insertions(+), 16 deletions(-) create mode 100644 src/IDF/Form/UserAccount.php create mode 100644 src/IDF/Views/User.php create mode 100644 src/IDF/templates/user/myaccount.html create mode 100644 src/IDF/templates/user/public.html diff --git a/src/IDF/Form/UserAccount.php b/src/IDF/Form/UserAccount.php new file mode 100644 index 0000000..dc2c49f --- /dev/null +++ b/src/IDF/Form/UserAccount.php @@ -0,0 +1,136 @@ +user = $extra['user']; + $this->fields['first_name'] = new Pluf_Form_Field_Varchar( + array('required' => false, + 'label' => __('First name'), + 'initial' => $this->user->first_name, + 'widget_attrs' => array( + 'maxlength' => 50, + 'size' => 15, + ), + )); + $this->fields['last_name'] = new Pluf_Form_Field_Varchar( + array('required' => true, + 'label' => __('Last name'), + 'initial' => $this->user->last_name, + 'widget_attrs' => array( + 'maxlength' => 50, + 'size' => 20, + ), + )); + $this->fields['password'] = new Pluf_Form_Field_Varchar( + array('required' => false, + 'label' => __('Your password'), + 'initial' => '', + 'widget' => 'Pluf_Form_Widget_PasswordInput', + 'help_text' => Pluf_Template::markSafe(__('Leave blank if you do not want to change your password.').'
'.__('Your password must be hard for other people to find it, but easy for you to remember.')), + 'widget_attrs' => array( + 'maxlength' => 50, + 'size' => 15, + ), + )); + $this->fields['password2'] = new Pluf_Form_Field_Varchar( + array('required' => false, + 'label' => __('Confirm your password'), + 'initial' => '', + 'widget' => 'Pluf_Form_Widget_PasswordInput', + 'widget_attrs' => array( + 'maxlength' => 50, + 'size' => 15, + ), + )); + + + } + + + /** + * Save the model in the database. + * + * @param bool Commit in the database or not. If not, the object + * is returned but not saved in the database. + * @return Object Model with data set from the form. + */ + function save($commit=true) + { + if (!$this->isValid()) { + throw new Exception(__('Cannot save the model from an invalid form.')); + } + unset($this->cleaned_data['password2']); + if (strlen($this->cleaned_data['password']) == 0) { + unset($this->cleaned_data['password']); + } + $this->user->setFromFormData($this->cleaned_data); + if ($commit) { + $this->user->update(); + } + return $this->user; + } + + function clean_last_name() + { + $last_name = trim($this->cleaned_data['last_name']); + if ($last_name == mb_strtoupper($last_name)) { + return mb_convert_case(mb_strtolower($last_name), + MB_CASE_TITLE, 'UTF-8'); + } + return $last_name; + } + + function clean_first_name() + { + $first_name = trim($this->cleaned_data['first_name']); + if ($first_name == mb_strtoupper($first_name)) { + return mb_convert_case(mb_strtolower($first_name), + MB_CASE_TITLE, 'UTF-8'); + } + return $first_name; + } + + /** + * Check to see if the 2 passwords are the same. + */ + public function clean() + { + if (!isset($this->errors['password']) + && !isset($this->errors['password2'])) { + $password1 = $this->cleaned_data['password']; + $password2 = $this->cleaned_data['password2']; + if ($password1 != $password2) { + throw new Pluf_Form_Invalid(__('The passwords do not match. Please give them again.')); + } + } + return $this->cleaned_data; + } +} diff --git a/src/IDF/Views/User.php b/src/IDF/Views/User.php new file mode 100644 index 0000000..c1e9e7b --- /dev/null +++ b/src/IDF/Views/User.php @@ -0,0 +1,78 @@ + $request->user); + if ($request->method == 'POST') { + $form = new IDF_Form_UserAccount($request->POST, $params); + if ($form->isValid()) { + $user = $form->save(); + $url = Pluf_HTTP_URL_urlForView('IDF_Views::index'); + $request->user->setMessage(__('Your personal information have been updated.')); + return new Pluf_HTTP_Response_Redirect($url); + } + } else { + $form = new IDF_Form_UserAccount($request->user->getData(), $params); + } + return Pluf_Shortcuts_RenderToResponse('user/myaccount.html', + array('page_title' => __('Your Account'), + 'form' => $form), + $request); + } + + /** + * Public profile of a user. + */ + public function view($request, $match) + { + $projects = Pluf::factory('IDF_Project')->getList(); + $sql = new Pluf_SQL('login=%s', array($match[1])); + $users = Pluf::factory('Pluf_User')->getList(array('filter'=>$sql->gen())); + if (count($users) != 1 or !$users[0]->active) { + throw new Pluf_HTTP_Error404(); + } + return Pluf_Shortcuts_RenderToResponse('user/public.html', + array('page_title' => (string) $users[0], + 'member' => $users[0], + 'projects' => $projects, + ), + $request); + } + +} \ No newline at end of file diff --git a/src/IDF/conf/views.php b/src/IDF/conf/views.php index 6fa1a85..4bb7e62 100644 --- a/src/IDF/conf/views.php +++ b/src/IDF/conf/views.php @@ -37,6 +37,18 @@ $ctl[] = array('regex' => '#^/login/$#', 'method' => 'login', 'name' => 'login_view'); +$ctl[] = array('regex' => '#^/preferences/$#', + 'base' => $base, + 'priority' => 4, + 'model' => 'IDF_Views_User', + 'method' => 'myAccount'); + +$ctl[] = array('regex' => '#^/u/(.*)/$#', + 'base' => $base, + 'priority' => 4, + 'model' => 'IDF_Views_User', + 'method' => 'view'); + $ctl[] = array('regex' => '#^/register/$#', 'base' => $base, 'priority' => 4, diff --git a/src/IDF/templates/base-simple.html b/src/IDF/templates/base-simple.html index 27d6962..729639a 100644 --- a/src/IDF/templates/base-simple.html +++ b/src/IDF/templates/base-simple.html @@ -34,7 +34,7 @@

-{if !$user.isAnonymous()}{blocktrans}Welcome, {$user.first_name} {$user.last_name}.{/blocktrans} {trans 'Sign Out'}{else}{trans 'Sign in or create your account'}{/if} +{if !$user.isAnonymous()}{aurl 'url', 'IDF_Views_User::myAccount'}{blocktrans}Welcome, {$user}.{/blocktrans} {trans 'Sign Out'}{else}{trans 'Sign in or create your account'}{/if} | {trans 'Help'}

{*