1: <?php
2: /**
3: * Description of Grid
4: *
5: * @author SARTAJ
6: */
7: //namespace Sphp\comp\data;
8: namespace{
9:
10: class Grid extends \Sphp\tools\Component{
11: public $pageNo = -1;
12: public $totalPages = 1;
13: public $perPageRows = 10;
14: public $sql = '';
15: public $pageCountSQL = '';
16: public $result;
17: public $row;
18: public $linkno = 10;
19: public $extraData = '';
20: public $strFormat = '';
21: public $fieldNames = '';
22: public $headNames = '';
23: public $colwidths = '';
24: public $where = '';
25: public $app = '';
26: public $blnEdit = false;
27: public $blnDelete = false;
28: public $cacheTime = 0;
29: private $eventName = 'show';
30: private $evtp='';
31: private $ctrl='';
32: private $extra='page=';
33: private $baseName='';
34: private $sesID=false;
35: private $blnajax = false;
36: private $blndlg = true;
37: private $cachefile = '';
38: private $cachekey = 'id';
39: private $cachesave = false;
40: private $header = '';
41: private $footer = '';
42:
43: protected function onit() {
44: $page = \SphpBase::page();
45: $sphp_router = \SphpBase::sphp_router();
46: $ctrl = $sphp_router->ctrl;
47: $tblName = \SphpBase::page()->tblName;
48: $JSServer = \SphpBase::JSServer();
49: $Client = \SphpBase::sphp_request();
50:
51: if(SphpBase::page()->isSesSecure){
52: $this->sesID = true;
53: }
54: $this->blnajax = true;
55: $this->eventName = $name ."_show";
56: $JSServer->getAJAX();
57: if($Client->isRequest($name.'page')){
58: \SphpBase::sphp_request()->session($name.'p', \SphpBase::sphp_request()->request($name.'page'));
59: \SphpBase::sphp_request()->session($name.'pc', $ctrl);
60: }else{
61: if($Client->session($name.'pc') == $ctrl){
62: \SphpBase::sphp_request()->request($name.'page', \SphpBase::sphp_request()->session($name.'p'));
63: }
64: }
65: $this->pageNo = $Client->request($name.'page') - 1;
66: $this->dtable = $tblName;
67: $this->setHTMLName('');
68: }
69:
70: public function handleEvents(){
71: extract(getGlobals(), EXTR_REFS);
72: $page = \SphpBase::page();
73: $JSServer = \SphpBase::JSServer();
74: $genFormFront = readGlobal("genFormFront");
75: if(SphpBase::page()->isevent)
76: {
77: switch(SphpBase::page()->getEvent()){
78: case $this->name.'_show':{
79: $JSServer->addJSONComp($this,'showalledtif');
80: break;
81: }
82: case $this->name.'_newa':{
83: $JSServer->addJSONFront($genFormFront,'showalledtif');
84: break;
85: }
86: }
87: }
88:
89: }
90:
91: public function getEventURL($eventName, $evtp='', $Appgate='', $extra='', $newBasePath='', $blnSesID=false){
92: $this->eventName = $eventName;
93: $this->evtp=$evtp;
94: $this->ctrl=$Appgate;
95: if($extra!=''){
96: $this->extra=$extra.'&'.$this->name.'page=';
97: }
98: $this->baseName=$newBasePath;
99: $this->sesID=$blnSesID;
100: }
101:
102: protected function genhelpPropList() {
103: $this->addHelpPropFunList('getEventURL','Set Event Path to get page', getEventURL($this->eventName, $this->evtp, $this->ctrl, $this->extra, $this->baseName, $this->sesID),'$eventName, $evtp="", $Appgate="", $extra="", $newBasePath="", $blnSesID=false');
104: $this->addHelpPropFunList('setMsgName','Name Display in placeholder and Error','','$val');
105: $this->addHelpPropFunList('setSQL','Set SQL Database Query','','$sql');
106: $this->addHelpPropFunList('setPageCountSQL','Set SQL Query for Count Page, only need to set if you use setSQL','','$sql');
107: $this->addHelpPropFunList('setPerPageRows','Set Per Page Rows Display','','$val');
108: $this->addHelpPropFunList('setExtraData','Set Extra query string to post server with every page request','','$sql');
109: $this->addHelpPropFunList('setCacheKey','Set Key for Cache default is id','','$val');
110: $this->addHelpPropFunList('setCacheTime','Set Cache Expiry Time 0 mean no cache and -1 mean always data from cache','','$val');
111: $this->addHelpPropFunList('setFieldNames','comma separated list for Database Table field name for auto sql, no need to setSQL','','$val');
112: $this->addHelpPropFunList('setHeaderNames','comma separated list which is use to generate html table tag head section, match with filed list','','$val');
113: $this->addHelpPropFunList('setColWidths','Set col width comma list for td tags','','$list');
114: $this->addHelpPropFunList('setWhere','SQL Query logic like WHERE','','$val');
115: $this->addHelpPropFunList('setApp','Bind with app controller','','$val');
116: $this->addHelpPropFunList('setAjax','Enable AJAX','','');
117: $this->addHelpPropFunList('setEdit','Enable Edit Button','','');
118: $this->addHelpPropFunList('setDelete','Enable Delete Button','','');
119: $this->addHelpPropFunList('setHeader','Set Header HTML','','$val');
120: $this->addHelpPropFunList('setFooter','Set Footer HTML','','$val');
121: $this->addHelpPropFunList('unsetDialog','Disable Dialog','','');
122: //$this->addHelpPropFunList('unsetAddButton','Disable Add Record Button','','$val');
123: $this->addHelpPropList('dtable','comma list for database tables to query');
124: }
125:
126: public function fu_setMsgName($val) { $this->msgName = $val;}
127: public function fu_setSQL($sql){
128: $this->sql = $sql;
129: }
130: public function fu_setPageCountSQL($sql){
131: $this->pageCountSQL = $sql;
132: }
133: public function fu_setPerPageRows($val){
134: $this->perPageRows = intval($val);
135: }
136: public function fu_setExtraData($val){
137: $this->extraData = $val;
138: }
139: public function setPageNo($val){
140: $this->pageNo = intval($val) - 1;
141: }
142: public function getPageNo(){
143: return $this->pageNo + 1;
144: }
145: public function setLinkNo($val){
146: $this->linkno = $val;
147: }
148: public function fu_setCacheFile($val){
149: $this->cachefile = $val;
150: }
151: public function fu_setCacheSave(){
152: $this->cachesave = true;
153: }
154: public function fu_setCacheKey($val){
155: $this->cachekey = $val;
156: }
157: public function fu_setCacheTime($val){
158: $this->cacheTime = intval($val);
159: }
160: public function fu_setFieldNames($val){
161: $this->fieldNames = $val;
162: }
163: public function fu_setHeaderNames($val){
164: $this->headNames = $val;
165: }
166: public function fu_setColWidths($val){
167: $this->colwidths = $val;
168: }
169: public function fu_setWhere($val){
170: $this->where = $val;
171: }
172: public function setApp($val){
173: $this->app = $val;
174: }
175: public function fu_setEdit(){
176: $this->blnEdit = true;
177: }
178: public function fu_setDelete(){
179: $this->blnDelete = true;
180: }
181: public function getPageBar(){
182: return $this->getPaging();
183: }
184: public function fu_setHeader($val){
185: $this->header = $val;
186: }
187: public function fu_setFooter($val){
188: $this->footer = $val;
189: }
190: public function fu_unsetDialog(){
191: $this->blndlg = false;
192: }
193:
194: public function executeSQL(){
195: $mysql = \SphpBase::dbEngine();
196: $HTMLParser = new \Sphp\tools\HTMLParser();
197: $sphp_settings = \SphpBase::sphp_settings();
198: $stro = "";
199: // count total page
200: $mysql->connect();
201: if($this->cachefile!=''){
202: $res = $mysql->fetchQuery($this->pageCountSQL,$this->cacheTime,$this->cachefile.'csql');
203: }else{
204: $res = $mysql->fetchQuery($this->pageCountSQL,$this->cacheTime);
205: }
206: if ($res){
207: $row = current($res['news']);
208: //$totalRows = intval($row['count(id)']);
209: $totalRows = intval(current($row));
210: $this->totalPages = intval(($totalRows + $this->perPageRows - 1) / $this->perPageRows);
211: if($this->pageNo < 0){
212: $this->pageNo = 0;
213: }
214: else if($this->pageNo + 1 > $this->totalPages){
215: $this->pageNo = $this->totalPages - 1 ;
216: }
217:
218: $startat = $this->pageNo * $this->perPageRows;
219: if($startat<0){$startat=0;}
220: $this->result = $mysql->fetchQuery($this->sql." LIMIT $startat,$this->perPageRows",$this->cacheTime,$this->cachefile,$this->cachekey,$this->cachesave);
221: $mysql->disconnect();
222: $stro = '';
223: if ($this->result){
224: if($this->fieldNames!='' && $this->strFormat==''){
225: $stro = '<table class="pagtable">';
226: if($this->headNames==''){
227: $this->headNames = $this->fieldNames;
228: }
229: $arr = explode(',',$this->headNames);
230: $lenw = -1;
231: $startw = 0;
232: $w = '';
233: if($this->colwidths!=''){
234: $arrw = explode(',',$this->colwidths);
235: $lenw = count($arrw)-1;
236: $startw = 0;
237: }
238: $stro .= "<tr class=\"paghead\">";
239: foreach($arr as $key=>$val){
240: if($lenw>=$startw){
241: $w = ' width="'.$arrw[$startw].'"';
242: $startw += 1;
243: }else{
244: $w = '';
245: }
246: $stro .= "<th$w>$val</th>";
247: }
248: if($this->blnEdit){
249: $stro .= "<th width=\"10\">Edit</th>";
250: }
251: if($this->blnDelete){
252: $stro .= "<th width=\"10\">Delete</th>";
253: }
254: $stro .= "</tr>";
255: $blnf = true;
256: foreach($this->result as $key1=>$keyar){
257: foreach($keyar as $key=>$row){
258: $arr = explode(',',$this->fieldNames);
259: if($blnf){
260: $stro .= "<tr class=\"pagrow1\">";
261: $blnf = false;
262: }else{
263: $stro .= "<tr class=\"pagrow2\">";
264: $blnf = true;
265: }
266: $startw = 0;
267: foreach($arr as $key=>$val){
268: if($lenw>=$startw){
269: $w = ' width="'.$arrw[$startw].'"';
270: $startw += 1;
271: }else{
272: $w = '';
273: }
274: $stro .= "<td$w>".$row[$val]."</td>";
275: }
276: if($this->blnEdit){
277: $stro .= "<td><a href=\"#\" onclick=\"pagiedit_$this->name('". getEventURL($this->name."_view",$row['id'],$this->app,$this->extraData,'',true)."');\" title=\"Click to Edit This Record\"><img src=\"{$sphp_settings->comp_respath}/comp/data/res/editBTN.gif\" border=\"0\" /></a></td>";
278: }
279: if($this->blnDelete){
280: $stro .= "<td><a href=\"#\" onClick=\"confirmDel_$this->name('".getEventURL($this->name."_delete",$row['id'],$this->app,$this->extraData,'',true)."')\" title=\"Click to Delete This Record\"><img src=\"{$sphp_settings->comp_respath}/comp/data/res/del.jpg\" border=\"0\" /></a></td>";
281: }
282:
283: $stro .= "</tr>";
284: }
285: }
286: $stro .= "</table>";
287: $stro .= $this->getPaging();
288: }
289: else if($this->strFormat!=''){
290: $stro = $HTMLParser->executePHPCode('<?php foreach($'. $this->name .'->result as $key1=>$keyar){
291: foreach($keyar as $index=>$'.$this->name.'->row){
292: $tmpf = new FrontFile($'.$this->name.'->strFormat,true);
293: $tmpf->run();
294: print $tmpf->data;
295: } } ?>');
296: $this->unsetrenderTag();
297: }
298: }
299: }
300: return $stro;
301:
302: }
303:
304: private function getPaging(){
305: $lynx = "";
306: $del = "";
307: $linkNo = $this->linkno;
308: $startPage = $this->getPageNo()- $linkNo;
309: $endPage = $this->getPageNo()+$linkNo;
310: if($startPage<1){
311: $startPage = 1;
312: }
313: if($endPage>$this->totalPages){
314: $endPage = $this->totalPages;
315: }
316: for ($k=$startPage; $k<=$endPage; $k++) {
317: if ($k != \SphpBase::sphp_request()->request($this->name.'page')) {
318: if($this->blnajax){
319: $lynx .= "<div style=\"float:left;\"><a href=\"#\" onclick=\"getURL('". getEventURL($this->eventName,$this->evtp,$this->ctrl,$this->extra.$k,$this->baseName,$this->sesID)."');\">".($k)."</a></div>";
320: }else{
321: $lynx .= "<div style=\"float:left;\"><a href=\"". getEventURL($this->eventName,$this->evtp,$this->ctrl,$this->extra.$k,$this->baseName,$this->sesID)."\">".($k)."</a></div>";
322: }
323: } else {
324: $lynx .= "<div style=\"float:left;\">".($k)."</div>";
325: }
326: }
327: $startPage2 = $this->getPageNo();
328: $blnEndP = false;
329: $blnStartP = false;
330: if($startPage2>1){
331: $prev = $startPage2 - 1;
332: }else{
333: $prev = 1;
334: $blnStartP = true;
335: }
336: if($startPage2 >= $this->totalPages){
337: $next = $this->totalPages;
338: $blnEndP = true;
339: }else{
340: $next = $startPage2 + 1;
341: }
342: $edt = '';
343: /* use for multi delete functions
344: if($this->blnEdit){
345: $edt = "&nbsp;&nbsp;<a class=\"pagedit\" href=\"". $this->linkURL. "?page=" . $next ."\">Edit</a>";
346: }
347: if($this->blnDelete){
348: $del = "&nbsp;&nbsp;<a class=\"pagdelete\" href=\"". $this->linkURL. "?page=" . $next ."\">Delete</a>";
349: }
350: *
351: */
352: if($blnStartP){
353: $strlinkP = "";
354: }else{
355: if($this->blnajax){
356: $strlinkP = "<a class=\"pagprev\" href=\"#\" onclick=\"getURL('". getEventURL($this->eventName,$this->evtp,$this->ctrl,$this->extra.$prev,$this->baseName,$this->sesID)."');\">Prev</a>&nbsp;&nbsp;";
357: }else{
358: $strlinkP = "<a class=\"pagprev\" href=\"". getEventURL($this->eventName,$this->evtp,$this->ctrl,$this->extra.$prev,$this->baseName,$this->sesID)."\">Prev</a>&nbsp;&nbsp;";
359: }
360: }
361: if($blnEndP){
362: $strlinkN = "";
363: }else{
364: if($this->blnajax){
365: $strlinkN = "<a class=\"pagnext\" href=\"#\" onclick=\"getURL('".getEventURL($this->eventName,$this->evtp,$this->ctrl,$this->extra.$next,$this->baseName,$this->sesID)."');\">Next</a>";
366: }else{
367: $strlinkN = "<a class=\"pagnext\" href=\"".getEventURL($this->eventName,$this->evtp,$this->ctrl,$this->extra.$next,$this->baseName,$this->sesID)."\">Next</a>";
368: }
369: }
370: $strlink = $strlinkP . $strlinkN ;
371:
372: if($blnStartP && $blnEndP){
373: $strout = '';
374: }else{
375: $strout = '<div class="pagbar"><div class="pagnums">&nbsp; '.$lynx.'</div>
376: <div class="pagprevnext">'.
377: $strlink .$edt.$del.'
378: </div></div>
379: <div style="clear:both"></div>';
380: }
381:
382: return $strout;
383: }
384:
385: protected function oncreate($element){
386: $this->strFormat = $element->innertext;
387: $element->innertext = '';
388: }
389:
390: public function startAJAX(){
391: $opendlg = "";
392: if($this->blndlg){
393: addHeaderJSFunctionCode('ready',$this->name,'jql("#'.$this->name.'_dlg").dialog({
394: autoOpen: false,
395: width: "auto",
396: height: "400",
397: show: {
398: effect: "blind",
399: duration: 1000
400: },
401: hide: {
402: effect: "explode",
403: duration: 1000
404: },
405: position: [10,10],
406: title: "Grid Editor Form"
407: });
408: ');
409: addHeaderCSS('dragdrop', '
410: .dragdrop
411: {
412: position: relative;
413: cursor: auto;
414: }
415: ');
416: $opendlg = "jql(\"#". $this->name ."_dlg\").dialog(\"open\");";
417: }
418: if($this->blnajax){
419: $jsAjax = "true";
420: }else{
421: $jsAjax = "false";
422: }
423: addHeaderJSCode($this->name,"
424: function confirmDel_$this->name(link){
425: confirmDel(link,$jsAjax);
426: }
427: function pagiedit_$this->name(link){
428: $opendlg
429: pagiedit(link,$jsAjax);
430: }
431: function paginew_$this->name(link){
432: $opendlg
433: pagiedit(link,$jsAjax);
434: }
435: ");
436: addHeaderJSCode('pagi',"
437: function confirmDel(link,jsajax){
438: var ans = confirm('Are You Sure to Delete This Record !') ;
439: if(ans){
440: if(jsajax){
441: getURL(link);
442: }else{
443: window.location = link ;
444: }
445: }
446: }
447: function pagiedit(link,jsajax) {
448: if(jsajax){
449: getURL(link);
450: }else{
451: window.location = link ;
452: }
453: return false;
454: }
455: function paginew(link,jsajax){
456: if(jsajax){
457: getURL(link);
458: }else{
459: window.location = link ;
460: }
461: }
462: ");
463: $ptag = '<div id="'.$this->name.'_dlg" class="dragdrop">
464: <div id="'.$this->name.'_editor" style="width:100%;height:100%;"></div>
465: </div><input type="button" value="Add" onclick="paginew_'.$this->name.'(\''.getEventURL($this->name.'_newa','','','','',true).'\');" />';
466: $divt = "$ptag<div id=\"{$this->name}_list\">";
467: $this->setPreTag($divt.$this->getPreTag());
468: $this->setPostTag('</div>'.$this->getPostTag());
469:
470: }
471:
472: protected function onjsrender(){
473: $JSServer = \SphpBase::JSServer();
474: if(! $JSServer->ajaxrender){
475: if($this->blnajax){
476: $this->startAJAX();
477: }
478: }
479:
480:
481: }
482:
483: protected function onrender(){
484: // set default values
485: $spt = explode(',', $this->dtable);
486: if(count($spt)>0){
487: $idf = $spt[0].".id";
488: }else{
489: $idf = "id";
490: }
491: if($this->pageCountSQL==''){
492: $this->pageCountSQL = "SELECT count($idf) FROM ".$this->dtable." ".$this->where;
493: }
494: if($this->sql==''){
495: $this->sql = "SELECT $idf,$this->fieldNames FROM ".$this->dtable." ".$this->where;
496: }
497:
498: $this->parameterA['class'] = 'pag';
499: $this->innerHTML = $this->header. $this->executeSQL() . $this->footer;
500: //$this->innerHTML = $this->header.$this->footer;
501: //$this->unsetrender();
502: }
503:
504:
505: }
506: }
507: