przewiń do treści

PHP, MySQL i MVC. Tworzenie witryn WWW opartych na bazie danych

Włodzimierz Gajda

define('BASE_PATH', 'openclipart-0.18-full/clipart/');

ImgPeer::doDeleteAll();
TagPeer::doDeleteAll();
ImgHasTagPeer::doDeleteAll();

$plks = get_file_list();
foreach ($plks as $plk) {
    $parsed = parse_filename($plk);

    $svg_filename = BASE_PATH . $parsed['dir'] . '/' . $parsed['filename'] .  '.svg';
    $txt_filename = BASE_PATH . $parsed['dir'] . '/' . $parsed['filename'] .  '.txt';
    $png_filename = BASE_PATH . $parsed['dir'] . '/' . $parsed['filename'] .  '.png';

    $dane_img = array(
        'slug' => string2slug($parsed['filename']),
        'svg' => file_get_contents($svg_filename)
    );
    if (file_exists($png_filename)) {
        $dane_img['png'] = file_get_contents($png_filename);
        $dane_img['has_preview'] = 1;
    } else {
        echo '*************** brak pliku png: ' . $plk . "\n";
    }
    $objImg = ImgPeer::insert($dane_img);

    if (file_exists($txt_filename)) {
        $txt_file = file_get_contents($txt_filename);
        $tags = parse_txt_file($txt_file);
    } else {
        $tags = array();
        echo '============error: ' . $txt_filename . "\n";
    }

    $tags = array_merge($tags, $parsed['folders']);
    $tags = array_map('strtolower', $tags);
    $tags = array_unique($tags);

    foreach ($tags as $tag) {
        $tag = str_replace('_', ' ', $tag);
        $tag = strtolower(trim($tag));

        $dane_tag = array(
            'tag' => $tag,
            'slug' => string2slug($tag)
        );

        $objTag = TagPeer::insertIfNotExists($dane_tag);

        try {
            $iht = new ImgHasTag;
            $iht->setImg($objImg);
            $iht->setTag($objTag);
            $iht->save();
        } catch (PropelException $e) {
        }

    }
}

foreach (ImgPeer::doSelect(new Criteria()) as $img) {
    $img->tworzMiniature();
}

Listing 23.1. Skrypt wypełniający bazę danych

Rozdział 23. Chmura tagów

listing-23-01.txt

Reklama

Szkolenia z Symfony 2.0