* [Friends] Contacts can now be edited.
[pivip.git] / project / modules / friends / controllers / FriendController.php
bloba70ab8456b23472ce0eb98af17b83632e8552339
1 <?php
3 /**
4 * Pivip
5 * Copyright (C) 2008 Vincent Tunru
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20 * @license http://www.fsf.org/licensing/licenses/info/GPLv2.html GPL v.2
21 * @category PivipModulesDefault
22 * @package Module_Friends
23 * @copyright (C) 2008 Vincent Tunru
24 * @author Vincent Tunru <email@vincentt.org>
27 /**
28 * Manage friends
30 class Friends_FriendController extends Page_Abstract
32 /**
33 * Add a contact
35 * If the user is not allowed to approve contacts, just apply and redirect
36 * to the homepage.
38 public function addAction()
40 $translate = Zend_Registry::get('Zend_Translate');
41 if(!$this->_isAllowed('apply'))
43 $this->_flashMessenger->setNamespace('error')
44 ->addMessage($translate->_(
45 'You are not allowed to apply for the contact list.'));
46 $this->_redirect('/');
48 $auth = Pivip_Auth::getInstance();
49 if(!$this->_isAllowed('approve'))
51 try
53 $friend = new Friend();
54 $identity = $auth->getIdentityProperties();
55 $friend->website = $identity->url;
56 $friend->name = $identity->fn;
57 $friend->save();
58 } catch(Exception $e) {
59 $this->_flashMessenger->setNamespace('error')
60 ->addMessage($translate->_(
61 'There was an error applying for the contact list.'));
62 $this->_redirect('/');
64 $this->_flashMessenger->addMessage($translate->_(
65 'Your request to be added to the contact list is pending approval.'));
66 $this->_redirect('/');
68 $form = $this->_getForm();
69 $website = urldecode($this->_request->getParam('friend'));
70 $friend = null;
71 if(!empty($website))
73 $friendsTable = Doctrine::getTable('Friend');
74 $friend = $friendsTable->find($website);
75 if(null !== $friend)
77 $data = array('name' => $friend->name, 'website' => $friend->website);
78 $form->populate($data);
81 $this->view->form = $form->render();
82 $defaultRequest = Zend_Registry::get('defaultRequest');
83 $this->_helper->actionStack($defaultRequest);
84 if(!$this->_request->isPost() ||
85 !$form->isValid($this->_request->getPost()))
87 return;
89 try
91 $this->_processForm($form, $friend);
92 } catch(Exception $e) {
93 $logger = Zend_Registry::get('logger');
94 $logger->err($e->getMessage());
95 $this->_refresh('The contact could not be added, please try again.');
97 $this->_flashMessenger->addMessage($translate->_(
98 'The contact has been added to the contact list.'));
99 if($this->_sendFriendRequest($friend->website))
101 $this->_flashMessenger->addMessage($translate->_(
102 'A friend request has been sent.'));
104 $this->_redirect('/');
108 * Edit a contact
110 public function editAction()
112 $translate = Zend_Registry::get('Zend_Translate');
113 if(!$this->_isAllowed('approve'))
115 $this->_flashMessenger->setNamespace('error')
116 ->addMessage($translate->_(
117 'You are not allowed to edit contacts.'));
118 $this->_redirect('/');
120 $website = urldecode($this->_request->getParam('friend'));
121 if(null === $website)
123 $this->_flashMessenger->setNamespace('error')
124 ->addMessage($translate->_(
125 'Please specify a contact to edit.'));
126 $this->_redirect('/');
128 $form = $this->_getForm();
129 $friendsTable = Doctrine::getTable('Friend');
130 $friend = $friendsTable->find($website);
131 if(null === $friend)
133 $this->_flashMessenger->setNamespace('error')
134 ->addMessage($translate->_(
135 'This contact could not be found'));
136 $this->_redirect('/');
138 $data = array();
139 foreach($friend as $k => $v)
141 $data[$k] = $v;
143 $form->populate($data);
144 $form->submit->setLabel('Edit contact');
145 $this->view->form = $form;
146 $defaultRequest = Zend_Registry::get('defaultRequest');
147 $this->_helper->actionStack($defaultRequest);
148 if(!$this->_request->isPost() ||
149 !$form->isValid($this->_request->getPost()))
151 return;
155 $this->_processForm($form, $friend);
156 } catch(Exception $e) {
157 $logger = Zend_Registry::get('logger');
158 $logger->err($e->getMessage());
159 $this->_refresh('The contact could not be edited, please try again.');
161 $this->_flashMessenger->addMessage($translate->_(
162 'The contact has been added to the contact list.'));
163 $this->_redirect('/');
167 * Delete a contact
169 * @todo Allow the user to undo this
171 public function deleteAction()
173 $friend = $this->_request->getParam('friend');
174 $translate = Zend_Registry::get('Zend_Translate');
176 if(null === $friend)
178 $this->_flashMessenger->addMessage($translate->_(
179 'No contact specified.'));
180 $this->_redirect('/');
183 if(!$this->_isAllowed('delete'))
185 $this->_flashMessenger->addMessage($translate->_(
186 'You are not allowed to delete contacts.'));
187 $this->_redirect('/');
190 $friendsTable = Doctrine::getTable('Friend');
191 $friend = $friendsTable->find(urldecode($friend));
195 $friend->delete();
196 $this->_flashMessenger->addMessage($translate->_(
197 'Contact removed.'));
198 $cache = Friends_Module::loadCache();
199 $cache->remove('friendlist');
200 } catch(Exception $e) {
201 $this->_flashMessenger->setNamespace('error')
202 ->addMessage($translate->_(
203 'An error occurred while removing the contact, ' .
204 'please try again.'));
206 $this->_redirect('/');
210 * Manage REST requests
212 public function restAction()
214 if(!$this->_isAllowed('apply'))
216 $this->_helper->viewRenderer->setNoRender();
217 return;
219 require_once 'modules/friends/models/ContactList.php';
220 $server = new Zend_Rest_Server();
221 $server->setClass('ContactList');
222 $server->handle();
226 * @return Zend_Form The form to add a friend
228 protected function _getForm()
230 $translate = Zend_Registry::get('Zend_Translate');
231 $form = new Zend_Form;
232 $form->setMethod('post')
233 ->setAction($this->_request->getRequestUri())
234 ->addElement('text', 'name')
235 ->name->setRequired(true)
236 ->setLabel('Contact name')
237 ->addValidator('NotEmpty');
238 $form->addElement('text', 'website')
239 ->website->setRequired(true)
240 ->setLabel('Contact website')
241 ->addValidator('NotEmpty');
242 $form->addDisplayGroup(array('name', 'website'), 'contact')
243 ->contact->setLegend('Contact');
244 $form->addElement('checkbox', 'met')
245 ->met->setLabel('We have met');
246 $form->addElement('select', 'friendship')
247 ->friendship->setLabel('We are')
248 ->addMultiOption('none', $translate->_('Nothing'))
249 ->addMultiOption('contact', $translate->_('Contacts'))
250 ->addMultiOption('acquaintance',
251 $translate->_('Acquaintances'))
252 ->addMultiOption('friend', $translate->_('Friends'))
253 ->setValue('none');
254 $form->addElement('checkbox', 'coworker')
255 ->coworker->setLabel('I work with this person');
256 $form->addElement('checkbox', 'colleague')
257 ->colleague->setLabel('This is a colleague');
258 $form->addElement('select', 'geographical')
259 ->geographical->setLabel('This person lives')
260 ->addMultiOption('co-resident',
261 $translate->_('In the same street'))
262 ->addMultiOption('neighbor',
263 $translate->_('Nearby'))
264 ->addMultiOption('none',
265 $translate->_('Far away'))
266 ->setValue('none');
267 $form->addElement('select', 'family')
268 ->family->setLabel('This person is a')
269 ->addMultiOption('none', $translate->_('Nothing'))
270 ->addMultiOption('child', $translate->_('Child'))
271 ->addMultiOption('parent', $translate->_('Parent'))
272 ->addMultiOption('sibling',
273 $translate->_('Brother or sister'))
274 ->addMultiOption('spouse',
275 $translate->_('Husband or wife'))
276 ->addMultiOption('kin', $translate->_('Relative'))
277 ->setValue('none');
278 $form->addElement('checkbox', 'muse')
279 ->muse->setLabel('This person inspires me');
280 $form->addElement('checkbox', 'crush')
281 ->crush->setLabel('I have a crush on this person');
282 $form->addElement('checkbox', 'date')
283 ->date->setLabel('I date this person');
284 $form->addElement('checkbox', 'sweetheart')
285 ->sweetheart->setLabel('We are intimate');
286 $form->addElement('checkbox', 'me')
287 ->me->setLabel('This is me');
288 $form->addDisplayGroup(array('friendship', 'met', 'coworker', 'colleague',
289 'geographical', 'family', 'muse', 'crush',
290 'date', 'sweetheart', 'me'),
291 'relationship')
292 ->relationship->setLegend('Relationship');
293 $submit = new Zend_Form_Element_Submit('submit');
294 $submit->setLabel('Add contact')
295 ->addDecorator('HtmlTag', array('tag' => 'dd'))
296 ->removeDecorator('DtDdWrapper');
297 $form->addElement($submit);
298 return $form;
302 * Process a submitted form
304 * @param Zend_Form $form
306 protected function _processForm(Zend_Form $form, $friend = null)
308 if(empty($friend))
310 $friend = new Friend();
312 $friend->website = rtrim($form->getValue('website'), '/') . '/';
313 $friend->name = $form->getValue('name');
314 $friend->met = $form->getValue('met');
315 $friend->friendship = $form->getValue('friendship');
316 $friend->coworker = $form->getValue('coworker');
317 $friend->colleague = $form->getValue('colleague');
318 $friend->geographical = $form->getValue('geographical');
319 $friend->family = $form->getValue('family');
320 $friend->muse = $form->getValue('muse');
321 $friend->crush = $form->getValue('crush');
322 $friend->date = $form->getValue('date');
323 $friend->sweetheart = $form->getValue('sweetheart');
324 $friend->me = $form->getValue('me');
325 $friend->approved = true;
326 $friend->save();
327 $cache = Friends_Module::loadCache();
328 $cache->remove('friendlist');
332 * Send a friend request to a website
334 * @param string $website The website to send the request to
336 protected function _sendFriendRequest($website)
338 if(!Zend_Uri::check($friend->website))
340 return false;
342 $restAddress = rtrim($website, '/') . '/contacts/rest';
343 $client = new Zend_Rest_Client($restAddress);
344 $generalConfig = Zend_Registry::get('generalConfig');
345 $link = Vogel_Utility_Url::absoluteUrl($this->_request->getBaseUrl());
346 $result = $client->add($link, $generalConfig->website->name)->get();
347 return $result->isSuccess();
351 * @param $privileges What the user needs to be allowed to do to blocks
352 * @return bool Whether the user has sufficient rights
354 protected function _isAllowed($privileges)
356 $auth = Pivip_Auth::getInstance();
357 $acl = Zend_Registry::get('acl');
358 $identity = $auth->getIdentityProperties();
359 if(!$auth->hasIdentity())
361 return $acl->isAllowed('guest', 'friendlist', $privileges);
363 return $acl->isAllowed($identity->aclRole, 'friendlist', $privileges);