<commit_before><?php

namespace SuperCMS\Leaves\Site\Product;

use Rhubarb\Leaf\Crud\Leaves\ModelBoundLeaf;
use SuperCMS\Controls\Notification\NotificationPrint;
use SuperCMS\Models\Product\ProductVariation;
use SuperCMS\Models\Shopping\Basket;

class ProductItem extends ModelBoundLeaf
{

    /** @var ProductItemModel */
    protected $model;

    protected function getViewClass()
    {
        return ProductItemView::class;
    }

    protected function createModel()
    {
        $model = new ProductItemModel();

        $model->addToCartEvent->attachHandler(function() {
            Basket::addVariationToBasket($this->getSelectedVariation());
            print new NotificationPrint('Item: <strong>' . $this->getSelectedVariation()->Name . '</strong> successfully added to basket <a href="/basket/">Click here to view your basket</a>');
        });

        $model->changeSelectedVariationEvent->attachHandler(function($id) {
            $this->setSelectedVariation(new ProductVariation($id));
            $variation = $this->model->selectedVariation;

            $class = new \stdClass();
            $class->MainImage = $variation->getPrimaryImage();
            $class->LargeImage = $variation->getPrimaryImage();
            $class->Name = $variation->Name;
            $class->Cost = $variation->Price;
            $class->Desc = $variation->Description;
            $class->AmountAvailable = $variation->AmountAvailable;
            $class->ImagesHTML = $this->model->getImagesHTMLEvent->raise($variation);

            return $class;
        });

        return $model;
    }

    protected function onModelCreated()
    {
        $model = parent::onModelCreated();

        $this->model->selectedVariation = $this->model->restModel->getDefaultProductVariation();

        return $model;
    }

    public function setSelectedVariation(ProductVariation $variation)
    {
        $this->model->selectedVariationId = $variation->UniqueIdentifier;
        $this->model->selectedVariation = $variation;
    }

    /**
     * @return ProductVariation
     */
    protected function getSelectedVariation()
    {
        if ($this->model->selectedVariationId&& $this->model->selectedVariationId != $this->model->selectedVariation->UniqueIdentifier) {
            return new ProductVariation($this->model->selectedVariationId);
        } else {
            return $this->model->selectedVariation;
        }
    }
}<commit_msg>Make the create model view cleaner<commit_after><?php

namespace SuperCMS\Leaves\Site\Product;

use Rhubarb\Leaf\Crud\Leaves\ModelBoundLeaf;
use SuperCMS\Controls\Notification\NotificationPrint;
use SuperCMS\Models\Product\ProductVariation;
use SuperCMS\Models\Shopping\Basket;

class ProductItem extends ModelBoundLeaf
{

    /** @var ProductItemModel */
    protected $model;

    protected function getViewClass()
    {
        return ProductItemView::class;
    }

    protected function createModel()
    {
        return new ProductItemModel();
    }

    protected function onModelCreated()
    {
        $model = parent::onModelCreated();

        $this->model->selectedVariation = $this->model->restModel->getDefaultProductVariation();

        $model->addToCartEvent->attachHandler(function() {
            Basket::addVariationToBasket($this->getSelectedVariation());
            print new NotificationPrint('Item: <strong>' . $this->getSelectedVariation()->Name . '</strong> successfully added to basket <a href="/basket/">Click here to view your basket</a>');
        });

        $model->changeSelectedVariationEvent->attachHandler(function($id) {
            $this->setSelectedVariation(new ProductVariation($id));
            $variation = $this->model->selectedVariation;

            $class = new \stdClass();
            $class->MainImage = $variation->getPrimaryImage();
            $class->LargeImage = $variation->getPrimaryImage();
            $class->Name = $variation->Name;
            $class->Cost = $variation->Price;
            $class->Desc = $variation->Description;
            $class->AmountAvailable = $variation->AmountAvailable;
            $class->ImagesHTML = $this->model->getImagesHTMLEvent->raise($variation);

            return $class;
        });

        return $model;
    }

    public function setSelectedVariation(ProductVariation $variation)
    {
        $this->model->selectedVariationId = $variation->UniqueIdentifier;
        $this->model->selectedVariation = $variation;
    }

    /**
     * @return ProductVariation
     */
    protected function getSelectedVariation()
    {
        if ($this->model->selectedVariationId&& $this->model->selectedVariationId != $this->model->selectedVariation->UniqueIdentifier) {
            return new ProductVariation($this->model->selectedVariationId);
        } else {
            return $this->model->selectedVariation;
        }
    }
}
{'fraction_non_alphanumeric': 0.12944318882268338, 'fraction_numerical': 0.0, 'mean_word_length': 2.944894651539708, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 105, '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': '6599587', 'n_tokens_mistral': 1314, 'n_tokens_neox': 1282, 'n_words': 217}