/** * Display the result of a search * * @param $request Request * @return $users : users (not banned + teacher or more) matching * @return $instruments : instrument matching * @return $coursesDay : courses matching if the query is a day (numeric or text) * @return $coursesTitle : courses matching (title) */ public function search(Request $request) { $day = -1; $search = $request->search; $str = str_replace(' ', '_', $search); $users = []; if (!is_numeric($str)) { $users = User::where('level_id', '>', 2)->where('banned', 0)->where('slug', 'LIKE', '%' . $str . '%')->with(['courses' => function ($query) { $query->where('level', 1)->where('validated', 1); }])->get(); } $instruments = Instrument::where('name', 'LIKE', '%' . $search . '%')->with('courses')->get(); if (is_numeric($str) && 0 < $str && $str < 7) { $day = $str; $coursesDay = Course::where('day', $str)->where('active', 1)->get(); } else { $days = ['lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche']; for ($i = 0; $i < 7; $i++) { if (strtolower($str) == $days[$i]) { $day = $i; } } $coursesDay = Course::where('day', $day)->where('active', 1)->get(); } $coursesTitle = Course::where('active', 1)->where('name', 'LIKE', '%' . $str . '%')->get(); return view('courses.search', compact('users', 'instruments', 'coursesDay', 'coursesTitle', 'search', 'day')); }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update(Request $request) { $instrument = Instrument::where('id', $request->id)->first(); $name = $instrument->name; $instrument->update(['name' => $request->name]); Modification::create(['table' => 'instruments', 'user_id' => Auth::user()->id, 'message' => 'updated instrument "' . $name . '" to "' . $request->name . '".']); Flash::success('Instrument modifié avec succès !'); return redirect('admin/instruments'); }
protected function music_import_csv(Request $request) { $path = $request->path; $filename = $request->filename; $file = $path . '/' . $filename; $fp = fopen($file, 'r'); $i = 0; // 定义空数组,方便下面使用 $isUsed_arr = []; // 判断文件名是否有重复 while ($arr = fgetcsv($fp)) { $isFree = self::isNoUsedFileName(mb_convert_encoding($arr[7], 'UTF-8', 'GB2312')); // 将重复的文件名放入一个数组中 if (!$isFree) { $isUsed_arr[] = mb_convert_encoding($arr[7], 'UTF-8', 'GB2312'); } } // 如果存在重复的文件名,则返回 if (count($isUsed_arr) > 0) { $data['status'] = false; $data['errMsg'] = $isUsed_arr; return $data; } // 重置文件指针 rewind($fp); while ($arr = fgetcsv($fp)) { $i++; if ($i == 1) { continue; } // 方便后面判断读取了几行 $music = new Music(); $music->name = mb_convert_encoding($arr[0], 'UTF-8', 'GB2312'); // 查询乐曲id $instrument = mb_convert_encoding($arr[1], 'UTF-8', 'GB2312'); $instrument_in_db = Instrument::where('name', '=', $instrument)->first(); // 如果这个乐器不存在,则先创建 if (empty($instrument_in_db)) { $new_instrument = new Instrument(); $new_instrument->name = $instrument; $new_instrument->save(); $music->instrument_id = $new_instrument->id; } else { $music->instrument_id = $instrument_in_db->id; } // 作曲人 $music->composer = mb_convert_encoding($arr[2], 'UTF-8', 'GB2312'); // 版本 $music->version = mb_convert_encoding($arr[3], 'UTF-8', 'GB2312'); // 出版社 $press = mb_convert_encoding($arr[4], 'UTF-8', 'GB2312'); $press_in_db = Press::where('name', $press)->first(); // 如果这个出版社不存在,则先创建 if (empty($press_in_db)) { $new_press = new Press(); $new_press->name = $press; $new_press->save(); $music->press_id = $new_press->id; } else { $music->press_id = $press_in_db->id; } // 主办单位 $organizer = mb_convert_encoding($arr[5], 'UTF-8', 'GB2312'); $organizer_in_db = Organizer::where('name', $organizer)->first(); // 如果这个主办机构不存在,则先创建 if (empty($organizer_in_db)) { $new_organizer = new Organizer(); $new_organizer->name = $organizer; $new_organizer->save(); $music->organizer_id = $new_organizer->id; } else { $music->organizer_id = $organizer_in_db->id; } // 操作人 $music->operator = $request->user()->id; // 评论内容 $note_content = mb_convert_encoding($arr[6], 'UTF-8', 'GB2312'); if (!empty($note_content)) { $music->note_content = $note_content; $music->note_operator = $request->user()->id; } $music->filename = mb_convert_encoding($arr[7], 'UTF-8', 'GB2312'); $result[] = $music->save(); } if ($i == 0 || $i == 1 || in_array(false, $result)) { $data['status'] = false; $data['errMsg'] = '表格中没有有效数据'; } else { $data['status'] = true; } return $data; }