/** * Attempt to submit a CSV Tank for discrimination * * @param array $data * @return null */ protected function rec_create($data) { $rec = $this->parent_rec; // check column headers $imp = new CSVImporter($rec); $valid = $imp->validate_headers(); if ($valid !== true) { throw new Rframe_Exception(Rframe::BAD_DATA, $valid); } // check for extra data (project/activity/org/etc) if (!$rec->TankOrg->count() > 0) { throw new Rframe_Exception(Rframe::BAD_DATA, 'Must define organization'); } if (!$rec->TankActivity->count() > 0) { throw new Rframe_Exception(Rframe::BAD_DATA, 'Must define activity'); } if (!$rec->TankActivity[0]->tact_prj_id) { throw new Rframe_Exception(Rframe::BAD_DATA, 'Must define project'); } // import in foreground... TODO: param for bg processing $n = $imp->import_file(); if (is_string($n)) { $rec->set_meta_field('submit_message', $n); $rec->set_meta_field('submit_success', false); $rec->save(); throw new Rframe_Exception(Rframe::BAD_DATA, $n); } // success! $rec->set_meta_field('submit_message', "Successfully imported {$n} rows"); $rec->set_meta_field('submit_success', true); $rec->save(); // add to job_queue if (!isset($data['nojob'])) { $this->queue_discriminator($rec->tank_id); } return '1'; }
/** * Update * * @param Tank $rec * @param array $data */ protected function air_update($rec, $data) { $allow_update = array(Tank::$STATUS_CSV_NEW); if (!in_array($rec->tank_status, $allow_update)) { throw new Rframe_Exception(Rframe::BAD_METHOD, 'Invalid tank status for update'); } // meta delimiters if (isset($data['csv_delim'])) { $rec->set_meta_field('csv_delim', $data['csv_delim']); } if (isset($data['csv_encl'])) { $rec->set_meta_field('csv_encl', $data['csv_encl']); } // new file if (isset($data['csvfile'])) { $file = $data['csvfile']; if ($file['name'] != $rec->tank_name) { $n = $rec->tank_name; $msg = "Error: you must upload the original file '{$n}', or start a new csv import."; throw new Rframe_Exception(Rframe::BAD_DATA, $msg); } // change files $rec->copy_file($file['tmp_name']); $size_kb = number_format($file['size'] / 1024, 1); $rec->set_meta_field('file_size', "{$size_kb} KB"); // setup submit errors and valids $rec->set_meta_field('submit_message', null); $rec->set_meta_field('submit_success', null); $rec->set_meta_field('valid_file', true); // use a CSVImporter to validate headers $imp = new CSVImporter($rec); $hdr_msg = $imp->validate_headers(); $hdr_valid = $hdr_msg === true ? true : false; $rec->set_meta_field('valid_header', $hdr_valid); } // Org if (isset($data['org_uuid'])) { $org = AIR2_Record::find('Organization', $data['org_uuid']); if (!$org) { throw new Rframe_Exception(Rframe::BAD_DATA, 'Invalid org_uuid'); } $rec->TankOrg[0]->to_org_id = $org->org_id; } // Activity $this->process_activity($rec, $data); }