Failed Conditions
Branch master (3ce7e2)
by Nick
14:43
created

COMMENTLIST::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 2
rs 10
ccs 0
cts 2
cp 0
crap 2
1
<?php
2
3
/*	 The class for displaying one or more comments.
4
    (There's also a function for adding a new comment to the DB because I wasn't
5
    sure where else to put it!).
6
7
    This works similarly to the HANSARDLIST class.
8
9
    To display all the comments for an epobject you'll do:
10
11
        $args = array ('epobject_id' => $epobject_id);
12
        $COMMENTLIST = new COMMENTLIST;
13
        $COMMENTLIST->display ('ep', $args);
14
15
    This will call the _get_data_by_ep() function which passes variables to the
16
    _get_comment_data() function. This gets the comments from the DB and returns
17
    an array of comments.
18
19
    The render() function is then called, which includes a template and
20
    goes through the array, displaying the comments. See the HTML comments.php
21
    template for the format.
22
    NOTE: You'll need to pass the 'body' of the comment through filter_user_input()
23
    and linkify() first.
24
25
    You could also just call the $COMMENTLIST->render() array with an array
26
    of comment data and display directly (used for previewing user input).
27
28
*/
29
30
class COMMENTLIST {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
31
32
    public function __construct() {
33
        global $this_page;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
34
35
        $this->db = new ParlDB;
0 ignored issues
show
Bug Best Practice introduced by
The property db does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
37
        // We use this to create permalinks to comments. For the moment we're
38
        // assuming they're on the same page we're currently looking at:
39
        // debate, wran, etc.
40
        $this->page = $this_page;
0 ignored issues
show
Bug Best Practice introduced by
The property page does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
41
42
    }
43
44
45
    public function display ($view, $args=array(), $format='html') {
46
        // $view is what we're viewing by:
47
        //	'ep' is all the comments attached to an epobject.
48
        //	'user' is all the comments written by a user.
49
        //	'recent' is the most recent comments.
50
51
        // $args is an associative array of stuff like
52
        //	'epobject_id' => '37'
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
53
        // Where 'epobject_id' is an epobject_id.
54
        // Or 'gid' is a hansard item gid.
55
56
        // Replace a hansard object gid with an epobject_id.
57
//		$args = $this->_fix_gid($args);
58
59
        // $format is the format the data should be rendered in.
60
61
        if ($view == 'ep' || $view == 'user' || $view == 'recent' || $view == 'search' || $view == 'dates') {
62
            // What function do we call for this view?
63
            $function = '_get_data_by_'.$view;
64
            // Get all the dta that's to be rendered.
65
            $data = $this->$function($args);
66
67
        } else {
68
            // Don't have a valid $view;
69
            $PAGE->error_message ("You haven't specified a view type.");
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $PAGE seems to be never defined.
Loading history...
70
            return false;
71
        }
72
73
        if ($view == 'user') {
74
            $template = 'comments_user';
75
        } elseif ($view == 'recent' or $view == 'dates') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
76
            $template = 'comments_recent';
77
        } elseif ($view == 'search') {
78
            $template = 'comments_search';
79
        } else {
80
            $template = 'comments';
81
        }
82
83
        $this->render($data, $format, $template);
84
85
        return true;
86
    }
87
88
    public function render($data, $format='html', $template='comments') {
89
        include (INCLUDESPATH."easyparliament/templates/$format/$template.php");
1 ignored issue
show
Bug introduced by
The constant INCLUDESPATH was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
90
    }
91
92
    public function _get_data_by_ep($args) {
93
        // Get all the data attached to an epobject.
94
        global $PAGE;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
95
96
        twfy_debug (get_class($this), "getting data by epobject");
97
98
        // What we return.
99
        $data = array();
100
        if (!is_numeric($args['epobject_id'])) {
101
            $PAGE->error_message ("Sorry, we don't have a valid epobject id");
102
            return $data;
103
        }
104
105
        // For getting the data.
106
        $input = array (
107
            'amount' => array (
108
                'user' => true
109
            ),
110
            'where' => array (
111
                'comments.epobject_id=' => $args['epobject_id'],
112
                #'visible=' => '1'
113
            ),
114
            'order' => 'posted ASC'
115
        );
116
117
        $commentsdata = $this->_get_comment_data($input);
118
119
        $data['comments'] = $commentsdata;
120
121
        if (isset($args['user_id']) && $args['user_id'] != '') {
122
            // We'll pass this on to the template so it can highlight the user's comments.
123
            $data['info']['user_id'] = $args['user_id'];
124
        }
125
126
        return $data;
127
128
    }
129
130
131
132
    public function _get_data_by_user($args) {
133
        // Get a user's most recent comments.
134
        // Could perhaps be modified to get different lists of a user's
135
        // comments by things in $args?
136
        global $PAGE;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
137
138
        twfy_debug (get_class($this), "getting data by user");
139
140
        // What we return.
141
        $data = array();
142
143
        if (!is_numeric($args['user_id'])) {
144
            $PAGE->error_message ("Sorry, we don't have a valid user id");
145
            return $data;
146
        }
147
148
        if (isset($args['num']) && is_numeric($args['num'])) {
149
            $num = $args['num'];
150
        } else {
151
            $num = 10;
152
        }
153
154
        if (isset($args['page']) && is_numeric($args['page']) && $args['page']>1) {
155
            $page = $args['page'];
156
        } else {
157
            $page = 1;
158
        }
159
160
        $limit = $num*($page-1) . ',' . $num;
161
162
        // We're getting the most recent comments posted to epobjects.
163
        // We're grouping them by epobject so we can just link to each hansard thing once.
164
        // When there are numerous comments on an epobject we're getting the most recent
165
        // 		comment_id and posted date.
166
        // We're getting the body details for the epobject.
167
        // We're NOT getting the comment bodies. Why? Because adding them to this query
168
        // would fetch the text for the oldest comment on an epobject group, rather
169
        // than the most recent. So we'll get the comment bodies later...
170
        $q = $this->db->query("SELECT MAX(comments.comment_id) AS comment_id,
171
                                MAX(comments.posted) AS posted,
172
                                COUNT(*) AS total_comments,
173
                                comments.epobject_id,
174
                                hansard.major,
175
                                hansard.gid,
176
                                users.firstname,
177
                                users.lastname,
178
                                epobject.body
179
                        FROM 	comments
180
                            join hansard  on comments.epobject_id = hansard.epobject_id
181
                            join users    on comments.user_id = users.user_id
182
                            join epobject on comments.epobject_id = epobject.epobject_id
183
                        where	users.user_id=:user_id
184
                        AND 	visible='1'
185
                        GROUP BY epobject_id
186
                        ORDER BY posted DESC
187
                        LIMIT " . $limit,
188
                        array(':user_id' => $args['user_id'])
189
                        );
190
191
        $comments = array();
192
        $comment_ids = array();
193
194
        if ($q->rows() > 0) {
195
196
            for ($n=0; $n<$q->rows(); $n++) {
197
198
                // All the basic stuff...
199
                $comments[$n] = array (
200
                    'comment_id'		=> $q->field($n, 'comment_id'),
201
                    'posted'			=> $q->field($n, 'posted'),
202
                    'total_comments'	=> $q->field($n, 'total_comments'),
203
                    'epobject_id'		=> $q->field($n, 'epobject_id'),
204
                    'firstname'			=> $q->field($n, 'firstname'),
205
                    'lastname'			=> $q->field($n, 'lastname'),
206
                    // Hansard item body, not comment body.
207
                    'hbody'				=> $q->field($n, 'body'),
208
                );
209
210
                // Add the URL...
211
                $urldata = array (
212
                    'major' 		=> $q->field($n, 'major'),
213
                    'gid'			=> $q->field($n, 'gid'),
214
                    'comment_id'	=> $q->field($n, 'comment_id'),
215
                    'user_id'		=> $args['user_id']
216
                );
217
218
                $comments[$n]['url'] = $this->_comment_url($urldata);
219
220
                // We'll need these for getting the comment bodies.
221
                $comment_ids[] = $q->field($n, 'comment_id');
222
223
            }
224
225
            $in = implode(', ', $comment_ids);
226
227
            $r = $this->db->query("SELECT comment_id,
228
                                    body
229
                            FROM	comments
230
                            WHERE	comment_id IN ($in)
231
                            ");
232
233
            if ($r->rows() > 0) {
234
235
                $commentbodies = array();
236
237
                for ($n=0; $n<$r->rows(); $n++) {
238
                    $commentbodies[ $r->field($n, 'comment_id') ] = $r->field($n, 'body');
239
                }
240
241
                // This does rely on both this and the previous query returning
242
                // stuff in the same order...
243
                foreach ($comments as $n => $commentdata) {
244
                    $comments[$n]['body'] = $commentbodies[ $comments[$n]['comment_id'] ];
245
                }
246
            }
247
        }
248
249
        $data['comments'] = $comments;
250
        $data['results_per_page'] = $num;
251
        $data['page'] = $page;
252
        $q = $this->db->query('SELECT COUNT(DISTINCT(epobject_id)) AS count FROM comments WHERE visible=1 AND user_id=' . $args['user_id']);
253
        $data['total_results'] = $q->field(0, 'count');
254
        return $data;
255
256
    }
257
258
259
260
    public function _get_data_by_recent($args) {
261
        // $args should contain 'num', indicating how many to get.
262
        // and perhaps pid too, for a particular person
263
264
        twfy_debug (get_class($this), "getting data by recent");
265
266
        // What we return.
267
        $data = array();
268
269
        if (isset($args['num']) && is_numeric($args['num'])) {
270
            $num = $args['num'];
271
        } else {
272
            $num = 25;
273
        }
274
275
        if (isset($args['page']) && is_numeric($args['page'])) {
276
            $page = $args['page'];
277
        } else {
278
            $page = 1;
279
        }
280
281
        $limit = $num*($page-1) . ',' . $num;
282
283
        $where = array(
284
            'visible=' => '1'
285
        );
286
        if (isset($args['pid']) && is_numeric($args['pid'])) {
287
            $where['person_id='] = $args['pid'];
288
        }
289
        $input = array (
290
            'amount' => array (
291
                'user' => true
292
            ),
293
            'where'  => $where,
294
            'order' => 'posted DESC',
295
            'limit' => $limit
296
        );
297
298
        $commentsdata = $this->_get_comment_data($input);
299
300
        $data['comments'] = $commentsdata;
301
        $data['results_per_page'] = $num;
302
        $data['page'] = $page;
303
        $params = array();
304
        if (isset($args['pid']) && is_numeric($args['pid'])) {
305
            $data['pid'] = $args['pid'];
306
            $q = 'SELECT title, given_name, family_name, lordofname, house FROM member m, person_names p WHERE m.person_id=p.person_id AND p.type="name" AND left_house="9999-12-31" AND m.person_id = :pid';
307
            $q = $this->db->query($q, array(':pid' => $args['pid']));
308
            $data['full_name'] = member_full_name($q->field(0, 'house'), $q->field(0, 'title'), $q->field(0, 'given_name'), $q->field(0, 'family_name'), $q->field(0,'lordofname'));
309
            $q = 'SELECT COUNT(*) AS count FROM comments,hansard WHERE visible=1 AND comments.epobject_id = hansard.epobject_id and hansard.person_id = :pid';
310
            $params[':pid'] = $args['pid'];
311
        } else {
312
            $q = 'SELECT COUNT(*) AS count FROM comments WHERE visible=1';
313
        }
314
        $q = $this->db->query($q, $params);
315
        $data['total_results'] = $q->field(0, 'count');
316
        return $data;
317
    }
318
319
  public function _get_data_by_dates($args) {
320
    // $args should contain start_date and end_date
321
322
        twfy_debug (get_class($this), "getting data by recent");
323
        $data = array();
324
        $where = array(
325
            'visible=' => '1',
326
            'date(posted)>=' => $args['start_date'],
327
            'date(posted)<=' => $args['end_date']
328
        );
329
        $input = array (
330
            'amount' => array (
331
                'user' => true
332
            ),
333
            'where'  => $where,
334
            'order'  => 'posted DESC'
335
        );
336
        $commentsdata = $this->_get_comment_data($input);
337
        $data['comments'] = $commentsdata;
338
        return $data;
339
  }
340
341
    public function _get_data_by_search($args) {
342
        // $args should contain 'num', indicating how many to get.
343
344
        twfy_debug (get_class($this), "getting data by search");
345
346
        // What we return.
347
        $data = array();
348
349
        if (isset($args['num']) && is_numeric($args['num'])) {
350
            $num = $args['num'];
351
        } else {
352
            $num = 10;
353
        }
354
355
        if (isset($args['page']) && is_numeric($args['page'])) {
356
            $page = $args['page'];
357
        } else {
358
            $page = 1;
359
        }
360
361
        $limit = $num*($page-1) . ',' . $num;
362
363
        $input = array (
364
            'amount' => array (
365
                'user'=> true
366
            ),
367
            'where'  => array (
368
                'comments.body LIKE' => "%$args[s]%"
369
            ),
370
            'order' => 'posted DESC',
371
            'limit' => $limit
372
        );
373
374
        $commentsdata = $this->_get_comment_data($input);
375
376
        $data['comments'] = $commentsdata;
377
        $data['search'] = $args['s'];
378
        #		$data['results_per_page'] = $num;
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
379
        #		$data['page'] = $page;
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
380
        #		$q = $this->db->query('SELECT COUNT(*) AS count FROM comments WHERE visible=1');
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
381
        #		$data['total_results'] = $q->field(0, 'count');
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
382
        return $data;
383
    }
384
385
386
    public function _comment_url($urldata) {
387
        global $hansardmajors;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
388
389
        // Pass it the major and gid of the comment's epobject and the comment_id.
390
        // And optionally the user's id, for highlighting the comments on the destination page.
391
        // It returns the URL for the comment.
392
393
        $major 		= $urldata['major'];
394
        $gid 		= $urldata['gid'];
395
        $comment_id = $urldata['comment_id'];
396
        $user_id = isset($urldata['user_id']) ? $urldata['user_id'] : false;
397
398
        // If you change stuff here, you might have to change it in
399
        // $COMMENT->_set_url() too...
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
400
401
        // We'll generate permalinks for each comment.
402
        // Assuming every comment is from the same major...
403
        $page = $hansardmajors[$major]['page'];
404
405
        $URL = new \MySociety\TheyWorkForYou\Url($page);
406
407
        $gid = fix_gid_from_db($gid); // In includes/utility.php
408
        $URL->insert(array('id' => $gid ));
409
        if ($user_id) {
410
            $URL->insert(array('u' => $user_id));
411
        }
412
        $url = $URL->generate() . '#c' . $comment_id;
413
414
        return $url;
415
    }
416
417
418
419
/*	function _fix_gid($args) {
420
421
        // Replace a hansard object gid with an epobject_id.
422
        // $args may have a 'gid' element. If so, we replace it
423
        // with the hansard object's epobject_id as 'epobject_id', because
424
        // comments are tied to epobject_ids.
425
        // Returns the corrected $args array.
426
427
        global $this_page;
428
429
        if (isset($args['gid']) && !isset($args['epobject_id'])) {
430
431
            if ($this_page == 'wran' || $this_page == 'wrans') {
432
                $gidextra = 'wrans';
433
            } else {
434
                $gidextra = 'debate';
435
            }
436
437
            $q = $this->db->query ("SELECT epobject_id FROM hansard WHERE gid = 'uk.org.publicwhip/" . $gidextra . '/' . addslashes($args['gid']) . "'");
438
439
            if ($q->rows() > 0) {
440
                unset($args['gid']);
441
                $args['epobject_id'] = $q->field(0, 'epobject_id');
442
            }
443
        }
444
445
        return $args;
446
447
    }
448
    */
449
450
    public function _get_comment_data($input) {
451
        // Generic function for getting hansard data from the DB.
452
        // It returns an empty array if no data was found.
453
        // It returns an array of items if 1 or more were found.
454
        // Each item is an array of key/value pairs.
455
        // eg:
456
        /*
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
457
            array (
458
                0	=> array (
459
                    'comment_id'	=> '2',
460
                    'user_id'		=> '10',
461
                    'body'			=> 'The text of the comment is here.',
462
                    etc...
463
                ),
464
                1	=> array (
465
                    'comment_id'	=> '3',
466
                    etc...
467
                )
468
            );
469
        */
470
471
        // $input is an array of things needed for the SQL query:
472
        // 'amount' has one or more of :
473
        //		'user'=>true - Users' names.
474
        //  	'hansard'=>true - Body text from the hansard items.
475
        // 'where' is an associative array of stuff for the WHERE clause, eg:
476
        // 		array ('id=' => '37', 'posted>' => '2003-12-31 00:00:00');
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
477
        // 'order' is a string for the $order clause, eg 'hpos DESC'.
478
        // 'limit' as a string for the $limit clause, eg '21,20'.
479
480
        $amount = isset($input['amount']) ? $input['amount'] : array();
481
        $wherearr = $input['where'];
482
        $order = isset($input['order']) ? $input['order'] : '';
483
        $limit = isset($input['limit']) ? $input['limit'] : '';
484
485
        // The fields to fetch from db. 'table' => array ('field1', 'field2').
486
        $fieldsarr = array (
487
            'comments' => array ('comment_id', 'user_id', 'epobject_id', 'body', 'posted', 'modflagged', 'visible'),
488
            'hansard' => array ('major', 'gid')
489
        );
490
491
        // Yes, we need the gid of a comment's associated hansard object
492
        // to make the comment's URL. And we have to go via the epobject
493
        // table to do that.
494
        $join = 'INNER JOIN epobject ON comments.epobject_id = epobject.epobject_id
495
                    INNER JOIN hansard ON comments.epobject_id = hansard.epobject_id';
496
497
        // Add on the stuff for getting a user's details.
498
        if (isset($amount['user']) && $amount['user'] == true) {
499
            $fieldsarr['users'] = array ('firstname', 'lastname', 'user_id');
500
            // Like doing "FROM comments, users" but it's easier to add
501
            // an "INNER JOIN..." automatically to the query.
502
            $join .= ' INNER JOIN users ON comments.user_id = users.user_id ';
503
        }
504
505
        // Add on that we need to get the hansard item's body.
506
        if (isset($amount['hansard']) && $amount['hansard'] == true) {
507
            $fieldsarr['epobject'] = array('body');
508
        }
509
510
        $fieldsarr2 = array ();
511
        // Construct the $fields clause.
512
        foreach ($fieldsarr as $table => $tablesfields) {
513
            foreach ($tablesfields as $n => $field) {
514
                // HACK.
515
                // If we're getting the body of a hansard object, we need to
516
                // get it AS 'hbody', so we don't confuse with the comment's 'body'
517
                // element.
518
                if ($table == 'epobject' && $field == 'body') {
519
                    $field .= ' AS hbody';
520
                }
521
                $fieldsarr2[] = $table.'.'.$field;
522
            }
523
        }
524
        $fields = implode(', ', $fieldsarr2);
525
526
527
        $wherearr2 = array ();
528
        $params = array();
529
        $i = 0;
530
        // Construct the $where clause.
531
        foreach ($wherearr as $key => $val) {
532
            $wherearr2[] = "$key :where$i";
533
            $params[":where$i"] = $val;
534
            $i++;
535
        }
536
        $where = implode (" AND ", $wherearr2);
537
538
        if ($order != '') {
539
            $order = "ORDER BY $order";
540
        }
541
        if ($limit != '') {
542
            # Can't use parameter as >1 argument
543
            $limit = "LIMIT $limit";
544
        }
545
546
        // Finally, do the query!
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
547
        $q = $this->db->query ("SELECT $fields
548
                        FROM 	comments
549
                        $join
550
                        WHERE $where
551
                        $order
552
                        $limit
553
                        ", $params);
554
555
        // Format the data into an array for returning.
556
        $data = array ();
557
558
        if ($q->rows() > 0) {
559
560
            // If you change stuff here, you might have to change it in
561
            // $COMMENT->_set_url() too...
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
562
563
            // We'll generate permalinks for each comment.
564
            // Assuming every comment is from the same major...
565
566
            for ($n=0; $n<$q->rows(); $n++) {
567
568
                // Put each row returned into its own array in $data.
569
                foreach ($fieldsarr as $table => $tablesfields) {
570
                    foreach ($tablesfields as $m => $field) {
571
572
                        // HACK 2.
573
                        // If we're getting the body of a hansard object, we have
574
                        // got it AS 'hbody', so we didn't duplicate the comment's 'body'
575
                        // element.
576
                        if ($table == 'epobject' && $field == 'body') {
577
                            $field = 'hbody';
578
                        }
579
580
                        $data[$n][$field] = $q->field($n, $field);
581
                    }
582
                }
583
584
                $urldata = array(
585
                    'major' => $q->field($n, 'major'),
586
                    'gid' => $data[$n]['gid'],
587
                    'comment_id' => $data[$n]['comment_id'],
588
#					'user_id' =>
589
                );
590
                $data[$n]['url'] = $this->_comment_url($urldata);
591
            }
592
        }
593
594
        return $data;
595
596
    }
597
598
599
}
600