/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Part(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Part'])) { $model->attributes = $_POST['Part']; if ($model->save()) { $this->redirect(array('view', 'id' => $model->id)); } } $this->render('create', array('model' => $model)); }
private function savePart() { $form = 'frost.views.cms.part.text.TextAdd'; $part = Part::addMediaToPart($form,'Text'); // Add the 'preview' to the part $part = new Part(); $part->clauseSafe('part_id',Application::param('part_id')); $part->parse(); $part->save(); //Part::setParttagsAndSave($part,Application::param('current_tags')); Application::setParam('module_id',Application::param('module_id')); Application::redirect('ModuleDetail'); }
public function addquestions($filename) { PHPExcel_Settings::setCacheStorageMethod(PHPExcel_CachedObjectStorageFactory::cache_in_memory_gzip); $files = scandir("./upload"); array_shift($files); array_shift($files); $errors = array(); $factors = array(); $j = 0; $this->db->begin(); foreach ($files as $file) { if (strstr($file, $filename)) { $objexcel = PHPExcel_IOFactory::load("./upload/" . $file); $factorsheet = $objexcel->getSheet(0); $questionsheet = $objexcel->getSheet(1); $part = new Part(); $part->type = 0; $part->name = (string) $factorsheet->getCell("B2")->getValue(); $part->description = (string) $factorsheet->getCell("B3")->getValue(); try { if (!$part->save()) { $errors[$j]['error'] = "part"; foreach ($part->getMessages() as $key => $message) { $errors[$j][$key] = $message; } $j++; $this->db->rollback(); $objexcel->disconnectWorksheets(); unlink("./upload/" . $file); return $errors; } else { $higestrow = $factorsheet->getHighestRow(); $i = 5; while ($i <= $higestrow) { $factor = new Factor(); $factor->ratio = $factorsheet->getCell("B" . $i)->getValue(); $k = $factorsheet->getCell("A" . $i)->getValue(); if (is_null($k) || $k == "") { break; } if ($factor->save()) { $factors["{$k}"] = $factor->f_id; $fprel = new Fprel(); $fprel->factor_id = $factor->f_id; $fprel->part_id = $part->p_id; if (!$fprel->save()) { $errors[$j]['error'] = "fprel"; foreach ($fprel->getMessages() as $key => $message) { $errors[$j][$key] = $message; } $j++; $this->db->rollback(); $objexcel->disconnectWorksheets(); unlink("./upload/" . $file); return $errors; } } else { $errors[$j]['error'] = "factor"; foreach ($factor->getMessages() as $key => $message) { $errors[$j][$key] = $message; } $j++; $this->db->rollback(); $objexcel->disconnectWorksheets(); unlink("./upload/" . $file); return $errors; } $i++; } $higestrow = $questionsheet->getHighestRow(); //不可靠 $i = 3; while ($i <= $higestrow) { $question = new Question(); $topic = (string) $questionsheet->getCell("A" . $i)->getValue(); if (is_null($topic) || $topic == "") { break; } $question->topic = $topic; $question->factor_id = $factors[$questionsheet->getCell("C" . $i)->getValue()]; $question->options = ""; $question->grade = ""; $col = "D"; $colnum = $questionsheet->getCell("B" . $i)->getValue(); for ($k = 1; $k <= $colnum; $k++, $col++) { $str = $questionsheet->getCell($col . $i)->getValue(); $mem = explode("|", $str); $question->options .= "|" . $mem[0]; $question->grade .= "|" . $mem[1]; } $question->options = substr($question->options, 1); $question->grade = substr($question->grade, 1); if (!$question->save()) { $errors[$j]['error'] = "question"; $errors[$j]['maxrow'] = $higestrow; foreach ($question->getMessages() as $key => $message) { $errors[$j][$key] = $message; } $j++; $this->db->rollback(); $objexcel->disconnectWorksheets(); unlink("./upload/" . $file); return $errors; } $i++; } } } catch (PDOException $ex) { foreach ($ex->getMessages() as $key => $message) { $errors[$j][$key] = $message; } $j++; $this->db->rollback(); $objexcel->disconnectWorksheets(); unlink("./upload/" . $file); throw $ex; //return $errors; } $objexcel->disconnectWorksheets(); unlink("./upload/" . $file); } } $this->db->commit(); return $errors; }