/** * Do insert record * * @param array $inputRecord * @return void */ protected function _doInsert($inputRecord) { $dataRec = new DataRecord(null, $this->getDataObj()); // $inputRecord['Id'] = null; // comment it out for name PK case foreach ($inputRecord as $k => $v) { $dataRec[$k] = $v; } // or $dataRec->$k = $v; try { $dataRec->save(); } catch (Openbizx\Validation\Exception $e) { $errElements = $this->getErrorElements($e->errors); if (count($e->errors) == count($errElements)) { $this->formHelper->processFormObjError($errElements); } else { $errmsg = implode("<br />", $e->errors); Openbizx::$app->getClientProxy()->showErrorMessage($errmsg); } return; } catch (Openbizx\Data\Exception $e) { $this->processDataException($e); return; } $this->activeRecord = null; $this->getActiveRecord($dataRec["Id"]); //$this->runEventLog(); return $dataRec["Id"]; }
/** * Import from CSV file * NOTE: This method must be called from a popup form where a file is uploaded. * The parent form of the popup form is the target to import. * * @param string $objName * @return void */ public function importCSV($objName) { // read in file from $_FILE foreach ($_FILES as $file) { $error = $file['error']; if ($error != 0) { $this->reportError($error); return; } $tmpFileName = $file['tmp_name']; break; } //echo "upload file name = $tmpFileName"; $filename = $file['name']; if (strpos($filename, ".csv") === false) { $errorMsg = MessageHelper::getMessage("EXCELSVC_INVALID_FILE", array($filename)); Openbizx::$app->getLog()->log(LOG_ERR, "EXCEL SERVICE", "Import error = " . $errorMsg); Openbizx::$app->getClientProxy()->showClientAlert($errorMsg); return; } /* @var $formObj EasyForm */ $formObj = Openbizx::getObject($objName); // get the existing EasyForm object $parentFormObj = Openbizx::getObject($formObj->parentFormName); $dataObj = $parentFormObj->getDataObj(); $handle = fopen($tmpFileName, "r"); $fields = fgetcsv($handle, 2000, ","); if (!$fields || count($fields) < 2) { $errorMsg = MessageHelper::getMessage("EXCELSVC_INVALID_FILE", array($filename)); Openbizx::$app->getLog()->log(LOG_ERR, "EXCEL SERVICE", "Import error = " . $errorMsg); Openbizx::$app->getClientProxy()->showClientAlert($errorMsg); return; } // convert form element names to DO field names foreach ($parentFormObj->dataPanel as $element) { $elem_fields[$element->label] = $element->fieldName; } // validate with dataobj fields for ($i = 0; $i < count($fields); $i++) { $fields[$i] = $elem_fields[$fields[$i]]; $field = $fields[$i]; if (!$dataObj->getField($field)) { $errorMsg = MessageHelper::getMessage("EXCELSVC_INVALID_COLUMN", array($field, $dataObj->objectName)); Openbizx::$app->getLog()->log(LOG_ERR, "EXCEL SERVICE", "Import error = " . $errorMsg); Openbizx::$app->getClientProxy()->showClientAlert($errorMsg); return; } } while (($arr = fgetcsv($handle, 2000, ",")) !== FALSE) { if (count($arr) != count($fields)) { continue; } unset($recArr); $i = 0; for ($i = 0; $i < count($arr); $i++) { $recArr[$fields[$i]] = $arr[$i]; } //print_r($recArr); echo "<hr>"; $dataRec = new DataRecord(null, $dataObj); foreach ($recArr as $k => $v) { $dataRec[$k] = $v; } $ok = $dataRec->save(); if (!$ok) { // NOTE: EasyForm::processDataObjError() not return any value (void) return $formObj->processDataObjError($ok); } } fclose($handle); // in case of popup form, close it, then rerender the parent form if ($formObj->parentFormName) { $formObj->close(); $formObj->renderParent(); } }
/** * Save wizard data of current+previous pages into database or other storage * * @return void */ public function commit() { if (!$this->getDataObj()) { return true; } // commit the form input. call SaveRecord() $recArr = $this->activeRecord; if (strtoupper($this->formType) == "NEW") { $dataRec = new DataRecord(null, $this->getDataObj()); } else { //$currentRec = $this->fetchData(); // wrong way to get current data. need to query the old one $currentRec = array(); // to get record with "" values $dataRec = new DataRecord($currentRec, $this->getDataObj()); } foreach ($recArr as $k => $v) { $dataRec[$k] = $v; } // or $dataRec->$k = $v; try { $dataRec->save(); } catch (Openbizx\Data\Exception $e) { $this->processDataException($e); return false; } return true; }