/** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { if ($request->has('lang') && Languages::has($request->input('lang'))) { // 1. Check URL parameter $locale = $request->input('lang'); App::setLocale($locale); // Logged-in users: save to database if (Auth::check()) { $user = Auth::user(); $user->locale = $locale; $user->save(); } Cookie::queue('locale', $locale, 24 * 60); } elseif (Auth::check() && Languages::has(Auth::user()->locale)) { // 2. Check database for logged in users App::setLocale(Auth::user()->locale); } elseif (Languages::has($request->cookie('locale'))) { // 3. Check cookies App::setLocale($request->cookie('locale')); } elseif (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { // 4. Check browser languages. Note that Googlebot do not have this. $accept_languages = explode(',', $_SERVER['HTTP_ACCEPT_LANGUAGE']); foreach ($accept_languages as $lang) { $lang = locale_accept_from_http($lang); $locale = locale_lookup(Languages::all(), $lang, true, 'en'); if (!empty($locale)) { App::setLocale($locale); break; } } } setlocale(LC_TIME, Languages::withRegion(App::getLocale()) . '.utf8'); return $next($request); }
/** * Plain text of content with 200 character length. * * @return string */ public function excerpt($field, $locale = null, $length = 200) { if (!Languages::has($locale)) { $locale = App::getLocale(); } $translation = $this->translations()->where('locale', $locale)->first(); if (count($translation) && $translation->hasHtml($field)) { return $translation->excerpt($field, $length); } elseif ($locale === 'en') { return ''; } else { return $this->excerpt($field, 'en'); // Fallback } }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $tag = Tag::find($id); if (empty($tag)) { abort(404); } if ($request->user()->cannot('edit-tag', $tag)) { abort(403); } if ($request->input('lock')) { if ($tag->lock()) { return; // 200 OK } else { abort(423); // 423 Locked } } $this->validate($request, ['level' => 'integer|between:0,255', 'image_id' => 'integer|exists:image,id', 'translations' => 'array', 'translations.*.name' => 'string|max:255', 'translations.*.alias' => 'string|max:255', 'translations.*.description' => 'string|max:255']); $tag->update($request->only(['level', 'image_id'])); if ($request->has('translations') && is_array($request->input('translations'))) { foreach ($request->input('translations') as $locale => $texts) { if (!Languages::has($locale)) { continue; } $translation = TagTranslation::firstOrCreate(['tag_id' => $id, 'locale' => $locale]); $translation->update(array_only($texts, ['name', 'alias', 'description'])); } } }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $place = Place::find($id); if (empty($place)) { abort(404); } if ($request->user()->cannot('edit-place', $place)) { abort(403); } if ($request->input('lock')) { if ($place->lock()) { return; // 200 OK } else { abort(423); // 423 Locked } } // Validate data $this->validate($request, ['type' => 'string|in:' . implode(',', Place::types()), 'image_id' => 'integer|exists:image,id', 'gallery_image_ids.*' => 'integer|exists:image,id', 'city_id' => 'integer|exists:city,id', 'address' => 'string|max:255', 'latitude' => 'numeric', 'longitude' => 'numeric', 'tag_ids.*' => 'integer|exists:tag,id', 'email' => 'email|max:255', 'phone' => 'string|max:255', 'website' => 'url|max:255', 'facebook' => 'url|max:255', 'translations.*.name' => 'string|max:255', 'translations.*.content' => 'string']); if ($request->has('user_id')) { if ($request->user()->can('transfer-place', $place)) { $place->transfer($request->input('user_id')); } else { abort(403); } } $place->fill($request->all()); $place->save(); if ($request->has('translations')) { foreach ($request->input('translations') as $locale => $texts) { if (!Languages::has($locale)) { continue; } $translation = PlaceTranslation::firstOrCreate(['place_id' => $id, 'locale' => $locale]); $translation->update(app_array_filter($texts, ['name', 'content'])); } } }
/** * Import data from geonames.org */ public function import() { $username = env('GEONAMES_USERNAME', 'demo'); $json = file_get_contents("http://api.geonames.org/getJSON?geonameId={$this->geoname_id}&username={$username}"); $data = json_decode($json); $names = []; foreach ($data->alternateNames as $name_pair) { if (isset($name_pair->lang) && Languages::has($name_pair->lang)) { if (!empty($name_pair->isPreferredName) || empty($names[$name_pair->lang])) { $names[$name_pair->lang] = $name_pair->name; } } } foreach ($names as $locale => $name) { $translation = CountryTranslation::firstOrNew(['country_id' => $this->id, 'locale' => $locale]); $translation->name = $name; $translation->save(); } $this->imported_at = Carbon::now()->format('Y-m-d H:i:s'); }
/** * Update the specified resource in storage. * * Only owner and admin, editor can edit designer page. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $design = Design::find($id); if (empty($design)) { abort(404); } if ($request->user()->cannot('edit-design', $design)) { abort(403); } if ($request->input('lock')) { if ($design->lock()) { return; // 200 OK } else { abort(423); // 423 Locked } } $this->validate($request, ['image_id' => 'integer|exists:image,id', 'gallery_image_ids.*' => 'integer|exists:image,id', 'designer_id' => 'integer|exists:designer,id', 'user_id' => 'integer|exists:user,id', 'tag_ids.*' => 'integer|exists:tag,id', 'price' => 'numeric|between:0.01,999999.99', 'currency' => 'required_with:price|' . Currencies::validator(), 'webshop' => 'url|max:255', 'translations.*.name' => 'string|max:255']); if ($request->has('user_id')) { if ($request->user()->can('transfer-design', $design)) { $design->transfer($request->input('user_id')); } else { abort(403); } } $design->fill($request->all()); $design->updateEuroPrice(); $design->save(); if ($request->has('translations')) { foreach ($request->input('translations') as $locale => $texts) { if (!Languages::has($locale)) { continue; } $translation = DesignTranslation::where([['design_id', $design->id], ['locale', $locale]])->first(); if (!count($translation)) { $translation = new DesignTranslation(); $translation->design_id = $design->id; $translation->locale = $locale; } $translation->fill($texts); $translation->save(); } } }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $story = Story::find($id); if (empty($story)) { abort(404); } if ($request->user()->cannot('edit-story', $story)) { abort(403); } if ($request->input('lock')) { if ($story->lock()) { return; // 200 OK } else { abort(423); // 423 Locked } } $this->validate($request, ['image_id' => 'integer|exists:image,id', 'user_id' => 'integer|exists:user,id', 'tag_ids.*' => 'integer|exists:tag,id', 'translations' => 'array', 'translations.*.title' => 'string|max:255', 'translations.*.content' => 'string']); $story->update(app_array_filter($request->all(), ['image_id', 'tag_ids'])); // TODO transfer story page to another user... if ($request->has('translations')) { foreach ($request->input('translations') as $locale => $texts) { if (!Languages::has($locale)) { continue; } $translation = StoryTranslation::firstOrCreate(['story_id' => $id, 'locale' => $locale]); $translation->update(app_array_filter($texts, ['title', 'content'])); } } }
/** * Update the specified resource in storage. * * Only owner and admin, editor can edit designer page. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { $designer = Designer::find($id); if (empty($designer)) { abort(404); } if ($request->user()->cannot('edit-designer', $designer)) { abort(403); } if ($request->input('lock')) { if ($designer->lock()) { return; // 200 OK } else { abort(423); // 423 Locked } } $this->validate($request, ['image_id' => 'integer|exists:image,id', 'logo_id' => 'integer|exists:image,id', 'gallery_image_ids.*' => 'integer|exists:image,id', 'city_id' => 'integer|exists:city,id', 'user_id' => 'integer|exists:user,id', 'tag_ids.*' => 'integer|exists:tag,id', 'email' => 'email|max:255', 'website' => 'url|max:255', 'facebook' => 'url|max:255', 'instagram' => 'url|max:255', 'pinterest' => 'url|max:255', 'youtube' => 'url|max:255', 'vimeo' => 'url|max:255', 'translations.*.name' => 'string|max:255', 'translations.*.tagline' => 'string|max:255', 'translations.*.content' => 'string']); if ($request->has('user_id')) { if ($request->user()->can('transfer-designer', $designer)) { $designer->transfer($request->input('user_id')); } else { abort(403); } } $designer->fill($request->all()); $designer->save(); if ($request->has('translations')) { foreach ($request->input('translations') as $locale => $texts) { if (!Languages::has($locale)) { continue; } $translation = DesignerTranslation::firstOrCreate(['designer_id' => $id, 'locale' => $locale]); $translation->update(app_array_filter($texts, ['name', 'tagline', 'content'])); } } }