/**
  * Method to subscribe a user to the newsletter. Implements the parent
  * abstract method.
  *
  * Hint: $subscriber_info array description:
  *
  * $subscriber_info[user_id] - User id of the user to subscribe.
  * $subscriber_info[username] - Username of the user to subscribe.
  * $subscriber_info[name] - Name of the user to subscribe.
  * $subscriber_info[email] - Email of the user to subscribe.
  * $subscriber_info[gid] - Group identifier in which the user will be subscribed.
  *
  * @access	public
  * @param   array $subscriber_info Subscriber info based on newsletter type.
  * @param   array $attributes Array of addictional attributes for subscription
  * @return  true if successfully subscribed. false if something wrong.
  * @since	0.6
  */
 function subscribe($subscriber_info, $attributes = null)
 {
     if (is_null($attributes) || !is_array($attributes)) {
         $attributes = array();
     }
     jincimport('utility.jincjoomlahelper');
     jincimport('utility.servicelocator');
     $servicelocator = ServiceLocator::getInstance();
     $logger = $servicelocator->getLogger();
     if (!isset($subscriber_info['user_id'])) {
         $this->setError('COM_JINC_ERR008');
         return false;
     }
     $news_id = $this->get('id');
     $user_id = $subscriber_info['user_id'];
     $user_info = JINCJoomlaHelper::getUserInfo($user_id);
     if (empty($user_info)) {
         $this->setError('COM_JINC_ERR008');
         return false;
     }
     $user =& JFactory::getUser($user_id);
     if (!$user->authorize('jinc.subscribe', 'com_jinc.newsletter.' . $news_id)) {
         $this->setError('COM_JINC_ERR006');
         return false;
     }
     if (!$this->checkMandatoryAttributes($attributes)) {
         $this->setError('COM_JINC_ERR048');
         return false;
     }
     if ($this->isSubscribed($subscriber_info) > 0) {
         $this->setError('COM_JINC_ERR015');
         return false;
     }
     jincimport('utility.jinchelper');
     $ip = JINCHelper::getRealIpAddr();
     $query = 'INSERT IGNORE INTO #__jinc_subscriber ' . '(news_id , user_id, datasub, ipaddr) ' . 'VALUES (' . (int) $news_id . ', ' . (int) $user_id . ', ' . 'now(), INET_ATON(\'' . $ip . '\'))';
     $logger->debug('PrivateNewsletter: Executing query: ' . $query);
     $dbo =& JFactory::getDBO();
     $dbo->setQuery($query);
     if (!$dbo->query()) {
         $this->setError('COM_JINC_ERR009');
         return false;
     }
     $sub_id = $dbo->insertid();
     $this->insertAttributeOnSubscription($sub_id, $attributes);
     $this->sendWelcome($user_info, $attributes);
     $dispatcher =& JDispatcher::getInstance();
     $params = array('news_id' => $this->get('id'), 'news_name' => $this->get('name'), 'subs_name' => $user_info['name'], 'news_notify' => $this->get('notify'));
     $result = $dispatcher->trigger('jinc_subscribe', $params);
     return true;
 }
 /**
  * The newsletter importer. It imports newsletter subscribers from a CSV file.
  *
  * @access	public
  * @param	integer $newsletter a newsletter object.
  * @param	string  $csvfile_name the CSV file name.
  * @return  array containing import results.
  * @since	0.6
  * @see     Newsletter
  */
 function ImportFromCSV($newsletter, $csvfile_name)
 {
     jincimport('utility.jincjoomlahelper');
     jincimport('utility.servicelocator');
     $servicelocator = ServiceLocator::getInstance();
     $logger = $servicelocator->getLogger();
     if (!($handle = @fopen($csvfile_name, "r"))) {
         $logger->finer('NewsletterImporter: unable to open ' . $csvfile_name);
         return false;
     }
     $result = array();
     while (($data = fgetcsv($handle, $this->_LINE_MAX_LENGTH, ",")) !== FALSE) {
         $logger->finer('NewsletterImporter: importing ' . implode(', ', $data));
         $info = $newsletter->getSubscriptionInfo();
         $subscriber_info = array();
         $attributes = array();
         for ($i = 0; $i < count($info); $i++) {
             $prefix = substr($info[$i], 0, 5);
             if ($prefix == 'attr_') {
                 $suffix = substr($info[$i], 5);
                 $attributes[$suffix] = isset($data[$i]) ? $data[$i] : '';
             } else {
                 $subscriber_info[$info[$i]] = $data[$i];
             }
         }
         $sub_result = array();
         $sub_result['data'] = implode(', ', $subscriber_info);
         switch ($newsletter->getType()) {
             case NEWSLETTER_PUBLIC_NEWS:
                 $subscriber_info['noptin'] = true;
                 break;
             case NEWSLETTER_PRIVATE_NEWS:
                 $user_id = $subscriber_info['user_id'];
                 $user_info = JINCJoomlaHelper::getUserInfo($user_id);
                 if (empty($user_info)) {
                     $user_info = JINCJoomlaHelper::getUserInfoByUsername($user_id);
                     if (empty($user_info)) {
                         $user_info = JINCJoomlaHelper::getUserInfoByUsermail($user_id);
                         if (!empty($user_info)) {
                             $subscriber_info['user_id'] = $user_info['id'];
                         }
                     } else {
                         $subscriber_info['user_id'] = $user_info['id'];
                     }
                 }
                 break;
             default:
                 break;
         }
         if ($newsletter->subscribe($subscriber_info, $attributes)) {
             $sub_result['result'] = 'OK';
         } else {
             $sub_result['result'] = $newsletter->getError();
         }
         array_push($result, $sub_result);
     }
     fclose($handle);
     return $result;
 }