<commit_before><?php
namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use common\models\Geeks;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use common\models\GeekForm;
use yii\web\UploadedFile;
use common\models\User;
use common\models\Likes;
use yii\web\Response;
use common\models\Subscription;

/**
 * Geeks controller
 */
class GeeksController extends Controller
{


    public $layout = 'base';

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['index', 'geeks', 'create', 'view', 'feed', 'like'],
                'rules' => [
                    [
                        'actions' => ['create', 'index', 'geeks', 'view', 'feed', 'like'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                    [
                        'actions' => ['index', 'geeks', 'view', 'like'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                ],
            ],
        ];
    }

    public function beforeAction($action)
    {
        if ($action->id == 'like') {
            $this->enableCsrfValidation = false;
        }

        return parent::beforeAction($action);
    }

    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        return $this->redirect('geeks/all');
    }

    public function actionAll()
    {
        $geeks = new Geeks();
        $geeks = $geeks->find()->all();

        return $this->render('all',[
            'geeks' => $geeks
        ]);
    }

    public function actionView($id)
    {
        $geek = new Geeks();
        $geek = $geek->findOne($id);

        if ($geek === null) {
            throw new NotFoundHttpException;
        }

        return $this->render('view', [
            'geek' => $geek,
        ]);
    }

    public function actionCreate()
    {
        $model = new GeekForm();
        $text = Yii::$app->request->post('GeekForm')['text'];

        if ($model->load(Yii::$app->request->post())) {

            $model->imageFile = UploadedFile::getInstance($model, 'imageFile');

            $geek = new Geeks();

            if ($model->upload()) {
                $path = 'upload/' . Yii::$app->user->id;

                $geek->image = $path . '/original/' . $model->imageFile->baseName . '.' . $model->imageFile->extension;
                $geek->thumbnail = $path . '/thumbnail/' . $model->imageFile->baseName . '.' . $model->imageFile->extension;
            }


            $geek->user_id = Yii::$app->user->id;
            $geek->text = $text;

            if ($geek->save()) {
                $result = "Твит успешно опубликован";
                $alert_type = 'success';
            } else {
                $result = "Неудача! Попробуйте снова";
                $alert_type = 'error';
            }

            Yii::$app->session->setFlash($alert_type, $result);

            return $this->refresh();
        }

        return $this->render('create', [
            'model'  => $model,
        ]);
    }

    public function actionFeed()
    {
        $geeks = Geeks::find()->select(['geeks.*', 'user.username'])
        ->join('INNER JOIN', User::tableName(),'user.id = geeks.user_id')
        ->join('INNER JOIN', Subscription::tableName(), 'subscription.subscribe_id = user.id')
        ->where(['subscription.user_id' => Yii::$app->user->id])
        ->orWhere(['geeks.user_id' => Yii::$app->user->id])
        ->orderBy(['geeks.created_at' => SORT_DESC])
        ->all();

        return $this->render('all',[
            'geeks' => $geeks,
        ]);
    }

