1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PhpBotFramework. |
5
|
|
|
* |
6
|
|
|
* PhpBotFramework is free software: you can redistribute it and/or modify |
7
|
|
|
* it under the terms of the GNU Lesser General Public License as |
8
|
|
|
* published by the Free Software Foundation, version 3. |
9
|
|
|
* |
10
|
|
|
* PhpBotFramework is distributed in the hope that it will be useful, but |
11
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13
|
|
|
* Lesser General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace PhpBotFramework\Localization; |
20
|
|
|
|
21
|
|
|
use PhpBotFramework\Core\CoreBot; |
22
|
|
|
use PhpBotFramework\Entities\InlineKeyboard; |
23
|
|
|
|
24
|
|
|
/** \class Button Button Inline keyboard with localizated buttons. */ |
25
|
|
|
class Button extends InlineKeyboard |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* \addtogroup InlineKeyboard InlineKeyboard |
29
|
|
|
* \brief Handle an inline keyboard to send along with messages. |
30
|
|
|
* @{ |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
/** \brief Stores a reference to the bot that's using the inline keyboard. */ |
34
|
|
|
protected $bot; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* \brief Create an inline keyboard object with localizated buttons. |
38
|
|
|
* @param CoreBot $bot References to the bot that's using the inline keyboard. |
39
|
|
|
* @param array $buttons Buttons passed as inizialization. |
40
|
|
|
*/ |
41
|
1 |
|
public function __construct(CoreBot &$bot, array $buttons = array()) |
42
|
|
|
{ |
43
|
1 |
|
$this->bot = $bot; |
44
|
|
|
|
45
|
|
|
// Call parent constructor passing array |
46
|
1 |
|
parent::__construct($buttons); |
47
|
1 |
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* \brief Get a simple Back button with back as <code>callback_data</code>. |
52
|
|
|
* @param $json_serialized return a json serialized string, or an array. |
53
|
|
|
* @return string|array A button with written "back". |
54
|
|
|
*/ |
55
|
|
|
public function getBackButton(bool $json_serialized = true) |
56
|
|
|
{ |
57
|
|
|
// Create the button |
58
|
|
|
$inline_keyboard = [ 'inline_keyboard' => |
59
|
|
|
[ |
60
|
|
|
[ |
61
|
|
|
[ |
62
|
|
|
'text' => $this->bot->local[$this->bot->language]['Back_Button'], |
|
|
|
|
63
|
|
|
'callback_data' => 'back' |
64
|
|
|
] |
65
|
|
|
] |
66
|
|
|
] |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
// Serialize everything as JSON if necessary |
70
|
|
|
if ($json_serialized) { |
71
|
|
|
return json_encode($inline_keyboard); |
72
|
|
|
} else { |
73
|
|
|
return $inline_keyboard; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* \brief Get a Back and a Skip buttons inthe same row. |
79
|
|
|
* \details Back button has callback_data "back" and Skip button has callback_data "skip". |
80
|
|
|
* @param bool $json_serialized return a json serialized string, or an array. |
81
|
|
|
* @return string|array A button with written "back" and one with written "Skip". |
82
|
|
|
*/ |
83
|
|
|
public function getBackSkipKeyboard(bool $json_serialized = true) |
84
|
|
|
{ |
85
|
|
|
// Create the keyboard |
86
|
|
|
$inline_keyboard = [ 'inline_keyboard' => |
87
|
|
|
[ |
88
|
|
|
[ |
89
|
|
|
[ |
90
|
|
|
'text' => $this->bot->local[$this->bot->language]['Back_Button'], |
|
|
|
|
91
|
|
|
'callback_data' => 'back' |
92
|
|
|
], |
93
|
|
|
[ |
94
|
|
|
'text' => $this->bot->local[$this->bot->language]['Skip_Button'], |
95
|
|
|
'callback_data' => 'skip' |
96
|
|
|
] |
97
|
|
|
] |
98
|
|
|
] |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
if ($json_serialized) { |
102
|
|
|
return json_encode($inline_keyboard); |
103
|
|
|
} else { |
104
|
|
|
return $inline_keyboard; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* \brief Get various buttons for set various languages. |
110
|
|
|
* \details Create a button for each language contained in <code>$localization['languages']</code> variable of $bot object. |
111
|
|
|
* There will be a button per row. |
112
|
|
|
* |
113
|
|
|
* The button's label will include the target language and the current one. |
114
|
|
|
* The callback data for each button will be "cl/key" where key is the key in <code>$localization['languages']</code>. |
115
|
|
|
* @param $prefix Prefix followed by '/' and the language index (en, it..). |
116
|
|
|
* @param $json_serialized Get a JSON-serialized string or an array. |
117
|
|
|
* @return string|array The buttons in the selected type. |
118
|
|
|
*/ |
119
|
|
|
public function getChooseLanguageKeyboard(string $prefix = 'cl', bool $json_serialized = true) |
120
|
|
|
{ |
121
|
|
|
$inline_keyboard = ['inline_keyboard' => array()]; |
122
|
|
|
|
123
|
|
|
foreach ($this->bot->local as $languages => $language_msg) { |
124
|
|
|
// If the language is the same as the one set for the current user in $bot |
125
|
|
|
if (strpos($languages, $this->bot->language) !== false) { |
126
|
|
|
// Just create a button with one language in it |
127
|
|
|
array_push($inline_keyboard['inline_keyboard'], [ |
128
|
|
|
[ |
129
|
|
|
'text' => $language_msg['Language'], |
130
|
|
|
'callback_data' => 'same/language' |
131
|
|
|
] |
132
|
|
|
]); |
133
|
|
|
} else { |
134
|
|
|
array_push($inline_keyboard['inline_keyboard'], [ |
135
|
|
|
[ |
136
|
|
|
'text' => $language_msg['Language'] . '/' . $this->bot->local[$this->bot->language][$languages], |
|
|
|
|
137
|
|
|
'callback_data' => $prefix . '/' . $languages |
138
|
|
|
] |
139
|
|
|
]); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
// Unset the variables from the foreach |
144
|
|
|
unset($languages); |
145
|
|
|
unset($language_msg); |
146
|
|
|
|
147
|
|
|
array_push($inline_keyboard['inline_keyboard'], [ |
148
|
|
|
[ |
149
|
|
|
'text' => $this->bot->local[$this->bot->language]['Back_Button'], |
150
|
|
|
'callback_data' => 'back' |
151
|
|
|
] |
152
|
|
|
]); |
153
|
|
|
|
154
|
|
|
if ($json_serialized) { |
155
|
|
|
return json_encode($inline_keyboard); |
156
|
|
|
} else { |
157
|
|
|
return $inline_keyboard; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** @} */ |
162
|
|
|
} |
163
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.