<commit_before><?php

/*
 * This file is part of the Tinyissue package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tinyissue\Form;

use Tinyissue\Model;

/**
 * Issue is a class to defines fields & rules for add/edit issue form.
 *
 * @author <NAME> <<EMAIL>>
 */
class Issue extends FormAbstract
{
    /**
     * An instance of project model.
     *
     * @var Model\Project
     */
    protected $project;

    /**
     * Collection of all tags.
     *
     * @var \Illuminate\Database\Eloquent\Collection
     */
    protected $tags = null;

    /**
     * Is issue readonly.
     *
     * @var bool
     */
    protected $readOnly = false;

    /**
     * @param string $type
     *
     * @return \Illuminate\Database\Eloquent\Collection|null
     */
    protected function getTags($type = null)
    {
        if ($this->tags === null) {
            $this->tags = (new Model\Tag())->getGroupTags();
        }

        if ($type) {
            return $this->tags->where('name', $type)->first()->tags;
        }

        return $this->tags;
    }

    /**
     * Get issue tag for specific type/group.
     *
     * @param string $type
     *
     * @return int
     */
    protected function getIssueTag($type)
    {
        if ($this->isEditing()) {
            $groupId     = $this->getTags($type)->first()->parent_id;
            $selectedTag = $this->getModel()->tags->where('parent_id', $groupId);

            if ($selectedTag->count() > 0) {
                return $selectedTag->last();
            }
        }

        return new Model\Tag();
    }

    /**
     * @param array $params
     *
     * @return void
     */
    public function setup(array $params)
    {
        $this->project = $params['project'];
        if (!empty($params['issue'])) {
            $this->editingModel($params['issue']);
            $this->readOnly = $this->getModel()->hasReadOnlyTag(auth()->user());
        }
    }

    /**
     * @return array
     */
    public function actions()
    {
        $actions = [];

        // Check if issue is in readonly tag
        if (!$this->readOnly) {
            $actions = [
                'submit' => $this->isEditing() ? 'update_issue' : 'create_issue',
            ];

            if ($this->isEditing() && auth()->user(Model\Permission::PERM_ISSUE_MODIFY)) {
                $actions['delete'] = [
                    'type'         => 'danger_submit',
                    'label'        => trans('tinyissue.delete_something', ['name' => '#' . $this->getModel()->id]),
                    'class'        => 'close-issue',
                    'name'         => 'delete-issue',
                    'data-message' => trans('tinyissue.delete_issue_confirm'),
                ];
            }
        }

        return $actions;
    }

    /**
     * @return array
     */
    public function fields()
    {
        $issueModify = \Auth::user()->permission('issue-modify');

        $fields = [];
        $fields += $this->readOnlyMessage();
        $fields += $this->fieldTitle();
        $fields += $this->fieldBody();
        $fields += $this->fieldTypeTags();

        // Only on creating new issue
        if (!$this->isEditing()) {
            $fields += $this->fieldUpload();
        }

        // Show fields for users with issue modify permission
        if ($issueModify) {
            $fields += $this->issueModifyFields();
        }

        return $fields;
    }

    /**
     * Return a list of fields for users with issue modify permission.
     *
     * @return array
     */
    protected function issueModifyFields()
    {
        $fields = [];

        $fields['internal_status'] = [
            'type' => 'legend',
        ];

        // Status tags
        $fields += $this->fieldStatusTags();

        // Assign users
        $fields += $this->fieldAssignedTo();

        // Quotes
        $fields += $this->fieldTimeQuote();

        // Resolution tags
        $fields += $this->fieldResolutionTags();

        return $fields;
    }

    /**
     * Returns message about read only issue.
     *
     * @return array
     */
    protected function readOnlyMessage()
    {
        return [
            'readonly' => [
                'type'  => 'plaintext',
                'label' => ' ',
                'value' => '<div class="alert alert-warning">' . trans('tinyissue.readonly_issue_message') . '</div>',
            ],
        ];
    }

