<?php $user_guid = (int) get_input('guid'); $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). */
/** * Pull together chat variables for the save form * * @param ElggChat $chat * @return array */ function chat_prepare_form_vars($chat = NULL) { $user = elgg_get_logged_in_user_entity(); // input names => defaults $values = array('title' => NULL, 'description' => NULL, 'access_id' => ACCESS_LOGGED_IN, 'container_guid' => NULL, 'guid' => NULL, 'members' => NULL); if ($chat) { foreach (array_keys($values) as $field) { if (isset($chat->{$field})) { $values[$field] = $chat->{$field}; } } $values['members'] = $chat->getMemberGuids(); } if (elgg_is_sticky_form('chat')) { $sticky_values = elgg_get_sticky_values('chat'); foreach ($sticky_values as $key => $value) { $values[$key] = $value; } // This is not a property of chat so add separately $values['description'] = $sticky_values['message']; } elgg_clear_sticky_form('chat'); return $values; }
forward(REFERER); } if (empty($members)) { register_error(elgg_echo("chat:error:missing:members")); forward(REFERER); } 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(); }