ActiveRecordController::getStart()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Alpha\Controller;
4
5
use Alpha\Controller\Front\FrontController;
6
use Alpha\Util\Logging\Logger;
7
use Alpha\Util\Config\ConfigProvider;
8
use Alpha\Util\Http\Request;
9
use Alpha\Util\Http\Response;
10
use Alpha\Util\Helper\Validator;
11
use Alpha\View\View;
12
use Alpha\View\ViewState;
13
use Alpha\Exception\IllegalArguementException;
14
use Alpha\Exception\ResourceNotFoundException;
15
use Alpha\Exception\ResourceNotAllowedException;
16
use Alpha\Exception\SecurityException;
17
use Alpha\Exception\AlphaException;
18
use Alpha\Exception\RecordNotFoundException;
19
use Alpha\Exception\ValidationException;
20
use Alpha\Model\ActiveRecord;
21
22
/**
23
 * The main active record CRUD controller for the framework.
24
 *
25
 * @since 2.0
26
 *
27
 * @author John Collins <[email protected]>
28
 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
29
 * @copyright Copyright (c) 2018, John Collins (founder of Alpha Framework).
30
 * All rights reserved.
31
 *
32
 * <pre>
33
 * Redistribution and use in source and binary forms, with or
34
 * without modification, are permitted provided that the
35
 * following conditions are met:
36
 *
37
 * * Redistributions of source code must retain the above
38
 *   copyright notice, this list of conditions and the
39
 *   following disclaimer.
40
 * * Redistributions in binary form must reproduce the above
41
 *   copyright notice, this list of conditions and the
42
 *   following disclaimer in the documentation and/or other
43
 *   materials provided with the distribution.
44
 * * Neither the name of the Alpha Framework nor the names
45
 *   of its contributors may be used to endorse or promote
46
 *   products derived from this software without specific
47
 *   prior written permission.
48
 *
49
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
50
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
51
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
52
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
54
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
56
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
57
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
59
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
60
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
61
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62
 * </pre>
63
 */