    /**
     * Returns title field.
     *
     * @return array
     */
    protected function fieldTitle()
    {
        return [
            'title' => [<commit_msg>Bug: readonly issue message always visible<commit_after><?php

/*
 * This file is part of the Tinyissue package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tinyissue\Form;

use Tinyissue\Model;

/**
 * Issue is a class to defines fields & rules for add/edit issue form.
 *
 * @author <NAME> <<EMAIL>>
 */
class Issue extends FormAbstract
{
    /**
     * An instance of project model.
     *
     * @var Model\Project
     */
    protected $project;

    /**
     * Collection of all tags.
     *
     * @var \Illuminate\Database\Eloquent\Collection
     */
    protected $tags = null;

    /**
     * Is issue readonly.
     *
     * @var bool
     */
    protected $readOnly = false;

    /**
     * @param string $type
     *
     * @return \Illuminate\Database\Eloquent\Collection|null
     */
    protected function getTags($type = null)
    {
        if ($this->tags === null) {
            $this->tags = (new Model\Tag())->getGroupTags();
        }

        if ($type) {
            return $this->tags->where('name', $type)->first()->tags;
        }

        return $this->tags;
    }

    /**
     * Get issue tag for specific type/group.
     *
     * @param string $type
     *
     * @return int
     */
    protected function getIssueTag($type)
    {
        if ($this->isEditing()) {
            $groupId     = $this->getTags($type)->first()->parent_id;
            $selectedTag = $this->getModel()->tags->where('parent_id', $groupId);

            if ($selectedTag->count() > 0) {
                return $selectedTag->last();
            }
        }

        return new Model\Tag();
    }

    /**
     * @param array $params
     *
     * @return void
     */
    public function setup(array $params)
    {
        $this->project = $params['project'];
        if (!empty($params['issue'])) {
            $this->editingModel($params['issue']);
            $this->readOnly = $this->getModel()->hasReadOnlyTag(auth()->user());
        }
    }

    /**
     * @return array
     */
    public function actions()
    {
        $actions = [];

        // Check if issue is in readonly tag
        if (!$this->readOnly) {
            $actions = [
                'submit' => $this->isEditing() ? 'update_issue' : 'create_issue',
            ];

            if ($this->isEditing() && auth()->user(Model\Permission::PERM_ISSUE_MODIFY)) {
                $actions['delete'] = [
                    'type'         => 'danger_submit',
                    'label'        => trans('tinyissue.delete_something', ['name' => '#' . $this->getModel()->id]),
                    'class'        => 'close-issue',
                    'name'         => 'delete-issue',
                    'data-message' => trans('tinyissue.delete_issue_confirm'),
                ];
            }
        }

        return $actions;
    }

    /**
     * @return array
     */
    public function fields()
    {
        $issueModify = \Auth::user()->permission('issue-modify');

        $fields = [];
        $fields += $this->readOnlyMessage();
        $fields += $this->fieldTitle();
        $fields += $this->fieldBody();
        $fields += $this->fieldTypeTags();

        // Only on creating new issue
        if (!$this->isEditing()) {
            $fields += $this->fieldUpload();
        }

        // Show fields for users with issue modify permission
        if ($issueModify) {
            $fields += $this->issueModifyFields();
        }

        return $fields;
    }

    /**
     * Return a list of fields for users with issue modify permission.
     *
     * @return array
     */
    protected function issueModifyFields()
    {
        $fields = [];

        $fields['internal_status'] = [
            'type' => 'legend',
        ];

        // Status tags
        $fields += $this->fieldStatusTags();

        // Assign users
        $fields += $this->fieldAssignedTo();

        // Quotes
        $fields += $this->fieldTimeQuote();

        // Resolution tags
        $fields += $this->fieldResolutionTags();

        return $fields;
    }

    /**
     * Returns message about read only issue.
     *
     * @return array
     */
    protected function readOnlyMessage()
    {
        $field = [];

        if ($this->readOnly) {
            $field = [
                'readonly' => [
                    'type'  => 'plaintext',
                    'label' => ' ',
                    'value' => '<div class="alert alert-warning">' . trans('tinyissue.readonly_issue_message') . '</div>',
                ],
            ];
        }

        return $field;
    }

    /**
     * Returns title field.
     *
     * @return array
     */
    protected function fieldTitle()
    {
        return [
            'title' => [
{'fraction_non_alphanumeric': 0.15374135401383357, 'fraction_numerical': 0.00020959966464053657, 'mean_word_length': 1.5953222735926027, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 130, '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': '4779058', 'n_tokens_mistral': 2725, 'n_tokens_neox': 2634, 'n_words': 596}