您的位置:首页 > 其它

Import multiple images to Drupal Image Field us...

2012-02-25 00:00 281 查看
Drupal 7.12, Migrate 2.3

class GarmentPictureMigration extends Migration {
public function __construct() {
parent::__construct(MigrateGroup::getInstance('plm'));
$this->description = t('Migrate Garment Pictures from PLM.');

$this->map = new MigrateSQLMap($this->machineName,
array(
'ID' => array(
'type' => 'varchar',
'size' => 'normal',
'not null' => TRUE,
'description' => 'User ID',
)
),
MigrateDestinationNode::getKeySchema()
);

$fields = array(
'ID' => 'ID',
'IMAGES' => 'Images'
);

$query = "select trim(style_id)||trim(color_id) ID
from PLM.SC_IMAGE WHERE MIGRATED=0 GROUP BY STYLE_ID,COLOR_ID";
$count_query = "SELECT COUNT(1) FROM (SELECT 1 FROM PLM.SC_IMAGE GROUP BY STYLE_ID,COLOR_ID)";

global $conf;
$this->source = new MigrateSourceOracle($conf['oracle_db'], $query, $count_query, $fields);
$this->destination = new MigrateDestinationNode('garment_picture');

$file_args = MigrateFileFieldHandler::arguments(array('source_field'=>'FILE_NAME'), 'file_blob', FILE_EXISTS_RENAME);
$this->addFieldMapping('field_images', 'IMAGES')
->arguments($file_args);
}

public function prepareRow($row) {
$images = array();

$s = oci_parse($this->source->getConnection(),
"SELECT rawtohex(id) id,file_name, file_blobfied_date
FROM PLM.SC_IMAGE WHERE trim(STYLE_ID) || trim(COLOR_ID) ='". $row->ID ."'");
oci_execute($s, OCI_DEFAULT);
while (oci_fetch($s)) {
$blob = oci_result($s, "FILE_BLOB")->load();
$data = array(
'path' => oci_result($s, "ID").'.jpg',
'alt' => oci_result($s, "FILE_NAME"),
'title' => oci_result($s, "FILE_NAME"),
'contents' => $blob,
);
$images[] = $data;
$row->FILE_NAME = oci_result($s, "FILE_NAME");
}
$row->IMAGES = $images;

return TRUE;
}

//修改MigrateFileFieldHandler, modules/migrate/plugins/destinations/fields.inc 275行
public function prepare($entity, array $field_info, array $instance, array $values) {
$destination_dir = $this->destinationDir($field_info, $instance);
file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
foreach($values as $value){
$destination_file = file_stream_wrapper_uri_normalize($destination_dir . "/" . str_replace('/', '-', $value['path']));
if (!file_exists($destination_file)) {
try {
$return = (bool) file_put_contents($destination_file, $value['contents']);
}
catch (Exception $exception) {
throw new MigrateException("Can't write file to: $destination_file");
return FALSE;
}
}
}
}

//修改MigrateFileFieldHandler, modules/migrate/plugins/destinations/fields.inc 357行
protected function buildFileArray($entity, array $field_info, array $instance, $migration, $arguments, $value) {
if (is_array($value) || $value{0} == '{') {
$properties = is_array($value) ? $value : drupal_json_decode($value);
$path = $properties['path'];
// Properties passed in with the image override any set via arguments
if (!empty($properties['path'])) {
$arguments['source_path'] = $properties['path'];
}
if (!empty($properties['alt'])) {
$arguments['alt'] = $properties['alt'];
}
if (!empty($properties['title'])) {
$arguments['title'] = $properties['title'];
}
if (!empty($properties['description'])) {
$arguments['description'] = $properties['description'];
}
if (!empty($properties['display'])) {
$arguments['display'] = $properties['display'];
}
}
else {
$path = $value;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