Import

Import data

Arguments

$import
string id or name of the import

Example

Import data from file

$this->get('import', $importId)->importFile($myFile);

Create new upload and import data in background in 100 row slices, 4 page/second, starting from second row:

$Upload = $this->get('import', $importId)->newUpload();

$Upload->setFilename($myFile)
    ->setAsync(true)
    ->setLimit(100)
    ->setThrottle(250)
    ->setStart(2)
    ->importData();

Set data from variable:

$Upload->setData($csvData)
    ->setAsync(true)
    ->setLimit(100)
    ->importData();

All methods

Import

Function return  Description
getId() string Import id
getTitle() string Visible title
getDescription() string Additonal description
getDocumentation() string Longer documentation.
Upload data return  Description
importFile($file) ImportUpload Create new upload and import given file.
newUpload() ImportUpload Create new upload.
getUpload($id) ImportUpload Get existing upload.
getUploads() ImportUpload[] Get previous uploads.

Variables

$file
string: path to file
array: PHP file upload info

ImportUpload

Uploading of given file.

Upload info return  Description
getId() string Upload id.
getImport() Import Import object the upload belongs to.
getPreference($name) mixed Get Import preference.
Upload data return  Description
setFile($file) this Set file to upload.
getFilename() string Get the name of the file that is being uploaded.
setData($data) string Import data in CSV format.
importData() this Execute upload file handling.
getData() array Raw data.
getDataMapped() array Mapped data (preferences+script).
setFeedback($bool) this Set page save feedback flag.
getFeedback() bool Set page save feedback flag.
setTest() this Set test flag.
getTest() bool Return test flag.
isTest() bool Return true, if test is on.

Define how much resources the uploading of the file takes.

Async and throttle return  Description
setAsync() this Set async flag. true=Run in backround, false=run now, auto=make in background, if data more than limit.
getAsync() bool Return async flag.
isAsync() bool Return true, if uses async.
setThrottle($msecs) this Set time to use for one line (max rows to handle per second)
getThrottle() int Get seconds to wait before next slice.
Slice return  Description
setLimit($int) this Set number of rows to handle in one slice.
getLimit() int Get number of rows to handle in one slice.
setStart($int) this Set row that upload is started from (1-based)
getStart() int Get start row number.
setEnd($int) this Set row that upload is ended in (1-based)
getEnd() int Get end row number.
getTotal() int Total number of rows in file.
isMore() bool Return TRUE, if there is more rows to handle after this slice.
rowsLeft() int Get number of rows left from the beginnin of this slice.
countRows() int Number of rows handled in this slice.
setDelay($secs) this Set time to wait before next slice.
getDelay() int Get seconds to wait before next slice.

Import PHP script

In import, you can modify PHP script that is run for every row after it has been mapped against import preferences.

Script is run inside of the ImportUpload class.

if ( !$this->isTest() ){
    // Do only if not test
}

Set default value to field:

$rivi['fieldname'] = '100';

Custom manipulate address -data: Postnumber and town in same field

// Content of field: '000000 XXX XXX'
$address = explode(' ',$row[8]);
$rivi[address_field][0][data][postinumero] = array_shift($address); // First part
$rivi[address_field][0][data][toimipaikka] = implode(' ',$address); // The rest

Add new pages to main page

// Get employee
$employee = $this->get('page','employee','employee_id='.$row[0]);
// Get current benefits
$edut = explode(',',$employee->benefits->getValue());

if ( $row[4] > 0 ){
  $benefit = $this->get('page','salary_and_benefits');
  $benefit->start_date  = $monthRateStart;
  $benefit->salary_type = 'Monthly';
  $benefit->salary      = $row[4];
  // Save new benefit
  if ( !$test ) $benefit->tallenna();
  // Add new benefit to employee
  $benefits[] = $benefit->nr();
}

// Save new benefits to employee
$employee->benefits = implode(',',$benefits);
$employee->tallenna();

// OR if we are running this is Employee, save with other info
$rivi['benefits'] = implode(',',$benefits);

Prevent normal saving of data

unset($rivi);  // Nothing to save ....

Script variables

Variable return  Description
$this ImportUpload Import handler class
$Import Action Import action
$rivi array Associative array of data to be saved.
$row array Row data in array.
$data array All rows data.
$i int Number of the row in file that is being processed.
$start int / Start row for this loop.
$end int. Ednd row for this loop.
$test  bool TRUE, jos tiedoston lähetyksessä on painettu "Testaa".
$taulu  mad_taulu  Table that the data is being saved into.