/** * Import a single album. */ static function import_album(&$queue) { $messages = array(); // The queue is a set of nested associative arrays where the key is the album id and the // value is an array of similar arrays. We'll do a breadth first tree traversal using the // queue to keep our state. Doing it breadth first means that the parent will be created by // the time we get to the child. // Dequeue the current album and enqueue its children list($album, $tree) = each($queue); unset($queue[$album]); g1_import::debug(t('Dequeued album %album.', array('album' => $album))); foreach ($tree as $key => $value) { $queue[$album . '/' . $key] = $value; g1_import::debug(t('Enqueued album %album.', array('album' => $album . '/' . $key))); } // Special handling for the root album if ($album == '') { if (!self::map('', '', 'album')) { $album = item::root(); self::set_map($album->id, '', '', 'album'); } return $messages; } // Album names come in as /Folder1/Folder2/FolderX $pos = strrpos($album, '/'); if ($pos === false) { return $messages; } // Get FolderX into g1_album $parent = substr($album, 0, $pos); $g1_album = substr($album, $pos + 1); // Reduce parent to Folder2 $pos = strrpos($parent, '/'); if ($pos !== false) { $parent = substr($parent, $pos + 1); } // Skip already-existing albums if (self::map($g1_album, '', 'album')) { $messages[] = t('Skipping already existing album %album.', array('album' => $parent . '/' . $g1_album)); return $messages; } $album_id = self::map($parent, '', 'album'); if (!$album_id) { $messages[] = t('Album %name not found', array('name' => $parent)); return $messages; } g1_import::debug(t('Now importing album %album.', array('album' => $parent . '/' . $g1_album))); $albumDir = self::$album_dir; if (substr($albumDir, -1) != DIRECTORY_SEPARATOR) { $albumDir .= DIRECTORY_SEPARATOR; } $importDir = $albumDir . $g1_album . DIRECTORY_SEPARATOR; $parent = ORM::factory('item', $album_id); $album = ORM::factory('item'); $album->type = 'album'; $album->parent_id = $album_id; g1_import::set_album_values($album, $g1_album); try { $album->validate(); } catch (ORM_Validation_Exception $e) { throw new G1_Import_Exception(t('Failed to validate Gallery 1 album with name %name.', array('name' => $g1_album)), $e); } try { $album->save(); self::set_map($album->id, $g1_album, '', 'album'); } catch (Exception $e) { throw new G1_Import_Exception(t('Failed to import Gallery 1 album with name %name.', array('name' => $g1_album)), $e); } try { require_once 'Gallery1DataParser.php'; list($result, $items) = Gallery1DataParser::getPhotos($importDir); if ($result == null) { foreach ($items as $object) { if (isset($object->highlight) && $object->highlight == 1 && isset($object->highlightImage) && is_a($object->highlightImage, 'G1Img')) { $g1_path = $importDir . $object->highlightImage->name . '.' . $object->highlightImage->type; if (is_file($g1_path) && @copy($g1_path, $album->thumb_path())) { $album->thumb_height = $object->highlightImage->height; $album->thumb_width = $object->highlightImage->width; $album->thumb_dirty = false; $album->save(); } } } } } catch (Exception $e) { throw new G1_Import_Exception(t('Failed to copy thumb for album %name.', array('name' => $g1_album)), $e); } try { if (isset(self::$albums_hidden[$g1_album])) { access::deny(identity::everybody(), 'view', $album); } } catch (Exception $e) { throw new G1_Import_Exception(t('Failed to set access permission for hidden album %name.', array('name' => $g1_album)), $e); } return $messages; }