<commit_before><?php

namespace Kregel\Warden\Http\Controllers;

use Illuminate\Database\Eloquent\Model;
use Input;
use Kregel\FormModel\FormModel;
use Route;
use Session;

class ModelController extends Controller
{

    /**
     * @param string $model_name A key in the warden.models configuration
     *
     * @return view
     */
    protected function getModelList($model_name)
    {
        /*
         * Here we need to make sure that we have all of the fields that we want to
         * have shown, if the visible field is not currently set we should grab
         * the fillable fields so we can populate the table on the view.
         */
        $model = $this->findModel($model_name, null);

        /*
         * We need to get the fields for the model, so use the model we newed up
         * above and grab the Visible field first, if it's not set then grab
         * the fillable fields.
         */
        $field_names = !empty($model->getVisible()) ?
            $model->getVisible() :
            $model->getFillable();
        // Paginate the results to plan for lots of items within the database.
        $desired_objects = $model::paginate(50);

        // Inject a quick form for deleting the objects.

        return view('warden::view-models')
            ->with('field_names', ($field_names))
            ->with('models', $desired_objects)
            ->with('model_name', $model_name);
    }

    /**
     * @param string $model_name Must be a valid class
     * @param int    $id         ID of the desired class
     *
     * @throws \Exception
     *
     * @return Model A model defined by ID
     */
    private function findModel($model_name, $id = null)
    {
        $model = config('kregel.warden.models.'.$model_name.'.model');
        if (empty($model)) {
            throw new \Exception('There is no model by the name {'.$model_name.'}');
        }
        if (empty($id) | !is_numeric($id)) {
            return new $model();
        }

        return $model::findOrFail($id);
    }

    /**
     * @param string    $model_name A key in the warden.models configuration
     * @param int       $id         The id of the model
     * @param FormModel $form       An injected model
     *
     * @return mixed
     */
    protected function getModel($model_name, $id, FormModel $form)
    {
        /*
       * We need to grab the model from the config and select one entry for
       * that model from within the database.
       */
        $model = $this->findModel($model_name, $id)->getOriginalContent();
        /*
         * Here we generate the form to update the model using the kregel/formmodel
         * package
         */
        $form = $form->using(config('kregel.formmodel.using.framework'))
                            ->withModel($model)
                            ->submitTo(route('warden::api.update-model', [$model_name, $model->id]));
        $form_info = $form->form([
            'method'  => 'put',
            'enctype' => 'multipart/form-data',
        ]);

        return view('warden::view-model')
                ->with('form', $form_info)
                ->with('model_name', $model_name)
                ->with('vue_components', $form->vue_components);
    }<commit_msg>Update ModelController.php<commit_after><?php

namespace Kregel\Warden\Http\Controllers;

use Illuminate\Database\Eloquent\Model;
use Input;
use Kregel\FormModel\FormModel;
use Route;
use Session;

class ModelController extends Controller
{

    /**
     * @param string $model_name A key in the warden.models configuration
     *
     * @return view
     */
    protected function getModelList($model_name)
    {
        /*
         * Here we need to make sure that we have all of the fields that we want to
         * have shown, if the visible field is not currently set we should grab
         * the fillable fields so we can populate the table on the view.
         */
        $model = $this->findModel($model_name, null);

        /*
         * We need to get the fields for the model, so use the model we newed up
         * above and grab the Visible field first, if it's not set then grab
         * the fillable fields.
         */
        $field_names = !empty($model->getVisible()) ?
            $model->getVisible() :
            $model->getFillable();
        // Paginate the results to plan for lots of items within the database.
        $desired_objects = $model::paginate(50);

        // Inject a quick form for deleting the objects.

        return view('warden::view-models')
            ->with('field_names', ($field_names))
            ->with('models', $desired_objects)
            ->with('model_name', $model_name);
    }

    /**
     * @param string $model_name Must be a valid class
     * @param int    $id         ID of the desired class
     *
     * @throws \Exception
     *
     * @return Model A model defined by ID
     */
    private function findModel($model_name, $id = null)
    {
        $model = config('kregel.warden.models.'.$model_name.'.model');
        if (empty($model)) {
            throw new \Exception('There is no model by the name {'.$model_name.'}');
        }
        if (empty($id) | !is_numeric($id)) {
            return new $model();
        }

        return $model::findOrFail($id);
    }

    /**
     * @param string    $model_name A key in the warden.models configuration
     * @param int       $id         The id of the model
     * @param FormModel $form       An injected model
     *
     * @return mixed
     */
    protected function getModel($model_name, $id, FormModel $form)
    {
        /*
       * We need to grab the model from the config and select one entry for
       * that model from within the database.
       */
        $model = $this->findModel($model_name, $id);
        /*
         * Here we generate the form to update the model using the kregel/formmodel
         * package
         */
        $form = $form->using(config('kregel.formmodel.using.framework'))
                            ->withModel($model)
                            ->submitTo(route('warden::api.update-model', [$model_name, $model->id]));
        $form_info = $form->form([
            'method'  => 'put',
            'enctype' => 'multipart/form-data',
        ]);

        return view('warden::view-model')
                ->with('form', $form_info)
                ->with('model_name', $model_name)
                ->with('vue_components', $form->vue_components);
    }
{'fraction_non_alphanumeric': 0.1166023166023166, 'fraction_numerical': 0.0006177606177606178, 'mean_word_length': 1.9543795620437956, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 42, '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': '3099669', 'n_tokens_mistral': 1825, 'n_tokens_neox': 1795, 'n_words': 563}