1 <?php
2
3 namespace PLus\Orders\Repositories\Messages;
4
5 6 7 8 9
10 class NewsRepository extends \PLus\Orders\Repositories\Repository {
11
12 13 14 15
16 public function getAllValidNews() {
17 return $this->getTable()->select('news.*, CONCAT_WS(\' \', user.firstname, user.lastname) AS uname')
18 ->where('valid', 1)
19 ->order('id DESC');
20 }
21
22 23 24 25 26
27 public function getLimitValidNews($limit) {
28 return $this->getTable()->select('news.*, CONCAT_WS(\' \', user.firstname, user.lastname) AS uname')
29 ->where('valid', 1)
30 ->limit($limit)
31 ->order('id DESC');
32 }
33
34 35 36 37 38
39 public function getGridSelectionNews($user_id) {
40 return $this->connection->query("SELECT DISTINCT `news`.`id`,`news`.`name`, `news`.`created`, `news`.`valid`, CONCAT_WS(' ', `user`.`firstname`,
41 `user`.`lastname`) AS `uname`, (SELECT COUNT(`id`) FROM `visit` WHERE `visit`.`news_id` = `news`.`id`
42 AND `visit`.`user_id` = $user_id) AS cnt
43 FROM `news`
44 LEFT JOIN `visit` ON `news`.`id` = `visit`.`news_id`
45 LEFT JOIN `user` ON `news`.`user_id` = `user`.`id`");
46 }
47
48
49
50
51 52 53 54
55 public function getCountValidNews() {
56 return $this->getTable()->select('COUNT(*) AS cntvalid')
57 ->where('valid', 1)
58 ->fetch();
59 }
60
61 62 63
64 public function getValidNews() {
65 return $this->getTable()->select('id')
66 ->where('valid', 1);
67 }
68
69 70 71 72 73 74
75 public function getCountVisitValidNews($ids, $user_id) {
76 return $this->connection->table('visit')->select('COUNT(*) AS cntvisit')
77 ->where('news_id', $ids)
78 ->where('user_id', $user_id)
79 ->fetch();
80 }
81
82
83 84 85 86 87 88
89 public function stampVisit($news_id, $user_id) {
90 try {
91 $this->connection->table('visit')->insert(array('news_id' => $news_id, 'user_id' => $user_id));
92 }
93 catch ( \PDOException $e) {
94 if ($e->getCode() == '23000')
95 $this->connection->table('visit')->where(array('news_id' => $news_id, 'user_id' => $user_id))
96 ->update(array('news_id' => $news_id, 'user_id' => $user_id));
97 else
98 throw $e;
99 }
100 }
101 }
102