<commit_before><?php

use RT\Core\FlashMessage;
use App\Models\Term;
use App\Models\Tag;
use App\Models\Language;
use RT\Services\ITermService;
use RT\Services\ILanguageService;

class TermController extends BaseController {

    private $termService;
    private $languageService;

    public function __construct(ITermService $termService, RT\Services\ILanguageService $languageService) {
        $this->termService = $termService;
        $this->languageService = $languageService;
        
        View::share('currentController', 'Term');
    }

    public function index() {
        $languages = $this->languageService->findAll();

        return View::make('terms.index')
                        ->with('languages', $languages)
        ;
    }

    public function postIndex() {
        $filter = Input::get('filter');
        $perPage = Input::get('perPage');
        $currentPage = Input::get('page');
        $sort = Input::get('sort');
        $sortDir = Input::get('sortDir');
        $result = $this->termService->findAllByFilter($filter, $perPage, $currentPage, $sort, $sortDir);

        $tarray = array();

        foreach ($result['terms'] as $t) {
            $source = empty($t->collectionNo) ? "" : $t->collectionNo . ". ";
            $source .= empty($t->collectionName) ? "" : $t->collectionName . ": ";
            $source .= $t->title;

            array_push($tarray, array(
                'id' => $t->id,
                'language' => $t->language_name,
                'code' => $t->language_code,
                'basePhrase' => $t->basePhrase,
                'definition' => $t->definition,
                'state' => $t->state,
                'phrase' => $t->phrase,
                'sentence' => $t->sentence,
                'added' => \RT\Core\HumanTime::toReadable($t->created_at),
                'updated' => \RT\Core\HumanTime::toReadable($t->updated_at),
                'source' => $source,
                'text_id' => $t->text_id,
                'l1_id' => $t->l1_id,
                'l2_id' => $t->l2_id,
                'group_id' => $t->group_id
            ));
        }

        $pages = ceil($result['count'] / $perPage);

        $content = View::make('terms.grid')
                ->with('terms', $tarray)
                ->with('currentPage', $currentPage)
                ->with('pages', $pages)
                ->with('count', $result['count'])
                ->__toString()
        ;

        return $content;
    }

    public function edit($id) {
        $term = Term::find($id);

        return View::make('terms.edit')
                        ->with('term', $term)
        ;
    }

    public function postEdit($id) {
        $returnTags = array();

        $tags = explode(',', Input::get('tags'));
        $term = Term::find($id);

        $term->basePhrase = trim(Input::get('basePhrase'));
        $term->definition = trim(Input::get('definition'));
        $term->sentence = Input::get('sentence');
        $term->state = strtolower(Input::get('state'));
        $term->Tags()->detach();

        foreach ($tags as $tag) {
            $tag = trim(mb_strtolower($tag));

            if (empty($tag) || $tag === '') {
                continue;
            }

            if (in_array($tag, $returnTags)) {
                continue;
            }

            $exists = Tag::where('tag', '=', $tag)->first();

            if ($exists == null) {
                $t = new Tag;
                $t->tag = $tag;
                $t->save();

                $term->Tags()->attach($t->id);
            } else {
                $term->Tags()->attach($exists->id);
            }

            array_push($returnTags, $tag);
        }

        $term->save();

        Session::flash(FlashMessage::MSG, new FlashMessage('Your term has been updated.'));

        return Redirect::action('TermController@edit', array('id' => $id));
    }

    public function postDelete($id) {
        $this->termService->delete($id);
        Session::flash(FlashMessage::MSG, new FlashMessage('Your term has been deleted.'));
        return Redirect::action('TermController@index');
    }

    public function exportTerms($id) {
        $termService = App::make('RT\Services\TermService');
        $languageService = App::make('RT\Services\LanguageService');

        $languages = $languageService->findAll();

        $larray = array();
        foreach ($languages as $l) {<commit_msg>Change term post messages to success colour<commit_after><?php

use RT\Core\FlashMessage;
use App\Models\Term;
use App\Models\Tag;
use App\Models\Language;
use RT\Services\ITermService;
use RT\Services\ILanguageService;

class TermController extends BaseController {

    private $termService;
    private $languageService;

    public function __construct(ITermService $termService, RT\Services\ILanguageService $languageService) {
        $this->termService = $termService;
        $this->languageService = $languageService;
        
        View::share('currentController', 'Term');
    }

    public function index() {
        $languages = $this->languageService->findAll();

        return View::make('terms.index')
                        ->with('languages', $languages)
        ;
    }

    public function postIndex() {
        $filter = Input::get('filter');
        $perPage = Input::get('perPage');
        $currentPage = Input::get('page');
        $sort = Input::get('sort');
        $sortDir = Input::get('sortDir');
        $result = $this->termService->findAllByFilter($filter, $perPage, $currentPage, $sort, $sortDir);

        $tarray = array();

        foreach ($result['terms'] as $t) {
            $source = empty($t->collectionNo) ? "" : $t->collectionNo . ". ";
            $source .= empty($t->collectionName) ? "" : $t->collectionName . ": ";
            $source .= $t->title;

            array_push($tarray, array(
                'id' => $t->id,
                'language' => $t->language_name,
                'code' => $t->language_code,
                'basePhrase' => $t->basePhrase,
                'definition' => $t->definition,
                'state' => $t->state,
                'phrase' => $t->phrase,
                'sentence' => $t->sentence,
                'added' => \RT\Core\HumanTime::toReadable($t->created_at),
                'updated' => \RT\Core\HumanTime::toReadable($t->updated_at),
                'source' => $source,
                'text_id' => $t->text_id,
                'l1_id' => $t->l1_id,
                'l2_id' => $t->l2_id,
                'group_id' => $t->group_id
            ));
        }

        $pages = ceil($result['count'] / $perPage);

        $content = View::make('terms.grid')
                ->with('terms', $tarray)
                ->with('currentPage', $currentPage)
                ->with('pages', $pages)
                ->with('count', $result['count'])
                ->__toString()
        ;

        return $content;
    }

    public function edit($id) {
        $term = Term::find($id);

        return View::make('terms.edit')
                        ->with('term', $term)
        ;
    }

    public function postEdit($id) {
        $returnTags = array();

        $tags = explode(',', Input::get('tags'));
        $term = Term::find($id);

        $term->basePhrase = trim(Input::get('basePhrase'));
        $term->definition = trim(Input::get('definition'));
        $term->sentence = Input::get('sentence');
        $term->state = strtolower(Input::get('state'));
        $term->Tags()->detach();

        foreach ($tags as $tag) {
            $tag = trim(mb_strtolower($tag));

            if (empty($tag) || $tag === '') {
                continue;
            }

            if (in_array($tag, $returnTags)) {
                continue;
            }

            $exists = Tag::where('tag', '=', $tag)->first();

            if ($exists == null) {
                $t = new Tag;
                $t->tag = $tag;
                $t->save();

                $term->Tags()->attach($t->id);
            } else {
                $term->Tags()->attach($exists->id);
            }

            array_push($returnTags, $tag);
        }

        $term->save();

        Session::flash(FlashMessage::MSG, new FlashMessage('Your term has been updated.', FlashMessage::SUCCESS));

        return Redirect::action('TermController@edit', array('id' => $id));
    }

    public function postDelete($id) {
        $this->termService->delete($id);
        Session::flash(FlashMessage::MSG, new FlashMessage('Your term has been deleted.', FlashMessage::SUCCESS));
        return Redirect::action('TermController@index');
    }

    public function exportTerms($id) {
        $termService = App::make('RT\Services\TermService');
        $languageService = App::make('RT\Services\LanguageService');

        $languages = $languageService->findAll();

        $larray = array();
        foreach ($languages as $l) {
{'fraction_non_alphanumeric': 0.17358152052029602, 'fraction_numerical': 0.0008970621215519175, 'mean_word_length': 1.9484297520661158, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 137, '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': '7537809', 'n_tokens_mistral': 2635, 'n_tokens_neox': 2486, 'n_words': 452}