1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35:
36:
37: class X2ActiveGridView extends X2GridViewBase {
38:
39: public $modelName;
40: public $fieldFormatter = 'X2ActiveRecordFieldFormatter';
41: public $specialColumns = array ();
42: public $columnOverrides = array ();
43: public $includedFields = array ();
44: public $excludedFields = array ();
45: public $isAdmin = false;
46: protected $specialColumnNames = array();
47:
48: protected $_model;
49: public function getModel ($row=null, $data=null) {
50: if (!isset ($this->_model)) {
51: $modelName = $this->modelName;
52: $this->_model = $modelName::model ();
53: }
54: return $this->_model;
55: }
56:
57: public function setFormatter ($data) {
58: if (isset ($this->fieldFormatter) &&
59: method_exists ($data, 'setFormatter')) {
60:
61: $data->formatter = $this->fieldFormatter;
62: }
63: return $data;
64: }
65:
66: public function setSummaryText () {
67: if ($this instanceof X2GridViewForSortableWidgets ||
68: $this instanceof X2ActiveGridViewForSortableWidgets) {
69: $this->setSummaryTextForSortableWidgets ();
70: return;
71: }
72: $this->asa ('X2BaseListViewBehavior')->setSummaryText ();
73: }
74:
75: protected function addSpecialFieldNames () {
76:
77: foreach($this->specialColumns as $columnName => &$columnData) {
78: if(isset($columnData['header'])) {
79: $this->specialColumnNames[$columnName] = $columnData['header'];
80: } else {
81: $this->specialColumnNames[$columnName] =
82: $this->getModel ()->getAttributeLabel ($columnName);
83: }
84: }
85:
86: if(!empty($this->specialColumnNames))
87: $this->allFieldNames = array_merge ($this->allFieldNames, $this->specialColumnNames);
88:
89: }
90:
91:
92: protected function addFieldNames () {
93: $this->addSpecialFieldNames ();
94:
95: foreach($this->includedFields ?
96: $this->includedFields :
97: array_diff ($this->getModel ()->attributeNames (), $this->excludedFields) as
98: $fieldName) {
99:
100: $this->allFieldNames[$fieldName] = $this->getModel ()->getAttributeLabel ($fieldName);
101: }
102: }
103:
104: protected function createSpecialColumn ($columnName, $width) {
105: $newColumn = $this->specialColumns[$columnName];
106: $newColumn['id'] = $this->namespacePrefix.'C_'.$columnName;
107: $newColumn['headerHtmlOptions'] = array('style'=>'width:'.$this->formatWidth ($width).';');
108: if (!isset ($newColumn['name']) && !isset ($newColumn['value'])) {
109: $newColumn['name'] = $columnName;
110: }
111: return $newColumn;
112: }
113:
114:
115: protected function generateColumns () {
116: $columns = array ();
117:
118: foreach($this->gvSettings as $columnName => $width) {
119: if($columnName == 'gvControls' && !$this->enableControls){
120: continue;
121: }
122:
123: $col = $this->addNewColumn ($columnName, $this->formatWidth ($width));
124: if (sizeof ($col))
125: $columns[] = $col;
126: }
127:
128: $this->columns = $columns;
129: }
130:
131: 132: 133: 134: 135:
136: protected function addNewColumn ($columnName, $width) {
137: $newColumn = array ();
138: if(array_key_exists($columnName,$this->specialColumnNames)) {
139: $newColumn = $this->createSpecialColumn ($columnName, $width);
140: } else if($columnName == 'gvControls') {
141: $newColumn = $this->getGvControlsColumn ($width);
142: if(!$this->isAdmin)
143: $newColumn['template'] = '{view}{update}';
144: } else if ($columnName == 'gvCheckbox') {
145: $newColumn = $this->getGvCheckboxColumn ($width);
146: } else {
147: $newColumn = $this->createDefaultStyleColumn ($columnName, $width);
148: }
149: if ($newColumn === array ()) return $newColumn;
150: $newColumn['htmlOptions'] = X2Html::mergeHtmlOptions (
151: isset ($newColumn['htmlOptions']) ?
152: $newColumn['htmlOptions'] : array (), array ('width' => $width));
153:
154: if (isset ($this->columnOverrides[$columnName])) {
155: $newColumn = array_merge ($newColumn, $this->columnOverrides[$columnName]);
156: }
157:
158: return $newColumn;
159: }
160:
161: protected function createDefaultStyleColumn ($columnName, $width) {
162: $isCurrency = in_array($columnName,array('annualRevenue','quoteAmount'));
163: $newColumn = array();
164:
165: if (in_array ($columnName, array_keys ($this->allFieldNames))) {
166:
167: $newColumn['name'] = $columnName;
168: $newColumn['id'] = $this->namespacePrefix.'C_'.$columnName;
169: $newColumn['header'] = $this->getModel ()->getAttributeLabel($columnName);
170: $newColumn['headerHtmlOptions'] = array(
171: 'style'=>'width:'.$this->formatWidth ($width).';');
172:
173: $newColumn['value'] =
174: '$this->grid->setFormatter ($data)
175: ->renderAttribute ("'.$columnName.'", false);';
176: }
177: return $newColumn;
178: }
179:
180: }
181:
182: ?>
183: