<commit_before><?php
namespace App\Http\Controllers;

use App\Helpers\Helper;
use App\Models\Category as Category;
use App\Models\Company;
use App\Models\Setting;
use Auth;
use DB;
use Input;
use Lang;
use Redirect;
use Str;
use View;

/**
 * This class controls all actions related to Categories for
 * the Snipe-IT Asset Management application.
 *
 * @version    v1.0
 * @author [<NAME>] [<<EMAIL>>]
 */
class CategoriesController extends Controller
{

    /**
    * Returns a view that invokes the ajax tables which actually contains
    * the content for the categories listing, which is generated in getDatatable.
    *
    * @author [<NAME>] [<<EMAIL>>]
    * @see CategoriesController::getDatatable() method that generates the JSON response
    * @since [v1.0]
    * @return View
    */
    public function getIndex()
    {
        // Show the page
        return View::make('categories/index');
    }


    /**
    * Returns a form view to create a new category.
    *
    * @author [<NAME>] [<<EMAIL>>]
    * @see CategoriesController::postCreate() method that stores the data
    * @since [v1.0]
    * @return View
    */
    public function getCreate()
    {
        // Show the page
         $category_types= Helper::categoryTypeList();
        return View::make('categories/edit')->with('category', new Category)
        ->with('category_types', $category_types);
    }


    /**
    * Validates and stores the new category data.
    *
    * @author [<NAME>] [<<EMAIL>>]
    * @see CategoriesController::getCreate() method that makes the form.
    * @since [v1.0]
    * @return Redirect
    */
    public function postCreate()
    {

        // create a new model instance
        $category = new Category();

        // Update the category data
        $category->name                 = e(Input::get('name'));
        $category->category_type        = e(Input::get('category_type'));
        $category->eula_text            = e(Input::get('eula_text'));
        $category->use_default_eula     = e(Input::get('use_default_eula', '0'));
        $category->require_acceptance   = e(Input::get('require_acceptance', '0'));
        $category->checkin_email        = e(Input::get('checkin_email', '0'));
        $category->user_id              = Auth::user()->id;

        if ($category->save()) {
        // Redirect to the new category  page
            return redirect()->to("admin/settings/categories")->with('success', trans('admin/categories/message.create.success'));
        } else {

          // The given data did not pass validation
            return redirect()->back()->withInput()->withErrors($category->getErrors());

        }

        // Redirect to the category create page
        return redirect()->to('admin/settings/categories/create')->with('error', trans('admin/categories/message.create.error'));


    }

    /**
    * Returns a view that makes a form to update a category.
    *
    * @author [<NAME>] [<<EMAIL>>]
    * @see CategoriesController::postEdit() method saves the data
    * @param int $categoryId
    * @since [v1.0]
    * @return View
    */
    public function getEdit($categoryId = null)
    {
        // Check if the category exists
        if (is_null($category = Category::find($categoryId))) {
            // Redirect to the blogs management page
            return redirect()->to('admin/settings/categories')->with('error', trans('admin/categories/message.does_not_exist'));
        }

        // Show the page
        //$category_options = array('' => 'Top Level') + Category::lists('name', 'id');

        $category_options = array('' => 'Top Level') + DB::table('categories')->where('id', '!=', $categoryId)->lists('name', 'id');
        $category_types= Helper::categoryTypeList();

        return View::make('categories/edit', compact('category'))
        ->with('category_options', $category_options)
        ->with('category_types', $category_types);
    }


    /**
    * Validates and stores the updated category data.
    *
    * @author [<NAME>] [<<EMAIL>>]
    * @see CategoriesController::getEdit() method that makes the form.
    * @param int $categoryId
    * @since [v1.0]
    * @return Redirect
    */
    public function postEdit($categoryId = null)
    {
        // Check if the blog post exists
        if (is_null($category = Category::find($categoryId))) {
            // Redirect to the blogs management page
            return redirect()->to('admin/categories')->with('error', trans('admin/categories/message.does_not_exist'));
        }

        // Update the category data
        $category->name            = e(Input::get('name'));
        $category->category_type        = e(Input::get('category_type'));
        $category->eula_text            = e(Input::get('eula_text'));
        $category->use_default_eula     = e(Input::get('use_default_eula', '0'));
        $category->require_acceptance   = e(Input::get('require_acceptance', '0'));
        $category->checkin_email        = e(Input::get('checkin_email', '0'));

        if ($category->save()) {
        // Redirect to the new category page
            return redirect()->to("admin/settings/categories")->with('success', trans('admin/categories/message.update.success'));
        } // attempt validation
        else {
          // The given data did not pass validation
            return redirect()->back()->withInput()->withErrors($category->getErrors());
        }

        // Redirect to the category management page
        return redirect()->back()->with('error', trans('admin/categories/message.update.error'));

    }

    /**
    * Validates and marks a category as deleted.
    *
    * @author [<NAME>] [<<EMAIL>>]
    * @since [v1.0]
    * @param int $categoryId
    * @return Redirect
    */
    public function getDelete($categoryId)
    {
        // Check if the category exists
        if (is_null($category = Category::find($categoryId))) {<commit_msg>Only set the category_type in postEdit if the category is empty. (#2337)<commit_after><?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Helpers\Helper;
use App\Models\Category as Category;
use App\Models\Company;
use App\Models\Setting;
use Auth;
use DB;
use Input;
use Lang;
use Redirect;
use Str;
use View;

/**
 * This class controls all actions related to Categories for
 * the Snipe-IT Asset Management application.
 *
 * @version    v1.0
 * @author [<NAME>] [<<EMAIL>>]
 */
class CategoriesController extends Controller
{

    /**
    * Returns a view that invokes the ajax tables which actually contains
    * the content for the categories listing, which is generated in getDatatable.
    *
    * @author [<NAME>] [<<EMAIL>>]
    * @see CategoriesController::getDatatable() method that generates the JSON response
    * @since [v1.0]
    * @return View
    */
    public function getIndex()
    {
        // Show the page
        return View::make('categories/index');
    }


    /**
    * Returns a form view to create a new category.
    *
    * @author [<NAME>] [<<EMAIL>>]
    * @see CategoriesController::postCreate() method that stores the data
    * @since [v1.0]
    * @return View
    */
    public function getCreate()
    {
        // Show the page
         $category_types= Helper::categoryTypeList();
        return View::make('categories/edit')->with('category', new Category)
        ->with('category_types', $category_types);
    }


    /**
    * Validates and stores the new category data.
    *
    * @author [<NAME>] [<<EMAIL>>]
    * @see CategoriesController::getCreate() method that makes the form.
    * @since [v1.0]
    * @return Redirect
    */
    public function postCreate()
    {

        // create a new model instance
        $category = new Category();

        // Update the category data
        $category->name                 = e(Input::get('name'));
        $category->category_type        = e(Input::get('category_type'));
        $category->eula_text            = e(Input::get('eula_text'));
        $category->use_default_eula     = e(Input::get('use_default_eula', '0'));
        $category->require_acceptance   = e(Input::get('require_acceptance', '0'));
        $category->checkin_email        = e(Input::get('checkin_email', '0'));
        $category->user_id              = Auth::user()->id;

        if ($category->save()) {
        // Redirect to the new category  page
            return redirect()->to("admin/settings/categories")->with('success', trans('admin/categories/message.create.success'));
        } else {

          // The given data did not pass validation
            return redirect()->back()->withInput()->withErrors($category->getErrors());

        }

        // Redirect to the category create page
        return redirect()->to('admin/settings/categories/create')->with('error', trans('admin/categories/message.create.error'));


    }

    /**
    * Returns a view that makes a form to update a category.
    *
    * @author [<NAME>] [<<EMAIL>>]
    * @see CategoriesController::postEdit() method saves the data
    * @param int $categoryId
    * @since [v1.0]
    * @return View
    */
    public function getEdit($categoryId = null)
    {
        // Check if the category exists
        if (is_null($category = Category::find($categoryId))) {
            // Redirect to the blogs management page
            return redirect()->to('admin/settings/categories')->with('error', trans('admin/categories/message.does_not_exist'));
        }

        // Show the page
        //$category_options = array('' => 'Top Level') + Category::lists('name', 'id');

        $category_options = array('' => 'Top Level') + DB::table('categories')->where('id', '!=', $categoryId)->lists('name', 'id');
        $category_types= Helper::categoryTypeList();

        return View::make('categories/edit', compact('category'))
        ->with('category_options', $category_options)
        ->with('category_types', $category_types);
    }


    /**
    * Validates and stores the updated category data.
    *
    * @author [<NAME>] [<<EMAIL>>]
    * @see CategoriesController::getEdit() method that makes the form.
    * @param int $categoryId
    * @since [v1.0]
    * @return Redirect
    */
    public function postEdit(Request $request, $categoryId = null)
    {
        // Check if the blog post exists
        if (is_null($category = Category::find($categoryId))) {
            // Redirect to the blogs management page
            return redirect()->to('admin/categories')->with('error', trans('admin/categories/message.does_not_exist'));
        }

        // Update the category data
        $category->name            = e($request->input('name'));
        // If the item count is > 0, we disable the category type in the edit. Disabled items
        // don't POST, so if the category_type is blank we just set it to the default.
        $category->category_type        = e($request->input('category_type', $category->category_type));
        $category->eula_text            = e($request->input('eula_text'));
        $category->use_default_eula     = e($request->input('use_default_eula', '0'));
        $category->require_acceptance   = e($request->input('require_acceptance', '0'));
        $category->checkin_email        = e($request->input('checkin_email', '0'));

        if ($category->save()) {
        // Redirect to the new category page
            return redirect()->to("admin/settings/categories")->with('success', trans('admin/categories/message.update.success'));
        } // attempt validation
        else {
          // The given data did not pass validation
            return redirect()->back()->withInput()->withErrors($category->getErrors());
        }

        // Redirect to the category management page
        return redirect()->back()->with('error', trans('admin/categories/message.update.error'));

    }

    /**
    * Validates and marks a category as deleted.
    *
    * @author [<NAME>] [<<EMAIL>>]
    * @since [v1.0]
    * @param int $categoryId
    * @return Redirect
    */
    public function getDelete($categoryId)
    {
        // Check if the category exists
        if (is_null($category = Category::find($categoryId))) {
{'fraction_non_alphanumeric': 0.1501602695816553, 'fraction_numerical': 0.003698528807429933, 'mean_word_length': 2.6398444510918337, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 141, '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': '6122897', 'n_tokens_mistral': 3411, 'n_tokens_neox': 3329, 'n_words': 873}