<commit_before><?php

namespace app\controllers;

use Yii;
use yii\base\Exception;
use yii\helpers;
use app\components\HelperMarketPlace;
use app\components\HelperUser;
use app\models\UsedItem;
use app\models\UsedItemType;
use app\models\UsedItemPhoto;
use app\models\Category;
use app\models\PhpbbUser;
use yii\web\Controller;
use yii\filters\AccessControl;

use app\components\HelperBase;
use yii\web\UploadedFile;

class MarketplaceController extends Controller
{
    public $layout = 'marketplace';

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => ['create', 'items', 'delete', 'edit', 'upload'],
                        'roles' => ['@'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['index', 'view'],
                        'roles' => ['?', '@'],
                    ],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    // Front page with catalog and search form
    public function actionIndex()
    {
        $model = new UsedItem();
        // Search request
        if (Yii::$app->request->get('UsedItem')) {
            $model->setScenario('search');
            // Search items according to received GET parameters
            $data = $model->search(Yii::$app->request->get());
        } else {
            // Get items with specified order
            $data = UsedItem::find()
                ->where('category_id > 0')
                ->orderBy(HelperMarketPlace::getSortParamForItemsList())
                ->all();
        }
        return $this->render('index', ['data' => $data, 'model' => $model]);
    }

