|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
*## TbJsonPager class file. |
|
4
|
|
|
* |
|
5
|
|
|
* @author: antonio ramirez <[email protected]> |
|
6
|
|
|
* @copyright Copyright © Clevertech 2012- |
|
7
|
|
|
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
Yii::import('booster.widgets.TbPager'); |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
*## Class TbJsonPager |
|
14
|
|
|
* |
|
15
|
|
|
* Use this specific pager for JSON grid, not the standard one! |
|
16
|
|
|
* |
|
17
|
|
|
* @package booster.widgets.supplementary |
|
18
|
|
|
*/ |
|
19
|
|
|
class TbJsonPager extends TbPager |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @var string json. |
|
23
|
|
|
*/ |
|
24
|
|
|
public $json; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
*### .run() |
|
28
|
|
|
* |
|
29
|
|
|
* Runs the widget. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function run() |
|
32
|
|
|
{ |
|
33
|
|
|
if (!$this->json) { |
|
34
|
|
|
parent::run(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
return $this->createPageButtons(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
*### .createPageButton() |
|
42
|
|
|
* |
|
43
|
|
|
* Creates a page button. |
|
44
|
|
|
* You may override this method to customize the page buttons. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $label the text label for the button |
|
47
|
|
|
* @param integer $page the page number |
|
48
|
|
|
* @param string $class the CSS class for the page button. This could be 'page', 'first', 'last', 'next' or 'previous'. |
|
49
|
|
|
* @param boolean $hidden whether this page button is visible |
|
50
|
|
|
* @param boolean $selected whether this page button is selected |
|
51
|
|
|
* |
|
52
|
|
|
* @return string the generated button |
|
53
|
|
|
*/ |
|
54
|
|
|
protected function createPageButton($label, $page, $class, $hidden, $selected) |
|
55
|
|
|
{ |
|
56
|
|
|
if ($this->json) { |
|
57
|
|
|
if ($hidden || $selected) { |
|
58
|
|
|
$class .= ' ' . ($hidden ? 'disabled' : 'active'); |
|
59
|
|
|
} |
|
60
|
|
|
return array('class' => $class, 'url' => $this->createPageUrl($page), 'text' => $label); |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
return parent::createPageButton($label, $page, $class, $hidden, $selected); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.