Overview

Classes

  • Ws_logged_inModel
  • Ws_permissionsModel
  • Ws_role_permModel
  • Ws_rolesModel
  • Ws_user_roleModel
  • Ws_userModel
  • WsAuth
  • WsauthController
  • WsChart
  • WsConfig
  • WsController
  • WsDatabase
  • WsForm
  • WsImage
  • WsLocalize
  • WsModel
  • WsModelForm
  • WsModelGridView
  • WsUrl

Functions

  • __autoload
  • callHook
  • WsErrorHandler
  • Overview
  • Class
  1: <?php
  2: /**
  3:  * WsModelGridView
  4:  * Displays a list of data items in terms of a table. Each row of the table
  5:  * represents the data of a single data item, and a column usually represents
  6:  * an attribute of the item. WsModelGridView supports both CRUD and pagination
  7:  * of the data items.
  8:  *
  9:  * @param WsModel $model Instance of WsModel class
 10:  * @param string $order Sorting order for a grid
 11:  *
 12:  * Example usage:
 13:  *
 14:  * <code>
 15:  * // class MyModel inherits from WsModel class
 16:  * $model = new MyModel();
 17:  * $grid = new WsModelGridView($model);
 18:  *
 19:  * // show grid
 20:  * $grid->show();
 21:  * </code>
 22:  *
 23:  */
 24: class WsModelGridView
 25: {
 26:     /**
 27:      * @var integer $itemsPerPage Number of items per page
 28:      *
 29:      */
 30:     public $itemsPerPage = 10;
 31:     /**
 32:      * @var string $noDataText Text to display if model is empty
 33:      *
 34:      */
 35:     public $noDataText;
 36:     /**
 37:      * @var boolean $showEdit Shows or hide controls for CRUD operations
 38:      *
 39:      */
 40:     public $showEdit = true;
 41:     /**
 42:      * @var string $_id ID of grid view
 43:      *
 44:      */
 45:     private $_id = '';
 46:     /**
 47:      * @var string $_modelName Name of model
 48:      *
 49:      */
 50:     private $_modelName = '';
 51:     /**
 52:      * @var string $_tableName Table name
 53:      *
 54:      */
 55:     private $_tableName;
 56:     /**
 57:      * @var string $_order Sorting order for items
 58:      *
 59:      */
 60:     private $_order = '';
 61:     /**
 62:      * @var string $_action AJAX action for grid population
 63:      *
 64:      */
 65:     private $_action = '';
 66:     /**
 67:      * @var string $_edit_action AJAX url for editing gridview item
 68:      *
 69:      */
 70:     private $_edit_action = '';
 71:     /**
 72:      * @var string $_delete_action AJAX url for removing gridview item
 73:      *
 74:      */
 75:     private $_delete_action = '';
 76:     /**
 77:      * ID of element that will show edit dialog
 78:      *
 79:      * @var string $_formId ID of CRUD form
 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:      * Pagination logic.
127:      *
128:      * @param integer $count Number of items per page.
129:      * @return integer $paginationCount
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:      * construct model grid view
145:      *
146:      * @return string $table
147:      *
148:      */
149:     protected function constructGrid()
150:     {
151:         // jquery script for loading first page of grid
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:         // master div element
173:         $table .= '<div class="row">';
174: 
175:         // header
176:         //$table .= '<div class="table-dialog-header">'
177:         //    .'<strong>'.$this->_model->metaName.'</strong>'
178:         //    .'</div>';
179: 
180:         // container for edit dialog
181:         if ($this->showEdit) {
182:             $table .= '<div id="'.$this->_formId.'"></div>';
183:         }
184: 
185:         // grid
186:         $table .= '<div class="row grid-header">';
187:         // title
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:         // end of grid header
196:         $table .= '</div>';
197: 
198:         // control row
199:         $table .= '<div class="row">';
200:         $table .= '<div class="column column-6 text-left">';
201:         $table .= '<form class="search-form">';
202:         // new item button
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:         // search control
214:         $table .= '<input class="search-input"';
215:         $table .= ' type="text" id="search_'.$this->_id.'"';
216:         $table .= '/>';
217:         $table .= '<input class="search-button"'
218:             .' value="&#8981"'
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:         // end of control row
234:         $table .= '</div>';
235:         $table .= '</div>';
236: 
237:         // Grid View table
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:         // container of table data loaded from AJAX request
262:         $table .= '<tbody id="'.$this->_id.'"></tbody>';
263: 
264:         // end of grid table
265:         $table .= '</table>';
266:         $table .= '</div>';
267:         $table .= '</div>';
268: 
269:         // get number ow rows from query so that we can make pager
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:         // number of items in pager
277:         $nPages = $this->getPagination($this->nRows);
278: 
279:         // construct pager
280:         $table .= '<div class="row">';
281:         $table .= '<ul class="pagination">';
282:         // links to pages
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:         // end of pager
304:         $table .= '</ul>';
305:         $table .= '</div>';
306: 
307:         // end of master div element
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:      * Displays grid view on screen
325:      *
326:      */
327:     public function show()
328:     {
329:         echo $this->constructGrid();
330:     }
331: }
332: 
API documentation generated by ApiGen