64
class ActiveRecordController extends Controller implements ControllerInterface
65
{
66
    /**
67
     * The start number for list pageination.
68
     *
69
     * @var int
70
     *
71
     * @since 2.0
72
     */
73
    protected $start = 0;
74
75
    /**
76
     * The amount of records to return during pageination.
77
     *
78
     * @var int
79
     *
80
     * @since 2.0
81
     */
82
    protected $limit;
83
84
    /**
85
     * The count of the records of this type in the database (used during pagination).
86
     *
87
     * @var int
88
     *
89
     * @since 2.0
90
     */
91
    protected $recordCount = 0;
92
93
    /**
94
     * The field name to sort the list by (optional, default is ID).
95
     *
96
     * @var string
97
     *
98
     * @since 2.0
99
     */
100
    protected $sort;
101
102
    /**
103
     * The order to sort the list by (optional, should be ASC or DESC, default is ASC).
104
     *
105
     * @var string
106
     *
107
     * @since 2.0
108
     */
109
    protected $order;
110
111
    /**
112
     * The name of the Record field to filter the list by (optional).
113
     *
114
     * @var string
115
     *
116
     * @since 2.0
117
     */
118
    protected $filterField;
119
120
    /**
121
     * The value of the filterField to filter by (optional).
122
     *
123
     * @var string
124
     *
125
     * @since 2.0
126
     */
127
    protected $filterValue;
128
129
    /**
130
     * Trace logger.
131
     *
132
     * @var \Alpha\Util\Logging\Logger
133
     *
134
     * @since 2.0
135
     */
136
    private static $logger = null;
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
137
138
    /**
139
     * Constructor to set up the object.
140
     *
141
     * @param string $visibility The name of the rights group that can access this controller.
142
     *
143
     * @since 1.0
144
     */
145
    public function __construct($visibility = 'Admin')
146
    {
147
        self::$logger = new Logger('ActiveRecordController');
148
        self::$logger->debug('>>__construct()');
149
150
        // ensure that the super class constructor is called, indicating the rights group
151
        parent::__construct($visibility);
152
153
        self::$logger->debug('<<__construct');
154
    }
155
156
    /**
157
     * Handle GET requests.
158
     *
159
     * @param \Alpha\Util\Http\Request $request
160
     *
161
     * @throws \Alpha\Exception\ResourceNotFoundException
162
     * @throws \Alpha\Exception\IllegalArguementException
163
     *
164
     * @return \Alpha\Util\Http\Response
165
     *
166
     * @since 2.0
167
     */
168
    public function doGET($request)
169
    {
170
        self::$logger->debug('>>doGET(request=['.var_export($request, true).'])');
171
172
        $params = $request->getParams();
173
        $accept = $request->getAccept();
174
175
        $body = '';
176
177
        try {
178
            // get a single record
179
            if (isset($params['ActiveRecordType']) && isset($params['ActiveRecordID'])) {
180
                $body .= $this->renderRecord($params, $accept);
181
            } elseif (isset($params['ActiveRecordType']) && isset($params['start'])) {
182
                // list all records of this type
183
                $body .= $this->renderRecords($params, $accept);
184
            } elseif (isset($params['ActiveRecordType'])) {
185
                // create a new record of this type
186
                $ActiveRecordType = urldecode($params['ActiveRecordType']);
187
188
                if (class_exists($ActiveRecordType)) {
189
                    $record = new $ActiveRecordType();
190
                } else {
191
                    throw new IllegalArguementException('No ActiveRecord available to create!');
192
                }
193
194
                // set up the title and meta details
195
                if (!isset($this->title)) {
196
                    $this->setTitle('Create a new '.$record->getFriendlyClassName());
197
                }
198
                if (!isset($this->description)) {
199
                    $this->setDescription('Create a new '.$record->getFriendlyClassName().'.');
200
                }
201
                if (!isset($this->keywords)) {
202
                    $this->setKeywords('create,new,'.$record->getFriendlyClassName());
203
                }
204
205
                $view = View::getInstance($record, false, $accept);
206
207
                $body .= View::displayPageHead($this);
208
                $fields = array('formAction' => $this->request->getURI());
209
                $body .= $view->createView($fields);
210
            } else {
211
                throw new IllegalArguementException('No ActiveRecord available to display!');
212
            }
213
        } catch (IllegalArguementException $e) {
214
            self::$logger->warn($e->getMessage());
215
            throw new ResourceNotFoundException('The record that you have requested cannot be found!');
216
        } catch (RecordNotFoundException $e) {
217
            self::$logger->warn($e->getMessage());
218
            throw new ResourceNotFoundException('The record that you have requested cannot be found!');
219
        }
220
221
        $body .= View::displayPageFoot($this);
222
223
        self::$logger->debug('<<doGET');
224
225
        return new Response(200, $body, array('Content-Type' => ($accept == 'application/json' ? 'application/json' : 'text/html')));
226
    }
227
228
    /**
229
     * Method to handle POST requests.
230
     *
231
     * @param \Alpha\Util\Http\Request $request
232
     *
233
     * @throws \Alpha\Exception\IllegalArguementException
234
     * @throws \Alpha\Exception\SecurityException
235
     *
236
     * @return \Alpha\Util\Http\Response
237
     *
238
     * @since 2.0
239
     */
240
    public function doPOST($request)
241
    {
242
        self::$logger->debug('>>doDPOST(request=['.var_export($request, true).'])');
243
244
        $config = ConfigProvider::getInstance();
245
246
        $params = $request->getParams();
247
        $accept = $request->getAccept();
248
249
        try {
250
            if (isset($params['ActiveRecordType'])) {
251
                $ActiveRecordType = urldecode($params['ActiveRecordType']);
252
            } else {
253
                throw new IllegalArguementException('No ActiveRecord available to create!');
254
            }
255
256
            if (class_exists($ActiveRecordType)) {
257
                $record = new $ActiveRecordType();
258
            } else {
259
                throw new IllegalArguementException('No ActiveRecord ['.$ActiveRecordType.'] available to create!');
260
            }
261
262
            // check the hidden security fields before accepting the form POST data
263
            if (!$this->checkSecurityFields()) {
264
                throw new SecurityException('This page cannot accept post data from remote servers!');
265
            }
266
267
            $record->populateFromArray($params);
268
269
            try {
270
                $record->save();
271
            } catch (ValidationException $e) {
272
                self::$logger->warn($e->getMessage());
273
                $this->setStatusMessage(View::displayErrorMessage($e->getMessage()));
274
            }
275
276
            self::$logger->action('Created new '.$ActiveRecordType.' instance with ID '.$record->getID());
277
278
            if (isset($params['statusMessage'])) {
279
                $this->setStatusMessage(View::displayUpdateMessage($params['statusMessage']));
280
            } else {
281
                $this->setStatusMessage(View::displayUpdateMessage('Created'));
282
            }
283
284
            ActiveRecord::disconnect();
285
286
            if ($accept == 'application/json') {
287
                $view = View::getInstance($record, false, $accept);
288
                $body = $view->detailedView();
289
                $response = new Response(201);
290
                $response->setHeader('Content-Type', 'application/json');
291
                $response->setHeader('Location', $config->get('app.url').'/record/'.$params['ActiveRecordType'].'/'.$record->getID());
292
                $response->setBody($body);
293
294
                self::$logger->debug('<<doPOST');
295
296
                return $response;
297
            } else {
298
                $response = new Response(301);
299
300
                if ($this->getNextJob() != '') {
301
                    $response->redirect($this->getNextJob());
302
                } else {
303
                    if ($this->request->isSecureURI()) {
304
                        $response->redirect(FrontController::generateSecureURL('act=Alpha\\Controller\\ActiveRecordController&ActiveRecordType='.$ActiveRecordType.'&ActiveRecordID='.$record->getID()));
305
                    } else {
306
                        $response->redirect($config->get('app.url').'/record/'.$params['ActiveRecordType'].'/'.$record->getID());
307
                    }
308
                }
309
310
                self::$logger->debug('<<doPOST');
311
312
                return $response;
313
            }
314
        } catch (SecurityException $e) {
315
            self::$logger->warn($e->getMessage());
316
            throw new ResourceNotAllowedException($e->getMessage());
317
        } catch (IllegalArguementException $e) {
318
            self::$logger->warn($e->getMessage());
319
            throw new ResourceNotFoundException('The record that you have requested cannot be found!');
320
        }
321
    }
322
323
    /**
324
     * Method to handle PUT requests.
325
     *
326
     * @param \Alpha\Util\Http\Request $request
327
     *
328
     * @throws \Alpha\Exception\IllegalArguementException
329
     * @throws \Alpha\Exception\SecurityException
330
     *
331
     * @return \Alpha\Util\Http\Response
332
     *
333
     * @since 2.0
334
     */
335
    public function doPUT($request)
336
    {
337
        self::$logger->debug('>>doPUT(request=['.var_export($request, true).'])');
338
339
        $config = ConfigProvider::getInstance();
340
341
        $params = $request->getParams();
342
        $accept = $request->getAccept();
343
344
        try {
345
            if (isset($params['ActiveRecordType'])) {
346
                $ActiveRecordType = urldecode($params['ActiveRecordType']);
347
            } else {
348
                throw new IllegalArguementException('No ActiveRecord available to edit!');
349
            }
350
351
            if (class_exists($ActiveRecordType)) {
352
                $record = new $ActiveRecordType();
353
            } else {
354
                throw new IllegalArguementException('No ActiveRecord ['.$ActiveRecordType.'] available to edit!');
355
            }
356
357
            // check the hidden security fields before accepting the form POST data
358
            if (!$this->checkSecurityFields()) {
359
                throw new SecurityException('This page cannot accept post data from remote servers!');
360
            }
361
362
            $record->load($params['ActiveRecordID']);
363
            $record->populateFromArray($params);
364
365
            try {
366
                $record->save();
367
                $this->record = $record;
368
            } catch (ValidationException $e) {
369
                self::$logger->warn($e->getMessage());
370
                $this->setStatusMessage(View::displayErrorMessage($e->getMessage()));
371
            }
372
373
            self::$logger->action('Saved '.$ActiveRecordType.' instance with ID '.$record->getID());
374
375
            if (isset($params['statusMessage'])) {
376
                $this->setStatusMessage(View::displayUpdateMessage($params['statusMessage']));
377
            } else {
378
                $this->setStatusMessage(View::displayUpdateMessage('Saved'));
379
            }
380
381
            ActiveRecord::disconnect();
382
383
            if ($accept == 'application/json') {
384
                $view = View::getInstance($record, false, $accept);
385
                $body = $view->detailedView();
386
                $response = new Response(200);
387
                $response->setHeader('Content-Type', 'application/json');
388
                $response->setHeader('Location', $config->get('app.url').'/record/'.$params['ActiveRecordType'].'/'.$record->getID());
389
                $response->setBody($body);
390
            } else {
391
                $response = new Response(301);
392
393
                if ($this->getNextJob() != '') {
394
                    $response->redirect($this->getNextJob());
395
                } else {
396
                    if ($this->request->isSecureURI()) {
397
                        $response->redirect(FrontController::generateSecureURL('act=Alpha\\Controller\\ActiveRecordController&ActiveRecordType='.urldecode($params['ActiveRecordType']).'&ActiveRecordID='.$record->getID().'&view=edit'));
398
                    } else {
399
                        $response->redirect($config->get('app.url').'/record/'.$params['ActiveRecordType'].'/'.$record->getID().'/edit');
400
                    }
401
                }
402
            }
403
404
            self::$logger->debug('<<doPUT');
405
406
            return $response;
407
        } catch (SecurityException $e) {
408
            self::$logger->warn($e->getMessage());
409
            throw new ResourceNotAllowedException($e->getMessage());
410
        } catch (IllegalArguementException $e) {
411
            self::$logger->warn($e->getMessage());
412
            throw new ResourceNotFoundException('The record that you have requested cannot be found!');
413
        } catch (RecordNotFoundException $e) {
414
            self::$logger->warn($e->getMessage());
415
            throw new ResourceNotFoundException('The record that you have requested cannot be found!');
416
        }
417
    }
418
419
    /**
420
     * Method to handle DELETE requests.
421
     *
422
     * @param \Alpha\Util\Http\Request $request
423
     *
424
     * @throws \Alpha\Exception\IllegalArguementException
425
     * @throws \Alpha\Exception\SecurityException
426
     * @throws \Alpha\Exception\ResourceNotAllowedException
427
     *
428
     * @return \Alpha\Util\Http\Response
429
     *
430
     * @since 2.0
431
     */
432
    public function doDELETE($request)
433
    {
434
        self::$logger->debug('>>doDELETE(request=['.var_export($request, true).'])');
435
436
        $config = ConfigProvider::getInstance();
437
438
        $params = $request->getParams();
439
        $accept = $request->getAccept();
440
441
        try {
442
            // check the hidden security fields before accepting the form data
443
            if (!$this->checkSecurityFields()) {
444
                throw new SecurityException('This page cannot accept data from remote servers!');
445
            }
446
447
            if (isset($params['ActiveRecordType'])) {
448
                $ActiveRecordType = urldecode($params['ActiveRecordType']);
449
            } else {
450
                throw new IllegalArguementException('No ActiveRecord available to edit!');
451
            }
452
453
            if (class_exists($ActiveRecordType)) {
454
                $record = new $ActiveRecordType();
455
            } else {
456
                throw new IllegalArguementException('No ActiveRecord ['.$ActiveRecordType.'] available to edit!');
457
            }
458
459
            // check the hidden security fields before accepting the form POST data
460
            if (!$this->checkSecurityFields()) {
461
                throw new SecurityException('This page cannot accept post data from remote servers!');
462
            }
463
464
            $record->load($params['ActiveRecordID']);
465
466
            ActiveRecord::begin();
467
            $record->delete();
468
            ActiveRecord::commit();
469
            ActiveRecord::disconnect();
470
471
            self::$logger->action('Deleted '.$ActiveRecordType.' instance with ID '.$params['ActiveRecordID']);
472
473
            if ($accept == 'application/json') {
474
                $response = new Response(200);
475
                $response->setHeader('Content-Type', 'application/json');
476
                $response->setBody(json_encode(array('message' => 'deleted')));
477
            } else {
478
                $response = new Response(301);
479
480
                if (isset($params['statusMessage'])) {
481
                    $this->setStatusMessage(View::displayUpdateMessage($params['statusMessage']));
482
                } else {
483
                    $this->setStatusMessage(View::displayUpdateMessage('Deleted'));
484
                }
485
486
                if ($this->getNextJob() != '') {
487
                    $response->redirect($this->getNextJob());
488
                } else {
489
                    if ($this->request->isSecureURI()) {
490
                        $response->redirect(FrontController::generateSecureURL('act=Alpha\\Controller\\ActiveRecordController&ActiveRecordType='.$ActiveRecordType.'&start=0&limit='.$config->get('app.list.page.amount')));
491
                    } else {
492
                        $response->redirect($config->get('app.url').'/records/'.$params['ActiveRecordType']);
493
                    }
494
                }
495
            }
496
497
            self::$logger->debug('<<doDELETE');
498
499
            return $response;
500
        } catch (SecurityException $e) {
501
            self::$logger->warn($e->getMessage());
502
            throw new ResourceNotAllowedException($e->getMessage());
503
        } catch (RecordNotFoundException $e) {
504
            self::$logger->warn($e->getMessage());
505
            throw new ResourceNotFoundException('The item that you have requested cannot be found!');
506
        } catch (AlphaException $e) {
507
            self::$logger->error($e->getMessage());
508
            ActiveRecord::rollback();
509
            throw new ResourceNotAllowedException($e->getMessage());
510
        }
511
    }
512
513
    /**
514
     * Sets up the pagination start point and limit.
515
     *
516
     * @since 2.0
517
     */
518
    public function after_displayPageHead_callback()
519
    {
520
        $body = parent::after_displayPageHead_callback();
521
522
        // set the start point for the list pagination
523
        if ($this->request->getParam('start') != null) {
524
            $this->start = $this->request->getParam('start');
0 ignored issues
show
Documentation Bug introduced by
The property $start was declared of type integer, but $this->request->getParam('start') is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
525
526
            $viewState = ViewState::getInstance();
527
            $viewState->set('selectedStart', $this->start);
528
529
            if ($this->request->getParam('limit') != null) {
530
                $this->limit = $this->request->getParam('limit');
0 ignored issues
show
Documentation Bug introduced by
The property $limit was declared of type integer, but $this->request->getParam('limit') is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
531
            } else {
532
                $config = ConfigProvider::getInstance();
533
                $this->limit = $config->get('app.list.page.amount');
534
            }
535
536
            $accept = $this->request->getAccept();
537
538
            if ($accept == 'application/json') {
539
                $body .= '[';
540
            }
541
        }
542
543
        return $body;
544
    }
545
546
    /**
547
     * Method to display the page footer with pageination links.
548
     *
549
     * @return string
550
     *
551
     * @since 2.0
552
     */
553
    public function before_displayPageFoot_callback()
554
    {
555
        $body = '';
556
557
        if ($this->request->getParam('start') != null) {
558
            $accept = $this->request->getAccept();
559
560
            if ($accept == 'application/json') {
561
                $body .= ']';
562
            } else {
563
                $body .= View::displayPageLinks($this);
564
                $body .= '<br>';
565
            }
566
        }
567
568
        return $body;
569
    }
570
571
    /**
572
     * Load the requested record and render the HTML or JSON for it.
573
     *
574
     * @param array $params The request params
575
     * @param string $accept The HTTP accept heard value
576
     *
577
     * @throws \Alpha\Exception\ResourceNotFoundException
578
     * @throws \Alpha\Exception\IllegalArguementException
579
     *
580
     * @return string The display data for the requested record
581
     *
582
     * @since 3.0
583
     */
584
    private function renderRecord($params, $accept)
585
    {
586
        if (!Validator::isInteger($params['ActiveRecordID'])) {
587
            throw new IllegalArguementException('Invalid oid ['.$params['ActiveRecordID'].'] provided on the request!');
588
        }
589
590
        $ActiveRecordType = urldecode($params['ActiveRecordType']);
591
592
        if (class_exists($ActiveRecordType)) {
593
            $record = new $ActiveRecordType();
594
        } else {
595
            throw new IllegalArguementException('No ActiveRecord available to view!');
596
        }
597
598
        // set up the title and meta details
599
        if (isset($params['view']) && $params['view'] == 'edit') {
600
            if (!isset($this->title)) {
601
                $this->setTitle('Editing a '.$record->getFriendlyClassName());
602
            }
603
            if (!isset($this->description)) {
604
                $this->setDescription('Page to edit a '.$record->getFriendlyClassName().'.');
605
            }
606
            if (!isset($this->keywords)) {
607
                $this->setKeywords('edit,'.$record->getFriendlyClassName());
608
            }
609
        } else {
610
            if (!isset($this->title)) {
611
                $this->setTitle('Viewing a '.$record->getFriendlyClassName());
612
            }
613
            if (!isset($this->description)) {
614
                $this->setDescription('Page to view a '.$record->getFriendlyClassName().'.');
615
            }
616
            if (!isset($this->keywords)) {
617
                $this->setKeywords('view,'.$record->getFriendlyClassName());
618
            }
619
        }
620
621
        $record->load($params['ActiveRecordID']);
622
        ActiveRecord::disconnect();
623
624
        $view = View::getInstance($record, false, $accept);
625
626
        $body = View::displayPageHead($this);
627
628
        $message = $this->getStatusMessage();
629
        if (!empty($message)) {
630
            $body .= $message;
631
        }
632
633
        $body .= View::renderDeleteForm($this->request->getURI());
634
635
        if (isset($params['view']) && $params['view'] == 'edit') {
636
            $fields = array('formAction' => $this->request->getURI());
637
            $body .= $view->editView($fields);
638
        } else {
639
            $body .= $view->detailedView();
640
        }
641
642
        return $body;
643
    }
644
645
    /**
646
     * Load all records of the type requested and render the HTML or JSON for them.
647
     *
648
     * @param array $params The request params
649
     * @param string $accept The HTTP accept heard value
650
     *
651
     * @throws \Alpha\Exception\ResourceNotFoundException
652
     * @throws \Alpha\Exception\IllegalArguementException
653
     *
654
     * @return string The display data for the requested records
655
     *
656
     * @since 3.0
657
     */
658
    private function renderRecords($params, $accept)
659
    {
660
        $ActiveRecordType = urldecode($params['ActiveRecordType']);
661
662
        if (class_exists($ActiveRecordType)) {
663
            $record = new $ActiveRecordType();
664
        } else {
665
            throw new IllegalArguementException('No ActiveRecord available to view!');
666
        }
667
668
        // set up the title and meta details
669
        if (!isset($this->title)) {
670
            $this->setTitle('Listing all '.$record->getFriendlyClassName());
671
        }
672
        if (!isset($this->description)) {
673
            $this->setDescription('Listing all '.$record->getFriendlyClassName());
674
        }
675
        if (!isset($this->keywords)) {
676
            $this->setKeywords('list,all,'.$record->getFriendlyClassName());
677
        }
678
679
        if (isset($this->filterField) && isset($this->filterValue)) {
680
            if (isset($this->sort) && isset($this->order)) {
681
                $records = $record->loadAllByAttribute($this->filterField, $this->filterValue, $params['start'], $params['limit'],
682
                    $this->sort, $this->order);
683
            } else {
684
                $records = $record->loadAllByAttribute($this->filterField, $this->filterValue, $params['start'], $params['limit']);
685
            }
686
687
            $this->recordCount = $record->getCount(array($this->filterField), array($this->filterValue));
688
        } else {
689
            if (isset($this->sort) && isset($this->order)) {
690
                $records = $record->loadAll($params['start'], $params['limit'], $this->sort, $this->order);
691
            } else {
692
                $records = $record->loadAll($params['start'], $params['limit']);
693
            }
694
695
            $this->recordCount = $record->getCount();
696
        }
697
698
        ActiveRecord::disconnect();
699
700
        $view = View::getInstance($record, false, $accept);
0 ignored issues
show
Unused Code introduced by
$view is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
701
702
        $body = View::displayPageHead($this);
703
704
        $message = $this->getStatusMessage();
705
        if (!empty($message)) {
706
            $body .= $message;
707
        }
708
709
        $body .= View::renderDeleteForm($this->request->getURI());
710
711
        foreach ($records as $record) {
712
            $view = View::getInstance($record, false, $accept);
713
            $fields = array('formAction' => $this->request->getURI());
714
            $body .= $view->listView($fields);
715
        }
716
717
        if ($accept == 'application/json') {
718
            $body = rtrim($body, ',');
719
        }
720
721
        return $body;
722
    }
723
724
    /**
725
     * Get the pagination start point
726
     *
727
     * @return int
728
     *
729
     * @since 3.0
730
     */
731
    public function getStart()
732
    {
733
        return $this->start;
734
    }
735
736
    /**
737
     * Get the pagination record count
738
     *
739
     * @return int
740
     *
741
     * @since 3.0
742
     */
743
    public function getRecordCount()
744
    {
745
        return $this->recordCount;
746
    }
747
748
    /**
749
     * Get the pagination limit
750
     *
751
     * @return int
752
     *
753
     * @since 3.0
754
     */
755
    public function getLimit()
756
    {
757
        return $this->limit;
758
    }
759
}
760