2012-11-26 18:03:59 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* defaultAjax.php
|
|
|
|
|
*
|
|
|
|
|
* ProcessMaker Open Source Edition
|
|
|
|
|
* Copyright (C) 2004 - 2008 Colosa Inc.23
|
|
|
|
|
*
|
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*
|
|
|
|
|
* For more information, contact Colosa Inc, 2566 Le Jeune Rd.,
|
|
|
|
|
* Coral Gables, FL, 33134, USA, or email info@colosa.com.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*NEXT LINE: Runs any configuration defined to be executed before dependent fields recalc*/
|
2017-08-10 15:45:07 -04:00
|
|
|
|
|
|
|
|
use ProcessMaker\Plugins\PluginRegistry;
|
|
|
|
|
|
2012-11-26 18:03:59 -04:00
|
|
|
if (isset( $_SESSION['CURRENT_PAGE_INITILIZATION'] ))
|
|
|
|
|
eval( $_SESSION['CURRENT_PAGE_INITILIZATION'] );
|
2012-12-11 18:45:51 -04:00
|
|
|
|
2017-08-02 16:06:56 -04:00
|
|
|
|
2012-12-11 18:45:51 -04:00
|
|
|
// $json=new Services_JSON();
|
2012-11-26 18:03:59 -04:00
|
|
|
if (! defined( 'XMLFORM_AJAX_PATH' ))
|
|
|
|
|
define( 'XMLFORM_AJAX_PATH', PATH_XMLFORM );
|
|
|
|
|
|
|
|
|
|
$_DBArray = array ();
|
|
|
|
|
if (isset( $_SESSION['_DBArray'] )) {
|
|
|
|
|
$_DBArray = $_SESSION['_DBArray'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$xmlFile = G::getUIDName( urlDecode( $_POST['form'] ) );
|
|
|
|
|
$sPath = XMLFORM_AJAX_PATH;
|
|
|
|
|
|
2012-12-11 18:45:51 -04:00
|
|
|
//if the xmlform file doesn't exist, then try with the plugins folders
|
2012-11-26 18:03:59 -04:00
|
|
|
if (! is_file( XMLFORM_AJAX_PATH . $xmlFile )) {
|
|
|
|
|
$aux = explode( PATH_SEP, $xmlFile );
|
2012-12-11 18:45:51 -04:00
|
|
|
//check if G_PLUGIN_CLASS is defined, because publisher can be called without an environment
|
2012-11-26 18:03:59 -04:00
|
|
|
if (count( $aux ) == 2 && defined( 'G_PLUGIN_CLASS' )) {
|
2017-08-10 15:45:07 -04:00
|
|
|
$oPluginRegistry = PluginRegistry::loadSingleton();
|
2012-11-26 18:03:59 -04:00
|
|
|
if ($oPluginRegistry->isRegisteredFolder( $aux[0] )) {
|
|
|
|
|
$sPath = PATH_PLUGINS;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-01-18 19:12:32 -04:00
|
|
|
|
2017-08-14 15:53:48 -04:00
|
|
|
$G_FORM = new Form( $xmlFile, $sPath );
|
2012-11-26 18:03:59 -04:00
|
|
|
$G_FORM->id = urlDecode( $_POST['form'] );
|
|
|
|
|
$G_FORM->values = isset( $_SESSION[$G_FORM->id] ) ? $_SESSION[$G_FORM->id] : array ();
|
2013-01-18 19:12:32 -04:00
|
|
|
|
2012-11-26 18:03:59 -04:00
|
|
|
$newValues = (Bootstrap::json_decode( urlDecode( stripslashes( $_POST['fields'] ) ) ));
|
2013-01-18 19:12:32 -04:00
|
|
|
|
2012-11-26 18:03:59 -04:00
|
|
|
if (isset( $_POST['grid'] )) {
|
|
|
|
|
$_POST['row'] = (int) $_POST['row'];
|
|
|
|
|
$aAux = array ();
|
2012-12-11 18:45:51 -04:00
|
|
|
|
2013-01-18 19:12:32 -04:00
|
|
|
foreach ($newValues as $sKey => $newValue) {
|
2012-11-26 18:03:59 -04:00
|
|
|
$newValue = (array) $newValue;
|
|
|
|
|
$aKeys = array_keys( $newValue );
|
2012-12-11 18:45:51 -04:00
|
|
|
if (count($aKeys)>0) {
|
|
|
|
|
$aValues = array ();
|
|
|
|
|
for ($i = 1; $i <= ($_POST['row'] - 1); $i ++) {
|
|
|
|
|
$aValues[$i] = array ($aKeys[0] => '' );
|
|
|
|
|
}
|
|
|
|
|
$aValues[$_POST['row']] = array ($aKeys[0] => $newValue[$aKeys[0]] );
|
|
|
|
|
$newValues[$sKey]->$_POST['grid'] = $aValues;
|
|
|
|
|
unset( $newValues[$sKey]->$aKeys[0] );
|
2012-11-26 18:03:59 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-11 18:45:51 -04:00
|
|
|
//Next Lines re-build newValues array to send multiple dependent fields merged by row into a grid.
|
2012-11-26 18:03:59 -04:00
|
|
|
if (sizeof( $newValues ) > 1 && isset( $_POST['grid'] )) {
|
|
|
|
|
$fieldBase = array ();
|
|
|
|
|
foreach ($newValues as $key => $values) {
|
|
|
|
|
for ($r2 = 1; $r2 <= $_POST['row']; $r2 ++) {
|
|
|
|
|
foreach ($values as $class => $value) {
|
|
|
|
|
if ($class == $_POST['grid']) {
|
|
|
|
|
$value = (array) $value;
|
|
|
|
|
$arrayK = $value[$r2];
|
|
|
|
|
foreach ($arrayK as $key2 => $val) {
|
|
|
|
|
$fieldBase[$r2][$key2] = is_array( $val ) ? $val[$key2] : $val;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$newValues[0]->$_POST['grid'] = $fieldBase;
|
|
|
|
|
}
|
|
|
|
|
|
2012-12-11 18:45:51 -04:00
|
|
|
//Resolve dependencies
|
|
|
|
|
//Returns an array ($dependentFields) with the names of the fields
|
|
|
|
|
//that depends of fields passed through AJAX ($_GET/$_POST)
|
2013-01-18 19:12:32 -04:00
|
|
|
//Returns all dependencies of all fields, this in grids
|
2012-11-26 18:03:59 -04:00
|
|
|
$dependentFields = array ();
|
|
|
|
|
$aux = array ();
|
|
|
|
|
for ($r = 0; $r < sizeof( $newValues ); $r ++) {
|
|
|
|
|
$newValues[$r] = (array) $newValues[$r];
|
|
|
|
|
$G_FORM->setValues( $newValues[$r] );
|
2012-12-11 18:45:51 -04:00
|
|
|
//Search dependent fields
|
2012-11-26 18:03:59 -04:00
|
|
|
foreach ($newValues[$r] as $k => $v) {
|
|
|
|
|
if (! is_array( $v )) {
|
|
|
|
|
$myDependentFields = subDependencies( $k, $G_FORM, $aux );
|
|
|
|
|
$_SESSION[$G_FORM->id][$k] = $v;
|
|
|
|
|
} else {
|
|
|
|
|
foreach ($v[$_POST['row']] as $k1 => $v1) {
|
|
|
|
|
$myDependentFields = subDependencies( $k1, $G_FORM, $aux, $_POST['grid'] );
|
|
|
|
|
$_SESSION[$G_FORM->id][$_POST['grid']][$_POST['row']][$k1] = $v1;
|
2012-12-20 17:03:13 -04:00
|
|
|
$G_FORM->values[$_POST['grid']][$_POST['row']][$k1] = $v1;
|
2012-11-26 18:03:59 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$dependentFields = array_merge( $dependentFields, $myDependentFields );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$dependentFields = array_unique( $dependentFields );
|
|
|
|
|
|
2015-03-23 15:13:24 -04:00
|
|
|
//Update when is depenfield set empty
|
|
|
|
|
$newForm = $G_FORM->values;
|
|
|
|
|
foreach($newForm as $fKey => $values){
|
|
|
|
|
foreach($dependentFields as $att){
|
|
|
|
|
if($att == $fKey){
|
|
|
|
|
$newForm[$fKey] = '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$G_FORM->values = $newForm;
|
|
|
|
|
|
2013-01-18 19:12:32 -04:00
|
|
|
//Delete all dependencies of all fields, we're interested only in the fields sending from AJAX, this in grids
|
2013-01-24 12:44:30 -04:00
|
|
|
$arrayFieldSubDependent = array();
|
|
|
|
|
|
2013-01-18 19:12:32 -04:00
|
|
|
if (isset($_POST["grid"])) {
|
|
|
|
|
$arrayField = (array)(Bootstrap::json_decode(urlDecode(stripslashes($_POST["fields"]))));
|
|
|
|
|
$arrayDependentField = array();
|
|
|
|
|
$ereg = null;
|
|
|
|
|
|
|
|
|
|
foreach ($arrayField as $fieldData) {
|
|
|
|
|
$arrayAux = (array)($fieldData);
|
|
|
|
|
|
|
|
|
|
foreach ($arrayAux as $index => $value) {
|
|
|
|
|
$ereg = $ereg . (($ereg != null)? "|" : null) . $index; //Concatenate field
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($ereg != null) {
|
|
|
|
|
foreach ($dependentFields as $value) {
|
2013-01-24 12:44:30 -04:00
|
|
|
//Direct dependent fields
|
|
|
|
|
if (preg_match("/^(?:$ereg)\|[^\|]*$/", $value)) {
|
2013-01-18 19:12:32 -04:00
|
|
|
$arrayAux = explode("|", $value);
|
|
|
|
|
|
|
|
|
|
$arrayDependentField[] = $arrayAux[1];
|
|
|
|
|
}
|
2013-01-24 12:44:30 -04:00
|
|
|
|
|
|
|
|
//Subdependent fields
|
|
|
|
|
if (preg_match("/^(?:$ereg)\|.*$/", $value)) {
|
|
|
|
|
$arrayAux = explode("|", $value);
|
|
|
|
|
$index = $arrayAux[0];
|
|
|
|
|
|
|
|
|
|
unset($arrayAux[0]);
|
|
|
|
|
|
|
|
|
|
if (isset($arrayFieldSubDependent[$index])) {
|
|
|
|
|
$arrayFieldSubDependent[$index] = array_unique(array_merge($arrayFieldSubDependent[$index], $arrayAux));
|
|
|
|
|
} else {
|
|
|
|
|
$arrayFieldSubDependent[$index] = array_unique($arrayAux);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-01-18 19:12:32 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$dependentFields = array_unique($arrayDependentField);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Completed all fields of the grid
|
|
|
|
|
if (isset($_POST["grid"]) && isset($_POST["gridField"])) {
|
2013-01-24 12:44:30 -04:00
|
|
|
//Completed all fields of the grid
|
2013-01-18 19:12:32 -04:00
|
|
|
$arrayGridField = (array)(Bootstrap::json_decode(urldecode(stripslashes($_POST["gridField"]))));
|
|
|
|
|
|
|
|
|
|
foreach ($arrayGridField as $index => $value) {
|
|
|
|
|
$G_FORM->values[$_POST["grid"]][$_POST["row"]][$index] = $value;
|
|
|
|
|
}
|
2013-01-24 12:44:30 -04:00
|
|
|
|
|
|
|
|
//Delete all fields subdependent
|
|
|
|
|
foreach ($arrayFieldSubDependent as $index1 => $value1) {
|
|
|
|
|
$arrayAux = $value1;
|
|
|
|
|
|
|
|
|
|
foreach ($arrayAux as $value2) {
|
|
|
|
|
unset($G_FORM->values[$_POST["grid"]][$_POST["row"]][$value2]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-01-18 19:12:32 -04:00
|
|
|
}
|
|
|
|
|
|
2012-12-11 18:45:51 -04:00
|
|
|
//Parse and update the new content
|
2013-01-18 19:12:32 -04:00
|
|
|
$newContent = $G_FORM->getFields(PATH_CORE . "templates" . PATH_SEP . "xmlform.html", (isset($_POST["row"])? $_POST["row"] : -1));
|
|
|
|
|
|
2012-12-11 18:45:51 -04:00
|
|
|
//Returns the dependentFields's content
|
2012-11-26 18:03:59 -04:00
|
|
|
$sendContent = array ();
|
|
|
|
|
$r = 0;
|
|
|
|
|
|
2013-01-18 19:12:32 -04:00
|
|
|
//Set data
|
2012-11-26 18:03:59 -04:00
|
|
|
foreach ($dependentFields as $d) {
|
2013-08-12 15:19:16 -04:00
|
|
|
$d = trim($d);
|
2014-10-20 16:24:05 -04:00
|
|
|
$sendContent[$r] = new stdclass();
|
2012-11-26 18:03:59 -04:00
|
|
|
$sendContent[$r]->name = $d;
|
2014-10-20 16:24:05 -04:00
|
|
|
$sendContent[$r]->content = new stdclass();
|
2013-01-18 19:12:32 -04:00
|
|
|
|
2012-11-26 18:03:59 -04:00
|
|
|
if (! isset( $_POST['grid'] )) {
|
|
|
|
|
if (isset( $G_FORM->fields[$d] )) {
|
|
|
|
|
foreach ($G_FORM->fields[$d] as $attribute => $value) {
|
|
|
|
|
switch ($attribute) {
|
|
|
|
|
case 'type':
|
|
|
|
|
$sendContent[$r]->content->{$attribute} = $value;
|
|
|
|
|
break;
|
|
|
|
|
case 'options':
|
|
|
|
|
$sendContent[$r]->content->{$attribute} = toJSArray( $value, $sendContent[$r]->content->type );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$sendContent[$r]->value = isset( $G_FORM->values[$d] ) ? $G_FORM->values[$d] : '';
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
foreach ($G_FORM->fields[$_POST['grid']]->fields[$d] as $attribute => $value) {
|
|
|
|
|
switch ($attribute) {
|
|
|
|
|
case 'type':
|
|
|
|
|
$sendContent[$r]->content->{$attribute} = $value;
|
|
|
|
|
break;
|
|
|
|
|
case 'options':
|
BUG 12738 "SQL doesn't work in GRID - Text-Area..." SOLVED
- SQL doesn't work in GRID - Text-Area fields.
- Problema:
Al crear una dependencia entre un Dropdown y Textarea en un grid, al añrlo a un Dynaform y ejecutar el caso,
la dependiencia no funcionaba, al seleccionar una opcion del Dropdown en el textarea no se mostraba ningun valor,
y al adicionar mas filas igual no funcionaba.
Solucion:
El problema es el siguiente: en la clase "class XmlForm_Field_Textarea" en su metodo "renderGrid" no estaba definido
variables de importancia y seteo de las mismas, se completo las variables faltantes.
Otro de los problemas que ocasionaba esta dificultad estaba en el archivo JavaScript en el metodo "updateDepententFields"
se agrego una validacion cuando es un campo de tipo "textarea", esto porque no se podia utilizar el metodo "G_TextArea"
que tambien le faltaba codigo, colocando este codigo faltante tampoco funcionaba.
Al crear una dependencia entre un Dropdown y Textarea en un grid la añrlo a un Dynaform, y ejecutar el caso,
dependiencia funciona correctamente.
Se agrego el siguiente codigo en la clase "class XmlForm_Field_Textarea" en el metodo "renderGrid":
...
$this->executeSQL($owner, $r);
if (isset($this->sqlOption)) {
$firstElement = key($this->sqlOption);
}
if (isset($firstElement)) {
$v = $firstElement;
}
$arrayOptions[$r] = $v;
...
$this->options = $arrayOptions;
...
Disponible para la version 2.5.2
2013-10-24 17:00:32 -04:00
|
|
|
if ($sendContent[$r]->content->type != "text" && $sendContent[$r]->content->type != "textarea") {
|
2013-01-18 19:12:32 -04:00
|
|
|
$sendContent[$r]->content->{$attribute} = toJSArray($value);
|
2012-11-26 18:03:59 -04:00
|
|
|
} else {
|
2013-01-18 19:12:32 -04:00
|
|
|
$sendContent[$r]->content->{$attribute} = toJSArray((isset($value[$_POST["row"]])? array($value[$_POST["row"]]) : array()));
|
2012-11-26 18:03:59 -04:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$sendContent[$r]->value = isset( $G_FORM->values[$_POST['grid']][$_POST['row']][$d] ) ? $G_FORM->values[$_POST['grid']][$_POST['row']][$d] : '';
|
|
|
|
|
}
|
2013-01-18 19:12:32 -04:00
|
|
|
|
|
|
|
|
$r = $r + 1;
|
2012-11-26 18:03:59 -04:00
|
|
|
}
|
2013-01-18 19:12:32 -04:00
|
|
|
|
|
|
|
|
echo Bootstrap::json_encode($sendContent);
|
2012-11-26 18:03:59 -04:00
|
|
|
|
|
|
|
|
function toJSArray ($array, $type = '')
|
|
|
|
|
{
|
|
|
|
|
$result = array ();
|
|
|
|
|
foreach ($array as $k => $v) {
|
2014-10-20 16:24:05 -04:00
|
|
|
$o = new stdclass();
|
2012-11-26 18:03:59 -04:00
|
|
|
$o->key = $k;
|
2012-12-11 18:45:51 -04:00
|
|
|
// TODO: review the condition to make the differentiation to dependent dropdowns in a grid function.
|
|
|
|
|
// this way of validation is if you have a dependent field in text fields
|
2012-11-26 18:03:59 -04:00
|
|
|
$o->value = ($type == 'text' || $type == 'textarea') ? $k : $v;
|
|
|
|
|
$result[] = $o;
|
|
|
|
|
}
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function subDependencies ($k, &$G_FORM, &$aux, $grid = '')
|
|
|
|
|
{
|
|
|
|
|
if (array_search( $k, $aux ) !== FALSE)
|
|
|
|
|
return array ();
|
|
|
|
|
if ($grid == '') {
|
|
|
|
|
if (! array_key_exists( $k, $G_FORM->fields ))
|
|
|
|
|
return array ();
|
|
|
|
|
if (! isset( $G_FORM->fields[$k]->dependentFields ))
|
|
|
|
|
return array ();
|
|
|
|
|
$aux[] = $k;
|
|
|
|
|
if (strpos( $G_FORM->fields[$k]->dependentFields, ',' ) !== false) {
|
|
|
|
|
$myDependentFields = explode( ',', $G_FORM->fields[$k]->dependentFields );
|
|
|
|
|
} else {
|
|
|
|
|
$myDependentFields = explode( '|', $G_FORM->fields[$k]->dependentFields );
|
|
|
|
|
}
|
|
|
|
|
for ($r = 0; $r < sizeof( $myDependentFields ); $r ++) {
|
|
|
|
|
if ($myDependentFields[$r] == "")
|
|
|
|
|
unset( $myDependentFields[$r] );
|
|
|
|
|
}
|
2013-01-15 11:42:56 -04:00
|
|
|
$mD = $myDependentFields;
|
|
|
|
|
foreach( $mD as $ki) {
|
|
|
|
|
$myDependentFields = array_merge( $myDependentFields , subDependencies( $ki , $G_FORM , $aux ) );
|
|
|
|
|
}
|
2012-11-26 18:03:59 -04:00
|
|
|
} else {
|
2012-12-11 18:45:51 -04:00
|
|
|
if (! isset($G_FORM->fields[$grid])) {
|
|
|
|
|
return array ();
|
|
|
|
|
}
|
2012-11-26 18:03:59 -04:00
|
|
|
if (! array_key_exists( $k, $G_FORM->fields[$grid]->fields ))
|
|
|
|
|
return array ();
|
|
|
|
|
if (! isset( $G_FORM->fields[$grid]->fields[$k]->dependentFields ))
|
|
|
|
|
return array ();
|
2013-01-18 19:12:32 -04:00
|
|
|
|
2012-11-26 18:03:59 -04:00
|
|
|
$aux[] = $k;
|
2013-01-18 19:12:32 -04:00
|
|
|
|
2012-11-26 18:03:59 -04:00
|
|
|
if (strpos( $G_FORM->fields[$grid]->fields[$k]->dependentFields, ',' ) !== false) {
|
|
|
|
|
$myDependentFields = explode( ',', $G_FORM->fields[$grid]->fields[$k]->dependentFields );
|
|
|
|
|
} else {
|
|
|
|
|
$myDependentFields = explode( '|', $G_FORM->fields[$grid]->fields[$k]->dependentFields );
|
|
|
|
|
}
|
2013-01-18 19:12:32 -04:00
|
|
|
|
2012-11-26 18:03:59 -04:00
|
|
|
for ($r = 0; $r < sizeof( $myDependentFields ); $r ++) {
|
|
|
|
|
if ($myDependentFields[$r] == "")
|
|
|
|
|
unset( $myDependentFields[$r] );
|
|
|
|
|
}
|
2013-01-18 19:12:32 -04:00
|
|
|
|
2013-01-15 11:42:56 -04:00
|
|
|
$mD = $myDependentFields;
|
2013-01-18 19:12:32 -04:00
|
|
|
|
2013-01-15 11:42:56 -04:00
|
|
|
foreach( $mD as $ki) {
|
|
|
|
|
$myDependentFields = array_merge( $myDependentFields , subDependencies( $ki , $G_FORM , $aux, $grid) );
|
|
|
|
|
}
|
2013-01-18 19:12:32 -04:00
|
|
|
|
|
|
|
|
//Set field and the dependent field of the grid
|
|
|
|
|
foreach ($myDependentFields as $index => $value) {
|
2013-01-24 12:44:30 -04:00
|
|
|
$myDependentFields[$index] = $k . "|" . $value;
|
2013-01-18 19:12:32 -04:00
|
|
|
}
|
2012-11-26 18:03:59 -04:00
|
|
|
}
|
2013-01-18 19:12:32 -04:00
|
|
|
|
2012-11-26 18:03:59 -04:00
|
|
|
return $myDependentFields;
|
2012-12-11 18:45:51 -04:00
|
|
|
}
|
2013-01-18 19:12:32 -04:00
|
|
|
|