private function saveChart(Chart $chart, $data) { $user = \Auth::user(); $jsonConfig = json_encode($data); $chart->name = $data["chart-name"]; $chart->notes = $data["chart-notes"]; $chart->slug = $data["chart-slug"]; unset($data["chart-notes"]); unset($data["chart-slug"]); $chart->last_edited_at = Carbon::now(); $chart->last_edited_by = $user->name; $chart->config = $jsonConfig; $chart->save(); Cache::flush(); // Purge exported png files so we can regenerate them $files = glob(public_path() . "/exports/" . $chart->slug . ".*"); foreach ($files as $file) { unlink($file); } }
/** * Update the specified resource in storage. * * @param int $id * @return Response */ public function update(Chart $chart) { $data = Input::all(); $chartName = $data["chart-name"]; $json = json_encode($data); $newData = new \stdClass(); $newData->config = $json; $newData->name = $chartName; $chart->fill(['name' => $chartName, 'config' => $json]); $chart->save(); Cache::flush(); return ['success' => true, 'data' => ['id' => $chart->id, 'viewUrl' => route('view', $chart->id)]]; }
public function showChart(Chart $chart) { $referer_s = \Request::header('referer'); if ($referer_s) { $root = parse_url(\Request::root()); $referer = parse_url($referer_s); if ($root['host'] == $referer['host'] && strlen($referer['path']) > 1 && !str_contains($referer_s, ".html") && !str_contains($referer_s, "wp-admin") && !str_contains($referer_s, "preview=true") && !str_contains($referer_s, "how-to") && !str_contains($referer_s, "grapher") && !str_contains($referer_s, "about") && !str_contains($referer_s, "roser/")) { $chart->origin_url = "https://" . $root['host'] . $referer['path']; $chart->save(); } } if ($chart) { $config = Chart::getConfigWithUrl($chart); $data = new \StdClass(); $logoUrl = Setting::where('meta_name', 'logoUrl')->first(); $data->logoUrl = !empty($logoUrl) ? url('/') . '/' . $logoUrl->meta_value : ''; $canonicalUrl = URL::to($chart->slug); // Make metadata for twitter embed cards! $chartMeta = new \StdClass(); // Replace the chart title placeholders with generic equivalents for now $title = $config->{"chart-name"}; $title = preg_replace("/, \\*time\\*/", " over time", $title); $title = preg_replace("/\\*time\\*/", "over time", $title); $chartMeta->title = $title; // Description is required by twitter if (isset($config->{"chart-subname"})) { $chartMeta->description = $config->{"chart-subname"}; } else { $chartMeta->description = "An interactive visualization from Our World In Data."; } $query = $_SERVER['QUERY_STRING']; $baseUrl = \Request::root() . "/" . $chart->slug; $canonicalUrl = $baseUrl; $imageUrl = $baseUrl . ".png"; if ($query != '') { $canonicalUrl .= "?" . $query; $imageUrl .= "?" . $query; } $chartMeta->imageUrl = $imageUrl; $chartMeta->canonicalUrl = $canonicalUrl; // Give the image exporter a head start on the request for imageUrl if (!str_contains(\Request::path(), ".export")) { Chart::exportPNGAsync($chart->slug, $_SERVER['QUERY_STRING'] . "&size=1000x700", 1000, 700); } return view('view.show', compact('chart', 'config', 'data', 'canonicalUrl', 'chartMeta')); } else { return 'No chart found to view'; } }