From 26c888ccd46d22494542b5d3767c80fa3da5af82 Mon Sep 17 00:00:00 2001 From: Nathan Gray Date: Mon, 14 Nov 2011 22:57:06 +0000 Subject: [PATCH] Encode square brackets and split names at ][, not just [ --- etemplate/inc/class.etemplate_widget.inc.php | 19 ++++++++----- etemplate/js/et2_core_arrayMgr.js | 24 +++++++++++++++-- etemplate/js/et2_core_widget.js | 5 ++++ etemplate/js/et2_widget_button.js | 1 + etemplate/js/etemplate2.js | 28 ++++++++++++++++---- 5 files changed, 64 insertions(+), 13 deletions(-) diff --git a/etemplate/inc/class.etemplate_widget.inc.php b/etemplate/inc/class.etemplate_widget.inc.php index 1ad1b2b902..23b0e4d61b 100644 --- a/etemplate/inc/class.etemplate_widget.inc.php +++ b/etemplate/inc/class.etemplate_widget.inc.php @@ -423,14 +423,14 @@ class etemplate_widget { foreach($value as &$val) { - $val = "'".str_replace(array("'",'"'),array('\\\'','"'),$val)."'"; + $val = "'".str_replace(array("'",'"','[',']'),array('\\\'','"','[',']'),$val)."'"; } - $value = '[ '.implode(', ',$value).' ]'; + $value = '[ '.implode(', ',$value).' ]'; $name = str_replace("'".$matches[1]."'",$value,$name); } else { - $value = str_replace(array("'",'"'),array('\\\'','"'),$value); + $value = str_replace(array("'",'"','[',']'),array('\\\'','"','[',']'),$value); $name = str_replace(array('{'.$matches[1].'}',$matches[1]),$value,$name); } } @@ -465,6 +465,7 @@ class etemplate_widget $name = self::get_array($cont,substr($name,1)); } } + $name = str_replace(array('[',']'),array('[',']'),$name); return $name; } @@ -526,7 +527,10 @@ class etemplate_widget */ static function form_name($cname,$name) { - $name_parts = explode('[',str_replace(']','',$name)); + if (count($name_parts = explode('[', $name, 2)) > 1) + { + $name_parts = array_merge(array($name_parts[0]), explode('][', substr($name_parts[1],0,-1))); + } if (!empty($cname)) { array_unshift($name_parts,$cname); @@ -534,7 +538,7 @@ class etemplate_widget $form_name = array_shift($name_parts); if (count($name_parts)) { - $form_name .= '['.implode('][',$name_parts).']'; + $form_name .= '['.implode('][',$name_parts).']'; } return $form_name; } @@ -559,7 +563,10 @@ class etemplate_widget } if (is_object($idx)) return false; // given an error in php5.2 - $idxs = explode('[',str_replace(']','',$idx)); + if (count($idxs = explode('[', $idx, 2)) > 1) + { + $idxs = array_merge(array($idxs[0]), explode('][', substr($idxs[1],0,-1))); + } $pos = &$arr; foreach($idxs as $idx) { diff --git a/etemplate/js/et2_core_arrayMgr.js b/etemplate/js/et2_core_arrayMgr.js index 2072a74a64..c911628ad0 100644 --- a/etemplate/js/et2_core_arrayMgr.js +++ b/etemplate/js/et2_core_arrayMgr.js @@ -43,7 +43,17 @@ var et2_arrayMgr = Class.extend({ if (this.splitIds) { for(var key in _data) { - var indexes = key.replace(/]/g,'').split('['); + var indexes = key.split('['); + if (indexes.length > 1) + { + indexes = [indexes.shift(), indexes.join('[')]; + indexes[1] = indexes[1].substring(0,indexes[1].length-6); + var children = indexes[1].split('[]'); + if(children.length) + { + indexes = jQuery.merge([indexes[0]], children); + } + } if(indexes.length > 1) { var value = _data[key]; @@ -142,7 +152,17 @@ var et2_arrayMgr = Class.extend({ if (this.splitIds) { - indexes = _key.replace(/]/g,'').split('['); + indexes = _key.split('['); + if (indexes.length > 1) + { + indexes = [indexes.shift(), indexes.join('[')]; + indexes[1] = indexes[1].substring(0,indexes[1].length-1); + var children = indexes[1].split(']['); + if(children.length) + { + indexes = jQuery.merge([indexes[0]], children); + } + } } var entry = this.data; diff --git a/etemplate/js/et2_core_widget.js b/etemplate/js/et2_core_widget.js index 259c89b400..7374239248 100644 --- a/etemplate/js/et2_core_widget.js +++ b/etemplate/js/et2_core_widget.js @@ -202,6 +202,11 @@ var et2_widget = Class.extend({ } } + if(this.id) + { + this.id = this.id.replace(/\[/g,'[').replace(/]/g,']'); + } + // Add all attributes hidden in the content arrays to the attributes // parameter this.transformAttributes(_attrs); diff --git a/etemplate/js/et2_widget_button.js b/etemplate/js/et2_widget_button.js index 3616c38482..e90b9d665f 100644 --- a/etemplate/js/et2_widget_button.js +++ b/etemplate/js/et2_widget_button.js @@ -195,6 +195,7 @@ var et2_button = et2_baseWidget.extend([et2_IInput, et2_IDetachedDOM], { this.options.onclick = _values["onclick"]; } this.btn.bind("click.et2_baseWidget", this, function(e) { + e.data.set_id(_values["id"]); return e.data.click.call(e.data,e); }); diff --git a/etemplate/js/etemplate2.js b/etemplate/js/etemplate2.js index 07b93e7a96..a7f16d8aac 100644 --- a/etemplate/js/etemplate2.js +++ b/etemplate/js/etemplate2.js @@ -211,6 +211,8 @@ etemplate2.prototype.submit = function(button) if (canSubmit) { // Button parameter used for submit buttons in datagrid + // TODO: This should probably go in nextmatch's getValues(), along with selected rows somehow. + // I'm just not sure how. if(button) { values.button = button.id @@ -223,7 +225,17 @@ etemplate2.prototype.submit = function(button) } if(target != values) { - var indexes = button.id.replace(/]/g,'').split('['); + var indexes = button.id.split('['); + if (indexes.length > 1) + { + indexes = [indexes.shift(), indexes.join('[')]; + indexes[1] = indexes[1].substring(0,indexes[1].length-1); + var children = indexes[1].split(']['); + if(children.length) + { + indexes = jQuery.merge([indexes[0]], children); + } + } var idx = ''; for(var i = 0; i < indexes.length; i++) { @@ -270,11 +282,17 @@ etemplate2.prototype.getValues = function(_root) // check if id contains a hierachical name, eg. "button[save]" var id = _widget.id; - if (_widget.id.indexOf('[') != -1) + var indexes = _widget.id.split('[',2); + if (indexes.length > 1) { - var parts = _widget.id.replace(/]/g,'').split('['); - id = parts.pop(); - path = path.concat(parts); + indexes = [indexes.shift(), indexes.join('[')]; + indexes[1] = indexes[1].substring(0,indexes[1].length-6); + var children = indexes[1].split('[]'); + if(children.length) + { + indexes = jQuery.merge([indexes[0]], children); + } + path = path.concat(indexes); } // Set the _target variable to that node