NewsSubmissionDAO   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 311
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 197
dl 0
loc 311
rs 10
c 0
b 0
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getAllSubmissions() 0 56 2
A __construct() 0 2 1
A getSubmissionCount() 0 29 2
A getNewsText() 0 21 2
A getSubmissionQuery() 0 29 2
B getSpecificSubmissions() 0 79 2
A getAllSubmissionsForUser() 0 56 2
1
<?php
2
namespace AL\Common\DAO;
3
4
require_once __DIR__."/../../lib/Db.php" ;
5
require_once __DIR__."/../../lib/functions.php" ;
6
require_once __DIR__."/../Model/News/NewsSubmission.php" ;
7
8
use AL\Common\Model\NewsSubmission;
9
10
/**
11
 * DAO for News Submlissions
12
 */
13
 
14
class NewsSubmissionDAO {
15
    private $mysqli;
16
17
    public function __construct($mysqli) {
18
        $this->mysqli = $mysqli;
19
    }
20
    
21
    private function getSubmissionQuery($user_id = null, $news_id = null) {
0 ignored issues
show
Unused Code introduced by
The parameter $news_id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

21
    private function getSubmissionQuery($user_id = null, /** @scrutinizer ignore-unused */ $news_id = null) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
22
        $query =  "SELECT
23
                news_submission.news_submission_id,
24
                  news_submission.news_headline,
25
                  news_submission.news_text, 
26
                  news_submission.news_date,
27
                  news_image.news_image_id,
28
                  CONCAT(news_image.news_image_id, \".\", news_image.news_image_ext) as news_image,
29
                  news_submission.user_id,
30
                  users.userid,
31
                  users.email,
32
                  users.join_date,
33
                  users.karma,
34
                  users.show_email,
35
                  users.avatar_ext,
36
                  (SELECT COUNT(*) 
37
                        FROM news_submission 
38
                            WHERE news_submission.user_id = users.user_id) AS user_submission_count
39
                  FROM news_submission
40
                  LEFT JOIN news_image ON (news_submission.news_image_id = news_image.news_image_id)
41
                  LEFT JOIN users ON (news_submission.user_id = users.user_id)";
42
              
43
        if (isset($user_id)) {
44
            $query .= " WHERE news_submission.user_id = ?";
45
        }
46
47
        $query .= " ORDER BY news_date DESC";
48
49
        return $query;
50
    }
51
    
52
    /**
53
    * Return all news submissions, sorted by descending date
54
    * @return \AL\Common\Model\News\News[] An array of news
55
    */
56
    public function getAllSubmissions() {
57
        $stmt = \AL\Db\execute_query(
58
            "NewsSubmissionDAO: getAllSubmissions",
59
            $this->mysqli,
60
            $this->getSubmissionQuery(),
61
            null, null
62
        );
63
64
        \AL\Db\bind_result(
65
            "NewsSubmissionDAO: get All Submissions",
66
            $stmt,
67
            $id,
68
            $headline,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $headline seems to be never defined.
Loading history...
69
            $text,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $text seems to be never defined.
Loading history...
70
            $date,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $date seems to be never defined.
Loading history...
71
            $image_id,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $image_id seems to be never defined.
Loading history...
72
            $image,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $image seems to be never defined.
Loading history...
73
            $userid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userid seems to be never defined.
Loading history...
74
            $username,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $username seems to be never defined.
Loading history...
75
            $email,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $email seems to be never defined.
Loading history...
76
            $join_date,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $join_date seems to be never defined.
Loading history...
77
            $karma,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $karma seems to be never defined.
Loading history...
78
            $show_email,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $show_email seems to be never defined.
Loading history...
79
            $avatar_ext,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $avatar_ext seems to be never defined.
Loading history...
80
            $user_subm_count
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user_subm_count seems to be never defined.
Loading history...
81
        );
82
83
        $news = [];
84
        
85
        while ($stmt->fetch()) {
86
            $text = nl2br($text);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $text does not seem to be defined for all execution paths leading up to this point.
Loading history...
87
            $text = InsertALCode($text);
88
            $text = trim($text);
89
            $text = RemoveSmillies($text);
90
        
91
            $news[] = new \AL\Common\Model\NewsSubmission\NewsSubmission(
92
                $id,
93
                $headline,
94
                $text,
95
                $date,
96
                $image_id,
97
                $image,
98
                $userid,
99
                $username,
100
                $email,
101
                $join_date,
102
                $karma,
103
                $show_email,
104
                $avatar_ext,
105
                $user_subm_count
106
            );
107
        }
108
109
        $stmt->close();
110
111
        return $news;
112
    }
113
    
114
    /**
115
    * Return all news submissions, sorted by descending date
116
    * @return \AL\Common\Model\News\News[] An array of news
117
    */
118
    public function getAllSubmissionsForUser($user_id) {
119
        $stmt = \AL\Db\execute_query(
120
            "NewsSubmissionDAO: getAllSubmissions",
121
            $this->mysqli,
122
            $this->getSubmissionQuery($user_id),
123
            "i", $user_id
124
        );
125
126
        \AL\Db\bind_result(
127
            "NewsSubmissionDAO: get All Submissions",
128
            $stmt,
129
            $id,
130
            $headline,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $headline seems to be never defined.
Loading history...
131
            $text,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $text seems to be never defined.
Loading history...
132
            $date,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $date seems to be never defined.
Loading history...
133
            $image_id,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $image_id seems to be never defined.
Loading history...
134
            $image,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $image seems to be never defined.
Loading history...
135
            $userid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userid does not exist. Did you maybe mean $user_id?
Loading history...
136
            $username,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $username seems to be never defined.
Loading history...
137
            $email,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $email seems to be never defined.
Loading history...
138
            $join_date,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $join_date seems to be never defined.
Loading history...
139
            $karma,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $karma seems to be never defined.
Loading history...
140
            $show_email,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $show_email seems to be never defined.
Loading history...
141
            $avatar_ext,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $avatar_ext seems to be never defined.
Loading history...
142
            $user_subm_count
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user_subm_count seems to be never defined.
Loading history...
143
        );
144
145
        $news = [];
146
        
147
        while ($stmt->fetch()) {
148
            $text = nl2br($text);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $text does not seem to be defined for all execution paths leading up to this point.
Loading history...
149
            $text = InsertALCode($text);
150
            $text = trim($text);
151
            $text = RemoveSmillies($text);
152
        
153
            $news[] = new \AL\Common\Model\NewsSubmission\NewsSubmission(
154
                $id,
155
                $headline,
156
                $text,
157
                $date,
158
                $image_id,
159
                $image,
160
                $userid,
161
                $username,
162
                $email,
163
                $join_date,
164
                $karma,
165
                $show_email,
166
                $avatar_ext,
167
                $user_subm_count
168
            );
169
        }
170
171
        $stmt->close();
172
173
        return $news;
174
    }
175
    
176
    
177
    /**
178
    * Return a specific news submissions
179
    * @return \AL\Common\Model\News\News[] An array of news
180
    */
181
    public function getSpecificSubmissions($news_id) {
182
        $stmt = \AL\Db\execute_query(
183
            "NewsSubmissionDAO: getSpecificSubmissions",
184
            $this->mysqli,
185
            "SELECT
186
            news_submission.news_submission_id,
187
            news_submission.news_headline,
188
            news_submission.news_text, 
189
            news_submission.news_date,
190
            news_image.news_image_id,
191
            CONCAT(news_image.news_image_id, \".\", news_image.news_image_ext) as news_image,
192
            news_submission.user_id,
193
            users.userid,
194
            users.email,
195
            users.join_date,
196
            users.karma,
197
            users.show_email,
198
            users.avatar_ext,
199
            (SELECT COUNT(*) 
200
                FROM news_submission 
201
                    WHERE news_submission.user_id = users.user_id) AS user_submission_count
202
            FROM news_submission
203
            LEFT JOIN news_image ON (news_submission.news_image_id = news_image.news_image_id)
204
            LEFT JOIN users ON (news_submission.user_id = users.user_id)
205
            WHERE news_submission.news_submission_id = ?",
206
            "i",
207
            $news_id
208
        );
209
210
        \AL\Db\bind_result(
211
            "NewsSubmissionDAO: get specific Submissions",
212
            $stmt,
213
            $id,
214
            $headline,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $headline seems to be never defined.
Loading history...
215
            $text,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $text seems to be never defined.
Loading history...
216
            $date,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $date seems to be never defined.
Loading history...
217
            $image_id,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $image_id seems to be never defined.
Loading history...
218
            $image,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $image seems to be never defined.
Loading history...
219
            $userid,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $userid seems to be never defined.
Loading history...
220
            $username,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $username seems to be never defined.
Loading history...
221
            $email,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $email seems to be never defined.
Loading history...
222
            $join_date,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $join_date seems to be never defined.
Loading history...
223
            $karma,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $karma seems to be never defined.
Loading history...
224
            $show_email,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $show_email seems to be never defined.
Loading history...
225
            $avatar_ext,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $avatar_ext seems to be never defined.
Loading history...
226
            $user_subm_count
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $user_subm_count seems to be never defined.
Loading history...
227
        );
228
229
        $news = [];
230
        
231
        while ($stmt->fetch()) {
232
            //$text = nl2br($text);
233
            //$text = InsertALCode($text);
234
            //$text = trim($text);
235
            //$text = RemoveSmillies($text);
236
            $breaks = array("<br />","<br>","<br/>");
237
            $text = str_ireplace($breaks, "\r\n", $text);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $text does not seem to be defined for all execution paths leading up to this point.
Loading history...
238
        
239
            $news[] = new \AL\Common\Model\NewsSubmission\NewsSubmission(
240
                $id,
241
                $headline,
242
                $text,
243
                $date,
244
                $image_id,
245
                $image,
246
                $userid,
247
                $username,
248
                $email,
249
                $join_date,
250
                $karma,
251
                $show_email,
252
                $avatar_ext,
253
                $user_subm_count
254
            );
255
        }
256
257
        $stmt->close();
258
259
        return $news;
260
    }
261
  
262
    /**
263
    * Get the total count of comments on the website
264
    *
265
    * @param  integer $user_id Optional ID of a user to count comments for
266
    * @return integer Number of comments     */
267
    public function getSubmissionCount($user_id = null) {
268
        if (isset($user_id)) {
269
            $stmt = \AL\Db\execute_query(
270
                "NewsSubmissionDAO: Get submission count for user_id $user_id",
271
                $this->mysqli,
272
                "SELECT COUNT(*) FROM news_submission WHERE user_id = ?",
273
                "i",
274
                $user_id
275
            );
276
        } else {
277
            $stmt = \AL\Db\execute_query(
278
                "NewsSubmissionDAO: Get submission count in database",
279
                $this->mysqli,
280
                "SELECT COUNT(*) FROM news_submission",
281
                null,
282
                null
283
            );
284
        }
285
286
        \AL\Db\bind_result(
287
            "NewsSubmissionDAO: Get submissino count",
288
            $stmt,
289
            $count
290
        );
291
292
        $stmt->fetch();
293
        $stmt->close();
294
295
        return $count;
296
    }
297
    
298
    /**
299
    * Get the comment text for a specific comment
300
    *
301
    * @param  integer $comments_id ID of a comment
302
    * @return text the text of the comment
0 ignored issues
show
Bug introduced by
The type AL\Common\DAO\text was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
303
    */
304
    public function getNewsText($news_id = null) {
305
        if (isset($news_id)) {
306
            $stmt = \AL\Db\execute_query(
307
                "NewsSubmissionDAO: Get news text for news_id $news_id",
308
                $this->mysqli,
309
                "SELECT news_text FROM news_submission WHERE news_submission_id = ?",
310
                "i",
311
                $news_id
312
            );
313
        }
314
315
        \AL\Db\bind_result(
316
            "NewsSubmissionDAO: Get news text",
317
            $stmt,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $stmt does not seem to be defined for all execution paths leading up to this point.
Loading history...
318
            $news
319
        );
320
321
        $stmt->fetch();
322
        $stmt->close();
323
324
        return $news;
325
    }
326
}
327