From 9924f254d04c50daa47ffed13a80f86b41b05ec0 Mon Sep 17 00:00:00 2001 From: Hector Cortez Date: Wed, 5 Dec 2012 15:19:49 -0400 Subject: [PATCH] BUG 10054 Feature request: grid to support a suggest field SOLVED - Add the possibility to insert Suggest fields in a grid. - New Feature, was added Suggest option to the grid. --- gulliver/js/grid/core/grid.js | 37 ++- gulliver/js/maborak/core/maborak.js | 17 +- gulliver/system/class.publisher.php | 2 + gulliver/system/class.xmlform.php | 274 +++++++++++++++--- .../methods/dynaforms/dynaforms_Preview.php | 13 +- .../xmlform/dynaforms/fields_ToolbarGrid.xml | 3 + 6 files changed, 290 insertions(+), 56 deletions(-) diff --git a/gulliver/js/grid/core/grid.js b/gulliver/js/grid/core/grid.js index 5748a9b55..017996771 100755 --- a/gulliver/js/grid/core/grid.js +++ b/gulliver/js/grid/core/grid.js @@ -340,6 +340,19 @@ var G_Grid = function(oForm, sGridName){ return newElem; }; + function replaceHtml(el, html) { + var oldEl = typeof el === "string" ? document.getElementById(el) : el; + /*Pure innerHTML is slightly faster in IE + oldEl.innerHTML = html; + return oldEl; + */ + var newEl = oldEl.cloneNode(false); + newEl.innerHTML = html; + oldEl.parentNode.replaceChild(newEl, oldEl); + /* Since we just removed the old element from the DOM, return a reference + to the new element, which can be used to restore variable references. */ + return newEl; + }; this.addGridRow = function() { this.oGrid = document.getElementById(this.sGridName); @@ -361,7 +374,7 @@ var G_Grid = function(oForm, sGridName){ // Clone Cells Loop for (i = 0; i < aCells.length; i++) { - oNewRow.appendChild(aCells[i].cloneNode(true)); //Clone First Cell exactly. + oNewRow.appendChild(aCells[i].cloneNode(true)); //Clone First Cell exactly. switch (i){ case 0: oNewRow.getElementsByTagName('td')[i].innerHTML = currentRow; @@ -445,6 +458,14 @@ var G_Grid = function(oForm, sGridName){ } } } + aObjectsScript = oNewRow.getElementsByTagName('td')[i].getElementsByTagName('script'); + sObjectType = this.aFields[i-1].sType; + if (aObjectsScript[0] != 'undefined' && sObjectType == 'suggest') { + sObjScript = aObjectsScript[0].innerHTML; + sNewObjScript = sObjScript.replace(/\[1\]/g, '\[' + currentRow + '\]'); + replaceHtml(aObjectsScript[0], sNewObjScript); + eval(aObjectsScript[0].innerHTML); + } break; case 'checkbox': //CHECKBOX var attributeCheckBox = elementAttributesNS(aObjects[n], ""); @@ -456,8 +477,12 @@ var G_Grid = function(oForm, sGridName){ } break; case 'hidden': //HIDDEN - if ((attributes.gridtype != 'yesno' && attributes.gridtype != 'dropdown') || typeof attributes.gridtype == 'undefined') - aObjects[n].value = defaultValue; + if ((attributes.gridtype != 'yesno' && attributes.gridtype != 'dropdown') || typeof attributes.gridtype == 'undefined') { + aObjects[n].value = defaultValue; + newID = aObjects[n].id.replace(/\[1\]/g, '\[' + currentRow + '\]'); + aObjects[n].id = newID; + aObjects[n].name = newID; + } break; case 'button': if (aObjects[n].onclick){ @@ -685,6 +710,9 @@ var G_Grid = function(oForm, sGridName){ //Set focus the first element in the grid for (var i = 0; i < this.aFields.length; i++) { var fieldName = 'form[' + sGridName + '][' + currentRow + '][' + this.aFields[i].sFieldName + ']'; + if (this.aFields[i].sType == 'suggest' ) { + fieldName = 'form[' + sGridName + '][' + currentRow + '][' + this.aFields[i].sFieldName + '_label]'; + } if (this.aFields[i].sType != 'file' && this.aFields[i].sType != 'hidden' && document.getElementById(fieldName).focus) { document.getElementById(fieldName).focus(); break; @@ -770,6 +798,9 @@ var G_Grid = function(oForm, sGridName){ } aObjects1[0].value = aObjects2[0].value; aObjects1[0].className = aObjects2[0].className; + if ( typeof(aObjects1[1]) != 'undefined' && typeof(aObjects2[1]) != 'undefined' ) { + aObjects1[1].value = aObjects2[1].value; + } break; } } diff --git a/gulliver/js/maborak/core/maborak.js b/gulliver/js/maborak/core/maborak.js index 9881ab432..f27734ca3 100644 --- a/gulliver/js/maborak/core/maborak.js +++ b/gulliver/js/maborak/core/maborak.js @@ -1169,7 +1169,7 @@ if(contracted)expandSubtitle(subTitleName);else contractSubtitle(subTitleName);} function concat_collection(obj1,obj2){var i;var arr=new Array();var len1=obj1.length;var len2=obj2.length;for(i=0;i0){exist=false;for(m=0;m0){if(aObjects[0]){aObjects[0].id=aObjects[0].id.replace('/\['+(iRowAux-1)+'\]/g','\['+iRowAux+'\]');aObjects[0].name=aObjects[0].id.replace('/\['+(iRowAux-1)+'\]/g','\['+iRowAux+'\]');if(aObjects[0].onclick){sAux=new String(aObjects[0].onclick);eval('aObjects[0].onclick = '+sAux.replace('/\['+(iRowAux-1)+'\]/g','\['+iRowAux+'\]')+';');}} aObjects=oCell1.getElementsByTagName('a');if(aObjects){if(aObjects[0]){if(aObjects[0].onclick){sAux=new String(aObjects[0].onclick);eval('aObjects[0].onclick = '+sAux.replace('/\['+(iRowAux-1)+'\]/g','\['+iRowAux+'\]')+';');}}}} break;case'setValues( $Part['Data'] ); $G_FORM->setValues( array ('G_FORM_ID' => $G_FORM->id ) ); +//G::LoadSystem('CVarDumper'); +//CVarDumper::dump($Part,10, true); //Asegurese de que no entre cuando $Part['Template']=="grid" //de hecho soo deberia usarse cuando $Part['Template']=="xmlform" diff --git a/gulliver/system/class.xmlform.php b/gulliver/system/class.xmlform.php index 276ad9bed..348df288e 100755 --- a/gulliver/system/class.xmlform.php +++ b/gulliver/system/class.xmlform.php @@ -1462,6 +1462,207 @@ class XmlForm_Field_Suggest extends XmlForm_Field_SimpleText //by neyek } } + /** + * render Field Grid + * + * @param type $value + * @param type $owner + * @param type $rowId + * @param type $ownerName + * @param type $index + * @return string + */ + public function renderFieldGrid($value = null, $owner = null, $rowId = '', $ownerName = '', $index = 0) + { + + if (! $this->sqlConnection) { + $this->sqlConnection = 'workflow'; + } + + if ($this->strTo === 'UPPER') { + $value = strtoupper( $value ); + } + if ($this->strTo === 'LOWER') { + $value = strtolower( $value ); + } + $onkeypress = G::replaceDataField( $this->onkeypress, $owner->values ); + + if ($this->replaceTags == 1) { + $value = G::replaceDataField( $value, $owner->values ); + } + + $storeEntry = null; + $storeEntryData = ", storeEntryData: [0]"; + if ($this->store_new_entry) { + $storeEntry = ' title="' . G::LoadTranslation( "ID_FIELD_DYNAFORM_SUGGEST_INPUT_TITLE" ) . '"'; + $storeEntryData = ", storeEntryData: [1, \"form" . $rowId . "[" . $this->name . "_label]\", \"" . $this->sqlConnection . "\", \"" . $this->table . "\", \"" . $this->primary_key . "\", \"" . $this->primary_key_type . "\", \"" . $this->field . "\"]"; + } + + $formVariableValue = ''; + $formVariableKeyValue = ''; + G::LoadClass( 'case' ); + $oApp = new Cases(); + if (isset( $_SESSION['APPLICATION'] ) && ($_SESSION['APPLICATION'] != null && $oApp->loadCase( $_SESSION['APPLICATION'] ) != null)) { + $aFields = $oApp->loadCase( $_SESSION['APPLICATION'] ); + if (isset( $aFields['APP_DATA'][$ownerName][$index][$this->name . '_label'] )) { + + $formVariableValue = $aFields['APP_DATA'][$ownerName][$index][$this->name . '_label']; + $formVariableKeyValue = $aFields['APP_DATA'][$ownerName][$index][$this->name]; + } + } + + if ($this->mode === 'edit') { + if ($this->readOnly) { + return 'htmlentities( $value, ENT_COMPAT, 'utf-8' ) . '\' readOnly="readOnly" style="' . htmlentities( $this->style, ENT_COMPAT, 'utf-8' ) . '" onkeypress="' . htmlentities( $onkeypress, ENT_COMPAT, 'utf-8' ) . '"/>'; + } else { + if (strlen( trim( $formVariableValue ) ) > 0) { + $value = $formVariableValue; + } + $name = "'" . $this->name . "'"; + $str = 'NSDependentFields( true ) . ' '; + $str .= '/>'; + $str .= 'name . ']" '; + $str .= 'name="form' . $rowId . '[' . $this->name . ']" '; + $str .= 'value="' . $this->htmlentities( $formVariableKeyValue, ENT_COMPAT, 'utf-8' ) . '" '; + $str .= 'type="hidden" />'; + + $str .= $this->renderHint(); + if (trim( $this->callback ) != '') { + $sCallBack = 'try{' . $this->callback . '}catch(e){alert("Suggest Widget call back error: "+e)}'; + } else { + $sCallBack = ''; + } + + $hash = str_rot13( base64_encode( $this->sql . '@|' . $this->sqlConnection ) ); + $sSQL = $this->sql; + $nCount = preg_match_all( '/\@(?:([\@\%\#\!Qq])([a-zA-Z\_]\w*)|([a-zA-Z\_][\w\-\>\:]*)\(((?:[^\\\\\)]*?)*)\))/', $sSQL, $match, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE ); + + $sResult = array (); + if ($nCount) { + for ($i = 0; $i < $nCount; $i ++) { + if (isset( $match[0][$i][0] ) && isset( $match[2][$i][0] )) { + $aResult[$match[0][$i][0]] = $match[2][$i][0]; + } + } + } + + $depValues = ''; + $i = 1; + if (isset( $aResult ) && $aResult) { + $sResult = '"' . implode( '","', $aResult ) . '"'; + $aResultKeys = array_keys( $aResult ); + $sResultKeys = str_rot13( base64_encode( implode( '|', $aResultKeys ) ) ); + + foreach ($aResult as $key => $field) { + $depValues .= 'getField("' . $field . '").value'; + if ($i ++ < count( $aResult )) { + $depValues .= '+"|"+'; + } + + } + $depValues = '+' . $depValues . '+'; + } else { + $sResult = ''; + $sResultKeys = ''; + $depValues = '+'; + } + + $aDepFields = array (); + $count = 0; + if ($this->dependentFields !== '') { + $dependentFields = explode( ",", $this->dependentFields ); + foreach ($dependentFields as $keyDependent => $valueDependent) { + $sqlDepField = $owner->fields[$valueDependent]->sql; + $count = preg_match_all( '/\@(?:([\@\%\#\=\!Qq])([a-zA-Z\_]\w*)|([a-zA-Z\_][\w\-\>\:]*)\(((?:[^\\\\\)]*?)*)\))/', $sqlDepField, $match, PREG_PATTERN_ORDER | PREG_OFFSET_CAPTURE ); + for ($cnt = 0; $cnt < $count; $cnt ++) { + $aDepFields[$cnt] = $match[2][$cnt][0]; + } + } + } + + $sOptions = 'script: function (input) { '; + + $rowIdField = substr($rowId, 1); + + $sOptions .= ' var inputValue = base64_encode(getField("' . $rowIdField . '[' . $this->name . '_label").value); '; + + $sOptions .= ' return "' . $this->ajaxServer . '?request=suggest&json=true&limit=' . $this->maxresults; + $sOptions .= '&hash=' . $hash . '&dependentFieldsKeys=' . $sResultKeys . '&dependentFieldsValue="'; + $sOptions .= $depValues . '"&input="+inputValue+"&inputEnconde64=enable"; '; + $sOptions .= '},'; + $sOptions .= 'json: true,'; + $sOptions .= 'limit: ' . $this->maxresults . ','; + + $sOptions .= 'shownoresults: ' . ($this->shownoresults ? 'true' : 'false') . ','; + $sOptions .= 'maxresults: ' . $this->maxresults . ','; + $sOptions .= 'chache: true,'; + + $sOptions .= 'callback: function (obj) { '; + + $sOptions .= 'if (typeof obj != "undefined") { '; + $sOptions .= ' var jField = { };'; + $sOptions .= ' var sField = "[]"; '; + + if ($count > 0) { + for ($cnt = 0; $cnt < $count; $cnt ++) { + $sOptions .= 'if ( "' . $this->name . '" == "' . $aDepFields[$cnt] . '" ) {'; + $sOptions .= ' jField["' . $aDepFields[$cnt] . '"] = obj.id;'; + $sOptions .= '} else { '; + $sOptions .= ' jField["' . $aDepFields[$cnt] . '\'] = getField("' . $aDepFields[$cnt] . '").value; '; + $sOptions .= '}'; + } + } + + $sOptions .= ' var sField = "["+ encodeURIComponent(jField.toJSONString()) + "]"; '; + $sOptions .= $sCallBack . '; getField("' . $rowIdField . '[' . $this->name . '").value = obj.id;'; + + $sOwnerId = (isset($owner->owner->id))? $owner->owner->id : $owner->id; + $sOptions .= 'var response = ajax_function("../gulliver/defaultAjaxDynaform", "reloadField", '; + $sOptions .= ' "form=' . $sOwnerId . '&fields=" + sField, "POST"); '; + + $sOptions .= 'if (response.substr(0,1) === "[") { '; + $sOptions .= ' var newcont; '; + $sOptions .= ' eval("newcont=" + response + ";"); '; + $sOptions .= ' for(var i = 0; iname . '_label]", {' . $sOptions . $storeEntryData . '}); '; + $str .= ''; + + return $str; + } + } else { + return $this->htmlentities( $formVariableValue, ENT_COMPAT, 'utf-8' ); + } + } + /** * Function renderGrid * @@ -1473,27 +1674,14 @@ class XmlForm_Field_Suggest extends XmlForm_Field_SimpleText //by neyek */ public function renderGrid ($values = array(), $owner = null) { - $result = array (); + $aResult = array(); $r = 1; - foreach ($values as $v) { - if ($this->replaceTags == 1) { - $v = G::replaceDataField( $v, $owner->values ); - } - if ($this->mode === 'edit') { - if ($this->readOnly) { - $result[] = ''; - } else { - $result[] = ''; - } - } elseif ($this->mode === 'view') { - $result[] = $this->htmlentities( $v, ENT_COMPAT, 'utf-8' ); - } else { - $result[] = $this->htmlentities( $v, ENT_COMPAT, 'utf-8' ); - } - $r ++; - } - return $result; + foreach ($values as $v) { + $aResult[] = $this->renderFieldGrid( $v, $owner, '[' . $owner->name . '][' . $r . ']', $owner->name, $r ); + $r++; + } + return $aResult; } /** @@ -4164,8 +4352,8 @@ class XmlForm_Field_Date extends XmlForm_Field_SimpleText $mask = '%Y-%m-%d'; //set default } - if ($this->defaultValue != "") { - $defaultValue = masktophp( $mask, $defaultValue); + if ($this->defaultValue != "") { + $defaultValue = masktophp( $mask, $defaultValue); } if (strpos( $mask, '%' ) === false) { @@ -5461,38 +5649,38 @@ function masktophp ($mask, $value) if (preg_match('/S/',$tmp)) { $tmp = str_replace("S", "s", $tmp); } - if (preg_match('/o/',$tmp)) { - $tmp = str_replace("o", "n", $tmp); + if (preg_match('/o/',$tmp)) { + $tmp = str_replace("o", "n", $tmp); } - if (preg_match('/a/',$tmp)) { - $tmp = str_replace("a", "D", $tmp); + if (preg_match('/a/',$tmp)) { + $tmp = str_replace("a", "D", $tmp); } - if (preg_match('/l/',$tmp)) { - $tmp = str_replace("l", "g", $tmp); + if (preg_match('/l/',$tmp)) { + $tmp = str_replace("l", "g", $tmp); } - if (preg_match('/A/',$tmp)) { - $tmp = str_replace("A", "l", $tmp); + if (preg_match('/A/',$tmp)) { + $tmp = str_replace("A", "l", $tmp); } - if (preg_match('/I/',$tmp)) { - $tmp = str_replace("I", "h", $tmp); + if (preg_match('/I/',$tmp)) { + $tmp = str_replace("I", "h", $tmp); } - if (preg_match('/j/',$tmp)) { - $tmp = str_replace("j", "z", $tmp); + if (preg_match('/j/',$tmp)) { + $tmp = str_replace("j", "z", $tmp); } - if (preg_match('/k/',$tmp)) { - $tmp = str_replace("k", "G", $tmp); + if (preg_match('/k/',$tmp)) { + $tmp = str_replace("k", "G", $tmp); } - if (preg_match('/e/',$tmp)) { - $tmp = str_replace("e", "j", $tmp); + if (preg_match('/e/',$tmp)) { + $tmp = str_replace("e", "j", $tmp); } - if (preg_match('/u/',$tmp)) { - $tmp = str_replace("u", "N", $tmp); + if (preg_match('/u/',$tmp)) { + $tmp = str_replace("u", "N", $tmp); } - if (preg_match('/p/',$tmp)) { - $tmp = str_replace("p", "A", $tmp); + if (preg_match('/p/',$tmp)) { + $tmp = str_replace("p", "A", $tmp); } - if (preg_match('/P/',$tmp)) { - $tmp = str_replace("P", "a", $tmp); + if (preg_match('/P/',$tmp)) { + $tmp = str_replace("P", "a", $tmp); } if ($value == 'today') { diff --git a/workflow/engine/methods/dynaforms/dynaforms_Preview.php b/workflow/engine/methods/dynaforms/dynaforms_Preview.php index ccfbf2c22..bb3b8c477 100755 --- a/workflow/engine/methods/dynaforms/dynaforms_Preview.php +++ b/workflow/engine/methods/dynaforms/dynaforms_Preview.php @@ -47,9 +47,16 @@ switch (basename( $form->template, '.html' )) { default: $template = 'xmlform'; } -$G_PUBLISH->AddContent( 'dynaform', $template, $file, '', array ('__DYNAFORM_OPTIONS' => array ('PREVIOUS_STEP' => '#','NEXT_STEP' => '#','PREVIOUS_ACTION' => 'return false;','NEXT_ACTION' => 'return false;' -) -), '' ); +$G_PUBLISH->AddContent( + 'dynaform', + $template, + $file, + '', + array ('__DYNAFORM_OPTIONS' => + array ('PREVIOUS_STEP' => '#','NEXT_STEP' => '#','PREVIOUS_ACTION' => 'return false;','NEXT_ACTION' => 'return false;') + ), + '' +); G::RenderPage( 'publish', 'raw' ); /* $toolbar = new ToolBar( '/dynaforms/dynaforms_Toolbar' , PATH_XMLFORM, SYS_LANG, false ); diff --git a/workflow/engine/xmlform/dynaforms/fields_ToolbarGrid.xml b/workflow/engine/xmlform/dynaforms/fields_ToolbarGrid.xml index e3048ab0e..3aa96a282 100755 --- a/workflow/engine/xmlform/dynaforms/fields_ToolbarGrid.xml +++ b/workflow/engine/xmlform/dynaforms/fields_ToolbarGrid.xml @@ -23,6 +23,9 @@ + + Suggest +