-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.php
More file actions
executable file
·176 lines (163 loc) · 5.4 KB
/
Copy pathadmin.php
File metadata and controls
executable file
·176 lines (163 loc) · 5.4 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
//include_once ('db.php');
include_once ('GeminiClient.php');
class plugins_geminiai_admin extends plugins_geminiai_db
{
/**
* @var backend_model_template $template
* @var backend_model_data $data
* @var component_core_message $message
* @var backend_controller_plugins $plugins
* @var backend_model_language $modelLanguage
* @var component_collections_language $collectionLanguage
*/
protected backend_model_template $template;
protected backend_model_data $data;
protected component_core_message $message;
protected backend_controller_plugins $plugins;
protected backend_model_language $modelLanguage;
protected component_collections_language $collectionLanguage;
/**
* @var string $action
* @var string $tabs
* @var string $type
*/
public string
$action,
$controller;
/**
* @var array
*/
public array $geminiai;
/**
* constructeur
*/
public function __construct(backend_model_template $t = null) {
$this->template = $t instanceof backend_model_template ? $t : new backend_model_template();
$this->plugins = new backend_controller_plugins();
$formClean = new form_inputEscape();
$this->message = new component_core_message($this->template);
$this->data = new backend_model_data($this);
// Global
if (http_request::isGet('action')) {
$this->action = $formClean->simpleClean($_GET['action']);
} elseif (http_request::isPost('action')) {
$this->action = $formClean->simpleClean($_POST['action']);
}
// POST
if (http_request::isPost('geminiai')) $this->geminiai = $formClean->arrayClean($_POST['geminiai']);
}
/**
* Method to override the name of the plugin in the admin menu
* @return string
*/
public function getExtensionName()
{
return $this->template->getConfigVars('geminiai_management');
}
// --- Database actions
/**
* Assign data to the defined variable or return the data
* @param string $type
* @param string|int|null $id
* @param ?string $context
* @param string|bool $assign
* @return mixed
*/
private function getItems(string $type, $id = null, ?string $context = null, $assign = true) {
return $this->data->getItems($type, $id, $context, $assign);
}
/**
* Update data
* @param string $type
* @param array $params
*/
private function upd(string $type, array $params) {
switch ($type) {
case 'config':
parent::update($type, $params);
break;
}
}
/**
* Insert data
* @param string $type
* @param array $params
*/
private function add(string $type, array $params) {
switch ($type) {
case 'config':
parent::insert($type, $params);
break;
}
}
private function save(){
$setData = $this->getItems('root',NULL,'one',false);
if($setData['id_gc']){
$this->upd(
'config',
[
'api_key_gc' => $this->geminiai['api_key_gc'],
'id' => $setData['id_gc']
]
);
}else{
$this->add(
'config',
[
'api_key_gc' => $this->geminiai['api_key_gc']
]
);
}
$this->message->json_post_response(true, 'update');
}
/**
* @param string $apikey
* @return void
*/
private function getGenerateAI(string $apikey) :void {
$geminiai = new GeminiClient($apikey);
$input = json_decode(file_get_contents('php://input'), true);
// Récupération des textes
$prompt = (string)($input['prompt'] ?? '');
$context = (string)($input['context'] ?? '');
// Préparation du nouveau paramètre $options
$options = [
'action_type' => $input['action_type'] ?? 'write',
'language' => $input['language'] ?? 'français',
'tone' => $input['tone'] ?? 'professionnel',
'length' => $input['length'] ?? 'standard'
];
// Utilisation des 3 paramètres
$result = $geminiai->generate($prompt, $context, $options);
header('Content-Type: application/json');
echo json_encode([
'content' => $result,
'success' => true
]);
exit;
}
/**
* Execute plugin
*/
public function run()
{
if (http_request::isRequest('action')) $this->action = form_inputEscape::simpleClean($_REQUEST['action']);
if(http_request::isGet('controller')) $this->controller = form_inputEscape::simpleClean($_GET['controller']);
if(http_request::isMethod('POST') && isset($this->action)) {
switch ($this->action) {
case 'edit':
$this->save();
break;
case 'generate':
$data = $this->getItems('root',NULL,'one',false);
$this->getGenerateAI($data['api_key_gc']);
break;
}
}else{
$data = $this->getItems('root',NULL,'one',false);
$this->template->assign('geminiai', $data);
$this->template->display('index.tpl');
}
}
}