From 3c7cd1767fb6c6399aff8a0172174b35cdf86ac5 Mon Sep 17 00:00:00 2001 From: vinnl Date: Tue, 6 May 2008 16:35:41 +0200 Subject: [PATCH] * [Friends] Contacts can now be edited. * [Friends] A little bit of refactoring. --- .../friends/controllers/FriendController.php | 143 ++++++++++++++++----- project/modules/friends/models/ContactList.php | 4 +- .../modules/friends/views/scripts/friend/add.phtml | 5 +- .../friends/views/scripts/friend/edit.phtml | 4 + 4 files changed, 124 insertions(+), 32 deletions(-) create mode 100644 project/modules/friends/views/scripts/friend/edit.phtml diff --git a/project/modules/friends/controllers/FriendController.php b/project/modules/friends/controllers/FriendController.php index 6db39ee..a70ab84 100644 --- a/project/modules/friends/controllers/FriendController.php +++ b/project/modules/friends/controllers/FriendController.php @@ -67,6 +67,7 @@ class Friends_FriendController extends Page_Abstract } $form = $this->_getForm(); $website = urldecode($this->_request->getParam('friend')); + $friend = null; if(!empty($website)) { $friendsTable = Doctrine::getTable('Friend'); @@ -87,27 +88,7 @@ class Friends_FriendController extends Page_Abstract } try { - if(empty($friend)) - { - $friend = new Friend(); - } - $friend->website = rtrim($form->getValue('website'), '/') . '/'; - $friend->name = $form->getValue('name'); - $friend->met = $form->getValue('met'); - $friend->friendship = $form->getValue('friendship'); - $friend->coworker = $form->getValue('coworker'); - $friend->colleague = $form->getValue('colleague'); - $friend->geographical = $form->getValue('geographical'); - $friend->family = $form->getValue('family'); - $friend->muse = $form->getValue('muse'); - $friend->crush = $form->getValue('crush'); - $friend->date = $form->getValue('date'); - $friend->sweetheart = $form->getValue('sweetheart'); - $friend->me = $form->getValue('me'); - $friend->approved = true; - $friend->save(); - $cache = Friends_Module::loadCache(); - $cache->remove('friendlist'); + $this->_processForm($form, $friend); } catch(Exception $e) { $logger = Zend_Registry::get('logger'); $logger->err($e->getMessage()); @@ -115,15 +96,70 @@ class Friends_FriendController extends Page_Abstract } $this->_flashMessenger->addMessage($translate->_( 'The contact has been added to the contact list.')); - if(Zend_Uri::check($friend->website)) + if($this->_sendFriendRequest($friend->website)) { - $restAddress = rtrim($friend->website, '/') . '/contacts/rest'; - $client = new Zend_Rest_Client($restAddress); - $generalConfig = Zend_Registry::get('generalConfig'); - $link = Vogel_Utility_Url::absoluteUrl($this->_request->getBaseUrl()); - $result = $client->add($link, - $generalConfig->website->name)->get(); - } + $this->_flashMessenger->addMessage($translate->_( + 'A friend request has been sent.')); + } + $this->_redirect('/'); + } + + /** + * Edit a contact + */ + public function editAction() + { + $translate = Zend_Registry::get('Zend_Translate'); + if(!$this->_isAllowed('approve')) + { + $this->_flashMessenger->setNamespace('error') + ->addMessage($translate->_( + 'You are not allowed to edit contacts.')); + $this->_redirect('/'); + } + $website = urldecode($this->_request->getParam('friend')); + if(null === $website) + { + $this->_flashMessenger->setNamespace('error') + ->addMessage($translate->_( + 'Please specify a contact to edit.')); + $this->_redirect('/'); + } + $form = $this->_getForm(); + $friendsTable = Doctrine::getTable('Friend'); + $friend = $friendsTable->find($website); + if(null === $friend) + { + $this->_flashMessenger->setNamespace('error') + ->addMessage($translate->_( + 'This contact could not be found')); + $this->_redirect('/'); + } + $data = array(); + foreach($friend as $k => $v) + { + $data[$k] = $v; + } + $form->populate($data); + $form->submit->setLabel('Edit contact'); + $this->view->form = $form; + $defaultRequest = Zend_Registry::get('defaultRequest'); + $this->_helper->actionStack($defaultRequest); + if(!$this->_request->isPost() || + !$form->isValid($this->_request->getPost())) + { + return; + } + try + { + $this->_processForm($form, $friend); + } catch(Exception $e) { + $logger = Zend_Registry::get('logger'); + $logger->err($e->getMessage()); + $this->_refresh('The contact could not be edited, please try again.'); + } + $this->_flashMessenger->addMessage($translate->_( +'The contact has been added to the contact list.')); $this->_redirect('/'); } @@ -263,6 +299,55 @@ class Friends_FriendController extends Page_Abstract } /** + * Process a submitted form + * + * @param Zend_Form $form + */ + protected function _processForm(Zend_Form $form, $friend = null) + { + if(empty($friend)) + { + $friend = new Friend(); + } + $friend->website = rtrim($form->getValue('website'), '/') . '/'; + $friend->name = $form->getValue('name'); + $friend->met = $form->getValue('met'); + $friend->friendship = $form->getValue('friendship'); + $friend->coworker = $form->getValue('coworker'); + $friend->colleague = $form->getValue('colleague'); + $friend->geographical = $form->getValue('geographical'); + $friend->family = $form->getValue('family'); + $friend->muse = $form->getValue('muse'); + $friend->crush = $form->getValue('crush'); + $friend->date = $form->getValue('date'); + $friend->sweetheart = $form->getValue('sweetheart'); + $friend->me = $form->getValue('me'); + $friend->approved = true; + $friend->save(); + $cache = Friends_Module::loadCache(); + $cache->remove('friendlist'); + } + + /** + * Send a friend request to a website + * + * @param string $website The website to send the request to + */ + protected function _sendFriendRequest($website) + { + if(!Zend_Uri::check($friend->website)) + { + return false; + } + $restAddress = rtrim($website, '/') . '/contacts/rest'; + $client = new Zend_Rest_Client($restAddress); + $generalConfig = Zend_Registry::get('generalConfig'); + $link = Vogel_Utility_Url::absoluteUrl($this->_request->getBaseUrl()); + $result = $client->add($link, $generalConfig->website->name)->get(); + return $result->isSuccess(); + } + + /** * @param $privileges What the user needs to be allowed to do to blocks * @return bool Whether the user has sufficient rights */ diff --git a/project/modules/friends/models/ContactList.php b/project/modules/friends/models/ContactList.php index fd417e4..016c9b7 100644 --- a/project/modules/friends/models/ContactList.php +++ b/project/modules/friends/models/ContactList.php @@ -41,8 +41,8 @@ class ContactList $friendsTable = Doctrine::getTable('Friend'); if(null !== $friendsTable->find($website)) { - return array('status' => true, - 'msg' => $translate->_('Request saved.')); + return array('status' => false, + 'msg' => $translate->_('You have already been added.')); } $friend = new Friend(); $friend->website = $website; diff --git a/project/modules/friends/views/scripts/friend/add.phtml b/project/modules/friends/views/scripts/friend/add.phtml index d818b35..626cc01 100644 --- a/project/modules/friends/views/scripts/friend/add.phtml +++ b/project/modules/friends/views/scripts/friend/add.phtml @@ -1 +1,4 @@ -form; \ No newline at end of file +
+

translate('Add a contact'); ?>

+form; ?> +
\ No newline at end of file diff --git a/project/modules/friends/views/scripts/friend/edit.phtml b/project/modules/friends/views/scripts/friend/edit.phtml new file mode 100644 index 0000000..82b9678 --- /dev/null +++ b/project/modules/friends/views/scripts/friend/edit.phtml @@ -0,0 +1,4 @@ +
+

translate('Edit a contact'); ?>

+form; ?> +
\ No newline at end of file -- 2.11.4.GIT