You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
richdocuments/lib/db/op.php

180 lines
4.1 KiB
PHTML

11 years ago
<?php
11 years ago
/**
* ownCloud - Documents App
11 years ago
*
* @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.
*/
11 years ago
10 years ago
namespace OCA\Documents\Db;
11 years ago
10 years ago
class Op extends \OCA\Documents\Db {
11 years ago
const DB_TABLE = '`*PREFIX*documents_op`';
protected $tableName = '`*PREFIX*documents_op`';
11 years ago
protected $insertStatement = 'INSERT INTO `*PREFIX*documents_op` (`es_id`, `optype`, `member`, `opspec`) VALUES (?, ?, ?, ?)';
11 years ago
public static function addOpsArray($esId, $memberId, $ops){
$lastSeq = "";
10 years ago
$opObj = new Op();
11 years ago
foreach ($ops as $op) {
$opObj->setData(array(
$esId,
$op['optype'],
$memberId,
json_encode($op)
));
$opObj->insert();
$lastSeq = $opObj->getLastInsertId();
11 years ago
}
return $lastSeq;
}
/**
* @returns "" when there are no Ops, or the seq of the last Op
*/
public function getHeadSeq($esId){
$query = \OCP\DB::prepare('
SELECT `seq`
FROM ' . $this->tableName . '
WHERE `es_id`=?
ORDER BY `seq` DESC
', 1);
11 years ago
$result = $query->execute(array(
$esId
))
->fetchOne()
;
11 years ago
return !$result ? "" : $result;
}
public function getOpsAfterJson($esId, $seq){
$ops = $this->getOpsAfter($esId, $seq);
11 years ago
if (!is_array($ops)){
$ops = array();
}
11 years ago
$ops = array_map(
function($x){
11 years ago
$decoded = json_decode($x['opspec'], true);
$decoded['memberid'] = strval($decoded['memberid']);
return $decoded;
},
11 years ago
$ops
11 years ago
);
11 years ago
return $ops;
11 years ago
}
public function getOpsAfter($esId, $seq){
11 years ago
if ($seq == ""){
$seq = -1;
}
$query = \OCP\DB::prepare('
SELECT `opspec`
FROM ' . self::DB_TABLE . '
WHERE `es_id`=?
AND `seq`>?
ORDER BY `seq` ASC
');
$result = $query->execute(array($esId, $seq));
return $result->fetchAll();
11 years ago
}
public function addMember($esId, $memberId, $fullName, $color, $imageUrl){
$op = array(
'optype' => 'AddMember',
'memberid' => (string) $memberId,
'timestamp' => (string) time(),
'setProperties' => array(
'fullName' => $fullName,
'color' => $color,
'imageUrl' => $imageUrl
)
);
$this->insertOp($esId, $memberId, $op);
}
10 years ago
public function removeCursor($esId, $memberId){
$op = array(
'optype' => 'RemoveCursor',
'memberid' => (string) $memberId,
'reason' => 'server-idle',
'timestamp' => (string) time()
);
if ($this->hasOp($esId, $memberId, 'AddCursor') && !$this->hasLastOp($esId, $memberId, 'RemoveCursor')){
$this->insertOp($esId, $memberId, $op);
10 years ago
}
}
public function removeMember($esId, $memberId){
$op = array(
'optype' => 'RemoveMember',
'memberid' => (string) $memberId,
'timestamp' => (string) time()
);
if ($this->hasOp($esId, $memberId, 'AddMember') && !$this->hasLastOp($esId, $memberId, 'RemoveMember')){
$this->insertOp($esId, $memberId, $op);
}
}
//TODO: Implement https://github.com/kogmbh/WebODF/blob/master/webodf/lib/ops/OpUpdateMember.js#L95
public function changeNick($esId, $memberId, $fullName){
$op = array(
'optype' => 'UpdateMember',
'memberid' => (string) $memberId,
'timestamp' => (string) time(),
'setProperties' => array(
'fullName' => $fullName,
)
);
$this->insertOp($esId, $memberId, $op);
}
protected function insertOp($esId, $memberId, $op){
10 years ago
$op = new Op(array(
$esId,
$op['optype'],
$memberId,
json_encode($op)
));
$op->insert();
}
protected function hasLastOp($esId, $memberId, $opType){
$query = \OCP\DB::prepare('
SELECT `opspec`
FROM ' . self::DB_TABLE . '
WHERE `es_id`=?
AND `member`=?
ORDER BY `seq` DESC
',
2,0
);
$result = $query->execute(array($esId, $memberId));
$ops = $result->fetchAll();
foreach ($ops as $op){
$decoded = json_decode($op['opspec'], true);
if ($decoded['optype']==$opType){
return true;
}
}
return false;
}
10 years ago
protected function hasOp($esId, $memberId, $opType){
$ops = $this->execute(
'SELECT * FROM ' . $this->tableName
. ' WHERE `es_id`=? AND `optype`=? AND `member`=?',
array($esId, $opType, $memberId)
);
$result = $ops->fetchAll();
return is_array($result) && count($result)>0;
}
11 years ago
}