Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace lib\Cs;
- class jqGrid
- {
- private $_id = 0;
- private $_where_rules = array(
- 'eq'=>" = ",
- 'ne'=>" <> ",
- 'lt'=>" < ",
- 'le'=>" <= ",
- 'gt'=>" > ",
- 'ge'=>" >= ",
- 'bw'=>" LIKE ",
- 'bn'=>" NOT LIKE ",
- 'in'=>" IN ",
- 'ni'=>" NOT IN ",
- 'ew'=>" LIKE ",
- 'en'=>" NOT LIKE ",
- 'cn'=>" LIKE ",
- 'nc'=>" NOT LIKE ",
- 'nu'=>" IS NULL",
- 'un'=>" IS NOT NULL",
- );
- private $_options = array(
- 'caption' => null,
- 'datatype' => 'local',
- 'rowNum' => 50,
- 'width' => 900,
- 'height' => 300,
- 'rowList' => array(10,20,30,50),
- 'viewrecords' => true,
- 'scrollrows' => true,
- 'url' => '',
- 'editurl' => '',
- 'scroll' => 0,
- 'sortable' => true,
- 'multiselect' => true,
- "jsonReader" => array("repeatitems" => false)
- );
- private $_actions = array(
- 'view' => false,
- 'edit' => false,
- 'add' => true,
- 'del' => false,
- 'search' => false,
- 'refresh' => false,
- );
- private $_inline_actions = array(
- );
- private $_events = array(
- 'onInsert' => null,
- 'onEdit' => null,
- 'onDelete' => null,
- 'afterInsert' => null,
- 'afterEdit' => null,
- 'afterDelete' => null,
- );
- private $_filters = array();
- private $_columns = array();
- private $_column_names = array();
- private $_default_column_options = array(
- 'editable' => true,
- );
- private $_actions_options = array(
- 'edit' => array(
- 'reloadAfterSubmit' => true,
- 'closeAfterEdit' => true,
- 'closeOnEscape' => true,
- 'afterSubmit' => 'function(response) {return eval(response.responseText)}',
- ),
- 'add' => array(
- 'reloadAfterSubmit' => true,
- 'closeAfterAdd' => true,
- 'closeOnEscape' => true,
- 'afterSubmit' => 'function(response) {return eval(response.responseText)}',
- ),
- 'delete' => array(
- 'reloadAfterSubmit' => true,
- 'closeOnEscape' => true,
- 'afterSubmit' => 'function(response) {return eval(response.responseText)}',
- ),
- 'search' => array(
- 'closeOnEscape' => true,
- 'multipleSearch' => true,
- ),
- 'view' => array(
- 'closeOnEscape' => true,
- ),
- );
- private $_methods = array(
- //'filterToolbar' => array(
- // 'stringResult' => true,
- // 'searchOnEnter' => false,
- // ),
- );
- private $_table = null;
- private $_table_pk = null;
- private $_select = null;
- private $_filter_method = 'where';
- public function __construct($table_name = null, $columns=array(), $options = array(), $actions = array(), $events = array())
- {
- $this->_options['url'] = "http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
- $this->_options['editurl'] = $this->_options['url'];
- $this->setTable($table_name);
- $this->setColumns($columns);
- $this->setOptions($options);
- $this->setActions($actions);
- $this->setEvents($events);
- }
- public function setId($id)
- {
- $this->_id = $id;
- $delim = (strpos($this->_options['url'], '?') === false)?'?':'&';
- $this->_options['url'] = $this->_options['url'] . $delim . '_grid_id=' . $this->_id;
- $this->_options['editurl'] = $this->_options['url'];
- }
- public function setFilterMethodOr()
- {
- $this->_filter_method = 'orWhere';
- }
- public function setTable($table_name)
- {
- $this->_table = $table_name;
- $this->setTablePk("id");
- $this->_select = "SELECT %s FROM $table_name";
- }
- public function setTablePk($pk)
- {
- $this->_table_pk = $pk;
- if($this->_table_pk)
- {
- $this->_options["jsonReader"]["id"] = $this->_table_pk;
- }
- }
- public function setKeyColumn($column)
- {
- $this->_options["jsonReader"]["id"] = $column;
- }
- public function setColumns($columns)
- {
- $this->_columns = array();
- $this->_column_names = array();
- $this->_options["colNames"] = array();
- foreach($columns as $col)
- {
- $this->_options["colNames"][] = $col["title"];
- if (!isset($col["virtual"]) || ($col["virtual"] != true))
- {
- $this->_column_names[] = $col["name"];
- }
- $this->_columns[] = array_merge($this->_default_column_options, $col);
- }
- }
- public function setOptions($options)
- {
- if (empty($options))
- {
- $options = array();
- }
- $this->_options = array_merge($this->_options,$options);
- }
- public function setDefaultColumnOptions($default_column_options)
- {
- if (empty($default_column_options))
- {
- $default_column_options = array();
- }
- $this->_default_column_options = array_merge($this->_default_column_options,$default_column_options);
- $this->setColumns($this->_columns);
- }
- public function setActions($actions)
- {
- if (empty($actions))
- {
- $actions = array();
- }
- $this->_actions = array_merge($this->_actions,$actions);
- }
- public function setInlineActions($actions)
- {
- if (empty($actions))
- {
- $actions = array();
- }
- $this->_inline_actions = array_merge($this->_inline_actions,$actions);
- }
- public function setEvents($events)
- {
- if (empty($events))
- {
- $events = array();
- }
- $this->_events = array_merge($this->_events,$events);
- }
- public function setMethods($methods)
- {
- if (empty($methods))
- {
- $methods = array();
- }
- $this->_methods = array_merge($this->_methods,$methods);
- }
- public function setFilters($filters)
- {
- $this->_filters = $filters;
- $ns_name = \CsContext::getController()->getModule().".".\CsContext::getController()->getAction();
- $ns = isset($_SESSION['jqFilters.'.$ns_name])?$_SESSION['jqFilters.'.$ns_name]:$_SESSION['jqFilters.'.$ns_name]=array();
- if(!isset($ns['grid_filters'][$this->_id]))
- {
- $ns['grid_filters'][$this->_id] = array();
- }
- foreach ($this->_filters as &$filter)
- {
- $ns['grid_filters'][$this->_id][$filter['filterName']] = \CsContext::getRequest()->getParameter($filter['filterName'], isset($ns['grid_filters'][$this->_id][$filter['filterName']])?$ns['grid_filters'][$this->_id][$filter['filterName']]:null);
- }
- foreach ($this->_filters as &$filter)
- {
- if(isset($ns['grid_filters'][$this->_id][$filter['filterName']]))
- {
- $filter['defaultValue'] = $ns['grid_filters'][$this->_id][$filter['filterName']];
- }
- }
- }
- public function getFilterValue($pos)
- {
- return $this->_filters[$pos]['defaultValue'];
- }
- private function executeOper($oper, $data)
- {
- switch($oper)
- {
- case "add":
- /*
- if($this->_events['onInsert'])
- {
- $continue = $this->_table->{$this->_events['onInsert']}($data);
- }
- if($continue === false)
- {
- break;
- }
- */
- unset($data['id']);
- $data['id'] = \CsContext::getDb()->insert($this->_table, $data);
- /*
- if($this->_events['afterInsert'])
- {
- $this->_table->{$this->_events['afterInsert']}($data);
- }
- */
- break;
- case "edit":
- /*
- if($this->_events['onEdit'])
- {
- $continue = $this->_table->{$this->_events['onEdit']}($data);
- }
- if($continue === false)
- {
- break;
- }
- */
- $id = $data['id'];
- unset($data['id']);
- \CsContext::getDb()->update($this->_table, $data, "$this->_table_pk = $id");
- $data['id'] = $id;
- /*
- if($this->_events['afterEdit'])
- {
- $this->_table->{$this->_events['afterEdit']}($data);
- }
- */
- break;
- case "del":
- /*
- if($this->_events['onDelete'])
- {
- $continue = $this->_table->{$this->_events['onDelete']}($data);
- }
- if($continue === false)
- {
- break;
- }
- */
- $id = $data['id'];
- \CsContext::getDb()->delete($this->_table, "$this->_table_pk IN ($id)");
- $data['id'] = $id;
- /*
- if($this->_events['afterDelete'])
- {
- $this->_table->{$this->_events['afterDelete']}($data);
- }
- */
- break;
- }
- }
- private function executeSearch()
- {
- if($searchField = $_REQUEST['searchField'])
- {
- $filters = array(
- 'rules' => array(
- 'field' => $searchField,
- 'op' => $_REQUEST['searchOper'],
- 'data' => $_REQUEST['searchString'],
- ),
- );
- }
- else
- {
- $filters = json_decode(\CsContext::getRequest()->getGetParameter('filters'), true);
- }
- if($where = $this->constructSearchWhere($filters))
- {
- $this->_select .= " ".$this->_filter_method." ".$where;
- }
- }
- private function constructSearchWhere($filters)
- {
- return $this->_getWhereForGroup($filters);
- }
- private function _getWhereForGroup($filters)
- {
- $s = "(";
- if(isset($filters['groups']) && $filters['groups'])
- {
- foreach ($filters['groups'] as $group)
- {
- if($group_where = $this->_getWhereForGroup($group))
- {
- if (strlen($s) > 1)
- {
- if ($filters['groupOp'] === "OR")
- {
- $s .= " OR ";
- }
- else
- {
- $s .= " AND ";
- }
- }
- $s .= $group_where;
- }
- }
- }
- if (isset($filters['rules']) && $filters['rules']) {
- foreach ($filters['rules'] as $rule)
- {
- if(($rule['data'] !==null) && ($rule['data'] !==''))
- {
- if (strlen($s) > 1)
- {
- if ($filters['groupOp'] === "OR")
- {
- $s .= " OR ";
- }
- else {
- $s .= " AND ";
- }
- }
- $s .= $this->_getWhereForRule($rule);
- }
- }
- }
- $s .= ")";
- if ($s == "()") {
- return ""; // ignore groups that don't have rules
- } else {
- return $s;
- }
- }
- private function _getWhereForRule($rule)
- {
- if($rule['op'] == 'in' || $rule['op'] == 'ni')
- {
- $question = " (?)";
- return $rule['field'].$this->_where_rules[$rule['op']].' ('.implode(',', array_map(function($val){return "'$val'";}, is_array($rule['data'])?$rule['data']:explode(',', $rule['data']))).')';
- }
- if($rule['op'] == 'nu' || $rule['op'] == 'un')
- {
- $question = " (?)";
- return $rule['field'].$this->_where_rules[$rule['op']];
- }
- if($rule['op']=='bw' || $rule['op']=='bn')
- {
- $rule['data'] = $rule['data'] . "%";
- }
- elseif ($rule['op']=='ew' || $rule['op']=='en')
- {
- $rule['data'] = "%" . $rule['data'];
- }
- elseif ($rule['op']=='cn' || $rule['op']=='nc')
- {
- $rule['data'] = "%" . $rule['data'] . "%";
- }
- return $rule['field'].$this->_where_rules[$rule['op']]." '". $rule['data']."'";
- }
- public function render($grid_id = "grid")
- {
- if(!$this->_select)
- {
- throw new \Exception('You should define data source for the grid!');
- }
- if(\CsContext::getRequest()->getParameter('_grid_id') == $this->_id)
- {
- if(\CsContext::getRequest()->getPostParameter('oper'))
- {
- $result = array(true);
- $post_params = $_POST;
- unset($post_params['oper']);
- try
- {
- $this->executeOper($_POST['oper'], $post_params);
- }
- catch (\Exception $e)
- {
- $result = array(false, $e->getMessage());
- }
- echo json_encode($result);exit;
- }
- if(\CsContext::getRequest()->getGetParameter('_search')=='true')
- {
- $this->executeSearch();
- }
- //echo($this->_select);exit;
- $this->_select = sprintf($this->_select, implode(", ", $this->_column_names));
- if(\CsContext::getRequest()->getGetParameter('oper')=='excel')
- {
- return $this->_select;
- }
- //echo $this->_select;exit;
- if (\CsContext::getRequest()->getGetParameter('page'))
- {
- $page = \CsContext::getRequest()->getGetParameter('page');
- if(!$page) $page = 1;
- $page_rows = \CsContext::getRequest()->getGetParameter('rows');
- if(!$page_rows) $page_rows = 20;
- $sidx = \CsContext::getRequest()->getGetParameter('sidx');
- $sord = \CsContext::getRequest()->getGetParameter('sord');
- if($sidx)
- {
- $this->_select .= " ORDER BY $sidx $sord";
- }
- $data = array();
- $data['records'] = \CsContext::getDb()->query("SELECT COUNT(*) FROM (".$this->_select.") t")->fetchColumn();
- $limit_start = ($page - 1) * $page_rows;
- $this->_select .= " LIMIT $limit_start, $page_rows";
- $data['page'] = $page;
- $data['total'] = ceil($data['records']/$page_rows);
- //echo $this->_select; exit;
- $data['rows'] = \CsContext::getDb()->query($this->_select)->fetchAll();
- //var_dump($data); exit;
- echo json_encode($data);exit;
- }
- }
- if(!empty($this->_inline_actions))
- {
- //echo 111; exit;
- array_push($this->_options["colNames"], 'Действия');
- array_push($this->_columns, array("name"=>"act", "align"=>"center", "index"=>"act", "width"=>"30", "sortable"=>false, "search"=>false));
- $err_func = 'function(response) {var data = eval(response.responseText); if(!data[0]) $.jgrid.info_dialog($.jgrid.errors.errcap,data[1], $.jgrid.edit.bClose); else {jQuery(event.target).parent().parent().children().toggle();} return data[0];}';
- $this->_options['gridComplete'] .= "function(){
- var ids = jQuery('#$grid_id').jqGrid('getDataIDs');
- for(var i=0;i < ids.length;i++){
- var cl = ids[i];
- be = ' <a title=\"Edit this row\" class=\"ui-icon ui-icon-pencil\" style=\"display: inline-block; padding: 0; margin-right: .1em; text-align: center;\" href=\"javascript:void(0);\" onclick=\"jQuery(\'#$grid_id\').jqGrid(\'editRow\', '+cl+', true, null, $err_func); jQuery(event.target).parent().parent().children().toggle();\">Edit</a>';
- de = ' | <a title=\"Delete this row\" class=\"ui-icon ui-icon-trash\" style=\"display: inline-block; padding: 0; margin-right: .1em; text-align: center;\" href=\"javascript:void(0);\" onclick=\"jQuery(\'#$grid_id\').delGridRow('+cl+');\">Delete</a>';
- se = ' <a title=\"Save this row\" class=\"ui-icon ui-icon-disk\" style=\"display: inline-block; padding: 0; margin-right: .1em; text-align: center;\" href=\"javascript:void(0);\" onclick=\"jQuery(\'#$grid_id\').saveRow('+cl+',$err_func);\">Save</a>';
- ce = ' | <a title=\"Restore this row\" class=\"ui-icon ui-icon-cancel\" style=\"display: inline-block; padding: 0; margin-right: .1em; text-align: center;\" href=\"javascript:void(0);\" onclick=\"jQuery(\'#$grid_id\').restoreRow('+cl+'); jQuery(this).parent().hide(); jQuery(this).parent().prev().show();\">Cancel</a>';
- jQuery('#$grid_id').jqGrid('setRowData',ids[i],{act:'<span id=\"edit_row_'+cl+'\">'+be+de+'</span>'+'<span style=display:none id=\"save_row_'+cl+'\">'+se+ce+'</span>'});
- }
- }";
- $this->_options['ondblClickRow'] = "function(id){
- if(id && id!==lastSel) {
- jQuery('#$grid_id').restoreRow(lastSel);
- jQuery('#edit_row_'+lastSel).parent().children().toggle();
- lastSel=id;
- }
- jQuery('#$grid_id').jqGrid('editRow', id, true, null, function(response) {var data = eval(response.responseText); if(!data[0]) $.jgrid.info_dialog($.jgrid.errors.errcap,data[1], $.jgrid.edit.bClose); else {jQuery('#edit_row_'+id).parent().children().toggle();} return data[0];});
- jQuery('#edit_row_'+id).parent().children().toggle();
- }";
- }
- $this->_options['pager'] = '#'.$grid_id.'_pager';
- $this->_options['colModel'] = $this->_columns;
- $encoded_options = $this->_encodeJson($this->_options);
- ob_start();
- ?>
- <table id="<?php echo $grid_id?>"></table>
- <div id="<?php echo $grid_id."_pager"?>"></div>
- <div id="<?php echo $grid_id."_search"?>"></div>
- <script>
- jQuery.jgrid.extend({
- navFilterAdd:function (elem,p) {
- p = $.extend({
- filterDefaultId: 'myfilter',
- filters: [],
- filterGroupOp: 'AND',
- sField:'searchField',
- sValue:'searchString',
- sOper: 'searchOper',
- sFilter: 'filters'
- }, p ||{});
- return this.each(function() {
- var $t = this;
- if( !$t.grid) { return; }
- if( elem.indexOf("#") !== 0) { elem = "#"+elem; }
- var findnav = $(".navtable",elem)[0];
- if(findnav) {
- var setSdata = function(reset)
- {
- var filters = {}, groups = [], sdata={}, res;
- filters.groupOp = p.filterGroupOp;
- $.each(p.filters, function(i, pFilter){
- var fid = $t.id + "_" + p.filterDefaultId + "_" + pFilter.filterName;
- var fdata;
- if(reset == true) {
- if(pFilter.filterType == 'checkbox') {
- $('#'+fid).attr('checked', false);
- }
- else {
- $('#'+fid).val('');
- }
- }
- if(pFilter.filterType == 'checkbox')
- {
- if($('#'+fid).attr('checked'))
- {
- fdata = $('#'+fid).val();
- }
- else
- {
- fdata = '';
- }
- }
- else
- {
- fdata = $('#'+fid).val();
- }
- sdata[pFilter.filterName] = fdata;
- var fRules = pFilter.filterRules;
- function setData(fRules, fdata)
- {
- if(fRules.groups) {
- fRules.groups = setData(fRules.groups, fdata);
- }
- $.each(fRules.rules, function(i, n){
- n.data = fdata;
- });
- return fRules;
- }
- fRules = setData(fRules, fdata);
- groups.push(fRules);
- });
- filters.groups = groups;
- try {
- // xmlJsonClass or JSON.stringify
- res = xmlJsonClass.toJson(filters, '', '', false);
- } catch (e) {
- try {
- res = JSON.stringify(filters);
- } catch (e2) { }
- }
- if(typeof(res)==="string") {
- sdata[p.sFilter] = res;
- $.each([p.sField,p.sValue, p.sOper], function() { sdata[this] = "";});
- }
- $t.p.search = true;
- $.extend($t.p.postData,sdata);
- }
- var sendFilterData = function()
- {
- setSdata();
- $($t).trigger("reloadGrid",[{page:1}]);
- return false;
- }
- function clearFilterData() {
- setSdata(true);
- $($t).trigger("reloadGrid",[{page:1}]);
- return false;
- }
- $.each(p.filters, function(i, pFilter){
- var tbd = $("<td></td>");
- $(tbd).addClass('ui-search-toolbar ui-th-ltr').css('padding', '0px 2px').append(pFilter.filterText);
- var fid = $t.id + "_" + p.filterDefaultId + "_" + pFilter.filterName;
- if(pFilter.filterType == 'text') {
- var filter = $("<input type='text' id='" + fid + "' />");
- if(pFilter.defaultValue)
- {
- filter.val(pFilter.defaultValue);
- }
- filter.keypress(function(e){
- if(e.keyCode ==13 ) {
- return sendFilterData();
- }
- });
- }
- if(pFilter.filterType == 'datepicker') {
- var filter = $("<input type='text' id='" + fid + "' />");
- if(pFilter.defaultValue)
- {
- filter.val(pFilter.defaultValue);
- }
- $(document).ready(function(){$(filter).datepicker({ dateFormat: 'yy-mm-dd' });});
- filter.change(function(e){
- return sendFilterData()
- });
- filter.keypress(function(e){
- if(e.keyCode ==13 ) {
- return sendFilterData();
- }
- });
- }
- else if(pFilter.filterType == 'select') {
- if(pFilter.filterNochoice) {
- var nochoice = pFilter.filterNochoice;
- }
- else {
- var nochoice = '';
- }
- var filter = $("<select id='" + fid + "'></select>").append('<option value="">'+nochoice+'</option>');
- $.each(pFilter.filterOptions, function(i,n){filter.append('<option value="'+i+'">'+n+'</option>')});
- if(pFilter.defaultValue)
- {
- $("option[value='" + pFilter.defaultValue + "']", filter).attr('selected', true);
- }
- filter.change(function(e){
- return sendFilterData()
- });
- }
- else if(pFilter.filterType == 'multiselect') {
- if(pFilter.filterNochoice) {
- var nochoice = pFilter.filterNochoice;
- }
- else {
- var nochoice = '';
- }
- var filter = $("<select id='" + fid + "' name='" + fid + "' multiple='multiple'></select>");
- $.each(pFilter.filterOptions, function(i,n){filter.append('<option value="'+i+'">'+n+'</option>')});
- if(pFilter.defaultValue)
- {
- $.each(pFilter.defaultValue, function(i,n){$("option[value='" + n + "']", filter).attr('selected', true)});
- }
- $(document).ready(function(){$(filter).multiselect({
- position:{
- my: 'top',
- at: 'top'
- },
- minWidth: '210',
- selectedList: '3',
- header: pFilter.filterText,
- noneSelectedText: 'Клик для выбора',
- selectedText: 'Выбрано: #'
- }).multiselectfilter({
- label: '',
- width: 60,
- placeholder: 'Поиск'
- });
- });
- filter.change(function(e){
- return sendFilterData()
- });
- }
- else if(pFilter.filterType == 'checkbox') {
- var filter = $("<input type='checkbox' id='" + fid + "' value='" + pFilter.filterOptions + "' />");
- if(pFilter.defaultValue !== "")
- {
- filter.attr('checked', true);
- }
- filter.change(function(e){
- return sendFilterData()
- });
- }
- $(tbd).append($(filter));
- $("tr",findnav).append(tbd);
- });
- $("tr",findnav).append(
- $("<td id='clear_filter' title='Очистить все фильтры'></td>")
- .addClass('ui-pg-button ui-corner-all')
- .css('padding-left', '2px')
- .append(
- $("<div></div>")
- .addClass('ui-pg-div')
- .append(
- $("<span></span>")
- .addClass('ui-icon ui-icon-close')
- )
- )
- .click(
- function(){return clearFilterData()}
- )
- );
- }
- setSdata();
- });
- }
- });
- jQuery(document).ready(function(){
- <?php echo $this->render_js($grid_id,$encoded_options);?>
- });
- </script>
- <?php
- return ob_get_clean();
- }
- public function render_js($grid_id,$encoded_options)
- {
- $encoded_actions = $this->_encodeJson($this->_actions);
- $encoded_edit_options = $this->_encodeJson($this->_actions_options['edit']);
- $encoded_add_options = $this->_encodeJson($this->_actions_options['add']);
- $encoded_delete_options = $this->_encodeJson($this->_actions_options['delete']);
- $encoded_search_options = $this->_encodeJson($this->_actions_options['search']);
- $encoded_view_options = $this->_encodeJson($this->_actions_options['view']);
- ?>
- var lastSel;
- var grid_<?php echo $grid_id?> = jQuery("#<?php echo $grid_id?>").jqGrid(<?php echo $encoded_options?>);
- jQuery("#<?php echo $grid_id?>").jqGrid('navGrid','#<?php echo $grid_id."_pager"?>',
- <?php echo $encoded_actions?>,
- <?php echo $encoded_edit_options?>,
- <?php echo $encoded_add_options?>,
- <?php echo $encoded_delete_options?>,
- <?php echo $encoded_search_options?>,
- <?php echo $encoded_view_options?>
- );
- <?php foreach ($this->_methods as $method=>$method_options):?>
- jQuery("#<?php echo $grid_id?>").jqGrid('<?php echo $method; ?>',"#<?php echo $grid_id."_pager"?>", <?php echo $this->_encodeJson($method_options); ?>);
- <?php endforeach;?>
- <?php if(!empty($this->_filters)): ?>
- jQuery("#<?php echo $grid_id?>").jqGrid('navFilterAdd', "#<?php echo isset($this->_options['filterContainer'])?$this->_options['filterContainer']:$grid_id."_pager"?>", {filters: <?php echo $this->_encodeJson($this->_filters); ?>});
- <?php endif; ?>
- <?php if($this->_options['height'] === null): ?>
- jQuery("#<?php echo $grid_id?>").setGridHeight(jQuery(document).height()-350);
- <?php endif; ?>
- grid_<?php echo $grid_id?>.setGridParam({datatype: 'json'});
- grid_<?php echo $grid_id?>.trigger('reloadGrid');
- <?php
- }
- /**
- * Кодирует в json. Если на нулевом уровне вложенности в значениях встречаеться функция - НЕ делает из нее строку.
- */
- private function _encodeJson($data)
- {
- $value_arr = array();
- $replace_keys = array();
- $this->_replaceFunctionsForJson($data, $replace_keys, $value_arr);
- $json = json_encode($data);
- $json = str_replace($replace_keys, $value_arr, $json);
- return $json;
- }
- private function _replaceFunctionsForJson(&$data, &$replace_keys, &$value_arr, &$i = 0)
- {
- foreach($data as $key => &$value)
- {
- if(is_array($value))
- {
- $this->_replaceFunctionsForJson($value, $replace_keys, $value_arr, $i);
- }
- elseif(strpos($value, 'function(')===0)
- {
- $i++;
- $value_arr[] = $value;
- $value = '%%' . $i . '%%';
- $replace_keys[] = '"' . $value . '"';
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement