/** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(SitemapRequest $request) { //get sitemap order_by_number $siblings_of_created_sitemap = Sitemap::where('parent_id', $request->get('parent_id'))->get(); $sitemap_order_by_number = $siblings_of_created_sitemap->max('order_by_number') + 1; //null+1 = 1 //dc($sitemap_order_by_number); //get sitemap depth $sitemap_depth = Sitemap::where('id', $request->get('parent_id'))->first()->depth + 1; //dc($sitemap_depth); //return "view"; $request->merge(array('created_by_user_id' => auth()->user()->id, 'updated_by_user_id' => auth()->user()->id, 'depth' => $sitemap_depth, 'order_by_number' => $sitemap_order_by_number)); DB::transaction(function () use($request) { try { $enabledLocales = $this->sitemap->getEnabledLocales(); $created_sitemap = Sitemap::create($request->all()); $template = $created_sitemap->template; $created_sitemap = $this->sitemap->getSelectedSitemap($created_sitemap->id, $template); if ($request->has('sitemapCategory')) { $syncArray = []; //dc('test'); foreach ($request['sitemapCategory'] as $formName => $formValue) { //dc($formName); //dc($formValue); $cnt = 1; $created_sitemap->categories()->where('category', $formName)->delete(); //$flight->delete(); //$sitemap->categories()->destroy(1); foreach ($formValue as $key2 => $sitemap_category_id) { //echo $mediatranslation_id; $syncArray = ['sitemap_id' => $created_sitemap->id, 'sitemap_category_id' => (int) $sitemap_category_id, 'category' => $formName, 'order_by_number' => $cnt++]; $created_sitemap->categories()->insert($syncArray); } } } foreach ($enabledLocales as $key => $enabledLocale) { //dc($request->translations); $localeRequest = array_add($request->translations[$enabledLocale->languageCode], 'locale_id', $enabledLocale->id); //translation $localeRequest['slug'] = str_slug($request->translations[$enabledLocale->languageCode]['name']); //$localeRequest['slug'] = str_slug($request->translations[$enabledLocale->languageCode]['name']); $localeRequest['content'] = clean($request->translations[$enabledLocale->languageCode]['content']); $created_translation = $created_sitemap->translations()->create($localeRequest); //translation->template $localeRequest[$template->slug]['content'] = clean($localeRequest[$template->slug]['content']); $created_translation->{$template->slug}()->create($localeRequest[$template->slug]); //mediatranslations $this->sitemap->syncThisTranslationMediaTranslation($created_translation->id, $request->translations[$enabledLocale->languageCode], $created_sitemap->translations[$enabledLocale->languageCode]); } //update reference_sitemap if ($request->has('reference')) { $syncArray = []; $cnt = 1; foreach ($request->get('reference') as $key => $component_reference) { //dc($id); foreach ($component_reference as $component_id => $reference_id) { //dc($component_id." = ".$reference_id); $testArray[] = ['sitemap_id' => $created_sitemap->id, 'reference_id' => $reference_id, 'component_id' => $component_id, 'order_by_number' => $cnt++]; } } $created_sitemap->references()->sync([]); //pivot 3 key $created_sitemap->references()->attach($testArray); } else { //$created_sitemap->references()->sync([]); } Flash::success('Je pagina is aangemaakt!'); } catch (\Exception $e) { Flash::error('Your Sitemap translation has NOT been created!'); dc($e); dd($e->getMessage()); //send mail with subject "db import failed" and body of $e->getMessage() } }); //dd('stop'); //Flash::success('Your Sitemap translation has been created!'); return redirect()->back(); }
/** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(SitemapRequest $request, $id) { //get sitemap order_by_number if ($request->get('order_by_number') == '0') { $siblings_of_updated_sitemap = Sitemap::where('parent_id', $request->get('parent_id'))->get(); //dc($siblings_of_updated_sitemap); $sitemap_order_by_number = $siblings_of_updated_sitemap->max('order_by_number') + 1; //null+1 = 1 //dc($sitemap_order_by_number); $request->merge(array('order_by_number' => $sitemap_order_by_number)); } //get sitemap depth if ($request->get('depth') == '') { $sitemap_depth = Sitemap::where('id', $request->get('parent_id'))->first()->depth + 1; $request->merge(array('depth' => $sitemap_depth)); } //dc($sitemap_order_by_number); //dc($sitemap_depth); //return "view"; //return "view"; $request->merge(array('updated_by_user_id' => auth()->user()->id)); DB::transaction(function () use($request, $id) { try { $enabledLocales = $this->sitemap->getEnabledLocales(); $template = Sitemap::with('template')->findOrFail($id)->template; //todo to much sql $sitemap = $this->sitemap->getSelectedSitemap($id, $template); $sitemap->update($request->all()); //sitemapCategory[locatie][] //dc($sitemap->categories); if ($request->has('sitemapCategory')) { $syncArray = []; //dc('test'); foreach ($request['sitemapCategory'] as $formName => $formValue) { //dc($formName); //dc($formValue); $cnt = 1; $sitemap->categories()->where('category', $formName)->delete(); //$flight->delete(); //$sitemap->categories()->destroy(1); foreach ($formValue as $key2 => $sitemap_category_id) { //echo $mediatranslation_id; $syncArray = ['sitemap_id' => $id, 'sitemap_category_id' => (int) $sitemap_category_id, 'category' => $formName, 'order_by_number' => $cnt++]; $sitemap->categories()->insert($syncArray); } } } //$sitemap->touch(); //$sitemap->timestamp('updated_at')->useCurrent(); foreach ($enabledLocales as $key => $enabledLocale) { $localeRequest = array_add($request->translations[$enabledLocale->languageCode], 'locale_id', $enabledLocale->id); //translation //make slug from name if ($sitemap->id != 1) { $localeRequest['slug'] = str_slug($request->translations[$enabledLocale->languageCode]['name']); } else { $localeRequest['slug'] = ""; } if (isset($localeRequest['content'])) { $localeRequest['content'] = clean($request->translations[$enabledLocale->languageCode]['content']); } $sitemap->translations[$enabledLocale->languageCode]->update($localeRequest); //translation->template $localeRequest[$template->slug]['content'] = clean($localeRequest[$template->slug]['content']); //dc($template->slug); $sitemap->translations[$enabledLocale->languageCode]->{$template->slug}->update($localeRequest[$template->slug]); //mediatranslation $this->sitemap->syncThisTranslationMediaTranslation($sitemap->translations[$enabledLocale->languageCode]->id, $request->translations[$enabledLocale->languageCode], $sitemap->translations[$enabledLocale->languageCode]); } //update reference_sitemap if ($request->has('reference')) { $syncArray = []; $cnt = 1; foreach ($request->input('reference') as $key => $component_reference) { //dc($id); foreach ($component_reference as $component_id => $reference_id) { //dc($component_id." = ".$reference_id); $testArray[] = ['sitemap_id' => $id, 'reference_id' => $reference_id, 'component_id' => $component_id, 'order_by_number' => $cnt++]; } } $sitemap->references()->sync([]); //pivot 3 key $sitemap->references()->attach($testArray); } else { $sitemap->references()->sync([]); } Flash::success('Update is gelukt!'); } catch (\Exception $e) { if (request()->ajax()) { throw new \Exception('Updaten is niet gelukt.<br>SitemapController->update() <br>' . $e->getMessage() . ' '); } Flash::error('Update is mislukt! ' . $e->getMessage() . ''); dc($e); dd($e->getMessage()); return redirect()->back(); } }); //return "view"; return redirect()->back(); }