Add database management page
authorChristian Weiske <cweiske@cweiske.de>
Tue, 14 Oct 2014 21:24:40 +0000 (23:24 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Tue, 14 Oct 2014 21:24:40 +0000 (23:24 +0200)
appinfo/routes.php
controller/guicontroller.php
grauphel.css
lib/notestorage.php
templates/appnavigation.php
templates/gui-database.php [new file with mode: 0644]

index 28ba16d185e4426f472d36f50dab04c8480491bd..19a4b930092d9107cfab4e09eb255c9454916e29 100644 (file)
@@ -73,6 +73,16 @@ $application->registerRoutes(
                 'name' => 'gui#tokens',
                 'verb' => 'GET',
             ),
+            array(
+                'url'  => '/database',
+                'name' => 'gui#database',
+                'verb' => 'GET',
+            ),
+            array(
+                'url'  => '/database',
+                'name' => 'gui#databaseReset',
+                'verb' => 'POST',
+            ),
 
             array(
                 'url'  => '/tokens/{username}/{tokenKey}',
index 58768f2e880883418be36d1ecf8613684bfa2ecb..6f0a15c821dba9139fc62beb025c9ae7e9c932fb 100644 (file)
@@ -119,6 +119,41 @@ class GuiController extends Controller
         return $res;
     }
 
+    /**
+     * Allow the user to clear his database
+     *
+     * @NoAdminRequired
+     * @NoCSRFRequired
+     */
+    public function database($reset = null)
+    {
+        $res = new TemplateResponse('grauphel', 'gui-database');
+        $res->setParams(array('reset' => $reset));
+        $this->addNavigation($res, null);
+        $this->addStats($res);
+
+        return $res;
+    }
+
+    /**
+     * Resets the database by deleting all notes and deleting the user's
+     * sync data.
+     *
+     * @NoAdminRequired
+     */
+    public function databaseReset()
+    {
+        $reset = false;
+        if ($_POST['username'] != '' && $_POST['username'] == $this->user->getUid()) {
+            $notes = $this->getNotes();
+            $notes->deleteAll();
+            $notes->deleteSyncData();
+            $reset = true;
+        }
+
+        return $this->database($reset);
+    }
+
     protected function addNavigation(TemplateResponse $res, $selectedRawtag = null)
     {
         $nav = new \OCP\Template('grauphel', 'appnavigation', '');
index cac269cab315cadf9e89ac1d87e44ee8dc5f70e0..6ba1cde0432139af86490d160a93e1118e87dd68 100644 (file)
     text-decoration: underline;
 }
 
+.app-grauphel #app-content .error {
+    color: red;
+}
+.app-grauphel #app-content .success {
+    color: green;
+}
+
 .app-grauphel .oauth-authorize {
     margin: 2ex;
     text-align: center;
index c6659034e8da7d337bca551b43cf32cf126dcac6..fb6803029f780b8a00b31c8f6b54685d40daf435 100644 (file)
@@ -189,6 +189,22 @@ class NoteStorage
         \OC_DB::executeAudited($sql, $params);
     }
 
+    /**
+     * Delete synchronization data for the given user.
+     *
+     * @param SyncData $syncdata Synchronization data object
+     *
+     * @return void
+     */
+    public function deleteSyncData()
+    {
+        \OC_DB::executeAudited(
+            'DELETE FROM `*PREFIX*grauphel_syncdata`'
+            . ' WHERE `syncdata_user` = ?',
+            array($this->username)
+        );
+    }
+
     /**
      * Load a note from the storage.
      *
@@ -280,6 +296,20 @@ class NoteStorage
         );
     }
 
+    /**
+     * Delete all notes from storage.
+     *
+     * @return void
+     */
+    public function deleteAll()
+    {
+        \OC_DB::executeAudited(
+            'DELETE FROM `*PREFIX*grauphel_notes`'
+            . ' WHERE `note_user` = ?',
+            array($this->username)
+        );
+    }
+
     /**
      * Load notes for the given user in short form.
      * Optionally only those changed after $since revision
index 8b15238534613da2c69f60380825671e8487d8da..b0657e113b8f64efa0f55721308d145b56e095bf 100644 (file)
@@ -14,6 +14,7 @@
         <li><a href="<?php p(OCP\Util::linkToRoute('grauphel.gui.index')); ?>">Info and stats</a></li>
       <?php if (OCP\User::isLoggedIn()) { ?>
         <li><a href="<?php p(OCP\Util::linkToRoute('grauphel.gui.tokens')); ?>">Manage access tokens</a></li>
+        <li><a href="<?php p(OCP\Util::linkToRoute('grauphel.gui.database')); ?>">Manage database</a></li>
       <?php } ?>
       </ul>
     </div>
diff --git a/templates/gui-database.php b/templates/gui-database.php
new file mode 100644 (file)
index 0000000..6fc057a
--- /dev/null
@@ -0,0 +1,40 @@
+<link rel="stylesheet" href="<?php p(OCP\Util::linkTo('grauphel','grauphel.css')); ?>" type="text/css"/>
+
+<?php /** @var $l OC_L10N */ ?>
+<?php $_['appNavigation']->printPage(); ?>
+
+<script type="text/javascript" src="<?php p(OCP\Util::linkTo('grauphel','js/grauphel.js')); ?>"></script>
+
+<div id="app-content" class="content">
+  <h1>Manage database</h1>
+  <p>
+    In case something went seriously wrong during synchronization, you may reset your
+    database.
+    It will delete all your notes and the synchronization status - as if you
+    never synced to this server before.
+  </p>
+
+  <?php isset($_['stats']) && $_['stats']->printPage(); ?>
+
+  <h2>Reset database</h2>
+  <?php if ($_['reset'] === true) { ?>
+  <p class="success">
+   Database has been reset!
+  </p>
+  <?php } else if ($_['reset'] === false) { ?>
+  <p class="error">
+   Database has <b>not</b> been reset!
+  </p>
+  <?php } ?>
+
+  <p>
+   To reset the database, enter your user name and click "reset database":
+  </p>
+  <form method="POST" action="<?php p(OCP\Util::linkToRoute('grauphel.gui.databaseReset')); ?>">
+    <input type="hidden" name="requesttoken" value="<?php p($_['requesttoken']) ?>"/>
+    <p>
+     <label>Username: <input type="text" name="username" value="" autocomplete="off" /></label>
+     <button type="submit">Reset database</button>
+    </p>
+  </form>
+</div>