1: <?php
2: /**
3: * Description of Pagination
4: *
5: * @author SARTAJ
6: */
7: namespace {
8:
9:
10: class Pagination extends \Sphp\tools\Component{
11: public $pageNo = -1;
12: public $totalPages = 1;
13: public $totalRows = 0;
14: public $perPageRows = 10;
15: public $sql = '';
16: public $pageCountSQL = '';
17: public $result;
18: public $row;
19: public $linkno = 10;
20: public $extraData = '';
21: public $strFormat = '';
22: public $fieldNames = '';
23: public $headNames = '';
24: public $colwidths = '';
25: public $where = '';
26: public $app = '';
27: public $blnEdit = false;
28: public $blnDelete = false;
29: public $eventName = 'show';
30: public $editeventName = 'view';
31: public $deleventName = 'delete';
32: private $evtp='';
33: private $ctrl='';
34: private $extra='page=';
35: private $baseName='';
36: private $sesID=false;
37: private $blnajax = false;
38: private $blndlg = true;
39: private $blnadd = true;
40: public $cacheTime = 0;
41: private $cachefile = '';
42: private $primarykey = "id";
43: private $cachekey = 'id';
44: private $cachesave = false;
45: private $header = '';
46: private $footer = '';
47: private $roote = null;
48: public $buttonnext = '';
49: public $buttonprev = '';
50: public $page_links = '';
51:
52: protected function onit() {
53: $ctrl = \SphpBase::sphp_router()->ctrl;
54: //\SphpBase::debug()->setMsg('tblName ' . $tableName);
55: if(SphpBase::page()->isSesSecure){
56: $this->sesID = true;
57: }
58: if(\SphpBase::sphp_request()->request('page') != ""){
59: \SphpBase::sphp_request()->session($name.'p', \SphpBase::sphp_request()->request('page'));
60: \SphpBase::sphp_request()->session($name.'pc', $ctrl);
61: }else{
62: \SphpBase::sphp_request()->request('page', 1);
63: if(\SphpBase::sphp_request()->isSession($name.'pc') && \SphpBase::sphp_request()->session($name.'pc', $ctrl)){
64: \SphpBase::sphp_request()->request('page', \SphpBase::sphp_request()->session($name.'p'));
65: }
66: }
67: $this->pageNo = intval(\SphpBase::sphp_request()->request('page')) - 1;
68: $this->dtable = \SphpBase::page()->tblName; echo ' gh del ' . \SphpBase::page()->tblName;
69: }
70:
71:
72: protected function genhelpPropList() {
73: $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');
74: $this->addHelpPropFunList('setMsgName','Name Display in placeholder and Error','','$val');
75: $this->addHelpPropFunList('setSQL','Set SQL Database Query','','$sql');
76: $this->addHelpPropFunList('setPageCountSQL','Set SQL Query for Count Page, only need to set if you use setSQL','','$sql');
77: $this->addHelpPropFunList('setPerPageRows','Set Per Page Rows Display','','$val');
78: $this->addHelpPropFunList('setExtraData','Set Extra query string to post server with every page request','','$sql');
79: $this->addHelpPropFunList('setCacheKey','Set Key for Cache default is id','','$val');
80: $this->addHelpPropFunList('setCacheTime','Set Cache Expiry Time 0 mean no cache and -1 mean always data from cache','','$val');
81: $this->addHelpPropFunList('setFieldNames','comma separated list for Database Table field name for auto sql, no need to setSQL','','$val');
82: $this->addHelpPropFunList('setHeaderNames','comma separated list which is use to generate html table tag head section, match with filed list','','$val');
83: $this->addHelpPropFunList('setColWidths','Set col width comma list for td tags','','$list');
84: $this->addHelpPropFunList('setWhere','SQL Query logic like WHERE','','$val');
85: $this->addHelpPropFunList('setApp','Bind with app controller','','$val');
86: $this->addHelpPropFunList('setAjax','Enable AJAX','','');
87: $this->addHelpPropFunList('setEdit','Enable Edit Button','','');
88: $this->addHelpPropFunList('setDelete','Enable Delete Button','','');
89: $this->addHelpPropFunList('setHeader','Set Header HTML','','$val');
90: $this->addHelpPropFunList('setFooter','Set Footer HTML','','$val');
91: $this->addHelpPropFunList('unsetDialog','Disable Dialog','','');
92: $this->addHelpPropFunList('unsetAddButton','Disable Add Record Button','','$val');
93: $this->addHelpPropList('dtable','comma list for database tables to query');
94: }
95:
96:
97: public function getEventURL($eventName, $evtp='', $Appgate='', $extra='', $newBasePath='', $blnSesID=false){
98: $this->eventName = $eventName;
99: $this->evtp=$evtp;
100: $this->ctrl=$Appgate;
101: if($extra!=''){
102: $this->extra=$extra.'&page=';
103: }
104: $this->baseName=$newBasePath;
105: $this->sesID=$blnSesID;
106: }
107: public function getRow($field) {
108: if(isset($this->row[$field])){
109: return $this->row[$field];
110: }else{
111: return "";
112: }
113: }
114: public function fu_setMsgName($val) { $this->msgName = $val;}
115: public function fu_setPrimaryKey($val){
116: $this->primarykey = $val;
117: if($this->cachekey == "id") $this->cachekey = $this->primarykey;
118: }
119: public function fu_setSQL($sql){
120: $this->sql = $sql;
121: }
122: public function fu_setPageCountSQL($sql){
123: $this->pageCountSQL = $sql;
124: }
125: public function fu_setPerPageRows($val){
126: $this->perPageRows = intval($val);
127: }
128: public function fu_setExtraData($val){
129: $this->extraData = $val;
130: }
131: public function setPageNo($val){
132: $this->pageNo = $val - 1;
133: }
134: public function getPageNo(){
135: return $this->pageNo + 1;
136: }
137: public function setLinkNo($val){
138: $this->linkno = $val;
139: }
140: public function fu_setCacheFile($val){
141: $this->cachefile = $val;
142: }
143: public function fu_setCacheSave(){
144: $this->cachesave = true;
145: }
146: public function fu_setCacheKey($val){
147: $this->cachekey = $val;
148: }
149: public function fu_setCacheTime($val){
150: $this->cacheTime = intval($val);
151: }
152: public function fu_setFieldNames($val){
153: $this->fieldNames = $val;
154: }
155: public function fu_setHeaderNames($val){
156: $this->headNames = $val;
157: }
158: public function fu_setColWidths($val){
159: $this->colwidths = $val;
160: }
161: public function fu_setWhere($val){
162: $this->where = $val;
163: }
164: public function fu_setApp($val){
165: $this->app = $val;
166: }
167: public function fu_setAjax(){
168: $this->blnajax = true;
169: $this->eventName = $this->name ."_show";
170: $this->editeventName = $this->name ."_view";
171: $this->deleventName = $this->name ."_delete";
172: SphpJsM::addjQueryUI();
173: }
174: public function fu_setEdit(){
175: $this->blnEdit = true;
176: }
177: public function fu_setDelete(){
178: $this->blnDelete = true;
179: }
180: public function getPageBar(){
181: return $this->page_links;
182: }
183: public function getButtonNext(){
184: return $this->buttonnext;
185: }
186: public function getButtonPrev(){
187: return $this->buttonprev;
188: }
189: public function fu_setHeader($val){
190: $this->header = $val;
191: }
192: public function fu_setFooter($val){
193: $this->footer = $val;
194: }
195: public function fu_unsetDialog(){
196: $this->blndlg = false;
197: }
198: public function fu_unsetAddButton(){
199: $this->blnadd = false;
200: }
201: public function executeSQL(){
202: $mysql = \SphpBase::dbEngine();
203: $respath = \SphpBase::sphp_settings()->res_path;
204: $stro = "";
205: // count total page
206: //$mysql->connect();
207: if($this->cachefile!=''){
208: $res = $mysql->fetchQuery($this->pageCountSQL,$this->cacheTime,$this->cachefile.'csql');
209: }else{
210: $res = $mysql->fetchQuery($this->pageCountSQL,$this->cacheTime);
211: }
212: if ($res){
213: $row = current($res['news']);
214: //$totalRows = intval($row['count(id)']);
215: $totalRows = intval(current($row));
216: $this->totalRows = $totalRows;
217: $this->totalPages = intval(($totalRows + $this->perPageRows - 1) / $this->perPageRows);
218: if($this->pageNo < 0){
219: $this->pageNo = 0;
220: }
221: else if($this->pageNo + 1 > $this->totalPages){
222: $this->pageNo = $this->totalPages - 1 ;
223: }
224:
225: $startat = $this->pageNo * $this->perPageRows;
226: if($startat<0){$startat=0;}
227: $this->result = $mysql->fetchQuery($this->sql." LIMIT $startat,$this->perPageRows",$this->cacheTime,$this->cachefile,$this->cachekey,$this->cachesave);
228: //$mysql->disconnect();
229: $stro = '';
230: if ($this->result){
231: if($this->fieldNames!='' && $this->strFormat==''){
232: $stro = '<table class="table table-striped pagtable">';
233: if($this->headNames==''){
234: $this->headNames = $this->fieldNames;
235: }
236: $arr = explode(',',$this->headNames);
237: $lenw = -1;
238: $startw = 0;
239: $w = '';
240: if($this->colwidths!=''){
241: $arrw = explode(',',$this->colwidths);
242: $lenw = count($arrw)-1;
243: $startw = 0;
244: }
245: $stro .= "<thead><tr class=\"paghead\">";
246: foreach($arr as $key=>$val){
247: if($lenw>=$startw){
248: $w = ' width="'.$arrw[$startw].'"';
249: $startw += 1;
250: }else{
251: $w = '';
252: }
253: $stro .= "<th$w>$val</th>";
254: }
255: if($this->blnEdit){
256: $stro .= "<th width=\"10\">Edit</th>";
257: }
258: if($this->blnDelete){
259: $stro .= "<th width=\"10\">Delete</th>";
260: }
261: $stro .= "</tr></thead><tbody>";
262: $blnf = true;
263: foreach($this->result as $key1=>$keyar){
264: foreach($keyar as $key=>$row){
265: $arr = explode(',',$this->fieldNames);
266: if($blnf){
267: $stro .= "<tr class=\"pagrow1\">";
268: $blnf = false;
269: }else{
270: $stro .= "<tr class=\"pagrow2\">";
271: $blnf = true;
272: }
273: $startw = 0;
274: foreach($arr as $key=>$val){
275: if($lenw>=$startw){
276: $w = ' width="'.$arrw[$startw].'"';
277: $startw += 1;
278: }else{
279: $w = '';
280: }
281: $stro .= "<td$w>".$row[$val]."</td>";
282: }
283: if($this->blnEdit){
284: $stro .= "<td width=\"25\"><a href=\"#\" onclick=\"pagiedit_$this->name('". getEventURL($this->editeventName,$row['id'],$this->app,$this->extraData,'',true)."');\" title=\"Click to Edit This Record\"><img src=\"". $this->myrespath ."/assets/editBTN.gif\" border=\"0\" /></a></td>";
285: }
286: if($this->blnDelete){
287: $stro .= "<td width=\"25\"><a href=\"#\" onClick=\"confirmDel_$this->name('".getEventURL($this->deleventName,$row['id'],$this->app,$this->extraData,'',true)."')\" title=\"Click to Delete This Record\"><img src=\"". $this->myrespath ."/assets/del.jpg\" border=\"0\" /></a></td>";
288: }
289:
290: $stro .= "</tr>";
291: }
292: }
293: $stro .= "</tbody></table>";
294: $stro .= $this->getPaging();
295: }
296: else if($this->strFormat!=''){
297: //$this->frontobj->HTMLParser->executePHPCode()
298: $stro = "";
299: if($this->roote == null){
300: $this->roote = $this->frontobj->getChildrenWrapper($this);
301: }
302: foreach($this->result as $key1=>$keyar){
303: foreach($keyar as $index=>$this->row){
304: //$tmpf = new \Sphp\tools\FrontFileChild($this->strFormat,true,null,$this->frontobj);
305: //$tmpf->run();
306: //$stro .= $tmpf->data;
307: $stro .= $this->frontobj->parseComponentChildren($this->roote);
308: } }
309: //echo $stro;
310: $this->fu_unsetrenderTag();
311: $strom = $this->getPaging();
312: }
313: }
314: }
315: return $stro;
316: }
317:
318: private function getPaging(){
319: $lynx = "";
320: $del = "";
321: $linkNo = $this->linkno;
322: $startPage = $this->getPageNo()- $linkNo;
323: $endPage = $this->getPageNo()+$linkNo;
324: if($startPage<1){
325: $startPage = 1;
326: }
327: if($endPage>$this->totalPages){
328: $endPage = $this->totalPages;
329: }
330: $strstart = "<div class=\"pfloat-left\">";
331: for ($k=$startPage; $k<=$endPage; $k++) {
332: if ($k != \SphpBase::sphp_request()->request('page')) {
333: if($this->blnajax){
334: $lynx .= $strstart. "<a href=\"#\" onclick=\"getURL('". getEventURL($this->eventName,$this->evtp,$this->ctrl,$this->extra.$k,$this->baseName,$this->sesID)."');\">".($k)."</a></div>";
335: }else{
336: $lynx .= $strstart."<a href=\"". getEventURL($this->eventName,$this->evtp,$this->ctrl,$this->extra.$k,$this->baseName,$this->sesID)."\">".($k)."</a></div>";
337: }
338: } else {
339: $lynx .= $strstart.($k)."</div>";
340: }
341: }
342: $startPage2 = $this->getPageNo();
343: $blnEndP = false;
344: $blnStartP = false;
345: if($startPage2>1){
346: $prev = $startPage2 - 1;
347: }else{
348: $prev = 1;
349: $blnStartP = true;
350: }
351: if($startPage2 >= $this->totalPages){
352: $next = $this->totalPages;
353: $blnEndP = true;
354: }else{
355: $next = $startPage2 + 1;
356: }
357: $edt = '';
358: /* use for multi delete functions
359: if($this->blnEdit){
360: $edt = "&nbsp;&nbsp;<a class=\"pagedit\" href=\"". $this->linkURL. "?page=" . $next ."\">Edit</a>";
361: }
362: if($this->blnDelete){
363: $del = "&nbsp;&nbsp;<a class=\"pagdelete\" href=\"". $this->linkURL. "?page=" . $next ."\">Delete</a>";
364: }
365: *
366: */
367: if($blnStartP){
368: $strlinkP = "";
369: }else{
370: if($this->blnajax){
371: $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;";
372: }else{
373: $strlinkP = "<a class=\"pagprev\" href=\"". getEventURL($this->eventName,$this->evtp,$this->ctrl,$this->extra.$prev,$this->baseName,$this->sesID)."\">Prev</a>&nbsp;&nbsp;";
374: }
375: }
376: if($blnEndP){
377: $strlinkN = "";
378: }else{
379: if($this->blnajax){
380: $strlinkN = "<a class=\"pagnext\" href=\"#\" onclick=\"getURL('".getEventURL($this->eventName,$this->evtp,$this->ctrl,$this->extra.$next,$this->baseName,$this->sesID)."');\">Next</a>";
381: }else{
382: $strlinkN = "<a class=\"pagnext\" href=\"".getEventURL($this->eventName,$this->evtp,$this->ctrl,$this->extra.$next,$this->baseName,$this->sesID)."\">Next</a>";
383: }
384: }
385:
386: if($blnStartP && $blnEndP){
387: $strout = '';
388: }else{
389: $strlink = $strlinkP . $strlinkN ;
390: $this->buttonnext = $strlinkN;
391: $this->buttonprev = $strlinkP;
392: $this->page_links = $lynx;
393: if($strlink!=''){
394: $strout = '<div class="pagbar"><div class="pagnums">&nbsp; '.$lynx.'</div>
395: <div class="pagprevnext">'.
396: $strlink .$edt.$del.'
397: </div></div>
398: <div style="clear:both"></div>';
399: }
400: }
401: return $strout;
402: }
403:
404: protected function oncreate($element){
405: //$this->strFormat = $element->innertext;
406: if($element->innertext != ""){
407: $this->strFormat = "datajfjfh";
408: }
409: //$element->innertext = '';
410: }
411:
412: public function startAJAX(){
413: $opendlg = "";
414: if($this->blndlg){
415: addHeaderJSFunctionCode('ready',$this->name,'
416: jql("#'.$this->name.'_dlg").dialog({
417: autoOpen: false,
418: width: "auto",
419: height: "700",
420: show: {
421: effect: "blind",
422: duration: 1000
423: },
424: hide: {
425: effect: "explode",
426: duration: 1000
427: },
428: position: { my: "center", at: "center", of: window },
429: title: "Grid Editor Form",
430: create: function(event, ui) {
431: $("#'.$this->name.'_dlg").dialog("moveToTop");
432: var widget = $(this).dialog("widget");
433: $(".ui-dialog-titlebar-close", widget)
434: .html(\'<span class="ui-button-icon ui-icon ui-icon-closethick"></span><span class="ui-button-icon-space"> </span>\')
435: .addClass("ui-button ui-corner-all ui-widget ui-button-icon-only ui-dialog-titlebar-close");
436: },
437: closeText: "",
438: modal: true,
439: beforeClose: function(){
440: $("#'.$this->name.'_editor").html("");
441: }
442: });
443: var maxZI = Math.max.apply(null,
444: $.map($(\'body *\'), function(el,index) {
445: if ($(el).css(\'position\') != \'static\')
446: return parseInt($(el).css(\'z-index\')) || 0;
447: }));
448: $("#'.$this->name.'_dlg").parent(".ui-dialog").css("z-index",maxZI+1);
449: ');
450: addHeaderCSS('dragdrop', '
451: .dragdrop
452: {
453: position: relative;
454: cursor: auto;
455: }
456: ');
457: $opendlg = "jql(\"#". $this->name ."_dlg\").dialog(\"open\");";
458: }
459:
460: if($this->blnajax){
461: $jsAjax = "true";
462: }else{
463: $jsAjax = "false";
464: }
465: addHeaderJSCode($this->name,"
466: window['confirmDel_$this->name'] = function (link){
467: confirmDel(link,$jsAjax);
468: };
469: window['pagiedit_$this->name'] = function (link){
470: $opendlg
471: pagiedit(link,$jsAjax);
472: };
473: window['paginew_$this->name'] = function (link){
474: $opendlg
475: pagiedit(link,$jsAjax);
476: };
477: ");
478: addHeaderJSCode('pagi',"
479: function confirmDel(link,jsajax){
480: var ans = confirm('Are You Sure to Delete This Record !') ;
481: if(ans){
482: if(jsajax){
483: getURL(link);
484: }else{
485: window.location = link ;
486: }
487: }
488: }
489: function pagiedit(link,jsajax) {
490: if(jsajax){
491: getURL(link);
492: }else{
493: window.location = link ;
494: }
495: return false;
496: }
497: function paginew(link,jsajax){
498: if(jsajax){
499: getURL(link);
500: }else{
501: window.location = link ;
502: }
503: }
504: $(\"#btnadd{$this->name}\").on('click',function(){paginew_{$this->name}('" . getEventURL($this->name.'_newa','','','','',true) . "');});
505:
506: ");
507:
508:
509: $ptag = '<div id="'.$this->name.'_dlg" class="dragdrop">
510: <div id="'.$this->name.'_editor" style="width:100%;height:100%;"></div>
511: </div><div id="'.$this->name.'_toolbar" class="pb-4">';
512: if($this->blnadd){
513: $ptag .= '<button id="btnadd'. $this->name .'" class="btn btn-primary" >Add</button>';
514: }
515: $divt = "$ptag</div><div id=\"{$this->name}_list\">";
516: $this->setPreTag($divt.$this->getPreTag());
517: $this->setPostTag('</div>'.$this->getPostTag());
518:
519: }
520:
521:
522: protected function onjsrender(){
523: $JSServer = SphpBase::JSServer();
524: $opendlg = "";
525: if(!$JSServer->ajaxrender){
526: if($this->blnajax){
527: $this->eventName = $this->name."_show";
528: $this->editeventName = $this->name."_view";
529: $this->deleventName = $this->name."_delete";
530: $this->startAJAX();
531: }else{
532: $jsAjax = "false";
533: addHeaderJSCode($this->name,"
534: window['confirmDel_$this->name'] = function(link){
535: confirmDel(link,$jsAjax);
536: };
537: window['pagiedit_$this->name'] = function (link){
538: $opendlg
539: pagiedit(link,$jsAjax);
540: };
541: window['paginew_$this->name'] = function (link){
542: $opendlg
543: pagiedit(link,$jsAjax);
544: };
545: ");
546: addHeaderJSCode('pagi',"
547: function confirmDel(link,jsajax){
548: var ans = confirm('Are You Sure to Delete This Record !') ;
549: if(ans){
550: if(jsajax){
551: getURL(link);
552: }else{
553: window.location = link ;
554: }
555: }
556: }
557: function pagiedit(link,jsajax) {
558: if(jsajax){
559: getURL(link);
560: }else{
561: window.location = link ;
562: }
563: return false;
564: }
565: function paginew(link,jsajax){
566: if(jsajax){
567: getURL(link);
568: }else{
569: window.location = link ;
570: }
571: }
572: ");
573: }
574: }
575: }
576:
577: protected function onprerender(){
578: // set default values
579: if($this->dtable == "") $this->dtable = \SphpBase::page()->tblName;
580: $commaPos = strpos($this->dtable, ",");
581: $spacePos = strpos($this->dtable, " ");
582: if($spacePos !== false && ($commaPos === false || $spacePos < $commaPos)){
583: $spt = explode(' ', $this->dtable,2);
584: }else{
585: $spt = explode(',', $this->dtable,2);
586: }
587:
588: if(count($spt)>0){
589: $idf = $spt[0].".{$this->primarykey}";
590: }else{
591: $idf = $this->primarykey;
592: }
593: if($this->pageCountSQL==''){
594: $this->pageCountSQL = "SELECT count($idf) FROM ".$this->dtable." ".$this->where;
595: }
596: if($this->sql==''){
597: $this->sql = "SELECT $idf,$this->fieldNames FROM ".$this->dtable." ".$this->where;
598: }
599:
600: $this->parameterA['class'] = 'pag';
601: //$this->innerHTML = $this->header. $this->executeSQL() . $this->footer;
602:
603: $this->element->replaceChildren($this->header. $this->executeSQL() . $this->footer);
604: //SphpBase::debug()->println("pagi end");
605: //$this->innerHTML = $this->header.$this->footer;
606: //$this->unsetrender();
607: }
608:
609: protected function onholder($obj) {
610: switch($obj->tagName){
611: case 'img':{
612: if($this->row[$obj->getAttribute("dfield")] != ''){
613: $obj->setAttribute('src',$this->row[$obj->getAttribute("dfield")]);
614: }else if($obj->hasAttribute('default')){
615: $obj->setAttribute('src',$obj->getAttribute('default'));
616: }
617: break;
618: }default:{
619: if($this->row[$obj->getAttribute("dfield")] != ''){
620: $obj->setInnerHTML($this->row[$obj->getAttribute("dfield")]);
621: }else if($obj->hasAttribute('default')){
622: $obj->setInnerHTML($obj->getAttribute('default'));
623: }
624: break;
625: }
626: }
627: }
628:
629:
630: }
631:
632: }
633: