aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Lib/Traits/HelpHelperTrait.php
blob: b7711bbd5e30210bcdd5c21751fa3670daf15b94 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php

namespace Zotlabs\Lib\Traits;

use CommerceGuys\Intl\Language\LanguageRepository;

trait HelpHelperTrait {

	// PHP versions before 8.2 does not support trait constants,
	// Leave this commented out until we drop support for PHP 8.1.
	//
	// const VALID_FILE_EXT = ['md', 'bb', 'html'];

	private string $file_name = '';
	private string $file_type = '';

	/**
	 * Determines help language.
	 *
	 * If the language was specified in the URL, override the language preference
	 * of the browser. Default to English if both of these are absent.
	 *
	 * Updates the `$lang` property of the module.
	 */
	private function determine_help_language() {

		$language_repository = new LanguageRepository;
		$languages = $language_repository->getList();

		if(array_key_exists(argv(1), $languages)) {
			$lang = argv(1);
			$from_url = true;
		} else {
			$lang = \App::$language;
			if(! isset($lang))
				$lang = 'en';

			$from_url = false;
		}

		$this->lang =  array('language' => $lang, 'from_url' => $from_url);
	}

	/**
	 * Find the full path name of the file, given it's base path and
	 * the language of the request.
	 *
	 * @param string $base_path		The path of the file to find, relative to the
	 *	doc root path, and without the extension.
	 */
	private function find_help_file(string $base_path, string $lang): void {

		// Use local variable until we can use trait constants.
		$valid_file_ext = ['md', 'bb', 'html'];

		$base_path = "doc/{$lang}/${base_path}";

		foreach ($valid_file_ext as $ext) {
			$path = "{$base_path}.{$ext}";
			if (file_exists($path)) {
				$this->file_name = $path;
				$this->file_type = $ext;

				break;
			}
		}
	}
}