2010-12-02 23:34:41 +00:00
< ? php
/**
2011-01-24 20:33:07 +00:00
* class . database_base . php
2011-06-01 18:37:18 -04:00
* @ package gulliver . system
2010-12-02 23:34:41 +00:00
*
* ProcessMaker Open Source Edition
2011-01-24 20:33:07 +00:00
* Copyright ( C ) 2004 - 2011 Colosa Inc .
2010-12-02 23:34:41 +00:00
*
* 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 .
*
*/
/**
* Class Form
* @ author David S . Callizaya S . < davidsantos @ colosa . com >
* @ package gulliver . system
* @ access public
*/
class Form extends XmlForm
{
var $id = '' ;
var $width = 600 ;
var $title = '' ;
var $fields = array ();
var $values = array ();
var $action = '' ;
var $ajaxServer = '' ;
var $enableTemplate = false ;
var $ajaxSubmit = false ;
var $callback = 'function(){}' ;
var $in_progress = 'function(){}' ;
var $template ;
var $className = " formDefault " ;
var $objectRequiredFields = null ;
var $nextstepsave = '' ;
var $printdynaform = '' ;
public $visual_frontend ;
/**
* Function setDefaultValues
* @ author David S . Callizaya S . < davidsantos @ colosa . com >
* @ access public
* @ return string
*/
function setDefaultValues ( )
{
foreach ( $this -> fields as $name => $content ) {
2010-12-06 15:12:37 +00:00
if ( is_object ( $content ) && get_class ( $content ) != '__PHP_Incomplete_Class' ) {
2010-12-02 23:34:41 +00:00
if ( isset ( $content -> defaultValue ))
$this -> values [ $name ] = $content -> defaultValue ;
else
$this -> values [ $name ] = '' ;
}
else {
$this -> values [ $name ] = '' ;
}
}
foreach ( $this -> fields as $k => $v ){
if ( is_object ( $v )) { //julichu
$this -> fields [ $k ] -> owner =& $this ;
}
}
}
/**
* Function Form
* @ author David S . Callizaya S . < davidsantos @ colosa . com >
* @ access public
* @ param string filename
* @ param string home
* @ param string language
* @ param string forceParse
* @ param string $visual_frontend
* @ return string
*/
function Form ( $filename , $home = '' , $language = '' , $forceParse = false , $visual_frontend = null )
{
$this -> visual_frontend = $visual_frontend ;
2011-06-01 18:37:18 -04:00
if ( $language === '' )
2010-12-02 23:34:41 +00:00
$language = defined ( 'SYS_LANG' ) ? SYS_LANG : 'en' ;
2011-06-01 18:37:18 -04:00
if ( $home === '' )
2010-12-02 23:34:41 +00:00
$home = defined ( 'PATH_XMLFORM' ) ? PATH_XMLFORM :
( defined ( 'PATH_DYNAFORM' ) ? PATH_DYNAFORM : '' );
//to do: obtain the error code in case the xml parsing has errors: DONE
//Load and parse the xml file
2011-06-01 18:37:18 -04:00
if ( substr ( $filename , - 4 ) !== '.xml' )
2010-12-02 23:34:41 +00:00
$filename = $filename . '.xml' ;
$this -> home = $home ;
$res = parent :: parseFile ( $filename , $language , $forceParse );
2011-06-01 18:37:18 -04:00
if ( $res == 1 )
2010-12-02 23:34:41 +00:00
trigger_error ( 'Faild to parse file ' . $filename . '.' , E_USER_ERROR );
2011-06-01 18:37:18 -04:00
if ( $res == 2 )
2010-12-02 23:34:41 +00:00
trigger_error ( 'Faild to create cache file "' . $xmlform -> parsedFile . '".' , E_USER_ERROR );
$this -> setDefaultValues ();
//to do: review if you can use the same form twice. in order to use once or not.
//DONE: Use require to be able to use the same xmlform more than once.
foreach ( $this -> fields as $k => $v ) {
//too memory? but it fails if it's loaded with baneco.xml with SYS_LANG='es'
//NOTE: This fails apparently when class of ($this->fields[$k]) is PHP_Incomplete_Class (because of cache)
if ( is_object ( $v )) { //julichu
$this -> fields [ $k ] -> owner =& $this ;
if ( $this -> fields [ $k ] -> type === 'grid' ) $this -> fields [ $k ] -> parseFile ( $home , $language );
}
}
$this -> template = PATH_CORE . 'templates/' . $this -> type . '.html' ;
}
/**
* Function printTemplate
* @ author David S . Callizaya S . < davidsantos @ colosa . com >
* @ access public
* @ param string template
* @ param string scriptContent
* @ return string
*/
function printTemplate ( $template , & $scriptContent )
{
if ( ! file_exists ( $template )){
throw ( new Exception ( 'Template "' . basename ( $template ) . '" doesn`t exist.' ));
}
$o = new xmlformTemplate ( $this , $template );
2011-06-01 18:37:18 -04:00
if ( is_array ( reset ( $this -> values )))
2010-12-02 23:34:41 +00:00
$this -> rows = count ( reset ( $this -> values ));
if ( $this -> enableTemplate ) {
$filename = substr ( $this -> fileName , 0 , - 3 ) .
( $this -> type === 'xmlform' ? '' : '.' . $this -> type ) . 'html' ;
if ( ! file_exists ( $filename )) {
$o -> template = $o -> printTemplate ( $this );
$f = fopen ( $filename , 'w+' );
fwrite ( $f , $o -> template );
fclose ( $f );
}
$o -> template = implode ( '' , file ( $filename ) );
} else {
$o -> template = $o -> printTemplate ( $this );
}
return $o -> template ;
}
/**
* Function render
* @ author David S . Callizaya S . < davidsantos @ colosa . com >
* @ access public
* @ param string template
* @ param string scriptContent
* @ return string
*/
function render ( $template , & $scriptContent )
{
/***
* This section was added for store the current used template .
*/
$tmp_var = explode ( '/' , $template );
if ( is_array ( $tmp_var ) ){
$tmp_var = $tmp_var [ sizeof ( $tmp_var ) - 1 ];
$this -> using_template = $tmp_var ;
}
/***/
$this -> template = $template ;
$o = new xmlformTemplate ( $this , $template );
$values = $this -> values ;
$aValuekeys = array_keys ( $values );
2011-06-01 18:37:18 -04:00
if ( isset ( $aValuekeys [ 0 ]) && (( int ) $aValuekeys [ 0 ] == 1 ))
2010-12-02 23:34:41 +00:00
$values = XmlForm_Field_Grid :: flipValues ( $values );
//TODO: Review when $values of a grid has only one row it is converted as a $values for a list (when template="grid" at addContent())
if ( is_array ( reset ( $values ))) {
$this -> rows = count ( reset ( $values ));
}
if ( $this -> enableTemplate ) {
$filename = substr ( $this -> fileName , 0 , - 3 ) . 'html' ;
if ( ! file_exists ( $filename )) {
$o -> template = $o -> printTemplate ( $this );
$f = fopen ( $filename , 'w+' );
fwrite ( $f , $o -> template );
fclose ( $f );
}
$o -> template = implode ( '' , file ( $filename ) );
} else {
$o -> template = $o -> printTemplate ( $this );
}
$scriptContent = $o -> printJavaScript ( $this );
$content = $o -> printObject ( $this );
return $content ;
}
/**
* Function setValues
* @ author David S . Callizaya S . < davidsantos @ colosa . com >
* @ access public
* @ param array $newValues
* @ return string
*/
function setValues ( $newValues = array ())
{
if ( ! is_array ( $newValues ) ){
return ;
}
foreach ( $this -> fields as $k => $v ){
if ( array_key_exists ( $k , $newValues ) ) {
if ( is_array ( $newValues [ $k ]) ) {
$this -> values [ $k ] = array ();
foreach ( $newValues [ $k ] as $j => $item ) {
if ( $this -> fields [ $k ] -> validateValue ( $newValues [ $k ][ $j ], $this ))
$this -> values [ $k ][ $j ] = $newValues [ $k ][ $j ];
}
2011-06-01 18:37:18 -04:00
if (( sizeof ( $this -> values [ $k ]) === 1 ) && ( $v -> type !== 'grid' ) && isset ( $this -> values [ $k ][ 0 ]) )
2010-12-02 23:34:41 +00:00
$this -> values [ $k ] = $this -> values [ $k ][ 0 ];
2011-06-01 18:37:18 -04:00
if ( sizeof ( $this -> values [ $k ]) === 0 )
2010-12-02 23:34:41 +00:00
$this -> values [ $k ] = '' ;
} else {
if ( $this -> fields [ $k ] -> validateValue ( $newValues [ $k ], $this ))
$this -> values [ $k ] = $newValues [ $k ];
}
}
}
foreach ( $newValues as $k => $v ) {
if ( strpos ( $k , 'SYS_GRID_AGGREGATE_' ) !== false ) {
$this -> values [ $k ] = $newValues [ $k ];
}
}
foreach ( $this -> fields as $k => $v ){
2010-12-10 14:09:28 +00:00
if ( is_object ( $this -> fields [ $k ]) && get_class ( $this -> fields [ $k ]) != '__PHP_Incomplete_Class' ){
2010-12-02 23:34:41 +00:00
$this -> fields [ $k ] -> owner =& $this ;
}
}
if ( isset ( $this -> labelWidth )){
$nMaxPorcent = 1024 ;
$nWidth = stripos ( $this -> width , '%' );
if ( $nWidth > 0 ) {
$sStrFind = $this -> width ;
2011-06-01 18:37:18 -04:00
$result = substr ( $sStrFind , 0 , strpos ( $sStrFind , '%' ));
2010-12-02 23:34:41 +00:00
$nWidth = ( int )(( $nMaxPorcent / 100 ) * $result );
} else {
2011-06-01 18:37:18 -04:00
$nWidth = ( int ) $this -> width ;
2010-12-02 23:34:41 +00:00
$nMaxPorcent = $nWidth ;
}
$nLabelWidth = stripos ( $this -> labelWidth , '%' );
if ( $nLabelWidth > 0 ) {
$sStrFind = $this -> labelWidth ;
2011-06-01 18:37:18 -04:00
$result = substr ( $sStrFind , 0 , strpos ( $sStrFind , '%' ));
2010-12-02 23:34:41 +00:00
$nLabelWidth = ( int )(( $nWidth / 100 ) * $result );
} else {
2011-06-01 18:37:18 -04:00
$nLabelWidth = ( int ) $this -> labelWidth ;
2010-12-02 23:34:41 +00:00
}
// krumo($nWidth,$nLabelWidth);
if (( $nWidth - $nLabelWidth ) > 0 )
$this -> fieldContentWidth = ( int )( $nWidth - $nLabelWidth );
}
}
/**
* Function getFields
* @ author David S . Callizaya S . < davidsantos @ colosa . com >
* @ access public
* @ param string template
* @ param int $therow
* @ return string
*/
function getFields ( $template , $therow = - 1 )
{
$o = new xmlformTemplate ( $this , $template );
return $o -> getFields ( $this , $therow );
}
/**
* Function that validates the values retrieved in $_POST
* @ author David S . Callizaya S . < davidsantos @ colosa . com >
* @ access public
* @ return array $_POST [ 'form' ]
*/
function validatePost ()
{
return $_POST [ 'form' ] = $this -> validateArray ( $_POST [ 'form' ]);
}
/**
* Function that validates the values retrieved in an Array :
* ex $_POST [ 'form' ]
* @ author David S . Callizaya S . < davidsantos @ colosa . com >
* @ access public
* @ param array $newValues
* @ return array
*/
function validateArray ( $newValues )
{
$values = array ();
2011-05-17 17:36:26 -04:00
foreach ( $this -> fields as $k => $v ) {
2010-12-02 23:34:41 +00:00
if (( $v -> type != 'submit' )) {
if ( $v -> type != 'file' ) {
if ( array_key_exists ( $k , $newValues ) ) {
2011-05-31 18:57:56 -04:00
2011-05-12 16:34:37 -04:00
switch ( $v -> type ){
2011-05-31 18:52:56 -04:00
case 'radiogroup' :
2011-05-17 17:36:26 -04:00
$values [ $k ] = $newValues [ $k ];
2011-05-31 18:52:56 -04:00
$values [ " { $k } _label " ] = $newValues [ " { $k } _label " ] = $v -> options [ $newValues [ $k ]];
break ;
case 'checkgroup' :
case 'listbox' :
$values [ $k ] = '' ;
foreach ( $newValues [ $k ] as $i => $value ) {
$values [ $k ] .= ( $i != 0 ? '|' : '' ) . $value ;
if ( isset ( $v -> options [ $value ])){
$values [ " { $k } _label " ] .= ( $i != 0 ? '|' : '' ) . $v -> options [ $value ];
} else {
2011-05-12 16:34:37 -04:00
$query = G :: replaceDataField ( $this -> fields [ $k ] -> sql , $newValues );
//we do the query to the external connection and we've got the label
2011-06-01 18:37:18 -04:00
$con = Propel :: getConnection ( $this -> fields [ $k ] -> sqlConnection != " " ? $this -> fields [ $k ] -> sqlConnection : " workflow " ); //use default connection workflow if connection is not defined. Same as Dynaforms
2011-05-12 16:34:37 -04:00
$stmt = $con -> prepareStatement ( $query );
$rs = $stmt -> executeQuery ( ResultSet :: FETCHMODE_NUM );
2011-05-31 18:52:56 -04:00
while ( $rs -> next ()) {
list ( $rowId , $rowContent ) = $rs -> getRow ();
if ( $value == $rowId ){
$values [ " { $k } _label " ] .= ( $i != 0 ? '|' : '' ) . $rowContent ;
2011-05-12 16:34:37 -04:00
break ;
}
}
}
}
2011-05-31 18:52:56 -04:00
$newValues [ $k ] = $values [ " { $k } _label " ];
2011-05-12 16:34:37 -04:00
break ;
2011-05-31 18:52:56 -04:00
case 'dropdown' :
$values [ $k ] = $newValues [ $k ];
if ( isset ( $v -> options [ $newValues [ $k ]])){
$values [ " { $k } _label " ] = $newValues [ " { $k } _label " ] = $v -> options [ $newValues [ $k ]];
} else {
$query = G :: replaceDataField ( $this -> fields [ $k ] -> sql , $newValues );
//we do the query to the external connection and we've got the label
2011-06-02 15:04:07 -04:00
$con = Propel :: getConnection ( $this -> fields [ $k ] -> sqlConnection != " " ? $this -> fields [ $k ] -> sqlConnection : " workflow " );
2011-05-31 18:52:56 -04:00
$stmt = $con -> prepareStatement ( $query );
$rs = $stmt -> executeQuery ( ResultSet :: FETCHMODE_NUM );
while ( $rs -> next ()) {
list ( $rowId , $rowContent ) = $rs -> getRow ();
if ( $newValues [ $k ] == $rowId ){
$values [ " { $k } _label " ] = $rowContent ;
break ;
}
}
}
break ;
2011-05-12 16:34:37 -04:00
case 'grid' :
2010-12-02 23:34:41 +00:00
foreach ( $newValues [ $k ] as $j => $item ) {
if ( is_array ( $item )){
$i = 0 ;
2011-05-12 16:34:37 -04:00
$values [ $k ][ $j ] = $this -> fields [ $k ] -> maskValue ( $newValues [ $k ][ $j ], $this );
2011-04-06 18:14:50 -04:00
foreach ( $item as $kk => $vv ){
2011-05-17 17:36:26 -04:00
2011-04-07 12:40:20 -04:00
//we need to know which fields are dropdowns
2011-05-17 17:36:26 -04:00
if ( $this -> fields [ $k ] -> fields [ $kk ] -> type == 'dropdown' ) {
$values [ $k ][ $j ] = $newValues [ $k ][ $j ];
2011-05-31 18:52:56 -04:00
if ( $this -> fields [ $k ] -> validateValue ( $newValues [ $k ][ $j ], $this )){
if ( isset ( $this -> fields [ $k ] -> fields [ $kk ] -> options [ $vv ])){
$values [ $k ][ $j ][ " { $kk } _label " ] = $newValues [ $k ][ $j ][ $kk . '_label' ] = $this -> fields [ $k ] -> fields [ $kk ] -> options [ $vv ];
} else {
$query = G :: replaceDataField ( $this -> fields [ $k ] -> fields [ $kk ] -> sql , $values [ $k ][ $j ]);
2011-06-02 15:04:07 -04:00
$con = Propel :: getConnection ( $this -> fields [ $k ] -> sqlConnection != " " ? $this -> fields [ $k ] -> sqlConnection : " workflow " );
2011-05-31 18:52:56 -04:00
$stmt = $con -> prepareStatement ( $query );
$rs = $stmt -> executeQuery ( ResultSet :: FETCHMODE_NUM );
while ( $rs -> next ()){
// from the query executed we only need certain elements
list ( $rowId , $rowContent ) = $rs -> getRow ();
if ( $vv == $rowId ){
$values [ $k ][ $j ][ " { $kk } _label " ] = $newValues [ $k ][ $j ][ $kk . '_label' ] = $rowContent ;
break ;
2011-04-25 12:33:04 -04:00
}
2010-12-02 23:34:41 +00:00
}
}
2011-05-17 17:36:26 -04:00
}
} else {
2011-05-12 16:34:37 -04:00
// if there are no dropdowns previously setted and the evaluated field is not a dropdown
// only then rewritte the $values
2011-05-17 17:36:26 -04:00
$values [ $k ][ $j ] = $this -> fields [ $k ] -> maskValue ( $newValues [ $k ][ $j ], $this );
}
2010-12-02 23:34:41 +00:00
$i ++ ;
}
2011-05-17 17:36:26 -04:00
} else {
$values [ $k ][ $j ] = $this -> fields [ $k ] -> maskValue ( $newValues [ $k ][ $j ], $this );
}
2010-12-02 23:34:41 +00:00
}
2011-05-12 16:34:37 -04:00
break ;
2011-05-31 18:52:56 -04:00
2011-05-12 16:34:37 -04:00
default :
if ( $this -> fields [ $k ] -> validateValue ( $newValues [ $k ], $this ))
2010-12-02 23:34:41 +00:00
$values [ $k ] = $this -> fields [ $k ] -> maskValue ( $newValues [ $k ], $this );
2011-05-12 16:34:37 -04:00
2010-12-02 23:34:41 +00:00
}
2011-05-12 16:34:37 -04:00
2010-12-02 23:34:41 +00:00
}
}
else {
if ( isset ( $_FILES [ 'form' ][ 'name' ][ $k ])) {
$values [ $k ] = $_FILES [ 'form' ][ 'name' ][ $k ];
}
2011-05-12 16:34:37 -04:00
/**
2010-12-02 23:34:41 +00:00
* FIXED for multiple inputs documents related to file type field
* By Erik Amaru Ortiz < erik @ colosa . com >
2011-05-12 16:34:37 -04:00
* Nov 24 th , 2009
2010-12-02 23:34:41 +00:00
*/
if ( isset ( $v -> input ) && $v -> input != '' ){
$_POST [ 'INPUTS' ][ $k ] = $v -> input ;
}
/**/
}
}
}
2011-05-12 16:34:37 -04:00
2010-12-02 23:34:41 +00:00
foreach ( $newValues as $k => $v ) {
if ( strpos ( $k , 'SYS_GRID_AGGREGATE_' ) !== false ) {
$values [ $k ] = $v ;
}
2011-05-12 16:34:37 -04:00
}
2010-12-02 23:34:41 +00:00
return $values ;
}
/**
* Function that return the valid fields to replace
* @ author Julio Cesar Laura Avendao ? = o < juliocesar @ colosa . com >
* @ access public
* @ param boolean $bWhitSystemVars
* @ return array
*/
2011-06-01 18:37:18 -04:00
function getVars ( $bWhitSystemVars = true )
2010-12-02 23:34:41 +00:00
{
$aFields = array ();
if ( $bWhitSystemVars ) {
$aAux = G :: getSystemConstants ();
foreach ( $aAux as $sName => $sValue ) {
$aFields [] = array ( 'sName' => $sName , 'sType' => 'system' );
}
}
foreach ( $this -> fields as $k => $v ) {
if (( $v -> type != 'title' ) && ( $v -> type != 'subtitle' ) && ( $v -> type != 'link' ) &&
( $v -> type != 'file' ) && ( $v -> type != 'button' ) && ( $v -> type != 'reset' ) &&
( $v -> type != 'submit' ) && ( $v -> type != 'listbox' ) && ( $v -> type != 'checkgroup' ) &&
( $v -> type != 'grid' ) && ( $v -> type != 'javascript' )) {
$aFields [] = array ( 'sName' => trim ( $k ), 'sType' => trim ( $v -> type ));
}
}
return $aFields ;
}
/**
* Function that verify the required fields without a correct value
* @ author Erik Amaru Ortiz < erik @ colosa . com >
* @ access public
* @ param array $values
2011-06-01 18:37:18 -04:00
* @ param array $aNoRequiredByJS
2010-12-02 23:34:41 +00:00
* @ return array / false
*/
function validateRequiredFields ( $values , $aNoRequiredByJS = array ())
{
$rFields = Array ();
$missingFields = Array ();
foreach ( $this -> fields as $o ) {
2010-12-06 15:12:37 +00:00
if ( is_object ( $o ) && property_exists ( get_class ( $o ), 'required' )) {
2010-12-02 23:34:41 +00:00
if ( $o -> required == 1 ) {
if ( ! in_array ( $o -> name , $aNoRequiredByJS )) {
array_push ( $rFields , $o -> name );
}
}
}
}
2011-05-19 17:27:24 -04:00
2010-12-02 23:34:41 +00:00
foreach ( $rFields as $field ){
#we verify if the requiered field is in array values,. t
if ( array_key_exists ( $field , $values )) {
if ( $values [ $field ] == " " ) {
array_push ( $missingFields , $field );
}
} else {
array_push ( $missingFields , $field );
}
}
if ( sizeof ( $missingFields ) != 0 ) {
return $missingFields ;
} else {
return false ;
}
}
}