File size: 17,695 Bytes
36f2119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
session_start();
if (!isset($_SESSION['admin_logged_in'])) {
    header('Location: login.php');
    exit;
}
require '../db_connect.php';

$section = $_GET['section'] ?? 'config';
$valid_sections = ['config', 'stats', 'services', 'founders', 'cases', 'pricing', 'faq', 'solutions', 'goals', 'roadmap', 'bottom_stats', 'about_gallery'];

if (!in_array($section, $valid_sections)) {
    die("Sezione non valida.");
}

// DELETE
if (isset($_GET['delete']) && $section !== 'config') {
    $id = (int)$_GET['delete'];
    $db->prepare("DELETE FROM $section WHERE id = ?")->execute([$id]);
    header("Location: manage.php?section=$section");
    exit;
}

// SAVE / UPDATE
$success = false;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if ($section === 'config') {
        foreach ($_POST as $k => $v) {
            if ($k === 'admin_password' || $k === 'id') continue;
            $stmt = $db->prepare("INSERT OR REPLACE INTO config (key_name, value_text) VALUES (?, ?)");
            $stmt->execute([$k, $v]);
        }
        // Handle Config Images
        foreach ($_FILES as $key => $file) {
            if ($file['error'] === UPLOAD_ERR_OK) {
                $ext = pathinfo($file['name'], PATHINFO_EXTENSION);
                $filename = "uploads/" . $key . "_" . time() . "." . $ext;
                if (move_uploaded_file($file['tmp_name'], "../" . $filename)) {
                     $db->prepare("INSERT OR REPLACE INTO config (key_name, value_text) VALUES (?, ?)")->execute([$key, $filename]);
                }
            }
        }
        $success = true;
    } else {
        // Add or Edit Item
        $is_edit = isset($_POST['id']);
        $fields = [];
        $values = [];
        
        // Define fields based on section (same logic as before)
        $expected_fields = [];
        if($section=='stats') $expected_fields=['label','value','unit','sort_order'];
        if($section=='services') $expected_fields=['title','icon','description','sort_order'];
        if($section=='founders') $expected_fields=['name','role','image_path','sort_order'];
        if($section=='cases') $expected_fields=['title','stats_text','role_text','tags','image_path','sort_order'];
        if($section=='pricing') $expected_fields=['name','price','features','is_recommended','sort_order'];
        if($section=='faq') $expected_fields=['question','answer','sort_order'];
        if($section=='solutions') $expected_fields=['title','description','sort_order'];
        if($section=='goals') $expected_fields=['title','description','sort_order'];
        if($section=='roadmap') $expected_fields=['phase','detail','sort_order'];
        if($section=='bottom_stats') $expected_fields=['value','label','sort_order'];
        if($section=='about_gallery') $expected_fields=['image_path','sort_order'];

        foreach ($expected_fields as $f) {
            if (isset($_POST[$f])) {
                $fields[] = $f;
                $values[] = $_POST[$f];
            }
        }
        
        // Handle File Upload for Item
        if (in_array('image_path', $expected_fields) && isset($_FILES['image_path']) && $_FILES['image_path']['error'] === UPLOAD_ERR_OK) {
             $ext = pathinfo($_FILES['image_path']['name'], PATHINFO_EXTENSION);
             $filename = "uploads/" . $section . "_" . time() . "." . $ext;
             if (move_uploaded_file($_FILES['image_path']['tmp_name'], "../" . $filename)) {
                 if(!in_array('image_path', $fields)) {
                     $fields[] = 'image_path';
                     $values[] = $filename;
                 } else {
                     $key = array_search('image_path', $fields);
                     $values[$key] = $filename;
                 }
             }
        }

        if ($is_edit) {
            $id = $_POST['id'];
            $set = [];
            foreach ($fields as $index => $field) {
                $set[] = "$field = ?";
            }
            $sql = "UPDATE $section SET " . implode(', ', $set) . " WHERE id = ?";
            $values[] = $id;
            $db->prepare($sql)->execute($values);
        } else {
            $placeholders = array_fill(0, count($fields), '?');
            $sql = "INSERT INTO $section (" . implode(', ', $fields) . ") VALUES (" . implode(', ', $placeholders) . ")";
            $db->prepare($sql)->execute($values);
        }
    }
    if ($section !== 'config') {
        header("Location: manage.php?section=$section");
        exit;
    }
}

// FETCH DATA
if ($section === 'config') {
    $items = [];
    try {
        $rows = $db->query("SELECT key_name, value_text FROM config")->fetchAll(PDO::FETCH_KEY_PAIR);
        $items = $rows;
    } catch (Exception $e) {
        die("Errore database config: " . $e->getMessage());
    }
} else {
    try {
        $items = $db->query("SELECT * FROM $section ORDER BY sort_order ASC")->fetchAll();
    } catch (Exception $e) {
        die("Errore database $section: " . $e->getMessage());
    }
}
?>
<!DOCTYPE html>
<html lang="it">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Gestisci <?php echo ucfirst($section); ?> β€” OFT Admin</title>
    <link href="style.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Manrope:wght@400;600;700;800&family=Inter:wght@400;500;600&display=swap" rel="stylesheet">
