PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/components/WGrid.php

http://web3cms.googlecode.com/
PHP | 332 lines | 160 code | 33 blank | 139 comment | 38 complexity | 7d0e9b5df04d2b179e4e21c0e918e247 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * WGrid class file.
  4. * WGrid is a widget displaying jqGrid or a static grid.
  5. */
  6. class WGrid extends CWidget
  7. {
  8. /**
  9. * @var array of the jqGrid columns
  10. */
  11. public $columns;
  12. /**
  13. * @var array of the jqGrid colModel, defines the look
  14. * of each column and the whole table, sets width and all other properties.
  15. * The total width should be area-2px, e.g. 698.
  16. */
  17. public $columnsModel;
  18. /**
  19. * @var string id of the controller, is necessary for the delete and other buttons.
  20. */
  21. public $controllerId;
  22. /**
  23. * @var string data type used by the data grid page to return to the jqGrid,
  24. * usually 'json' or 'xml'.
  25. */
  26. public $datatype;
  27. /**
  28. * @var boolean whether display the button close in the static grid titlebar
  29. */
  30. public $displayButtonClose;
  31. /**
  32. * @var boolean whether display the jqGrid,
  33. * default is true.
  34. */
  35. public $displayGrid;
  36. /**
  37. * @var boolean whether display the static grid,
  38. * default is true.
  39. */
  40. public $displaySGrid;
  41. /**
  42. * @var boolean whether display the static grid pager,
  43. * should be displayed in the most cases, default is true.
  44. */
  45. public $displaySGridPager;
  46. /**
  47. * @var boolean whether display the static grid titlebar,
  48. * default is true.
  49. */
  50. public $displaySTitlebar;
  51. /**
  52. * @var boolean whether display the jqGrid titlebar,
  53. * default is true.
  54. */
  55. public $displayTitlebar;
  56. /**
  57. * @var string id of the jqGrid,
  58. * default is 'w3Grid'.
  59. */
  60. public $gridId;
  61. /**
  62. * @var string id of the jqGrid pager,
  63. * default is 'w3GridPager'.
  64. */
  65. public $gridPagerId;
  66. /**
  67. * @var boolean whether grid has a link icon (show, edit, and so on),
  68. * so we need to register the hover effects script for it,
  69. * default is true.
  70. */
  71. public $hasLinkIcon;
  72. /**
  73. * @var integer total height of the jqGrid rows area, in pixels,
  74. * default is 500.
  75. */
  76. public $height;
  77. /**
  78. * @var array of important rows that should be displayed in the bottom
  79. * of the static grid. this variable is the array of rows (<tr>), each row
  80. * is an array of columns (<td>). Content of these rows will be displayed
  81. * bold, with a background like the pager bar.
  82. */
  83. public $importantRowsBottom;
  84. /**
  85. * @var integer maximum row's number of the static grid
  86. */
  87. public $maxRow;
  88. /**
  89. * @var integer minimum row's number of the static grid
  90. */
  91. public $minRow;
  92. /**
  93. * @var string method type used to request jqGrid data from the grid data page,
  94. * should be 'GET' or 'POST', default is 'POST'.
  95. */
  96. public $mtype;
  97. /**
  98. * @var object instance of {@link CPagination}, optional,
  99. * may be used to calculate maxRow, minRow, totalRecords.
  100. */
  101. public $pages;
  102. /**
  103. * @var boolean whether register script for the grid link icon,
  104. * such as delete button, default is true.
  105. */
  106. public $registerGridLinkIcon;
  107. /**
  108. * @var integer number of rows to display in the jqGrid,
  109. * default is 50.
  110. */
  111. public $rowNum;
  112. /**
  113. * @var string list of numbers of rows per page allowed by jqGrid to display,
  114. * default is '[25,50,100,200]'.
  115. */
  116. public $rowList;
  117. /**
  118. * @var array of the static grid rows data - array or rows (<tr>),
  119. * each row is an array of columns (<td>).
  120. */
  121. public $rows;
  122. /**
  123. * @var array of the static grid columns
  124. */
  125. public $sColumns;
  126. /**
  127. * @var string id of the static grid,
  128. * default is 'w3StaticGrid'.
  129. */
  130. public $sGridId;
  131. /**
  132. * @var string id of the static grid wrapper,
  133. * default is 'w3StaticGridWrapper'.
  134. */
  135. public $sGridWrapperId;
  136. /**
  137. * @var string name of the jqGrid sort field
  138. */
  139. public $sortname;
  140. /**
  141. * @var string sort order for the jqGrid sort field,
  142. * should be 'asc' or 'desc', default is 'asc'.
  143. */
  144. public $sortorder;
  145. /**
  146. * @var string title of the static grid
  147. */
  148. public $sTitle;
  149. /**
  150. * @var string title of the jqGrid
  151. */
  152. public $title;
  153. /**
  154. * @var integer total number of existing records,
  155. * used for the static grid pager bar.
  156. */
  157. public $totalRecords;
  158. /**
  159. * @var string url of the grid data page,
  160. * default is controllerId/gridData.
  161. */
  162. public $url;
  163. /**
  164. * @var boolean whether display the number of total records
  165. * in the pager bar of the jqGrid, default is true.
  166. */
  167. public $viewrecords;
  168. /**
  169. * When widget is called, following function is run.
  170. */
  171. public function run()
  172. {
  173. // set the default values and validate the data
  174. if($this->displayGrid===null)
  175. $this->displayGrid=$this->columns!==null && $this->columnsModel!==null;
  176. if($this->displaySGrid===null)
  177. $this->displaySGrid=$this->sColumns!==null && $this->rows!==null;
  178. if($this->title===null)
  179. $this->title=CHtml::encode(MParams::getPageLabel());
  180. else
  181. $this->title=(string)$this->title;
  182. // jqGrid
  183. if($this->displayGrid)
  184. {
  185. if(!is_array($this->columns))
  186. $this->columns=array();
  187. if(!is_array($this->columnsModel))
  188. $this->columnsModel=array();
  189. if($this->datatype===null)
  190. $this->datatype='json';
  191. if($this->displayTitlebar===null)
  192. $this->displayTitlebar=true;
  193. if($this->gridId===null)
  194. $this->gridId='w3Grid';
  195. if($this->gridPagerId===null)
  196. $this->gridPagerId=$this->gridId.'Pager';
  197. if($this->height===null)
  198. $this->height=500;
  199. else
  200. $this->height=(int)$this->height;
  201. if($this->mtype===null)
  202. $this->mtype='POST';
  203. if($this->rowList===null)
  204. $this->rowList='[25,50,100,200]';
  205. if($this->rowNum===null)
  206. $this->rowNum=_CController::GRID_PAGE_SIZE;
  207. $this->rowNum=(int)$this->rowNum;
  208. $this->sortname=(string)$this->sortname;
  209. if($this->sortorder===null)
  210. $this->sortorder='asc';
  211. if($this->url===null)
  212. $this->url=Yii::app()->urlManager->createUrl(Yii::app()->controller->id.'/gridData');
  213. $this->url=(string)$this->url;
  214. if($this->viewrecords===null)
  215. $this->viewrecords=true;
  216. }
  217. // static grid
  218. if($this->displaySGrid)
  219. {
  220. if(!is_array($this->rows))
  221. $this->rows=array();
  222. if($this->controllerId===null)
  223. $this->controllerId=Yii::app()->controller->id;
  224. if($this->displayButtonClose===null)
  225. $this->displayButtonClose=false;
  226. if($this->displaySGridPager===null)
  227. $this->displaySGridPager=true;
  228. if($this->displaySTitlebar===null)
  229. $this->displaySTitlebar=true;
  230. if($this->hasLinkIcon===null)
  231. $this->hasLinkIcon=true;
  232. if(!is_array($this->importantRowsBottom))
  233. $this->importantRowsBottom=array();
  234. if($this->maxRow===null && $this->pages instanceof CPagination)
  235. {
  236. $maxRow=$this->pages->getCurrentPage()*$this->pages->getPageSize()+count($this->rows); // ($this->pages->getCurrentPage()+1)*$this->pages->getPageSize()
  237. $this->maxRow=$maxRow > $this->pages->getItemCount() ? $this->pages->getItemCount() : $maxRow;
  238. }
  239. else
  240. $this->maxRow=(int)$this->maxRow;
  241. if($this->minRow===null && $this->pages instanceof CPagination)
  242. $this->minRow=$this->pages->getCurrentPage()*$this->pages->getPageSize()+1;
  243. else
  244. $this->minRow=(int)$this->minRow;
  245. if($this->registerGridLinkIcon===null)
  246. $this->registerGridLinkIcon=true;
  247. if(!is_array($this->sColumns))
  248. $this->sColumns=array();
  249. if($this->sGridId===null)
  250. $this->sGridId='w3StaticGrid';
  251. if($this->sGridWrapperId===null)
  252. $this->sGridWrapperId=$this->sGridId.'Wrapper';
  253. if($this->sTitle===null)
  254. $this->sTitle=$this->title;
  255. if($this->totalRecords===null && $this->pages instanceof CPagination)
  256. $this->totalRecords=$this->pages->getItemCount();
  257. else
  258. $this->totalRecords=(int)$this->totalRecords;
  259. }
  260. // data for the renderer
  261. $data=array(
  262. 'columns'=>$this->columns,
  263. 'columnsModel'=>$this->columnsModel,
  264. 'controllerId'=>$this->controllerId,
  265. 'datatype'=>$this->datatype,
  266. 'displayButtonClose'=>$this->displayButtonClose,
  267. 'displayGrid'=>$this->displayGrid,
  268. 'displaySGrid'=>$this->displaySGrid,
  269. 'displaySGridPager'=>$this->displaySGridPager,
  270. 'displayTitlebar'=>$this->displayTitlebar,
  271. 'displaySTitlebar'=>$this->displaySTitlebar,
  272. 'gridId'=>$this->gridId,
  273. 'gridPagerId'=>$this->gridPagerId,
  274. 'hasLinkIcon'=>$this->hasLinkIcon,
  275. 'height'=>$this->height,
  276. 'importantRowsBottom'=>$this->importantRowsBottom,
  277. 'maxRow'=>$this->maxRow,
  278. 'minRow'=>$this->minRow,
  279. 'mtype'=>$this->mtype,
  280. 'registerGridLinkIcon'=>$this->registerGridLinkIcon,
  281. 'rowList'=>$this->rowList,
  282. 'rowNum'=>$this->rowNum,
  283. 'rows'=>$this->rows,
  284. 'sColumns'=>$this->sColumns,
  285. 'sGridId'=>$this->sGridId,
  286. 'sGridWrapperId'=>$this->sGridWrapperId,
  287. 'sortname'=>$this->sortname,
  288. 'sortorder'=>$this->sortorder,
  289. 'sTitle'=>$this->sTitle,
  290. 'title'=>$this->title,
  291. 'totalRecords'=>$this->totalRecords,
  292. 'url'=>$this->url,
  293. 'viewrecords'=>$this->viewrecords,
  294. 'colModel','colNames','i','n'
  295. );
  296. // render the view file
  297. $this->render('wGrid',$data);
  298. }
  299. }