diff options
author | Arno Richter <oelna@oelna.de> | 2022-12-21 15:05:28 +0100 |
---|---|---|
committer | Arno Richter <oelna@oelna.de> | 2022-12-21 15:05:28 +0100 |
commit | 482fd7adee5e9e0990bf5904ed7d754d315de649 (patch) | |
tree | 7523a6f3482b6cb024624310f21fa7eeb05e9866 /templates/single.inc.php | |
parent | 057cace8b32e6c3d105695b517eae262071601f4 (diff) | |
download | microblog-482fd7adee5e9e0990bf5904ed7d754d315de649.tar.gz microblog-482fd7adee5e9e0990bf5904ed7d754d315de649.tar.bz2 microblog-482fd7adee5e9e0990bf5904ed7d754d315de649.zip |
first attempt at image attachments!
Diffstat (limited to 'templates/single.inc.php')
-rw-r--r-- | templates/single.inc.php | 89 |
1 files changed, 85 insertions, 4 deletions
diff --git a/templates/single.inc.php b/templates/single.inc.php index b858181..64e7f6d 100644 --- a/templates/single.inc.php +++ b/templates/single.inc.php @@ -39,6 +39,32 @@ // edit post if(!empty($_POST['action']) && $_POST['action'] == 'edit') { + // check changes to attachments + $attached_files = db_get_attached_files($_POST['id']); + if(!empty($attached_files)) { + $files_ids = array_column($attached_files, 'id'); + + if(empty($_POST['attachments'])) { + // remove ALL attachments + $to_remove = $files_ids; + } else { + // remove specified attachments + /* + $to_remove = array_filter($attached_files, function($v) { + return !in_array($v['id'], $_POST['attachments']); + }); + */ + $to_remove = array_diff($files_ids, $_POST['attachments']); + } + + if(count($to_remove) > 0) { + if(!detatch_files($to_remove, $_POST['id'])) { + // could not remove attachments + // var_dump($to_remove); + } + } + } + $result = db_update((int) $_POST['id'], $_POST['content']); if(!$result) { @@ -71,12 +97,41 @@ <li class="single-post" data-post-id="<?= $post['id'] ?>"> <?php if($action == 'edit'): ?> <form action="" method="post" class="edit"> - <textarea name="content" maxlength="<?= $config['max_characters'] ?>"><?= $post['post_content'] ?></textarea> - <p id="count"><?= $config['max_characters'] ?></p> - <input type="hidden" name="action" value="edit" /> <input type="hidden" name="id" value="<?= $post['id'] ?>" /> - <input type="submit" class="button" value="Update this post" /> + + <textarea name="content" maxlength="<?= $config['max_characters'] ?>"><?= $post['post_content'] ?></textarea> + + <div class="post-nav"> + <!--<label id="post-attachments-label">Add Files<input type="file" multiple="multiple" name="attachments[]" id="post-attachments" accept="image/*" /></label> + <div id="post-droparea" class="hidden">Add Files</div>--> + <ul id="post-attachments-list"> + <?php + $attachments = db_get_attached_files($post['id']); + ?> + <?php if(!empty($attachments)): ?> + <?php foreach($attachments as $a): ?> + <?php if(strpos($a['file_mime_type'], 'image') === 0): ?> + <?php + $abs = ROOT.DS.get_file_path($a); + list($width, $height, $_, $size_string) = getimagesize($abs); + $url = $config['url'] .'/'. get_file_path($a); + ?> + <li> + <label> + <input type="checkbox" name="attachments[]" value="<?= $a['id'] ?>" checked /> + <img class="file-preview" src="<?= $url ?>" alt="<?= $a['file_original'] ?>" <?= $size_string ?> loading="lazy" /> + <?= $a['file_original'] ?> + </label> + </li> + <?php else: ?> + <?php endif; ?> + <?php endforeach; ?> + <?php endif; ?> + </ul> + <p id="count"><?= $config['max_characters'] ?></p> + <input type="submit" class="button" value="Update this post" /> + </div> </form> <?php else: ?> <?php @@ -85,6 +140,9 @@ $datetime = date_format($date, 'Y-m-d H:i:s'); $formatted_time = date_format($date, 'M d Y H:i'); + + $attachments = db_get_attached_files($post['id']); + // var_dump($attachments); ?> <span class="post-timestamp"> <time class="published" datetime="<?= $datetime ?>" data-unix-time="<?= $post['post_timestamp'] ?>"><?= $formatted_time ?></time> @@ -103,6 +161,29 @@ </ul><?php endif; ?> </nav> <div class="post-content"><?= nl2br(autolink($post['post_content'])) ?></div> + <?php if(!empty($attachments)): ?> + <ul class="post-attachments"> + <?php foreach($attachments as $a): ?> + <li> + <?php if(strpos($a['file_mime_type'], 'image') === 0): ?> + <?php + $abs = ROOT.DS.get_file_path($a); + list($width, $height, $_, $size_string) = getimagesize($abs); + $url = $config['url'] .'/'. get_file_path($a); + ?> + <a href="<?= $url ?>"> + <picture> + <source srcset="<?= $url ?>" type="image/jpeg" /> + <img src="<?= $url ?>" alt="<?= $a['file_original'] ?>" <?= $size_string ?> loading="lazy" /> + </picture> + </a> + <?php else: ?> + <a href="<?= $url ?>" download="<?= $a['file_original'] ?>"><?= $a['file_original'] ?></a> + <?php endif; ?> + </li> + <?php endforeach; ?> + </ul> + <?php endif; ?> <?php if($action == 'delete'): ?> <form action="" method="post" class="delete"> <input type="hidden" name="action" value="delete" /> |