Source for file TFieldList.php
Documentation is available at TFieldList.php
namespace
Adianti \
Widget \
Form ;
use
Adianti \
Widget \
Container \
TTable ;
use
Adianti \
Widget \
Form \
TLabel ;
use
Adianti \
Widget \
Util \
TImage ;
* @author Pablo Dall'Oglio
* @copyright Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
* @license http://www.adianti.com.br/framework-license
private $remove_function ;
private $fields_properties ;
private $total_functions ;
private $allow_post_empty ;
$this -> { 'id' } =
'tfieldlist_' .
mt_rand ( 1000000000 , 1999999999 ) ;
$this -> { 'name' } =
$this -> { 'id' } ;
$this -> { 'class' } =
'tfieldlist' ;
$this -> fields_properties =
[ ] ;
$this -> row_functions =
[ ] ;
$this -> body_created =
false ;
$this -> automatic_aria =
false ;
$this -> remove_function =
'ttable_remove_row(this)' ;
$this -> clone_function =
'ttable_clone_previous_row(this)' ;
$this -> summarize =
false ;
$this -> total_functions =
null ;
$this -> remove_enabled =
true ;
$this -> allow_post_empty =
true ;
$this -> allow_post_empty =
false ;
* Get post data as object list
foreach ( $this -> fields as $field )
foreach ( $data as $name =>
$values )
if ( ! empty ( $this -> field_prefix ))
$field_name =
str_replace ( $this -> field_prefix .
'_' , '' , $field_name ) ;
foreach ( $values as $row =>
$value )
$results [ $row ] =
$results [ $row ] ??
new stdClass ;
$results [ $row ] -> $field_name =
$value ;
if ( ! $this -> allow_post_empty )
foreach ( $results as $row =>
$object )
$array_object = (array)
$object ;
unset
( $array_object [ 'uniq' ] ) ;
if ( count ( $this -> fields ) >
0 )
if ( isset
( $this -> fields [ $field_name ] ))
$field =
$this -> fields [ $field_name ] ;
else if ( isset
( $this -> fields [ $field_name .
'[]' ] ))
$field =
$this -> fields [ $field_name .
'[]' ] ;
$this -> remove_enabled =
false ;
* Generate automatic aria-labels
$this -> automatic_aria =
true ;
* Define the action to be executed when the user sort rows
* @param $action TAction object
$this -> sort_action =
$action ;
$string_action =
$action -> toString ( ) ;
* Set the remove javascript action
$this -> remove_function =
$action ;
$this -> remove_icon =
$icon ;
$this -> remove_title =
$title ;
public function setRemoveAction ( TAction $action =
null , $icon =
null , $title =
null )
$this -> remove_action =
$action ;
$string_action =
$action -> toString ( ) ;
$this -> remove_icon =
$icon ;
$this -> remove_title =
$title ;
* Set the clone javascript action
$this -> clone_function =
$action ;
$this -> row_functions [ ] =
[ $function , $icon , $title ] ;
$this -> row_actions [ ] =
[ $action , $icon , $title ] ;
* Define total update action
parent :: setProperty ( 'total-update-action' , $action -> serialize ( false )) ;
$this -> field_prefix =
$prefix ;
return $this -> field_prefix ;
* @param $label Field Label
* @param $object Field Object
public function addField ( $label , AdiantiWidgetInterface $field , $properties =
null )
if ( ! empty ( $this -> field_prefix ) &&
strpos ( $name , $this -> field_prefix ) ===
false )
$name =
$this -> field_prefix .
'_' .
$name ;
if ( isset
( $this -> fields [ $name ] ) AND substr ( $name , -
2 ) !==
'[]' )
$this -> fields [ $name ] =
$field ;
$this -> fields_properties [ $name ] =
$properties ;
if ( isset
( $properties [ 'sum' ] ) &&
$properties [ 'sum' ] ==
true )
if ( isset
( $properties [ 'uniqid' ] ) &&
$properties [ 'uniqid' ] ==
true )
$field -> { 'uniqid' } =
'true' ;
$label_value =
$label -> getValue ( ) ;
$label_field =
new TLabel ( $label ) ;
$field -> setLabel ( $label_value ) ;
$this -> labels [ $name ] =
$label_field ;
$this -> thead =
$section =
parent :: addSection ( 'thead' ) ;
foreach ( $this -> fields as $name =>
$field )
$cell =
$row -> addCell ( '' ) ;
$cell -> { 'style' } =
'display:none' ;
$cell =
$row -> addCell ( $this -> labels [ $field -> getName ( ) ] ) ;
if ( ! empty ( $this -> fields_properties [ $name ] ))
foreach ( $this -> fields_properties [ $name ] as $property =>
$value )
$all_actions =
array_merge ( (array)
$this -> row_functions , (array)
$this -> row_actions ) ;
foreach ( $all_actions as $row_action )
$cell =
$row -> addCell ( '' ) ;
$cell -> { 'style' } =
'display:none' ;
if ( $this -> remove_enabled )
// aligned with remove button
$cell =
$row -> addCell ( '' ) ;
$cell -> { 'style' } =
'display:none' ;
* @param $item Data object
$uniqid =
mt_rand ( 1000000 , 9999999 ) ;
$field_list_name =
$this -> { 'name' } ;
if ( ! $this -> body_created )
$this -> body_created =
true ;
$move =
new TImage ( 'fas:arrows-alt gray' ) ;
$move -> { 'class' } .=
' handle' ;
$move -> { 'style' } .=
';font-size:100%;cursor:move' ;
foreach ( $this -> fields as $field )
if ( $this -> detail_row ==
0 )
if ( isset
( $this -> fields_properties [ $field_name ] [ 'sum' ] ) &&
$this -> fields_properties [ $field_name ] [ 'sum' ] ==
true )
$field -> { 'exitaction' } =
" tfieldlist_update_sum('{$field_list_name } ', '{$name } ', 'callback') " ;
$field -> { 'onBlur' } =
" tfieldlist_update_sum('{$field_list_name } ', '{$name } ', 'callback') " ;
$this -> total_functions .=
$field -> { 'exitaction' } .
';' ;
$value = isset
( $item -> $name ) ?
$item -> $name :
0 ;
if ( isset
( $field -> { 'data-nmask' } ))
$dec_sep =
substr ( $field -> { 'data-nmask' } , 1 , 1 ) ;
$tho_sep =
substr ( $field -> { 'data-nmask' } , 2 , 1 ) ;
if ( ( strpos ( $value , $tho_sep ) !==
false ) &&
( strpos ( $value , $dec_sep ) !==
false ) )
if ( isset
( $this -> totals [ $name ] ))
$this -> totals [ $name ] +=
$value ;
$this -> totals [ $name ] =
$value ;
if ( $this -> automatic_aria )
$label =
$this -> labels [ $field -> getName ( ) ] ;
$aria_label =
$label -> getValue ( ) ;
$field -> { 'aria-label' } =
$aria_label ;
$clone -> setId ( $name .
'_' .
$uniqid ) ;
$clone -> { 'data-row' } =
$this -> detail_row ;
$cell =
$row -> addCell ( $clone ) ;
$cell -> { 'class' } =
'field' ;
if ( ! empty ( $this -> fields_properties [ $field_name ] ))
foreach ( $this -> fields_properties [ $field_name ] as $property =>
$value )
$cell -> { 'style' } =
'display:none' ;
if ( ! empty ( $item -> $name ) OR ( isset
( $item -> $name ) AND $item -> $name ==
'0' ))
$clone -> setValue ( $item -> $name ) ;
if ( $field -> { 'uniqid' } ==
true )
$clone -> setValue ( mt_rand ( 1000000000 , 1999999999 ) ) ;
$clone -> setValue ( null ) ;
foreach ( $this -> row_actions as $row_action )
$string_action =
$row_action [ 0 ] -> serialize ( FALSE ) ;
$btn -> { 'class' } =
'btn btn-default btn-sm' ;
$btn -> { 'onclick' } =
" __adianti_post_exec('{$string_action } ', tfieldlist_get_row_data(this), null, undefined, '1') " ;
$btn -> { 'title' } =
$row_action [ 2 ] ;
if ( $this -> row_functions )
foreach ( $this -> row_functions as $row_function )
$btn -> { 'class' } =
'btn btn-default btn-sm' ;
$btn -> { 'onclick' } =
$row_function [ 0 ] ;
$btn -> { 'title' } =
$row_function [ 2 ] ;
if ( $this -> remove_enabled )
$del -> { 'class' } =
'btn btn-default btn-sm' ;
$del -> { 'onclick' } =
$this -> total_functions .
$this -> remove_function ;
if ( isset
( $this -> remove_action ))
$string_action =
$this -> remove_action -> serialize ( FALSE ) ;
$del -> { 'onclick' } .=
" ;__adianti_post_exec('{$string_action } ', tfieldlist_get_row_data(this), null, undefined, '1') " ;
$del -> add ( $this -> remove_icon ?
new TImage ( $this -> remove_icon ) :
'<i class="fa fa-times red"></i>' ) ;
public function addCloneAction ( TAction $clone_action =
null , $icon =
null , $title =
null )
if ( ! $this -> body_created )
foreach ( $this -> fields as $field )
$cell =
$row -> addCell ( '' ) ;
$cell -> { 'style' } =
'display:none' ;
else if ( isset
( $this -> fields_properties [ $field_name ] [ 'sum' ] ) &&
$this -> fields_properties [ $field_name ] [ 'sum' ] ==
true )
$grand_total =
clone $field ;
$grand_total -> setId ( $field_name .
'_' .
mt_rand ( 1000000 , 9999999 )) ;
$grand_total -> setName ( 'grandtotal_' .
$field_name ) ;
$grand_total -> { 'field_name' } =
$field_name ;
$grand_total -> setEditable ( FALSE ) ;
$grand_total -> { 'style' } .=
';font-weight:bold;border:0 !important;background:none' ;
if ( ! empty ( $this -> totals [ $field_name ] ))
$grand_total -> setValue ( $this -> totals [ $field_name ] ) ;
$cell -> add ( $grand_total ) ;
$all_actions =
array_merge ( (array)
$this -> row_functions , (array)
$this -> row_actions ) ;
foreach ( $all_actions as $row_action )
$cell =
$row -> addCell ( '' ) ;
$add -> { 'class' } =
'btn btn-default btn-sm' ;
$add -> { 'onclick' } =
$this -> clone_function ;
$string_action =
$clone_action -> serialize ( FALSE ) ;
$add -> { 'onclick' } =
" __adianti_post_exec('{$string_action } ', tfieldlist_get_last_row_data(this), null, undefined, '1'); " .
$add -> { 'onclick' } ;
$add -> add ( $icon ?
new TImage ( $icon ) :
'<i class="fa fa-plus green"></i>' ) ;
* @param $name field list name
public static function clear ( $name )
* Clear some field list rows
* @param $name field list name
* @param $index field list name
* @param $quantity field list name
public static function clearRows ( $name , $start =
0 , $length =
0 )
TScript :: create ( " tfieldlist_clear_rows('{$name } ', {$start } , {$length } ); " ) ;
* @param $name field list name
* @param $rows quantity rows
* @param $timeout timeout
public static function addRows ( $name , $rows , $timeout =
50 )
TScript :: create ( " tfieldlist_add_rows('{$name } ', {$rows } , {$timeout } ); " ) ;
$this -> thead -> { 'style' } .=
';display:block' ;
$this -> tbody -> { 'style' } .=
';display:block;overflow-y: scroll;height:' .
$height .
'px' ;
$this -> tbody -> { 'class' } .=
' thin-scroll' ;
$this -> tfoot -> { 'style' } .=
';display:block;float:right;margin-right:10px' ;
if ( empty ( $this -> sort_action ))
if ( ! empty ( $this -> fields ))
$this -> sort_action -> setParameter ( 'static' , '1' ) ;
$form_name =
$first_field -> getFormName ( ) ;
$string_action =
$this -> sort_action -> serialize ( FALSE ) ;
$sort_action =
" function() { __adianti_post_data('{$form_name } ', '{$string_action } '); } " ;
TScript :: create ( " ttable_sortable_rows('{$id } ', '.handle', $sort_action ) " ) ;