$my = JXFactory::getUser(); if (!$my->id) { include 'guest.php'; return; } $option = JRequest::getVar('option'); $view = JRequest::getVar('view'); //Should Be in Module include_once JPATH_ROOT . DS . 'components' . DS . 'com_stream' . DS . 'factory.php'; $option = JRequest::getVar('option'); $view = JRequest::getVar('view'); $task = JRequest::getVar('task'); $group_id = JRequest::getVar('group_id'); $my = JXFactory::getUser(); $lastMessageRead = $my->getParam('message_last_read'); $count = StreamMessage::countMessageSince($lastMessageRead); $streamModel = StreamFactory::getModel('stream'); // Groups // Get the list $groupModel = StreamFactory::getModel('groups'); $groupIJoin = $my->getParam('groups_member'); $groupIFollow = $my->getParam('groups_follow'); $groupIFollow = JXUtility::csvDiff($groupIFollow, $groupIJoin); $myGroupsIds = JXUtility::csvMerge($groupIFollow, $groupIJoin); class StreamGroupSorter { public static function sortLastActive($groupA, $groupB) { $my = JXFactory::getUser(); $diffA = $groupA->getParam('last_message') - $my->getParam('group_' . $groupA->id . '_last'); $diffB = $groupB->getParam('last_message') - $my->getParam('group_' . $groupB->id . '_last');
public function notification() { $my = JXFactory::getUser(); $data = array(); // No notification for guest. Trigger a logout in user's browser if (!$my->id) { $data['logout'] = true; echo json_encode($data); exit; } include_once JPATH_LIBRARIES . DS . 'joomla' . DS . 'html' . DS . 'html' . DS . 'string.php'; $phash = JRequest::getVar('pHash'); // public data cache hash $data['notification'] = array(); $data['comments'] = array(); $data['updates'] = array(); // Get my groups $streamModel = StreamFactory::getModel('stream'); $groupsModel = StreamFactory::getModel('groups'); $groupIJoin = $my->getParam('groups_member'); $groupIFollow = $my->getParam('groups_follow'); $myGroups = JXUtility::csvMerge($groupIFollow, $groupIJoin); $myGroups = $groupsModel->getGroups(array('id' => $myGroups)); // Get public notices $lastMessageRead = $my->getParam('message_last_read'); $publicCount = StreamMessage::countMessageSince($lastMessageRead); $data['notification']['company_updates'] = $publicCount; // we can merge this request with the group request so that template are fetched on the same loop but the group content is unique to each group // @todo: optimize the whole request $publicMessageContents = $streamModel->getStream(array('group_id' => 0), $publicCount); $publicMessagesHTML = '<ul>'; foreach ($publicMessageContents as $row) { $tmpl = new StreamTemplate(); $tmpl->set('stream', $row); $publicMessagesHTML .= $tmpl->fetch('notification.message'); $data['updates'][0]['message_id'][] = $row->id; } $publicMessagesHTML .= '</ul>'; $data['data_content'][0] = $publicMessagesHTML; $data['updates'][0]['name'] = 'public'; $data['updates'][0]['notification'] = $publicCount; $data['updates'][0]['content'] = $publicMessagesHTML; // @todo: this is kinda heavy. Optimize further. Otherwise the live update // will kill us $updatedGroups = array(); $updatedGroupsComment = array(); foreach ($myGroups as $group) { $lastReadId = $my->getParam('group_' . $group->id . '_read'); $lastCommentId = $my->getParam('group_' . $group->id . '_comment'); $groupLastMsg = $group->getParam('last_message'); $groupLastComment = $group->getParam('last_comment'); $groupNewMsg = $streamModel->countStream(array('!user_id' => $my->id, 'id_more' => $lastReadId, 'group_id' => $group->id)); $groupNewComment = $streamModel->countComments(array('group_id' => $group->id, 'id_more' => $lastCommentId)); // // the array key is the same as html id element of the <span> that // will take this value $data['notification']['groups_' . $group->id] = intval($groupNewMsg); // rand ( 0 , 20 ); $data['comments']['groups_' . $group->id] = intval($groupNewComment); // rand ( 0 , 20 ); $data['updates'][$group->id]['name'] = $group->name; $data['updates'][$group->id]['notification'] = intval($groupNewMsg); $data['updates'][$group->id]['comments'] = intval($groupNewComment); // Add popover data if ($groupNewMsg > 0) { $updatedGroups[] = $group; } if ($groupNewComment > 0) { $updatedGroupsComment[] = $group; } } // Get the popover data if necessary // This is done saperately to give opportunity for caching if (md5(serialize($data['notification']) . serialize($data['comments'])) != $phash) { if (!empty($updatedGroups)) { foreach ($updatedGroups as $group) { $limit = $data['notification']['groups_' . $group->id]; $newMessages = $streamModel->getStream(array('group_id' => $group->id), $limit); $newMessagesHTML = '<ul>'; foreach ($newMessages as $row) { $tmpl = new StreamTemplate(); $tmpl->set('stream', $row); $newMessagesHTML .= $tmpl->fetch('notification.message'); } $newMessagesHTML .= '</ul>'; $data['data_content']['#groups_' . $group->id . '_link'] = $newMessagesHTML; $data['updates'][$group->id]['content'] = $newMessagesHTML; } } /* if(!empty($updatedGroupsComment)){ foreach($updatedGroupsComment as $group) { $data['data_comment']['#groups_'.$group->id.'_link'] = ''; } } */ } if ($phash == md5(serialize($data['notification']) . serialize($data['comments']))) { $data['data_content'] = array(); $data['notification'] = array(); $data['comments'] = array(); $data['updates'] = array(); } $data['pHash'] = md5(serialize($data['notification']) . serialize($data['comments'])); header('Content-Type: text/json'); echo json_encode($data); exit; }