New Amember 4 Plugin, catching the submit form event

Discussion in 'Customization & add-ons' started by valentinuzzi, Jun 19, 2012.

  1. valentinuzzi

    valentinuzzi aMember Pro Customer

    Joined:
    Oct 22, 2010
    Messages:
    13
    I'm developing a plugin for my Amember 4 installation.

    How can I get my plugin "do something" just after the save button of the form I added to the Setup/Configuration menu of Amember is clicked?

    Can someone just write down the code of the function I need to put into the plugin to do this?

    Thanks a lot, hope to have explained myself.
  2. alex

    alex Administrator Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,020
    You have to override Am_Form_Setup class with your own, and then redefine
    PHP:
    class Am_Form_Setup_MyCustom extends Am_Form_Setup
    {
        public function 
    beforeSaveConfig(Am_Config $beforeAm_Config $after)
        {
            
    // if exception thrown from here, config won't be saved
        
    }
        public function 
    afterSaveConfig(Am_Config $beforeAm_Config $after)
        {
            
    // run custom actions here
        
    }
    }
    If you show me a piece of your code when you create the setup form, I will show how to do that.
  3. valentinuzzi

    valentinuzzi aMember Pro Customer

    Joined:
    Oct 22, 2010
    Messages:
    13
    Thanks a lot,

    here is the code generating the form:


    PHP:
     
    class Am_Plugin_Ivaitaliana extends Am_Plugin
    {
     
        public function 
    isConfigured()
        {
            return (bool)
    $this->getConfig('iva');
        }
     
        function 
    onSetupForms(Am_Event_SetupForms $forms)
        {
            
    $form = new Am_Form_Setup('ivaitaliana_setup');
            
    $form->setTitle("Iva Italiana");
            
    $form->setComment('Qui aggiungi un tuo commento...');
         
            
    $f1 $form->addFieldset()->setLabel('Attuale percentuale di IVA');
            
    $f1->addText("iva",array('size'=>5))->setLabel(array("Inserisci IVA senza %"));
       
            
    $id_ricevuta Am_Di::getInstance()->db->select("SELECT invoice_id FROM ?_invoice WHERE invoice_id ");   
            
    $valore_adesso=0;
            foreach(
    $id_ricevuta as $id_ric){
                if (
    $id_ric['invoice_id'] > $valore_adesso)
                        {
                            
    $valore_adesso=$id_ric['invoice_id'];
                        }
            }
            
    $config Am_Di::getInstance()->config;
            
    $config->saveValue(nfatt,(string)$valore_adesso);
            
    $f2 $form->addFieldset()->setLabel('Attuale numero di ricevuta');
            
    $f2->addText("nfatt",array('size'=>5))->setLabel(array("Ultimo numero ricevuta:"));     
         
            
    $f3 $form->addFieldset()->setLabel('Operazione di fine anno (ATTENZIONE! Usare con cura!)');
            
    $f3->addAdvCheckbox('fineanno')->setLabel('Avvia la procedura di fine anno');     
         
            
    $forms->addForm($form);
        }
     
    The idea is to have the invoice table records to be modified in the database on the basis of the nfatt field of fieldset $f2.

    So if I understand properly I can use the
    beforeSaveConfig
    to validate the form. In the case of a non valid form how can I throw properly an exception so the correct error is handled by Amember core?

    Into the
    afterSaveConfig
    section I can run all the code to execute in the case the form is a valid one, isn't it?

    Thanks a lot Max ;)
  4. valentinuzzi

    valentinuzzi aMember Pro Customer

    Joined:
    Oct 22, 2010
    Messages:
    13
    I tryied overriding
    Am_Form_Setup class in my Plugin but I get another empty button on the Admin Setup menu bar and nothing more....​

    I think I'm missing some point :) hope in your help ;)
  5. alex

    alex Administrator Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,020
    Here you go:

    PHP:
    class Am_Form_Setup_Ivaitaliana extends Am_Form_Setup
    {
        public function 
    afterSaveConfig(Am_Config $beforeAm_Config $after)
        {
            
    // get the plugin object iif necessary
            
    $plugin Am_Di::getInstance()->plugins_misc->loadGet('ivaitaliana');
            
    // run custom actions here
            // uncomment next line for quick debug
            // print_rre($after);
        
    }
    }
     
     
    class 
    Am_Plugin_Ivaitaliana extends Am_Plugin
    {
     
        public function 
    isConfigured()
        {
            return (bool)
    $this->getConfig('iva');
        }
     
        function 
    onSetupForms(Am_Event_SetupForms $forms)
        {
            
    $form = new Am_Form_Setup_Ivaitaliana('ivaitaliana_setup'); // here we insert classname
            
    $form->setTitle("Iva Italiana");
            
    $form->setComment('Qui aggiungi un tuo commento...');
       
            
    $f1 $form->addFieldset()->setLabel('Attuale percentuale di IVA');
            
    $f1->addText("iva",array('size'=>5))->setLabel(array("Inserisci IVA senza %"));
     
            
    $id_ricevuta Am_Di::getInstance()->db->select("SELECT invoice_id FROM ?_invoice WHERE invoice_id "); 
            
    $valore_adesso=0;
            foreach(
    $id_ricevuta as $id_ric){
                if (
    $id_ric['invoice_id'] > $valore_adesso)
                        {
                            
    $valore_adesso=$id_ric['invoice_id'];
                        }
            }
            
    $config Am_Di::getInstance()->config;
            
    $config->saveValue(nfatt,(string)$valore_adesso);
            
    $f2 $form->addFieldset()->setLabel('Attuale numero di ricevuta');
            
    $f2->addText("nfatt",array('size'=>5))->setLabel(array("Ultimo numero ricevuta:"));   
       
            
    $f3 $form->addFieldset()->setLabel('Operazione di fine anno (ATTENZIONE! Usare con cura!)');
            
    $f3->addAdvCheckbox('fineanno')->setLabel('Avvia la procedura di fine anno');   
       
            
    $forms->addForm($form);
        }
    }
  6. valentinuzzi

    valentinuzzi aMember Pro Customer

    Joined:
    Oct 22, 2010
    Messages:
    13
    Thanks, that worked GREAT!!!!

    I need to solve another important step, hope it is possible ;)

    The user changes the saved vars, for example 'nfatt'.

    The Plugin checks the new value after the old one, and shows to the admin an alert box saying "Are you aware that this new value of 'nfatt' will do such and such things to the datatabase?"

    If he clicks OK then some actions are taken on the DB

    if he clicks CANCEL then the form is not saved and everything remains unchanged.

    So the question is, how to create such an alert box? A popup alert would be great but could be also an unformatted page, no mind....

    Thanks a lot Alex ;)
  7. valentinuzzi

    valentinuzzi aMember Pro Customer

    Joined:
    Oct 22, 2010
    Messages:
    13
    HI, any idea on the alert "confirmation" box?
  8. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,274
    You can do this in javascript.
    For example:
    PHP:
             $form->addScript()->setScript(<<<CUT
    $(function(){
    var nfatt_changed = false; 
    $('input[name="nfatt"]').change(function(){ nfatt_changed= true;})
    $('#save-0').click(function(e){
    if(confirm('Are you sure you want to change nfatt value?')){
    return true;
    }else{
    e.preventDefault();
    return false;
    }
    });

    });
    CUT
             );
  9. valentinuzzi

    valentinuzzi aMember Pro Customer

    Joined:
    Oct 22, 2010
    Messages:
    13
    Thanks really a lot, that worked GREAT!!!

    Now I understand how to catch and manipulate all the events I need.

    I have a couple of refinements I cannot understand where I'm wrong.

    1) In the main page of the admin panel I receive a red message "[ivaitaliana] plugin not configured". Even if I configure it (I save the form with all the datat inside) the alert remains there. What to do?

    2) By adding to my plugin the new
    class
    Am_Form_Setup_Ivaitaliana
    extends
    Am_Form_Setup
    which works great for me, a new empty button appears on the admin/setup page with this link associated "http://www.mywebsite.it/amember/admin-setup/index". Why is this appearing even if a new form is not generated and how can I make it disappear?

    Thank you very much for patience,
    Tiziano.
  10. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,274
    1. Change isConfigured function:
    PHP:
        public function isConfigured()
        {
            return (bool) 
    $this->getDi()->config->get('iva');
        }
    2. You have to move fields defenitions into form class here is updated code:
    PHP:
    <?php

    class Am_Form_Setup_Ivaitaliana extends Am_Form_Setup
    {
        public function 
    __construct()
        {
            
    parent::__construct('ivaitaliana_setup');
            
    $this->setTitle("Iva Italiana");
            
    $this->setComment('Qui aggiungi un tuo commento...');
        }
        public function 
    initElements()
        {
            
    $f1 $this->addFieldset()->setLabel('Attuale percentuale di IVA');
            
    $f1->addText("iva",array('size'=>5))->setLabel(array("Inserisci IVA senza %"));
     
            
    $id_ricevuta Am_Di::getInstance()->db->select("SELECT invoice_id FROM ?_invoice WHERE invoice_id "); 
            
    $valore_adesso=0;
            foreach(
    $id_ricevuta as $id_ric){
                if (
    $id_ric['invoice_id'] > $valore_adesso)
                        {
                            
    $valore_adesso=$id_ric['invoice_id'];
                        }
            }
            
    $config Am_Di::getInstance()->config;
            
    $config->saveValue('nfatt',(string)$valore_adesso);
            
    $f2 $this->addFieldset()->setLabel('Attuale numero di ricevuta');
            
    $f2->addText("nfatt",array('size'=>5))->setLabel(array("Ultimo numero ricevuta:"));   
       
            
    $f3 $this->addFieldset()->setLabel('Operazione di fine anno (ATTENZIONE! Usare con cura!)');
            
    $f3->addAdvCheckbox('fineanno')->setLabel('Avvia la procedura di fine anno');   
            
            
        }
        public function 
    afterSaveConfig(Am_Config $beforeAm_Config $after)
        {
            
    // get the plugin object iif necessary
            
    $plugin Am_Di::getInstance()->plugins_misc->loadGet('ivaitaliana');
            
    // run custom actions here
            // uncomment next line for quick debug
            // print_rre($after);
        
    }
    }
     
     
    class 
    Am_Plugin_Ivaitaliana extends Am_Plugin
    {
     
        public function 
    isConfigured()
        {
            return (bool) 
    $this->getDi()->config->get('iva');
        }
     
    }
  11. valentinuzzi

    valentinuzzi aMember Pro Customer

    Joined:
    Oct 22, 2010
    Messages:
    13
    Thanks a lot, everything was smooth, brilliant solutions ;) Thanks MAX

Share This Page