Beispiel #1
0
$owner = elgg_get_logged_in_user_entity();
$site = elgg_get_site_entity();
$dbprefix = elgg_get_config('dbprefix');
$users = array((int) $user_guid, (int) $owner->guid);
sort($users);
$chat_id = json_encode($users);
$chats = elgg_get_entities_from_metadata(array('type' => 'object', 'subtype' => 'chat', 'owner_guid' => $site->guid, 'metadata_name_value_pairs' => array('name' => 'chat_id', 'value' => $chat_id)));
$json = array();
if (empty($chats)) {
    $chat = new ElggChat();
    $chat->owner_guid = $site->guid;
    $chat->container_guid = $site->guid;
    $chat->access_id = ACCESS_LOGGED_IN;
    $chat->title = 'Test';
    $chat->chat_id = $chat_id;
    if ($chat->save()) {
        $chat->addMember($owner->guid);
        $chat->addMember($user_guid);
    } else {
        register_error(elgg_echo('chat:error:cannot_save'));
        return;
    }
} else {
    $chat = $chats[0];
    /**
     * Get messages ascending to get latest messages and then reverse
     * them to make the order chronological (latest messages at bottom).
     */
    $messages = elgg_get_entities(array('type' => 'object', 'subtype' => 'chat_message', 'container_guid' => $chat->guid, 'limit' => 6, 'order_by' => 'e.time_created desc', 'pagination' => false));
    $messages = array_reverse($messages);
    foreach ($messages as $message) {
Beispiel #2
0
}
if ($guid) {
    $entity = get_entity($guid);
    if (elgg_instanceof($entity, 'object', 'chat') && $entity->canEdit()) {
        // Everything ok
    } else {
        register_error(elgg_echo('noaccess'));
        forward(REFERER);
    }
} else {
    $entity = new ElggChat();
    $entity->subtype = 'chat';
    $entity->access_id = ACCESS_LOGGED_IN;
}
$entity->title = $title;
if ($entity->save()) {
    elgg_clear_sticky_form('chat');
} else {
    register_error(elgg_echo('chat:error:cannot_save'));
    forward(REFERER);
}
$user = elgg_get_logged_in_user_entity();
// Add user itself if missing
if (!in_array($user->getGUID(), $members)) {
    $members[] = $user->getGUID();
}
$old_member_guids = $entity->getMemberGuids();
// Add selected users to the chat
foreach ($members as $member_guid) {
    // Skip users that are already members
    if (in_array($member_guid, $old_member_guids)) {