Created
August 18, 2014 10:07
-
-
Save SergiuSavva/5e8b3d39641ab1ce0f77 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class MigratePikolorArticleMigration extends PikolorBase { | |
public function __construct($arguments) { | |
parent::__construct($arguments); | |
$text_format = 'full_html'; | |
$this->language = 'ro'; | |
$this->file_source = drupal_realpath('public://') . '/posts'; | |
$node_options = MigrateDestinationNode::options($this->language, $text_format); | |
$this->destination = new MigrateDestinationNode('article', $node_options); | |
$this->source = new MigrateSourceSQL($this->query()); | |
$this->map = new MigrateSQLMap($this->machineName, | |
array( | |
'node_id' => array( | |
'type' => 'int', | |
'unsigned' => TRUE, | |
'not null' => TRUE, | |
) | |
), | |
MigrateDestinationNode::getKeySchema() | |
); | |
$this->addFieldMapping('title', 'title'); | |
$this->addFieldMapping('created', 'date'); | |
// Default user: Admin (uid = 1). | |
$this->addFieldMapping('uid') | |
->defaultValue(1); | |
// Default html format: full_html. | |
$this->addFieldMapping('body', 'body'); | |
$this->addFieldMapping('body:format') | |
->defaultValue('full_html'); | |
$this->addFieldMapping('field_image', 'images'); | |
$this->addFieldMapping('field_image:file_replace') | |
->defaultValue(FILE_EXISTS_REPLACE); | |
$this->addFieldMapping('field_image:preserve_files') | |
->defaultValue(TRUE); | |
$this->addFieldMapping('field_image:source_dir') | |
->defaultValue($this->file_source); // Legacy website directory from where to pull files. | |
if (module_exists('redirect')) { | |
$this->addFieldMapping('migrate_redirects', 'link'); | |
} | |
} | |
/** | |
* Generate query. | |
* @return SelectQueryInterface | |
*/ | |
public function query() { | |
try { | |
$query = Database::getConnection('default', 'pikolor') | |
->select('node', 'n') | |
->fields('n', array('title', 'link', 'date')) | |
->condition('type', 'posts') | |
->condition('fv.lang', $this->language, '=') | |
->condition('fv.system_name', 'post_content', '=') | |
->groupBy('n.id') | |
->orderBy('n.date'); | |
$query->join('fields_val', 'fv', 'fv.node_id = n.id'); | |
$query->addField('fv', 'val', 'body'); | |
$query->addField('fv', 'node_id', 'node_id'); | |
} catch (Exception $e) { | |
drupal_set_message($e->getMessage(), 'error'); | |
} | |
return $query; | |
} | |
public function prepareRow($row) { | |
// Always include this fragment at the beginning of every prepareRow() | |
// implementation, so parent classes can ignore rows. | |
if (parent::prepareRow($row) === FALSE) { | |
return FALSE; | |
} | |
//Get data for database. | |
$image_names = $this->get_images($row->node_id); | |
$row->images = array(); | |
if (!empty($image_names)) { | |
foreach ($image_names as $image_name) { | |
if (file_exists($this->file_source . '/' . $image_name . '.jpg')) { | |
$row->images[] = $image_name . '.jpg'; | |
} | |
else { | |
watchdog('Pikolor migrate', 'File doesn\'t exit : %filename', array('%filename' => $this->file_source . '/' . $image_name . '.jpg')); | |
} | |
} | |
} | |
return TRUE; | |
} | |
/** | |
* Helper function, get images from table 'fields_val'. | |
* @param null $id | |
* @return array|bool|mixed | |
*/ | |
protected function get_images($id = NULL) { | |
if (empty($id)) { | |
return FALSE; | |
} | |
$image_name = array(); | |
$query = Database::getConnection('default', 'pikolor') | |
->select('fields_val', 'fv') | |
->condition('node_id', $id, '=') | |
->condition('fv.system_name', 'post_photo', '=') | |
->range(0, 1); | |
$query->addField('fv', 'val', 'images'); | |
$result = $query->execute()->fetchField(); | |
// Decode to array. | |
if (!empty($result)) { | |
$image_name = json_decode($result); | |
} | |
return $image_name; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment