aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/class/art2.php28
-rw-r--r--app/class/controller.php6
-rw-r--r--app/class/controllerart.php36
-rw-r--r--app/class/routes.php2
-rw-r--r--app/view/templates/edit.php3
-rw-r--r--app/view/templates/layout.php6
-rw-r--r--assets/js/edit.js41
-rw-r--r--assets/js/worker.js37
8 files changed, 154 insertions, 5 deletions
diff --git a/app/class/art2.php b/app/class/art2.php
index 7354e8c..4f04858 100644
--- a/app/class/art2.php
+++ b/app/class/art2.php
@@ -39,6 +39,7 @@ class Art2
protected $affcount;
protected $visitcount;
protected $editcount;
+ protected $editby;
const LEN = 255;
@@ -110,6 +111,7 @@ class Art2
$this->setaffcount(0);
$this->setvisitcount(0);
$this->seteditcount(0);
+ $this->seteditby([]);
}
@@ -441,6 +443,10 @@ class Art2
return $this->editcount;
}
+ public function editby($type = 'array')
+ {
+ return $this->editby;
+ }
@@ -761,6 +767,13 @@ class Art2
}
}
+ public function seteditby($editby)
+ {
+ if(is_array($editby)) {
+ $this->editby = $editby;
+ }
+ }
+
// __________________________________ C O U N T E R S ______________________________
@@ -794,6 +807,21 @@ class Art2
}
}
+ public function addeditby(string $id)
+ {
+ $this->editby[$id] = true;
+ }
+
+ public function removeeditby(string $id)
+ {
+ unset($this->editby[$id]);
+ }
+
+ public function iseditedby()
+ {
+ return count($this->editby) > 0;
+ }
+
}
diff --git a/app/class/controller.php b/app/class/controller.php
index 65d61c3..ce1507a 100644
--- a/app/class/controller.php
+++ b/app/class/controller.php
@@ -79,6 +79,12 @@ class Controller
$this->redirect($this->router->generate($route, []) . $get);
}
+ public function error(int $code)
+ {
+ http_response_code($code);
+ exit;
+ }
+
}
diff --git a/app/class/controllerart.php b/app/class/controllerart.php
index ef720a1..dfdde7f 100644
--- a/app/class/controllerart.php
+++ b/app/class/controllerart.php
@@ -275,6 +275,7 @@ class Controllerart extends Controller
$this->art->hydrate($date);
$this->art->updateedited();
$this->art->addauthor($this->user->id());
+ $this->art->removeeditby($this->user->id());
$this->artmanager->update($this->art);
@@ -293,6 +294,41 @@ class Controllerart extends Controller
$this->routedirect('art');
}
+ /**
+ * This function set the actual editor of the page
+ *
+ * @param string $artid as the page id
+ */
+ public function editby(string $artid)
+ {
+ $this->art = new Art2(['id' => $artid]);
+ if($this->importart($artid)) {
+ $this->art->addeditby($this->user->id());
+ $this->artmanager->update($this->art);
+ echo json_encode(['success' => true]);
+ } else {
+ $this->error(400);
+ }
+ }
+
+ /**
+ * This function remove the actual editor of the page
+ *
+ * @param string $artid as the page id
+ */
+ public function removeeditby(string $artid)
+ {
+ $this->art = new Art2(['id' => $artid]);
+ if($this->importart($artid)) {
+ $this->art->removeeditby($this->user->id());
+ $this->artmanager->update($this->art);
+ echo json_encode(['success' => true]);
+ } else {
+ $this->error(400);
+ }
+ }
+
+
public function movepanels()
{
$_SESSION['workspace']['showrightpanel'] = isset($_POST['workspace']['showrightpanel']);
diff --git a/app/class/routes.php b/app/class/routes.php
index 9f3ce37..f3b9adf 100644
--- a/app/class/routes.php
+++ b/app/class/routes.php
@@ -43,6 +43,8 @@ class Routes
['GET', '/[cid:art]/log', 'Controllerart#log', 'artlog'],
['GET', '/[cid:art]/download', 'Controllerart#download', 'artdownload'],
['POST', '/[cid:art]/edit', 'Controllerart#update', 'artupdate'],
+ ['POST', '/[cid:art]/editby', 'Controllerart#editby', 'arteditby'],
+ ['POST', '/[cid:art]/removeeditby', 'Controllerart#removeeditby', 'artremoveeditby'],
['GET', '/[cid:art]/delete', 'Controllerart#confirmdelete', 'artconfirmdelete'],
['POST', '/[cid:art]/delete', 'Controllerart#delete', 'artdelete'],
['GET', '/[cid:art]/[*]', 'Controllerart#artdirect', 'artread/etoile'],
diff --git a/app/view/templates/edit.php b/app/view/templates/edit.php
index 7c6610a..8d79174 100644
--- a/app/view/templates/edit.php
+++ b/app/view/templates/edit.php
@@ -24,6 +24,9 @@
</main>
+<script>
+ const artid = '<?= $art->id() ?>';
+</script>
<script src="<?= Model::jspath() ?>edit.js"></script>
</body>
diff --git a/app/view/templates/layout.php b/app/view/templates/layout.php
index e0b2e1c..b054791 100644
--- a/app/view/templates/layout.php
+++ b/app/view/templates/layout.php
@@ -12,7 +12,11 @@
<?php } ?>
<title><?= $title ?></title>
<link rel="stylesheet" href="<?= $css ?>">
-
+ <script>
+ // global js vars
+ const basepath = '/<?= Config::basepath() ?>/';
+ const jspath = '<?= Model::jspath() ?>';
+ </script>
</head>
diff --git a/assets/js/edit.js b/assets/js/edit.js
index d838017..ab7471f 100644
--- a/assets/js/edit.js
+++ b/assets/js/edit.js
@@ -1,5 +1,7 @@
let form;
let unsavedChanges = false;
+const arturl = basepath + artid;
+const myWorker = new Worker(jspath + 'worker.js');
window.onload = () => {
form = document.getElementById('update');
@@ -12,6 +14,19 @@ window.onload = () => {
form.onsubmit = submitHandler;
window.onkeydown = keyboardHandler;
window.onbeforeunload = confirmExit;
+ myWorker.onmessage = function (e) {
+ switch (e.data.type) {
+ case 'quitEditing':
+ myWorker.postMessage({ type: 'stillEditing' })
+ break;
+ }
+ }
+
+ myWorker.postMessage({
+ type: 'init',
+ arturl: arturl,
+ });
+ myWorker.postMessage({ type: 'stillEditing' });
};
/**
@@ -39,7 +54,6 @@ function keyboardHandler(e) {
*/
function changeHandler(e) {
unsavedChanges = true;
- // console.log({unsavedChanges});
}
/**
@@ -55,8 +69,27 @@ function submitHandler(e) {
* @param {BeforeUnloadEvent} e
*/
function confirmExit(e) {
- // console.log({unsavedChanges});
if (unsavedChanges) {
- return "You have unsaved changes, do you really want to leave this page?";
+ const url = arturl + '/removeeditby';
+ console.log('send quit editing')
+ fetch(url, { method: 'POST' })
+ .then(handleErrors)
+ .then((response) => {
+ console.log(response);
+ setTimeout(() => {
+ myWorker.postMessage({ type: 'stillEditing' });
+ }, 1500);
+ });
+ return 'You have unsaved changes, do you really want to leave this page?';
+ } else {
+ myWorker.postMessage({ type: 'quitEditing' });
}
-} \ No newline at end of file
+}
+
+async function handleErrors(response) {
+ if (!response.ok) {
+ const data = await response.json();
+ throw Error(`${response.statusText}. ${data.message}`);
+ }
+ return response.json();
+}
diff --git a/assets/js/worker.js b/assets/js/worker.js
new file mode 100644
index 0000000..846acba
--- /dev/null
+++ b/assets/js/worker.js
@@ -0,0 +1,37 @@
+let arturl;
+
+onmessage = function (e) {
+ switch (e.data.type) {
+ case 'init':
+ arturl = e.data.arturl;
+ break;
+ case 'stillEditing':
+ stillEditing();
+ break;
+ case 'quitEditing':
+ quitEditing();
+ break;
+ };
+}
+
+function stillEditing() {
+ console.log('send still editing');
+ const url = arturl + '/editby';
+ const req = new XMLHttpRequest();
+ req.open('POST', url, false);
+ req.send(null);
+
+ const res = JSON.parse(req.responseText);
+ console.log(res);
+}
+
+function quitEditing() {
+ console.log('send quit editing');
+ const url = arturl + '/removeeditby';
+ const req = new XMLHttpRequest();
+ req.open('POST', url, false);
+ req.send(null);
+
+ const res = JSON.parse(req.responseText);
+ console.log(res);
+} \ No newline at end of file