move to PSR-4
This commit is contained in:
61
lib/Db/Acl.php
Normal file
61
lib/Db/Acl.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
// db/author.php
|
||||
namespace OCA\Deck\Db;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
class Acl extends Entity implements JsonSerializable {
|
||||
|
||||
public $id;
|
||||
protected $participant;
|
||||
protected $type;
|
||||
protected $boardId;
|
||||
protected $permissionWrite;
|
||||
protected $permissionInvite;
|
||||
protected $permissionManage;
|
||||
protected $owner;
|
||||
|
||||
public function __construct() {
|
||||
$this->addType('id','integer');
|
||||
$this->addType('boardId','integer');
|
||||
$this->addType('permissionWrite', 'boolean');
|
||||
$this->addType('permissionInvite', 'boolean');
|
||||
$this->addType('permissionManage', 'boolean');
|
||||
$this->addType('owner', 'boolean');
|
||||
$this->addRelation('owner');
|
||||
}
|
||||
public function jsonSerialize() {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'participant' => $this->participant,
|
||||
'type' => $this->type,
|
||||
'boardId' => $this->boardId,
|
||||
'permissionWrite' => $this->permissionWrite,
|
||||
'permissionInvite' => $this->permissionInvite,
|
||||
'permissionManage' => $this->permissionManage,
|
||||
'owner' => $this->owner
|
||||
];
|
||||
}
|
||||
}
|
||||
52
lib/Db/AclMapper.php
Normal file
52
lib/Db/AclMapper.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Deck\Db;
|
||||
|
||||
use OCP\AppFramework\Db\Entity;
|
||||
use OCP\IDb;
|
||||
use OCP\AppFramework\Db\Mapper;
|
||||
|
||||
|
||||
class AclMapper extends DeckMapper {
|
||||
|
||||
public function __construct(IDb $db) {
|
||||
parent::__construct($db, 'deck_board_acl', '\OCA\Deck\Db\Acl');
|
||||
}
|
||||
|
||||
public function findAll($boardId, $limit=null, $offset=null) {
|
||||
$sql = 'SELECT id, board_id, type, participant, permission_write, permission_invite, permission_manage, 0 as owner FROM `*PREFIX*deck_board_acl` WHERE `board_id` = ? UNION SELECT 0, id, \'user\', owner, 1, 1, 1, 1 FROM `*PREFIX*deck_boards` WHERE `id` = ? ';
|
||||
return $this->findEntities($sql, [$boardId, $boardId], $limit, $offset);
|
||||
}
|
||||
|
||||
public function findAllForCard($cardId, $userId) {
|
||||
$findBoardId = "(SELECT board_id from oc_deck_stacks WHERE id IN (SELECT stack_id from oc_deck_cards WHERE id = 15))";
|
||||
$sql = "SELECT 0, id, 'user', owner, 1, 1, 1, 1 as owner FROM `oc_deck_boards` WHERE `id` IN (SELECT board_id from oc_deck_stacks WHERE id IN (SELECT stack_id from oc_deck_cards WHERE id = 15))
|
||||
UNION
|
||||
SELECT id, board_id, type, participant, permission_write, permission_invite, permission_manage, 0 FROM oc_deck_board_acl
|
||||
WHERE participant = 'admin' AND board_id IN (SELECT board_id from oc_deck_stacks WHERE id IN (SELECT stack_id from oc_deck_cards WHERE id = 15));";
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
75
lib/Db/Board.php
Normal file
75
lib/Db/Board.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
// db/author.php
|
||||
namespace OCA\Deck\Db;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
class Board extends \OCA\Deck\Db\Entity implements JsonSerializable {
|
||||
|
||||
public $id;
|
||||
protected $title;
|
||||
protected $owner;
|
||||
protected $color;
|
||||
protected $archived;
|
||||
protected $labels;
|
||||
protected $acl;
|
||||
protected $shared;
|
||||
|
||||
public function __construct() {
|
||||
$this->addType('id','integer');
|
||||
$this->addType('shared','integer');
|
||||
$this->addRelation('labels');
|
||||
$this->addRelation('acl');
|
||||
$this->addRelation('shared');
|
||||
$this->shared = -1;
|
||||
}
|
||||
|
||||
public function jsonSerialize() {
|
||||
$result = [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'owner' => $this->owner,
|
||||
'color' => $this->color,
|
||||
'labels' => $this->labels,
|
||||
'acl' => $this->acl,
|
||||
];
|
||||
if($this->shared!==-1) {
|
||||
$result['shared'] = $this->shared;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
public function setLabels($labels) {
|
||||
foreach ($labels as $l) {
|
||||
$this->labels[] = $l;
|
||||
}
|
||||
}
|
||||
|
||||
public function setAcl($acl) {
|
||||
foreach ($acl as $a) {
|
||||
$this->acl[$a->id] = $a;
|
||||
}
|
||||
}
|
||||
}
|
||||
139
lib/Db/BoardMapper.php
Normal file
139
lib/Db/BoardMapper.php
Normal file
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Deck\Db;
|
||||
|
||||
use OCP\IDb;
|
||||
use OCP\AppFramework\Db\Mapper;
|
||||
use Symfony\Component\Config\Definition\Exception\Exception;
|
||||
|
||||
|
||||
class BoardMapper extends Mapper {
|
||||
|
||||
private $labelMapper;
|
||||
private $_relationMappers = array();
|
||||
|
||||
public function addRelationMapper($name, $mapper) {
|
||||
$this->_relationMappers[$name] = $mapper;
|
||||
}
|
||||
|
||||
public function __construct(IDb $db, LabelMapper $labelMapper, AclMapper $aclMapper) {
|
||||
parent::__construct($db, 'deck_boards', '\OCA\Deck\Db\Board');
|
||||
$this->labelMapper = $labelMapper;
|
||||
$this->aclMapper = $aclMapper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
|
||||
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
|
||||
*/
|
||||
public function find($id) {
|
||||
$sql = 'SELECT id, title, owner, color, archived FROM `*PREFIX*deck_boards` ' .
|
||||
'WHERE `id` = ?';
|
||||
$board = $this->findEntity($sql, [$id]);
|
||||
|
||||
// Add labels
|
||||
$labels = $this->labelMapper->findAll($id);
|
||||
$board->setLabels($labels);
|
||||
|
||||
// Add acl
|
||||
$acl = $this->aclMapper->findAll($id);
|
||||
$board->setAcl($acl);
|
||||
|
||||
return $board;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all boards for a given user
|
||||
* @param $userId
|
||||
* @param null $limit
|
||||
* @param null $offset
|
||||
* @return array
|
||||
*/
|
||||
public function findAllByUser($userId, $limit=null, $offset=null) {
|
||||
$sql = 'SELECT id, title, owner, color, archived, 0 as shared FROM oc_deck_boards WHERE owner = ? UNION ' .
|
||||
'SELECT boards.id, title, owner, color, archived, 1 as shared FROM oc_deck_boards as boards ' .
|
||||
'JOIN oc_deck_board_acl as acl ON boards.id=acl.board_id WHERE acl.participant=? AND acl.type=\'user\' AND boards.owner != ?';
|
||||
$entries = $this->findEntities($sql, [$userId, $userId, $userId], $limit, $offset);
|
||||
/* @var Board $entry */
|
||||
foreach ($entries as $entry) {
|
||||
$acl = $this->aclMapper->findAll($entry->id);
|
||||
$entry->setAcl($acl);
|
||||
}
|
||||
return $entries;
|
||||
}
|
||||
/**
|
||||
* Find all boards for a given user
|
||||
* @param $groups
|
||||
* @param null $limit
|
||||
* @param null $offset
|
||||
* @return array
|
||||
*/
|
||||
public function findAllByGroups($userId, $groups, $limit=null, $offset=null) {
|
||||
if(count($groups)<=0) {
|
||||
return [];
|
||||
}
|
||||
$sql = 'SELECT boards.id, title, owner, color, archived, 2 as shared FROM oc_deck_boards as boards ' .
|
||||
'INNER JOIN oc_deck_board_acl as acl ON boards.id=acl.board_id WHERE owner != ? AND type=\'group\' AND (';
|
||||
$countGroups = 0;
|
||||
foreach ($groups as $group) {
|
||||
$sql .= 'acl.participant = ? ';
|
||||
if(count($groups)>1 && $countGroups++<count($groups)-1)
|
||||
$sql .= ' OR ';
|
||||
}
|
||||
$sql .= ');';
|
||||
$entries = $this->findEntities($sql, array_merge([$userId], $groups), $limit, $offset);
|
||||
/* @var Board $entry */
|
||||
foreach ($entries as $entry) {
|
||||
$acl = $this->aclMapper->findAll($entry->id);
|
||||
$entry->setAcl($acl);
|
||||
}
|
||||
return $entries;
|
||||
}
|
||||
|
||||
public function delete(\OCP\AppFramework\Db\Entity $entity) {
|
||||
//$this->deleteRelationalEntities('label', $entity);
|
||||
return parent::delete($entity);
|
||||
}
|
||||
|
||||
public function userCanView($boardId, $userInfo) {
|
||||
$board = $this->find($boardId);
|
||||
if($board->getOwner()===$userInfo['user']) {
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
$sql = 'SELECT acl.* FROM oc_deck_boards as boards ' .
|
||||
'JOIN oc_deck_board_acl as acl ON boards.id=acl.board_id WHERE acl.participant=? AND acl.type=\'user\' AND boards.id = ? AND boards.owner != ?';
|
||||
$acl = $this->find($sql, [$userInfo['user'], $boardId, $userInfo['user']], $limit, $offset);
|
||||
return true;
|
||||
} catch (Exception $e) { }
|
||||
try {
|
||||
$acl = $this->find($sql, [$userInfo['user'], $boardId, $userInfo['user']], $limit, $offset);
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
68
lib/Db/Card.php
Normal file
68
lib/Db/Card.php
Normal file
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
// db/author.php
|
||||
namespace OCA\Deck\Db;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
class Card extends Entity implements JsonSerializable {
|
||||
|
||||
public $id;
|
||||
protected $title;
|
||||
protected $description;
|
||||
protected $stackId;
|
||||
protected $type;
|
||||
protected $lastModified;
|
||||
protected $createdAt;
|
||||
protected $labels;
|
||||
protected $owner;
|
||||
protected $order;
|
||||
protected $archived;
|
||||
|
||||
public function __construct() {
|
||||
$this->addType('id','integer');
|
||||
$this->addType('stackId','integer');
|
||||
$this->addType('order','integer');
|
||||
$this->addType('lastModified', 'integer');
|
||||
$this->addType('createdAt', 'integer');
|
||||
$this->addType('archived','boolean');
|
||||
$this->addRelation('labels');
|
||||
}
|
||||
|
||||
public function jsonSerialize() {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'description' => $this->description,
|
||||
'type' => $this->type,
|
||||
'lastModified' => $this->lastModified,
|
||||
'createdAt' => $this->createdAt,
|
||||
'owner' => $this->owner,
|
||||
'order' => $this->order,
|
||||
'stackId' => $this->stackId,
|
||||
'labels' => $this->labels,
|
||||
'archived' => $this->archived,
|
||||
];
|
||||
}
|
||||
}
|
||||
111
lib/Db/CardMapper.php
Normal file
111
lib/Db/CardMapper.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Deck\Db;
|
||||
|
||||
use OCP\AppFramework\Db\Entity;
|
||||
use OCP\IDb;
|
||||
use OCP\AppFramework\Db\Mapper;
|
||||
|
||||
|
||||
|
||||
class CardMapper extends Mapper {
|
||||
|
||||
private $labelMapper;
|
||||
|
||||
public function __construct(IDb $db, LabelMapper $labelMapper) {
|
||||
parent::__construct($db, 'deck_cards', '\OCA\Deck\Db\Card');
|
||||
$this->labelMapper = $labelMapper;
|
||||
}
|
||||
|
||||
public function insert(Entity $entity) {
|
||||
$entity->setCreatedAt(time());
|
||||
$entity->setLastModified(time());
|
||||
return parent::insert($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Entity $entity
|
||||
* @return Entity
|
||||
*/
|
||||
public function update(Entity $entity) {
|
||||
$entity->setLastModified(time());
|
||||
return parent::update($entity);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
|
||||
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
|
||||
*/
|
||||
public function find($id) {
|
||||
$sql = 'SELECT * FROM `*PREFIX*deck_cards` ' .
|
||||
'WHERE `id` = ?';
|
||||
$card = $this->findEntity($sql, [$id]);
|
||||
$labels = $this->labelMapper->findAssignedLabelsForCard($card->id);
|
||||
$card->setLabels($labels);
|
||||
return $card;
|
||||
}
|
||||
|
||||
public function findAllByBoard($boardId, $limit=null, $offset=null) {
|
||||
|
||||
}
|
||||
|
||||
public function findAll($stackId, $limit=null, $offset=null) {
|
||||
// TODO: Exclude fields like text
|
||||
$sql = 'SELECT * FROM `*PREFIX*deck_cards`
|
||||
WHERE `stack_id` = ? AND NOT archived ORDER BY `order`';
|
||||
$entities = $this->findEntities($sql, [$stackId], $limit, $offset);
|
||||
return $entities;
|
||||
}
|
||||
|
||||
// TODO: test
|
||||
public function findAllArchived($stackId, $limit=null, $offset=null) {
|
||||
$sql = 'SELECT * FROM `*PREFIX*deck_cards` WHERE `stack_id`=? AND archived ORDER BY `last_modified`';
|
||||
$entities = $this->findEntities($sql, [$stackId], $limit, $offset);
|
||||
return $entities;
|
||||
}
|
||||
|
||||
public function delete(Entity $entity) {
|
||||
// FIXME: delete linked elements, because owncloud doesn't support foreign keys for apps
|
||||
return parent::delete($entity);
|
||||
}
|
||||
|
||||
public function assignLabel($card, $label) {
|
||||
$sql = 'INSERT INTO `*PREFIX*deck_assigned_labels` (`label_id`,`card_id`) VALUES (?,?)';
|
||||
$stmt = $this->db->prepare($sql);
|
||||
$stmt->bindParam(1, $label, \PDO::PARAM_INT);
|
||||
$stmt->bindParam(2, $card, \PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
public function removeLabel($card, $label) {
|
||||
$sql = 'DELETE FROM `*PREFIX*deck_assigned_labels` WHERE card_id = ? AND label_id = ?';
|
||||
$stmt = $this->db->prepare($sql);
|
||||
$stmt->bindParam(1, $card, \PDO::PARAM_INT);
|
||||
$stmt->bindParam(2, $label, \PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
53
lib/Db/DeckMapper.php
Normal file
53
lib/Db/DeckMapper.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Deck\Db;
|
||||
|
||||
use OCP\AppFramework\Db\Entity;
|
||||
use OCP\AppFramework\Db\Mapper;
|
||||
|
||||
abstract class DeckMapper extends Mapper {
|
||||
|
||||
/**
|
||||
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
|
||||
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
|
||||
*/
|
||||
public function find($id) {
|
||||
$sql = 'SELECT * FROM `' . $this->tableName . '` ' . 'WHERE `id` = ?';
|
||||
return $this->findEntity($sql, [$id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add relational data to an Entity by calling the related Mapper
|
||||
* @param $entities
|
||||
* @param $entityType
|
||||
* @param $property
|
||||
* addRelation($cards, $labels, function($one, $many) {
|
||||
* if($one->id == $many->cardId)
|
||||
* }
|
||||
*/
|
||||
public function addRelation($entities, $entityType, $property) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
70
lib/Db/Entity.php
Normal file
70
lib/Db/Entity.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: jus
|
||||
* Date: 22.06.16
|
||||
* Time: 13:32
|
||||
*/
|
||||
|
||||
namespace OCA\Deck\Db;
|
||||
|
||||
|
||||
class Entity extends \OCP\AppFramework\Db\Entity {
|
||||
|
||||
private $_relations = array();
|
||||
private $_updatedFields = array();
|
||||
|
||||
/**
|
||||
* Mark a property as relation so it will not get updated using Mapper::update
|
||||
* @param string $property Name of the property
|
||||
*/
|
||||
public function addRelation(string $property) {
|
||||
if (!in_array($property, $this->_relations)) {
|
||||
$this->_relations[] = $property;
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Mark am attribute as updated
|
||||
* overwritten from \OCP\AppFramework\Db\Entity to avoid writing relational attributes
|
||||
* @param string $attribute the name of the attribute
|
||||
* @since 7.0.0
|
||||
*/
|
||||
protected function markFieldUpdated($attribute){
|
||||
if(!in_array($attribute, $this->_relations)) {
|
||||
$this->_updatedFields[$attribute] = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* overwritten from \OCP\AppFramework\Db\Entity to avoid writing relational attributes
|
||||
* @return array Array of field's update status
|
||||
*/
|
||||
public function getUpdatedFields(){
|
||||
return $this->_updatedFields;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
48
lib/Db/Label.php
Normal file
48
lib/Db/Label.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
// db/author.php
|
||||
namespace OCA\Deck\Db;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
class Label extends Entity implements JsonSerializable {
|
||||
|
||||
public $id;
|
||||
protected $title;
|
||||
protected $color;
|
||||
protected $boardId;
|
||||
protected $cardId;
|
||||
public function __construct() {
|
||||
$this->addType('id','integer');
|
||||
}
|
||||
public function jsonSerialize() {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'boardId' => $this->boardId,
|
||||
'cardId' => $this->cardId,
|
||||
'color' => $this->color,
|
||||
];
|
||||
}
|
||||
}
|
||||
69
lib/Db/LabelMapper.php
Normal file
69
lib/Db/LabelMapper.php
Normal file
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Deck\Db;
|
||||
|
||||
use OCP\AppFramework\Db\Entity;
|
||||
use OCP\IDb;
|
||||
use OCP\AppFramework\Db\Mapper;
|
||||
|
||||
|
||||
class LabelMapper extends DeckMapper {
|
||||
|
||||
public function __construct(IDb $db) {
|
||||
parent::__construct($db, 'deck_labels', '\OCA\Deck\Db\Label');
|
||||
}
|
||||
|
||||
public function findAll($boardId, $limit=null, $offset=null) {
|
||||
$sql = 'SELECT * FROM `*PREFIX*deck_labels` WHERE `board_id` = ? ORDER BY `id`';
|
||||
return $this->findEntities($sql, [$boardId], $limit, $offset);
|
||||
}
|
||||
|
||||
public function delete(Entity $entity) {
|
||||
// FIXME: delete linked elements, because owncloud doesn't support foreign keys for apps
|
||||
return parent::delete($entity);
|
||||
}
|
||||
|
||||
public function findAssignedLabelsForCard($cardId) {
|
||||
$sql = 'SELECT l.* FROM `*PREFIX*deck_assigned_labels` as al INNER JOIN *PREFIX*deck_labels as l ON l.id = al.label_id WHERE `card_id` = ? ORDER BY l.id';
|
||||
return $this->findEntities($sql, [$cardId], $limit, $offset);
|
||||
}
|
||||
public function findAssignedLabelsForBoard($boardId, $limit=null, $offset=null) {
|
||||
$sql = "SELECT c.id as card_id, l.id as id, l.title as title, l.color as color FROM oc_deck_cards as c " .
|
||||
" INNER JOIN oc_deck_assigned_labels as al ON al.card_id = c.id INNER JOIN oc_deck_labels as l ON al.label_id = l.id WHERE board_id=? ORDER BY l.id";
|
||||
$entities = $this->findEntities($sql, [$boardId], $limit, $offset);
|
||||
return $entities;
|
||||
}
|
||||
|
||||
public function getAssignedLabelsForBoard($boardId) {
|
||||
$labels = $this->findAssignedLabelsForBoard($boardId);
|
||||
$result = array();
|
||||
foreach ($labels as $label) {
|
||||
if(!is_array($result[$label->getCardId()])) {
|
||||
$result[$label->getCardId()] = array();
|
||||
}
|
||||
$result[$label->getCardId()][] = $label;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
55
lib/Db/Stack.php
Normal file
55
lib/Db/Stack.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
// db/author.php
|
||||
namespace OCA\Deck\Db;
|
||||
|
||||
use JsonSerializable;
|
||||
|
||||
|
||||
class Stack extends Entity implements JsonSerializable {
|
||||
|
||||
public $id;
|
||||
protected $title;
|
||||
protected $boardId;
|
||||
protected $cards = array();
|
||||
protected $order;
|
||||
public function __construct() {
|
||||
$this->addType('id','integer');
|
||||
$this->addType('boardId','integer');
|
||||
$this->addType('order','integer');
|
||||
|
||||
}
|
||||
public function setCards($cards) {
|
||||
$this->cards = $cards;
|
||||
}
|
||||
public function jsonSerialize() {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->title,
|
||||
'order' => $this->order,
|
||||
'boardId' => $this->boardId,
|
||||
'cards' => $this->cards,
|
||||
];
|
||||
}
|
||||
}
|
||||
62
lib/Db/StackMapper.php
Normal file
62
lib/Db/StackMapper.php
Normal file
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @license GNU AGPL version 3 or any later version
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as
|
||||
* published by the Free Software Foundation, either version 3 of the
|
||||
* License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCA\Deck\Db;
|
||||
|
||||
use OCP\AppFramework\Db\Entity;
|
||||
use OCP\IDb;
|
||||
use OCP\AppFramework\Db\Mapper;
|
||||
|
||||
|
||||
class StackMapper extends Mapper {
|
||||
|
||||
private $cardMapper;
|
||||
|
||||
public function __construct(IDb $db, CardMapper $cardMapper) {
|
||||
parent::__construct($db, 'deck_stacks', '\OCA\Deck\Db\Stack');
|
||||
$this->cardMapper = $cardMapper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws \OCP\AppFramework\Db\DoesNotExistException if not found
|
||||
* @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result
|
||||
*/
|
||||
public function find($id) {
|
||||
$sql = 'SELECT * FROM `*PREFIX*deck_stacks` ' .
|
||||
'WHERE `id` = ?';
|
||||
return $this->findEntity($sql, [$id]);
|
||||
}
|
||||
|
||||
|
||||
public function findAll($boardId, $limit=null, $offset=null) {
|
||||
$sql = 'SELECT * FROM `*PREFIX*deck_stacks` WHERE `board_id` = ? ORDER BY `order`';
|
||||
return $this->findEntities($sql, [$boardId], $limit, $offset);
|
||||
}
|
||||
|
||||
|
||||
public function delete(Entity $entity) {
|
||||
// FIXME: delete linked elements, because owncloud doesn't support foreign keys for apps
|
||||
return parent::delete($entity);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user