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 | |
parent | 057cace8b32e6c3d105695b517eae262071601f4 (diff) | |
download | microblog-482fd7adee5e9e0990bf5904ed7d754d315de649.tar.gz microblog-482fd7adee5e9e0990bf5904ed7d754d315de649.tar.bz2 microblog-482fd7adee5e9e0990bf5904ed7d754d315de649.zip |
first attempt at image attachments!
Diffstat (limited to 'templates')
-rw-r--r-- | templates/postform.inc.php | 19 | ||||
-rw-r--r-- | templates/single.inc.php | 89 | ||||
-rw-r--r-- | templates/timeline.inc.php | 30 |
3 files changed, 130 insertions, 8 deletions
diff --git a/templates/postform.inc.php b/templates/postform.inc.php index 149028b..df7566c 100644 --- a/templates/postform.inc.php +++ b/templates/postform.inc.php @@ -10,7 +10,7 @@ $message = array(); if(!empty($_POST['content'])) { - + $id = db_insert($_POST['content'], NOW); if($id > 0) { @@ -19,6 +19,11 @@ 'message' => 'Successfully posted status #'.$id ); + // handle files + if(!empty($_FILES['attachments'])) { + attach_uploaded_files($_FILES['attachments'], $id); + } + rebuild_feeds(); if($config['ping'] == true) ping_microblog(); if($config['crosspost_to_twitter'] == true) { @@ -43,10 +48,16 @@ <?php if(isset($message['status']) && isset($message['message'])): ?> <p class="message <?= $message['status'] ?>"><?= $message['message'] ?></p> <?php endif; ?> - <form action="" method="post"> + <form action="" method="post" enctype="multipart/form-data" id="post-new-form" data-redirect="<?= $config['url'] ?>"> <textarea name="content" maxlength="<?= $config['max_characters'] ?>"></textarea> - <p id="count"><?= $config['max_characters'] ?></p> - <input type="submit" name="" value="Post" /> + + <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"></ul> + <p id="count"><?= $config['max_characters'] ?></p> + <input type="submit" name="" value="Post" /> + </div> </form> </div> <?php require(ROOT.DS.'snippets'.DS.'footer.snippet.php'); ?> 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" /> diff --git a/templates/timeline.inc.php b/templates/timeline.inc.php index bc06de9..6dd37c1 100644 --- a/templates/timeline.inc.php +++ b/templates/timeline.inc.php @@ -36,6 +36,8 @@ $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']); ?> <a class="post-timestamp" href="<?= $config['url'] ?>/<?= $post['id'] ?>"> <time class="published" datetime="<?= $datetime ?>" data-unix-time="<?= $post['post_timestamp'] ?>"><?= $formatted_time ?></time> @@ -50,6 +52,34 @@ </ul><?php endif; ?> </nav> <div class="post-content"><?= nl2br(autolink($post['post_content'])) ?></div> + <?php if(!empty($attachments)): ?> + <?php + $attachments_total = count($attachments); + // only display the first attachment on the timeline + array_splice($attachments, 1); + ?> + <ul class="post-attachments"> + <?php foreach($attachments as $a): ?> + <li title="<?= ($attachments_total > 1) ? 'and '.($attachments_total-1).' more' : '' ?>"> + <?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="<?= $config['url'] ?>/<?= $post['id'] ?>"> + <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; ?> </li> <?php endforeach; ?> </ul> |