Example #1
0
 /**
  * Import data
  * @TODO write to tmp table for better record tracking
  */
 public function import()
 {
     $newData = array();
     $total = 0;
     if (!empty($_FILES['csvFile']['name']) && !empty($_FILES['csvFile']['tmp_name'])) {
         require_once 'csv.class.php';
         $csv = new CSV();
         $data = $csv->import($_FILES['csvFile']['tmp_name']);
         foreach ($data as $item) {
             $newData[] = $this->create($item, false);
             $total++;
         }
         $status = 'success';
         $message = 'Data was imported';
     } else {
         $status = 'failed';
         $message = 'File did not upload';
         $newData[] = array();
     }
     //$item = $item->toArray();
     //$data[] = $item;
     return $this->makeJson($status, $message, $total, $total, $newData);
 }
}
require_once $SYSTEM_ROOT . '/core/include/init.inc';
$GLOBALS['SQ_SYSTEM']->setCurrentUser($GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user'));
$assetid = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : 0;
$asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid, 'data_source_csv');
if (empty($asset)) {
    echo "You need to supply a CSV Data Source assetid as the second argument\n";
    print_usage();
    exit;
}
if ($_SERVER['argc'] < 4) {
    echo "You need to supply the path to CSV file as the third argument\n";
    print_usage();
    exit;
}
$filepath = $_SERVER['argv'][3];
require_once SQ_FUDGE_PATH . '/csv/csv.inc';
$GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
$csv = new CSV($filepath);
$csv->import();
$asset->setAttrValue('cached_content', $csv->values);
// overwrite the cache with the new content
$asset->setResultSet(array(), $asset->name);
$asset->getResultSet($asset->name);
$asset->saveAttributes();
// set last update time if forcibly required
$FORCE_UPDATE = isset($_SERVER['argv'][4]) ? $_SERVER['argv'][4] : 'n';
if (strtolower($FORCE_UPDATE) == 'y') {
    $asset->_updated();
}
$GLOBALS['SQ_SYSTEM']->restoreRunLevel();