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

143 lines
4.6 KiB
PHP
Raw Normal View History

2017-07-16 00:35:08 -05:00
<?php
/**
* IntroController.php
2020-01-31 00:32:04 -06:00
* Copyright (c) 2019 james@firefly-iii.org
2017-07-16 00:35:08 -05:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 01:40:00 -05: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 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05: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 01:40:00 -05: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 00:35:08 -05:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Json;
use FireflyIII\Support\Http\Controllers\GetConfigurationData;
2018-07-08 05:28:42 -05:00
use Illuminate\Http\JsonResponse;
2017-07-22 03:50:30 -05:00
use Log;
2017-07-16 00:35:08 -05:00
/**
2017-11-15 05:25:49 -06:00
* Class IntroController.
2017-07-16 00:35:08 -05:00
*/
class IntroController
{
use GetConfigurationData;
2017-07-16 00:35:08 -05:00
/**
2018-07-21 01:06:24 -05:00
* Returns the introduction wizard for a page.
*
2018-07-08 05:08:53 -05:00
* @param string $route
* @param string|null $specificPage
2017-07-16 00:35:08 -05:00
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
2017-07-16 00:35:08 -05:00
*/
2018-07-08 05:28:42 -05:00
public function getIntroSteps(string $route, string $specificPage = null): JsonResponse
2017-07-16 00:35:08 -05:00
{
2017-12-23 10:42:07 -06:00
Log::debug(sprintf('getIntroSteps for route "%s" and page "%s"', $route, $specificPage));
2018-07-08 05:08:53 -05:00
$specificPage = $specificPage ?? '';
2017-07-20 23:00:10 -05:00
$steps = $this->getBasicSteps($route);
$specificSteps = $this->getSpecificSteps($route, $specificPage);
if (0 === count($specificSteps)) {
2017-12-23 10:42:07 -06:00
Log::debug(sprintf('No specific steps for route "%s" and page "%s"', $route, $specificPage));
2017-12-29 02:05:35 -06:00
2018-03-10 13:30:09 -06:00
return response()->json($steps);
2017-07-20 23:00:10 -05:00
}
if ($this->hasOutroStep($route)) {
2017-12-23 10:42:07 -06:00
// @codeCoverageIgnoreStart
2017-07-20 23:00:10 -05:00
// save last step:
$lastStep = $steps[count($steps) - 1];
2017-07-20 23:00:10 -05:00
// remove last step:
array_pop($steps);
// merge arrays and add last step again
$steps = array_merge($steps, $specificSteps);
$steps[] = $lastStep;
2017-12-23 10:42:07 -06:00
// @codeCoverageIgnoreEnd
2017-07-20 23:00:10 -05:00
}
if (!$this->hasOutroStep($route)) {
$steps = array_merge($steps, $specificSteps);
}
2018-03-10 13:30:09 -06:00
return response()->json($steps);
2017-07-20 23:00:10 -05:00
}
/**
2018-07-21 01:06:24 -05:00
* Returns true if there is a general outro step.
*
2017-07-20 23:00:10 -05:00
* @param string $route
*
* @return bool
*/
public function hasOutroStep(string $route): bool
{
$routeKey = str_replace('.', '_', $route);
2017-12-23 10:42:07 -06:00
Log::debug(sprintf('Has outro step for route %s', $routeKey));
2017-07-20 23:00:10 -05:00
$elements = config(sprintf('intro.%s', $routeKey));
if (!is_array($elements)) {
2017-07-22 03:50:30 -05:00
return false;
}
2018-04-02 08:10:40 -05:00
$hasStep = array_key_exists('outro', $elements);
2017-12-23 10:42:07 -06:00
Log::debug('Elements is array', $elements);
Log::debug('Keys is', array_keys($elements));
2018-04-02 08:10:40 -05:00
Log::debug(sprintf('Keys has "outro": %s', var_export($hasStep, true)));
2017-07-20 23:00:10 -05:00
2018-04-02 08:10:40 -05:00
return $hasStep;
2017-07-20 23:00:10 -05:00
}
2017-07-23 00:30:05 -05:00
/**
2018-07-21 01:06:24 -05:00
* Enable the boxes for a specific page again.
*
2018-07-08 05:08:53 -05:00
* @param string $route
* @param string|null $specialPage
2017-07-23 00:30:05 -05:00
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
2017-07-23 00:30:05 -05:00
*/
2018-07-08 05:28:42 -05:00
public function postEnable(string $route, string $specialPage = null): JsonResponse
2017-07-23 00:30:05 -05:00
{
2018-07-08 05:08:53 -05:00
$specialPage = $specialPage ?? '';
$route = str_replace('.', '_', $route);
$key = 'shown_demo_' . $route;
2017-11-15 05:25:49 -06:00
if ('' !== $specialPage) {
2017-07-23 00:30:05 -05: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 13:58:13 -05:00
app('preferences')->mark();
2017-07-23 00:30:05 -05:00
return response()->json(['message' => (string) trans('firefly.intro_boxes_after_refresh')]);
2017-07-23 00:30:05 -05:00
}
2017-07-20 23:00:10 -05:00
/**
2018-07-21 01:06:24 -05:00
* Set that you saw them.
*
2018-07-08 05:08:53 -05:00
* @param string $route
* @param string|null $specialPage
2017-07-20 23:00:10 -05:00
*
2018-07-08 05:28:42 -05:00
* @return JsonResponse
2017-07-20 23:00:10 -05:00
*/
2018-07-08 05:28:42 -05:00
public function postFinished(string $route, string $specialPage = null): JsonResponse
2017-07-20 23:00:10 -05:00
{
2018-07-08 05:08:53 -05:00
$specialPage = $specialPage ?? '';
$key = 'shown_demo_' . $route;
2017-11-15 05:25:49 -06:00
if ('' !== $specialPage) {
2017-07-22 03:50:30 -05:00
$key .= '_' . $specialPage;
}
2017-07-23 00:30:05 -05: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-20 23:00:10 -05:00
2019-06-21 22:51:32 -05:00
return response()->json(['result' => sprintf('Reported demo watched for route "%s" (%s): %s.', $route, $specialPage, $key)]);
2017-07-20 23:00:10 -05:00
}
2017-08-12 00:48:39 -05:00
}