3333use App \Base \Abstracts \Controllers \BasePage ;
3434use App \Site \Models \MediaElement ;
3535use Symfony \Component \HttpFoundation \Response ;
36+ use Degami \PHPFormsApi as FAPI ;
3637
3738/**
3839 * Base for admin page that manages a Model
@@ -172,8 +173,11 @@ protected function collectActionButtons() : self
172173 }
173174 }
174175
175- if (( $ this ->template_data ['action ' ] ?? 'list ' ) != 'list ' ) {
176+ if (! in_array (( $ this ->template_data ['action ' ] ?? 'list ' ), [ 'list ' , ' duplicate ' , ' delete ' ,]) ) {
176177 $ this ->addBackButton ();
178+ if (($ this ->template_data ['action ' ] ?? 'list ' ) != 'new ' ) { // no need to remove something that is not there
179+ $ this ->addRemoveButton ();
180+ }
177181 }
178182
179183 return $ this ;
@@ -739,6 +743,26 @@ public function getModelTableName(): mixed
739743 protected function beforeRender (): BasePage |Response
740744 {
741745 if ($ this ->getRequest ()->query ->get ('action ' ) == 'duplicate ' ) {
746+
747+ $ form = FAPI \FormBuilder::getForm ([$ this , 'getDuplicateFormDefinition ' ], $ this ->getFormId ())
748+ ->setValidate ([[$ this , 'duplicateFormValidate ' ]])
749+ ->setSubmit ([[$ this , 'duplicateFormSubmitted ' ]]);
750+
751+ $ this ->template_data = [
752+ 'action ' => $ this ->getRequest ()->query ->get ('action ' ) ?? 'list ' ,
753+ 'form ' => $ form ,
754+ ];
755+
756+ $ this ->getApp ()->event ('before_form_process ' , ['form ' => $ form ]);
757+ $ form ->process ();
758+
759+ if ($ form && $ form ->isSubmitted ()) {
760+ $ this ->getApp ()->event ('form_submitted ' , ['form ' => $ form ]);
761+ return $ form ->getSubmitResults (get_class ($ this ) . '::duplicateFormSubmitted ' );
762+ }
763+
764+ /*
765+ // if we wish to use "javascript confirmation"
742766 $object = $this->getObject();
743767 $copy = $object?->duplicate()->persist();
744768 if ($copy) {
@@ -747,12 +771,46 @@ protected function beforeRender(): BasePage|Response
747771 } else {
748772 $this->addErrorFlashMessage($this->getUtils()->translate('Error duplicating object', locale: $this->getCurrentLocale()));
749773 return $this->doRedirect($this->getControllerUrl() . '?action=list');
750- }
774+ }*/
751775 }
752776
753777 return parent ::beforeRender ();
754778 }
755779
780+ public function getDuplicateFormDefinition (FAPI \Form $ form , array &$ form_state ): FAPI \Form
781+ {
782+ $ object = $ this ->getObject ();
783+ $ this ->fillConfirmationForm ('Do you confirm the duplication of the selected element? ' , $ form , $ this ->getControllerUrl () . '?action=edit& ' . $ this ->getObjectIdQueryParam () . '= ' . $ object ->getId ());
784+ return $ form ;
785+ }
786+
787+ public function duplicateFormValidate (FAPI \Form $ form , &$ form_state ): bool |string
788+ {
789+ return true ;
790+ }
791+
792+ public function duplicateFormSubmitted (FAPI \Form $ form , &$ form_state ): mixed
793+ {
794+
795+ $ object = $ this ->getObject ();
796+ $ copy = $ object ?->duplicate()->persist ();
797+ if ($ copy ) {
798+ $ this ->addSuccessFlashMessage ($ this ->getUtils ()->translate ('Object duplicated successfully ' , locale: $ this ->getCurrentLocale ()));
799+ return $ this ->doRedirect ($ this ->getControllerUrl () . '?action=edit& ' . $ this ->getObjectIdQueryParam () . '= ' . $ copy ->getId ());
800+ } else {
801+ $ this ->addErrorFlashMessage ($ this ->getUtils ()->translate ('Error duplicating object ' , locale: $ this ->getCurrentLocale ()));
802+ return $ this ->doRedirect ($ this ->getControllerUrl () . '?action=list ' );
803+ }
804+ }
805+
806+ protected function getModelRowButtons (BaseModel $ object ) : array
807+ {
808+ return [
809+ static ::EDIT_BTN => $ this ->getEditButton ($ object ->getId ()),
810+ static ::DELETE_BTN => $ this ->getDeleteButton ($ object ->getId ()),
811+ ];
812+ }
813+
756814 public static function exposeDataToDashboard () : mixed
757815 {
758816 return App::getInstance ()->containerCall ([static ::getObjectClass (), 'getCollection ' ])->count ();
0 commit comments