diff options
author | steckbrief <steckbrief@chefmail.de> | 2017-12-28 13:46:55 +0100 |
---|---|---|
committer | steckbrief <steckbrief@chefmail.de> | 2017-12-28 13:46:55 +0100 |
commit | 391875ea8389d043360961cf2c5a69f2b6d7a91a (patch) | |
tree | 7bd8c535cd8c6912f34c193ece35a2efcd73530c | |
parent | 31ba0cea75b128a664e777a0a2f4180a19501ec3 (diff) |
-rw-r--r-- | deleteCharacter.php | 22 | ||||
-rw-r--r-- | deleteStory.php | 21 | ||||
-rw-r--r-- | editCharacter.php | 44 | ||||
-rw-r--r-- | editStory.php | 40 | ||||
-rw-r--r-- | index.php | 86 | ||||
-rw-r--r-- | js/stories.js | 4 | ||||
m--------- | lib/commons | 0 | ||||
-rw-r--r-- | lib/functions.stories.inc.php | 30 | ||||
-rw-r--r-- | lib/new-character-dialog.inc.php | 14 | ||||
-rw-r--r-- | lib/new-story-dialog.inc.php | 14 |
10 files changed, 256 insertions, 19 deletions
diff --git a/deleteCharacter.php b/deleteCharacter.php new file mode 100644 index 0000000..33e0b9b --- /dev/null +++ b/deleteCharacter.php @@ -0,0 +1,22 @@ +<?php +require_once(__DIR__.'/lib/commons/functions.common.inc.php'); +require_once(__DIR__.'/lib/commons/functions.http.inc.php'); +require_once(__DIR__.'/lib/commons/functions.files.inc.php'); +require_once(__DIR__.'/lib/functions.stories.inc.php'); + +//print_r($_POST);exit(); + +$storyName = getMandatoryPostParameter('story-name', NULL, true); +$charName = getMandatoryPostParameter('char-name', NULL, true); + +$pathToChar = generatePath(['stories', $storyName, $charName], __DIR__); +echo $pathToChar; +if (dir_exists($pathToChar)) { + rm_dir($pathToChar, TRUE); + echo "dir deleted"; +} else { + echo "dir does not exist"; + return 1; +} + +?> diff --git a/deleteStory.php b/deleteStory.php new file mode 100644 index 0000000..17a4901 --- /dev/null +++ b/deleteStory.php @@ -0,0 +1,21 @@ +<?php +require_once(__DIR__.'/lib/commons/functions.common.inc.php'); +require_once(__DIR__.'/lib/commons/functions.http.inc.php'); +require_once(__DIR__.'/lib/commons/functions.files.inc.php'); +require_once(__DIR__.'/lib/functions.stories.inc.php'); + +//print_r($_POST);exit(); + +$storyName = getMandatoryPostParameter('story-name', NULL, true); + +$pathToStory = generatePath(['stories', $storyName], __DIR__); + +if (dir_exists($pathToStory)) { + rm_dir($pathToStory, TRUE); + echo "dir deleted"; +} else { + echo "dir does not exist"; + return 1; +} + +?> diff --git a/editCharacter.php b/editCharacter.php new file mode 100644 index 0000000..eb9bee3 --- /dev/null +++ b/editCharacter.php @@ -0,0 +1,44 @@ +<?php +/* + * + * createStory.php + * + */ + +require_once(__DIR__.'/lib/commons/functions.common.inc.php'); +require_once(__DIR__.'/lib/commons/functions.http.inc.php'); +require_once(__DIR__.'/lib/commons/functions.files.inc.php'); +require_once(__DIR__.'/lib/functions.stories.inc.php'); + +//print_r($_POST);exit(); + +$storyName = getMandatoryPostParameter('story-name', NULL, true); +$charName = getMandatoryPostParameter('char-name', NULL, true); +$charPassphoto = getOptionalFileParameter('char-passphoto'); +$charDescription = getOptionalPostParameter('char-description'); +$action = getMandatoryPostParameter('char-action'); + +//$pathToChar = __DIR__.DIRECTORY_SEPARATOR.'stories'.DIRECTORY_SEPARATOR.generateDirectoryName($storyName).DIRECTORY_SEPARATOR.generateDirectoryName($charName); + +$pathToChar = generatePath(['stories', $storyName, $charName], __DIR__); +if ('edit' == $action) { + $result = editEntity($pathToChar, $charDescription, $charPassphoto, 'passphoto.png'); +} else { + $result = createEntity($pathToChar, $charDescription, $charPassphoto, 'passphoto.png'); +} +if (TRUE !== $result) { + switch ($result) { + case 1: + sendHttpReturnCodeAndJson(409, 'Character exists already'); + break; + case 2: + sendHttpReturnCodeAndJson(500, 'Could not create directory for character'); + break; + default: + sendHttpReturnCodeAndJson(500, 'Unexpected error'); + } +} else { + sendHttpReturnCodeAndMessage(201); +} + +?> diff --git a/editStory.php b/editStory.php new file mode 100644 index 0000000..2e8284d --- /dev/null +++ b/editStory.php @@ -0,0 +1,40 @@ +<?php +/* + * + * createStory.php + * + */ + +require_once(__DIR__.'/lib/commons/functions.common.inc.php'); +require_once(__DIR__.'/lib/commons/functions.http.inc.php'); +require_once(__DIR__.'/lib/commons/functions.files.inc.php'); +require_once(__DIR__.'/lib/functions.stories.inc.php'); + +$storyName = getMandatoryPostParameter('story-name', '', true); +$storyLogoFile = getOptionalFileParameter('story-logo'); +$storyDescription = getOptionalPostParameter('story-description'); +$action = getMandatoryPostParameter('story-action'); + +$pathToStory = generatePath(['stories', $storyName], __DIR__); + +if ('edit' == $action) { + $result = editEntity($pathToStory, $storyDescription, $storyLogoFile, 'logo.png'); +} else { + $result = createEntity($pathToStory, $storyDescription, $storyLogoFile, 'logo.png'); +} +if (TRUE !== $result) { + switch ($result) { + case 1: + sendHttpReturnCodeAndJson(409, 'Story exists already'); + break; + case 2: + sendHttpReturnCodeAndJson(500, 'Could not create directory for story'); + break; + default: + sendHttpReturnCodeAndJson(500, 'Unexpected error'); + } +} else { + sendHttpReturnCodeAndMessage(201); +} + +?> @@ -2,6 +2,7 @@ include_once(__DIR__.'/lib/commons/functions.common.inc.php'); include_once(__DIR__.'/lib/commons/functions.http.inc.php'); +require_once(__DIR__.'/lib/commons/functions.files.inc.php'); include_once(__DIR__.'/lib/functions.stories.inc.php'); $config = require(__DIR__.'/config/config.inc.php'); @@ -44,6 +45,7 @@ ksort($stories, SORT_STRING); $currentStoryName = visibleName($currentStory); function printEntities($entities, $storyName = NULL) { + global $isAdmin, $isAuthenticated; foreach ($entities as $entity=>$description) { $name = visibleName($entity); $imgName = (NULL != $storyName) ? 'passphoto.png' : 'logo.png'; @@ -56,8 +58,18 @@ function printEntities($entities, $storyName = NULL) { <img class="img-fluid rounded mb-3 mb-md-0 figure-pic" src="<?=$imgPath.'/'.$imgName;?>" alt="<?=$name;?>" title="<?=$name;?>"> </div> <div class="col-md-5"> - <h3><?=$name;?></h3> - <p class="figure-desc"><?=$description;?></p> + <h3 id="name-<?=str_replace(' ', '-', $name);?>"> + <?=$name;?> +<?php +if ($isAuthenticated && $isAdmin) { +?> +<button <?=((NULL != $storyName) ? ' data-storyname="'.$storyName.'"' : '');?> data-entityname="<?=$name;?>" class="delete-btn">-</button> +<button <?=((NULL != $storyName) ? ' data-storyname="'.$storyName.'"' : '');?> data-entityname="<?=$name;?>" class="edit-btn">e</button> +<?php +} +?> + </h3> + <p class="figure-desc" id="desc-<?=str_replace(' ', '-', $name);?>"><?=$description;?></p> </div> </div> <!-- /.row --> @@ -76,6 +88,74 @@ function printEntities($entities, $storyName = NULL) { <script src="https://www.sl-its.de/js/jquery-1.11.2.min.js"></script> <script src="js/bootstrap.min.js"></script> <script src="js/stories.js"></script> + <script> + function editElement(elem) { + var storyName = $(elem).data('storyname'); + var entityName = $(elem).data('entityname'); + var data = new FormData(); + + var desc = $('#desc-' + entityName.replace(' ', '-')).text(); + var modalName = 'newStoryModal'; + var prefix = 'story'; + var modalTitle = 'Kindergeschichte bearbeiten'; + + if (storyName) { + modalName = 'newCharacterModal'; + prefix = 'char'; + modalTitle = 'Charakter bearbeiten'; + } + $('#' + modalName).modal('show'); + $('#' + prefix + '-description').val(desc); + $('#' + prefix + '-name').val(entityName); + $('#' + prefix + '-action').val('edit'); + $('#modal-title').text(modalTitle); + $('#create-' + prefix + '-btn').text('Bearbeiten'); + } + + $(document).ready(function() { + $('.delete-btn').click(function() { + if (confirm('Bist Du sicher?')) { + var storyName = $(this).data('storyname'); + var entityName = $(this).data('entityname'); + var url = ''; + var data = new FormData(); + + if (storyName) { + url = 'deleteCharacter.php'; + data.append('story-name', storyName); + data.append('char-name', entityName); + } else { + url = 'deleteStory.php'; + data.append('story-name', entityName); + } + jQuery.ajax({ + url: url, + data: data, + cache: false, + contentType: false, + processData: false, + method: 'POST' + }).done(function(data, textStatus) { + console.log('successfully deleted'); + location.reload(); + }).fail(function(jqXHR, textStatus) { + var errorObj = jqXHR.responseJSON; + switch (jqXHR.status) { + case 400: + console.log(errorObj.msg + ' ' + errorObj.parameters['missing_parameter']); + break; + } + console.log('error deleting entity'); + console.log(jqXHR); + console.log(textStatus); + }); + } + }); + $('.edit-btn').click(function() { + editElement(this); + }); + }); + </script> <style type="text/css"> .figure-desc { text-align: justify; @@ -147,7 +227,7 @@ function printEntities($entities, $storyName = NULL) { </div> </div> <?php - printEntities($characters); + printEntities($characters, $currentStory); } else { printEntities($stories); } diff --git a/js/stories.js b/js/stories.js index dcab144..9938bec 100644 --- a/js/stories.js +++ b/js/stories.js @@ -1,13 +1,13 @@ function createNewCharacter() { var formName = 'create-char-form'; - var url = 'createCharacter.php'; + var url = 'editCharacter.php'; var modalName = 'newCharModal'; create(formName, url, modalName); } function createNewStory() { var formName = 'create-story-form'; - var url = 'createStory.php'; + var url = 'editStory.php'; var modalName = 'newStoryModal'; create(formName, url, modalName); } diff --git a/lib/commons b/lib/commons -Subproject b28e7e4125cd8ce0f25dbcaaad14a2ef2446cdb +Subproject 406f05cd56a6b4d8192ea00b15bb9edef68c733 diff --git a/lib/functions.stories.inc.php b/lib/functions.stories.inc.php index 5e0527e..8f35e27 100644 --- a/lib/functions.stories.inc.php +++ b/lib/functions.stories.inc.php @@ -13,20 +13,30 @@ function generateDirectoryName($name) { return urlencode($name); } +function saveEntity($path, $description, $file, $filename) { + // create description file + if (file_put_contents($path.DIRECTORY_SEPARATOR.'description.txt', $description) === FALSE) { + // Error on writing description file + } + // move uploaded logo + if (NULL === $file || move_uploaded_file($file['tmp_name'], $path.DIRECTORY_SEPARATOR.$filename) || $file['error'] == UPLOAD_ERR_NO_FILE) { + return TRUE; + } else { + return 3; + } +} + +function editEntity($path, $description, $file, $filename) { + if (dir_exists($path)) { + return saveEntity($path, $description, $file, $filename); + } +} + function createEntity($path, $description, $file, $filename) { if (!dir_exists($path)) { // create directory for story if (mkdir($path)) { - // create description file - if (file_put_contents($path.DIRECTORY_SEPARATOR.'description.txt', $description) === FALSE) { - // Error on writing description file - } - // move uploaded logo - if (NULL === $file || move_uploaded_file($file['tmp_name'], $path.DIRECTORY_SEPARATOR.$filename) || $file['error'] == UPLOAD_ERR_NO_FILE) { - return TRUE; - } else { - return 3; - } + return saveEntity($path, $description, $file, $filename); } else { // Error on directory creation // return 'Could not create directory "'.$path; diff --git a/lib/new-character-dialog.inc.php b/lib/new-character-dialog.inc.php index 5cf25c0..0e77ad1 100644 --- a/lib/new-character-dialog.inc.php +++ b/lib/new-character-dialog.inc.php @@ -10,7 +10,7 @@ <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> - <h4 class="modal-title">Neuen Charakter anlegen</h4> + <h4 id="modal-title" class="modal-title">Neuen Charakter anlegen</h4> </div> <div class="modal-body"> <form action="" method="post" enctype="multipart/form-data" id="create-char-form"> @@ -27,7 +27,8 @@ <label for="story-logo">Foto des Charakters:</label> <input type="file" name="char-passphoto" id="char-passphoto"> </div> - <button type="submit" class="btn btn-info btn-sm">Anlegen</button> + <input type="hidden" value="create" id="char-action" name="char-action"> + <button type="submit" id="create-char-btn" class="btn btn-info btn-sm">Anlegen</button> </form> </div> </div> @@ -38,4 +39,13 @@ createNewCharacter(); event.preventDefault(); }); + $('#newCharacterModal').on('hide.bs.modal', function(evt) { + formReset(); + }); + function formReset() { + $('#create-char-form')[0].reset(); + $('#char-action').val('create'); + $('#modal-title').text('Neuen Charakter anlegen'); + $('#create-char-btn').text('Anlegen'); + } </script> diff --git a/lib/new-story-dialog.inc.php b/lib/new-story-dialog.inc.php index 73c6abf..c833933 100644 --- a/lib/new-story-dialog.inc.php +++ b/lib/new-story-dialog.inc.php @@ -10,7 +10,7 @@ <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">×</button> - <h4 class="modal-title">Neue Kindergeschichte anlegen</h4> + <h4 id="modal-title" class="modal-title">Neue Kindergeschichte anlegen</h4> </div> <div class="modal-body"> <form action="" method="post" enctype="multipart/form-data" id="create-story-form"> @@ -30,7 +30,8 @@ <label for="story-header">Header Bild der Kindergeschichte:</label> <input type="file" name="story-header" id="story-header"> </div> - <button type="submit" class="btn btn-info btn-sm">Anlegen</button> + <input type="hidden" value="create" name="story-action" id="story-action"> + <button type="submit" id="create-story-btn" class="btn btn-info btn-sm">Anlegen</button> </form> </div> </div> @@ -41,4 +42,13 @@ createNewStory(); event.preventDefault(); }); + $('#newCharacterModal').on('hide.bs.modal', function(evt) { + formReset(); + }); + function formReset() { + $('#create-story-form')[0].reset(); + $('#story-action').val('create'); + $('#modal-title').text('Neue Kindergeschichte anlegen'); + $('#create-story-btn').text('Anlegen'); + } </script> |