By the looks of this, you are using this import function as a way to populate already existing records with updated data from a spreadsheet, as well as import new data that isn't already in the spreadsheet.
The behaviour I would expect (based on your code) is:
1. New records can be previewed before they are inserted.
2. Existing records will be changed before you even get to the preview stage.
There is an alternative way to handle imports that is more amenable to previews. That is using Dataface_ImportRecord objects instead of Dataface_Record objects for doing the import. Steps:
1. Create a class that extends the Dataface_ImportRecord class.
1a. Implement the commit() method of your class. This is the method that will be run to actually perform the import.
2. In your delegate class's __import__xxx method, you return an array of your Dataface_ImportRecord objects instead of Dataface_Record objects.
Here is a full example from another app.
- Code: Select all
<?php
class tables_section_schedule_items {
function __import__csv($data){
$temp = tmpfile();
fwrite($temp, $data);
fseek($temp, 0);
$records = array();
fgetcsv($temp); // first row just has headings.
$out = array();
$cols = array(
'strm'=>0,
'subject'=>2,
'catalog_nbr'=>3,
'class_section'=>4,
'ssr_component'=>5,
'session_code'=>6,
'section_status'=>7,
'instructor'=>7+1,
'facility_id'=>8+1,
'meeting_time_start'=>9+1,
'meeting_time_end'=>10+1,
'mon'=>11+1,
'tues'=>12+1,
'wed'=>13+1,
'thurs'=>14+1,
'fri'=>15+1,
'sat'=>16+1,
'sun'=>17+1,
'Course_Title'=>19+1,
'Start_date'=>21+1,
'End_date'=>22+1,
'Cancel_Date'=>23+1,
'Prerequisites'=>20+1,
'Description'=>24+1
);
$dayCodes = array(
'mon'=>'M',
'tues'=>'T',
'wed'=>'W',
'thurs'=>'R',
'fri'=>'F',
'sat'=>'S',
'sun'=>'Su'
);
while ( ($row = fgetcsv($temp)) !== false ){
//if ( @$alreadyProcessed[$row[0].'-'.$row[1]] ) continue;
foreach ( array('mon','tues','wed','thurs','fri','sat','sun') as $day ){
if ( $row[$cols[$day]] == 'Y' ){
$irec = new tables_section_schedule_items_csv_importer(array(
'semester_code'=> $row[$cols['strm']],
'startDate' => $row[$cols['Start_date']],
'endDate'=> $row[$cols['End_date']],
'session_code'=> 'R',
'department_code'=>$row[$cols['subject']],
'course_number'=>$row[$cols['catalog_nbr']],
'course_name'=>$row[$cols['Course_Title']],
'instructor'=>$row[$cols['instructor']],
'section_type'=>$row[$cols['ssr_component']],
'section_number'=>$row[$cols['class_section']],
'room_number'=>$row[$cols['facility_id']],
'day'=> $dayCodes[$day],
'start_time'=>$row[$cols['meeting_time_start']],
'end_time'=>$row[$cols['meeting_time_end']],
'description'=>$row[$cols['Description']],
'section_status'=>$row[$cols['section_status']]
));
$out[] = $irec;
unset($irec);
}
}
}
return $out;
}
}
import('Dataface/ImportRecord.php');
class tables_section_schedule_items_csv_importer extends Dataface_ImportRecord {
function __construct($data){
$this->Dataface_ImportRecord(__FILE__, $data);
}
function commit(){
// For some reason views aren't updating fast enough when adding records
// so we're getting duplicates.
if ( !defined('DISABLE_PROXY_VIEWS') ) define('DISABLE_PROXY_VIEWS',1);
$semester = df_get_record('semesters', array('semester_code'=>'='.$this->val('semester_code')));
if ( !$semester ){
$semester = new Dataface_Record('semesters', array());
$semester->setValues(array(
'semester_code'=>$this->val('semester_code'),
'startDate'=> $this->val('startDate'),
'endDate'=> $this->val('endDate')
));
$semester->save();
}
$session = df_get_record('sessions', array('semester_id'=>$semester->val('semester_id'), 'session_code'=>'='.$this->val('session_code')));
if ( !$session ){
$session = new Dataface_Record('sessions', array());
$session->setValues(array(
'semester_id'=>$semester->val('semester_id'),
'session_code'=>$this->val('session_code')
));
$session->save();
}
$department = df_get_record('departments', array('code'=>'='.$this->val('department_code')));
if ( !$department ){
$department = new Dataface_Record('departments', array());
$department->setValues(array(
'name'=>$this->val('department_code'),
'code'=>$this->val('department_code')
));
$department->save();
}
$course = df_get_record('courses', array('department_id'=>'='.$department->val('department_id'), 'course_number'=>'='.$this->val('course_number')));
if ( !$course ){
$course = new Dataface_Record('courses', array());
$course->setValues(array(
'department_id'=>$department->val('department_id'),
'course_number'=>$this->val('course_number'),
'course_name'=>$this->val('course_name'),
'description'=>$this->val('description')
));
$course->save();
}
$offering = df_get_record('course_offerings', array('course_id'=>'='.$course->val('course_id'), 'session_id'=>'='.$session->val('session_id')));
if ( !$offering ){
$offering = new Dataface_Record('course_offerings', array());
$offering->setValues(array(
'course_id'=>$course->val('course_id'),
'session_id'=>$session->val('session_id'),
'instructor'=>$this->val('instructor')
));
$res = $offering->save();
if ( PEAR::isError($res) ) throw new Exception($res->getMessage());
$offering = df_get_record('course_offerings', array('course_id'=>'='.$course->val('course_id'), 'session_id'=>'='.$session->val('session_id')));
if ( !$offering ){
die("What the fuck!!??? Just added offering, but offering not found!!!!".__FILE__.':'.__LINE__);
}
}
$section = df_get_record('course_sections', array('course_offering_id'=>'='.$offering->val('course_offering_id'), 'section_number'=>'='.$this->val('section_number')));
if ( !$section ){
//echo "[".$offering->val('course_offering_id').']['.$this->val('section_number').']';
$section = new Dataface_Record('course_sections', array());
$section->setValues(array(
'course_offering_id'=>$offering->val('course_offering_id'),
'section_type'=>$this->val('section_type'),
'section_number'=>$this->val('section_number'),
'section_status'=>$this->val('section_status')
));
$res = $section->save();
if ( PEAR::isError($res) ){
print_r($offering->vals());
print_r($this->getValues());
throw new Exception($res->getMessage());
}
}
$section->setValue('section_status', $this->val('section_status'));
$res = $section->save();
if ( PEAR::isError($res) ) throw new Exception($res->getMessage());
$campusName = 'BBY';
if ( preg_match('/^HCC/', $this->val('room_number') ) ){
$campusName = 'VAN';
} else if ( preg_match('/^SUR/', $this->val('room_number') ) ){
$campusName = 'SUR';
}
$campus = df_get_record('campus', array('campus_code'=>'='.$campusName) );
if ( !$campus ){
$campus = new Dataface_Record('campus', array());
$campus->setValues(array(
'campus_code'=>$campusName
));
$campus->save();
}
$room = df_get_record('rooms', array('campus_id'=>$campus->val('campus_id'), 'room_number'=>$this->val('room_number')));
if ( !$room ) {
$room = new Dataface_Record('rooms', array());
$room->setValues(array(
'campus_id'=>$campus->val('campus_id'),
'room_number'=>$this->val('room_number')
));
$room->save();
}
$sched = df_get_record('section_schedule_items', array('course_section_id'=>$section->val('course_section_id'), 'room_id'=>$room->val('room_id')));
if ( !$sched ){
$sched = new Dataface_Record('section_schedule_items', array());
$sched->setValues(array(
'course_section_id'=>$section->val('course_section_id'),
'room_id'=>$room->val('room_id'),
'day'=>$this->val('day'),
'start_time'=>$this->val('start_time'),
'end_time'=>$this->val('end_time')
));
$sched->save();
}
}
}