118 lines
2.9 KiB
PHP
Raw Normal View History

2013-07-19 18:52:52 +03:00
<?php
2013-08-08 00:22:21 +03:00
/**
2013-08-28 12:02:27 +02:00
* ownCloud - Documents App
2013-08-08 00:22:21 +03:00
*
* @author Victor Dubiniuk
* @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com
*
* This file is licensed under the Affero General Public License version 3 or
* later.
*/
2013-07-19 18:52:52 +03:00
2013-08-28 12:02:27 +02:00
namespace OCA\Documents;
2013-07-19 18:52:52 +03:00
2013-09-27 18:43:10 +03:00
class Db_Op extends Db {
2013-07-20 18:56:09 +03:00
const DB_TABLE = '`*PREFIX*documents_op`';
2013-09-27 18:43:10 +03:00
protected $tableName = '`*PREFIX*documents_op`';
2013-08-06 18:07:05 +03:00
2013-09-27 18:43:10 +03:00
protected $insertStatement = 'INSERT INTO `*PREFIX*documents_op` (`es_id`, `member`, `opspec`) VALUES (?, ?, ?)';
2013-08-07 16:49:52 +03:00
public static function addOpsArray($esId, $memberId, $ops){
2013-08-07 18:02:20 +03:00
$lastSeq = "";
2013-09-27 18:43:10 +03:00
$opObj = new Db_Op();
2013-08-07 16:49:52 +03:00
foreach ($ops as $op) {
2013-09-27 18:43:10 +03:00
$opObj->setData(array(
$esId,
$memberId,
json_encode($op)
));
$opObj->insert();
$lastSeq = $opObj->getLastInsertId();
2013-08-07 16:49:52 +03:00
}
return $lastSeq;
}
2013-09-27 18:43:10 +03:00
2013-08-07 13:04:32 +02:00
/**
* @returns "" when there are no Ops, or the seq of the last Op
*/
2013-09-27 18:43:10 +03:00
public function getHeadSeq($esId){
2013-08-28 15:24:41 +02:00
$query = \OCP\DB::prepare('
SELECT `seq`
2013-09-27 18:43:10 +03:00
FROM ' . $this->tableName . '
2013-08-28 15:24:41 +02:00
WHERE `es_id`=?
ORDER BY `seq` DESC
2013-08-28 15:24:41 +02:00
', 1);
2013-08-06 18:07:05 +03:00
$result = $query->execute(array(
$esId
))
->fetchOne()
;
2013-08-07 16:49:52 +03:00
return !$result ? "" : $result;
}
2013-09-27 18:43:10 +03:00
public function getOpsAfterJson($esId, $seq){
$ops = $this->getOpsAfter($esId, $seq);
2013-08-12 19:07:44 +03:00
if (!is_array($ops)){
$ops = array();
}
2013-08-07 19:01:50 +03:00
$ops = array_map(
2013-08-12 15:53:27 +03:00
function($x){
2013-08-12 19:07:44 +03:00
$decoded = json_decode($x['opspec'], true);
$decoded['memberid'] = strval($decoded['memberid']);
2013-08-12 15:53:27 +03:00
return $decoded;
},
2013-08-07 19:01:50 +03:00
$ops
2013-08-07 16:49:52 +03:00
);
2013-08-07 19:01:50 +03:00
return $ops;
2013-08-06 18:07:05 +03:00
}
2013-09-27 18:43:10 +03:00
public function getOpsAfter($esId, $seq){
2013-08-07 16:49:52 +03:00
if ($seq == ""){
$seq = -1;
}
2013-08-28 15:24:41 +02:00
$query = \OCP\DB::prepare('
SELECT `opspec`
FROM ' . self::DB_TABLE . '
2013-08-28 15:24:41 +02:00
WHERE `es_id`=?
AND `seq`>?
ORDER BY `seq` ASC
');
2013-08-07 13:04:32 +02:00
$result = $query->execute(array($esId, $seq));
2013-08-07 18:02:20 +03:00
return $result->fetchAll();
2013-07-20 18:56:09 +03:00
}
2013-09-27 18:43:10 +03:00
public function removeCursor($esId, $memberId){
2013-11-25 21:26:40 +03:00
$op = '{"optype":"RemoveCursor","memberid":"'. $memberId .'","reason":"server-idle","timestamp":'. time() .'}';
$this->insertOp($esId, $op);
}
public function addMember($esId, $memberId, $fullName, $color, $imageUrl){
$op = '{"optype":"AddMember","memberid":"'. $memberId .'","timestamp":"'. time() .'", "setProperties":{"fullName":"'. $fullName .'","color":"'. $color .'","imageUrl":"'. $imageUrl .'"}}';
$this->insertOp($esId, $op);
}
public function removeMember($esId, $memberId){
$op ='{"optype":"RemoveMember","memberid":"'. $memberId .'","timestamp":'. time() .'}';
$this->insertOp($esId, $op);
}
public function updateMember($esId, $memberId, $fullName, $color, $imageUrl){
//TODO: Follow the spec https://github.com/kogmbh/WebODF/blob/master/webodf/lib/ops/OpUpdateMember.js#L95
$op = '{"optype":"UpdateMember","memberid":"'. $memberId .'","fullName":"'. $fullName .'","color":"'. $color .'","imageUrl":"'. $imageUrl .'","timestamp":'. time() .'}'
;
$this->insertOp($esId, $op);
}
protected function insertOp($esId, $op){
2013-09-27 18:43:10 +03:00
$op = new Db_Op(array(
$esId,
0,
2013-11-25 21:26:40 +03:00
$op
2013-09-27 18:43:10 +03:00
));
$op->insert();
2013-08-17 18:32:24 +03:00
}
2013-11-25 21:26:40 +03:00
2013-07-19 18:52:52 +03:00
}