    // Create item page
    public function actionCreate()
    {
        $model      = new UsedItem();
        $modelPhoto = new UsedItemPhoto();
        $model->setScenario('create');
        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
            $modelPhoto->validateUploadedFilesAndPassErrorsToFromModel($modelPhoto, $model);
            $model->user_id = HelperUser::uid();
            if ($model->validate(null, false) && $model->save(false)) {
                if ($modelPhoto->hasUploadedFiles()) {
                    $modelPhoto->saveUploadedFileNames($model->id);
                }
                Yii::$app->session->setFlash('item_create_success', 'A new item has been added.');
                $this->redirect('/');
            }
        }
        return $this->render('create', [
            'model'         => $model,
            'modelPhoto'    => $modelPhoto,
            'categories'    => (new Category())->prepareDropDown(),
            'typeData'      => (new UsedItemType())->prepareList()
        ]);
    }

    // View item page
    public function actionView($id)
    {
        $item = UsedItem::find()->where('id = :id', [':id' => $id])->one();
        if (!$item) {
            $this->redirect('/');
        }
        return $this->render('view', ['data' => $item]);
    }

    // View items added by a current user
    public function actionItems()
    {
        // Only users who posted any items should have access
        if (!(new PhpbbUser)->hasItems(HelperUser::uid())) {
            $this->redirect('/');
        }
        $items = PhpbbUser::findUser(HelperUser::uid())->items;
        return $this->render('items', ['data' => $items]);
    }

    // Delete an item
    public function actionDelete($id)
    {
        $item = UsedItem::findOne($id);
        // Item must be in database
        // User can only delete items which have been added by himself
        if (empty($item) || $item->user_id != HelperUser::uid()) {
            $this->redirect('/');
            return false;
        }

        $transaction = Yii::$app->db->beginTransaction();
        try {
            if ($item->delete()) {
                Yii::$app->session->setFlash('item_delete_success', 'Item has been deleted.');
            } else {
                Yii::$app->session->setFlash('item_delete_error', 'Item could not be deleted.');
            }
            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollBack();
            throw $e;
        }
        $this->redirect('/items');
    }

    // Edit item
    public function actionEdit($id)
    {
        $model = UsedItem::findOne($id);
        $model->setScenario('edit');
        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
            if ($model->validate() && $model->save(false)) {
                Yii::$app->session->setFlash('item_edit_success', 'Item data has been updated.');
                $this->redirect('/items');
            }
        }
        return $this->render('edit', [
            'model'         => $model,
            'modelPhoto'    => new UsedItemPhoto(),
            'categories'    => (new Category())->prepareDropDown(),
            'typeData'      => (new UsedItemType())->prepareList()
        ]);
    }

    // Upload images for items (edit item page)
    public function actionUpload()
    {

        // @todo update validateUploadedFilesAndPassErrorsToFromModel and use it
        // @todo display validation errors

        $model = new UsedItemPhoto();

        if (Yii::$app->request->isPost) {
            $files = UploadedFile::getInstances($model, 'file');

            $itemId = Yii::$app->request->post('UsedItemPhoto')['item_id'];

//            HelperBase::dump($model->file);

//            if ($model->file && $model->validate()) {
            foreach ($files as $file) {
                $photoModel = new UsedItemPhoto();
                $photoModel->item_id = $itemId;
                $photoModel->file = $file;
                if ($photoModel->validate()) {
                    $photoModel->save(false);
                }
            }

            /*if ($model->file && $model->validate()) {

                foreach ($model->file as $file) {
//                    HelperBase::dump($file->baseName);
//                    $file->saveAs('uploads/' . $file->baseName . '.' . $file->extension);
                }
            }

            HelperBase::dump($model->getErrors());*/
        }<commit_msg>up MarketPlace controller<commit_after><?php

namespace app\controllers;

use Yii;
use yii\base\Exception;
use yii\helpers;
use app\components\HelperMarketPlace;
use app\components\HelperUser;
use app\models\UsedItem;
use app\models\UsedItemType;
use app\models\UsedItemPhoto;
use app\models\Category;
use app\models\PhpbbUser;
use yii\web\Controller;
use yii\filters\AccessControl;

use app\components\HelperBase;
use yii\web\UploadedFile;

class MarketplaceController extends Controller
{
    public $layout = 'marketplace';

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => ['create', 'items', 'delete', 'edit', 'upload'],
                        'roles' => ['@'],
                    ],
                    [
                        'allow' => true,
                        'actions' => ['index', 'view'],
                        'roles' => ['?', '@'],
                    ],
                ],
            ],
        ];
    }

    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    // Front page with catalog and search form
    public function actionIndex()
    {
        $model = new UsedItem();
        // Search request
        if (Yii::$app->request->get('UsedItem')) {
            $model->setScenario('search');
            // Search items according to received GET parameters
            $data = $model->search(Yii::$app->request->get());
        } else {
            // Get items with specified order
            $data = UsedItem::find()
                ->where('category_id > 0')
                ->orderBy(HelperMarketPlace::getSortParamForItemsList())
                ->all();
        }
        return $this->render('index', ['data' => $data, 'model' => $model]);
    }

    // Create item page
    public function actionCreate()
    {
        $model      = new UsedItem();
        $modelPhoto = new UsedItemPhoto();
        $model->setScenario('create');
        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
            $modelPhoto->validateUploadedFiles();
            if ($modelPhoto->hasErrors('file')) {
                $model->addError('file', $modelPhoto->getErrors('file')[0]);
            }
            $model->user_id = HelperUser::uid();
            if ($model->validate(null, false) && $model->save(false)) {
                if ($modelPhoto->hasUploadedFiles()) {
                    $modelPhoto->saveUploadedFileNames($model->id);
                }
                Yii::$app->session->setFlash('item_create_success', 'A new item has been added.');
                $this->redirect('/');
            }
        }
        return $this->render('create', [
            'model'         => $model,
            'modelPhoto'    => $modelPhoto,
            'categories'    => (new Category())->prepareDropDown(),
            'typeData'      => (new UsedItemType())->prepareList()
        ]);
    }

    // View item page
    public function actionView($id)
    {
        $item = UsedItem::find()->where('id = :id', [':id' => $id])->one();
        if (!$item) {
            $this->redirect('/');
        }
        return $this->render('view', ['data' => $item]);
    }

    // View items added by a current user
    public function actionItems()
    {
        // Only users who posted any items should have access
        if (!(new PhpbbUser)->hasItems(HelperUser::uid())) {
            $this->redirect('/');
        }
        $items = PhpbbUser::findUser(HelperUser::uid())->items;
        return $this->render('items', ['data' => $items]);
    }

    // Delete an item
    public function actionDelete($id)
    {
        $item = UsedItem::findOne($id);
        // Item must be in database
        // User can only delete items which have been added by himself
        if (empty($item) || $item->user_id != HelperUser::uid()) {
            $this->redirect('/');
            return false;
        }

        $transaction = Yii::$app->db->beginTransaction();
        try {
            if ($item->delete()) {
                Yii::$app->session->setFlash('item_delete_success', 'Item has been deleted.');
            } else {
                Yii::$app->session->setFlash('item_delete_error', 'Item could not be deleted.');
            }
            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollBack();
            throw $e;
        }
        $this->redirect('/items');
    }

    // Edit item
    public function actionEdit($id)
    {
        $model = UsedItem::findOne($id);
        $model->setScenario('edit');

        if (Yii::$app->session->hasFlash('edit_item_upload_photo_validation_error')) {
            $model->addError('file', Yii::$app->session->getFlash('edit_item_upload_photo_validation_error'));
        }

        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
            if ($model->validate() && $model->save(false)) {
                Yii::$app->session->setFlash('item_edit_success', 'Item data has been updated.');
                $this->redirect('/items');
            }
        }
        return $this->render('edit', [
            'model'         => $model,
            'modelPhoto'    => new UsedItemPhoto(),
            'categories'    => (new Category())->prepareDropDown(),
            'typeData'      => (new UsedItemType())->prepareList()
        ]);
    }

    // Upload images for items (edit item page)
    public function actionUpload()
    {
        // @todo add files limit
        // @todo Yii::app->session refactoring

        $model = new UsedItemPhoto();

        if (Yii::$app->request->isPost) {
            $modelPhoto = new UsedItemPhoto();
            $itemId = Yii::$app->request->post('UsedItemPhoto')['item_id'];
            $modelPhoto->validateUploadedFiles();
            $modelPhoto->item_id = $itemId;
            if ($modelPhoto->hasErrors('file')) {
                Yii::$app->session->setFlash('edit_item_upload_photo_validation_error', $modelPhoto->getErrors('file')[0]);
            } else {
                if ($modelPhoto->save(false)) {
                    Yii::$app->session->setFlash('edit_item_upload_photo_success', 'New images have been added');
                } else {
                    Yii::$app->session->setFlash('edit_item_upload_photo_error', 'New images could not be uploaded');
                }
            }

            $this->redirect('/item/edit/' . $itemId);
        } else {
            $this->redirect('/');
        }
{'fraction_non_alphanumeric': 0.16156513872135103, 'fraction_numerical': 0.00030156815440289503, 'mean_word_length': 1.750933222729158, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 224, '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': '233230', 'n_tokens_mistral': 3650, 'n_tokens_neox': 3568, 'n_words': 718}