<commit_before><?php

namespace Backend\Modules\Groups\Engine;

use Backend\Core\Engine\Model as BackendModel;

/**
 * In this file we store all generic functions that we will be using in the groups module.
 */
class Model
{
    const QUERY_BROWSE =
        'SELECT g.id, g.name, COUNT(u.id) AS num_users
         FROM groups AS g
         LEFT OUTER JOIN users_groups AS ug ON g.id = ug.group_id
         LEFT OUTER JOIN users AS u ON u.id = ug.user_id
         GROUP BY g.id';

    const QUERY_ACTIVE_USERS =
        'SELECT u.id, u.email
         FROM users AS u
         INNER JOIN users_groups AS ug ON u.id = ug.user_id
         WHERE ug.group_id = ? AND u.deleted = ?';

    public static function addActionPermissions(array $actionPermissions): void
    {
        foreach ((array) $actionPermissions as $permission) {
            if (!self::existsActionPermission($permission)) {
                BackendModel::getContainer()->get('database')->insert('groups_rights_actions', $permission);
            }
        }
    }

    public static function addModulePermissions(array $modulePermissions): void
    {
        foreach ((array) $modulePermissions as $permission) {
            if (!self::existsModulePermission($permission)) {
                BackendModel::getContainer()->get('database')->insert('groups_rights_modules', $permission);
            }
        }
    }

    public static function alreadyExists(string $groupName): bool
    {
        return (bool) BackendModel::getContainer()->get('database')->getVar(
            'SELECT i.*
             FROM groups AS i
             WHERE i.name = ?',
            [$groupName]
        );
    }

    public static function delete(int $groupId): void
    {
        BackendModel::getContainer()->get('database')->delete('groups', 'id = ?', [$groupId]);
        BackendModel::getContainer()->get('database')->delete('groups_settings', 'group_id = ?', [$groupId]);
        BackendModel::getContainer()->get('database')->delete('groups_rights_actions', 'group_id = ?', [$groupId]);
        BackendModel::getContainer()->get('database')->delete('groups_rights_modules', 'group_id = ?', [$groupId]);
    }

    public static function deleteActionPermissions(array $actionPermissions): void
    {
        foreach ((array) $actionPermissions as $permission) {<commit_msg>Use variable to avoid calling the container over and over again<commit_after><?php

namespace Backend\Modules\Groups\Engine;

use Backend\Core\Engine\Model as BackendModel;

/**
 * In this file we store all generic functions that we will be using in the groups module.
 */
class Model
{
    const QUERY_BROWSE =
        'SELECT g.id, g.name, COUNT(u.id) AS num_users
         FROM groups AS g
         LEFT OUTER JOIN users_groups AS ug ON g.id = ug.group_id
         LEFT OUTER JOIN users AS u ON u.id = ug.user_id
         GROUP BY g.id';

    const QUERY_ACTIVE_USERS =
        'SELECT u.id, u.email
         FROM users AS u
         INNER JOIN users_groups AS ug ON u.id = ug.user_id
         WHERE ug.group_id = ? AND u.deleted = ?';

    public static function addActionPermissions(array $actionPermissions): void
    {
        foreach ((array) $actionPermissions as $permission) {
            if (!self::existsActionPermission($permission)) {
                BackendModel::getContainer()->get('database')->insert('groups_rights_actions', $permission);
            }
        }
    }

    public static function addModulePermissions(array $modulePermissions): void
    {
        foreach ((array) $modulePermissions as $permission) {
            if (!self::existsModulePermission($permission)) {
                BackendModel::getContainer()->get('database')->insert('groups_rights_modules', $permission);
            }
        }
    }

    public static function alreadyExists(string $groupName): bool
    {
        return (bool) BackendModel::getContainer()->get('database')->getVar(
            'SELECT i.*
             FROM groups AS i
             WHERE i.name = ?',
            [$groupName]
        );
    }

    public static function delete(int $groupId): void
    {
        /** @var SpoonDatabase $database */
        $database = BackendModel::getContainer()->get('database');

        $database->delete('groups', 'id = ?', [$groupId]);
        $database->delete('groups_settings', 'group_id = ?', [$groupId]);
        $database->delete('groups_rights_actions', 'group_id = ?', [$groupId]);
        $database->delete('groups_rights_modules', 'group_id = ?', [$groupId]);
    }

    public static function deleteActionPermissions(array $actionPermissions): void
    {
        foreach ((array) $actionPermissions as $permission) {
{'fraction_non_alphanumeric': 0.12398112398112399, 'fraction_numerical': 0.0, 'mean_word_length': 2.72146847565842, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 28, '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': '4616220', 'n_tokens_mistral': 1303, 'n_tokens_neox': 1251, 'n_words': 329}