コード例 #1
0
 public function importData($results, $sessionKey = null)
 {
     $firstRow = reset($results);
     /*
      * Validation
      */
     if ($this->auto_create_lists && !array_key_exists('lists', $firstRow)) {
         throw new ApplicationException('Please specify a match for the Lists column.');
     }
     /*
      * Import
      */
     foreach ($results as $row => $data) {
         try {
             if (!($email = array_get($data, 'email'))) {
                 $this->logSkipped($row, 'Missing email address');
                 continue;
             }
             /*
              * Find or create
              */
             $subscriber = Subscriber::firstOrNew(['email' => $email]);
             $subscriberExists = $subscriber->exists;
             /*
              * Set attributes
              */
             $except = ['lists'];
             foreach (array_except($data, $except) as $attribute => $value) {
                 $subscriber->{$attribute} = $value ?: null;
             }
             $subscriber->forceSave();
             if ($listIds = $this->getListIdsForSubscriber($data)) {
                 $subscriber->subscriber_lists()->sync($listIds, false);
             }
             /*
              * Log results
              */
             if ($subscriberExists) {
                 $this->logUpdated();
             } else {
                 $this->logCreated();
             }
         } catch (Exception $ex) {
             $this->logError($row, $ex->getMessage());
         }
     }
 }