Files
firefly-iii/app/Http/Controllers/Json/IntroController.php

143 lines
4.6 KiB
PHP
Raw Normal View History

2017-07-16 07:35:08 +02:00
<?php
/**
* IntroController.php
* Copyright (c) 2019 thegrumpydictator@gmail.com
2017-07-16 07:35:08 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 08:40:00 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-07-16 07:35:08 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Json;
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
2018-07-08 12:28:42 +02:00
use Illuminate\Http\JsonResponse;
2017-07-22 10:50:30 +02:00
use Log;
2017-07-16 07:35:08 +02:00
/**
2017-11-15 12:25:49 +01:00
* Class IntroController.
2017-07-16 07:35:08 +02:00
*/
class IntroController
{
use GetConfigurationData;
2017-07-16 07:35:08 +02:00
/**
2018-07-21 08:06:24 +02:00
* Returns the introduction wizard for a page.
*
2018-07-08 12:08:53 +02:00
* @param string $route
* @param string|null $specificPage
2017-07-16 07:35:08 +02:00
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
2017-07-16 07:35:08 +02:00
*/
2018-07-08 12:28:42 +02:00
public function getIntroSteps(string $route, string $specificPage = null): JsonResponse
2017-07-16 07:35:08 +02:00
{
2017-12-23 17:42:07 +01:00
Log::debug(sprintf('getIntroSteps for route "%s" and page "%s"', $route, $specificPage));
2018-07-08 12:08:53 +02:00
$specificPage = $specificPage ?? '';
2017-07-21 06:00:10 +02:00
$steps = $this->getBasicSteps($route);
$specificSteps = $this->getSpecificSteps($route, $specificPage);
if (0 === count($specificSteps)) {
2017-12-23 17:42:07 +01:00
Log::debug(sprintf('No specific steps for route "%s" and page "%s"', $route, $specificPage));
2017-12-29 09:05:35 +01:00
2018-03-10 20:30:09 +01:00
return response()->json($steps);
2017-07-21 06:00:10 +02:00
}
if ($this->hasOutroStep($route)) {
2017-12-23 17:42:07 +01:00
// @codeCoverageIgnoreStart
2017-07-21 06:00:10 +02:00
// save last step:
$lastStep = $steps[count($steps) - 1];
2017-07-21 06:00:10 +02:00
// remove last step:
array_pop($steps);
// merge arrays and add last step again
$steps = array_merge($steps, $specificSteps);
$steps[] = $lastStep;
2017-12-23 17:42:07 +01:00
// @codeCoverageIgnoreEnd
2017-07-21 06:00:10 +02:00
}
if (!$this->hasOutroStep($route)) {
$steps = array_merge($steps, $specificSteps);
}
2018-03-10 20:30:09 +01:00
return response()->json($steps);
2017-07-21 06:00:10 +02:00
}
/**
2018-07-21 08:06:24 +02:00
* Returns true if there is a general outro step.
*
2017-07-21 06:00:10 +02:00
* @param string $route
*
* @return bool
*/
public function hasOutroStep(string $route): bool
{
$routeKey = str_replace('.', '_', $route);
2017-12-23 17:42:07 +01:00
Log::debug(sprintf('Has outro step for route %s', $routeKey));
2017-07-21 06:00:10 +02:00
$elements = config(sprintf('intro.%s', $routeKey));
if (!is_array($elements)) {
2017-07-22 10:50:30 +02:00
return false;
}
2018-04-02 15:10:40 +02:00
$hasStep = array_key_exists('outro', $elements);
2017-12-23 17:42:07 +01:00
Log::debug('Elements is array', $elements);
Log::debug('Keys is', array_keys($elements));
2018-04-02 15:10:40 +02:00
Log::debug(sprintf('Keys has "outro": %s', var_export($hasStep, true)));
2017-07-21 06:00:10 +02:00
2018-04-02 15:10:40 +02:00
return $hasStep;
2017-07-21 06:00:10 +02:00
}
2017-07-23 07:30:05 +02:00
/**
2018-07-21 08:06:24 +02:00
* Enable the boxes for a specific page again.
*
2018-07-08 12:08:53 +02:00
* @param string $route
* @param string|null $specialPage
2017-07-23 07:30:05 +02:00
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
2017-07-23 07:30:05 +02:00
*/
2018-07-08 12:28:42 +02:00
public function postEnable(string $route, string $specialPage = null): JsonResponse
2017-07-23 07:30:05 +02:00
{
2018-07-08 12:08:53 +02:00
$specialPage = $specialPage ?? '';
$route = str_replace('.', '_', $route);
$key = 'shown_demo_' . $route;
2017-11-15 12:25:49 +01:00
if ('' !== $specialPage) {
2017-07-23 07:30:05 +02:00
$key .= '_' . $specialPage;
}
Log::debug(sprintf('Going to mark the following route as NOT done: %s with special "%s" (%s)', $route, $specialPage, $key));
app('preferences')->set($key, false);
2019-10-10 20:58:13 +02:00
app('preferences')->mark();
2017-07-23 07:30:05 +02:00
2018-07-15 09:38:49 +02:00
return response()->json(['message' => (string)trans('firefly.intro_boxes_after_refresh')]);
2017-07-23 07:30:05 +02:00
}
2017-07-21 06:00:10 +02:00
/**
2018-07-21 08:06:24 +02:00
* Set that you saw them.
*
2018-07-08 12:08:53 +02:00
* @param string $route
* @param string|null $specialPage
2017-07-21 06:00:10 +02:00
*
2018-07-08 12:28:42 +02:00
* @return JsonResponse
2017-07-21 06:00:10 +02:00
*/
2018-07-08 12:28:42 +02:00
public function postFinished(string $route, string $specialPage = null): JsonResponse
2017-07-21 06:00:10 +02:00
{
2018-07-08 12:08:53 +02:00
$specialPage = $specialPage ?? '';
$key = 'shown_demo_' . $route;
2017-11-15 12:25:49 +01:00
if ('' !== $specialPage) {
2017-07-22 10:50:30 +02:00
$key .= '_' . $specialPage;
}
2017-07-23 07:30:05 +02:00
Log::debug(sprintf('Going to mark the following route as done: %s with special "%s" (%s)', $route, $specialPage, $key));
app('preferences')->set($key, true);
2017-07-21 06:00:10 +02:00
2019-06-22 05:51:32 +02:00
return response()->json(['result' => sprintf('Reported demo watched for route "%s" (%s): %s.', $route, $specialPage, $key)]);
2017-07-21 06:00:10 +02:00
}
2017-08-12 07:48:39 +02:00
}