1
0

CardSearchDao.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\ContactsInteraction\Db;
  25. use OCP\DB\QueryBuilder\IQueryBuilder;
  26. use OCP\IDBConnection;
  27. use OCP\IUser;
  28. use function is_resource;
  29. use function stream_get_contents;
  30. class CardSearchDao {
  31. private IDBConnection $db;
  32. public function __construct(IDBConnection $db) {
  33. $this->db = $db;
  34. }
  35. public function findExisting(IUser $user,
  36. ?string $uid,
  37. ?string $email,
  38. ?string $cloudId): ?string {
  39. $addressbooksQuery = $this->db->getQueryBuilder();
  40. $cardQuery = $this->db->getQueryBuilder();
  41. $propQuery = $this->db->getQueryBuilder();
  42. $propOr = $propQuery->expr()->orX();
  43. if ($uid !== null) {
  44. $propOr->add($propQuery->expr()->andX(
  45. $propQuery->expr()->eq('name', $cardQuery->createNamedParameter('UID')),
  46. $propQuery->expr()->eq('value', $cardQuery->createNamedParameter($uid))
  47. ));
  48. }
  49. if ($email !== null) {
  50. $propOr->add($propQuery->expr()->andX(
  51. $propQuery->expr()->eq('name', $cardQuery->createNamedParameter('EMAIL')),
  52. $propQuery->expr()->eq('value', $cardQuery->createNamedParameter($email))
  53. ));
  54. }
  55. if ($cloudId !== null) {
  56. $propOr->add($propQuery->expr()->andX(
  57. $propQuery->expr()->eq('name', $cardQuery->createNamedParameter('CLOUD')),
  58. $propQuery->expr()->eq('value', $cardQuery->createNamedParameter($cloudId))
  59. ));
  60. }
  61. $addressbooksQuery->selectDistinct('id')
  62. ->from('addressbooks')
  63. ->where($addressbooksQuery->expr()->eq('principaluri', $cardQuery->createNamedParameter("principals/users/" . $user->getUID())));
  64. $propQuery->selectDistinct('cardid')
  65. ->from('cards_properties')
  66. ->where($propQuery->expr()->in('addressbookid', $propQuery->createFunction($addressbooksQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
  67. ->andWhere($propOr)
  68. ->groupBy('cardid');
  69. $cardQuery->select('carddata')
  70. ->from('cards')
  71. ->where($cardQuery->expr()->in('id', $cardQuery->createFunction($propQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
  72. ->andWhere($cardQuery->expr()->in('addressbookid', $cardQuery->createFunction($addressbooksQuery->getSQL()), IQueryBuilder::PARAM_INT_ARRAY))
  73. ->setMaxResults(1);
  74. $result = $cardQuery->execute();
  75. /** @var string|resource|false $card */
  76. $card = $result->fetchOne();
  77. if ($card === false) {
  78. return null;
  79. }
  80. if (is_resource($card)) {
  81. return stream_get_contents($card);
  82. }
  83. return $card;
  84. }
  85. }