<commit_before><?php
namespace Concrete\Core\Permission\Access\Entity;

use Concrete\Core\Cache\Level\RequestCache;
use Concrete\Core\Support\Facade\Facade;
use Loader;
use Concrete\Core\User\Group\Group;
use Config;
use Concrete\Core\Permission\Access\Access as PermissionAccess;
use URL;

class GroupEntity extends Entity
{
    protected $group = false;

    public function getGroupObject()
    {
        return $this->group;
    }

    public function getAccessEntityUsers(PermissionAccess $pa)
    {
        if (is_object($this->group)) {
            return $this->group->getGroupMembers();
        }

        return [];
    }

    public function getAccessEntityTypeLinkHTML()
    {
        $html = '<a href="' . URL::to('/ccm/system/dialogs/group/search') . '" class="dropdown-item dialog-launch" dialog-width="640" dialog-height="480" dialog-modal="false" dialog-title="' . t('Add Group') . '">' . tc('PermissionAccessEntityTypeName',
                'Group') . '</a>';

        return $html;
    }

    public static function configureFromImport(\SimpleXMLElement $element)
    {
        if (isset($element['path'])) {
            $g = Group::getByPath((string) $element['path']);
        } else {
            $g = Group::getByName((string)$element['name']);
        }
        if (!is_object($g)) {
            $g = Group::add((string) $element['name'], (string) $element['description']);
        }

        return static::getOrCreate($g);
    }

    public static function getAccessEntitiesForUser($user)
    {
        $entities = array();
        $ingids = array();
        $db = Loader::db();
        foreach ($user->getUserGroups() as $key => $val) {
            $group = Group::getByID($key);
            foreach($group->getParentGroups() as $parentGroup) {
                $ingids[] = $parentGroup->getGroupID();
            }
            $ingids[] = $group->getGroupID();
        }
        $instr = implode(',', $ingids);
        $peIDs = $db->GetCol('select pae.peID from PermissionAccessEntities pae inner join PermissionAccessEntityTypes paet on pae.petID = paet.petID inner join PermissionAccessEntityGroups paeg on pae.peID = paeg.peID where petHandle = \'group\' and paeg.gID in (' . $instr . ')');
        if (is_array($peIDs)) {
            foreach ($peIDs as $peID) {
                $entity = \Concrete\Core\Permission\Access\Entity\Entity::getByID($peID);
                if (is_object($entity)) {
                    $entities[] = $entity;
                }
            }
        }

        return $entities;
    }

    public static function getOrCreate(Group $g)
    {
        /** @var RequestCache $cache */
        $cache = Facade::getFacadeApplication()->make('cache/request');
        $key = '/Permission/Access/Entity/Group/' . $g->getGroupID();
        if ($cache->isEnabled()) {
            $item = $cache->getItem($key);
            if ($item->isHit()) {
                return $item->get();
            }
        }

        $pe = null;
        $db = Loader::db();
        $petID = $db->GetOne('select petID from PermissionAccessEntityTypes where petHandle = \'group\'');
        $peID = $db->GetOne('select pae.peID from PermissionAccessEntities pae inner join PermissionAccessEntityGroups paeg on pae.peID = paeg.peID where petID = ? and paeg.gID = ?',
            array($petID, $g->getGroupID()));
        if (!$peID) {
            $db->Execute("insert into PermissionAccessEntities (petID) values(?)", array($petID));
            $peID = $db->Insert_ID();
            Config::save('concrete.misc.access_entity_updated', time());
            $db->Execute('insert into PermissionAccessEntityGroups (peID, gID) values (?, ?)',
                array($peID, $g->getGroupID()));
        }

        $pe = \Concrete\Core\Permission\Access\Entity\Entity::getByID($peID);

        if (is_object($pe) && isset($item) && $item->isMiss()) {
            $item->set($pe);
            $cache->save($item);
        }

        return $pe;
    }

    public function load()
    {<commit_msg>Remove unused variable<commit_after><?php
namespace Concrete\Core\Permission\Access\Entity;

use Concrete\Core\Cache\Level\RequestCache;
use Concrete\Core\Support\Facade\Facade;
use Loader;
use Concrete\Core\User\Group\Group;
use Config;
use Concrete\Core\Permission\Access\Access as PermissionAccess;
use URL;

class GroupEntity extends Entity
{
    protected $group = false;

    public function getGroupObject()
    {
        return $this->group;
    }

    public function getAccessEntityUsers(PermissionAccess $pa)
    {
        if (is_object($this->group)) {
            return $this->group->getGroupMembers();
        }

        return [];
    }

    public function getAccessEntityTypeLinkHTML()
    {
        $html = '<a href="' . URL::to('/ccm/system/dialogs/group/search') . '" class="dropdown-item dialog-launch" dialog-width="640" dialog-height="480" dialog-modal="false" dialog-title="' . t('Add Group') . '">' . tc('PermissionAccessEntityTypeName',
                'Group') . '</a>';

        return $html;
    }

    public static function configureFromImport(\SimpleXMLElement $element)
    {
        if (isset($element['path'])) {
            $g = Group::getByPath((string) $element['path']);
        } else {
            $g = Group::getByName((string)$element['name']);
        }
        if (!is_object($g)) {
            $g = Group::add((string) $element['name'], (string) $element['description']);
        }

        return static::getOrCreate($g);
    }

    public static function getAccessEntitiesForUser($user)
    {
        $entities = array();
        $ingids = array();
        $db = Loader::db();
        foreach ($user->getUserGroups() as $key => $val) {
            $group = Group::getByID($key);
            foreach($group->getParentGroups() as $parentGroup) {
                $ingids[] = $parentGroup->getGroupID();
            }
            $ingids[] = $group->getGroupID();
        }
        $instr = implode(',', $ingids);
        $peIDs = $db->GetCol('select pae.peID from PermissionAccessEntities pae inner join PermissionAccessEntityTypes paet on pae.petID = paet.petID inner join PermissionAccessEntityGroups paeg on pae.peID = paeg.peID where petHandle = \'group\' and paeg.gID in (' . $instr . ')');
        if (is_array($peIDs)) {
            foreach ($peIDs as $peID) {
                $entity = \Concrete\Core\Permission\Access\Entity\Entity::getByID($peID);
                if (is_object($entity)) {
                    $entities[] = $entity;
                }
            }
        }

        return $entities;
    }

    public static function getOrCreate(Group $g)
    {
        /** @var RequestCache $cache */
        $cache = Facade::getFacadeApplication()->make('cache/request');
        $key = '/Permission/Access/Entity/Group/' . $g->getGroupID();
        if ($cache->isEnabled()) {
            $item = $cache->getItem($key);
            if ($item->isHit()) {
                return $item->get();
            }
        }

        $db = Loader::db();
        $petID = $db->GetOne('select petID from PermissionAccessEntityTypes where petHandle = \'group\'');
        $peID = $db->GetOne('select pae.peID from PermissionAccessEntities pae inner join PermissionAccessEntityGroups paeg on pae.peID = paeg.peID where petID = ? and paeg.gID = ?',
            array($petID, $g->getGroupID()));
        if (!$peID) {
            $db->Execute("insert into PermissionAccessEntities (petID) values(?)", array($petID));
            $peID = $db->Insert_ID();
            Config::save('concrete.misc.access_entity_updated', time());
            $db->Execute('insert into PermissionAccessEntityGroups (peID, gID) values (?, ?)',
                array($peID, $g->getGroupID()));
        }

        $pe = \Concrete\Core\Permission\Access\Entity\Entity::getByID($peID);

        if (is_object($pe) && isset($item) && $item->isMiss()) {
            $item->set($pe);
            $cache->save($item);
        }

        return $pe;
    }

    public function load()
    {
{'fraction_non_alphanumeric': 0.1572907571410752, 'fraction_numerical': 0.0014968192590744667, 'mean_word_length': 2.3520066889632107, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1457028', 'n_tokens_mistral': 2388, 'n_tokens_neox': 2329, 'n_words': 470}