BlockedUsers.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ T13jccjbcdXPyWH204s2cSxBfAYcT+9Dxi/+R3nE8l8=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2016 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.7
  11. */
  12. /**
  13. * blocked users table service class
  14. */
  15. namespace Ppb\Service;
  16. use Cube\Db\Expr,
  17. Ppb\Db\Table\BlockedUsers as BlockedUsersTable,
  18. Ppb\Db\Table\Row\BlockedUser as BlockedUserModel;
  19. class BlockedUsers extends AbstractService
  20. {
  21. /**
  22. *
  23. * class constructor
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $this->setTable(
  29. new BlockedUsersTable());
  30. }
  31. /**
  32. *
  33. * create or update an blocked user
  34. *
  35. * @param array $data
  36. *
  37. * @return $this
  38. */
  39. public function save($data)
  40. {
  41. $row = null;
  42. $data = $this->_prepareSaveData($data);
  43. if (array_key_exists('id', $data)) {
  44. $select = $this->_table->select()
  45. ->where("id = ?", $data['id']);
  46. unset($data['id']);
  47. $row = $this->_table->fetchRow($select);
  48. }
  49. if (count($row) > 0) {
  50. $data['updated_at'] = new Expr('now()');
  51. $this->_table->update($data, "id='{$row['id']}'");
  52. }
  53. else {
  54. $data['created_at'] = new Expr('now()');
  55. $this->_table->insert($data);
  56. }
  57. return $this;
  58. }
  59. /**
  60. *
  61. * check if a user is blocked on a certain action
  62. * if true, then return the first matching blocked user object for display purposes
  63. *
  64. * @param string $blockedAction (register, messaging, purchase)
  65. * @param array $dataToBlock
  66. * @param int|null $blockerId
  67. *
  68. * @return \Ppb\Db\Table\Row\BlockedUser|null
  69. */
  70. public function check($blockedAction, $dataToBlock = array(), $blockerId = null)
  71. {
  72. $select = $this->_table->select();
  73. $adapter = $this->_table->getAdapter();
  74. if ($blockerId !== null) {
  75. $select->where('user_id = ? or user_id is null', $blockerId);
  76. }
  77. else {
  78. $select->where('user_id is null');
  79. }
  80. $where = array();
  81. foreach ($dataToBlock as $key => $value) {
  82. if (array_key_exists($key, BlockedUserModel::$blockTypes)) {
  83. $where[] = '(' . implode(' AND ', array(
  84. $adapter->quoteInto('type = ?', $key),
  85. $adapter->quoteInto("value = ?", $value)
  86. )) . ')';
  87. }
  88. }
  89. if (count($where) > 0) {
  90. $select->where(implode(' OR ', $where));
  91. }
  92. $select->where("blocked_actions REGEXP '\"" . $blockedAction . "\"'");
  93. return $this->fetchAll($select)->getRow(0);
  94. }
  95. /**
  96. *
  97. * delete a blocked user row from the table
  98. *
  99. * @param int $id the id of the row to be deleted
  100. * @param int $userId the id of owner of the row
  101. *
  102. * @return int returns the number of affected rows
  103. */
  104. public function delete($id, $userId = null)
  105. {
  106. $adapter = $this->_table->getAdapter();
  107. $where[] = $adapter->quoteInto('id = ?', $id);
  108. if ($userId !== null) {
  109. $where[] = $adapter->quoteInto('user_id = ?', $userId);
  110. }
  111. return $this->_table->delete($where);
  112. }
  113. }