View difference between Paste ID: H2j5xeBk and XFm71Cji
SHOW: | | - or go back to the newest paste.
1
class PRMigration extends Migration {
2
  public function __construct() {
3
    parent::__construct();
4
    $this->map = new MigrateSQLMap($this->machineName,
5
        array(
6
          'id' => array(
7
          	'type' => 'int',
8
            'not null' => TRUE,
9
            'description' => 'Press Release ID',
10
          )
11
        ),
12
        MigrateDestinationNode::getKeySchema()
13
      );
14
15
    $query = Database::getConnection('default', 'secondary')
16
             ->select('releases', 'r')
17
             ->fields('r', array('id', 'title', 'released_at', 'synopsis', 'body',
18
             	 'never_post', 'position', 'writer', 'created_by', 'updated_by',
19
             	 'created_at', 'updated_at', 'subtitle', 'contact_id',
20
             	 'version', 'deleted_at'));
21
    
22
    $source_fields = array(
23
      'links' => t('Related links'),
24
      'source_images' => t('Image assets attached ot this press release.'),
25
    );
26
    $this->source = new MigrateSourceSQL($query, $source_fields, NULL, array('map_joinable' => FALSE));
27
    // Using the default Node destination class.
28
    $this->destination = new MigrateDestinationNode('press_release');
29
    
30
    ///////////////////////////////////////////////////////
31
    // Mapped fields
32
    ///////////////////////////////////////////////////////
33
    $file_args = MigrateFileFieldHandler::arguments(NULL, 'file_blob', FILE_EXISTS_RENAME);
34
    $this->addFieldMapping('field_image', 'source_images')
35
         ->arguments($file_args);
36
         
37
  }
38
  public function prepareRow($row) {
39
    // Pull in the associated images for this release.
40
    $source_db = Database::getConnection('default', 'secondary');
41
    $query = $source_db
42
             ->select('assets', 'a')
43
             ->fields('a', array('id', 'filename', 'content_type', 'alt', 'caption',
44
             	 'photo_credit', 'size', 'height', 'width', 'parent_id', 'thumbnail',
45
             	 'db_file_id', 'created_by', 'updated_by', 'created_at', 'updated_at'));
46
    $query->innerJoin('db_files', 'dbf', 'a.db_file_id = dbf.id');
47
    $query->addField('dbf', 'data');
48
    $query->leftJoin('assignments', 'assign', 'a.id = assign.asset_id');
49
    $query->condition('assign.category_type', 'Release', '=');
50
    $query->condition('assign.category_id', $row->id, '=');
51
    $results = $query->execute();
52
    
53
    $images = array();
54
    
55
    foreach ($results as $row) {
56
      $image_information = array(
57
        'path' => $row->filename,
58
        'alt' => $row->alt,
59
        'title' => $row->caption,
60
      	'contents' => $row->data,
61
      );
62
      $images[] = drupal_json_encode($image_information);
63
      
64
    }
65
    
66
    $row->source_images = $images;
67
    return TRUE;
68
  }
69
}
70
71
class PRAssetMigration extends Migration {
72
  public function __construct() {
73
    parent::__construct();
74
    
75
    $this->map = new MigrateSQLMap($this->machineName,
76
        array(
77
          'id' => array(
78
          	'type' => 'int',
79
            'not null' => TRUE,
80
            'description' => 'Asset ID',
81
          )
82
        ),
83
        MigrateDestinationNode::getKeySchema()
84
      );
85
      
86
    // Select all the fields from the 'assets' table, and join it with the related 'data' field in db_files.
87
    $source_db = Database::getConnection('default', 'secondary');
88
    $query = $source_db
89
             ->select('assets', 'a')
90
             ->fields('a', array('id', 'filename', 'content_type', 'alt', 'caption',
91
             	 'photo_credit', 'size', 'height', 'width', 'parent_id', 'thumbnail',
92
             	 'db_file_id', 'created_by', 'updated_by', 'created_at', 'updated_at'));
93
    $query->innerJoin('db_files', 'dbf', 'a.db_file_id = dbf.id');
94
    $query->addField('dbf', 'data');
95
    $count_query = $source_db->select('assets', 'a');
96
    $count_query->addExpression('COUNT(id)', 'cnt');
97
    
98
    // User the simplified count query so it doesn't have to do the join to get the count.
99
    $this->source = new MigrateSourceSQL($query, array(), $count_query, array('map_joinable' => FALSE));
100
    $this->destination = new MigrateDestinationFile(MigrateDestinationFile::options(FALSE, NULL, NULL, NULL, FALSE, TRUE));
101
    
102
    ///////////////////////////////////////////////////////
103
    // Mapped fields
104
    ///////////////////////////////////////////////////////
105
    $this->addSimpleMappings(array());
106
107
    $this->addFieldMapping('contents', 'data');
108
    $this->addFieldMapping('filename', 'filename')
109
         ->dedupe('file_managed', 'filename');
110
    $this->addFieldMapping('filemime', 'content_type');
111
    // Since we don't care about the history of the source files, use updated_at instead of created_at.
112
    $this->addFieldMapping('timestamp', 'updated_at');
113
    
114
    
115
    ///////////////////////////////////////////////////////
116
    // Unmapped Source fields
117
    ///////////////////////////////////////////////////////
118
    $this->addUnmigratedSources(array('db_file_id', 'created_by', 'updated_by', 'created_at'));
119
    
120
    ///////////////////////////////////////////////////////
121
    // Unmapped destination fields
122
    ///////////////////////////////////////////////////////
123
    $this->addUnmigratedDestinations(array('uri', 'path', 'uid'));
124
    
125
         
126
  }
127
}