diff options
author | splitt3r <splitt3r@users.noreply.github.com> | 2017-10-08 16:10:15 +0300 |
---|---|---|
committer | splitt3r <splitt3r@users.noreply.github.com> | 2017-10-08 16:10:15 +0300 |
commit | 4b47fe4f06ae99c01771b6cb26af8f84b2f5b604 (patch) | |
tree | c7c7fdd6217f29e3e6f8f35d388cf327ba2a43a4 /lib | |
parent | d10da8dc1dd91c58caa4a6a2cef86d77421c9cdd (diff) |
Changed to new NC app skeleton structure
Diffstat (limited to 'lib')
-rw-r--r-- | lib/AppInfo/Application.php | 175 | ||||
-rw-r--r-- | lib/Controller/PageController.php | 607 | ||||
-rw-r--r-- | lib/Controller/PollController.php | 30 | ||||
-rw-r--r-- | lib/Db/Access.php | 37 | ||||
-rw-r--r-- | lib/Db/AccessMapper.php | 71 | ||||
-rw-r--r-- | lib/Db/Comment.php | 43 | ||||
-rw-r--r-- | lib/Db/CommentMapper.php | 101 | ||||
-rw-r--r-- | lib/Db/Date.php | 37 | ||||
-rw-r--r-- | lib/Db/DateMapper.php | 90 | ||||
-rw-r--r-- | lib/Db/Event.php | 61 | ||||
-rw-r--r-- | lib/Db/EventMapper.php | 125 | ||||
-rw-r--r-- | lib/Db/Notification.php | 37 | ||||
-rw-r--r-- | lib/Db/NotificationMapper.php | 90 | ||||
-rw-r--r-- | lib/Db/Participation.php | 43 | ||||
-rw-r--r-- | lib/Db/ParticipationMapper.php | 106 | ||||
-rw-r--r-- | lib/Db/ParticipationText.php | 43 | ||||
-rw-r--r-- | lib/Db/ParticipationTextMapper.php | 106 | ||||
-rw-r--r-- | lib/Db/Text.php | 37 | ||||
-rw-r--r-- | lib/Db/TextMapper.php | 75 |
19 files changed, 1914 insertions, 0 deletions
diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php new file mode 100644 index 00000000..0c660506 --- /dev/null +++ b/lib/AppInfo/Application.php @@ -0,0 +1,175 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\AppInfo; + +use OC\AppFramework\Utility\SimpleContainer; +use \OCP\AppFramework\App; +use \OCA\Polls\Db\AccessMapper; +use \OCA\Polls\Db\CommentMapper; +use \OCA\Polls\Db\DateMapper; +use \OCA\Polls\Db\EventMapper; +use \OCA\Polls\Db\NotificationMapper; +use \OCA\Polls\Db\ParticipationMapper; +use \OCA\Polls\Db\ParticipationTextMapper; +use \OCA\Polls\Db\TextMapper; +use \OCA\Polls\Controller\PageController; + +class Application extends App { + + /** + * Application constructor. + * + * @param array $urlParams + */ + public function __construct(array $urlParams = array()) { + parent::__construct('polls', $urlParams); + + $container = $this->getContainer(); + $server = $container->getServer(); + + /** + * Controllers + */ + $container->registerService('PageController', function ($c) use ($server) { + /** @var SimpleContainer $c */ + return new PageController( + $c->query('AppName'), + $c->query('Request'), + $c->query('UserManager'), + $c->query('GroupManager'), + $c->query('AvatarManager'), + $c->query('Logger'), + $c->query('L10N'), + $c->query('ServerContainer')->getURLGenerator(), + $c->query('UserId'), + $c->query('AccessMapper'), + $c->query('CommentMapper'), + $c->query('DateMapper'), + $c->query('EventMapper'), + $c->query('NotificationMapper'), + $c->query('ParticipationMapper'), + $c->query('ParticipationTextMapper'), + $c->query('TextMapper') + ); + }); + + $container->registerService('UserManager', function ($c) { + /** @var SimpleContainer $c */ + return $c->query('ServerContainer')->getUserManager(); + }); + + $container->registerService('GroupManager', function ($c) { + /** @var SimpleContainer $c */ + return $c->query('ServerContainer')->getGroupManager(); + }); + + $container->registerService('AvatarManager', function ($c) { + /** @var SimpleContainer $c */ + return $c->query('ServerContainer')->getAvatarManager(); + }); + + $container->registerService('Logger', function ($c) { + /** @var SimpleContainer $c */ + return $c->query('ServerContainer')->getLogger(); + }); + + $container->registerService('L10N', function ($c) { + return $c->query('ServerContainer')->getL10N($c->query('AppName')); + }); + + $container->registerService('AccessMapper', function ($c) use ($server) { + /** @var SimpleContainer $c */ + return new AccessMapper( + $server->getDatabaseConnection() + ); + }); + + $container->registerService('CommentMapper', function ($c) use ($server) { + /** @var SimpleContainer $c */ + return new CommentMapper( + $server->getDatabaseConnection() + ); + }); + + $container->registerService('DateMapper', function ($c) use ($server) { + /** @var SimpleContainer $c */ + return new DateMapper( + $server->getDatabaseConnection() + ); + }); + + $container->registerService('EventMapper', function ($c) use ($server) { + /** @var SimpleContainer $c */ + return new EventMapper( + $server->getDatabaseConnection() + ); + }); + + $container->registerService('NotificationMapper', function ($c) use ($server) { + /** @var SimpleContainer $c */ + return new NotificationMapper( + $server->getDatabaseConnection() + ); + }); + + $container->registerService('ParticipationMapper', function ($c) use ($server) { + /** @var SimpleContainer $c */ + return new ParticipationMapper( + $server->getDatabaseConnection() + ); + }); + + $container->registerService('ParticipationTextMapper', function ($c) use ($server) { + /** @var SimpleContainer $c */ + return new ParticipationTextMapper( + $server->getDatabaseConnection() + ); + }); + + $container->registerService('TextMapper', function ($c) use ($server) { + /** @var SimpleContainer $c */ + return new TextMapper( + $server->getDatabaseConnection() + ); + }); + } + + /** + * Register navigation entry for main navigation. + */ + public function registerNavigationEntry() { + $container = $this->getContainer(); + $container->query('OCP\INavigationManager')->add(function () use ($container) { + $urlGenerator = $container->query('OCP\IURLGenerator'); + $l10n = $container->query('OCP\IL10N'); + return [ + 'id' => 'polls', + 'order' => 77, + 'href' => $urlGenerator->linkToRoute('polls.page.index'), + 'icon' => $urlGenerator->imagePath('polls', 'app-logo-polls.svg'), + 'name' => $l10n->t('Polls') + ]; + }); + } +} diff --git a/lib/Controller/PageController.php b/lib/Controller/PageController.php new file mode 100644 index 00000000..408a01bb --- /dev/null +++ b/lib/Controller/PageController.php @@ -0,0 +1,607 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Controller; + +use \OCA\Polls\Db\Comment; +use \OCA\Polls\Db\Date; +use \OCA\Polls\Db\Event; +use \OCA\Polls\Db\Notification; +use \OCA\Polls\Db\Participation; +use \OCA\Polls\Db\ParticipationText; +use \OCA\Polls\Db\Text; + +use \OCA\Polls\Db\AccessMapper; +use \OCA\Polls\Db\CommentMapper; +use \OCA\Polls\Db\DateMapper; +use \OCA\Polls\Db\EventMapper; +use \OCA\Polls\Db\NotificationMapper; +use \OCA\Polls\Db\ParticipationMapper; +use \OCA\Polls\Db\ParticipationTextMapper; +use \OCA\Polls\Db\TextMapper; +use \OCP\IUserManager; +use \OCP\IGroupManager; +use \OCP\IAvatarManager; +use \OCP\ILogger; +use \OCP\IL10N; +use \OCP\IRequest; +use \OCP\IURLGenerator; +use OCP\Security\ISecureRandom; +use \OCP\AppFramework\Http\TemplateResponse; +use \OCP\AppFramework\Http\RedirectResponse; +use \OCP\AppFramework\Http\JSONResponse; +use \OCP\AppFramework\Controller; + +class PageController extends Controller { + + private $userId; + private $accessMapper; + private $commentMapper; + private $dateMapper; + private $eventMapper; + private $notificationMapper; + private $participationMapper; + private $participationTextMapper; + private $textMapper; + private $urlGenerator; + private $manager; + private $avatarManager; + private $logger; + private $trans; + private $userMgr; + private $groupManager; + + public function __construct($appName, IRequest $request, + IUserManager $manager, + IGroupManager $groupManager, + IAvatarManager $avatarManager, + ILogger $logger, + IL10N $trans, + IURLGenerator $urlGenerator, + $userId, + AccessMapper $accessMapper, + CommentMapper $commentMapper, + DateMapper $dateMapper, + EventMapper $eventMapper, + NotificationMapper $notificationMapper, + ParticipationMapper $ParticipationMapper, + ParticipationTextMapper $ParticipationTextMapper, + TextMapper $textMapper) { + parent::__construct($appName, $request); + $this->manager = $manager; + $this->groupManager = $groupManager; + $this->avatarManager = $avatarManager; + $this->logger = $logger; + $this->trans = $trans; + $this->urlGenerator = $urlGenerator; + $this->userId = $userId; + $this->accessMapper = $accessMapper; + $this->commentMapper = $commentMapper; + $this->dateMapper = $dateMapper; + $this->eventMapper = $eventMapper; + $this->notificationMapper = $notificationMapper; + $this->participationMapper = $ParticipationMapper; + $this->participationTextMapper = $ParticipationTextMapper; + $this->textMapper = $textMapper; + $this->userMgr = \OC::$server->getUserManager(); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function index() { + $polls = $this->eventMapper->findAllForUserWithInfo($this->userId); + $comments = $this->commentMapper->findDistinctByUser($this->userId); + $partic = $this->participationMapper->findDistinctByUser($this->userId); + $response = new TemplateResponse('polls', 'main.tmpl', ['polls' => $polls, 'comments' => $comments, 'participations' => $partic, 'userId' => $this->userId, 'userMgr' => $this->manager, 'urlGenerator' => $this->urlGenerator]); + if (class_exists('OCP\AppFramework\Http\ContentSecurityPolicy')) { + $csp = new \OCP\AppFramework\Http\ContentSecurityPolicy(); + $response->setContentSecurityPolicy($csp); + } + return $response; + } + + private function sendNotifications($pollId, $from) { + $poll = $this->eventMapper->find($pollId); + $notifs = $this->notificationMapper->findAllByPoll($pollId); + foreach ($notifs as $notif) { + if ($from === $notif->getUserId()) { + continue; + } + $email = \OC::$server->getConfig()->getUserValue($notif->getUserId(), 'settings', 'email'); + if (strlen($email) === 0 || !isset($email)) { + continue; + } + $url = \OC::$server->getURLGenerator()->getAbsoluteURL(\OC::$server->getURLGenerator()->linkToRoute('polls.page.goto_poll', array('hash' => $poll->getHash()))); + + $recUser = $this->userMgr->get($notif->getUserId()); + $sendUser = $this->userMgr->get($from); + $rec = ""; + if ($recUser !== null) { + $rec = $recUser->getDisplayName(); + } + if ($sendUser !== null) { + $sender = $sendUser->getDisplayName(); + } else { + $sender = $from; + } + $msg = $this->trans->t('Hello %s,<br/><br/><strong>%s</strong> participated in the poll \'%s\'.<br/><br/>To go directly to the poll, you can use this <a href="%s">link</a>', array( + $rec, $sender, $poll->getTitle(), $url)); + + $msg .= "<br/><br/>"; + + $toname = $this->userMgr->get($notif->getUserId())->getDisplayName(); + $subject = $this->trans->t('ownCloud Polls - New Comment'); + $fromaddress = \OCP\Util::getDefaultEmailAddress('no-reply'); + $fromname = $this->trans->t("ownCloud Polls") . ' (' . $from . ')'; + + try { + $mailer = \OC::$server->getMailer(); + $message = $mailer->createMessage(); + $message->setSubject($subject); + $message->setFrom(array($fromaddress => $fromname)); + $message->setTo(array($email => $toname)); + $message->setHtmlBody($msg); + $mailer->send($message); + } catch (\Exception $e) { + $message = 'error sending mail to: ' . $toname . ' (' . $email . ')'; + \OCP\Util::writeLog("polls", $message, \OCP\Util::ERROR); + } + } + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @PublicPage + */ + public function gotoPoll($hash) { + $poll = $this->eventMapper->findByHash($hash); + if ($poll->getType() == '0') { + $dates = $this->dateMapper->findByPoll($poll->getId()); + $votes = $this->participationMapper->findByPoll($poll->getId()); + } else { + $dates = $this->textMapper->findByPoll($poll->getId()); + $votes = $this->participationTextMapper->findByPoll($poll->getId()); + } + $comments = $this->commentMapper->findByPoll($poll->getId()); + try { + $notification = $this->notificationMapper->findByUserAndPoll($poll->getId(), $this->userId); + } catch(\OCP\AppFramework\Db\DoesNotExistException $e) { + $notification = null; + } + if ($this->hasUserAccess($poll)) { + return new TemplateResponse('polls', 'goto.tmpl', ['poll' => $poll, 'dates' => $dates, 'comments' => $comments, 'votes' => $votes, 'notification' => $notification, 'userId' => $this->userId, 'userMgr' => $this->manager, 'urlGenerator' => $this->urlGenerator, 'avatarManager' => $this->avatarManager]); + } else { + \OCP\User::checkLoggedIn(); + return new TemplateResponse('polls', 'no.acc.tmpl', []); + } + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function deletePoll($pollId) { + $poll = new Event(); + $poll->setId($pollId); + $this->eventMapper->delete($poll); + $this->textMapper->deleteByPoll($pollId); + $this->dateMapper->deleteByPoll($pollId); + $this->participationMapper->deleteByPoll($pollId); + $this->participationTextMapper->deleteByPoll($pollId); + $this->commentMapper->deleteByPoll($pollId); + $url = $this->urlGenerator->linkToRoute('polls.page.index'); + return new RedirectResponse($url); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function editPoll($hash) { + $poll = $this->eventMapper->findByHash($hash); + if ($this->userId !== $poll->getOwner()) { + return new TemplateResponse('polls', 'no.create.tmpl'); + } + if ($poll->getType() == '0') { + $dates = $this->dateMapper->findByPoll($poll->getId()); + } else { + $dates = $this->textMapper->findByPoll($poll->getId()); + } + return new TemplateResponse('polls', 'create.tmpl', ['poll' => $poll, 'dates' => $dates, 'userId' => $this->userId, 'userMgr' => $this->manager, 'urlGenerator' => $this->urlGenerator]); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function updatePoll($pollId, $pollType, $pollTitle, $pollDesc, $userId, $chosenDates, $expireTs, $accessType, $accessValues) { + $event = $this->eventMapper->find($pollId); + $event->setTitle(htmlspecialchars($pollTitle)); + $event->setDescription(htmlspecialchars($pollDesc)); + + if ($accessType === 'select') { + if (isset($accessValues)) { + $accessValues = json_decode($accessValues); + if ($accessValues !== null) { + $groups = array(); + $users = array(); + if ($accessValues->groups !== null) { + $groups = $accessValues->groups; + } + if ($accessValues->users !== null) { + $users = $accessValues->users; + } + $accessType = ''; + foreach ($groups as $gid) { + $accessType .= $gid . ';'; + } + foreach ($users as $uid) { + $accessType .= $uid . ';'; + } + } + } + } + $event->setAccess($accessType); + + $chosenDates = json_decode($chosenDates); + + $expire = null; + if ($expireTs !== null && $expireTs !== '') { + $expire = date('Y-m-d H:i:s', $expireTs + 60*60*24); //add one day, so it expires at the end of a day + } + $event->setExpire($expire); + + $this->dateMapper->deleteByPoll($pollId); + $this->textMapper->deleteByPoll($pollId); + if ($pollType === 'event') { + $event->setType(0); + $this->eventMapper->update($event); + sort($chosenDates); + foreach ($chosenDates as $el) { + $date = new Date(); + $date->setPollId($pollId); + $date->setDt(date('Y-m-d H:i:s', $el)); + $this->dateMapper->insert($date); + } + } else { + $event->setType(1); + $this->eventMapper->update($event); + foreach ($chosenDates as $el) { + $text = new Text(); + $text->setText($el); + $text->setPollId($pollId); + $this->textMapper->insert($text); + } + } + $url = $this->urlGenerator->linkToRoute('polls.page.index'); + return new RedirectResponse($url); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function createPoll() { + return new TemplateResponse('polls', 'create.tmpl', ['userId' => $this->userId, 'userMgr' => $this->manager, 'urlGenerator' => $this->urlGenerator]); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function insertPoll($pollType, $pollTitle, $pollDesc, $userId, $chosenDates, $expireTs, $accessType, $accessValues, $isAnonymous, $hideNames) { + $event = new Event(); + $event->setTitle(htmlspecialchars($pollTitle)); + $event->setDescription(htmlspecialchars($pollDesc)); + $event->setOwner($userId); + $event->setCreated(date('Y-m-d H:i:s')); + $event->setHash(\OC::$server->getSecureRandom()->getMediumStrengthGenerator()->generate(16, + ISecureRandom::CHAR_DIGITS. + ISecureRandom::CHAR_LOWER. + ISecureRandom::CHAR_UPPER)); + $event->setIsAnonymous($isAnonymous ? '1' : '0'); + $event->setFullAnonymous($isAnonymous && $hideNames ? '1' : '0'); + + if ($accessType === 'select') { + if (isset($accessValues)) { + $accessValues = json_decode($accessValues); + if ($accessValues !== null) { + $groups = array(); + $users = array(); + if ($accessValues->groups !== null) { + $groups = $accessValues->groups; + } + if ($accessValues->users !== null) { + $users = $accessValues->users; + } + $accessType = ''; + foreach ($groups as $gid) { + $accessType .= $gid . ';'; + } + foreach ($users as $uid) { + $accessType .= $uid . ';'; + } + } + } + } + $event->setAccess($accessType); + + $chosenDates = json_decode($chosenDates); + + $expire = null; + if ($expireTs !== null && $expireTs !== '') { + $expire = date('Y-m-d H:i:s', $expireTs + 60*60*24); //add one day, so it expires at the end of a day + } + $event->setExpire($expire); + + if ($pollType === 'event') { + $event->setType(0); + $ins = $this->eventMapper->insert($event); + $poll_id = $ins->getId(); + sort($chosenDates); + foreach ($chosenDates as $el) { + $date = new Date(); + $date->setPollId($poll_id); + $date->setDt(date('Y-m-d H:i:s', $el)); + $this->dateMapper->insert($date); + } + } else { + $event->setType(1); + $ins = $this->eventMapper->insert($event); + $poll_id = $ins->getId(); + $cnt = 1; + foreach ($chosenDates as $el) { + $text = new Text(); + $text->setText($el . '_' . $cnt); + $text->setPollId($poll_id); + $this->textMapper->insert($text); + $cnt++; + } + } + $url = $this->urlGenerator->linkToRoute('polls.page.index'); + return new RedirectResponse($url); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @PublicPage + */ + public function insertVote($pollId, $userId, $types, $dates, $notif, $changed) { + if ($this->userId !== null) { + if ($notif === 'true') { + try { + //check if user already set notification for this poll + $this->notificationMapper->findByUserAndPoll($pollId, $userId); + } catch(\OCP\AppFramework\Db\DoesNotExistException $e) { + //insert if not exist + $not = new Notification(); + $not->setUserId($userId); + $not->setPollId($pollId); + $this->notificationMapper->insert($not); + } + } else { + try { + //delete if entry is in db + $not = $this->notificationMapper->findByUserAndPoll($pollId, $userId); + $this->notificationMapper->delete($not); + } catch(\OCP\AppFramework\Db\DoesNotExistException $e) { + //doesn't exist in db, nothing to do + } + } + } else { + // TODO: Needs investigation! + $userId = $userId; + } + $poll = $this->eventMapper->find($pollId); + if ($changed === 'true') { + $dates = json_decode($dates); + $types = json_decode($types); + if ($poll->getType() == '0') { + $this->participationMapper->deleteByPollAndUser($pollId, $userId); + } else { + $this->participationTextMapper->deleteByPollAndUser($pollId, $userId); + } + for ($i=0; $i<count($dates); $i++) { + if ($poll->getType() == '0') { + $part = new Participation(); + $part->setPollId($pollId); + $part->setUserId($userId); + $part->setDt(date('Y-m-d H:i:s', $dates[$i])); + $part->setType($types[$i]); + $this->participationMapper->insert($part); + } else { + $part = new ParticipationText(); + $part->setPollId($pollId); + $part->setUserId($userId); + $part->setText($dates[$i]); + $part->setType($types[$i]); + $this->participationTextMapper->insert($part); + } + + } + $this->sendNotifications($pollId, $userId); + } + $hash = $poll->getHash(); + $url = $this->urlGenerator->linkToRoute('polls.page.goto_poll', ['hash' => $hash]); + return new RedirectResponse($url); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * @PublicPage + */ + public function insertComment($pollId, $userId, $commentBox) { + $poll = $this->eventMapper->find($pollId); + $comment = new Comment(); + $comment->setPollId($pollId); + $comment->setUserId($userId); + $comment->setComment($commentBox); + $comment->setDt(date('Y-m-d H:i:s')); + $this->commentMapper->insert($comment); + $this->sendNotifications($pollId, $userId); + $hash = $this->eventMapper->find($pollId)->getHash(); + $url = $this->urlGenerator->linkToRoute('polls.page.goto_poll', ['hash' => $hash]); + if ($this->manager->get($userId) !== null) { + $newUserId = $this->manager->get($userId)->getDisplayName(); + } else { + $newUserId = $userId; + } + return new JSONResponse(array('comment' => $commentBox, 'date' => date('Y-m-d H:i:s'), 'userName' => $newUserId)); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function search($searchTerm, $groups, $users) { + return array_merge($this->searchForGroups($searchTerm, $groups), $this->searchForUsers($searchTerm, $users)); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function searchForGroups($searchTerm, $groups) { + $selectedGroups = json_decode($groups); + $groups = $this->groupManager->search($searchTerm); + $gids = array(); + $sgids = array(); + foreach ($selectedGroups as $sg) { + $sgids[] = str_replace('group_', '', $sg); + } + foreach ($groups as $g) { + $gids[] = $g->getGID(); + } + $diffGids = array_diff($gids, $sgids); + $gids = array(); + foreach ($diffGids as $g) { + $gids[] = ['gid' => $g, 'isGroup' => true]; + } + return $gids; + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function searchForUsers($searchTerm, $users) { + $selectedUsers = json_decode($users); + \OCP\Util::writeLog("polls", print_r($selectedUsers, true), \OCP\Util::ERROR); + $userNames = $this->userMgr->searchDisplayName($searchTerm); + $users = array(); + $susers = array(); + foreach ($selectedUsers as $su) { + $susers[] = str_replace('user_', '', $su); + } + foreach ($userNames as $u) { + $alreadyAdded = false; + foreach ($susers as &$su) { + if ($su === $u->getUID()) { + unset($su); + $alreadyAdded = true; + break; + } + } + if (!$alreadyAdded) { + $users[] = array('uid' => $u->getUID(), 'displayName' => $u->getDisplayName(), 'isGroup' => false); + } else { + continue; + } + } + return $users; + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function getDisplayName($username) { + return $this->manager->get($username)->getDisplayName(); + } + + public function getPollsForUser() { + return $this->eventMapper->findAllForUser($this->userId); + } + + public function getPollsForUserWithInfo($user = null) { + if($user === null) return $this->eventMapper->findAllForUserWithInfo($this->userId); + else return $this->eventMapper->findAllForUserWithInfo($user); + } + + public function getGroups() { + // $this->requireLogin(); + if (class_exists('\OC_Group', true)) { + // Nextcloud <= 11, ownCloud + return \OC_Group::getUserGroups($this->$userId); + } + // Nextcloud >= 12 + $groups = \OC::$server->getGroupManager()->getUserGroups(\OC::$server->getUserSession()->getUser()); + return array_map(function ($group) { + return $group->getGID(); + }, $groups); + } + + private function hasUserAccess($poll) { + $access = $poll->getAccess(); + $owner = $poll->getOwner(); + if ($access === 'public') { + return true; + } + if ($access === 'hidden') { + return true; + } + if ($this->userId === null) { + return false; + } + if ($access === 'registered') { + return true; + } + if ($owner === $this->userId) { + return true; + } + \OCP\Util::writeLog("polls", $this->userId, \OCP\Util::ERROR); + $user_groups = $this->getGroups(); + $arr = explode(';', $access); + foreach ($arr as $item) { + if (strpos($item, 'group_') === 0) { + $grp = substr($item, 6); + foreach ($user_groups as $user_group) { + if ($user_group === $grp) { + return true; + } + } + } else if (strpos($item, 'user_') === 0) { + $usr = substr($item, 5); + if ($usr === \OCP\User::getUser()) { + return true; + } + } + } + return false; + } +} diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php new file mode 100644 index 00000000..70fd9691 --- /dev/null +++ b/lib/Controller/PollController.php @@ -0,0 +1,30 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Controller; + +use \OCP\AppFramework\Controller; + +class PollController extends Controller { + +} diff --git a/lib/Db/Access.php b/lib/Db/Access.php new file mode 100644 index 00000000..6456cf74 --- /dev/null +++ b/lib/Db/Access.php @@ -0,0 +1,37 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Entity; + +/** + * @method integer getPId() + * @method void setPId(integer $value) + * @method string getAccessType() + * @method void setAccessType(string $value) + */ +class Access extends Entity { + public $pId; + public $accessType; +} diff --git a/lib/Db/AccessMapper.php b/lib/Db/AccessMapper.php new file mode 100644 index 00000000..fa391214 --- /dev/null +++ b/lib/Db/AccessMapper.php @@ -0,0 +1,71 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Mapper; +use OCP\IDBConnection; + +class AccessMapper extends Mapper { + + public function __construct(IDBConnection $db) { + parent::__construct($db, 'polls_access', '\OCA\Polls\Db\Access'); + } + + /** + * @param int $id + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result + * @return Favorite + */ + public function find($id) { + $sql = 'SELECT * FROM `*PREFIX*polls_access` '. + 'WHERE `id` = ?'; + return $this->findEntity($sql, [$id]); + } + + /** + * @param string $userId + * @param string $from + * @param string $until + * @param int $limit + * @param int $offset + * @return Favorite[] + */ + public function findBetween($userId, $from, $until, $limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_access` '. + 'WHERE `userId` = ?'. + 'AND `timestamp` BETWEEN ? and ?'; + return $this->findEntities($sql, [$userId, $from, $until], $limit, $offset); + } + + /** + * @param int $limit + * @param int $offset + * @return Favorite[] + */ + public function findAll($limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_access`'; + return $this->findEntities($sql, [], $limit, $offset); + } +} diff --git a/lib/Db/Comment.php b/lib/Db/Comment.php new file mode 100644 index 00000000..fa0481f1 --- /dev/null +++ b/lib/Db/Comment.php @@ -0,0 +1,43 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Entity; + +/** + * @method string getUserId() + * @method void setUserId(string $value) + * @method string getDt() + * @method void setDt(string $value) + * @method string getComment() + * @method void setComment(string $value) + * @method integer getPollId() + * @method void setPollId(integer $value) + */ +class Comment extends Entity { + public $userId; + public $dt; + public $comment; + public $pollId; +} diff --git a/lib/Db/CommentMapper.php b/lib/Db/CommentMapper.php new file mode 100644 index 00000000..10cad506 --- /dev/null +++ b/lib/Db/CommentMapper.php @@ -0,0 +1,101 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Mapper; +use OCP\IDBConnection; + +class CommentMapper extends Mapper { + + public function __construct(IDBConnection $db) { + parent::__construct($db, 'polls_comments', '\OCA\Polls\Db\Comment'); + } + + /** + * @param int $id + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result + * @return Comment + */ + public function find($id) { + $sql = 'SELECT * FROM `*PREFIX*polls_comments` '. + 'WHERE `id` = ?'; + return $this->findEntity($sql, [$id]); + } + + /** + * @param string $userId + * @param string $from + * @param string $until + * @param int $limit + * @param int $offset + * @return Comment[] + */ + public function findBetween($userId, $from, $until, $limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_comments` '. + 'WHERE `userId` = ?'. + 'AND `timestamp` BETWEEN ? and ?'; + return $this->findEntities($sql, [$userId, $from, $until], $limit, $offset); + } + + /** + * @param int $limit + * @param int $offset + * @return Comment[] + */ + public function findAll($limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_comments`'; + return $this->findEntities($sql, [], $limit, $offset); + } + + /** + * @param string $userId + * @param int $limit + * @param int $offset + * @return Comment[] + */ + public function findDistinctByUser($userId, $limit=null, $offset=null) { + $sql = 'SELECT DISTINCT * FROM `*PREFIX*polls_comments` WHERE user_id=?'; + return $this->findEntities($sql, [$userId], $limit, $offset); + } + + /** + * @param string $userId + * @param int $limit + * @param int $offset + * @return Comment[] + */ + public function findByPoll($pollId, $limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_comments` WHERE poll_id=? ORDER BY Dt DESC'; + return $this->findEntities($sql, [$pollId], $limit, $offset); + } + + /** + * @param string $pollId + */ + public function deleteByPoll($pollId) { + $sql = 'DELETE FROM `*PREFIX*polls_comments` WHERE poll_id=?'; + $this->execute($sql, [$pollId]); + } +} diff --git a/lib/Db/Date.php b/lib/Db/Date.php new file mode 100644 index 00000000..05ff0a06 --- /dev/null +++ b/lib/Db/Date.php @@ -0,0 +1,37 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Entity; + +/** + * @method timestamp getDt() + * @method void setDt(timestamp $value) + * @method integer getPollId() + * @method void setPollId(integer $value) + */ +class Date extends Entity { + public $dt; + public $pollId; +} diff --git a/lib/Db/DateMapper.php b/lib/Db/DateMapper.php new file mode 100644 index 00000000..9a16ebda --- /dev/null +++ b/lib/Db/DateMapper.php @@ -0,0 +1,90 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Mapper; +use OCP\IDBConnection; + +class DateMapper extends Mapper { + + public function __construct(IDBConnection $db) { + parent::__construct($db, 'polls_dts', '\OCA\Polls\Db\Date'); + } + + /** + * @param int $id + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result + * @return Date + */ + public function find($id) { + $sql = 'SELECT * FROM `*PREFIX*polls_dts` '. + 'WHERE `id` = ?'; + return $this->findEntity($sql, [$id]); + } + + /** + * @param string $userId + * @param string $from + * @param string $until + * @param int $limit + * @param int $offset + * @return Date[] + */ + public function findBetween($userId, $from, $until, $limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_dts` '. + 'WHERE `userId` = ?'. + 'AND `timestamp` BETWEEN ? and ?'; + return $this->findEntities($sql, [$userId, $from, $until], $limit, $offset); + } + + /** + * @param int $limit + * @param int $offset + * @return Date[] + */ + public function findAll($limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_dts`'; + return $this->findEntities($sql, [], $limit, $offset); + } + + /** + * @param string $pollId + * @param int $limit + * @param int $offset + * @return Date[] + */ + public function findByPoll($pollId, $limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_dts` WHERE poll_id=?'; + return $this->findEntities($sql, [$pollId], $limit, $offset); + } + + /** + * @param string $pollId + */ + public function deleteByPoll($pollId) { + $sql = 'DELETE FROM `*PREFIX*polls_dts` WHERE poll_id=?'; + $this->execute($sql, [$pollId]); + } +} diff --git a/lib/Db/Event.php b/lib/Db/Event.php new file mode 100644 index 00000000..8882adcb --- /dev/null +++ b/lib/Db/Event.php @@ -0,0 +1,61 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Entity; + +/** + * @method integer getType() + * @method void setType(integer $value) + * @method string getTitle() + * @method void setTitle(string $value) + * @method string getDescription() + * @method void setDescription(string $value) + * @method string getOwner() + * @method void setOwner(string $value) + * @method timestamp getCreated() + * @method void setCreated(timestamp $value) + * @method string getAccess() + * @method void setAccess(string $value) + * @method timestamp getExpire() + * @method void setExpire(timestamp $value) + * @method string getHash() + * @method void setHash(string $value) + * @method integer getIsAnonymous() + * @method void setIsAnonymous(integer $value) + * @method integer getFullAnonymous() + * @method void setFullAnonymous(integer $value) + */ +class Event extends Entity { + public $type; + public $title; + public $description; + public $owner; + public $created; + public $access; + public $expire; + public $hash; + public $isAnonymous; + public $fullAnonymous; +} diff --git a/lib/Db/EventMapper.php b/lib/Db/EventMapper.php new file mode 100644 index 00000000..862bc475 --- /dev/null +++ b/lib/Db/EventMapper.php @@ -0,0 +1,125 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Mapper; +use OCP\IDBConnection; + +class EventMapper extends Mapper { + + public function __construct(IDBConnection $db) { + parent::__construct($db, 'polls_events', '\OCA\Polls\Db\Event'); + } + + /** + * @param int $id + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result + * @return Event + */ + public function find($id) { + $sql = 'SELECT * FROM `*PREFIX*polls_events` '. + 'WHERE `id` = ?'; + return $this->findEntity($sql, [$id]); + } + + /** + * @param string $userId + * @param string $from + * @param string $until + * @param int $limit + * @param int $offset + * @return Event[] + */ + public function findBetween($userId, $from, $until, $limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_events` '. + 'WHERE `userId` = ?'. + 'AND `timestamp` BETWEEN ? and ?'; + return $this->findEntities($sql, [$userId, $from, $until], $limit, $offset); + } + + /** + * @param int $limit + * @param int $offset + * @return Event[] + */ + public function findAll($limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_events`'; + return $this->findEntities($sql, [], $limit, $offset); + } + + /** + * @param int $limit + * @param int $offset + * @return Event + */ + public function findByHash($hash, $limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_events` WHERE `hash`=?'; + return $this->findEntity($sql, [$hash], $limit, $offset); + } + + /** + * @param int $limit + * @param int $offset + * @return Event[] + */ + public function findAllForUser($userId, $limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_events` WHERE `owner`=?'; + return $this->findEntities($sql, [$userId], $limit, $offset); + } + + /** + * @param int $limit + * @param int $offset + * @return Event[] + */ + public function findAllForUserWithInfo($userId, $limit=null, $offset=null) { + $sql = 'SELECT DISTINCT *PREFIX*polls_events.id, + *PREFIX*polls_events.hash, + *PREFIX*polls_events.type, + *PREFIX*polls_events.title, + *PREFIX*polls_events.description, + *PREFIX*polls_events.owner, + *PREFIX*polls_events.created, + *PREFIX*polls_events.access, + *PREFIX*polls_events.expire, + *PREFIX*polls_events.is_anonymous, + *PREFIX*polls_events.full_anonymous + FROM *PREFIX*polls_events + LEFT JOIN *PREFIX*polls_particip + ON *PREFIX*polls_events.id = *PREFIX*polls_particip.id + LEFT JOIN *PREFIX*polls_comments + ON *PREFIX*polls_events.id = *PREFIX*polls_comments.id + WHERE + (*PREFIX*polls_events.access =? and *PREFIX*polls_events.owner =?) + OR + *PREFIX*polls_events.access !=? + OR + *PREFIX*polls_particip.user_id =? + OR + *PREFIX*polls_comments.user_id =? + ORDER BY created'; + return $this->findEntities($sql, ['hidden', $userId, 'hidden', $userId, $userId], $limit, $offset); + } +} diff --git a/lib/Db/Notification.php b/lib/Db/Notification.php new file mode 100644 index 00000000..6b2d4408 --- /dev/null +++ b/lib/Db/Notification.php @@ -0,0 +1,37 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Entity; + +/** + * @method string getUserId() + * @method void setUserId(string $value) + * @method string getPollId() + * @method void setPollId(string $value) + */ +class Notification extends Entity { + public $userId; + public $pollId; +} diff --git a/lib/Db/NotificationMapper.php b/lib/Db/NotificationMapper.php new file mode 100644 index 00000000..95978f0f --- /dev/null +++ b/lib/Db/NotificationMapper.php @@ -0,0 +1,90 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Mapper; +use OCP\IDBConnection; + +class NotificationMapper extends Mapper { + + public function __construct(IDBConnection $db) { + parent::__construct($db, 'polls_notif', '\OCA\Polls\Db\Notification'); + } + + /** + * @param int $id + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result + * @return Notification + */ + public function find($id) { + $sql = 'SELECT * FROM `*PREFIX*polls_notif` '. + 'WHERE `id` = ?'; + return $this->findEntity($sql, [$id]); + } + + /** + * @param string $userId + * @param string $from + * @param string $until + * @param int $limit + * @param int $offset + * @return Notification[] + */ + public function findBetween($userId, $from, $until, $limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_notif` '. + 'WHERE `userId` = ?'. + 'AND `timestamp` BETWEEN ? and ?'; + return $this->findEntities($sql, [$userId, $from, $until], $limit, $offset); + } + + /** + * @param int $limit + * @param int $offset + * @return Notification[] + */ + public function findAll($limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_notif`'; + return $this->findEntities($sql, [], $limit, $offset); + } + + /** + * @param int $limit + * @param int $offset + * @return Notification[] + */ + public function findAllByPoll($pollId, $limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_notif` WHERE `poll_id`=?'; + return $this->findEntities($sql, [$pollId], $limit, $offset); + } + + /** + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @return Notification + */ + public function findByUserAndPoll($pollId, $userId) { + $sql = 'SELECT * FROM `*PREFIX*polls_notif` WHERE `poll_id`=? AND `user_id`=?'; + return $this->findEntity($sql, [$pollId, $userId]); + } +} diff --git a/lib/Db/Participation.php b/lib/Db/Participation.php new file mode 100644 index 00000000..8e343e18 --- /dev/null +++ b/lib/Db/Participation.php @@ -0,0 +1,43 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Entity; + +/** + * @method timestamp getDt() + * @method void setDt(timestamp $value) + * @method string getUserId() + * @method void setUserId(string $value) + * @method integer getPollId() + * @method void setPollId(integer $value) + * @method integer getType() + * @method void setType(integer $value) + */ +class Participation extends Entity { + public $dt; + public $userId; + public $pollId; + public $type; +} diff --git a/lib/Db/ParticipationMapper.php b/lib/Db/ParticipationMapper.php new file mode 100644 index 00000000..054e1d90 --- /dev/null +++ b/lib/Db/ParticipationMapper.php @@ -0,0 +1,106 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Mapper; +use OCP\IDBConnection; + +class ParticipationMapper extends Mapper { + + public function __construct(IDBConnection $db) { + parent::__construct($db, 'polls_particip', '\OCA\Polls\Db\Participation'); + } + + /** + * @param int $id + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result + * @return Participation + */ + public function find($id) { + $sql = 'SELECT * FROM `*PREFIX*polls_particip` '. + 'WHERE `id` = ?'; + return $this->findEntity($sql, [$id]); + } + + public function deleteByPollAndUser($pollId, $userId) { + $sql = 'DELETE FROM `*PREFIX*polls_particip` WHERE poll_id=? AND user_id=?'; + $this->execute($sql, [$pollId, $userId]); + } + + /** + * @param string $userId + * @param string $from + * @param string $until + * @param int $limit + * @param int $offset + * @return Participation[] + */ + public function findBetween($userId, $from, $until, $limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_particip` '. + 'WHERE `userId` = ?'. + 'AND `timestamp` BETWEEN ? and ?'; + return $this->findEntities($sql, [$userId, $from, $until], $limit, $offset); + } + + /** + * @param int $limit + * @param int $offset + * @return Participation[] + */ + public function findAll($limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_particip`'; + return $this->findEntities($sql, [], $limit, $offset); + } + + /** + * @param string $userId + * @param int $limit + * @param int $offset + * @return Participation[] + */ + public function findDistinctByUser($userId, $limit=null, $offset=null) { + $sql = 'SELECT DISTINCT * FROM `*PREFIX*polls_particip` WHERE user_id=?'; + return $this->findEntities($sql, [$userId], $limit, $offset); + } + + /** + * @param string $userId + * @param int $limit + * @param int $offset + * @return Participation[] + */ + public function findByPoll($pollId, $limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_particip` WHERE poll_id=?'; + return $this->findEntities($sql, [$pollId], $limit, $offset); + } + + /** + * @param string $pollId + */ + public function deleteByPoll($pollId) { + $sql = 'DELETE FROM `*PREFIX*polls_particip` WHERE poll_id=?'; + $this->execute($sql, [$pollId]); + } +} diff --git a/lib/Db/ParticipationText.php b/lib/Db/ParticipationText.php new file mode 100644 index 00000000..56ac2415 --- /dev/null +++ b/lib/Db/ParticipationText.php @@ -0,0 +1,43 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Entity; + +/** + * @method text getText() + * @method void setText(text $value) + * @method string getUserId() + * @method void setUserId(string $value) + * @method integer getPollId() + * @method void setPollId(integer $value) + * @method integer getType() + * @method void setType(integer $value) + */ +class ParticipationText extends Entity { + public $text; + public $userId; + public $pollId; + public $type; +} diff --git a/lib/Db/ParticipationTextMapper.php b/lib/Db/ParticipationTextMapper.php new file mode 100644 index 00000000..efa30820 --- /dev/null +++ b/lib/Db/ParticipationTextMapper.php @@ -0,0 +1,106 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Mapper; +use OCP\IDBConnection; + +class ParticipationTextMapper extends Mapper { + + public function __construct(IDBConnection $db) { + parent::__construct($db, 'polls_particip_text', '\OCA\Polls\Db\ParticipationText'); + } + + /** + * @param int $id + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result + * @return ParticipationText + */ + public function find($id) { + $sql = 'SELECT * FROM `*PREFIX*polls_particip_text` '. + 'WHERE `id` = ?'; + return $this->findEntity($sql, [$id]); + } + + public function deleteByPollAndUser($pollId, $userId) { + $sql = 'DELETE FROM `*PREFIX*polls_particip_text` WHERE poll_id=? AND user_id=?'; + $this->execute($sql, [$pollId, $userId]); + } + + /** + * @param string $userId + * @param string $from + * @param string $until + * @param int $limit + * @param int $offset + * @return ParticipationText[] + */ + public function findBetween($userId, $from, $until, $limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_particip_text` '. + 'WHERE `userId` = ?'. + 'AND `timestamp` BETWEEN ? and ?'; + return $this->findEntities($sql, [$userId, $from, $until], $limit, $offset); + } + + /** + * @param int $limit + * @param int $offset + * @return ParticipationText[] + */ + public function findAll($limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_particip_text`'; + return $this->findEntities($sql, [], $limit, $offset); + } + + /** + * @param string $userId + * @param int $limit + * @param int $offset + * @return ParticipationText[] + */ + public function findDistinctByUser($userId, $limit=null, $offset=null) { + $sql = 'SELECT DISTINCT * FROM `*PREFIX*polls_particip_text` WHERE user_id=?'; + return $this->findEntities($sql, [$userId], $limit, $offset); + } + + /** + * @param string $userId + * @param int $limit + * @param int $offset + * @return ParticipationText[] + */ + public function findByPoll($pollId, $limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_particip_text` WHERE poll_id=?'; + return $this->findEntities($sql, [$pollId], $limit, $offset); + } + + /** + * @param string $pollId + */ + public function deleteByPoll($pollId) { + $sql = 'DELETE FROM `*PREFIX*polls_particip_text` WHERE poll_id=?'; + $this->execute($sql, [$pollId]); + } +} diff --git a/lib/Db/Text.php b/lib/Db/Text.php new file mode 100644 index 00000000..e98c168b --- /dev/null +++ b/lib/Db/Text.php @@ -0,0 +1,37 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Entity; + +/** + * @method string getText() + * @method void setText(string $value) + * @method integer getPollId() + * @method void setPollId(integer $value) + */ +class Text extends Entity { + public $text; + public $pollId; +} diff --git a/lib/Db/TextMapper.php b/lib/Db/TextMapper.php new file mode 100644 index 00000000..dde08890 --- /dev/null +++ b/lib/Db/TextMapper.php @@ -0,0 +1,75 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @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\Polls\Db; + +use OCP\AppFramework\Db\Mapper; +use OCP\IDBConnection; + +class TextMapper extends Mapper { + + public function __construct(IDBConnection $db) { + parent::__construct($db, 'polls_txts', '\OCA\Polls\Db\Text'); + } + + /** + * @param int $id + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result + * @return Text + */ + public function find($id) { + $sql = 'SELECT * FROM `*PREFIX*polls_txts` '. + 'WHERE `id` = ?'; + return $this->findEntity($sql, [$id]); + } + + /** + * @param int $limit + * @param int $offset + * @return Text[] + */ + public function findAll($limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_txts`'; + return $this->findEntities($sql, [], $limit, $offset); + } + + /** + * @param string $pollId + * @param int $limit + * @param int $offset + * @return Text[] + */ + public function findByPoll($pollId, $limit=null, $offset=null) { + $sql = 'SELECT * FROM `*PREFIX*polls_txts` WHERE poll_id=?'; + return $this->findEntities($sql, [$pollId], $limit, $offset); + } + + /** + * @param string $pollId + */ + public function deleteByPoll($pollId) { + $sql = 'DELETE FROM `*PREFIX*polls_txts` WHERE poll_id=?'; + $this->execute($sql, [$pollId]); + } +} |