summaryrefslogtreecommitdiffstats
path: root/application/service/multipaste_queue.php
diff options
context:
space:
mode:
authorFlorian Pritz <bluewind@xinu.at>2016-09-06 14:02:31 +0200
committerFlorian Pritz <bluewind@xinu.at>2016-11-01 17:26:31 +0100
commitc553bb64715f40d1755bb84c277442df88e3a925 (patch)
tree21ea1a2d134beece14fe4a4cc949a26a338f780a /application/service/multipaste_queue.php
parent9efd2db81ccf987a5455e0e4575c7d3f9072870f (diff)
Add multipaste queue
Signed-off-by: Florian Pritz <bluewind@xinu.at>
Diffstat (limited to 'application/service/multipaste_queue.php')
-rw-r--r--application/service/multipaste_queue.php85
1 files changed, 85 insertions, 0 deletions
diff --git a/application/service/multipaste_queue.php b/application/service/multipaste_queue.php
new file mode 100644
index 000000000..1836eeacb
--- /dev/null
+++ b/application/service/multipaste_queue.php
@@ -0,0 +1,85 @@
+<?php
+/*
+ * Copyright 2016 Florian "Bluewind" Pritz <bluewind@server-speed.net>
+ *
+ * Licensed under AGPLv3
+ * (see COPYING for full license text)
+ *
+ */
+
+namespace service;
+
+class multipaste_queue {
+
+ public function __construct() {
+ $CI =& get_instance();
+ $CI->load->model("mfile");
+ $CI->load->model("mmultipaste");
+ $this->session = $CI->session;
+ $this->mfile = $CI->mfile;
+ $this->mmultipaste = $CI->mmultipaste;
+ }
+
+ /**
+ * Append ids to the queue
+ *
+ * @param array ids
+ * @return void
+ */
+ public function append(array $ids) {
+ $old_ids = $this->get();
+
+ $old_ids = $this->get();
+
+ # replace multipaste ids with their corresponding paste ids
+ $ids = array_map(function($id) {return array_values($this->resolve_multipaste($id));}, $ids);
+ $ids = array_reduce($ids, function($a, $b) {return array_merge($a, $b);}, []);
+
+ $ids = array_unique(array_merge($old_ids, $ids));
+ $this->set($ids);
+ }
+
+ /**
+ * Return array of ids in a multipaste if the argument id is a multipaste.
+ * Otherwise return an array containing just the argument id.
+ *
+ * @param id
+ * @return array of ids
+ */
+ private function resolve_multipaste($id) {
+ if (strpos($id, "m-") === 0) {
+ if ($this->mmultipaste->valid_id($id)) {
+ return array_map(function($filedata) {return $filedata['id'];}, $this->mmultipaste->get_files($id));
+ }
+ }
+ return [$id];
+ }
+
+ /**
+ * Get the queue
+ *
+ * @return array of ids
+ */
+ public function get() {
+ $ids = $this->session->userdata("multipaste_queue");
+ if ($ids === false) {
+ $ids = [];
+ }
+
+ assert(is_array($ids));
+ return $ids;
+ }
+
+ /**
+ * Set the queue to $ids
+ *
+ * @param array ids
+ * @return void
+ */
+ public function set(array $ids) {
+ $ids = array_filter($ids, function($id) {return $this->mfile->valid_id($id);});
+
+ $this->session->set_userdata("multipaste_queue", $ids);
+ }
+
+}