示例#1
0
 /**
  * Second step is to import clients and contacts
  * @return
  */
 public static function import_clients()
 {
     // Store the import progress
     $progress = get_option(self::PROGRESS_OPTION, array());
     // Suppress notifications
     add_filter('suppress_notifications', '__return_true');
     $total_records = 0;
     if (!isset($progress['clients_complete'])) {
         require_once SI_PATH . '/importers/lib/harvest/HarvestAPI.php';
         spl_autoload_register(array('HarvestAPI', 'autoload'));
         $api = new HarvestAPI();
         $api->setUser(self::$harvest_user);
         $api->setPassword(self::$harvest_pass);
         $api->setAccount(self::$harvest_account);
         $api->setRetryMode(HarvestAPI::RETRY);
         $api->setSSL(true);
         $progress_key = 'clients_import_progress';
         if (!isset($progress[$progress_key])) {
             $progress[$progress_key] = 0;
             update_option(self::PROGRESS_OPTION, $progress);
         }
         $result = $api->getClients();
         if (!$result->isSuccess()) {
             self::return_error(self::__('Client import error.'));
         }
         // Start importing the clients 20 at a time
         $total_records = count($result->data);
         // Break the array up into pages
         $paged_data = array_chunk($result->data, 20);
         $pages = count($paged_data);
         $total_imported = intval($total_records / $pages * $progress[$progress_key]);
         if ($progress[$progress_key] <= $pages) {
             $current_page = $paged_data[$progress[$progress_key]];
             foreach ($current_page as $client_id => $client) {
                 self::create_client($client);
             }
             $progress[$progress_key]++;
             update_option(self::PROGRESS_OPTION, $progress);
             // Return the progress
             self::return_progress(array('authentication' => array('message' => sprintf(self::__('Attempting to import %s clients...'), $total_records), 'progress' => 10 + $progress[$progress_key]), 'clients' => array('message' => sprintf(self::__('Imported about %s clients so far.'), $total_imported), 'progress' => intval($progress[$progress_key] / $pages * 100), 'next_step' => 'clients')));
         }
         // Mark as complete
         $progress['clients_complete'] = 1;
         update_option(self::PROGRESS_OPTION, $progress);
         // Complete
         self::return_progress(array('authentication' => array('message' => sprintf(self::__('Successfully imported %s clients...'), $total_records), 'progress' => 50), 'clients' => array('message' => sprintf(self::__('Imported %s clients!'), $total_records), 'progress' => 100, 'next_step' => 'contacts')));
     }
     // Completed previously
     self::return_progress(array('authentication' => array('message' => sprintf(self::__('Successfully imported %s clients already, moving on...'), $total_records), 'progress' => 50), 'clients' => array('progress' => 100, 'message' => sprintf(self::__('Successfully imported %s clients already.'), $total_records), 'next_step' => 'contacts')));
     // If this is needed something went wrong since json should have been printed and exited.
     return;
 }