function combinePath($baseType, $filename) { $file = ServerUtils::buildPath(ServerUtils::getRealPath($baseType), $filename); return ServerUtils::formalPath($file); }
function UploadRecord($table, $fields, $guidfield, $idfield, $syncfield, $delimiter = ',', $enclosure = '"') { LogUtils::log_str('UploadRecord Begin'); LogUtils::log_obj(func_get_args()); $server =& $GLOBALS['as_server']; $sys =& $GLOBALS['system']; $db = $sys->database(); $syncitems = array(); $atts = $server->getAttachments(); LogUtils::log_obj($atts); if (count($atts) > 0) { $att = null; foreach ($atts as $attitem) { $att = $attitem; break; } $csvfile = ServerUtils::formalPath(ServerUtils::buildPath(AS_TMP_DIR, 'tmpcsv' . time() . '.txt')); file_put_contents($csvfile, $att['data']); LogUtils::log_str($csvfile); $list = TextUtils::csv2array($csvfile, $fields, $delimiter, $enclosure); unlink($csvfile); $validators = BaseValidator::loadValidators(AS_VALIDATOR_DIR, $table, $sys); $idcolarr = split(',', $idfield); foreach ($list as $row) { LogUtils::log_obj($row); $sync_item = array(); $sync_item['guid'] = ''; $sync_item['id'] = ''; $sync_item['succ'] = false; $sync_item['errmsg'] = ''; $sync_item['syncstate'] = AS_SYNC_ADDED; if (array_key_exists($guidfield, $row)) { $sync_item['guid'] = $row[$guidfield]; } if (array_key_exists($syncfield, $row)) { $sync_item['syncstate'] = $row[$syncfield]; } $idcnd = array(); $idcndstr = ''; foreach ($idcolarr as $idcol) { if (array_key_exists($idcol, $row)) { $idcnd[$idcol] = $row[$idcol]; if (!empty($idcndstr)) { $idcndstr .= ' and '; } $idcndstr .= $idcol . "=" . $db->quote($row[$idcol]); } } $sync_item['id'] = implode(',', $idcnd); LogUtils::log_obj($idcnd); switch ($sync_item['syncstate']) { case AS_SYNC_DELETED: if (count($idcnd) > 0) { if (BaseValidator::runValidateBefore($validators, 'delete', $row)) { $sql = "delete from sdb_{$table} where {$idcndstr}"; LogUtils::log_str($sql); if ($db->exec($sql)) { $sync_item['succ'] = true; BaseValidator::runValidateAfter($validators, 'delete', $row); } } } break; case AS_SYNC_UNCHANGED: case AS_SYNC_MODIFIED: if (count($idcnd) > 0) { $sql = "select * from sdb_{$table} where {$idcndstr}"; LogUtils::log_str($sql); $count = $db->_count($sql); if ($count > 0) { if (BaseValidator::runValidateBefore($validators, 'update', $row)) { $rs = $db->query($sql); $sql = $db->getUpdateSql($rs, $row, true); LogUtils::log_str($sql); if ($sql && $db->exec($sql)) { $sync_item['succ'] = true; BaseValidator::runValidateAfter($validators, 'update', $row); } } } else { if (BaseValidator::runValidateBefore($validators, 'insert', $row)) { $rs = $db->query($sql); $sql = $db->getInsertSQL($rs, $row); LogUtils::log_str($sql); if ($sql && $db->exec($sql)) { if (count($idcnd) == 1) { $sync_item['id'] = $db->lastInsertId(); } $sync_item['succ'] = true; BaseValidator::runValidateAfter($validators, 'insert', $row); } } } } break; case AS_SYNC_ADDED: $count = 0; if (count($idcnd) > 0) { $sql = "select * from sdb_{$table} where {$idcndstr}"; LogUtils::log_str($sql); $count = $db->_count($sql); } if ($count > 0) { if (BaseValidator::runValidateBefore($validators, 'update', $row)) { $rs = $db->query($sql); $sql = $db->getUpdateSql($rs, $row, true); LogUtils::log_str($sql); if ($sql && $db->exec($sql)) { $sync_item['succ'] = true; BaseValidator::runValidateAfter($validators, 'update', $row); } } } else { if (BaseValidator::runValidateBefore($validators, 'insert', $row)) { $sql = "select * from sdb_{$table} where 0=1"; LogUtils::log_str($sql); $rs = $db->query($sql); $sql = $db->getInsertSQL($rs, $row); LogUtils::log_str($sql); if ($sql && $db->exec($sql)) { if (count($idcnd) == 1) { $sync_item['id'] = $db->lastInsertId(); } $sync_item['succ'] = true; BaseValidator::runValidateAfter($validators, 'insert', $row); } } } break; } LogUtils::log_obj($sync_item); $syncitems[] = $sync_item; } } $pack = array('items' => $syncitems); LogUtils::log_str('UploadRecord Return'); return $pack; }