public function renderGrid($tblelement = '', $pager = '', $script = true, array $summary = null, array $params = null, $createtbl = false, $createpg = false, $echo = true) { $oper = $this->GridParams["oper"]; $goper = jqGridUtils::GetParam($oper, 'nooper'); if ($goper == $this->GridParams["excel"]) { if (!$this->export) { return false; } $this->exportToExcel($summary, $params, $this->colModel); } else { if (in_array($goper, array_values($this->GridParams))) { $this->editGrid($summary, $params, $goper); } else { if (!isset($this->gridOptions["datatype"])) { $this->gridOptions["datatype"] = $this->dataType; } $this->gridOptions['colModel'] = $this->colModel; if (isset($this->gridOptions['postData'])) { $this->gridOptions['postData'] = jqGridUtils::array_extend($this->gridOptions['postData'], array($oper => $this->GridParams["query"])); } else { $this->setGridOptions(array("postData" => array($oper => $this->GridParams["query"]))); } if (isset($this->primaryKey)) { $this->GridParams["id"] = $this->primaryKey; } $this->setGridOptions(array("prmNames" => $this->GridParams)); $s = ''; if ($createtbl) { $tmptbl = $tblelement; if (strpos($tblelement, "#") === false) { $tblelement = "#" . $tblelement; } else { $tmptbl = substr($tblelement, 1); } $s .= "<table id='" . $tmptbl . "'></table>"; } if ($createpg) { $tmppg = $pager; if (strpos($pager, "#") === false) { $pager = "#" . $pager; } else { $tmppg = substr($pager, 1); } $s .= "<div id='" . $tmppg . "'></div>"; } if (strlen($pager) > 0) { $this->setGridOptions(array("pager" => $pager)); } $this->editOptions['mtype'] = $this->mtype; $this->addOptions['mtype'] = $this->mtype; $this->delOptions['mtype'] = $this->mtype; if ($script) { $s .= "<script type='text/javascript'>"; $s .= "jQuery(document).ready(function() {"; } $s .= "jQuery('" . $tblelement . "').jqGrid(" . jqGridUtils::encode($this->gridOptions) . ");"; if ($this->navigator && strlen($pager) > 0) { $s .= "jQuery('" . $tblelement . "').jqGrid('navGrid','" . $pager . "'," . jqGridUtils::encode($this->navOptions); $s .= "," . jqGridUtils::encode($this->editOptions); $s .= "," . jqGridUtils::encode($this->addOptions); $s .= "," . jqGridUtils::encode($this->delOptions); $s .= "," . jqGridUtils::encode($this->searchOptions); $s .= "," . jqGridUtils::encode($this->viewOptions) . ");"; if ($this->navOptions["excel"] == true) { $eurl = $this->getGridOption('url'); $exexcel = <<<EXCELE onClickButton : function(e) { try { jQuery("{$tblelement}").jqGrid('excelExport',{url:'{$eurl}'}); } catch (e) { window.location= '{$eurl}?oper=excel'; } } EXCELE; $s .= "jQuery('" . $tblelement . "').jqGrid('navButtonAdd','" . $pager . "',{caption:'',title:'Export to Excel'," . $exexcel . "});"; } } if ($this->toolbarfilter) { $s .= "jQuery('" . $tblelement . "').jqGrid('filterToolbar'," . jqGridUtils::encode($this->filterOptions) . ");\n"; } $gM = count($this->gridMethods); if ($gM > 0) { for ($i = 0; $i < $gM; $i++) { $s .= $this->gridMethods[$i] . "\n"; } } if (strlen($this->customCode) > 0) { $s .= jqGridUtils::encode($this->customCode); } if ($script) { $s .= " });</script>"; } if ($echo) { echo $s; } return $echo ? "" : $s; } } }
/** * Main method which do allmost everthing for the grid. * Construct the grid, perform CRUD operations, perform Query and serch operations, * export to excel, set a jqGrid method, and javascript code * @param string $tblelement the id of the table element to costrict the grid * @param string $pager the id for the pager element * @param boolean $script if set to true add a script tag before constructin the grid. * @param array $summary - set which columns should be sumarized in order to be displayed to the grid * By default this parameter uses SQL SUM function: array("colmodelname"=>"sqlname"); * It can be set to use other one this way : * array("colmodelname"=>array("sqlname"=>"AVG")); * By default the first field correspond to the name of colModel the second to * the database name * @param array $params parameters passed to the query * @param boolean $createtbl if set to true the table element is created automatically * from this method. Default is false * @param boolean $createpg if set to true the pager element is created automatically * from this script. Default false. * @param boolean $echo if set to false the function return the string representing * the grid * @return mixed. */ public function renderGrid($tblelement = '', $pager = '', $script = true, array $summary = null, array $params = null, $createtbl = false, $createpg = false, $echo = true) { $oper = $this->GridParams["oper"]; $goper = $this->oper ? $this->oper : 'nooper'; if ($goper == $this->GridParams["autocomplete"]) { return false; } else { if ($goper == $this->GridParams["excel"]) { if (!$this->export) { return false; } $this->exportToExcel($summary, $params, $this->colModel, true, $this->exportfile); } else { if ($goper == "pdf") { if (!$this->export) { return false; } $this->exportToPdf($summary, $params, $this->colModel, $this->pdffile); } else { if ($goper == "csv") { if (!$this->export) { return false; } $this->exportToCsv($summary, $params, $this->colModel, true, $this->csvfile, $this->csvsep, $this->csvsepreplace); } else { if (in_array($goper, array_values($this->GridParams))) { if ($this->inlineNav) { $this->getLastInsert = true; } return $this->editGrid($summary, $params, $goper, $echo); } else { if (!isset($this->gridOptions["datatype"])) { $this->gridOptions["datatype"] = $this->dataType; } // hack for editable=true as default $ed = true; if (isset($this->gridOptions['cmTemplate'])) { $edt = $this->gridOptions['cmTemplate']; $ed = isset($edt['editable']) ? $edt['editable'] : true; } foreach ($this->colModel as $k => $cm) { if (!isset($this->colModel[$k]['editable'])) { $this->colModel[$k]['editable'] = $ed; } } $this->gridOptions['colModel'] = $this->colModel; if (isset($this->gridOptions['postData'])) { $this->gridOptions['postData'] = jqGridUtils::array_extend($this->gridOptions['postData'], array($oper => $this->GridParams["query"])); } else { $this->setGridOptions(array("postData" => array($oper => $this->GridParams["query"]))); } if (isset($this->primaryKey)) { $this->GridParams["id"] = $this->primaryKey; } $this->setGridOptions(array("prmNames" => $this->GridParams)); $s = ''; if ($createtbl) { $tmptbl = $tblelement; if (strpos($tblelement, "#") === false) { $tblelement = "#" . $tblelement; } else { $tmptbl = substr($tblelement, 1); } $s .= "<table id='" . $tmptbl . "'></table>"; } if (strlen($pager) > 0) { $tmppg = $pager; if (strpos($pager, "#") === false) { $pager = "#" . $pager; } else { $tmppg = substr($pager, 1); } if ($createpg) { $s .= "<div id='" . $tmppg . "'></div>"; } } // set the Error handler for data if (!isset($this->gridOptions['loadError'])) { $err = "function(xhr,status, err){ try {jQuery.jgrid.info_dialog(jQuery.jgrid.errors.errcap,'<div class=\"ui-state-error\">'+ xhr.responseText +'</div>', jQuery.jgrid.edit.bClose,{buttonalign:'right'});} catch(e) { alert(xhr.responseText);} }"; $this->setGridEvent('loadError', $err); } //if(!isset($this->editOptions['mtype']) && $this->showError) { //$this->setNavEvent('edit', 'afterSubmit', "function(res,pdata){ var result = res.responseText.split('#'); if(result[0]=='$this->successmsg') return [true,result[1],result[2]]; else return [false,result[1],'']; }"); //} //if(!isset($this->addOptions['mtype']) && $this->showError) { //$this->setNavEvent('add', 'afterSubmit', "function(res,pdata){ var result = res.responseText.split('#'); if(result[0]=='$this->successmsg') return [true,result[1],result[2]]; else return [false,result[1],''];}"); //} if (strlen($pager) > 0) { $this->setGridOptions(array("pager" => $pager)); } //$this->editOptions['mtype'] = $this->mtype; //$this->addOptions['mtype'] = $this->mtype; //$this->delOptions['mtype'] = $this->mtype; if ($this->sharedEditOptions == true) { $this->gridOptions['editOptions'] = $this->editOptions; } if ($this->sharedAddOptions == true) { $this->gridOptions['addOptions'] = $this->addOptions; } if ($this->sharedDelOptions == true) { $this->gridOptions['delOptions'] = $this->delOptions; } if ($script) { $s .= "<script type='text/javascript'>"; $s .= "jQuery(document).ready(function(\$) {"; } $s .= "jQuery('" . $tblelement . "').jqGrid(" . jqGridUtils::encode($this->gridOptions) . ");"; if ($this->navigator && strlen($pager) > 0) { $s .= "jQuery('" . $tblelement . "').jqGrid('navGrid','" . $pager . "'," . jqGridUtils::encode($this->navOptions); $s .= "," . jqGridUtils::encode($this->editOptions); $s .= "," . jqGridUtils::encode($this->addOptions); $s .= "," . jqGridUtils::encode($this->delOptions); $s .= "," . jqGridUtils::encode($this->searchOptions); $s .= "," . jqGridUtils::encode($this->viewOptions) . ");"; if ($this->navOptions["excel"] == true) { $eurl = $this->getGridOption('url'); $exexcel = <<<EXCELE onClickButton : function(e) { \ttry { \t\tjQuery("{$tblelement}").jqGrid('excelExport',{tag:'excel', url:'{$eurl}'}); \t} catch (e) { \t\twindow.location= '{$eurl}?oper=excel'; \t} } EXCELE; $s .= "jQuery('" . $tblelement . "').jqGrid('navButtonAdd','" . $pager . "',{id:'" . $tmppg . "_excel', caption:'" . $this->expoptions['excel']['caption'] . "',title:'" . $this->expoptions['excel']['title'] . "'," . $exexcel . ",buttonicon:'" . $this->expoptions['excel']['buttonicon'] . "'});"; } if ($this->navOptions["pdf"] == true) { $eurl = $this->getGridOption('url'); $expdf = <<<PDFE onClickButton : function(e) { \ttry { \t\tjQuery("{$tblelement}").jqGrid('excelExport',{tag:'pdf', url:'{$eurl}'}); \t} catch (e) { \t\twindow.location= '{$eurl}?oper=pdf'; \t} } PDFE; $s .= "jQuery('" . $tblelement . "').jqGrid('navButtonAdd','" . $pager . "',{id:'" . $tmppg . "_pdf',caption:'" . $this->expoptions['pdf']['caption'] . "',title:'" . $this->expoptions['pdf']['title'] . "'," . $expdf . ", buttonicon:'" . $this->expoptions['pdf']['buttonicon'] . "'});"; } if ($this->navOptions["csv"] == true) { $eurl = $this->getGridOption('url'); $excsv = <<<CSVE onClickButton : function(e) { \ttry { \t\tjQuery("{$tblelement}").jqGrid('excelExport',{tag:'csv', url:'{$eurl}'}); \t} catch (e) { \t\twindow.location= '{$eurl}?oper=csv'; \t} } CSVE; $s .= "jQuery('" . $tblelement . "').jqGrid('navButtonAdd','" . $pager . "',{id:'" . $tmppg . "_csv',caption:'" . $this->expoptions['csv']['caption'] . "',title:'" . $this->expoptions['csv']['title'] . "'," . $excsv . ",buttonicon:'" . $this->expoptions['csv']['buttonicon'] . "'});"; } if ($this->navOptions["columns"] == true) { $clopt = jqGridUtils::encode($this->expoptions['columns']['options']); $excolumns = <<<COLUMNS onClickButton : function(e) { \tjQuery("{$tblelement}").jqGrid('columnChooser',{$clopt}); } COLUMNS; $s .= "jQuery('" . $tblelement . "').jqGrid('navButtonAdd','" . $pager . "',{id:'" . $tmppg . "_col',caption:'" . $this->expoptions['columns']['caption'] . "',title:'" . $this->expoptions['columns']['title'] . "'," . $excolumns . ",buttonicon:'" . $this->expoptions['columns']['buttonicon'] . "'});"; } } // inline navigator if ($this->inlineNav && strlen($pager) > 0) { $aftersave = <<<AFTERS function (id, res) { \tres = res.responseText.split("#"); \ttry { \t\t\$(this).jqGrid('setCell', id, res[0], res[1]); \t\t\$("#"+id, "#"+this.p.id).removeClass("jqgrid-new-row").attr("id",res[1] ); \t\t\$(this)[0].p.selrow = res[1]; \t} catch (asr) {} } AFTERS; $this->inlineNavOpt['addParams'] = jqGridUtils::array_extend($this->inlineNavOpt['addParams'], array("aftersavefunc" => "js:" . $aftersave)); $this->inlineNavOpt['editParams'] = jqGridUtils::array_extend($this->inlineNavOpt['editParams'], array("aftersavefunc" => "js:" . $aftersave)); $s .= "jQuery('" . $tblelement . "').jqGrid('inlineNav','" . $pager . "'," . jqGridUtils::encode($this->inlineNavOpt) . ");\n"; } // toolbar filter if ($this->toolbarfilter) { $s .= "jQuery('" . $tblelement . "').jqGrid('filterToolbar'," . jqGridUtils::encode($this->filterOptions) . ");\n"; } // grid methods $gM = count($this->gridMethods); if ($gM > 0) { for ($i = 0; $i < $gM; $i++) { $s .= $this->gridMethods[$i] . "\n"; } } //at end the custom code if (strlen($this->customCode) > 0) { $s .= jqGridUtils::encode($this->customCode); } if ($script) { $s .= " });</script>"; } if ($echo) { echo $s; } return $echo ? "" : $s; } } } } } }
public static function _mongoSearch($mongoquery, $GridParams = array(), $encoding = 'utf-8', $datearray = array(), $mongointegers = array()) { $s = ''; $v = array(); $sopt = array('eq' => '===', 'ne' => '!==', 'lt' => '<', 'le' => '<=', 'gt' => '>', 'ge' => '>=', 'bw' => "", 'bn' => "", 'in' => '==', 'ni' => '!=', 'ew' => '', 'en' => '', 'cn' => '', 'nc' => ''); $filters = jqGridUtils::GetParam($GridParams["filter"], ""); $rules = ""; // multiple filter if ($filters) { if (function_exists('json_decode') && strtolower(trim($encoding)) == "utf-8") { $jsona = json_decode($filters, true); } else { $jsona = jqGridUtils::decode($filters); } if (is_array($jsona)) { $gopr = strtolower(trim($jsona['groupOp'])); $rules = $jsona['rules']; } // single filter } else { if (jqGridUtils::GetParam($GridParams['searchField'], '')) { $gopr = 'or'; $rules[0]['field'] = jqGridUtils::GetParam($GridParams['searchField'], ''); $rules[0]['op'] = jqGridUtils::GetParam($GridParams['searchOper'], ''); $rules[0]['data'] = jqGridUtils::GetParam($GridParams['searchString'], ''); } } if ($gopr == 'or') { $gopr = ' || '; } else { $gopr = ' && '; } $i = 0; if (!is_array($mongoquery)) { $mongoquery = array(); } foreach ($rules as $key => $val) { $field = $val['field']; $op = $val['op']; $v = $val['data']; if (strlen($v) != 0 && $op) { $string = true; if (in_array($field, $datearray)) { $av = explode(",", jqGridUtils::parseDate('d/m/Y H:i:s', $v, 'Y,m,d,H,i,s')); $av[1] = (int) $av[1] - 1; $v = "new Date(" . implode(",", $av) . ")"; $string = false; } if (in_array($field, $mongointegers)) { $string = false; } $i++; if ($i > 1) { $s .= $gopr; } switch ($op) { case 'bw': $s .= "this." . $field . ".match(/^{$v}.*\$/i)"; break; case 'bn': $s .= "!this." . $field . ".match(/^{$v}.*\$/i)"; break; case 'ew': $s .= "this." . $field . ".match(/^.*{$v}\$/i)"; break; case 'en': $s .= "!this." . $field . ".match(/^.*{$v}\$/i)"; break; case 'cn': $s .= "this." . $field . ".match(/^.*{$v}.*\$/i)"; break; case 'nc': $s .= "!this." . $field . ".match(/^.*{$v}.*\$/i)"; break; default: if ($string) { $v = "'" . $v . "'"; } $s .= " this." . $field . " " . $sopt[$op] . $v; break; } } } if (isset($mongoquery) && is_array($mongoquery)) { $mongoquery = jqGridUtils::array_extend($mongoquery, array('$where' => "function(){ return " . $s . ";}")); } else { $mongoquery = array('$where' => "function(){ return " . $s . ";}"); } return $mongoquery; }