<commit_before><?php

namespace verbb\cpnav\services;

use verbb\cpnav\models\Layout as LayoutModel;
use verbb\cpnav\records\Layout as LayoutRecord;

use Craft;
use craft\base\Component;
use craft\elements\User;

class LayoutService extends Component
{

    // Public Methods
    // =========================================================================

    /**
     * @param null $indexBy
     *
     * @return array
     */
    public function getAll($indexBy = null): array
    {
        $records = LayoutRecord::find()
            ->indexBy($indexBy)
            ->all();
        $models = [];

        foreach ($records as $record) {
            $model = new LayoutModel($record->getAttributes());

            $models[] = $model;
        }

        return $models;
    }

    /**
     * @param integer $layoutId
     *
     * @return null|\verbb\cpnav\models\Layout
     */
    public function getById($layoutId)
    {
        $record = LayoutRecord::findOne(['id' => $layoutId]);

        if ($record) {
            return new LayoutModel($record->getAttributes());
        }

        return null;
    }

    /**
     * @return null|\verbb\cpnav\models\Layout
     */
    public function getByUserId()
    {
        $layoutId = 1; // Default layout
        $userId = Craft::$app->getUser()->id;
        $records = LayoutRecord::find()->all();

        if (Craft::$app->getEdition() == Craft::Client) {
            $client = User::find()->client()->status(null)->one();

            // Is there even a client account?
            if ($client) {
                foreach ($records as $key => $record) {
                    if (is_array($record->permissions)) {
                        if (in_array('client', $record->permissions)) {
                            $layoutId = $record->id;

                            break; // break out immediately
                        }
                    }
                }
            }

            $variables['clientAccount'] = $client;
        } else if (Craft::$app->getEdition() == Craft::Pro) {
            $groups = Craft::$app->userGroups->getGroupsByUserId($userId);

            foreach ($groups as $index => $group) {
                foreach ($records as $key => $record) {
                    if (is_array($record->permissions)) {
                        if (in_array($group->id, $record->permissions)) {
                            $layoutId = $record->id;

                            break 2; // break out immediately
                        }
                    }
                }
            }
        }

        return $this->getById($layoutId);
    }

    /**
     * @param integer $layoutId
     *
     * @return bool
     */
    public function setDefaultLayout($layoutId): bool<commit_msg>fix layout permissions for client and user groups<commit_after><?php

namespace verbb\cpnav\services;

use verbb\cpnav\models\Layout as LayoutModel;
use verbb\cpnav\records\Layout as LayoutRecord;

use Craft;
use craft\base\Component;
use craft\elements\User;

class LayoutService extends Component
{

    // Public Methods
    // =========================================================================

    /**
     * @param null $indexBy
     *
     * @return array
     */
    public function getAll($indexBy = null): array
    {
        $records = LayoutRecord::find()
            ->indexBy($indexBy)
            ->all();
        $models = [];

        foreach ($records as $record) {
            $model = new LayoutModel($record->getAttributes());

            $models[] = $model;
        }

        return $models;
    }

    /**
     * @param integer $layoutId
     *
     * @return null|\verbb\cpnav\models\Layout
     */
    public function getById($layoutId)
    {
        $record = LayoutRecord::findOne(['id' => $layoutId]);

        if ($record) {
            return new LayoutModel($record->getAttributes());
        }

        return null;
    }

    /**
     * @return null|\verbb\cpnav\models\Layout
     */
    public function getByUserId()
    {
        $layoutId = 1; // Default layout
        /** @var LayoutRecord $records */
        $records = LayoutRecord::find()->all();

        if (Craft::$app->getEdition() == Craft::Client) {
            $client = User::find()->client()->status(null)->one();

            // Is there even a client account?
            if ($client) {
                foreach ($records as $key => $record) {
                    $permissions = json_decode($record->permissions);
                    if (\is_array($permissions) && \in_array('client', $permissions, false)) {
                        $layoutId = $record->id;

                        break; // break out immediately
                    }
                }
            }

            $variables['clientAccount'] = $client;
        } else if (Craft::$app->getEdition() == Craft::Pro) {
            $userId = Craft::$app->getUser()->id;
            $groups = Craft::$app->userGroups->getGroupsByUserId($userId);

            foreach ($groups as $index => $group) {
                foreach ($records as $key => $record) {
                    $permissions = json_decode($record->permissions);
                    if (\is_array($permissions) && \in_array($group->id, $permissions, false)) {
                        $layoutId = $record->id;

                        break 2; // break out immediately
                    }
                }
            }
        }

        return $this->getById($layoutId);
    }

    /**
     * @param integer $layoutId
     *
     * @return bool
     */
    public function setDefaultLayout($layoutId): bool
{'fraction_non_alphanumeric': 0.16179093828041385, 'fraction_numerical': 0.0007135212272565109, 'mean_word_length': 1.6460594620103823, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, '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': '7296006', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1445, 'n_words': 313}