Beispiel #1
0
 /**
  * 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 (Openbiz\Validation\Exception $e) {
         $errElements = $this->getErrorElements($e->errors);
         if (count($e->errors) == count($errElements)) {
             $this->formHelper->processFormObjError($errElements);
         } else {
             $errmsg = implode("<br />", $e->errors);
             Openbiz::$app->getClientProxy()->showErrorMessage($errmsg);
         }
         return;
     } catch (Openbiz\Data\Exception $e) {
         $this->processDataException($e);
         return;
     }
     $this->activeRecord = null;
     $this->getActiveRecord($dataRec["Id"]);
     //$this->runEventLog();
     return $dataRec["Id"];
 }
 public function LogDataChanges($formObj, $inputRecord, $currentRecord, $comment = null, $panel = null)
 {
     if (!$panel) {
         $panel = clone $formObj->dataPanel;
     }
     $postFields = $_POST;
     $elem_mapping = array();
     foreach ($postFields as $elem_name => $value) {
         $elem = $panel->get($elem_name);
         $fld_name = $elem->fieldName;
         if ($elem) {
             $elem_mapping[$fld_name] = $elem;
         }
     }
     $logDO = $formObj->getDataObj()->getRefObject($this->logDO);
     if (!$logDO) {
         return true;
     }
     $cond_column = $logDO->association['CondColumn'];
     $cond_value = $logDO->association['CondValue'];
     if ($cond_column) {
         $type = $cond_value;
     }
     $foreign_id = $currentRecord['Id'];
     $logRecord = array();
     foreach ($inputRecord as $fldName => $fldVal) {
         $oldVal = $currentRecord[$fldName];
         if ($oldVal == $fldVal) {
             continue;
         }
         $elem = $elem_mapping[$fldName]->xmlMeta;
         if (!$elem) {
             $elem = $panel->getByField($fldName)->xmlMeta;
         }
         $logRecord[$fldName] = array('old' => $oldVal, 'new' => $fldVal, 'element' => $elem);
     }
     if (empty($logRecord)) {
         return true;
     }
     $formMetaLite = array("name" => $formObj->objectName, "package" => $formObj->package, "message_file" => $formObj->messageFile);
     // save to comment do
     $dataRec = new DataRecord(null, $logDO);
     $dataRec['foreign_id'] = $foreign_id;
     $dataRec['type'] = $type;
     $dataRec['form'] = serialize($formMetaLite);
     $dataRec['data'] = serialize($logRecord);
     $dataRec['comment'] = $comment;
     try {
         $dataRec->save();
     } catch (Openbiz\data\Exception $e) {
         $this->processDataException($e);
         return true;
     }
     return true;
 }
 protected function _doUpdate($inputRecord, $currentRecord)
 {
     $lang = $inputRecord['lang'];
     $record_id = $currentRecord["Id"];
     $transDO = Openbiz::getObject($this->translateDO, 1);
     $newRecord = array("{$this->recordFKField}" => $record_id, "lang" => $lang);
     foreach ($inputRecord as $field => $value) {
         if (substr($field, 0, 1) == '_') {
             $newRecord[substr($field, 1, strlen($field) - 1)] = $value;
         }
     }
     $searchRule = "[{$this->recordFKField}]='{$record_id}' AND [lang]='{$lang}'";
     $currentRecord = $transDO->fetchOne($searchRule);
     if ($currentRecord) {
         $currentRecord = $currentRecord->toArray();
     }
     $dataRec = new DataRecord($currentRecord, $transDO);
     foreach ($newRecord as $k => $v) {
         $dataRec[$k] = $v;
         // or $dataRec->$k = $v;
     }
     try {
         //test dump data
         //var_dump($currentRecord);
         //var_dump($dataRec->toArray());exit;
         $dataRec->save();
     } catch (Openbiz\Validation\Exception $e) {
         $errElements = $this->getErrorElements($e->errors);
         if (count($e->errors) == count($errElements)) {
             $this->processFormObjError($errElements);
         } else {
             $errmsg = implode("<br />", $e->errors);
             Openbiz::$app->getClientProxy()->showErrorMessage($errmsg);
         }
         return false;
     } catch (Openbiz\data\Exception $e) {
         $this->processDataException($e);
         return false;
     }
     $this->activeRecord = null;
     $this->getActiveRecord($dataRec["Id"]);
     $this->runEventLog();
     return true;
 }
 protected function _doUpdate($inputRecord, $currentRecord)
 {
     $lang = $inputRecord['lang'];
     $setting_id = $currentRecord["Id"];
     $transDO = Openbiz::getObject($this->translateDO, 1);
     $newRecord = array("setting_id" => $setting_id, "lang" => $lang);
     foreach ($inputRecord as $field => $value) {
         if (substr($field, 0, 1) == '_') {
             $newRecord[substr($field, 1, strlen($field) - 1)] = $value;
         }
     }
     $currentRecord = $transDO->fetchOne("[setting_id]='{$setting_id}' AND [lang]='{$lang}'");
     if ($currentRecord) {
         $currentRecord = $currentRecord->toArray();
     }
     $dataRec = new DataRecord($currentRecord, $transDO);
     foreach ($newRecord as $k => $v) {
         $dataRec[$k] = $v;
         // or $dataRec->$k = $v;
     }
     Openbiz::getObject("extend.widget.ExtendSettingDetailForm", 1)->processOptions($inputRecord['_options'], $setting_id, $lang);
     try {
         $dataRec->save();
     } catch (Openbiz\Validation\Exception $e) {
         $errElements = $this->getErrorElements($e->errors);
         if (count($e->errors) == count($errElements)) {
             $this->processFormObjError($errElements);
         } else {
             $errmsg = implode("<br />", $e->errors);
             Openbiz::$app->getClientProxy()->showErrorMessage($errmsg);
         }
         return false;
     } catch (Openbiz\data\Exception $e) {
         $this->processDataException($e);
         return false;
     }
     $this->activeRecord = null;
     $this->getActiveRecord($dataRec["Id"]);
     $this->runEventLog();
     return true;
 }
 public function ShareRecord()
 {
     $prtForm = $this->parentFormName;
     if (!$prtForm) {
         return;
     }
     $prtFormObj = Openbiz::getObject($prtForm);
     $recId = $this->parentRecordId;
     $dataObj = $prtFormObj->getDataObj();
     $dataRec = $dataObj->fetchById($recId);
     $recArr = $this->readInputRecord();
     $DataRec = $dataRec;
     $DataRecOld = $dataRec;
     $currentRecord = $DataRecOld->toArray();
     //notice users has new shared data
     //test if changed a new owner
     if ($recArr['notify_user']) {
         $data = $this->fetchData();
         $data['app_index'] = OPENBIZ_APP_INDEX_URL;
         $data['app_url'] = OPENBIZ_APP_URL;
         $data['operator_name'] = Openbiz::$app->getProfile()->getProfileName(Openbiz::$app->getUserProfile("Id"));
         $emailSvc = Openbiz::getService(CUBI_USER_EMAIL_SERVICE);
         if ($DataRec['owner_id'] != $recArr['owner_id']) {
             $emailSvc->DataAssignedEmail($recArr['owner_id'], $data);
         }
         //test if changes for group level visiable
         if ($recArr['group_perm'] >= 1) {
             $group_id = $recArr['group_id'];
             $userList = $this->_getGroupUserList($group_id);
             foreach ($userList as $user_id) {
                 $emailSvc->DataSharingEmail($user_id, $data);
             }
         }
         //test if changes for other group level visiable
         if ($recArr['other_perm'] >= 1) {
             $groupList = $this->_getGroupList();
             foreach ($groupList as $group_id) {
                 if ($recArr['group_id'] == $group_id) {
                     continue;
                 }
                 $userList = $this->_getGroupUserList($group_id);
                 foreach ($userList as $user_id) {
                     $emailSvc->DataSharingEmail($user_id, $data);
                 }
             }
         }
     }
     if (isset($recArr['group_perm'])) {
         $DataRec['group_perm'] = $recArr['group_perm'];
     }
     if (isset($recArr['other_perm'])) {
         $DataRec['other_perm'] = $recArr['other_perm'];
     }
     if (isset($recArr['group_id'])) {
         $DataRec['group_id'] = $recArr['group_id'];
     }
     if (isset($recArr['owner_id'])) {
         $DataRec['owner_id'] = $recArr['owner_id'];
     }
     if (isset($recArr['create_by'])) {
         $DataRec['create_by'] = $recArr['create_by'];
         $DataRec['update_by'] = $recArr['create_by'];
         $DataRec['update_time'] = date('Y-m-d H:i:s');
     }
     $DataRec->save();
     $inputRecord = $recArr;
     //$prtFormObj->getDataObj()->updateRecord($newDataRec,$dataRec);
     //save change log
     $postFields = $_POST;
     $elem_mapping = array();
     foreach ($postFields as $elem_name => $value) {
         $elem = $this->dataPanel->get($elem_name);
         $fld_name = $elem->fieldName;
         if ($elem) {
             $elem_mapping[$fld_name] = $elem;
         }
     }
     $logDO = $dataObj->getRefObject($this->logDO);
     if ($logDO) {
         $cond_column = $logDO->association['CondColumn'];
         $cond_value = $logDO->association['CondValue'];
         if ($cond_column) {
             $type = $cond_value;
         }
         $foreign_id = $currentRecord['Id'];
         $logRecord = array();
         foreach ($inputRecord as $fldName => $fldVal) {
             $oldVal = $currentRecord[$fldName];
             if ($oldVal == $fldVal) {
                 continue;
             }
             if ($oldVal === null || $fldVal === null) {
                 continue;
             }
             $elem = $elem_mapping[$fldName]->xmlMeta;
             if (!$elem) {
                 $elem = $this->dataPanel->getByField($fldName)->xmlMeta;
             }
             $logRecord[$fldName] = array('old' => $oldVal, 'new' => $fldVal, 'element' => $elem);
         }
         $formMetaLite = array("name" => $this->objectName, "package" => $this->package, "message_file" => $this->messageFile);
         // save to comment do
         $logRec = new DataRecord(null, $logDO);
         $logRec['foreign_id'] = $foreign_id;
         $logRec['type'] = $type;
         $logRec['form'] = serialize($formMetaLite);
         $logRec['data'] = serialize($logRecord);
         $logRec['comment'] = $comment;
         $logRec->save();
     }
     //end save change log
     if ($recArr['update_ref_data']) {
         if ($dataObj->objReferences->count()) {
             $this->_casacadeUpdate($dataObj, $recArr);
         }
     }
     if ($this->parentFormName) {
         $this->close();
         $this->renderParent();
     }
     $this->processPostAction();
 }
 public function put($resource, $id, $request, $response)
 {
     $DOName = $this->getDOName($resource);
     if (empty($DOName)) {
         $response->status(404);
         $response->body("Resource '{$resource}' is not found.");
         return;
     }
     $dataObj = Openbiz::getObject($DOName);
     $rec = $dataObj->fetchById($id);
     if (empty($rec)) {
         $response->status(400);
         $response->body("No data is found for {$resource} {$id}");
         return;
     }
     $dataRec = new DataRecord($rec, $dataObj);
     $inputRecord = json_decode($request->getBody());
     foreach ($inputRecord as $k => $v) {
         $dataRec[$k] = $v;
         // or $dataRec->$k = $v;
     }
     try {
         $dataRec->save();
     } catch (Openbiz\Validation\Exception $e) {
         $response->status(400);
         $errmsg = implode("\n", $e->errors);
         $response->body($errmsg);
         return;
     } catch (Openbiz\data\Exception $e) {
         $response->status(400);
         $response->body($e->getMessage());
         return;
     }
     $format = strtolower($request->params('format'));
     $response->status(200);
     $message = "Successfully updated record of {$resource} {$id}";
     if ($format == 'json') {
         $response['Content-Type'] = 'application/json';
         $response->body($message);
     } else {
         $response['Content-Type'] = "text/xml; charset=utf-8";
         $response->body($message);
     }
     return;
 }
 /**
  * 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));
         Openbiz::$app->getLog()->log(LOG_ERR, "EXCEL SERVICE", "Import error = " . $errorMsg);
         Openbiz::$app->getClientProxy()->showClientAlert($errorMsg);
         return;
     }
     /* @var $formObj EasyForm */
     $formObj = Openbiz::getObject($objName);
     // get the existing EasyForm object
     $parentFormObj = Openbiz::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));
         Openbiz::$app->getLog()->log(LOG_ERR, "EXCEL SERVICE", "Import error = " . $errorMsg);
         Openbiz::$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));
             Openbiz::$app->getLog()->log(LOG_ERR, "EXCEL SERVICE", "Import error = " . $errorMsg);
             Openbiz::$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();
     }
 }