    public function actionLike()
    {

        if (Yii::$app->request->isAjax)
        {
            $like = new Likes();
            $geek_id = Yii::$app->request->post('id');

            if (Geeks::findOne($geek_id) === null) {
                throw new NotFoundHttpException;
            }

            if (Likes::isRelationExist(Yii::$app->user->id, $geek_id)){<commit_msg>refactor actionFeed and actionAll<commit_after><?php
namespace frontend\controllers;

use Yii;
use yii\db\Query;
use yii\web\Controller;
use common\models\Geeks;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use common\models\GeekForm;
use yii\web\UploadedFile;
use common\models\User;
use common\models\Likes;
use yii\web\Response;
use common\models\Subscription;

/**
 * Geeks controller
 */
class GeeksController extends Controller
{


    public $layout = 'base';

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['index', 'geeks', 'create', 'view', 'feed', 'like'],
                'rules' => [
                    [
                        'actions' => ['create', 'index', 'geeks', 'view', 'feed', 'like'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                    [
                        'actions' => ['index', 'geeks', 'view'],
                        'allow' => true,
                        'roles' => ['?'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                ],
            ],
        ];
    }

    public function beforeAction($action)
    {
        if ($action->id == 'like') {
            $this->enableCsrfValidation = false;
        }

        return parent::beforeAction($action);
    }

    /**
     * Displays homepage.
     *
     * @return mixed
     */
    public function actionIndex()
    {
        return $this->redirect('geeks/all');
    }

    public function actionAll()
    {
        $geeks = Geeks::find()
            ->select(['geeks.*', 'user.username'])
            ->join('INNER JOIN', User::tableName(),'user.id = geeks.user_id')
            ->all();

        if (Yii::$app->user->id){
            $query = new Query();
            $query = $query->select(['geek_id'])->from(Likes::tableName())->where(['user_id' => Yii::$app->user->id])->all();
            for ($i = 0; $i< count($query); $i++) {
                $likes[] = $query[$i]['geek_id'];
            }
        } else {
            $likes = [];
        }

        return $this->render('all',[
            'geeks' => $geeks,
            'likes' => $likes
        ]);
    }

    public function actionView($id)
    {
        $geek = new Geeks();
        $geek = $geek->findOne($id);

        if ($geek === null) {
            throw new NotFoundHttpException;
        }

        return $this->render('view', [
            'geek' => $geek,
        ]);
    }

    public function actionCreate()
    {
        $model = new GeekForm();
        $text = Yii::$app->request->post('GeekForm')['text'];

        if ($model->load(Yii::$app->request->post())) {

            $model->imageFile = UploadedFile::getInstance($model, 'imageFile');

            $geek = new Geeks();

            if ($model->upload()) {
                $path = 'upload/' . Yii::$app->user->id;

                $geek->image = $path . '/original/' . $model->imageFile->baseName . '.' . $model->imageFile->extension;
                $geek->thumbnail = $path . '/thumbnail/' . $model->imageFile->baseName . '.' . $model->imageFile->extension;
            }


            $geek->user_id = Yii::$app->user->id;
            $geek->text = $text;

            if ($geek->save()) {
                $result = "Твит успешно опубликован";
                $alert_type = 'success';
            } else {
                $result = "Неудача! Попробуйте снова";
                $alert_type = 'error';
            }

            Yii::$app->session->setFlash($alert_type, $result);

            return $this->refresh();
        }

        return $this->render('create', [
            'model'  => $model,
        ]);
    }

    public function actionFeed()
    {
        // Find geeks of users on which we subscribed
        $geeks = Geeks::find()->select(['geeks.*', 'user.username'])
        ->join('INNER JOIN', User::tableName(),'user.id = geeks.user_id')
        ->join('INNER JOIN', Subscription::tableName(), 'subscription.subscribe_id = user.id')
        ->where(['subscription.user_id' => Yii::$app->user->id])
        ->orWhere(['geeks.user_id' => Yii::$app->user->id])
        ->orderBy(['geeks.created_at' => SORT_DESC])
        ->all();

        // Find geeks that we liked
        $query = new Query();
        $query = $query->select(['geek_id'])->from(Likes::tableName())->where(['user_id' => Yii::$app->user->id])->all();
        for ($i = 0; $i< count($query); $i++) {
            $likes[] = $query[$i]['geek_id'];
        }

        return $this->render('all',[
            'geeks' => $geeks,
            'likes' => $likes
        ]);
    }

    public function actionLike()
    {

        if (Yii::$app->request->isAjax)
        {
            $like = new Likes();
            $geek_id = Yii::$app->request->post('id');

            if (Geeks::findOne($geek_id) === null) {
                throw new NotFoundHttpException;
            }

            if (Likes::isRelationExist(Yii::$app->user->id, $geek_id)){
{'fraction_non_alphanumeric': 0.18005828476269775, 'fraction_numerical': 0.00020815986677768527, 'mean_word_length': 1.6326027397260274, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 170, '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': '2752500', 'n_tokens_mistral': 2952, 'n_tokens_neox': 2889, 'n_words': 508}