Custom plugin issues

Discussion in 'aMember Pro v.4' started by leetspeak, Jun 27, 2012.

  1. leetspeak

    leetspeak New Member

    Joined:
    Apr 9, 2012
    Messages:
    21
    Hi, i followed the admin-plugin-maker wizard and everything seemed to go well, i could add the plugin and then add the integration via 'protect content' without any issues. However when i manually create a new user i get an error that just says 'The database has encountered a problem, please try again later.'

    If i remove the plugin i can then create users without any issues.

    Note that i am using the same database amember is on with a custom table that i want the active user data written to.

    my plugin data is as follows:

    Code:
    class Am_Protect_Actives extends Am_Protect_Databased
    {
        const PLUGIN_DATE = '$Date$';
        const PLUGIN_REVISION = [EMAIL]'@@VERSION@@'[/EMAIL];
        protected $guessTablePattern = "am_custom_actives";
        protected $guessFieldsPattern = array(
            'user_id','pw',    
        );
        protected $groupMode = Am_Protect_Databased::GROUP_NONE;
        public function afterAddConfigItems(Am_Form_Setup_ProtectDatabased $form)
        {
            parent::afterAddConfigItems($form);
            // additional configuration items for the plugin may be inserted here
        }
        
        public function getPasswordFormat()
        {
            return SavedPassTable::PASSWORD_PLAIN;
        }
        /**
         * Return record of customer currently logged-in to the
         * third-party script, or null if not found or not logged-in
         * @return Am_Record|null
         */
        public function getLoggedInRecord()
        {
            // for single-login must return 
        }
        public function loginUser(Am_Record $record, $password)
        {
            // login user to third-party script
        }
        public function logoutUser(User $user)
        {
            // logout user from third-party script
        }
        public function createTable()
        {
            $table = new Am_Protect_Table($this, $this->getDb(), '?_am_custom_actives', 'id');
            $table->setFieldsMapping(array(
                array(Am_Protect_Table::FIELD_ID, 'user_id'),
                array(Am_Protect_Table::FIELD_PASS, 'pw'),
                
            ));
                    
            return $table;
        }
        
        function getReadme()
        {
            return <<<CUT
        actives README
        
    CUT;
        }
    }
    
  2. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Please check aMember CP -> Error log there should be more info about error that was generated.
  3. leetspeak

    leetspeak New Member

    Joined:
    Apr 9, 2012
    Messages:
    21
    here is what is in the logs, something missing? no field name?

    Code:
    Unknown column '' in 'where clause' at /home/leetspea/public_html/test/cms/library/Am/Record.php line 601(1054) in query: SELECT * FROM am_custom_actives WHERE ``='sdflkjsdf' LIMIT 1
    
  4. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    You must specify login field in your plugin.
    Create field for login in your mysql table, then edit plugin and change this function:
    PHP:
     public function createTable()
        {
            
    $table = new Am_Protect_Table($this$this->getDb(), '?_am_custom_actives''id');
            
    $table->setFieldsMapping(array(
                array(
    Am_Protect_Table::FIELD_ID'user_id'),
                array(
    Am_Protect_Table::FIELD_PASS'pw'),
                array(
    Am_Protect_Table::FIELD_LOGIN,  'login'),
                
            ));
                    
            return 
    $table;
        }
  5. leetspeak

    leetspeak New Member

    Joined:
    Apr 9, 2012
    Messages:
    21
    Worked like a charm, thanks for that
  6. leetspeak

    leetspeak New Member

    Joined:
    Apr 9, 2012
    Messages:
    21
    another related question, when i remove someones access by deleting the invoice or access they are not being removed from the integration database table, should this occur? if not, how do i make it happen?

    The manual for custom integrations says

    "When their subscription expires, users will be automatically disabled or (if configured) removed from third-party database. "

    how do i configure the remove option?
  7. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Add this to plugin:
    PHP:
    function onSubscriptionDeleted(Am_Event_SubscriptionDeleted $event){
       
    $found $this->getTable()->findByAmember($event->getUser());
            if (!
    $found || !$this->canRemove($found))
                return;
            if(
    $this->calculateGroups($event->getUser())) return; 
            if (
    $this->getConfig('remove_users'))
            {
                
    $this->_table->removeRecord($found);
            } elseif (!
    $this->isBanned($found)) { 
                
    $this->_table->disableRecord($found$this->calculateGroups(nulltrue));
            }

    }
  8. flyinjs

    flyinjs New Member

    Joined:
    Aug 29, 2006
    Messages:
    7
    Alex,

    Hello...I have been trying to get in touch with you. I need help with a custom plugin for a payment system and integrate it with amember. I do not have the knowledge to create this on my own. Can you help? I have a help ticket number, but it disapeared #QXR-77923-674

    John Slater
    flyinjs@epix.net
  9. leetspeak

    leetspeak New Member

    Joined:
    Apr 9, 2012
    Messages:
    21
    Thanks again Alex, works like a charm, next problem though, i setup a single group plugin and it works fine, puts the group_id into my group_id on the integrated table, although, when i setup multi groups its not passing the group_id in, the field is set to varchar(10) and everything else goes in, just not the group_id. latest plugin below:

    Code:
    <?php
     class Am_Protect_Tsactives extends Am_Protect_Databased
    {
        const PLUGIN_DATE = '$Date$';
        const PLUGIN_REVISION = [EMAIL]'@@VERSION@@'[/EMAIL];
        protected $guessTablePattern = "am_custom_actives";
        protected $guessFieldsPattern = array(
            'user_id','server_id','pw','login','email','name_f',    
        );
        protected $groupMode = Am_Protect_Databased::GROUP_MULTI;
        public function afterAddConfigItems(Am_Form_Setup_ProtectDatabased $form)
        {
            parent::afterAddConfigItems($form);
            // additional configuration items for the plugin may be inserted here
        }
        
        public function getPasswordFormat()
        {
            return SavedPassTable::PASSWORD_MD5;
        }
        /**
         * Return record of customer currently logged-in to the
         * third-party script, or null if not found or not logged-in
         * @return Am_Record|null
         */
        public function getLoggedInRecord()
        {
            // for single-login must return 
        }
        public function loginUser(Am_Record $record, $password)
        {
            // login user to third-party script
        }
        public function logoutUser(User $user)
        {
            // logout user from third-party script
        }
        public function createTable()
        {
            $table = new Am_Protect_Table($this, $this->getDb(), '?_am_custom_actives', 'id');
            $table->setFieldsMapping(array(
                array(Am_Protect_Table::FIELD_ID, 'user_id'),
                array(Am_Protect_Table::FIELD_GROUP_ID, 'server_id'),
                array(Am_Protect_Table::FIELD_PASS, 'pw'),
                array(Am_Protect_Table::FIELD_LOGIN, 'login'),
                array(Am_Protect_Table::FIELD_EMAIL, 'email'),
                array(Am_Protect_Table::FIELD_NAME_F, 'name_f'),
                
            ));
                    
            return $table;
        }
        
        public function getAvailableUserGroupsSql()
        {
            return "SELECT
                server_id as id,
                server_name as title,
                NULL as is_banned, #must be customized
                NULL as is_admin # must be customized
                FROM ?_am_custom_servers";
        }
        
        function getReadme()
        {
            return <<<CUT
        tsactives README
        
    CUT;
        }
    }
    
  10. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
  11. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    leetspeak,
    Default usage of Am_Protect_Databased::GROUP_MULTI is when you have separate table where groups are stored.
    In this situation you should describe that table using Am_Protect_Table::setFieldsMapping function.
    If you store multiple groups in one fileld, you must override Am_Protect_Table::setGroups and Am_Protect_Table::getGroups
    above two methods are used to save groups and to get groups from user record.
    First remove GROUP_ID field defenition from your createTable method.
    At the bottom of your plugin create new class:
    PHP:
    class Am_Protect_Table_Tsactives extends Am_Protect_Table{

        function 
    setGroups(Am_Record $record$groups){
            
    // Let's say you will separate groups by comma, 
            
    $record->updateQuick('server_id'join(','$groups));
        }

        function 
    getGroups($record){
            return 
    split(','$record->server_id);
        }

    }

    then in createTable method change class name from Am_Protect_Table to Am_Protect_Table_Tsactives
  12. leetspeak

    leetspeak New Member

    Joined:
    Apr 9, 2012
    Messages:
    21
    thanks again :)

Share This Page