/**
  * Handle the event that is generated after a customer is saved in Magento (JSON-RPC)
  *
  * @param array $arguments
  *
  * @return int
  */
 public function mageCustomerSaveAfter($arguments = array())
 {
     // Check if this call is valid
     if (!isset($arguments['customer'])) {
         return false;
     }
     if (!isset($arguments['customer']['email'])) {
         return false;
     }
     // Fetch the right variables
     $customer = $arguments['customer'];
     if (isset($customer['addresses'][0][0])) {
         $address = $customer['addresses'][0][0];
         unset($customer['addresses']);
     } else {
         $address = array();
     }
     // Try to load the user through the Joomla! ID stored in Magento
     if (isset($customer['joomla_id'])) {
         $user = JFactory::getUser();
         $user->load($customer['joomla_id']);
     }
     // Try to load the user through its email-address
     if (!isset($user) || !isset($user->id) || !$user->id > 0) {
         $user = $this->getUser()->loadByEmail($customer['email']);
     }
     // Encrypt the user-password before continuing
     if (isset($customer['password'])) {
         $customer['password'] = MageBridge::decrypt($customer['password']);
     }
     // Start building the data-input for creating the Joomla! user
     if (!empty($customer['name']) && !empty($customer['email'])) {
         $data = array();
         // Include the received email
         if (!empty($customer['email'])) {
             $data['email'] = $customer['email'];
         }
         // Include the real name
         $data['name'] = $this->getRealname($user, $customer);
         // Include the username
         $data['username'] = $this->getUsername($user, $customer);
         // Set the firstname and lastname
         $data['magebridgefirstlast'] = array('firstname' => $customer['firstname'], 'lastname' => $customer['lastname']);
         // Include the password
         if (!empty($customer['password'])) {
             $data['password'] = $customer['password'];
             $data['password2'] = $customer['password'];
         }
         // Activate this account, if it's also activated in Magento
         if (isset($customer['is_active'])) {
             if ($customer['is_active'] == 1) {
                 $data['activation'] = '';
                 $data['block'] = 0;
             } else {
                 $data['block'] = 1;
             }
         }
         // Add the usergroup ID to this user (based upon groups configured in #__magebridge_usergroups)
         $usergroups = is_array($user->groups) ? $user->groups : array();
         $customer['usergroup'] = MageBridgeUserHelper::getJoomlaGroupIds($customer, $usergroups);
         //MageBridgeModelDebug::getInstance()->trace("Customer group", $customer['usergroup']);
         if (!empty($customer['usergroup'])) {
             $user->groups = $customer['usergroup'];
         }
         // If this user did not exist yet, create it
         if ($this->params->get('autocreate', 1) == 1 && ($user == false || $user->id == 0)) {
             $user = $this->getUser()->create($data, true);
             if (is_object($user) && $user->id > 0) {
                 // Save the user through the Profile Connectors
                 $profile = new MageBridgeConnectorProfile();
                 $profile->onSave($user, $customer, $address);
                 // Return the new user-ID
                 return $user->id;
             } else {
                 return 3;
             }
         }
         // Bind the data to the current user
         if ($user->bind($data) == false) {
             return false;
         }
         // Save the user
         if ($user->save() == false) {
             return false;
         }
         // Save the user through the Profile Connectors
         $profile = new MageBridgeConnectorProfile();
         $profile->onSave($user, $customer, $address);
         // Run the newsletter plugins
         if (isset($customer['is_subscribed'])) {
             JPluginHelper::importPlugin('magebridgenewsletter');
             JFactory::getApplication()->triggerEvent('onNewsletterSubscribe', array($user, (bool) $customer['is_subscribed']));
         }
         // Return the user ID for convenience
         return $user->id;
     }
     return false;
 }