Example #1
0
 public function copyChart($src)
 {
     $chart = new Chart();
     // new id
     $chart->setId($this->getUnusedRandomId());
     // but the rest remains the same
     $chart->setUser($src->getUser());
     $chart->setTitle($src->getTitle() . ' (' . __('Copy') . ')');
     $chart->setMetadata(json_encode($src->getMetadata()));
     $chart->setTheme($src->getTheme());
     $chart->setLocale($src->getLocale());
     $chart->setType($src->getType());
     $chart->setCreatedAt(time());
     $chart->setLastModifiedAt(time());
     $chart->setLastEditStep(3);
     // we need to copy the data, too
     $chart->writeData($src->loadData());
     $chart->save();
     return $chart;
 }
$user = new User();
$user->setEmail('test');
$pwd = !empty($dw_config['testuser_pwd']) ? $dw_config['testuser_pwd'] : 'test';
$user->setPwd(hash_hmac('sha256', $pwd, DW_AUTH_SALT));
$user->setRole('editor');
$user->setCreatedAt(time());
$user->save();
$themes = DatawrapperTheme::all(true);
foreach (glob("../test/test-charts/*.json") as $test) {
    $config = json_decode(file_get_contents($test), true);
    $data = $config['_data'];
    unset($config['_data']);
    unset($config['_sig']);
    if (isset($config['_id'])) {
        $config['metadata']['describe']['__test_id'] = $config['_id'];
        unset($config['_id']);
    }
    unset($config['id']);
    foreach ($themes as $theme) {
        $chart = new Chart();
        $chart->setId(ChartQuery::create()->getUnusedRandomId());
        $chart->setUser($user);
        $chart->unserialize($config);
        $chart->writeData($data);
        $chart->setTheme($theme['id']);
        $chart->setLastEditStep(5);
        $chart->save();
    }
}
print "To see the charts, please visit\n";
print 'http://' . $GLOBALS['dw_config']['domain'] . '/admin/charts/' . $user->getId() . "\n";