/** * Add a user to a group * * @param ElggGroup $group the group to add the user to * @param ElggUser $user the user to be added * @param string $text (optional) extra text for the notification * * @return boolean true if successfull */ function group_tools_add_user(ElggGroup $group, ElggUser $user, $text = "") { $result = false; $loggedin_user = elgg_get_logged_in_user_entity(); if (!empty($user) && $user instanceof ElggUser && !empty($group) && $group instanceof ElggGroup && !empty($loggedin_user)) { // make sure all goes well $ia = elgg_set_ignore_access(true); if ($group->join($user)) { // Remove any invite or join request flags remove_entity_relationship($group->getGUID(), "invited", $user->getGUID()); remove_entity_relationship($user->getGUID(), "membership_request", $group->getGUID()); // notify user $subject = elgg_echo("group_tools:groups:invite:add:subject", array($group->name)); $msg = elgg_echo("group_tools:groups:invite:add:body", array($user->name, $loggedin_user->name, $group->name, $text, $group->getURL())); $params = array("group" => $group, "inviter" => $loggedin_user, "invitee" => $user); $msg = elgg_trigger_plugin_hook("invite_notification", "group_tools", $params, $msg); if (notify_user($user->getGUID(), $group->getOwnerGUID(), $subject, $msg, null, "email")) { $result = true; } } // restore access elgg_set_ignore_access($ia); } return $result; }
function group_tools_add_user(ElggGroup $group, ElggUser $user, $text = "") { $result = false; if (!empty($user) && $user instanceof ElggUser && !empty($group) && $group instanceof ElggGroup && ($loggedin_user = elgg_get_logged_in_user_entity())) { if ($group->join($user)) { // Remove any invite or join request flags remove_entity_relationship($group->getGUID(), "invited", $user->getGUID()); remove_entity_relationship($user->getGUID(), "membership_request", $group->getGUID()); // notify user $subject = elgg_echo("group_tools:groups:invite:add:subject", array($group->name)); $msg = elgg_echo("group_tools:groups:invite:add:body", array($user->name, $loggedin_user->name, $group->name, $text, $group->getURL())); if (notify_user($user->getGUID(), $group->getOwnerGUID(), $subject, $msg)) { $result = true; } } } return $result; }
/** * Send a welcome message to the new user of the group * * @param \ElggUser $recipient the new user * @param \ElggGroup $group the group * * @return void */ protected static function sendWelcomeMessage(\ElggUser $recipient, \ElggGroup $group) { if (!$recipient instanceof \ElggUser || !$group instanceof \ElggGroup) { return; } // get welcome messgae $welcome_message = $group->getPrivateSetting('group_tools:welcome_message'); $check_message = trim(strip_tags($welcome_message)); if (empty($check_message)) { return; } // replace the place holders $welcome_message = str_ireplace('[name]', $recipient->name, $welcome_message); $welcome_message = str_ireplace('[group_name]', $group->name, $welcome_message); $welcome_message = str_ireplace('[group_url]', $group->getURL(), $welcome_message); // subject $subject = elgg_echo('group_tools:welcome_message:subject', [$group->name]); // notify the user notify_user($recipient->getGUID(), $group->getGUID(), $subject, $welcome_message); }
/** * Dispatches subgroups pages. * URLs take the form of * * Group view subgroups: subgroups/owner/<group_guid> * Group manage subgroups: subgroups/manage/<group_guid> * * @param array $page * @return NULL */ function subgroups_page_handler($page) { $pages_path = elgg_get_plugins_path() . "subgroups/pages"; switch ($page[0]) { case 'add': case 'edit': elgg_set_page_owner_guid($page[1]); include $pages_path . "/subgroups/edit.php"; break; case 'owner': elgg_set_page_owner_guid($page[1]); include $pages_path . "/subgroups/owner.php"; break; case 'new': $group = new ElggGroup((int) $page[1]); if (!$group->guid) { register_error(elgg_echo('error:default')); return false; } elgg_load_library('elgg:groups'); $title = elgg_echo('subgroups:new:of', array($group->name)); elgg_push_breadcrumb(elgg_echo('groups'), "groups/all"); elgg_push_breadcrumb($group->name, $group->getURL()); elgg_push_breadcrumb(elgg_echo('subgroups:new')); set_input('container_guid', $group->guid); $body = elgg_view_layout('content', array('content' => elgg_view('groups/edit'), 'title' => $title, 'filter' => '')); echo elgg_view_page($title, $body); break; default: return false; } return true; }
/** * Helper function to transfer the ownership of a group to a new user * * @param ElggGroup $group the group to transfer * @param ElggUser $new_owner the new owner * * @return bool */ function group_tools_transfer_group_ownership(ElggGroup $group, ElggUser $new_owner) { if (!$group instanceof ElggGroup || !$group->canEdit()) { return false; } if (!$new_owner instanceof ElggUser) { return false; } $loggedin_user = elgg_get_logged_in_user_entity(); // register plugin hook to make sure transfer can complete elgg_register_plugin_hook_handler('permissions_check', 'group', '\\ColdTrick\\GroupTools\\Access::allowGroupOwnerTransfer'); $old_owner = $group->getOwnerEntity(); // transfer ownership $group->owner_guid = $new_owner->getGUID(); $group->container_guid = $new_owner->getGUID(); if (!$group->save()) { return false; } // make sure user is added to the group $group->join($new_owner); // remove existing group administrator role for new owner remove_entity_relationship($new_owner->getGUID(), 'group_admin', $group->getGUID()); // check for group icon if (!empty($group->icontime)) { $prefix = "groups/{$group->getGUID()}"; $sizes = elgg_get_icon_sizes($group->getType()); $ofh = new ElggFile(); $ofh->owner_guid = $old_owner->getGUID(); $nfh = new ElggFile(); $nfh->owner_guid = $group->getOwnerGUID(); foreach ($sizes as $size => $info) { // set correct file to handle $ofh->setFilename("{$prefix}{$size}.jpg"); if (!$ofh->exists()) { // file doesn't exist continue; } $nfh->setFilename("{$prefix}{$size}.jpg"); // open files $ofh->open('read'); $nfh->open('write'); // copy file $nfh->write($ofh->grabFile()); // close file $ofh->close(); $nfh->close(); // cleanup old file $ofh->delete(); } $group->icontime = time(); } // move metadata of the group to the new owner $options = ['guid' => $group->getGUID(), 'limit' => false]; $metadata = elgg_get_metadata($options); if (!empty($metadata)) { foreach ($metadata as $md) { if ($md->owner_guid == $old_owner->getGUID()) { $md->owner_guid = $new_owner->getGUID(); $md->save(); } } } // notify new owner if ($new_owner->getGUID() !== $loggedin_user->getGUID()) { $subject = elgg_echo('group_tools:notify:transfer:subject', [$group->name]); $message = elgg_echo('group_tools:notify:transfer:message', [$new_owner->name, $loggedin_user->name, $group->name, $group->getURL()]); notify_user($new_owner->getGUID(), $group->getGUID(), $subject, $message); } // unregister plugin hook to make sure transfer can complete elgg_unregister_plugin_hook_handler('permissions_check', 'group', '\\ColdTrick\\GroupTools\\Access::allowGroupOwnerTransfer'); return true; }
/** * @SWG\Definition( * definition="Group", * required={"guid","name", "membership"}, * @SWG\Property(property="guid", type="integer"), * @SWG\Property(property="name", type="string"), * @SWG\Property(property="description", type="string"), * @SWG\Property(property="membership", type="string", description="Can be open or closed."), * @SWG\Property(property="icon_url", type="string"), * @SWG\Property(property="url", type="string"), * @SWG\Property(property="activities_unread_count", type="integer"), * @SWG\Property(property="time_created", type="string") * ) */ private function parseGroup(\ElggGroup $group) { $user = elgg_get_logged_in_user_entity(); return array('guid' => $group->guid, 'name' => html_entity_decode($group->name, ENT_QUOTES), 'description' => html_entity_decode($group->description, ENT_QUOTES), 'membership' => $group->membership === 2 ? "open" : "closed", 'icon_url' => $group->getIconURL(), 'url' => $group->getURL(), 'activities_unread_count' => $this->handler->getContainerUnreadCount($user, $group), 'time_created' => date('c', $group->time_created)); }
$metadata = elgg_get_metadata(array('guid' => $group_guid, 'limit' => false)); if ($metadata) { foreach ($metadata as $md) { if ($md->owner_guid == $old_owner_guid) { $md->owner_guid = $new_owner_guid; $md->save(); } } } // @todo Remove this when #4683 fixed $owner_has_changed = true; $old_icontime = $group->icontime; // $new_owner = get_entity($new_owner_guid); $subject = elgg_echo('zhgroups:transfer:onwer:subject', array($group->name)); $body = '<div style="color:#333;font-size:16px;">' . elgg_echo('zhgroups:transfer:onwer:body', array($new_owner->name, $user->getURL(), $user->name, $group->getURL(), $group->name, $group->getURL(), $group->name)) . '</div>'; zhgroups_send_email_to_user($group, $new_owner, $subject, $body, true, true); } } $must_move_icons = $owner_has_changed && $old_icontime; $group->save(); add_entity_relationship($group->owner_guid, 'notifyemail', $group->guid); if (!check_entity_relationship($group->owner_guid, 'notifyemail', $group->guid)) { register_error(elgg_echo('zhgroups:notifications:sub:error') . elgg_echo('zhaohu:sorry')); } // Invisible group support // @todo this requires save to be called to create the acl for the group. This // is an odd requirement and should be removed. Either the acl creation happens // in the action or the visibility moves to a plugin hook if (elgg_get_plugin_setting('hidden_groups', 'groups') == 'yes') { $visibility = (int) get_input('vis', '', false);
function hj_framework_edit_object_action() { $form_name = get_input('form_name', false); elgg_make_sticky_form($form_name); if (!hj_framework_validate_form($form_name)) { return false; } $guid = get_input('guid', ELGG_ENTITIES_ANY_VALUE); $event = $guid ? 'edit' : 'create'; $type = get_input('type'); $subtype = get_input('subtype'); $class = get_subtype_class($type, $subtype); //get the attachments $attachments = $_FILES['attachments']; if ($class) { $entity = new $class($guid); } else { switch (get_input('type', 'object')) { case 'object': $entity = new ElggObject($guid); break; case 'user': $entity = new ElggUser($guid); break; case 'group': $entity = new ElggGroup($guid); break; default: return false; break; } } if ($guid) { // Entity already exists if ((int) get_input('container_guid', 0) > 0) { $entity->container_guid = get_input('container_guid', ELGG_ENTITIES_ANY_VALUE); } if ($title = get_input('title', '')) { $entity->title = $title; } if ($description = get_input('description', '')) { $entity->description = $description; } if ($access_id = get_input('access_id', ACCESS_DEFAULT)) { $entity->access_id = $access_id; } } else { // Creating new entity $entity->subtype = get_input('subtype', 'hjformsubmission'); if ($owner_guid = get_input('owner_guid', ELGG_ENTITIES_ANY_VALUE)) { $entity->owner_guid = $owner_guid; } if ($container_guid = get_input('container_guid', ELGG_ENTITIES_ANY_VALUE)) { $entity->container_guid = $container_guid; } $entity->title = get_input('title', ''); $entity->description = get_input('description', ''); $entity->access_id = get_input('access_id', ACCESS_DEFAULT); } $guid = $entity->save(); if (!$guid) { register_error(elgg_echo('hj:framework:error:cannotcreateentity')); return false; } else { //check to see if existing attachments $existingAttachments = elgg_get_entities_from_relationship(array("relationship" => "attachment", "relationship_guid" => $guid, "inverse_relationship" => true)); foreach ($existingAttachments as $attachment) { $updatedAttachment = new ElggFile($attachment->guid); $updatedAttachment->access_id = $access_id; $updatedAttachment->save(); } //$entity = get_entity($guid); if ($attachments) { $count = count($attachments['name']); for ($i = 0; $i < $count; $i++) { if ($attachments['error'][$i] || !$attachments['name'][$i]) { continue; } $name = $attachments['name'][$i]; $file = new ElggFile(); $file->container_guid = $guid; $file->title = $name; $file->access_id = (int) $entity->access_id; $prefix = "file/"; $filestorename = elgg_strtolower(time() . $name); $file->setFilename($prefix . $filestorename); $file->open("write"); $file->close(); move_uploaded_file($attachments['tmp_name'][$i], $file->getFilenameOnFilestore()); $saved = $file->save(); if ($saved) { $mime_type = ElggFile::detectMimeType($attachments['tmp_name'][$i], $attachments['type'][$i]); $info = pathinfo($name); $office_formats = array('docx', 'xlsx', 'pptx'); if ($mime_type == "application/zip" && in_array($info['extension'], $office_formats)) { switch ($info['extension']) { case 'docx': $mime_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"; break; case 'xlsx': $mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; break; case 'pptx': $mime_type = "application/vnd.openxmlformats-officedocument.presentationml.presentation"; break; } } // check for bad ppt detection if ($mime_type == "application/vnd.ms-office" && $info['extension'] == "ppt") { $mime_type = "application/vnd.ms-powerpoint"; } //add_metastring("projectId"); //$file->projectId = $project_guid; $file->setMimeType($mime_type); $file->originalfilename = $name; if (elgg_is_active_plugin('file')) { $file->simpletype = file_get_simple_type($mime_type); } $saved = $file->save(); if ($saved) { $file->addRelationship($guid, 'attachment'); } } } } $accesslevel = get_input('accesslevel', false); $params = array('entity' => $entity); $form = hj_framework_prepare_form($form_name, $params); $fields = $form['form']['fields']; $ignore_fields = array('guid', 'type', 'subtype', 'owner_guid', 'container_guid', 'access_id', 'title', 'description'); foreach ($fields as $name => $options) { if (in_array($name, $ignore_fields)) { continue; } if (!$options) { continue; } $type = elgg_extract('input_type', $options, 'text'); $accesslevel_id = isset($accesslevel[$name]) ? $accesslevel[$name] : $entity->access_id; $params = array('name' => $name, 'form_name' => $form_name, 'field' => $options, 'access_id' => $accesslevel_id, 'entity' => $entity, 'event' => $event); if (!elgg_trigger_plugin_hook('process:input', "form:input:name:{$name}", $params, false) && !elgg_trigger_plugin_hook('process:input', "form:input:type:{$type}", $params, false)) { $value = get_input($name); set_input($name, null); // if (!$value) { // elgg_delete_metadata(array( // 'guid' => $entity->guid, // 'metadata_name' => $name // )); // // continue; // } // // if (is_array($value) && count($value) > 1) { // elgg_delete_metadata(array( // 'guid' => $entity->guid, // 'metadata_name' => $name // )); // foreach ($value as $val) { // if (!empty($val)) { // create_metadata($entity->guid, $name, $val, '', $entity->owner_guid, $accesslevel_id, true); // } // } // } else { // if (is_array($value)) { // $value = implode(',', $value); // } // create_metadata($entity->guid, $name, $value, '', $entity->owner_guid, $accesslevel_id); // } $entity->{$name} = $value; } } $entity->save(); elgg_trigger_plugin_hook('process:form', "form:{$form_name}", array('form_name' => $form_name, 'entity' => $entity), null); } $forward_url = elgg_trigger_plugin_hook('action:forward', 'form', array('entity' => $entity, 'form_name' => $form_name), $entity->getURL()); foreach ($_POST['user-callout-id'] as $callout_user) { $callout_user_guids[] = $callout_user; } if ($callout_user_guids) { $calloutUsers = new UserCallout(get_entity(elgg_get_logged_in_user_guid()), $callout_user_guids, "a forum post", $forward_url); $calloutUsers->sendUserNotifications(); } system_message(elgg_echo('hj:framework:submit:success')); elgg_clear_sticky_form($form_name); hj_framework_clear_form_validation_status($form_name); return array('entity' => $entity, 'forward' => $forward_url); }