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: class WsModelGridView
25: {
26: 27: 28: 29:
30: public $itemsPerPage = 10;
31: 32: 33: 34:
35: public $noDataText;
36: 37: 38: 39:
40: public $showEdit = true;
41: 42: 43: 44:
45: private $_id = '';
46: 47: 48: 49:
50: private $_modelName = '';
51: 52: 53: 54:
55: private $_tableName;
56: 57: 58: 59:
60: private $_order = '';
61: 62: 63: 64:
65: private $_action = '';
66: 67: 68: 69:
70: private $_edit_action = '';
71: 72: 73: 74:
75: private $_delete_action = '';
76: 77: 78: 79: 80: 81:
82: private $_formId = '';
83: private $_model;
84:
85:
86: public function __construct($model, $order='',
87: $edit_action='', $delete_action='')
88: {
89: $this->_id = uniqid('WsGridView_');
90:
91: if (! $model instanceof WsModel) {
92: return false;
93: }
94:
95: $this->noDataText = WsLocalize::msg('no data found');
96:
97: $this->_modelName = get_class($model);
98: $this->_model = $model;
99: $this->_order = $order;
100:
101: $this->_edit_action = WsSERVER_ROOT
102: .'/protected/library/ajax/WsEditModel.php';
103: if (trim($edit_action) != '') {
104: $this->_edit_action = $edit_action;
105: }
106:
107: $this->_delete_action = WsSERVER_ROOT
108: .'/protected/library/ajax/WsDeleteFromModel.php';
109: if (trim($delete_action) != '') {
110: $this->_delete_action = $delete_action;
111: }
112:
113: $this->_action = WsSERVER_ROOT
114: .'/protected/library/ajax/WsModelGrid.php';
115: $this->_formId = $this->_id.'_edit_form';
116: }
117:
118:
119: public function __toString()
120: {
121: return $this->constructGrid();
122: }
123:
124:
125: 126: 127: 128: 129: 130: 131:
132: protected function getPagination($count)
133: {
134: $paginationCount= floor($count / $this->itemsPerPage);
135: $paginationModCount= $count % $this->itemsPerPage;
136: if(!empty($paginationModCount)){
137: $paginationCount++;
138: }
139: return $paginationCount;
140: }
141:
142:
143: 144: 145: 146: 147: 148:
149: protected function constructGrid()
150: {
151:
152: $table = "
153: <script type=\"text/javascript\">
154: // load first page
155: WschangeModelPagination(
156: '$this->_id',
157: '$this->_action',
158: '$this->_modelName',
159: '$this->noDataText',
160: $this->itemsPerPage,
161: $this->showEdit,
162: '$this->_order',
163: '$this->_formId',
164: 0,
165: '$this->_id'+'_0',
166: '$this->_edit_action',
167: '$this->_delete_action'
168: );
169: </script>
170: ";
171:
172:
173: $table .= '<div class="row">';
174:
175:
176:
177:
178:
179:
180:
181: if ($this->showEdit) {
182: $table .= '<div id="'.$this->_formId.'"></div>';
183: }
184:
185:
186: $table .= '<div class="row grid-header">';
187:
188: $table .= '<div class="row">';
189: $table .= '<div class="column column-6 text-left text-error">';
190: $table .= '<div class="grid-title">';
191: $table .= $this->_model->metaName;
192: $table .= '</div>';
193: $table .= '</div>';
194: $table .= '</div>';
195:
196: $table .= '</div>';
197:
198:
199: $table .= '<div class="row">';
200: $table .= '<div class="column column-6 text-left">';
201: $table .= '<form class="search-form">';
202:
203: if ($this->showEdit) {
204: $table .= '<input class="add-button"'
205: .' id="btn_create_'.$this->_id.'"'
206: .' value="+"'
207: .' type="button" onclick="WseditModelID('
208: .'\''.$this->_formId.'\', '
209: .'\''.$this->_modelName.'\', '
210: .'0, \''.$this->_edit_action.'\', \''
211: .$this->_model->metaName.'\')"/>';
212: }
213:
214: $table .= '<input class="search-input"';
215: $table .= ' type="text" id="search_'.$this->_id.'"';
216: $table .= '/>';
217: $table .= '<input class="search-button"'
218: .' value="⌕"'
219: .' id="btn_search_'.$this->_id
220: .'" type="button" onclick="WschangeModelPagination('
221: .'\''.$this->_id.'\', '
222: .'\''.$this->_action.'\', '
223: .'\''.$this->_modelName.'\', '
224: .'\''.$this->noDataText.'\', '
225: .$this->itemsPerPage.', '
226: .$this->showEdit.', '
227: .'\''.$this->_order.'\', '
228: .'\''.$this->_formId.'\', '
229: .'0, \''.$this->_id.'\'+\'_0\', '
230: .'\''.$this->_edit_action.'\', '
231: .'\''.$this->_delete_action.'\')"/>';
232: $table .= '</form>';
233:
234: $table .= '</div>';
235: $table .= '</div>';
236:
237:
238: $table .= '<div class="row">';
239: $table .= '<div class="column column-12" ';
240: $table .= ' style="overflow: auto;">';
241: $table .= '<table class="grid">';
242: $table .= '<thead>';
243: $table .= '<tr class="ws_tr">';
244: foreach ($this->_model->columns as $column) {
245: if (!in_array($column, $this->_model->hiddenColumns)) {
246: if (isset($this->_model->columnHeaders[$column])) {
247: $table .= '<th class="ws_th">'
248: .$this->_model->columnHeaders[$column];
249: $table .= '</th>';
250: } else {
251: $table .= '<th class="ws_th">'.$column.'</th>';
252: }
253: }
254: }
255: if ($this->showEdit) {
256: $table .= '<th class="ws_th"></th>';
257: }
258: $table .= '</tr>';
259: $table .= '</thead>';
260:
261:
262: $table .= '<tbody id="'.$this->_id.'"></tbody>';
263:
264:
265: $table .= '</table>';
266: $table .= '</div>';
267: $table .= '</div>';
268:
269:
270: $db = new WsDatabase();
271: $countQuery = 'SELECT COUNT(*) AS nrows FROM '.$this->_model->tableName;
272: $result = $db->query($countQuery);
273: $this->nRows = intval($result[0]['nrows']);
274: $db->close();
275:
276:
277: $nPages = $this->getPagination($this->nRows);
278:
279:
280: $table .= '<div class="row">';
281: $table .= '<ul class="pagination">';
282:
283: for ($i = 0; $i < $nPages; $i++) {
284: $table .= '<li>';
285: $table .= '
286: <a id="'.$this->_id.'_'.$i.'"
287: href="javascript:void(0)"
288: onclick="WschangeModelPagination('
289: .'\''.$this->_id.'\', '
290: .'\''.$this->_action.'\', '
291: .'\''.$this->_modelName.'\', '
292: .'\''.$this->noDataText.'\', '
293: .$this->itemsPerPage.', '
294: .$this->showEdit.', '
295: .'\''.$this->_order.'\', '
296: .'\''.$this->_formId.'\', '
297: .$i.',\''.$this->_id.'_'.$i.'\', '
298: .'\''.$this->_edit_action.'\', '
299: .'\''.$this->_delete_action.'\')"/>'
300: .($i+1).'</a>';
301: $table .= '</li>';
302: }
303:
304: $table .= '</ul>';
305: $table .= '</div>';
306:
307:
308: $table .= '</div><br/>';
309:
310: $table .= '<script type="text/javascript">'
311: .'$("#search_'.$this->_id.'").keydown(function(event) {'
312: .' if(event.keyCode == 13) {'
313: .' event.preventDefault();'
314: .' $("#btn_search_'.$this->_id.'").click();'
315: .' }'
316: .'});'
317: .'</script>';
318:
319: return $table;
320: }
321:
322:
323: 324: 325: 326:
327: public function show()
328: {
329: echo $this->constructGrid();
330: }
331: }
332: