Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #2 from deivid-rodriguez/master
new messages threads, Borrado de mensajes
  • Loading branch information
jipipayo committed Mar 12, 2013
2 parents adac0c0 + 256b7e1 commit 151ec52
Show file tree
Hide file tree
Showing 16 changed files with 426 additions and 136 deletions.
5 changes: 5 additions & 0 deletions application/Bootstrap.php
Expand Up @@ -145,6 +145,10 @@ protected function _initFront()
':language/message/list/*', array ( 'language' => null,
'controller' => 'message',
'action' => 'list'));
$routeMessageDelete = new Zend_Controller_Router_Route (
':language/message/delete/:id/*', array ( 'language' => null,
'controller' => 'message',
'action' => 'delete'));

$router->addRoute ( 'default', $routeLang );//important, put the default route first!
$router->addRoute ( 'woeid/woeid/ad_type', $routeWoeid );
Expand All @@ -158,6 +162,7 @@ protected function _initFront()
$router->addRoute ( 'message/reply', $routeMessageReply);
$router->addRoute ( 'message/show', $routeMessageShow);
$router->addRoute ( 'message/list', $routeMessageList);
$router->addRoute ( 'message/delete', $routeMessageDelete);

$front->setRouter ( $router );
return $front;
Expand Down
226 changes: 151 additions & 75 deletions application/controllers/MessageController.php
Expand Up @@ -27,23 +27,32 @@ public function indexAction() {
public function createAction() {

$request = $this->getRequest();

$id_user_to = $request->getParam('id_user_to');
$m_user = new Model_User();
$object_user = $m_user->fetchUser($id_user_to);
$this->view->user_to = $object_user->username;

$f_message_create = new Form_MessageCreate();
$lang = $this->lang;

//first we check if user is logged, if not redir to login
// first we check if user is logged, if not redir to login
$auth = Zend_Auth::getInstance ();
if (!$auth->hasIdentity()) {
//keep this url in zend session to redir after login
$aNamespace = new Zend_Session_Namespace('Nolotiro');
$aNamespace->redir = $this->lang . '/message/create/id_user_to/' . $id_user_to . '/subject/' . $request->getParam('subject');
$this->_redirect($this->lang . '/auth/login');
$aNamespace->redir = $lang . '/message/create/id_user_to/' . $id_user_to . '/subject/' .
$request->getParam('subject');
$this->_redirect($lang . '/auth/login');
}

// check sender and recipient are not the same
if ($auth->getIdentity()->id == $id_user_to) {
$this->_helper->_flashMessenger->addMessage(
$this->view->translate('You are not allowed to do that'));
$this->_redirect('/' . $lang . '/woeid/' . $this->location . '/give');
}

$m_user = new Model_User();
$object_user = $m_user->fetchUser($id_user_to);
$this->view->user_to = $object_user->username;

$f_message_create = new Form_MessageCreate();

if ($this->getRequest()->isPost()) {

if ($f_message_create->isValid($request->getPost())) {
Expand Down Expand Up @@ -91,7 +100,6 @@ public function createAction() {
}
} else {
$data['subject'] = $this->_getParam('subject');

$f_message_create->populate($data);
}

Expand Down Expand Up @@ -124,45 +132,59 @@ public function replyAction() {

if ($request->isPost()) {

// collect data
$f = new Zend_Filter_StripTags ( );
$data['body'] = $f->filter($request->getPost('body'));
$data['user_from'] = $auth->getIdentity()->id;
$f_message_reply = new Form_MessageReply();
if ($f_message_reply->isValid($request->getPost())) {

if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$data['ip'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$data['ip'] = $_SERVER['REMOTE_ADDR'];
// collect data
$f = new Zend_Filter_StripTags ( );
$data['body'] = $f->filter($request->getPost('body'));
$data['user_from'] = $auth->getIdentity()->id;

if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$data['ip'] = $_SERVER['HTTP_X_FORWARDED_FOR'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$data['ip'] = $_SERVER['REMOTE_ADDR'];
}

// Insert new message in database
$m_message = new Model_Message();
$m_message->createMessage($data);

// Send notification e-mail
$mail = new Zend_Mail('utf-8');
$hostname = 'http://' . $this->getRequest()->getHttpHost();
$username_from = $auth->getIdentity()->username;
$data['body'] = $data['subject'] . '<br/>' . $data['body'] . '<br/>';
$data['body'] .= $this->view->translate(
'Go to this url to reply this message:') . '<br/>
<a href="' . $hostname . '/' . $this->lang . '/message/received">' .
$hostname . '/' . $this->lang . '/message/received</a>
<br>---------<br/>';
$data['body'] .= $this->view->translate(
'This is an automated notification. Please, don\'t reply at this email address.');
$mail->setBodyHtml($data['body']);
$mail->setFrom('noreply@nolotiro.org', 'nolotiro.org');
$m_user = new Model_User();
$object_user = $m_user->fetchUser($data['user_to']);
$mail->addTo($object_user->email);
$mail->setSubject('[nolotiro.org] - ' . $this->view->translate('You have a new message from user') . ' ' . $username_from);
$mail->send();

// Show flash success notification
$this->_helper->_flashMessenger->addMessage(
$this->view->translate('Message sent successfully!'));

} else {
// Show flash failure notification
$this->_helper->_flashMessenger->addMessage(
$this->view->translate('There was an error sending your message'));
}

// Insert new message in database
$m_message = new Model_Message();
$m_message->createMessage($data);

// Send notification e-mail
$mail = new Zend_Mail('utf-8');
$hostname = 'http://' . $this->getRequest()->getHttpHost();
$username_from = $auth->getIdentity()->username;
$data['body'] = $data['subject'] . '<br/>' . $data['body'] . '<br/>';
$data['body'] .= $this->view->translate(
'Go to this url to reply this message:') . '<br/>
<a href="' . $hostname . '/' . $this->lang . '/message/received">' .
$hostname . '/' . $this->lang . '/message/received</a>
<br>---------<br/>';
$data['body'] .= $this->view->translate(
'This is an automated notification. Please, don\'t reply at this email address.');
$mail->setBodyHtml($data['body']);
$mail->setFrom('noreply@nolotiro.org', 'nolotiro.org');
$m_user = new Model_User();
$object_user = $m_user->fetchUser($data['user_to']);
$mail->addTo($object_user->email);
$mail->setSubject('[nolotiro.org] - ' . $this->view->translate('You have a new message from user') . ' ' . $username_from);
$mail->send();

$this->_helper->_flashMessenger->addMessage($this->view->translate('Message sent successfully!'));
$this->_redirect('/' . $this->lang . '/message/list');
/* Redirect back to message list.
* XXX: Do this in a way validation errors are kept. Javascript I
* guess */
$this->_redirect('/' . $this->lang . '/message/show/' . $id);
}

}


Expand Down Expand Up @@ -207,50 +229,104 @@ public function listAction() {
*/
public function showAction() {

$id = $this->_request->getParam('id');
$request = $this->getRequest();
$id = $request->getParam('id');
$lang = $this->lang;
$subject = $this->view->subject = $this->_request->getParam('subject');

$m_message = new Model_Message();
$this->view->thread = $m_message->getMessagesFromThread($id);

if (!$this->view->thread) {
$this->_helper->_flashMessenger->addMessage($this->view->translate('This thread does not exist!'));
$this->_redirect('/' . $lang . '/woeid/' . $this->location . '/give');
return;
}

/* First check wheter the user should be looking here */
$auth = Zend_Auth::getInstance ();
if ($auth->hasIdentity()) {
// if not the sender or receiver of the message, go to hell

/* Information for the view */
$this->view->me = $me = $auth->getIdentity()->id;
// i need the username in the view
$this->view->my_name = $auth->getIdentity()->username;
$to = $this->view->thread[0]['user_to'];
$from = $this->view->thread[0]['user_from'];
if (($to != $me) && ($from != $me) ) {
$this->_helper->_flashMessenger->addMessage($this->view->translate('You are not allowed to view this page'));

/* Grab thread from database */
$m_message = new Model_Message();
$thread = $m_message->getThreadFromId($id);

/* Check if user is allowed to see the conversation */
if ( ($thread->user_to != $me && $thread->user_from != $me) ||
($thread->user_to == $me && $thread->deleted_to) ||
($thread->user_from == $me && $thread->deleted_from) ) {

$this->_helper->_flashMessenger->addMessage(
$this->view->translate('You are not allowed to view this page'));
$this->_redirect('/' . $lang . '/woeid/' . $this->location . '/give');
}
} else { //maybe the owner , but not logged, redir to login
//keep this url in zend session to redir after login
$aNamespace = new Zend_Session_Namespace('Nolotiro');
$aNamespace->redir = $lang . '/message/show/' . $id . '/subject/' . $subject;
$this->_redirect($lang . '/auth/login');

/* Grab the whole conversation from database */
$this->view->thread = $m_message->getMessagesFromThread($id);

if (!$this->view->thread) {
$this->_helper->_flashMessenger->addMessage(
$this->view->translate('This thread does not exist!'));
$this->_redirect('/' . $lang . '/woeid/' . $this->location . '/give');
}

/* Mark conversation as read */
$m_message = new Model_Message();
$m_message->markAsRead($id, $me);

$reply_to = ($me == $thread->user_to) ? $thread->user_from : $thread->user_to;

$this->view->subject = $thread->subject;
$this->view->page_title .= $thread->subject;

$f_message_reply = new Form_MessageReply();

$f_message_reply->setAction('/' . $lang . '/message/reply/' . $id .
'/to/' . $reply_to);

$this->view->createreply = $f_message_reply;
}

$m_message = new Model_Message();
$m_message->markAsRead($id, $me);
}

$reply_to = ($me == $to) ? $from : $to;
$this->view->page_title .= $subject;

$f_message_reply = new Form_MessageReply();
/**
* Delete a conversation
*/
public function deleteAction() {

$f_message_reply->setAction('/' . $lang . '/message/reply/' . $id .
'/to/' . $reply_to);
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout()->disableLayout();
$lang = $this->lang;
$location = $this->location;
$request = $this->getRequest();

$this->view->createreply = $f_message_reply;
/* User not logging, not allowed */
$auth = Zend_Auth::getInstance ();
if (!$auth->hasIdentity()) {
$this->_helper->_flashMessenger->addMessage(
$this->view->translate('You are not allowed to view this page'));
$this->_redirect('/' . $lang . '/woeid/' . $location . '/give');
return;
}

$data['user_id'] = $auth->getIdentity()->id;
$data['thread_id'] = (int)$request->getParam('id');

$modelM = new Model_Message();
$thread = $modelM->getThreadFromId($data['thread_id']);

/* check current user is sender or recpient */
if ($data['user_id'] == $thread->user_to ||
$data['user_id'] == $thread->user_from) {

$modelM->deleteThread($data);
$this->_helper->_flashMessenger->addMessage(
$this->view->translate('Message succesfully deleted'));
$aNamespace = new Zend_Session_Namespace('Nolotiro');
$this->_redirect('/' . $lang . '/message/list');

} else {

$this->_helper->_flashMessenger->addMessage(
$this->view->translate('You are not allowed to view this page'));
$this->_redirect('/' . $lang . '/woeid/' . $location . '/give');
return;
}
}

}
53 changes: 29 additions & 24 deletions application/controllers/UserController.php
Expand Up @@ -124,7 +124,8 @@ public function profileAction() {
$userId = $this->view->userId = $this->getRequest()->getParam('id');

if (!$userId) {
$this->_helper->_flashMessenger->addMessage($this->view->translate('This user does not exist'));
$this->_helper->_flashMessenger->addMessage(
$this->view->translate('This user does not exist'));
$this->_redirect('/' . $lang . '/woeid/' . $this->location . '/give');
}

Expand All @@ -135,32 +136,36 @@ public function profileAction() {
$this->view->friendlist = $modelU->fetchUserFriends($userId);

$send_link = '/' . $lang . '/message/create/id_user_to/' . $userId;
$this->view->sendmessage_tab = '
<a class="world_link" href="' . $send_link . '">' .
$this->view->translate('send message to') . ' ' . $userName . '
<img src="/images/email_send.png" alt="send a message"/></a>';

$auth = Zend_Auth::getInstance ();
if ($auth->hasIdentity()) {
$this->view->myUserId = $auth->getIdentity()->id;

//check if this user is friend or not to paint proper link
if ($modelU->isMyFriend($this->view->myUserId, $userId)) {
$this->view->friend_link = '
<a class="world_link"
href="/' . $lang . '/user/addfriend/id/' . $userId . '">
' . $this->view->translate('add') . ' ' .
$userName . ' ' .
$this->view->translate('to your friends list'). '
<img src="/images/friend_add.png" alt="Add friend"/></a>';
} else {
$this->view->friend_link = '
<a class="world_link"
href="/' . $lang . '/user/deletefriend/id/' . $userId . '">
' . $this->view->translate('remove') . ' ' .
$userName . ' ' .
$this->view->translate('from your friends list'). '
<img src="/images/friend_delete.png" alt="Delete friend"/></a>';
$myUserId = $this->view->myUserId = $auth->getIdentity()->id;

// Don't display message or add_friend links if it's my own profile
if ($myUserId != $userId) {
$this->view->sendmessage_tab = '
<a class="world_link" href="' . $send_link . '">' .
$this->view->translate('send message to') . ' ' . $userName . '
<img src="/images/email_send.png" alt="send a message"/></a>';

// Check if this user is friend or not to paint proper link
if ($modelU->isMyFriend($myUserId, $userId)) {
$this->view->friend_link = '
<a class="world_link"
href="/' . $lang . '/user/addfriend/id/' . $userId . '">
' . $this->view->translate('add') . ' ' .
$userName . ' ' .
$this->view->translate('to your friends list'). '
<img src="/images/friend_add.png" alt="Add friend"/></a>';
} else {
$this->view->friend_link = '
<a class="world_link"
href="/' . $lang . '/user/deletefriend/id/' . $userId . '">
' . $this->view->translate('remove') . ' ' .
$userName . ' ' .
$this->view->translate('from your friends list'). '
<img src="/images/friend_delete.png" alt="Delete friend"/></a>';
}
}
} else
$this->view->myUserId = null;
Expand Down
2 changes: 1 addition & 1 deletion application/forms/MessageCreate.php
Expand Up @@ -17,7 +17,7 @@ public function init() {
$this->addElement ( 'textarea', 'body', array (
'validators' => array (
array ('StringLength', false, array (3, 800) )),
'rows' => 3,
'rows' => 6,
'required' => true
));
$this->getElement('body')->removeDecorator('label');
Expand Down
2 changes: 1 addition & 1 deletion application/forms/MessageReply.php
Expand Up @@ -8,7 +8,7 @@ public function init() {

$this->addElement ( 'textarea', 'body', array (
'validators' => array (
array ('StringLength', false, array (3, 800) )),
array ('StringLength', false, array (2, 800) )),
'rows' => 3,
'required' => true
));
Expand Down

0 comments on commit 151ec52

Please sign in to comment.