public function save()
 {
     access::verify_csrf();
     $form = $this->_get_import_form();
     if ($form->validate()) {
         $albums_path = $form->configure_g1_import->albums_path->value;
         if (!is_file($albums_path) && file_exists("{$albums_path}/albums.php")) {
             $albums_path = "{$albums_path}/albums.php";
         }
         if (($g1_init_error = g1_import::is_valid_albums_path($albums_path)) == 'ok') {
             message::success(t('Gallery 1 path saved'));
             module::set_var('g1_import', 'albums_path', $albums_path);
             url::redirect('admin/g1_import');
         } else {
             $form->configure_g1_import->albums_path->add_error($g1_init_error, 1);
         }
     }
     $view = new Admin_View('admin.html');
     $view->content = new View('admin_g1_import.html');
     $view->content->form = $form;
     print $view;
 }
 static function import($task)
 {
     $start = microtime(true);
     g1_import::init();
     $stats = $task->get('stats');
     $done = $task->get('done');
     $total = $task->get('total');
     $completed = $task->get('completed');
     $mode = $task->get('mode');
     $queue = $task->get('queue');
     if (!isset($mode)) {
         $stats = g1_import::g1_stats();
         $stats['items'] = $stats['photos'] + $stats['movies'];
         unset($stats['photos']);
         unset($stats['movies']);
         $stats['fix'] = 1;
         $task->set('stats', $stats);
         $task->set('total', $total = array_sum(array_values($stats)));
         $completed = 0;
         $mode = 0;
         $done = array();
         foreach (array_keys($stats) as $key) {
             $done[$key] = 0;
         }
         $task->set('done', $done);
     }
     $modes = array('users', 'albums', 'items', 'comments', 'highlights', 'fix', 'done');
     while (!$task->done && microtime(true) - $start < 1.5) {
         if ($done[$modes[$mode]] >= $stats[$modes[$mode]]) {
             // Nothing left to do for this mode.  Advance.
             $mode++;
             $task->set('last_id', 0);
             $queue = array();
             // Start the loop from the beginning again.  This way if we get to a mode that requires no
             // actions (eg, if the G1 comments module isn't installed) we won't try to do any comments
             // queries.. in the next iteration we'll just skip over that mode.
             if ($modes[$mode] != 'done') {
                 continue;
             }
         }
         switch ($modes[$mode]) {
             case 'users':
                 $done['users'] = $stats['users'] - 1;
                 $task->status = t('Ignoring users (%count of %total)', array('count' => $done['users'], 'total' => $stats['users']));
                 break;
             case 'albums':
                 if (empty($queue)) {
                     if (count(g1_import::$tree) == 0) {
                         g1_import::g1_stats();
                     }
                     $task->set('queue', $queue = array('' => g1_import::$tree));
                 }
                 $log_message = g1_import::import_album($queue);
                 if ($log_message) {
                     $task->log($log_message);
                 }
                 $task->status = t('Importing albums (%count of %total)', array('count' => $done['albums'] + 1, 'total' => $stats['albums']));
                 break;
             case 'items':
                 if (empty($queue)) {
                     if (count(g1_import::$queued_items) == 0) {
                         g1_import::g1_stats();
                     }
                     $queuelen = 100;
                     $thisstart = $task->get('last_id', 0);
                     $nextstart = $thisstart + $queuelen;
                     $task->set('last_id', $nextstart);
                     $task->set('queue', $queue = array_splice(g1_import::$queued_items, $thisstart, $queuelen));
                 }
                 $log_message = g1_import::import_item($queue);
                 if ($log_message) {
                     $task->log($log_message);
                 }
                 $task->status = t('Importing photos (%count of %total)', array('count' => $done['items'] + 1, 'total' => $stats['items']));
                 break;
             case 'comments':
                 if (empty($queue)) {
                     if (count(g1_import::$queued_comments) == 0) {
                         g1_import::g1_stats();
                     }
                     $queuelen = 100;
                     $thisstart = $task->get('last_id', 0);
                     $nextstart = $thisstart + $queuelen;
                     $task->set('last_id', $nextstart);
                     $task->set('queue', $queue = array_splice(g1_import::$queued_comments, $thisstart, $queuelen));
                 }
                 $log_message = g1_import::import_comment($queue);
                 if ($log_message) {
                     $task->log($log_message);
                 }
                 $task->status = t('Importing comments (%count of %total)', array('count' => $done['comments'] + 1, 'total' => $stats['comments']));
                 break;
             case 'highlights':
                 if (empty($queue)) {
                     if (count(g1_import::$queued_highlights) == 0) {
                         g1_import::g1_stats();
                     }
                     $queuelen = 100;
                     $thisstart = $task->get('last_id', 0);
                     $nextstart = $thisstart + $queuelen;
                     $task->set('last_id', $nextstart);
                     $task->set('queue', $queue = array_splice(g1_import::$queued_highlights, $thisstart, $queuelen));
                 }
                 $log_message = g1_import::set_album_highlight($queue);
                 if ($log_message) {
                     $task->log($log_message);
                 }
                 $task->status = t('Album highlights (%count of %total)', array('count' => $done['highlights'] + 1, 'total' => $stats['highlights']));
                 break;
             case 'fix':
                 if (empty($queue)) {
                     if (count(g1_import::$albums_flat) == 0) {
                         g1_import::g1_stats();
                     }
                     $task->set('queue', $queue = 'dummy');
                 }
                 $log_message = g1_import::hotfix_all();
                 if ($log_message) {
                     $task->log($log_message);
                 }
                 $task->status = t('Final Hotfixing (%count of %total)', array('count' => $done['fix'] + 1, 'total' => $stats['fix']));
                 break;
             case 'done':
                 $task->status = t('Import complete');
                 $task->done = true;
                 $task->state = 'success';
                 break;
         }
         if (!$task->done) {
             $done[$modes[$mode]]++;
             $completed++;
         }
     }
     $task->percent_complete = 100 * ($completed / $total);
     $task->set('completed', $completed);
     $task->set('mode', $mode);
     $task->set('queue', $queue);
     $task->set('done', $done);
 }
 static function item_created($item)
 {
     g1_import::copy_matching_thumbnails_and_resizes($item);
 }
 /**
  * Import a single comment.
  */
 static function import_comment(&$queue)
 {
     $messages = array();
     if (count($queue) == 0) {
         //this case happens if more than one comment is found on one or more items
         $messages[] = t('Empty comments queue');
         return $messages;
     }
     $element = array_shift($queue);
     list($item, $comments) = each($element);
     g1_import::debug(t('Now importing %$comments comment(s) for item %$item', array('album' => $item, 'comments' => count($comments))));
     // Item names come in as FolderX/ItemX
     $pos = strrpos($item, '/');
     if ($pos === false) {
         $messages[] = t('Invalid item %item', array('item' => $item));
         return $messages;
     }
     // Get ItemX into g1_item
     $g1_item = substr($item, $pos + 1);
     // Get FolderX into g1_item
     $g1_album = substr($item, 0, $pos);
     if (self::map($g1_album, $g1_item, 'comment')) {
         return $messages;
     }
     $item_id = self::map($g1_album, $g1_item, 'item');
     if (empty($item_id)) {
         $messages[] = t('Could not find item %item', array('item' => $item));
         return;
     }
     foreach ($comments as $g1comment) {
         // Just import the fields we know about.  Do this outside of the comment API for now so that
         // we don't trigger spam filtering events
         $comment = ORM::factory('comment');
         $comment->author_id = identity::guest()->id;
         $comment->guest_name = utf8_encode(self::_decode_html_special_chars(trim($g1comment['name'])));
         $comment->guest_name or $comment->guest_name = (string) t('Anonymous coward');
         $comment->guest_email = '*****@*****.**';
         $comment->item_id = $item_id;
         $comment->text = utf8_encode(self::_decode_html_special_chars(trim($g1comment['commentText'])));
         $comment->state = 'published';
         $comment->server_http_host = utf8_encode(self::_decode_html_special_chars(trim($g1comment['IPNumber'])));
         try {
             $comment->save();
         } catch (Exception $e) {
             $messages[] = (string) new G1_Import_Exception(t('Failed to import comment for item: %item.', array('item' => $item)), $e);
             return $messages;
         }
         // Backdate the creation date.  We can't do this at creation time because
         // Comment_Model::save() will override it.
         db::update('comments')->set('created', utf8_encode(self::_decode_html_special_chars(trim($g1comment['datePosted']))))->set('updated', utf8_encode(self::_decode_html_special_chars(trim($g1comment['datePosted']))))->where('id', '=', $comment->id)->execute();
     }
     self::set_map($item_id, $g1_album, $g1_item, 'comment');
     return $messages;
 }