</head>
<body>

<div class="app-layout">

    <!-- Sidebar -->

    <aside class="sidebar">

        <div class="brand">

            <span>⚑</span> OFT Admin

        </div>

        

        <nav>

            <div class="nav-section">Contenuti</div>

            <a href="manage.php?section=config" class="nav-link <?php if($section=='config') echo 'active'; ?>">βš™οΈ Configurazione</a>

            <a href="manage.php?section=solutions" class="nav-link <?php if($section=='solutions') echo 'active'; ?>">πŸ’‘ Soluzioni</a>

            <a href="manage.php?section=goals" class="nav-link <?php if($section=='goals') echo 'active'; ?>">πŸš€ Obiettivi</a>

            <a href="manage.php?section=roadmap" class="nav-link <?php if($section=='roadmap') echo 'active'; ?>">πŸ—ΊοΈ Roadmap</a>

            

            <div class="nav-section">Liste</div>

            <a href="manage.php?section=services" class="nav-link <?php if($section=='services') echo 'active'; ?>">πŸ› οΈ Servizi</a>

            <a href="manage.php?section=cases" class="nav-link <?php if($section=='cases') echo 'active'; ?>">πŸ“‚ Case Studies</a>

            <a href="manage.php?section=founders" class="nav-link <?php if($section=='founders') echo 'active'; ?>">πŸ‘₯ Team</a>

            <a href="manage.php?section=about_gallery" class="nav-link <?php if($section=='about_gallery') echo 'active'; ?>">πŸ–ΌοΈ Gallery Chi Siamo</a>

            <a href="manage.php?section=pricing" class="nav-link <?php if($section=='pricing') echo 'active'; ?>">πŸ’Ž Pricing</a>

            <a href="manage.php?section=faq" class="nav-link <?php if($section=='faq') echo 'active'; ?>">❓ FAQ</a>



            <div class="nav-section">Altro</div>

            <a href="manage.php?section=stats" class="nav-link <?php if($section=='stats') echo 'active'; ?>">πŸ“Š Statistiche</a>

            <a href="manage.php?section=bottom_stats" class="nav-link <?php if($section=='bottom_stats') echo 'active'; ?>">πŸ“ˆ Big Stats</a>

        </nav>



        <div style="margin-top:auto">

            <a href="index.php?logout=1" class="nav-link" style="color:var(--danger)">πŸšͺ Logout</a>

        </div>

    </aside>



    <!-- Main Content -->

    <main class="main-content">

        <div class="page-header">

            <div>

                <a href="index.php" style="color:var(--muted); font-size:12px; font-weight:700; text-transform:uppercase;">← Dashboard</a>

                <h1 style="text-transform:capitalize">Gestisci <?php echo str_replace('_', ' ', $section); ?></h1>

            </div>

        </div>



        <?php if($section === 'config'): ?>

            <?php if($success): ?>

                <div style="background:rgba(16,185,129,0.1); color:var(--success); padding:16px; border-radius:8px; margin-bottom:24px; border:1px solid var(--success)">

                    βœ… Configurazione salvata con successo!

                </div>

            <?php endif; ?>

            <div class="panel" style="padding:32px; max-width:800px">

                <form method="POST" enctype="multipart/form-data">

                    <?php foreach($items as $key => $val): if($key=='admin_password' || $key=='id') continue; ?>

                    <div class="form-group">

                        <label><?php echo str_replace('_', ' ', $key); ?></label>

                        <?php if(strpos($key, 'image') !== false): ?>

                             <div style="display:flex; align-items:center; gap:12px; margin-bottom:12px; background:var(--bg); padding:10px; border-radius:8px; border:1px solid var(--border)">

                                <img src="../<?php echo htmlspecialchars($val); ?>" style="height:60px; width:60px; object-fit:cover; border-radius:6px;">

                                <div style="font-size:13px; color:var(--muted); word-break:break-all"><?php echo htmlspecialchars($val); ?></div>

                            </div>

                            <input type="file" name="<?php echo $key; ?>" accept="image/*">

                            <input type="hidden" name="<?php echo $key; ?>" value="<?php echo htmlspecialchars($val); ?>">

                        <?php else: ?>

                            <input type="text" name="<?php echo $key; ?>" value="<?php echo htmlspecialchars($val); ?>">

                        <?php endif; ?>

                    </div>

                    <?php endforeach; ?>

                    <button type="submit" class="btn">Salva Modifiche</button>

                </form>

            </div>

        

        <?php else: ?>

            <!-- Add New -->

            <div style="margin-bottom:30px">

                <details>

                    <summary class="btn" style="display:inline-flex; list-style:none">

                        <span>+ Aggiungi Nuovo</span>

                    </summary>

                    <div class="panel" style="margin-top:20px; padding:32px; max-width:700px; animation: fadeIn 0.3s ease">

                        <h3 style="margin-top:0">Nuovo Elemento</h3>

                        <form method="POST" enctype="multipart/form-data">

                             <!-- Fields based on section -->

                            <?php 

                            $fields = [];

                            if($section=='stats') $fields=['label','value','unit','sort_order'];

                            if($section=='services') $fields=['title','icon','description','sort_order'];

                            if($section=='founders') $fields=['name','role','image_path','sort_order'];

                            if($section=='cases') $fields=['title','stats_text','role_text','tags','image_path','sort_order'];

                            if($section=='pricing') $fields=['name','price','features','is_recommended','sort_order'];

                            if($section=='faq') $fields=['question','answer','sort_order'];

                            if($section=='solutions') $fields=['title','description','sort_order'];

                            if($section=='goals') $fields=['title','description','sort_order'];

                            if($section=='roadmap') $fields=['phase','detail','sort_order'];

                            if($section=='bottom_stats') $fields=['value','label','sort_order'];

                            if($section=='about_gallery') $fields=['image_path','sort_order'];

                            

                            foreach($fields as $f): ?>

                            <div class="form-group">

                                <label><?php echo str_replace('_', ' ', $f); ?></label>

                                <?php if($f == 'description' || $f == 'answer' || $f == 'features'): ?>

                                    <textarea name="<?php echo $f; ?>" rows="4"></textarea>

                                <?php elseif($f == 'image_path'): ?>

                                     <input type="file" name="<?php echo $f; ?>" accept="image/*">

                                <?php else: ?>

                                    <input type="text" name="<?php echo $f; ?>" value="<?php echo ($f=='sort_order')?'0':''; ?>">

                                <?php endif; ?>

                            </div>

                            <?php endforeach; ?>

                            <button type="submit" class="btn">Aggiungi Elemento</button>

                        </form>

                    </div>

                </details>

            </div>



            <!-- List Panel -->

            <div class="panel">

                <?php foreach($items as $row): ?>

                <div class="list-item">

                    <?php if(isset($row['image_path'])): ?>

                        <img src="../<?php echo htmlspecialchars($row['image_path']); ?>" class="item-image">

                    <?php endif; ?>

                    

                    <div class="item-content">

                        <span class="item-title">

                            <?php echo htmlspecialchars(reset($row)); // fallback ?> 

                            <?php echo isset($row['title']) ? $row['title'] : (isset($row['name']) ? $row['name'] : (isset($row['label']) ? $row['label'] : (isset($row['question']) ? $row['question'] : ''))); ?>

                        </span>

                        <div class="item-meta">

                            Ordine: <?php echo $row['sort_order']; ?>

                        </div>

                    </div>

                    

                    <div style="display:flex; gap:10px; align-items:center">

                         <details>

                            <summary class="btn btn-outline" style="padding:6px 12px; font-size:12px">Modifica</summary>

                            <div style="position:fixed; inset:0; background:rgba(0,0,0,0.8); z-index:999; display:flex; align-items:center; justify-content:center; padding:20px">

                                <div class="panel" style="width:100%; max-width:600px; padding:32px; position:relative; max-height:90vh; overflow-y:auto">

                                    <div style="position:absolute; top:20px; right:20px; cursor:pointer" onclick="this.closest('details').removeAttribute('open')">βœ•</div>

                                    <h3 style="margin-top:0">Modifica Elemento</h3>

                                    

                                    <form method="POST" enctype="multipart/form-data">

                                        <input type="hidden" name="id" value="<?php echo $row['id']; ?>">

                                        <?php foreach($fields as $f): ?>

                                        <div class="form-group">

                                            <label><?php echo str_replace('_', ' ', $f); ?></label>

                                            

                                            <?php if($f == 'description' || $f == 'answer' || $f == 'features'): ?>

                                                <textarea name="<?php echo $f; ?>" rows="4"><?php echo htmlspecialchars($row[$f]); ?></textarea>

                                            

                                            <?php elseif($f == 'image_path'): ?>

                                                <div style="display:flex; align-items:center; gap:12px; margin-bottom:10px">

                                                    <img src="../<?php echo htmlspecialchars($row[$f]); ?>" style="height:40px; border-radius:4px">

                                                </div>

                                                <input type="file" name="<?php echo $f; ?>" accept="image/*">

                                            

                                            <?php else: ?>

                                                <input type="text" name="<?php echo $f; ?>" value="<?php echo htmlspecialchars($row[$f]); ?>">

                                            <?php endif; ?>

                                        </div>

                                        <?php endforeach; ?>

                                        <button type="submit" class="btn">Salva Modifiche</button>

                                    </form>

                                </div>

                            </div>

                        </details>

                        

                        <a href="?section=<?php echo $section; ?>&delete=<?php echo $row['id']; ?>" class="btn-danger" style="padding:8px 12px; border-radius:6px; font-size:12px; text-decoration:none" onclick="return confirm('Sicuro di voler eliminare?')">Elimina</a>

                    </div>

                </div>

                <?php endforeach; ?>

                

                <?php if(empty($items)): ?>

                    <div style="padding:40px; text-align:center; color:var(--muted)">

                        Nessun elemento presente.

                    </div>

                <?php endif; ?>

            </div>

        <?php endif; ?>

    </main>

</div>



</body>

</html>