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