Ejemplo n.º 1
0
 /**
  * Called before each test method.
  */
 public function setUp()
 {
     $this->group = new ElggGroup();
     $this->group->membership = ACCESS_PUBLIC;
     $this->group->access_id = ACCESS_PUBLIC;
     $this->group->save();
     $this->user = new ElggUser();
     $this->user->username = '******' . rand();
     $this->user->save();
 }
 /**
  * Checks if additional select columns are readable as volatile data even if we hit the cache while fetching entity.
  *
  * https://github.com/Elgg/Elgg/issues/5544
  */
 public function testSqlAdditionalSelectsAsVolatileDataWithCache()
 {
     // remove ignore access as it disables entity cache
     $access = elgg_set_ignore_access(false);
     // may not have groups in DB - let's create one
     $group = new ElggGroup();
     $group->name = 'test_group';
     $group->access_id = ACCESS_PUBLIC;
     $this->assertTrue($group->save() !== false);
     foreach (array('site', 'user', 'group', 'object') as $type) {
         $entities = elgg_get_entities(array('type' => $type, 'selects' => array('42 as added_col3'), 'limit' => 1));
         $entity = array_shift($entities);
         $this->assertTrue($entity instanceof ElggEntity);
         $this->assertEqual($entity->added_col3, null, "Additional select columns are leaking to attributes for " . get_class($entity));
         $this->assertEqual($entity->getVolatileData('select:added_col3'), 42);
         // make sure we have cached the entity
         $this->assertNotEqual(false, _elgg_retrieve_cached_entity($entity->guid));
     }
     // run these again but with different value to make sure cache does not interfere
     foreach (array('site', 'user', 'group', 'object') as $type) {
         $entities = elgg_get_entities(array('type' => $type, 'selects' => array('64 as added_col3'), 'limit' => 1));
         $entity = array_shift($entities);
         $this->assertTrue($entity instanceof ElggEntity);
         $this->assertEqual($entity->added_col3, null, "Additional select columns are leaking to attributes for " . get_class($entity));
         $this->assertEqual($entity->getVolatileData('select:added_col3'), 64, "Failed to overwrite volatile data in cached entity");
     }
     elgg_set_ignore_access($access);
     $group->delete();
 }
Ejemplo n.º 3
0
function CreateLTIGroup($user, $name, $context_id, $consumer_key)
{
    $group_guid = 0;
    $group = new ElggGroup($group_guid);
    // Set the group properties that we can!
    $group->name = $name;
    $group->context_id = $context_id;
    // This is a unique identifier from the consumer for this context
    $group->consumer_key = $consumer_key;
    // Which consumer is creating this group
    $group->membership = ACCESS_PRIVATE;
    $group->access_id = ACCESS_PUBLIC;
    $group->briefdescription = elgg_echo('LTI:provision:group');
    $consumer_instance = new LTI_Tool_Consumer_Instance($group->consumer_key, elgg_get_config('dbprefix'));
    $context = new LTI_Context($consumer_instance, $group->context_id);
    $group->description = $context->title;
    $group->save();
    $group->join($user);
    // Add images
    $prefix = 'groups/' . $group->guid;
    $filename = GetImage($consumer_key, '.jpg');
    $thumbtiny = get_resized_image_from_existing_file($filename, 25, 25, true);
    $thumbsmall = get_resized_image_from_existing_file($filename, 40, 40, true);
    $thumbmedium = get_resized_image_from_existing_file($filename, 100, 100, true);
    $thumblarge = get_resized_image_from_existing_file($filename, 200, 200, false);
    if ($thumbtiny) {
        $thumb = new ElggFile();
        $thumb->owner_guid = $group->owner_guid;
        $thumb->setMimeType('image/jpeg');
        $thumb->setFilename($prefix . "tiny.jpg");
        $thumb->open("write");
        $thumb->write($thumbtiny);
        $thumb->close();
        $thumb->setFilename($prefix . "small.jpg");
        $thumb->open("write");
        $thumb->write($thumbsmall);
        $thumb->close();
        $thumb->setFilename($prefix . "medium.jpg");
        $thumb->open("write");
        $thumb->write($thumbmedium);
        $thumb->close();
        $thumb->setFilename($prefix . "large.jpg");
        $thumb->open("write");
        $thumb->write($thumblarge);
        $thumb->close();
        $group->icontime = time();
    }
    // return the URL
    return $group;
}
 /**
  * Called before each test object.
  */
 public function __construct()
 {
     elgg_set_ignore_access(true);
     $this->entities = array();
     $this->subtypes = array('object' => array(), 'user' => array(), 'group' => array());
     // sites are a bit wonky.  Don't use them just now.
     $this->types = array('object', 'user', 'group');
     // create some fun objects to play with.
     // 5 with random subtypes
     for ($i = 0; $i < 5; $i++) {
         $subtype = 'test_object_subtype_' . rand();
         $e = new ElggObject();
         $e->subtype = $subtype;
         $e->save();
         $this->entities[] = $e;
         $this->subtypes['object'][] = $subtype;
     }
     // and users
     for ($i = 0; $i < 5; $i++) {
         $subtype = "test_user_subtype_" . rand();
         $e = new ElggUser();
         $e->username = "******" . rand();
         $e->subtype = $subtype;
         $e->save();
         $this->entities[] = $e;
         $this->subtypes['user'][] = $subtype;
     }
     // and groups
     for ($i = 0; $i < 5; $i++) {
         $subtype = "test_group_subtype_" . rand();
         $e = new ElggGroup();
         $e->subtype = $subtype;
         $e->save();
         $this->entities[] = $e;
         $this->subtypes['group'][] = $subtype;
     }
     parent::__construct();
 }
Ejemplo n.º 5
0
//$dgroup->files_enable = get_input('files_enable', 'yes');
//$dgroup->pages_enable = get_input('pages_enable', 'yes');
//$dgroup->forum_enable = get_input('forum_enable', 'yes');
// Set dgroup tool options
if (isset($CONFIG->dgroup_tool_options)) {
    foreach ($CONFIG->dgroup_tool_options as $dgroup_option) {
        $dgroup_option_toggle_name = $dgroup_option->name . "_enable";
        if ($dgroup_option->default_on) {
            $dgroup_option_default_value = 'yes';
        } else {
            $dgroup_option_default_value = 'no';
        }
        $dgroup->{$dgroup_option_toggle_name} = get_input($dgroup_option_toggle_name, $dgroup_option_default_value);
    }
}
$dgroup->save();
if (!$dgroup->isMember($user)) {
    $dgroup->join($user);
}
// Creator always a member
// Now see if we have a file icon
if (isset($_FILES['icon']) && substr_count($_FILES['icon']['type'], 'image/')) {
    $prefix = "dgroups/" . $dgroup->guid;
    $filehandler = new ElggFile();
    $filehandler->owner_guid = $dgroup->owner_guid;
    $filehandler->setFilename($prefix . ".jpg");
    $filehandler->open("write");
    $filehandler->write(get_uploaded_file('icon'));
    $filehandler->close();
    $thumbtiny = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), 25, 25, true);
    $thumbsmall = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), 40, 40, true);
Ejemplo n.º 6
0
 public function testJoinLeaveGroupACL()
 {
     if (!elgg_is_active_plugin('groups')) {
         return;
     }
     $group = new ElggGroup();
     $group->name = 'Test group';
     $group->save();
     $result = $group->join($this->user);
     $this->assertTrue($result);
     // disable security since we run as admin
     $ia = elgg_set_ignore_access(false);
     // need to set the page owner to emulate being in a group context.
     // this is kinda hacky.
     elgg_set_page_owner_guid($group->getGUID());
     if ($result) {
         $can_edit = can_edit_access_collection($group->group_acl, $this->user->guid);
         $this->assertTrue($can_edit);
     }
     $result = $group->leave($this->user);
     $this->assertTrue($result);
     if ($result) {
         $can_edit = can_edit_access_collection($group->group_acl, $this->user->guid);
         $this->assertFalse($can_edit);
     }
     elgg_set_ignore_access($ia);
     $group->delete();
 }
Ejemplo n.º 7
0
 foreach (array(hypefaker_get_group_content_access_mode('members_only'), hypefaker_get_group_content_access_mode('unrestricted')) as $content_access_mode) {
     foreach (array(ACCESS_PRIVATE, ACCESS_PUBLIC) as $membership) {
         for ($i = 0; $i < $count; $i++) {
             $users = elgg_get_entities_from_metadata(array('types' => 'user', 'limit' => 1, 'order_by' => 'RAND()', 'metadata_names' => '__faker'));
             $owner = $users[0];
             $group = new ElggGroup();
             $group->name = $faker->sentence(5);
             $group->owner_guid = $owner->guid;
             $group->container_guid = $owner->guid;
             $group->description = $faker->text(500);
             $group->briefdescription = $faker->bs;
             $group->interests = $faker->words(10);
             $group->access_id = ACCESS_PUBLIC;
             $group->membership = $membership;
             $group->content_access_mode = $content_access_mode;
             $guid = $group->save();
             if (!$guid) {
                 $errors++;
                 continue;
             }
             if ($visibility != ACCESS_PUBLIC && $visibility != ACCESS_LOGGED_IN) {
                 $visibility = $group->group_acl;
             }
             if ($group->access_id != $visibility) {
                 $group->access_id = $visibility;
             }
             $group->__faker = true;
             // store this flag so we can easily find fake entities
             $group->join($owner);
             $icon_sizes = elgg_get_config('icon_sizes');
             $files = array();
Ejemplo n.º 8
0
 public function testElggObjectContainer()
 {
     $this->assertEqual($this->entity->getContainerGUID(), elgg_get_logged_in_user_guid());
     // create and save to group
     $group = new \ElggGroup();
     $guid = $group->save();
     $this->assertTrue($this->entity->setContainerGUID($guid));
     // check container
     $this->assertEqual($this->entity->getContainerGUID(), $guid);
     $this->assertIdenticalEntities($group, $this->entity->getContainerEntity());
     // clean up
     $group->delete();
 }
 public function testElggObjectContainer()
 {
     $this->assertEqual($this->entity->getContainer(), get_loggedin_userid());
     // fals when container not a group
     $this->assertFalse($this->entity->getContainerEntity());
     // create and save to group
     $group = new ElggGroup();
     $guid = $group->save();
     $this->assertTrue($this->entity->setContainer($guid));
     // check container
     $this->assertEqual($this->entity->getContainer(), $guid);
     $this->assertIdentical($group, $this->entity->getContainerEntity());
     // clean up
     $group->delete();
 }
Ejemplo n.º 10
0
 /**
  * createGroups Create the groups in Elgg
  *
  * @return boolean
  */
 function createGroups($post_data)
 {
     global $CONFIG;
     $final_report = array();
     /// Final report of the creation process
     $this->headers = explode(',', $post_data['header']);
     /// Create the groups from the $groups array
     for ($i = 0; $i < $post_data['num_of_groups']; $i++) {
         /// Get the group details from POST data for all headers
         foreach ($this->headers as $header) {
             $group[$header] = $post_data[$header][$i];
         }
         /// Add the basic fields to the report
         $report = array('name' => $group['name'], 'subtype' => $group['subtype'], 'description' => $group['description'], 'membership' => $group['membership'], 'visibility' => $group['visibility'], 'container_guid' => $group['container_guid']);
         /// Try to create the group
         try {
             $user = elgg_get_logged_in_user_entity();
             $new_group = new ElggGroup();
             $new_group->name = $group['name'];
             $new_group->subtype = $group['subtype'];
             $new_group->membership = $group['membership'];
             $new_group->container_guid = $group['container_guid'];
             $new_group->owner_guid = $user->guid;
             $new_group->access_id = $group['visibility'];
             $guid = $new_group->save();
             if ($guid) {
                 elgg_set_page_owner_guid($group->guid);
                 $new_group->join($user);
                 add_to_river('river/group/create', 'create', $user->guid, $new_group->guid);
                 if (elgg_get_plugin_setting('hidden_groups', 'groups') == 'yes') {
                     $visibility = $group['visibility'];
                     if ($visibility != ACCESS_PUBLIC && $visibility != ACCESS_LOGGED_IN) {
                         $visibility = $new_group->group_acl;
                     }
                     if ($new_group->access_id != $visibility) {
                         $new_group->access_id = $visibility;
                         $new_group->save();
                     }
                 }
                 /// Add all other fields as metadata
                 foreach ($this->headers as $header) {
                     if (in_array($header, array('name', 'subtype', 'membership', 'visibility', 'description', 'container_guid'))) {
                         continue;
                     }
                     /// Metadata could be a comma separated list if the delimiter is something else than a comma
                     if ($this->delimiter != ',' && strpos($group[$header], ',')) {
                         /// Multiple tags found
                         $tags = string_to_tag_array($group[$header]);
                         foreach ($tags as $tag) {
                             create_metadata($guid, $header, $tag, 'text', $guid, ACCESS_PRIVATE, true);
                         }
                     } else {
                         create_metadata($guid, $header, $group[$header], 'text', $guid);
                     }
                     /// Add this metadata field to the report
                     $report[$header] = $group[$header];
                 }
                 /// Add status message to the report
                 $report['status'] = elgg_echo('upload_groups:success');
             }
         } catch (RegistrationException $r) {
             //register_error($r->getMessage());
             $report['status'] = '<span class="error">' . $r->getMessage() . '</span>';
             $report['password'] = '';
             /// Reset password in failed cases
             $this->number_of_failed_groups++;
         }
         $final_report[] = $report;
     }
     $this->creation_report = $final_report;
     return true;
 }
Ejemplo n.º 11
0
 /**
  * Make sure entity is loaded from cache during save operations
  * See #10612
  */
 public function testNewGroupLoadedFromCacheDuringSaveOperations()
 {
     $group = new \ElggGroup();
     $group->subtype = 'test_group_subtype';
     // Add temporary metadata, annotation and private settings
     // to extend the scope of tests and catch issues with save operations
     $group->test_metadata = 'bar';
     $group->annotate('test_annotation', 'baz');
     $group->setPrivateSetting('test_setting', 'foo');
     $metadata_called = false;
     $metadata_event_handler = function ($event, $type, $metadata) use(&$metadata_called) {
         /* @var $metadata \ElggMetadata */
         $entity = get_entity($metadata->entity_guid);
         $this->assertEqual($metadata->entity_guid, $entity->guid);
         $metadata_called = true;
     };
     $annotation_called = false;
     $annotation_event_handler = function ($event, $type, $annotation) use(&$annotation_called) {
         /* @var $metadata \ElggAnnotation */
         $entity = get_entity($annotation->entity_guid);
         $this->assertEqual($annotation->entity_guid, $entity->guid);
         $annotation_called = true;
     };
     elgg_register_event_handler('create', 'metadata', $metadata_event_handler);
     elgg_register_event_handler('create', 'annotation', $annotation_event_handler);
     $group->save();
     elgg_unregister_event_handler('create', 'metadata', $metadata_event_handler);
     elgg_unregister_event_handler('create', 'annotation', $annotation_event_handler);
     $group->delete();
     $this->assertTrue($metadata_called);
     $this->assertTrue($annotation_called);
 }
Ejemplo n.º 12
0
 /**
  * Ensure additional select columns do not end up in entity attributes.
  *
  * https://github.com/Elgg/Elgg/issues/5538
  */
 public function test_extra_columns_dont_appear_in_attributes()
 {
     global $ENTITY_CACHE;
     // may not have groups in DB - let's create one
     $group = new ElggGroup();
     $group->name = 'test_group';
     $group->access_id = ACCESS_PUBLIC;
     $this->assertTrue($group->save() !== false);
     // entity cache interferes with our test
     $ENTITY_CACHE = array();
     foreach (array('site', 'user', 'group', 'object') as $type) {
         $entities = elgg_get_entities(array('type' => $type, 'selects' => array('1 as _nonexistent_test_column'), 'limit' => 1));
         if (!$this->assertTrue($entities, "Query for '{$type}' did not return an entity.")) {
             continue;
         }
         $entity = $entities[0];
         $this->assertNull($entity->_nonexistent_test_column, "Additional select columns are leaking to attributes for '{$type}'");
     }
     $group->delete();
 }
Ejemplo n.º 13
0
if (!$request instanceof \Groups\Requests\Request) {
    register_error(elgg_echo('group_requests:not_found'));
    forward(REFERER);
}
$user = $request->getOwnerEntity();
if ($action == 'deny') {
    $subject = elgg_echo('group_requests:denied:title', array(), $user->language);
    $body = elgg_echo('group_requests:denied:body', array($user->name, $request->title), $user->language);
    notify_user($request->owner_guid, elgg_get_site_entity()->guid, $subject, $body);
    $request->delete();
    system_message(elgg_echo('group_requests:deny:success'));
    forward(REFERER);
}
$group = new ElggGroup();
$group->owner_guid = $request->owner_guid;
$group->container_guid = $request->container_guid;
$group->name = $request->title;
$group->access_id = ACCESS_PUBLIC;
if ($group->save()) {
    // Besides being owner, user also needs to be a member
    $group->join($user);
    $subject = elgg_echo('group_requests:approved:title', array(), $user->language);
    $body = elgg_echo('group_requests:approved:body', array($user->name, $group->name, elgg_normalize_url("groups/edit/{$group->guid}")), $user->language);
    $params = array('action' => 'create', 'object' => $group);
    notify_user($user->guid, elgg_get_site_entity()->guid, $subject, $body, $params);
    $request->delete();
    system_message(elgg_echo('group_requests:approve:success'));
} else {
    register_error(elgg_echo('group_requests:approve:error'));
}
forward(REFERER);
Ejemplo n.º 14
0
/**
 * 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;
}
Ejemplo n.º 15
0
 function test_can_write_to_container()
 {
     $user = new ElggUser();
     $user->username = '******' . rand();
     $user->name = 'test_user_name_' . rand();
     $user->email = '*****@*****.**';
     $user->container_guid = 0;
     $user->owner_guid = 0;
     $user->save();
     $object = new ElggObject();
     $object->save();
     $group = new ElggGroup();
     $group->save();
     // disable access overrides because we're admin.
     $ia = elgg_set_ignore_access(false);
     $this->assertFalse(can_write_to_container($user->guid, $object->guid));
     global $elgg_test_user;
     $elgg_test_user = $user;
     // register hook to allow access
     function can_write_to_container_test_hook($hook, $type, $value, $params)
     {
         global $elgg_test_user;
         if ($params['user']->getGUID() == $elgg_test_user->getGUID()) {
             return true;
         }
     }
     elgg_register_plugin_hook_handler('container_permissions_check', 'all', 'can_write_to_container_test_hook');
     $this->assertTrue(can_write_to_container($user->guid, $object->guid));
     elgg_unregister_plugin_hook_handler('container_permissions_check', 'all', 'can_write_to_container_test_hook');
     $this->assertFalse(can_write_to_container($user->guid, $group->guid));
     $group->join($user);
     $this->assertTrue(can_write_to_container($user->guid, $group->guid));
     elgg_set_ignore_access($ia);
     $user->delete();
     $object->delete();
     $group->delete();
 }
Ejemplo n.º 16
0
        $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;
    }
}
$must_move_icons = $owner_has_changed && $old_icontime;
$group->save();
// 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);
    if ($visibility != ACCESS_PUBLIC && $visibility != ACCESS_LOGGED_IN) {
        $visibility = $group->group_acl;
    }
    if ($group->access_id != $visibility) {
        $group->access_id = $visibility;
    }
}
$group->save();
// group saved so clear sticky form
Ejemplo n.º 17
0
/**
 * Create all local groups
 */
function create_local_groups()
{
    // first, creates country groups
    $countrygroup_guid = array();
    $options["type"] = 'group';
    $options["limit"] = NULL;
    $options["metadata_name_value_pairs"][] = array("name" => 'grouptype', "value" => 'local');
    $options["metadata_name_value_pairs"][] = array("name" => 'localtype', "value" => 'national');
    $existing_countrygroups = elgg_get_entities_from_metadata($options);
    $country_data = get_country_data();
    foreach ($country_data as $country) {
        if (!is_group_name_existing($country, $existing_countrygroups)) {
            $group = new ElggGroup();
            $group->name = $country;
            $group->description = "";
            $group->grouptype = 'local';
            $group->localtype = 'national';
            $group->membership = ACCESS_PUBLIC;
            $group->access_id = ACCESS_LOGGED_IN;
            $group->save();
            $countrygroup_guid[$country] = $group->guid;
        }
    }
    // then, creates departement groups
    $depgroup_guids = array();
    $options["type"] = 'group';
    $options["limit"] = NULL;
    $options["metadata_name_value_pairs"][] = array("name" => 'grouptype', "value" => 'local');
    $options["metadata_name_value_pairs"][] = array("name" => 'localtype', "value" => 'departemental');
    $existing_depgroups = elgg_get_entities_from_metadata($options);
    $departements_data = get_departement_data();
    foreach ($departements_data as $num => $name) {
        $groupname = $num . " - " . $name;
        if (!is_group_name_existing($groupname, $existing_depgroups)) {
            $group = new ElggGroup();
            $group->name = $groupname;
            $group->description = "";
            $group->grouptype = 'local';
            $group->localtype = 'departemental';
            $group->membership = ACCESS_PUBLIC;
            $group->access_id = ACCESS_LOGGED_IN;
            $group->save();
            $depgroup_guids[$num] = $group->guid;
        }
    }
    // finally, creates region groups
    $options["metadata_name_value_pairs"] = array(array('name' => 'grouptype', 'value' => 'local', 'operand' => '=', 'case_sensitive' => TRUE), array('name' => 'localtype', 'value' => 'regional', 'operand' => '=', 'case_sensitive' => TRUE));
    $existing_region_groups = elgg_get_entities_from_metadata($options);
    $regions_data = get_region_data();
    foreach ($regions_data as $name => $dep_nums) {
        if (!is_group_name_existing($name, $existing_region_groups)) {
            $group = new ElggGroup();
            $group->name = $name;
            $group->grouptype = 'local';
            $group->localtype = 'regional';
            $group->membership = ACCESS_PUBLIC;
            $group->access_id = ACCESS_LOGGED_IN;
            $group->save();
            //add relationship with departement groups
            foreach ($dep_nums as $num) {
                $depgroup = get_entity($depgroup_guids[$num]);
                if ($depgroup) {
                    $group->addRelationship($depgroup->guid, 'child');
                    $depgroup->addRelationship($group->guid, 'parent');
                    $depgroup->save();
                }
            }
            // add relationship with country group
            $countrygroup = get_entity($countrygroup_guid['France']);
            if ($countrygroup) {
                $countrygroup->addRelationship($group->guid, 'child');
                $group->addRelationship($countrygroup->guid, 'parent');
                $countrygroup->save();
            }
            $group->save();
        }
    }
}
Ejemplo n.º 18
0
 /**
  * Ensure that \ElggBatch doesn't go into infinite loop when disabling annotations recursively when show hidden is enabled.
  *
  * https://github.com/Elgg/Elgg/issues/5952
  */
 public function test_disabling_annotations_infinite_loop()
 {
     //let's have some entity
     $group = new \ElggGroup();
     $group->name = 'test_group';
     $group->access_id = ACCESS_PUBLIC;
     $this->assertTrue($group->save() !== false);
     $total = 51;
     //add some annotations
     for ($cnt = 0; $cnt < $total; $cnt++) {
         $group->annotate('test_annotation', 'value_' . $total);
     }
     //disable them
     $show_hidden = access_get_show_hidden_status();
     access_show_hidden_entities(true);
     $options = array('guid' => $group->guid, 'limit' => $total);
     elgg_disable_annotations($options);
     access_show_hidden_entities($show_hidden);
     //confirm all being disabled
     $annotations = $group->getAnnotations(array('limit' => $total));
     foreach ($annotations as $annotation) {
         $this->assertTrue($annotation->enabled == 'no');
     }
     //delete group and annotations
     $group->delete();
 }
Ejemplo n.º 19
0
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);
}