<commit_before><?php

namespace app\models\forms;

use app\models\db\Motion;
use app\models\db\MotionSection;
use app\models\exceptions\Internal;

class MotionMergeAmendmentsDraftForm
{
    /** @var Motion */
    public $origMotion;
    public $draft;

    /**
     * MotionMergeAmendmentsDraftForm constructor.
     * @param Motion $origMotion
     */
    public function __construct($origMotion)
    {
        $this->origMotion = $origMotion;
        $draftStati       = [Motion::STATUS_MERGING_DRAFT_PUBLIC, Motion::STATUS_MERGING_DRAFT_PRIVATE];
        $this->draft      = Motion::find()
            ->where(['parentMotionId' => $origMotion->id])
            ->andWhere(['status' => $draftStati])->one();
        if ($this->draft) {
            $this->draft->dateCreation = date('Y-m-d H:i:s');
        } else {
            $this->draft = new Motion();
            $this->draft->setAttributes($this->origMotion->getAttributes(), false);
            $this->draft->id             = null;
            $this->draft->dateCreation   = date('Y-m-d H:i:s');
            $this->draft->status         = Motion::STATUS_MERGING_DRAFT_PRIVATE;
            $this->draft->titlePrefix    = '';
            $this->draft->parentMotionId = $this->origMotion->id;
        }
    }

    /**
     * @param int $sectionId
     * @return MotionSection
     * @throws Internal
     */
    private function getSection($sectionId)
    {
        foreach ($this->draft->sections as $section) {
            if ($section->sectionId == $sectionId) {
                return $section;
            }
        }
        foreach ($this->origMotion->sections as $section) {
            if ($section->sectionId == $sectionId) {
                $newSection = new MotionSection();
                $newSection->setAttributes($section->getAttributes(), false);
                $newSection->motionId = $this->draft->id;
                return $newSection;
            }
        }
        throw new Internal('Invalid section');
    }

    /**
     * @param int $public
     * @param string[] $sections
     * @return Motion
     */
    public function save($public, $sections)
    {
        if ($public) {
            $this->draft->status = Motion::STATUS_MERGING_DRAFT_PUBLIC;
        } else {
            $this->draft->status = Motion::STATUS_MERGING_DRAFT_PRIVATE;
        }
        $this->draft->save();

        foreach ($this->origMotion->sections as $origSection) {
            $section = $this->getSection($origSection->sectionId);
            if (isset($sections[$section->sectionId])) {
                $section->dataRaw = $sections[$section->sectionId];
                $section->data    = '';
            }
            $section->save();
        }

        return $this->draft;
    }
}
<commit_msg>Bugfix: Drafts could not be saved when a slug was set<commit_after><?php

namespace app\models\forms;

use app\models\db\Motion;
use app\models\db\MotionSection;
use app\models\exceptions\Internal;

class MotionMergeAmendmentsDraftForm
{
    /** @var Motion */
    public $origMotion;
    public $draft;

    /**
     * MotionMergeAmendmentsDraftForm constructor.
     * @param Motion $origMotion
     */
    public function __construct($origMotion)
    {
        $this->origMotion = $origMotion;
        $draftStati       = [Motion::STATUS_MERGING_DRAFT_PUBLIC, Motion::STATUS_MERGING_DRAFT_PRIVATE];
        $this->draft      = Motion::find()
            ->where(['parentMotionId' => $origMotion->id])
            ->andWhere(['status' => $draftStati])->one();
        if ($this->draft) {
            $this->draft->dateCreation = date('Y-m-d H:i:s');
        } else {
            $this->draft = new Motion();
            $this->draft->setAttributes($this->origMotion->getAttributes(), false);
            $this->draft->id             = null;
            $this->draft->dateCreation   = date('Y-m-d H:i:s');
            $this->draft->status         = Motion::STATUS_MERGING_DRAFT_PRIVATE;
            $this->draft->titlePrefix    = '';
            $this->draft->parentMotionId = $this->origMotion->id;
            $this->draft->slug           = null;
        }
    }

    /**
     * @param int $sectionId
     * @return MotionSection
     * @throws Internal
     */
    private function getSection($sectionId)
    {
        foreach ($this->draft->sections as $section) {
            if ($section->sectionId == $sectionId) {
                return $section;
            }
        }
        foreach ($this->origMotion->sections as $section) {
            if ($section->sectionId == $sectionId) {
                $newSection = new MotionSection();
                $newSection->setAttributes($section->getAttributes(), false);
                $newSection->motionId = $this->draft->id;
                return $newSection;
            }
        }
        throw new Internal('Invalid section');
    }

    /**
     * @param int $public
     * @param string[] $sections
     * @return Motion
     */
    public function save($public, $sections)
    {
        if ($public) {
            $this->draft->status = Motion::STATUS_MERGING_DRAFT_PUBLIC;
        } else {
            $this->draft->status = Motion::STATUS_MERGING_DRAFT_PRIVATE;
        }
        $this->draft->save();

        foreach ($this->origMotion->sections as $origSection) {
            $section = $this->getSection($origSection->sectionId);
            if (isset($sections[$section->sectionId])) {
                $section->dataRaw = $sections[$section->sectionId];
                $section->data    = '';
            }
            $section->save();
        }

        return $this->draft;
    }
}

{'fraction_non_alphanumeric': 0.14654099235283657, 'fraction_numerical': 0.0, 'mean_word_length': 1.9568874868559412, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 115, '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': '5036754', 'n_tokens_mistral': 1679, 'n_tokens_neox': 1582, 'n_words': 272}