@ -1,4 +1,5 @@
< ? php
/**
* @version CVS: 1.0.0
* @package Com_Circolari
@ -50,8 +51,8 @@ class FirmatipoModel extends AdminModel
*/
protected $item = null ;
/**
* Returns a reference to the a Table object, always creating it.
@ -86,25 +87,24 @@ class FirmatipoModel extends AdminModel
// Get the form.
$form = $this -> loadForm (
'com_circolari.firmaTipo' ,
'firmaTipo' ,
array (
'control' => 'jform' ,
'load_data' => $loadData
)
);
'com_circolari.firmaTipo' ,
'firmaTipo' ,
array (
'control' => 'jform' ,
'load_data' => $loadData
)
);
if ( empty ( $form ))
{
if ( empty ( $form )) {
return false ;
}
return $form ;
}
/**
* Method to get the data that should be injected in the form.
@ -118,15 +118,12 @@ class FirmatipoModel extends AdminModel
// Check the session for previously entered form data.
$data = Factory :: getApplication () -> getUserState ( 'com_circolari.edit.firmatipo.data' , array ());
if ( empty ( $data ))
{
if ( $this -> item === null )
{
if ( empty ( $data )) {
if ( $this -> item === null ) {
$this -> item = $this -> getItem ();
}
$data = $this -> item ;
}
return $data ;
@ -143,21 +140,111 @@ class FirmatipoModel extends AdminModel
*/
public function getItem ( $pk = null )
{
if ( $item = parent :: getItem ( $pk ))
{
if ( isset ( $item -> params ))
{
$item -> params = json_encode ( $item -> params );
}
// Do any procesing on fields here if needed
if ( $item = parent :: getItem ( $pk )) {
if ( isset ( $item -> params )) {
$item -> params = json_encode ( $item -> params );
}
return $item ;
if ( $item && ( int ) $item -> id ) {
$db = Factory :: getDbo ();
$q = $db -> getQuery ( true )
-> select ( $db -> quoteName ([ 'label' , 'ordering' ]))
-> from ( $db -> quoteName ( '#__circolari_firmetipi_bottoni' ))
-> where ( $db -> quoteName ( 'firmatipo_id' ) . ' = ' . ( int ) $item -> id )
-> order ( $db -> escape ( 'ordering ASC, id ASC' ));
$db -> setQuery ( $q );
$rows = ( array ) $db -> loadObjectList ();
// Adatta ai nomi del subform (etichetta/ordering)
$item -> bottoni_firma = array_map ( static function ( $r ) {
return [
'etichetta' => ( string ) ( $r -> label ? ? '' ),
'ordering' => ( int ) ( $r -> ordering ? ? 0 ),
];
}, $rows );
} else {
// default per form nuovo
if ( is_object ( $item )) {
$item -> bottoni_firma = [];
}
}
// Do any procesing on fields here if needed
}
return $item ;
}
public function save ( $data )
{
// Stacco il subform per non confondere la tabella principale
$bottoni = $data [ 'bottoni_firma' ] ? ? null ;
unset ( $data [ 'bottoni_firma' ]);
// Salvo il firmatipo
$result = parent :: save ( $data );
if ( ! $result ) {
return false ;
}
// Recupero l'ID del firmatipo appena salvato
$id = ( int ) ( $data [ 'id' ] ? ? 0 );
if ( $id <= 0 ) {
$id = ( int ) $this -> getState ( $this -> getName () . '.id' );
}
if ( $id <= 0 ) {
// Estremo: ricarico l'item corrente
$item = parent :: getItem ();
$id = ( int ) ( $item -> id ? ? 0 );
}
if ( $id <= 0 ) {
$this -> setError ( 'Impossibile determinare l’ ID del firmatipo dopo il salvataggio.' );
return false ;
}
// Se il campo non è presente nel POST (permessi/layout), non tocco i bottoni
if ( $bottoni === null ) {
return true ;
}
$db = Factory :: getDbo ();
$db -> transactionStart ();
try {
// Pulisce i bottoni esistenti del firmatipo
$delete = $db -> getQuery ( true )
-> delete ( $db -> quoteName ( '#__circolari_firmetipi_bottoni' ))
-> where ( $db -> quoteName ( 'firmatipo_id' ) . ' = ' . $id );
$db -> setQuery ( $delete ) -> execute ();
// Re-inserisce dai dati del subform
foreach (( array ) $bottoni as $row ) {
$label = trim (( string ) ( $row [ 'etichetta' ] ? ? '' ));
if ( $label === '' ) {
continue ; // salta righe vuote
}
$ordering = ( int ) ( $row [ 'ordering' ] ? ? 0 );
$insert = $db -> getQuery ( true )
-> insert ( $db -> quoteName ( '#__circolari_firmetipi_bottoni' ))
-> columns ( $db -> quoteName ([ 'firmatipo_id' , 'label' , 'ordering' ]))
-> values (( int ) $id . ', ' . $db -> quote ( $label ) . ', ' . ( int ) $ordering );
$db -> setQuery ( $insert ) -> execute ();
}
$db -> transactionCommit ();
} catch ( \Throwable $e ) {
$db -> transactionRollback ();
$this -> setError ( $e -> getMessage ());
return false ;
}
return true ;
}
/**
* Method to duplicate an Etichetta
*
@ -171,11 +258,10 @@ class FirmatipoModel extends AdminModel
{
$app = Factory :: getApplication ();
$user = $app -> getIdentity ();
$dispatcher = $this -> getDispatcher ();
$dispatcher = $this -> getDispatcher ();
// Access checks.
if ( ! $user -> authorise ( 'core.create' , 'com_circolari' ))
{
if ( ! $user -> authorise ( 'core.create' , 'com_circolari' )) {
throw new \Exception ( Text :: _ ( 'JERROR_CORE_CREATE_NOT_PERMITTED' ));
}
@ -186,61 +272,55 @@ class FirmatipoModel extends AdminModel
$table = $this -> getTable ();
foreach ( $pks as $pk )
{
if ( $table -> load ( $pk , true ))
{
// Reset the id to create a new record.
$table -> id = 0 ;
foreach ( $pks as $pk ) {
if ( ! $table -> check ( ))
{
throw new \Exception ( $table -> getError ()) ;
}
if ( $table -> load ( $pk , true )) {
// Reset the id to create a new record.
$table -> id = 0 ;
// Create the before save event.
$beforeSaveEvent = AbstractEvent :: create (
$this -> event_before_save ,
[
'context' => $context ,
'subject' => $table ,
'isNew' => true ,
'data' => $table ,
]
);
// Trigger the before save event.
$dispatchResult = Factory :: getApplication () -> getDispatcher () -> dispatch ( $this -> event_before_save , $beforeSaveEvent );
// Check if dispatch result is an array and handle accordingly
$result = isset ( $dispatchResult [ 'result' ]) ? $dispatchResult [ 'result' ] : [];
// Proceed with your logic
if ( in_array ( false , $result , true ) || ! $table -> store ()) {
throw new \Exception ( $table -> getError ());
}
// Trigger the after save event.
Factory :: getApplication () -> getDispatcher () -> dispatch (
$this -> event_after_save ,
AbstractEvent :: create (
$this -> event_after_save ,
[
'context' => $context ,
'subject' => $table ,
'isNew' => true ,
'data' => $table ,
]
)
);
}
else
{
if ( ! $table -> check ()) {
throw new \Exception ( $table -> getError ());
}
// Create the before save event.
$beforeSaveEvent = AbstractEvent :: create (
$this -> event_before_save ,
[
'context' => $context ,
'subject' => $table ,
'isNew' => true ,
'data' => $table ,
]
);
// Trigger the before save event.
$dispatchResult = Factory :: getApplication () -> getDispatcher () -> dispatch ( $this -> event_before_save , $beforeSaveEvent );
// Check if dispatch result is an array and handle accordingly
$result = isset ( $dispatchResult [ 'result' ]) ? $dispatchResult [ 'result' ] : [];
// Proceed with your logic
if ( in_array ( false , $result , true ) || ! $table -> store ()) {
throw new \Exception ( $table -> getError ());
}
// Trigger the after save event.
Factory :: getApplication () -> getDispatcher () -> dispatch (
$this -> event_after_save ,
AbstractEvent :: create (
$this -> event_after_save ,
[
'context' => $context ,
'subject' => $table ,
'isNew' => true ,
'data' => $table ,
]
)
);
} else {
throw new \Exception ( $table -> getError ());
}
}
// Clean cache
@ -262,11 +342,9 @@ class FirmatipoModel extends AdminModel
{
jimport ( 'joomla.filter.output' );
if ( empty ( $table -> id ))
{
if ( empty ( $table -> id )) {
// Set ordering to the last item if not set
if ( @ $table -> ordering === '' )
{
if ( @ $table -> ordering === '' ) {
$db = $this -> getDbo ();
$db -> setQuery ( 'SELECT MAX(ordering) FROM #__circolari_firmetipi' );
$max = $db -> loadResult ();