Guest User

Model Relations

a guest
Mar 25th, 2018
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.79 KB | None | 0 0
  1. <?php
  2. /* Relations
  3. * Se o usuário adicionou um subscribe inicialmente vai mostrar como 0
  4. * Caso o dono do post aceite esse subscribe, mostra como 1 e lista em yoursubscribes
  5. * Se ele recusou o subscribe vai aparecer como 3 e não aparece em yoursubscribes */
  6.  
  7. namespace App\Models;
  8.  
  9. use App\Core\Model;
  10. use App\Models\Account;
  11.  
  12. class Relations extends Model
  13. {
  14. public function verifySubscriber()
  15. {
  16. $id = addslashes($_SESSION['user']);
  17. $id_post = addslashes($_GET['id']);
  18.  
  19. $sql = "SELECT * FROM relations WHERE id_user_from = :id AND id_post = :id_post";
  20. $sql = $this->db->prepare($sql);
  21. $sql->bindValue(':id', $id);
  22. $sql->bindValue(':id_post', $id_post);
  23. try {
  24. $sql->execute();
  25.  
  26. if($sql->rowCount() > 0) {
  27.  
  28. return true;
  29.  
  30. } else {
  31.  
  32. return false;
  33. }
  34.  
  35. } catch (PDOException $e) {
  36. die($e->getMessage());
  37. }
  38. }
  39.  
  40. public function addSubscriber($id, $id_post, $id_user_to)
  41. {
  42. $id = addslashes($_SESSION['user']);
  43. $id_post = addslashes($_POST['id_post']);
  44. $id_user_to = addslashes($_POST['id_user_to']);
  45.  
  46. $sql = "SELECT * FROM relations WHERE id_user_from = :id AND id_post = :id_post";
  47. $sql = $this->db->prepare($sql);
  48. $sql->bindValue(':id', $id);
  49. $sql->bindValue(':id_post', $id_post);
  50. try {
  51. $sql->execute();
  52.  
  53. if($sql->rowCount() == 0) {
  54.  
  55. $sql = "INSERT INTO relations (id_user_from, id_user_to, id_post, status) VALUES (:id, :id_user_to, :id_post, '0')";
  56. $sql = $this->db->prepare($sql);
  57. $sql->bindValue(':id', $id);
  58. $sql->bindValue(':id_user_to', $id_user_to);
  59. $sql->bindValue(':id_post', $id_post);
  60. $sql->execute();
  61.  
  62. $array = "Subscribe Success!";
  63. return header("Location: ".BASE."subscribes");
  64.  
  65. } else {
  66.  
  67. return false;
  68. }
  69.  
  70. } catch (PDOException $e) {
  71. die($e->getMessage());
  72. }
  73. }
  74.  
  75. public function acceptSubscriber($id_from, $id_to, $id_post)
  76. {
  77. $id_to = $_POST['id_user_to'];
  78. $id_post = $_POST['id_post'];
  79. $id_from = $_SESSION['user'];
  80.  
  81. $sql = "UPDATE relations SET status = '1' WHERE id_user_from = '$id_to' AND id_user_to = '$id_from' AND id_post = '$id_post'";
  82. try {
  83. $this->db->query($sql);
  84.  
  85. } catch (PDOException $e) {
  86. die($e->getMessage());
  87. }
  88.  
  89. }
  90.  
  91. public function notSubscriber($id_from, $id_to, $id_post)
  92. {
  93. $id_to = $_POST['id_user_to'];
  94. $id_post = $_POST['id_post'];
  95. $id_from = $_SESSION['user'];
  96.  
  97. $sql = "UPDATE relations SET status = '2' WHERE id_user_from = '$id_to' AND id_user_to = '$id_from' AND id_post = '$id_post'";
  98. try {
  99. $this->db->query($sql);
  100.  
  101. } catch (PDOException $e) {
  102. die($e->getMessage());
  103. }
  104.  
  105. }
  106.  
  107. public function getRequisicoes()
  108. {
  109. $array = array();
  110.  
  111. $id_user = $_SESSION['user'];
  112.  
  113. $sql = "SELECT
  114.  
  115. user.*,
  116.  
  117. relations.*,
  118.  
  119. post.*, post.url as main_image
  120.  
  121. FROM relations
  122.  
  123. LEFT JOIN users as user ON user.id = relations.id_user_from
  124.  
  125. LEFT JOIN posts_pl as post ON post.id = relations.id_post
  126.  
  127. WHERE relations.id_post IN (post.id) AND relations.id_user_to = $id_user AND relations.status = '0' ORDER BY post.id";
  128.  
  129. $sql = $this->db->query($sql);
  130.  
  131.  
  132.  
  133. if($sql->rowCount() > 0) {
  134.  
  135. foreach ($result = $sql->fetchAll() as $value) {
  136.  
  137. $array[ ] = [
  138. 'id_freelancer' => $value['id_user_from'],
  139. 'id_userPost' => $value['id_user'],
  140. 'id_post' => $value['id']
  141. ];
  142.  
  143.  
  144. }
  145.  
  146. }
  147.  
  148. return $result;
  149. }
  150.  
  151. public function getList()
  152. {
  153. $array = array();
  154.  
  155. $sql = "SELECT *
  156.  
  157. FROM relations
  158.  
  159. WHERE relations.id_user_to = 1 AND relations.status = 0 ";
  160.  
  161. $sql = $this->db->query($sql);
  162.  
  163. if($sql->rowCount() > 0) {
  164.  
  165. foreach ($sql->fetchAll() as $value) {
  166.  
  167. $ids['id_post'][] = $value['id_post'];
  168. $ids['id_host'] = $value['id_user_to'];
  169. $ids['id_user'][] = $value['id_user_from'];
  170.  
  171. }
  172. return $ids;
  173.  
  174. }
  175.  
  176.  
  177. }
  178.  
  179. public function getListPost()
  180. {
  181. $array = array();
  182.  
  183.  
  184. $ids = self::getList();
  185. $list = self::getListUser();
  186.  
  187. $id_host = $ids['id_host'];
  188. $id_post = $ids['id_post'];
  189. $id_user = $ids['id_user'];
  190.  
  191. $id_post = implode(", ", $id_post);
  192. $id_user = implode(", ", $id_user);
  193.  
  194. $sql = "SELECT *
  195. FROM posts_pl
  196.  
  197. WHERE id_user = '$id_host' AND id in ($id_post)";
  198.  
  199. $sql = $this->db->query($sql);
  200.  
  201. if($sql->rowCount() > 0) {
  202.  
  203. foreach ($sql->fetchAll() as $value) {
  204.  
  205.  
  206. $array['host'] = ['id_post' => $value['id'],
  207. 'id_host' => $value['id_user'],
  208. 'id_category' => $value['id_category'],
  209. 'title' => $value['title'],
  210. 'description' => $value['description'],
  211. 'main_image' => $value['url'],
  212. 'date_create' => $value['date_create']
  213. ];
  214.  
  215.  
  216. }
  217.  
  218. }
  219.  
  220. $array['freelancer'] = $list;
  221.  
  222. return $array;
  223. }
  224.  
  225. public function getListUser()
  226. {
  227. $array = array();
  228.  
  229. $list = self::getList();
  230.  
  231. $id_user = $list['id_user'];
  232. $id_user = implode(", ", $id_user);
  233.  
  234. $sql = "SELECT *
  235.  
  236. FROM users
  237.  
  238. WHERE id in ($id_user)";
  239.  
  240. $sql = $this->db->query($sql);
  241.  
  242. if($sql->rowCount() > 0) {
  243.  
  244. foreach ($sql->fetchAll() as $key => $value) {
  245.  
  246. $array[ ] = ['id_freelancer' => $value['id'],
  247. 'id_host' => $list['id_host'],
  248. 'id_post' => $list['id_post'][0],
  249. 'name' => $value['name'],
  250. 'email' => $value['email'],
  251. 'url' => $value['url'],
  252. 'bio' => $value['bio'],
  253. 'phone' => $value['phone'],
  254. 'date_joined' => $value['date_joined']
  255. ];
  256. }
  257. }
  258.  
  259. return $array;
  260. }
  261.  
  262. public function getAll()
  263. {
  264. $array = array();
  265.  
  266. $Host = self::getListPost();
  267. $User = self::getListUser();
  268.  
  269. $array[] = array_merge($Host, $User);
  270.  
  271.  
  272. return $array;
  273. }
  274.  
  275. /*
  276. public function listSubscribes()
  277. {
  278. $array = array();
  279.  
  280. $sql = "SELECT users.*, relations.id_user_from, posts_pl.id, posts_pl.id_user FROM relations
  281.  
  282. LEFT JOIN users ON users.id = relations.id_user_from
  283.  
  284. LEFT JOIN posts_pl ON id_user = '".($_SESSION['user'])."'
  285.  
  286. WHERE relations.id_user_to = '".($_SESSION['user'])."' AND relations.id_post = posts_pl.id AND relations.status = ORDER BY users.id";
  287.  
  288. $sql = $this->db->query($sql);
  289.  
  290. if($sql->rowCount() > 0) {
  291.  
  292. $array = $sql->fetchAll();
  293.  
  294. }
  295.  
  296. return $array;
  297. }
  298. */
  299.  
  300. public function getIdsSubs($id) {
  301. $array = array();
  302.  
  303. $sql = "SELECT * FROM relations WHERE (id_user_from = '$id' OR id_user_to = '$id') AND status = '1'";
  304. $sql = $this->db->query($sql);
  305.  
  306. if($sql->rowCount() > 0) {
  307. foreach($sql->fetchAll() as $ritem) {
  308. $array[] = $ritem['id_user_from'];
  309. $array[] = $ritem['id_user_to'];
  310. }
  311. }
  312.  
  313. return $array;
  314. }
  315.  
  316. public function getSubscribers()
  317. {
  318. $array = array();
  319.  
  320. $sql = "SELECT posts_pl.id, posts_pl.id_user FROM relations
  321.  
  322. LEFT JOIN posts_pl ON id_user = relations.id_user_to
  323.  
  324. WHERE relations.id_user_from = '".($_SESSION['user'])."' AND relations.id_post = posts_pl.id AND relations.status = 1 ORDER BY posts_pl.id_user";
  325.  
  326. $sql = $this->db->query($sql);
  327.  
  328. if($sql->rowCount() > 0) {
  329.  
  330. $array = $sql->fetchAll();
  331.  
  332. }
  333.  
  334. return $array;
  335. }
  336.  
  337. public function getTotalSubs()
  338. {
  339. $array = array();
  340. $sql = "SELECT COUNT(*) as totalSubs FROM relations WHERE id_user_to = '".($_SESSION['user'])."' AND relations.status = 0";
  341. $sql = $this->db->query($sql);
  342. $sql = $sql->fetch();
  343.  
  344. return $sql['totalSubs'];
  345.  
  346.  
  347. }
  348.  
  349. public function getTotalAmigos($id) {
  350.  
  351. $sql = "SELECT COUNT(*) as c FROM relacionamentos WHERE (usuario_de = '$id' OR usuario_para = '$id') AND status = '1'";
  352. $sql = $this->db->query($sql);
  353. $sql = $sql->fetch();
  354.  
  355. return $sql['c'];
  356. }
  357.  
  358. public function getIdsFriends($id) {
  359. $array = array();
  360.  
  361. $sql = "SELECT * FROM relacionamentos WHERE (usuario_de = '$id' OR usuario_para = '$id') AND status = '1'";
  362. $sql = $this->db->query($sql);
  363.  
  364. if($sql->rowCount() > 0) {
  365. foreach ($sql->fetchAll() as $ritem ) {
  366. $array[] = $ritem['usuario_de'];
  367. $array[] = $ritem['usuario_para'];
  368. }
  369. }
  370.  
  371. return $array;
  372. }
  373.  
  374. /* public function getRel() {
  375. $id = addslashes($_SESSION['user']);
  376.  
  377. $sql = "SELECT *, posts_pl.id FROM relations, posts_pl WHERE relations.id_user_from = $id AND relations.id_post = posts_pl.id";
  378. $sql = $this->db->prepare($sql);
  379. //$sql->bindValue(':id', $id);
  380. try {
  381. $sql->execute();
  382.  
  383. if($sql->rowCount() > 0) {
  384. var_dump($sql);
  385. exit;
  386.  
  387. return true;
  388.  
  389. } else {
  390. return false;
  391. }
  392. } catch (PDOException $e) {
  393. die($e->getMessage());
  394. }
  395.  
  396. }
  397. */
  398. }
Advertisement
Add Comment
Please, Sign In to add comment