Exemple #1
0
 public function composeAction()
 {
     // Render
     $this->_helper->content->setEnabled();
     // Make form
     $this->view->form = $form = new Messages_Form_Compose();
     //$form->setAction($this->view->url(array('to' => null, 'multi' => null)));
     // Get params
     $multi = $this->_getParam('multi');
     $to = $this->_getParam('to');
     $viewer = Engine_Api::_()->user()->getViewer();
     $toObject = null;
     $permissionsTable = Engine_Api::_()->getDbtable('permissions', 'authorization');
     $messDay = Engine_Api::_()->authorization()->getPermission($viewer->level_id, 'user', 'mess_day');
     if ($messDay == null) {
         $row = $permissionsTable->fetchRow($permissionsTable->select()->where('level_id = ?', $viewer->level_id)->where('type = ?', 'user')->where('name = ?', 'mess_day'));
         if ($row) {
             $messDay = $row->value;
         }
     }
     if ($messDay > 0) {
         $messTbl = Engine_Api::_()->getDbTable('messages', 'messages');
         $select = $messTbl->select()->where('user_id = ?', $viewer->getIdentity())->where('date >= ?', date('Y-m-d H:i:s', strtotime('yesterday')));
         $numOfMessDay = count($messTbl->fetchAll($select));
         if ($numOfMessDay >= $messDay) {
             return $this->_helper->requireAuth()->forward();
         }
     }
     $messMonth = Engine_Api::_()->authorization()->getPermission($viewer->level_id, 'user', 'mess_month');
     if ($messMonth == null) {
         $row = $permissionsTable->fetchRow($permissionsTable->select()->where('level_id = ?', $viewer->level_id)->where('type = ?', 'user')->where('name = ?', 'mess_month'));
         if ($row) {
             $messMonth = $row->value;
         }
     }
     if ($messMonth > 0) {
         $messTbl = Engine_Api::_()->getDbTable('messages', 'messages');
         $select = $messTbl->select()->where('user_id = ?', $viewer->getIdentity())->where('date >= ?', date('Y-m-d H:i:s', strtotime('last month')));
         $numOfMessMonth = count($messTbl->fetchAll($select));
         if ($numOfMessMonth >= $messMonth) {
             return $this->_helper->requireAuth()->forward();
         }
     }
     if ($messDay > 0 && $messMonth > 0) {
         $description = $this->view->translate('You have %s/%s messages sent during today and %s/%s messages sent during this month.', $numOfMessDay, $messDay, $numOfMessMonth, $messMonth);
     } else {
         if ($messDay > 0) {
             $description = $this->view->translate('You have %s/%s messages sent during today.', $numOfMessDay, $messDay);
         } else {
             if ($messMonth > 0) {
                 $description = $this->view->translate('You have %s/%s messages sent during this month.', $numOfMessMonth, $messMonth);
             } else {
                 $description = $this->view->translate('You can send messages unlimited.');
             }
         }
     }
     $form->setDescription($description);
     // Build
     $isPopulated = false;
     if (!empty($to) && (empty($multi) || $multi == 'user')) {
         $multi = null;
         // Prepopulate user
         $toUser = Engine_Api::_()->getItem('user', $to);
         $isMsgable = 'friends' != Engine_Api::_()->authorization()->getPermission($viewer, 'messages', 'auth') || $viewer->membership()->isMember($toUser);
         if ($toUser instanceof User_Model_User && (!$viewer->isBlockedBy($toUser) && !$toUser->isBlockedBy($viewer)) && isset($toUser->user_id) && $isMsgable) {
             $this->view->toObject = $toObject = $toUser;
             $form->toValues->setValue($toUser->getGuid());
             $isPopulated = true;
         } else {
             $multi = null;
             $to = null;
         }
     } else {
         if (!empty($to) && !empty($multi)) {
             // Prepopulate group/event/etc
             $item = Engine_Api::_()->getItem($multi, $to);
             // Potential point of failure if primary key column is something other
             // than $multi . '_id'
             if ($item instanceof Core_Model_Item_Abstract && $item->getIdentity() && ($item->isOwner($viewer) || $item->authorization()->isAllowed($viewer, 'edit'))) {
                 $this->view->toObject = $toObject = $item;
                 $form->toValues->setValue($item->getGuid());
                 $isPopulated = true;
             } else {
                 $multi = null;
                 $to = null;
             }
         }
     }
     $this->view->isPopulated = $isPopulated;
     // Build normal
     if (!$isPopulated) {
         // Apparently this is using AJAX now?
         //      $friends = $viewer->membership()->getMembers();
         //      $data = array();
         //      foreach( $friends as $friend ) {
         //        $data[] = array(
         //          'label' => $friend->getTitle(),
         //          'id' => $friend->getIdentity(),
         //          'photo' => $this->view->itemPhoto($friend, 'thumb.icon'),
         //        );
         //      }
         //      $this->view->friends = Zend_Json::encode($data);
     }
     // Assign the composing stuff
     $composePartials = array();
     foreach (Zend_Registry::get('Engine_Manifest') as $data) {
         if (empty($data['composer'])) {
             continue;
         }
         foreach ($data['composer'] as $type => $config) {
             // is the current user has "create" privileges for the current plugin
             $isAllowed = Engine_Api::_()->authorization()->isAllowed($config['auth'][0], null, $config['auth'][1]);
             if (!empty($config['auth']) && !$isAllowed) {
                 continue;
             }
             $composePartials[] = $config['script'];
         }
     }
     $this->view->composePartials = $composePartials;
     // $this->view->composePartials = $composePartials;
     // Get config
     $this->view->maxRecipients = $maxRecipients = 10;
     // Check method/data
     if (!$this->getRequest()->isPost()) {
         return;
     }
     if (!$form->isValid($this->getRequest()->getPost())) {
         return;
     }
     // Process
     $db = Engine_Api::_()->getDbtable('messages', 'messages')->getAdapter();
     $db->beginTransaction();
     try {
         // Try attachment getting stuff
         $attachment = null;
         $attachmentData = $this->getRequest()->getParam('attachment');
         if (!empty($attachmentData) && !empty($attachmentData['type'])) {
             $type = $attachmentData['type'];
             $config = null;
             foreach (Zend_Registry::get('Engine_Manifest') as $data) {
                 if (!empty($data['composer'][$type])) {
                     $config = $data['composer'][$type];
                 }
             }
             if ($config) {
                 $plugin = Engine_Api::_()->loadClass($config['plugin']);
                 $method = 'onAttach' . ucfirst($type);
                 $attachment = $plugin->{$method}($attachmentData);
                 $parent = $attachment->getParent();
                 if ($parent->getType() === 'user') {
                     $attachment->search = 0;
                     $attachment->save();
                 } else {
                     $parent->search = 0;
                     $parent->save();
                 }
             }
         }
         $viewer = Engine_Api::_()->user()->getViewer();
         $values = $form->getValues();
         // Prepopulated
         if ($toObject instanceof User_Model_User) {
             $recipientsUsers = array($toObject);
             $recipients = $toObject;
             // Validate friends
             if ('friends' == Engine_Api::_()->authorization()->getPermission($viewer, 'messages', 'auth')) {
                 if (!$viewer->membership()->isMember($recipients)) {
                     return $form->addError('One of the members specified is not in your followers list.');
                 }
             }
         } else {
             if ($toObject instanceof Core_Model_Item_Abstract && method_exists($toObject, 'membership')) {
                 $recipientsUsers = $toObject->membership()->getMembers();
                 //        $recipients = array();
                 //        foreach( $recipientsUsers as $recipientsUser ) {
                 //          $recipients[] = $recipientsUser->getIdentity();
                 //        }
                 $recipients = $toObject;
             } else {
                 $recipients = preg_split('/[,. ]+/', $values['toValues']);
                 // clean the recipients for repeating ids
                 // this can happen if recipient is selected and then a friend list is selected
                 $recipients = array_unique($recipients);
                 // Slice down to 10
                 $recipients = array_slice($recipients, 0, $maxRecipients);
                 // Get user objects
                 $recipientsUsers = Engine_Api::_()->getItemMulti('user', $recipients);
                 // Validate friends
                 if ('friends' == Engine_Api::_()->authorization()->getPermission($viewer, 'messages', 'auth')) {
                     foreach ($recipientsUsers as &$recipientUser) {
                         if (!$viewer->membership()->isMember($recipientUser)) {
                             return $form->addError('One of the members specified is not in your followers list.');
                         }
                     }
                 }
             }
         }
         // Create conversation
         $conversation = Engine_Api::_()->getItemTable('messages_conversation')->send($viewer, $recipients, $values['title'], $values['body'], $attachment);
         // Send notifications
         foreach ($recipientsUsers as $user) {
             if ($user->getIdentity() == $viewer->getIdentity()) {
                 continue;
             }
             Engine_Api::_()->getDbtable('notifications', 'activity')->addNotification($user, $viewer, $conversation, 'message_new');
         }
         // Increment messages counter
         Engine_Api::_()->getDbtable('statistics', 'core')->increment('messages.creations');
         // Commit
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
         throw $e;
     }
     if ($this->getRequest()->getParam('format') == 'smoothbox') {
         return $this->_forward('success', 'utility', 'core', array('messages' => array(Zend_Registry::get('Zend_Translate')->_('Your message has been sent successfully.')), 'smoothboxClose' => true, 'parentRefresh' => true));
     } else {
         return $this->_forward('success', 'utility', 'core', array('messages' => array(Zend_Registry::get('Zend_Translate')->_('Your message has been sent successfully.')), 'redirect' => $conversation->getHref()));
     }
 }
