forked from scan5415/grav-plugin-form-database
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathform-database.php
170 lines (151 loc) · 5.97 KB
/
form-database.php
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
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;
use PDO;
use Grav;
/**
* Class FormDatabasePlugin
* @package Grav\Plugin
*/
class FormDatabasePlugin extends Plugin {
protected $db;
protected $table;
protected $config;
protected $pname;//plugin's name
/**
* @return array
*
* The getSubscribedEvents() gives the core a list of events
* that the plugin wants to listen to. The key of each
* array section is the event that the plugin listens to
* and the value (in the form of an array) contains the
* callable (or function) as well as the priority. The
* higher the number the higher the priority.
*/
public static function getSubscribedEvents() {
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
'onFormProcessed' => ['onFormProcessed', 0]
];
}
/**
* Initialize the plugin
*/
public function onPluginsInitialized() {
// Don't proceed if we are in the admin plugin
$this->pname = 'form-database';
if ($this->isAdmin()) {
return;
}
}
/**
* Save Data in Database when processing the form
*
* @param Event $event
*/
public function onFormProcessed(Event $event) {
$action = $event['action'];
switch ($action) {
case 'database' :
//$this->grav['debugger']->addMessage('onFormProcessed - database');
$params = $event['params'];
$form = $event['form'];
$this->prepareDB($params);
$form_fields = $this->prepareFormFields($params['table_fields']?? $params['fields'], $form);
$fields = array_keys($form_fields);
$string = 'INSERT INTO ' . $this->table . ' ('. implode(', ', $fields).') VALUES (:'. implode(', :', $fields). ')';
$this->db->insert($string, $form_fields);
break;
}
}
/**
* Ensure Data is ready for PDO
* @param type $formFields
* @param type $form
* @return type
*/
private function prepareFormFields($formFields, $form) {
$data = $form['data'];
//$this->grav['debugger']->addMessage($data);
$twig = $this->grav['twig'];
$vars = [
'form' => $form
];
$fields = $formFields;
$separator = $this->config->get('plugins.'.$this->pname.'.array_separator')??';';//backwards compatible
foreach ($fields as $field => $val) {
$dataValue = $data[$val];
if (strrpos($val, '{{')>=0 && strrpos($val, '}}')>0) {
// Process with Twig
$dataValue = $twig->processString($val, $vars);
} else if(is_null($dataValue)){
// if value hard coded
$dataValue = $val;
}
if (gettype($dataValue) == 'array')
{
//stringify array
$dataValue = implode($separator, $dataValue); //if form result = array expl. checkboxes or multiple selection
}
$fields[$field] = $dataValue;
}
return $fields;
}
private function prepareDB($params) {
//if db not passed with the form
$db_name = $params['db']?? $this->config->get('plugins.'.$this->pname.'.db');
if($db_name=='')
{
throw new \RuntimeException( "NO db SET. Set it in {$this->pname}.yaml of in your form's yaml");
}
$this->table = $params['table']?? $this->config->get('plugins.'.$this->pname.'.table');
if ($this->table == '') {
throw new \RuntimeException( "NO table SET. Set it in {$this->pname}.yaml of in your form's yaml");
}
// backwards compatible config
$engine = $this->config->get('plugins.'.$this->pname.'.engine')??'mysql';
$user = $this->config->get('plugins.'.$this->pname.'.username')??$this->config->get('plugins.'.$this->pname.'.mysql_username')??'';
$pwd = $this->config->get('plugins.'.$this->pname.'.password')??$this->config->get('plugins.'.$this->pname.'.mysql_password')??'';
$server = $this->config->get('plugins.'.$this->pname.'.server')??$this->config->get('plugins.'.$this->pname.'.mysql_server')??'';
$port = $this->config->get('plugins.'.$this->pname.'.port')??$this->config->get('plugins.'.$this->pname.'.mysql_port')??'';
//
$dsn = $engine . ':';
switch ($engine) {
case 'mysql':
$dsn .= 'host=' . $server;
$dsn .= ';dbname=' . $db_name;
$dsn .= ';port=' .$port;
$user = $user;
$pwd = $pwd;
break;
case 'pgsql':
$dsn .= 'host=' . $server;
$dsn .= ' dbname=' . $db_name;
$dsn .= ' port=' . $port;
$dsn .= ' user=' . $user;
$dsn .= ' password=' . $pwd;
$user = '';
$pwd = '';
break;
case 'sqlite':
$dsn .= $server;
$dsn .= '/';
$dsn .= $db_name;
break;
default:
$dsn .= 'host=' . $server;
$dsn .= ';dbname=' . $db_name;
$dsn .= ';port=' .$port;
$user = $user;
$pwd = $pwd;
break;
}
try {
$this->db = $this->grav['database']->connect( $dsn, $user, $pwd, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION] );
} catch (Exception $e) {
throw new \RuntimeException($user . ":" . $pwd . " | " . $dsn . " | " . $e->getMessage());
}
return $this->db;
}
}