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