Exemplo n.º 1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Requests\MaterialRequest $request)
 {
     //
     $data = $request->except(['_token']);
     Material::create($data);
     return redirect()->to('material');
 }
Exemplo n.º 2
0
 public function update($id, MaterialRequest $request)
 {
     $material = Material::findOrFail($id);
     $material->name = $request->input('name');
     $material->type = $request->input('type');
     $material->status = $request->input('status');
     $material->updated_by = Auth::user()->id;
     $material->updated_at = time();
     $material->update();
     Session()->flash('flash_message', 'Material has been updated!');
     return redirect('materials');
 }
 public function update(MaterialRequest $request, Material $material)
 {
     // Update material
     if (isset($request->title)) {
         $material->title = ucwords($request->title);
         $material->slug = str_slug($request->title, '-');
     }
     if (isset($request->objective)) {
         $material->objective = ucfirst(strtolower($request->objective));
     }
     if (isset($request->target_language)) {
         $material->target_language = ucfirst(strtolower($request->target_language));
     }
     if (isset($request->time_needed_prep)) {
         $material->time_needed_prep = $request->time_needed_prep;
     }
     if (isset($request->time_needed_class)) {
         $material->time_needed_class = $request->time_needed_class;
     }
     if (isset($request->materials)) {
         $material->materials = ucfirst(strtolower($request->materials));
     }
     if (isset($request->procedure_before)) {
         $material->procedure_before = ucfirst($request->procedure_before);
     }
     if (isset($request->procedure_in)) {
         $material->procedure_in = ucfirst($request->procedure_in);
     }
     // store files
     $dir = Auth::user()->username;
     $path = public_path() . '/users/' . $dir;
     $empty = count(glob("{$path}/*")) === 0 ? true : false;
     if (!$empty) {
         $files = [];
         foreach (glob("{$path}/*") as $filename) {
             array_push($files, $filename);
         }
         MaterialFile::store($files, $material);
     }
     if (isset($request->category_list)) {
         // add category to categories table
         $categoryIds = [];
         //find or create new categories on material_category table
         foreach ($request->Input('category_list') as $category) {
             $material_category = Auth::user()->material_category()->firstOrCreate(['category' => title_case($category), 'slug' => str_slug($category, '-')]);
             array_push($categoryIds, $material_category->id);
             // update category to materials pivot table category / material
         }
         $material->categories()->sync($categoryIds);
     }
     if (isset($request->level)) {
         $levelIds = [];
         foreach ($request->Input('level') as $level) {
             $material_level = MaterialLevel::firstOrCreate(['level' => title_case($level), 'slug' => str_slug($level, '-')]);
             array_push($levelIds, $material_level->id);
         }
         // update level on materials pivot
         $material->levels()->sync($levelIds);
     }
     if (isset($request->language_focus)) {
         //add language focuses to material_language_focuses database
         $language_focusIds = [];
         foreach ($request->language_focus as $lf) {
             $material_language_focus = MaterialLanguageFocus::firstOrCreate(['language_focus' => title_case($lf), 'slug' => str_slug($lf, '-')]);
             array_push($language_focusIds, $material_language_focus->id);
         }
         // update language focus to materials pivot
         $material->languageFocuses()->sync($language_focusIds);
     }
     if (isset($request->activity_use)) {
         //add activity use to material_activity_uses database
         $activity_useIds = [];
         foreach ($request->activity_use as $au) {
             $material_activity_use = MaterialActivityUse::firstOrCreate(['activity_use' => title_case($au), 'slug' => str_slug($au, '-')]);
             array_push($activity_useIds, $material_activity_use->id);
         }
         // update activity use to materials pivot
         $material->activityUses()->sync($activity_useIds);
     }
     if (isset($request->pupil_task)) {
         //add pupil task to material_pupil_tasks database
         $pupil_taskIds = [];
         foreach ($request->pupil_task as $pt) {
             $material_pupil_task = MaterialPupilTask::firstOrCreate(['pupil_task' => title_case($pt), 'slug' => str_slug($pt, '-')]);
             array_push($pupil_taskIds, $material_pupil_task->id);
         }
         // add pupil task use to materials pivot
         $material->pupilTasks()->sync($pupil_taskIds);
     }
     if (isset($request->book)) {
         $book = MaterialBook::firstOrCreate(['book' => $request->book[0], 'slug' => str_slug($request->book[0], '-')]);
         $material->book_id = $book->id;
     }
     if (isset($request->page)) {
         $material->page = $request->page;
     }
     if (isset($request->follow_up)) {
         //update material
         $material->follow_up = ucfirst($request->follow_up);
     }
     if (isset($request->variations)) {
         $material->variations = ucfirst($request->variations);
     }
     if (isset($request->tips)) {
         $material->tips = ucfirst($request->tips);
     }
     if (isset($request->notes)) {
         $material->notes = ucfirst($request->notes);
     }
     $material->update();
     return redirect()->action('Admin\\UserController@home');
 }