Blame view
app/Repositories/ForumPostRepository.php
5.54 KB
e77200db5 Initial commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
<?php namespace FootyRoom\Repositories; use DateTime; use FootyRoom\Core\ForumPost\ForumPost; use FootyRoom\Support\AutoMapper; use Illuminate\Database\Connection; class ForumPostRepository { /** * @var \Illuminate\Database\Connection */ protected $mysql; /** * Constructor. * * @param \Illuminate\Database\Connection $mysql */ public function __construct(Connection $mysql) { $this->mysql = $mysql; } /** * Finds post by id. * * @param int $id * * @return \FootyRoom\Core\ForumPost\ForumPost */ public function findById($id) { $postDto = $this->mysql ->table('wp_posts') ->join('wp_term_relationships', 'wp_posts.id', '=', 'wp_term_relationships.object_id') ->select([ 'id', 'post_title as title', 'post_name as slug', 'post_author as userId', 'post_title as title', 'comment_status as discussionStatus', 'post_status as status', 'term_taxonomy_id as categoryId', 'post_content as content', 'post_date as date', 'guid', ]) ->where('ID', '=', $id) ->first(); if (!$postDto) { return null; } $postDto->date = new DateTime($postDto->date); $post = AutoMapper::map($postDto, ForumPost::class); return $post; } /** * Finds forum posts by title. * * @param string $q * @param int $offset * @param int $limit * * @return object[]|null */ public function findByTitle($q, $offset, $limit) { return $this->mysql ->table('wp_posts AS p') ->select(['id', 'post_title as title', 'post_name as slug', 'comment_count as commentCount']) ->join('wp_term_relationships AS tr', 'p.id', '=', 'tr.object_id') ->join('wp_terms AS t', 'tr.term_taxonomy_id', '=', 't.term_id') ->where('p.post_status', '=', 'publish') ->where('p.post_type', '=', 'post') ->where('p.post_title', 'LIKE', "%$q%") ->where('t.term_group', '=', 100) ->orderBy('p.id', 'desc') ->offset($offset) ->limit($limit) ->get(); } /** * Saves new forum post. * * @param \FootyRoom\Core\ForumPost\ForumPost $post * * @return \FootyRoom\Core\ForumPost\ForumPost | null */ public function create(ForumPost $post) { $options = [ 'post_status' => $post->isPublished() ? 'publish' : 'draft', 'post_type' => 'post', 'post_author' => $post->getUserId(), 'post_name' => $post->getSlug(), 'ping_status' => '', 'post_parent' => 0, 'post_password' => '', 'post_content' => '', 'post_title' => $post->getTitle(), 'post_date' => $post->getDate()->format('c'), 'post_date_gmt' => $post->getDate()->format('c'), ]; $postId = $this->mysql ->table('wp_posts') ->insertGetId($options); AutoMapper::setValue($post, 'id', $postId); // Associate this post with its category. $this->assignCategory($post->getId(), $post->getCategoryId()); return $post; } /** * Update post's status. * * @param \FootyRoom\Core\ForumPost\ForumPost $post */ public function updateStatus(ForumPost $post) { $this->mysql ->table('wp_posts') ->where('id', '=', $post->getId()) ->update([ 'post_status' => $post->isPublished() ? 'publish' : 'draft', ]); } /* * Updates settings of a specified forum post. * * @param \FootyRoom\Core\ForumPost\ForumPost $post */ public function updateSettings(ForumPost $post) { if (!$post->isSticky()) { $sticky = 0; } else { $sticky = $post->isLocalSticky() ? 1 : 5; } $update = [ 'sticky' => $sticky, 'post_name' => $post->getSlug(), 'comment_status' => $post->isDiscussionOpen() ? 'open' : 'closed', 'post_title' => $post->getTitle(), 'post_author' => $post->getUserId(), 'post_status' => $post->isPublished() ? 'publish' : 'draft', ]; $this->mysql ->table('wp_posts') ->where('id', '=', $post->getId()) ->update($update); // Associate this post with its category. $this->updateCategory($post->getId(), $post->getCategoryId()); } /** * Update category of existing post. * * @param int $postId * @param int $categoryId */ protected function updateCategory($postId, $categoryId) { $this->mysql ->table('wp_term_relationships') ->where('object_id', '=', $postId) ->update([ 'term_taxonomy_id' => $categoryId, ]); } /** * Assign category to new post. * * @param int $postId * @param int $categoryId */ protected function assignCategory($postId, $categoryId) { $this->mysql ->table('wp_term_relationships') ->insert([ 'object_id' => $postId, 'term_taxonomy_id' => $categoryId, ]); } /** * Increment posts comment count. * * @param int $postId */ public function incrementCommentCount($postId) { $this->mysql ->table('wp_posts') ->where('ID', '=', $postId) ->increment('comment_count'); } } |