Skip to content

Instantly share code, notes, and snippets.

@nitriques
Created June 20, 2014 19:31
Show Gist options
  • Save nitriques/aac750d2d37c38ca178f to your computer and use it in GitHub Desktop.
Save nitriques/aac750d2d37c38ca178f to your computer and use it in GitHub Desktop.

Revisions

  1. nitriques created this gist Jun 20, 2014.
    68 changes: 68 additions & 0 deletions import.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    <?php

    // You run it with php-cli
    // i.e. php-cli import.php

    // I usually run in on the server via ssh.
    // I also usually put my scripts into manifest (to protect them from http access)

    // This scripts imports data from a csv...


    // BOOTSTRAP SYMPHONY
    define('DOCROOT', str_replace('/manifest/pays', '', rtrim(dirname(__FILE__), '\\/') ));
    define('VERBOSE', 0);
    define('COUNTRIES_FILE', DOCROOT . '/manifest/pays/countries.csv');
    define('MUTEX_KEY', 'mutex');
    // section id
    define('SOURCE', 169);

    require_once(DOCROOT . '/symphony/lib/boot/bundle.php');
    require_once(DOCROOT . '/symphony/lib/core/class.cacheable.php');
    require_once(DOCROOT . '/symphony/lib/core/class.symphony.php');
    require_once(DOCROOT . '/symphony/lib/core/class.administration.php');
    require_once(DOCROOT . '/symphony/lib/toolkit/class.general.php');
    require_once(TOOLKIT . '/class.sectionmanager.php');
    require_once(TOOLKIT . '/class.entrymanager.php');

    // creates the DB
    Administration::instance();

    $handle = fopen(COUNTRIES_FILE, 'r');

    $x = 1;

    while ( ($csv = fgetcsv($handle) ) != NULL ) {
    if (saveEntry($csv, $x)) {
    $x++;
    }
    }

    fclose($handle);

    echo 'Done. Inserted ' . ($x-1) . ' countries.' . PHP_EOL;

    function saveEntry($data, $x) {
    $entry = EntryManager::create();
    $entry->set('section_id', SOURCE);

    if ($data[0] == 'CA' || $data[0] == 'US') {
    return false;
    }

    $entry->setData(1262, array('value' => $data[0], 'handle' => $data[0]));
    $entry->setData(1261, array(
    'value-en' => $data[1], 'handle-en' => Lang::createHandle($data[1]),
    'value-fr' => $data[2], 'handle-fr' => Lang::createHandle($data[2]),
    'value' => $data[2], 'handle' => Lang::createHandle($data[2])
    ));
    $entry->setData(1266, array('value' => $x));

    $res = $entry->commit();

    if(!$res) {
    throw new Exception('Could not create entry');
    }

    return $res;
    }