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

188 lines
6.0 KiB
PHP
Raw Normal View History

2017-07-16 07:35:08 +02:00
<?php
/**
* IntroController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-07-16 07:35:08 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Json;
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
{
/**
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);
2018-04-28 06:23:13 +02:00
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:
2018-04-28 06:23:13 +02:00
$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
}
/**
* @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));
2018-04-02 15:10:40 +02:00
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-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);
2017-07-23 07:30:05 +02:00
2018-03-10 20:30:09 +01:00
return response()->json(['message' => trans('firefly.intro_boxes_after_refresh')]);
2017-07-23 07:30:05 +02:00
}
2017-07-21 06:00:10 +02:00
/**
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
2018-03-10 20:30:09 +01:00
return response()->json(['result' => sprintf('Reported demo watched for route "%s".', $route)]);
2017-07-21 06:00:10 +02:00
}
/**
* @param string $route
*
* @return array
*/
private function getBasicSteps(string $route): array
{
$routeKey = str_replace('.', '_', $route);
$elements = config(sprintf('intro.%s', $routeKey));
2017-07-16 07:35:08 +02:00
$steps = [];
2018-04-28 06:23:13 +02:00
if (\is_array($elements) && \count($elements) > 0) {
2017-07-16 07:35:08 +02:00
foreach ($elements as $key => $options) {
$currentStep = $options;
// get the text:
$currentStep['intro'] = trans('intro.' . $route . '_' . $key);
// save in array:
$steps[] = $currentStep;
}
}
2018-04-28 06:23:13 +02:00
Log::debug(sprintf('Total basic steps for %s is %d', $routeKey, \count($steps)));
2017-07-16 07:35:08 +02:00
2017-07-21 06:00:10 +02:00
return $steps;
2017-07-16 07:35:08 +02:00
}
/**
* @param string $route
2017-07-21 06:00:10 +02:00
* @param string $specificPage
2017-07-16 07:35:08 +02:00
*
2017-07-21 06:00:10 +02:00
* @return array
2017-07-16 07:35:08 +02:00
*/
2017-07-21 06:00:10 +02:00
private function getSpecificSteps(string $route, string $specificPage): array
2017-07-16 07:35:08 +02:00
{
2017-12-23 17:42:07 +01:00
$steps = [];
$routeKey = '';
2017-07-16 07:35:08 +02:00
2017-07-21 06:00:10 +02:00
// user is on page with specific instructions:
2018-04-28 06:23:13 +02:00
if (\strlen($specificPage) > 0) {
2017-07-21 06:00:10 +02:00
$routeKey = str_replace('.', '_', $route);
$elements = config(sprintf('intro.%s', $routeKey . '_' . $specificPage));
2018-04-28 06:23:13 +02:00
if (\is_array($elements) && \count($elements) > 0) {
2017-07-21 06:00:10 +02:00
foreach ($elements as $key => $options) {
2017-07-22 10:50:30 +02:00
$currentStep = $options;
2017-07-21 06:00:10 +02:00
// get the text:
$currentStep['intro'] = trans('intro.' . $route . '_' . $specificPage . '_' . $key);
// save in array:
$steps[] = $currentStep;
}
}
}
2018-04-28 06:23:13 +02:00
Log::debug(sprintf('Total specific steps for route "%s" and page "%s" (routeKey is "%s") is %d', $route, $specificPage, $routeKey, \count($steps)));
2017-07-21 06:00:10 +02:00
return $steps;
2017-07-16 07:35:08 +02:00
}
2017-08-12 07:48:39 +02:00
}