<commit_before><?php

/*
 *	Copyright 2015 RhubarbPHP
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

namespace Rhubarb\Patterns\Mvp\Crud\ModelForm;

use Rhubarb\Crown\Exceptions\ForceResponseException;
use Rhubarb\Crown\Response\RedirectResponse;
use Rhubarb\Crown\String\StringTools;
use Rhubarb\Leaf\Presenters\Controls\Buttons\Button;
use Rhubarb\Leaf\Presenters\Forms\MvpRestBoundForm;
use Rhubarb\Leaf\Presenters\Presenter;
use Rhubarb\Stem\Models\Validation\Validator;
use Rhubarb\Stem\Schema\SolutionSchema;

class ModelFormPresenter extends MvpRestBoundForm
{
    protected function getTitle()
    {
        if ($this->restModel !== null) {
            if ($this->restModel->isNewRecord()) {
                return "Adding a new " . strtolower(
                    StringTools::wordifyStringByUpperCase(
                        SolutionSchema::getModelNameFromClass(get_class($this->restModel))
                    )
                ) . " entry";
            } else {
                return ucfirst(
                    strtolower(
                        StringTools::wordifyStringByUpperCase(
                            SolutionSchema::getModelNameFromClass(get_class($this->restModel))
                        )
                    ) . " '" . $this->restModel->GetLabel() . "'"
                );
            }
        } else {
            if ($this->restCollection !== null) {
                return StringTools::wordifyStringByUpperCase(
                    StringTools::makePlural(
                        SolutionSchema::getModelNameFromClass($this->restCollection->getModelClassName())
                    )
                );
            } else {
                return "Untitled";
            }
        }
    }

    protected function applyModelToView()
    {
        parent::applyModelToView();

        $this->view->title = $this->getTitle();

        if ($this->restModel) {
            $this->view->newModel = $this->restModel->isNewRecord();
        }
    }

    protected function redirectAfterSave()
    {
        $this->redirectAfterCancel();
    }

    protected function redirectAfterCancel()
    {
        throw new ForceResponseException(new RedirectResponse("../"));
    }

    protected function saveRestModel()
    {
        $this->restModel->save();
        return $this->restModel;
    }

    protected final function save()
    {
        $validator = $this->getDefaultValidator();

        if ($validator && $validator instanceof Validator) {
            if (!$this->validate($validator)) {
                return;
            }
        }

        $this->saveRestModel();
        $this->redirectAfterSave();
    }

    protected function cancel()
    {
        $this->redirectAfterCancel();
    }

    protected function delete()
    {
        $this->restModel->delete();
        $this->redirectAfterSave();
    }

    protected function onPresenterAdded(Presenter $presenter)
    {
        if ($presenter->getName() == "Save") {
            if ($presenter instanceof Button) {
                $presenter->validator = $this->getDefaultClientSideValidator();
            }
        }

        parent::onPresenterAdded($presenter);
    }<commit_msg>fix for validation error<commit_after><?php

/*
 *	Copyright 2015 RhubarbPHP
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

namespace Rhubarb\Patterns\Mvp\Crud\ModelForm;

use Rhubarb\Crown\Exceptions\ForceResponseException;
use Rhubarb\Crown\Response\RedirectResponse;
use Rhubarb\Crown\String\StringTools;
use Rhubarb\Leaf\Presenters\Controls\Buttons\Button;
use Rhubarb\Leaf\Presenters\Forms\MvpRestBoundForm;
use Rhubarb\Leaf\Presenters\Presenter;
use Rhubarb\Stem\Models\Validation\Validator;
use Rhubarb\Stem\Schema\SolutionSchema;

class ModelFormPresenter extends MvpRestBoundForm
{
    protected function getTitle()
    {
        if ($this->restModel !== null) {
            if ($this->restModel->isNewRecord()) {
                return "Adding a new " . strtolower(
                    StringTools::wordifyStringByUpperCase(
                        SolutionSchema::getModelNameFromClass(get_class($this->restModel))
                    )
                ) . " entry";
            } else {
                return ucfirst(
                    strtolower(
                        StringTools::wordifyStringByUpperCase(
                            SolutionSchema::getModelNameFromClass(get_class($this->restModel))
                        )
                    ) . " '" . $this->restModel->GetLabel() . "'"
                );
            }
        } else {
            if ($this->restCollection !== null) {
                return StringTools::wordifyStringByUpperCase(
                    StringTools::makePlural(
                        SolutionSchema::getModelNameFromClass($this->restCollection->getModelClassName())
                    )
                );
            } else {
                return "Untitled";
            }
        }
    }

    protected function applyModelToView()
    {
        parent::applyModelToView();

        $this->view->title = $this->getTitle();

        if ($this->restModel) {
            $this->view->newModel = $this->restModel->isNewRecord();
        }
    }

    protected function redirectAfterSave()
    {
        $this->redirectAfterCancel();
    }

    protected function redirectAfterCancel()
    {
        throw new ForceResponseException(new RedirectResponse("../"));
    }

    protected function saveRestModel()
    {
        $this->restModel->save();
        return $this->restModel;
    }

    protected final function save()
    {
        $validator = $this->createDefaultValidator();

        if ($validator && $validator instanceof Validator) {
            if (!$this->validate($validator)) {
                return;
            }
        }

        $this->saveRestModel();
        $this->redirectAfterSave();
    }

    protected function cancel()
    {
        $this->redirectAfterCancel();
    }

    protected function delete()
    {
        $this->restModel->delete();
        $this->redirectAfterSave();
    }

    protected function onPresenterAdded(Presenter $presenter)
    {
        if ($presenter->getName() == "Save") {
            if ($presenter instanceof Button) {
                $presenter->validator = $this->createDefaultClientSideValidator();
            }
        }

        parent::onPresenterAdded($presenter);
    }
{'fraction_non_alphanumeric': 0.10792625487821289, 'fraction_numerical': 0.0021531422419593595, 'mean_word_length': 2.045901639344262, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1681993', 'n_tokens_mistral': 1972, 'n_tokens_neox': 1914, 'n_words': 411}