editing works basically
authorChristian Weiske <cweiske@cweiske.de>
Wed, 28 Mar 2012 15:24:44 +0000 (17:24 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 28 Mar 2012 15:24:44 +0000 (17:24 +0200)
data/templates/edit-file.htm
data/templates/edit.htm
src/Phorkie/File.php
src/Phorkie/Repository.php
www/edit.php
www/index.php

index 66647f076b06460cbdc548488e8510bce2edd9ab..b22314d1540efc25aea3d9f18dc4e5d7bad82e16 100644 (file)
@@ -2,7 +2,8 @@
   <div class="row-fluid">
    <div class="span6">
     <label for="filename_1">Filename</label>
   <div class="row-fluid">
    <div class="span6">
     <label for="filename_1">Filename</label>
-    <input type="text" name="files[{{fileid}}][name]" id="filename_{{fileid}}" value="{{ file.getFilename }}"/>
+    <input type="hidden" name="files[{{fileid}}][original_name]" value="{{file.getFilename}}"/>
+    <input type="text" name="files[{{fileid}}][name]" id="filename_{{fileid}}" value="{{file.getFilename}}"/>
    </div>
    <div class="span6" style="text-align: right">
     <label for="type_{{fileid}}">Type</label>
    </div>
    <div class="span6" style="text-align: right">
     <label for="type_{{fileid}}">Type</label>
@@ -14,6 +15,6 @@
     </select>
    </div>
   </div>
     </select>
    </div>
   </div>
-  <textarea name="files[{{fileid}}][content]" id="content_1" cols="80" rows="10" class="content">{{ file.getContent }}</textarea>
+  <textarea name="files[{{fileid}}][content]" id="content_{{fileid}}" cols="80" rows="10" class="content">{{file.getContent}}</textarea>
  </div>
 
  </div>
 
index 56e08ef1d1535080da35e57f602e2e3a6b42c449..300b6cbc9a2d3481a6b1f6f72c368098136eb864 100644 (file)
@@ -2,7 +2,7 @@
 {% block title %}Edit paste{% endblock %}
 
 {% block content %}
 {% block title %}Edit paste{% endblock %}
 
 {% block content %}
-<form method="post" action="/">
+<form method="post" action="{{repo.getLink('edit')}}">
  <div class="control-group well pastedata">
    <label for="description">Description</label>
    <input type="text" name="description" id="description" value="{{repo.getDescription}}"/>
  <div class="control-group well pastedata">
    <label for="description">Description</label>
    <input type="text" name="description" id="description" value="{{repo.getDescription}}"/>
index 6cdc8333617b98c000f15f50ef49f6f46f201f0e..3c6ea4b42e687b532563e54633a4557a4360b9fe 100644 (file)
@@ -58,6 +58,16 @@ class File
         return basename($this->path);
     }
 
         return basename($this->path);
     }
 
+    /**
+     * Return the full path to the file
+     *
+     * @return string
+     */
+    public function getPath()
+    {
+        return $this->path;
+    }
+
     /**
      * Get file extension without dot
      *
     /**
      * Get file extension without dot
      *
index aeccc72f826107a2465a4a9d4023490bc6b83175..5273b60a99d5105dab3f1eb22e319304f915b7ed 100644 (file)
@@ -82,6 +82,9 @@ class Repository
         if ($base != $name) {
             throw new Exception('No directories supported for now');
         }
         if ($base != $name) {
             throw new Exception('No directories supported for now');
         }
+        if ($name == '') {
+            throw new Exception_Input('Empty file name given');
+        }
         $path = $this->repoDir . '/' . $base;
         if (!is_readable($path)) {
             throw new Exception_Input('File does not exist');
         $path = $this->repoDir . '/' . $base;
         if (!is_readable($path)) {
             throw new Exception_Input('File does not exist');
@@ -89,6 +92,16 @@ class Repository
         return new File($path, $this);
     }
 
         return new File($path, $this);
     }
 
+    public function hasFile($name)
+    {
+        try {
+            $this->getFileByName($name);
+        } catch (Exception $e) {
+            return false;
+        }
+        return true;
+    }
+
     public function getDescription()
     {
         if (!is_readable($this->repoDir . '/.git/description')) {
     public function getDescription()
     {
         if (!is_readable($this->repoDir . '/.git/description')) {
@@ -97,6 +110,11 @@ class Repository
         return file_get_contents($this->repoDir . '/.git/description');
     }
 
         return file_get_contents($this->repoDir . '/.git/description');
     }
 
+    public function setDescription($description)
+    {
+        file_put_contents($this->repoDir . '/.git/description', $description);
+    }
+
     /**
      * Get a link to the repository
      *
     /**
      * Get a link to the repository
      *
index d86df41f2b45ccd2577a71f6d42637156a2a7f49..3bcec6e797525a5c995920835ca8424a64e7cce6 100644 (file)
@@ -8,6 +8,48 @@ require_once 'www-header.php';
 $repo = new Repository();
 $repo->loadFromRequest();
 
 $repo = new Repository();
 $repo->loadFromRequest();
 
+if (isset($_POST['files'])) {
+    $vc = $repo->getVc();
+    $repo->setDescription($_POST['description']);
+
+    $bChanged = false;
+    foreach ($_POST['files'] as $num => $arFile) {
+        if (!isset($arFile['original_name'])
+            || !$repo->hasFile($arFile['original_name'])
+        ) {
+            //FIXME: Show error message
+            continue;
+        }
+        //FIXME: fix file names from .. and ./
+        if ($arFile['original_name'] != $arFile['name']) {
+            //FIXME: what to do with overwrites?
+            $vc->getCommand('mv')
+                ->addArgument($arFile['original_name'])
+                ->addArgument($arFile['name'])
+                ->execute();
+            $bChanged = true;
+        }
+        $file = $repo->getFileByName($arFile['name']);
+        if ($file->getContent() != $arFile['content']) {
+            file_put_contents($file->getPath(), $arFile['content']);
+            $command = $vc->getCommand('add')
+                ->addArgument($file->getFilename())
+                ->execute();
+            $bChanged = true;
+        }
+    }
+
+    if ($bChanged) {
+        $vc->getCommand('commit')
+            ->setOption('message', '')
+            ->setOption('allow-empty-message')
+            ->setOption('author', 'Anonymous <anonymous@phorkie>')
+            ->execute();
+    }
+
+    redirect($repo->getLink('display'));
+}
+
 render(
     'edit',
     array(
 render(
     'edit',
     array(
index f821c840d1e6c3cfd7cee4ecd3c21d41eb542949..db3fd7bf42b12434de4a4ac1a955445d447c78b1 100644 (file)
@@ -21,10 +21,11 @@ if (isset($_POST['files'])) {
     foreach (glob($repo->repoDir . '/.git/hooks/*') as $hookfile) {
         unlink($hookfile);
     }
     foreach (glob($repo->repoDir . '/.git/hooks/*') as $hookfile) {
         unlink($hookfile);
     }
-    file_put_contents($repo->repoDir . '.git/description', $_POST['description']);
+    $repo->setDescription($_POST['description']);
 
     foreach ($_POST['files'] as $num => $arFile) {
         if ($arFile['name'] != '') {
 
     foreach ($_POST['files'] as $num => $arFile) {
         if ($arFile['name'] != '') {
+            //FIXME: fix file name from ..
             $fname = $arFile['name'];
         } else {
             $fname = 'phork' . $num . '.' . $arFile['type'];
             $fname = $arFile['name'];
         } else {
             $fname = 'phork' . $num . '.' . $arFile['type'];