Exemple #2
0
 public function composeAction()
 {
     $multi = $this->_getParam('multi', null);
     $to = $this->_getParam('to', null);
     $viewer = Engine_Api::_()->user()->getViewer();
     $this->view->navigation = $this->getNavigation();
     $this->view->form = $form = new Messages_Form_Compose();
     $friends = $this->_helper->api()->user()->getViewer()->membership()->getMembers();
     $data = array();
     foreach ($friends as $friend) {
         $friend_photo = $this->view->itemPhoto($friend, 'thumb.icon');
         $data[] = array('label' => $friend->getTitle(), 'id' => $friend->getIdentity(), 'photo' => $friend_photo);
     }
     $data = Zend_Json::encode($data);
     $this->view->friends = $data;
     if ($to !== null && empty($multi)) {
         $toUser = $this->_helper->api()->user()->getUser($to);
         if (!$viewer->isBlockedBy($toUser)) {
             $this->view->toUser = $toUser;
             $form->toValues->setValue($to);
         }
     }
     // logic for handling multiple recipients (i.e. messaging group members)
     if (!empty($multi)) {
         // get item using multi + to (which is the id of the item)
         $item = Engine_Api::_()->getItem($multi, $to);
         if ($item->isOwner($viewer)) {
             // get membership. put the id's into a comma separated value
             $select = $item->membership()->getMembersObjectSelect();
             $members = Zend_Paginator::factory($select);
             $multi_ids = '';
             foreach ($members as $member) {
                 // leave out the viewer in the recipient array
                 if ($member->getIdentity() != $viewer->getIdentity()) {
                     if (!$multi_ids) {
                         $multi_ids = $member->getIdentity();
                     } else {
                         $multi_ids = $multi_ids . "," . $member->getIdentity();
                     }
                 }
             }
             // make sure the viewer/owner is not the only one in the list
             if ($multi_ids) {
                 $this->view->multi = $multi;
                 $this->view->multi_name = $item->getTitle();
                 $this->view->multi_ids = $multi_ids;
                 $form->toValues->setValue($multi_ids);
             }
         }
     }
     // Assign the composing stuff
     $composePartials = array();
     foreach (Zend_Registry::get('Engine_Manifest') as $data) {
         if (empty($data['composer'])) {
             continue;
         }
         foreach ($data['composer'] as $type => $config) {
             $composePartials[] = $config['script'];
         }
     }
     $this->view->composePartials = $composePartials;
     // Check method/data
     if (!$this->getRequest()->isPost()) {
         return;
     }
     if (!$form->isValid($this->getRequest()->getPost())) {
         return;
     }
     // Process
     $db = $this->_helper->api()->getDbtable('messages', 'messages')->getAdapter();
     $db->beginTransaction();
     try {
         // Try attachment getting stuff
         $attachment = null;
         $attachmentData = $this->getRequest()->getParam('attachment');
         if (!empty($attachmentData) && !empty($attachmentData['type'])) {
             $type = $attachmentData['type'];
             $config = null;
             foreach (Zend_Registry::get('Engine_Manifest') as $data) {
                 if (!empty($data['composer'][$type])) {
                     $config = $data['composer'][$type];
                 }
             }
             if ($config) {
                 $plugin = Engine_Api::_()->loadClass($config['plugin']);
                 $method = 'onAttach' . ucfirst($type);
                 $attachment = $plugin->{$method}($attachmentData);
                 $parent = $attachment->getParent();
                 if ($parent->getType() === 'user') {
                     $attachment->search = 0;
                     $attachment->save();
                 } else {
                     $parent->search = 0;
                     $parent->save();
                 }
             }
         }
         $viewer = $this->_helper->api()->user()->getViewer();
         $values = $form->getValues();
         $recipients = preg_split('/[,. ]+/', $values['toValues']);
         // limit recipients if it is not a special list of members
         if (empty($multi)) {
             $recipients = array_slice($recipients, 0, 10);
         }
         // Slice down to 10
         // clean the recipients for repeating ids
         // this can happen if recipient is selected and then a friend list is selected
         $recipients = array_unique($recipients);
         $recipientsUsers = Engine_Api::_()->getItemMulti('user', $recipients);
         $conversation = Engine_Api::_()->getItemTable('messages_conversation')->send($viewer, $recipients, $values['title'], $values['body'], $attachment);
         /*
               $conversation_id = $this->_helper->api()->messages()->sendMessage(
          $viewer,
          $recipients,
          $values['title'],
          $values['body'],
          $attachment
               );
         * 
         */
         foreach ($recipientsUsers as $user) {
             if ($user->getIdentity() == $viewer->getIdentity()) {
                 continue;
             }
             Engine_Api::_()->getDbtable('notifications', 'activity')->addNotification($user, $viewer, $conversation, 'message_new');
         }
         // Increment messages counter
         Engine_Api::_()->getDbtable('statistics', 'core')->increment('messages.creations');
         $db->commit();
         return $this->_forward('success', 'utility', 'core', array('messages' => array(Zend_Registry::get('Zend_Translate')->_('Your message has been sent successfully.')), 'redirect' => $this->getFrontController()->getRouter()->assemble(array('action' => 'inbox'))));
     } catch (Exception $e) {
         $db->rollBack();
         throw $e;
     }
 }