ResultTrait::init()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 1
eloc 6
c 2
b 2
f 0
nc 1
nop 0
dl 0
loc 13
rs 10
1
<?php
2
3
/**
4
 * Contains the functionality common to both ResultList and ResultGrid. They 
5
 * don't share a common ancestor so using a base class is not possible.
6
 * 
7
 * The beforeParentInit() method is used to inject custom initilization from 
8
 * derived classes.
9
 *
10
 * @author Sam Stenvall <[email protected]>
11
 * @copyright Copyright &copy; Sam Stenvall 2014-
12
 * @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0
13
 * 
14
 * Properties from attached classes (makes the IDE happy)
15
 * @property string $id
16
 * @property string $template
17
 * @property array|string $pager
18
 * @property boolean $enableHistory
19
 * @property string $emptyText
20
 * @property IDataProvider $dataProvider
21
 */
22
trait ResultTrait
23
{
24
	
25
	/**
26
	 * @var string the display mode context to use in the toggle switch. 
27
	 * Defaults to DisplayMode::CONTEXT_RESULTS 
28
	 */
29
	public $displayModeContext = DisplayMode::CONTEXT_RESULTS;
30
	
31
	/**
32
	 * Called right before parent::init() is called in the trait
33
	 */
34
	abstract public function beforeParentInit();
35
36
	/**
37
	 * Initializes the component
38
	 */
39
	public function init()
40
	{
41
		$this->id = 'result-list';
42
		
43
		// Configure the pager and template
44
		$this->template = '{summary} {items} {pager}';
45
		$this->pager = ResultHelper::getDefaultPagerConfiguration();
46
47
		// needed to update the URL when page is changed
48
		$this->enableHistory = true;
49
		
50
		$this->beforeParentInit();
51
		parent::init();
52
	}
53
	
54
	/**
55
	 * Renders the data item list.
56
	 * 
57
	 * When the data provider is empty, Yii generates a <span> directly inside 
58
	 * a <ul> which is invalid an needs to be fixed.
59
	 */
60
	public function renderItems()
61
	{
62
		// Get rid of that pesky dot at the end
63
		if ($this->emptyText === null)
64
			$this->emptyText = substr(Yii::t('GenericList', 'No results found.'), 0, strlen(Yii::t('GenericList', 'No results found.')) - 1);
65
66
		if ($this->dataProvider->totalItemCount > 0)
0 ignored issues
show
Bug introduced by
Accessing totalItemCount on the interface IDataProvider suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
67
			parent::renderItems();
68
		else
69
			echo CHtml::tag('div', array('class'=>'alert alert-block alert-error'), $this->emptyText);
70
	}
71
	
72
	/**
73
	 * Renders the summary and the display mode toggle.
74
	 */
75
	public function renderSummary()
76
	{
77
		// Render the actual summary into a variable
78
		ob_start();
79
		parent::renderSummary();
80
		$summaryContent = ob_get_clean();
81
		
82
		ResultHelper::renderDisplayModeToggle($summaryContent, $this->displayModeContext);
83
	}
84
85
}
86