Importing users

Discussion in 'aMember Pro v.4' started by piratepete, Oct 17, 2012.

  1. piratepete

    piratepete aMember Pro Customer

    Joined:
    Apr 14, 2009
    Messages:
    23
    Alexander, question regarding importing users who join or renew offline using the import feature in aMember V4.

    We allow members to join or renew using cheques offline as well as online payments. I'm looking to make it easier to administer our system by importing offline members. However, I notice your system checks the usernames to see if the member already exists. The problem with this is that members joining offline don't have usernames and those renewing might have changed their usernames, as we allow them to do this.

    We use membership numbers, which are unique to each member. Is there a way to get the system to check this field instead? This number never changes, and new members joining offline are assigned a number as soon as they join.

    Many thanks,

    Peter
  2. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    Peter, I believe it could be easier to write a PHP script for your particular case. From aMember side there will be a few lines of code, but it will work with your file format and import will be instant. Let me know if you need details and if you have programming skills.
  3. piratepete

    piratepete aMember Pro Customer

    Joined:
    Apr 14, 2009
    Messages:
    23
    Many thanks, Alex. I'd love the details, and yes, I have PHP programming skills.

    Peter
  4. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    it will be something like that (needs testing and tweaking)

    put it into amember/application/default/plugins/misc/custom-import.php
    enable custom-import plugin at aMember Cp -> Setup -> Plugins
    try to access http://yoursite.com/amember/admin-custom-import

    PHP:
    class Am_Plugin_CustomImport extends Am_Plugin {}
     
    class 
    AdminCustomImportController extends Am_Controller
    {   
        protected 
    $lines_imported 0;
     
        public function 
    checkAdminPermissions(Admin $admin)
        {
            return 
    $admin->hasPermission(Am_Auth_Admin::PERM_IMPORT);
        }
     
        function 
    indexAction()
        {
            
    // here we load file from predefined location - amember/data/custom-import.csv
            
    $f fopen(DATA_DIR '/custom-import.csv''r');
            if (!
    $f) throw new Am_Exception_InputError("No custom-import.csv uploaded");
       
            
    $csv_fields = array(
                  
    => null// first line is record number, ignore it
                  
    => 'customid'// second field is your internal userid,
                  
    => 'amount'// payment amount
                  
    => 'refid'// payment reference id
                  
    => 'email'// custom email
            
    );
     
            
    $lines 0// count lines
            
    while ($line fgets($f))
            {
                  
    $lines++;
                  
    $line preg_split('/\;/'$line); // fields must be ; separated
                  
    if (count($line) < 2) continue; // skip empty lines
                  
    $record = array();
                  foreach (
    $csv_fields as $k => $v)
                      if (
    $v)
                          
    $record[$v] = $line[$k];
                  
    // we now pass $record as array('customid'=>'...', 'amount' => '...', 'refid' => '...', 'email' => '...');
                  
    $this->processRecord($record);
            }
            
    // now display success
            
    $this->view->title "Custom Import";
            
    $this->view->content("$lines lines processed, {$this->lines_imported} lines imported.");
            
    $this->view->display('admin/layout.phtml');
        }
     
        function 
    processRecord(array $record)
        {
                if (!
    $record['customid']) { print "Empty customid, skip line<br />"; return; }
                
    // try find a user record by customid (it must be a SQL field added via aMember CP -> Add Fields)
                
    $user $this->getDi()->userTable->findFirstBy(array('customid' => $record['customid']));
                
    // if user is not found, search by email address
                
    if (!$user)
                      
    $user $this->getDi()->userTable->findFirstByEmail($record['email']);
                
    // if user is not find, create new one
                
    if (!$user) {
                      
    $user $this->getDi()->userTable->createRecord();
                      
    $user->email $record['email'];
                      
    $user->generateLogin();
                      
    $user->generatePassword();
                      
    $user->insert();
                }
                
    // load product #11 to insert into invoice
                
    $product $this->getDi()->productTable->load(11); // or load product based on $record          
                // now create a new pending invoice
                
    $invoice $this->getDi()->invoiceTable->createRecord();
                
    $invoice->setUser($user);
                
    $invoice->paysys_id 'offline'// or something else based on $record
                
    $invoice->add($product);
                
    $invoice->calculate();
                
    $invoice->insert();
                
    // now we add payment record for invoice
                
    $payplugin $this->getDi()->plugins_payment->loadGet($invoice->paysys_id);
                
    $tr = new Am_Paysystem_Transaction_Manual($payplugin);
                
    $tr->setAmount($record['amount']);
                
    $tr->setReceiptId($record['refid']);
                
    $invoice->addPayment($tr);
                
    $this->lines_imported++;
        }
    }
  5. piratepete

    piratepete aMember Pro Customer

    Joined:
    Apr 14, 2009
    Messages:
    23
    Alex, that looks great, thank you!

    Now, I assume I can add more fields into the array, such as address details & custom fields already added to the database?

    This is perfect for me to work on a final code...many thanks for your help!
  6. alex

    alex aMember Pro Customer Staff Member

    Joined:
    Jan 24, 2004
    Messages:
    6,021
    Sure you can.
    Just add more lines like that
    $user->email = $record['email'];
  7. piratepete

    piratepete aMember Pro Customer

    Joined:
    Apr 14, 2009
    Messages:
    23
    That is brilliant, thank you Alex!

Share This Page