Beispiel #8
0
 /**
  * Update login time
  *
  * @return void
  */
 protected function updateLoginTime()
 {
     //echo __METHOD__ .'-'. __LINE__ .' : BEGIN ===============<br />';
     /* @var $userObj Openbiz\Data\BizDataObj */
     $userObj = Openbiz::getObject('system.do.UserDO');
     try {
         $curRecs = $userObj->directFetch("[username]='" . $this->username . "'", 1);
         if (count($curRecs) == 0) {
             return false;
         }
         $curRec = $curRecs[0];
         //$dataRec = $curRec;
         $dataRec = new DataRecord($curRec, $userObj);
         $dataRec['lastlogin'] = date("Y-m-d H:i:s");
         $ok = $dataRec->save();
         if (!$ok) {
             $errorMsg = $userObj->getErrorMessage();
             Openbiz::$app->getLog()->log(LOG_ERR, "DATAOBJ", "DataObj error = " . $errorMsg);
             Openbiz::$app->getClientProxy()->showErrorMessage($errorMsg);
             return false;
         }
     } catch (Openbiz\Data\Exception $e) {
         $errorMsg = $e->getMessage();
         Openbiz::$app->getLog()->log(LOG_ERR, "DATAOBJ", "DataObj error = " . $errorMsg);
         Openbiz::$app->getClientProxy()->showErrorMessage($errorMsg);
         return false;
     }
     return true;
 }
 /**
  * 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 (Openbiz\Data\Exception $e) {
         $this->processDataException($e);
         return false;
     }
     return true;
 }
Beispiel #10
0
 /**
  * Do update record
  *
  * @param array $inputRecord
  * @param array $currentRecord
  * @return void
  */
 protected function _doUpdate($inputRecord, $currentRecord)
 {
     $dataRec = new DataRecord($currentRecord, $this->getDataObj());
     foreach ($inputRecord as $k => $v) {
         $dataRec[$k] = $v;
         // or $dataRec->$k = $v;
     }
     try {
         $dataRec->save();
     } catch (Openbiz\Validation\Exception $e) {
         $errElements = $this->getErrorElements($e->errors);
         if (count($e->errors) == count($errElements)) {
             $this->formHelper->processFormObjError($errElements);
         } else {
             $errmsg = implode("<br />", $e->errors);
             Openbiz::$app->getClientProxy()->showErrorMessage($errmsg);
         }
         return false;
     } catch (Openbiz\Data\Exception $e) {
         $this->processDataException($e);
         return false;
     }
     $this->activeRecord = null;
     $this->getActiveRecord($dataRec["Id"]);
     //$this->runEventLog();
     return true;
 }
 protected function _doUpdate($inputRecord, $currentRecord)
 {
     parent::_doUpdate($inputRecord, $currentRecord);
     $postFields = $_POST;
     $elem_mapping = array();
     foreach ($postFields as $elem_name => $value) {
         $elem = $this->dataPanel->get($elem_name);
         $fld_name = $elem->fieldName;
         if ($elem) {
             $elem_mapping[$fld_name] = $elem;
         }
     }
     $logDO = $this->getDataObj()->getRefObject($this->logDO);
     if (!$logDO) {
         return true;
     }
     $cond_column = $logDO->association['CondColumn'];
     $cond_value = $logDO->association['CondValue'];
     if ($cond_column) {
         $type = $cond_value;
     }
     $foreign_id = $currentRecord['Id'];
     $logRecord = array();
     foreach ($inputRecord as $fldName => $fldVal) {
         $oldVal = $currentRecord[$fldName];
         if ($oldVal == $fldVal) {
             continue;
         }
         $elem = $elem_mapping[$fldName]->xmlMeta;
         if (!$elem) {
             $elem = $this->dataPanel->getByField($fldName)->xmlMeta;
         }
         if ($elem['ATTRIBUTES']['CLASS'] != 'Hidden') {
             $logRecord[$fldName] = array('old' => $oldVal, 'new' => $fldVal, 'element' => $elem);
         }
     }
     $comment = Openbiz::$app->getClientProxy()->getFormInputs("fld_changelog_comment");
     if (empty($logRecord) && empty($comment)) {
         return true;
     }
     $formMetaLite = array("name" => $this->objectName, "package" => $this->package, "message_file" => $this->messageFile);
     // save to comment do
     $dataRec = new DataRecord(null, $logDO);
     $dataRec['foreign_id'] = $foreign_id;
     $dataRec['type'] = $type;
     $dataRec['form'] = serialize($formMetaLite);
     $dataRec['data'] = serialize($logRecord);
     $dataRec['comment'] = $comment;
     try {
         $dataRec->save();
     } catch (Openbiz\data\Exception $e) {
         $this->processDataException($e);
         return true;
     }
     $this->runEventLog();
     return true;
 }
 protected function setInstallInfo($pkgArr, $installInfo)
 {
     $pkgDo = Openbiz::getObject(self::INSTALLED_DO);
     $searchRule = " [app_id]='" . $pkgArr['app_id'] . "' AND         \t\t\t\t\n        \t\t\t\t[repository_uid]='" . $pkgArr['repository_uid'] . "' \n        \t\t\t\t";
     $dataRec = $pkgDo->fetchOne($searchRule);
     if (!$dataRec) {
         $dataRec = new DataRecord(null, $pkgDo);
         $dataRec["app_id"] = $pkgArr['app_id'];
         $dataRec["version"] = $pkgArr['version'];
         $dataRec["repository_uid"] = $pkgArr['repository_uid'];
     }
     if (isset($installInfo['version'])) {
         $dataRec["version"] = $installInfo['version'];
     }
     if (isset($installInfo['time'])) {
         $dataRec["install_time"] = $installInfo['time'];
     }
     if (isset($installInfo['state'])) {
         $dataRec["install_state"] = $installInfo['state'];
     }
     if (isset($installInfo['log'])) {
         $dataRec["install_log"] = $installInfo['log'];
     }
     if (isset($installInfo['filesize'])) {
         $dataRec["install_download_filesize"] = $installInfo['filesize'];
     }
     if (isset($installInfo['download'])) {
         $dataRec["install_download"] = $installInfo['download'];
     }
     try {
         $dataRec->save();
     } catch (Exception $e) {
         throw new Exception("setInstallInfo. Unable to save the record. " . $e->getMessage());
     }
     return true;
 }