/** * Get the number of records according the Select SQL * * @param object $db database connection * @param string $sql SQL string * @return int number of records */ private function _getNumberRecords($db, $sql) { $has_subquery = false; if (preg_match("/\\(\\s*?SELECT\\s*?.+\\)/si", $sql)) { $has_subquery = true; } if (preg_match("/^\\s*SELECT\\s+DISTINCT/is", $sql) || preg_match('/\\s+GROUP\\s+BY\\s+/is', $sql)) { // ok, has SELECT DISTINCT or GROUP BY so see if we can use a table alias $rewritesql = preg_replace('/(\\sORDER\\s+BY\\s.*)/is', '', $sql); $rewritesql = "SELECT COUNT(*) FROM ({$rewritesql}) _TABLE_ALIAS_"; } elseif ($has_subquery == false) { // now replace SELECT ... FROM with SELECT COUNT(*) FROM $rewritesql = preg_replace('/\\s*?SELECT\\s.*?\\s+FROM\\s/is', 'SELECT COUNT(*) FROM ', $sql); // Because count(*) and 'order by' fails with mssql, access and postgresql. // Also a good speedup optimization - skips sorting! $rewritesql = preg_replace('/(\\sORDER\\s+BY\\s.*)/is', '', $rewritesql); } else { $rewritesql = $sql; } try { if ($this->cacheLifeTime > 0) { $cache_id = md5($this->objectName . $rewritesql . serialize($bindValues)); //try to process cache service. $cacheSvc = Openbizx::getService(CACHE_SERVICE); $cacheSvc->init($this->objectName, $this->cacheLifeTime); if ($cacheSvc->test($cache_id)) { //Openbizx::$app->getLog()->log(LOG_DEBUG, "DATAOBJ", ". Query Sql = ".$rewritesql); $resultArray = $cacheSvc->load($cache_id); } else { Openbizx::$app->getLog()->log(LOG_DEBUG, "DATAOBJ", "Query Sql = " . $rewritesql); $result = $db->query($rewritesql); $resultArray = $result->fetch(); $cacheSvc->save($resultArray, $cache_id); } } else { Openbizx::$app->getLog()->log(LOG_DEBUG, "DATAOBJ", "Query Sql = " . $rewritesql); $resultSet = $db->query($rewritesql); $resultArray = $resultSet->fetch(); } } catch (Exception $e) { Openbizx::$app->getLog()->log(LOG_ERR, "DATAOBJ", "Query Error: " . $e->getMessage()); $this->errorMessage = $this->getMessage("DATA_ERROR_QUERY") . ": Rewrite:" . $rewritesql . ". Raw:" . $sql . ". " . $e->getMessage(); throw new \Openbizx\Data\Exception($this->errorMessage); return 0; } if ($has_subquery) { $record_count = (int) $resultSet->rowCount(); } else { $record_count = (int) $resultArray[0]; } return (string) $record_count; }
/** * Render this view. This function is called by Render() or ReRender() * * @return mixed either print html content or return html content if called by Render(), or void if called by ReRender() */ protected function _render() { if ($this->cacheLifeTime > 0) { $pageUrl = $this->curPageURL(); $cache_id = md5($pageUrl); //try to process cache service. $cacheSvc = Openbizx::getService(CACHE_SERVICE, 1); $cacheSvc->init($this->objectName, $this->cacheLifeTime); $this->consoleOutput = false; $output = ViewRenderer::render($this); Openbizx::$app->getLog()->log(LOG_DEBUG, "VIEW", "Set cache. url = " . $pageUrl); $cacheSvc->save($output, $cache_id); return $output; } else { $this->setClientScripts(); return ViewRenderer::render($this); } return; }
/** * Replace var expression * @objname:property, @objname:field[fldname].property, @objname:control[ctrlname].property * @:prop = @thisobjname:prop * * @global BizSystem $g_BizSystem * @param string $expression * @param object $object * @return string */ protected static function replaceVarExpr($expression, $object) { // replace @objname:property to GetObject()->getProperty(property) while (true) { // TODO: one clause must be separated by whitespace //modified by jixian for support package full name of a object //e.g : shared.objects.compaines.objCompany:Field[Id].Value $pattern = "/@([[a-zA-Z0-9_\\.]*):([a-zA-Z0-9_\\.\\[\\]]+)/"; if (!preg_match($pattern, $expression, $matches)) { break; } $macro = $matches[0]; $objName = $matches[1]; $propExpr = $matches[2]; $obj = null; if ($objName == "profile") { // @profile:attribute is reserved $profileAttribute = Openbizx::$app->getUserProfile($propExpr); $expression = str_replace($macro, $profileAttribute, $expression); continue; } if ($objName == "home") { // @home:url is reserved switch ($propExpr) { case "url": $value = "'" . OPENBIZ_APP_INDEX_URL . "'"; break; case "base_url": $value = "'" . OPENBIZ_APP_URL . "'"; break; } $expression = str_replace($macro, $value, $expression); continue; } elseif (in_array($objName, array_keys(Expression::$services))) { // reserved keywords $body = $expression; $objFunc = '@' . $objName . ':' . $propExpr; $posStart = strpos($body, $objFunc); $beforeString = substr($body, 0, $posStart); $paramStart = strpos($body, $objFunc . '(') + strlen($objFunc . '('); $paramEnd = strpos($body, ')', $paramStart); $paramLen = $paramEnd - $paramStart; $function = $propExpr; $paramString = substr($body, $paramStart, $paramLen); $restString = substr($body, $paramEnd + 1); $paramString = Expression::evaluateExpression('{' . $paramString . '}', $object); $serviceName = Expression::$services[$objName]; $serviceObj = Openbizx::getService($serviceName); $params = explode(',', $paramString); for ($i = 0; $i < count($params); $i++) { $params[$i] = trim($params[$i]); } $val_result = call_user_func_array(array($serviceObj, $function), $params); return $beforeString . $val_result . $restString; } elseif ($objName == "" || $objName == "this") { $obj = $object; $body = $expression; $objFunc = '@' . $objName . ':' . $propExpr; $posStart = strpos($body, $objFunc); $beforeString = substr($body, 0, $posStart); if (strpos($body, '(') > 0 && substr($expression, 0, 2) == '@:') { $paramStart = strpos($body, $objFunc . '(') + strlen($objFunc . '('); $paramEnd = strpos($body, ')', $paramStart); $paramLen = $paramEnd - $paramStart; $function = $propExpr; $paramString = substr($body, $paramStart, $paramLen); $restString = substr($body, $paramEnd + 1); $params = explode(',', $paramString); // bug fix for ($i = 0; $i < count($params); $i++) { $params[$i] = trim($params[$i]); } if (!is_array($params)) { $params = array(); } if (method_exists($obj, $function)) { $val_result = call_user_func_array(array($obj, $function), $params); return $beforeString . $val_result . $restString; } } } else { $obj = Openbizx::getObject($objName); } if ($obj == null) { throw new \Exception("Wrong expression syntax " . $expression . ", cannot get object " . $objName); } $pos = strpos($propExpr, "."); $paramStart = strpos($expression, $objFunc . '('); if ($pos > 0) { // in case of @objname:field[fldname].property $property1 = substr($propExpr, 0, $pos); $property2 = substr($propExpr, $pos + 1); $propertyObj = $obj->getProperty($property1); if ($propertyObj == null) { $propertyObj = $obj->getDataObj()->getProperty($property1); if ($propertyObj == null) { throw new Exception("Wrong expression syntax " . $expression . ", cannot get property object " . $property1 . " of object " . $objName); } else { $val = $propertyObj->getProperty($property2); } } $val = $propertyObj->getProperty($property2); } else { // in case of @objname:property $val = $obj->getProperty($propExpr); } if ($val === null) { $val = ""; } if (is_string($val)) { $val = "'{$val}'"; } $expression = str_replace($macro, $val, $expression); } return $expression; }
public function buildDeleteSQLwithCondition($dataObj, $condition = null) { $sql = "DELETE FROM `" . $dataObj->mainTableName . "`"; if ($condition) { $whereStr = $this->_convertSqlExpressionWithoutPrefix($dataObj, $condition); $sql .= " WHERE " . $whereStr; } // append DataPerm in the WHERE clause if ($dataObj->dataPermControl == 'Y') { $svcObj = Openbizx::getService(OPENBIZ_DATAPERM_SERVICE); $hasOwnerField = $this->_hasOwnerField($dataObj); $dataPermSQLRule = $svcObj->buildSqlRule($dataObj, 'delete', $hasOwnerField); $sqlSearchRule = $this->_convertSqlExpressionWithoutPrefix($dataObj, $dataPermSQLRule); if ($whereStr != '') { $sql .= ' AND ' . $sqlSearchRule; } else { $sql .= $sqlSearchRule; } } return $sql; }
public function getProfile() { return Openbizx::getService(PROFILE_SERVICE); }
/** * Run event log * * @return void */ protected function runEventLog() { $logMessage = $this->getEventLogMsg(); $eventName = $this->eventName; if ($logMessage && $eventName) { $logElements = $this->getOnEventElements(); $eventlog = Openbizx::getService(OPENBIZ_EVENTLOG_SERVICE); $eventlog->log($eventName, $logMessage, $logElements); } }
/** * Clean chache * * @global BizSystem $g_BizSystem * @return void */ public function cleanCache() { if ($this->cacheLifeTime > 0) { $cacheSvc = Openbizx::getService(CACHE_SERVICE, 1); $cacheSvc->init($this->objectName, $this->cacheLifeTime); $cacheSvc->cleanAll(); } }
/** * Check value type * * @param mixed $value * @return mixed|boolean */ public function checkValueType($value = null) { if (!$value) { $value = $this->value; } $validator = Openbizx::getService(VALIDATE_SERVICE); switch ($this->type) { case "Number": $result = is_numeric($value); break; case "Text": $result = is_string($value); break; case "Date": $result = $validator->date($value); break; /* case "Datetime": // zend doesn't support date time $result = $validator->date($value); break; case "Currency": $result = $validator->date($value); break; */ /* case "Datetime": // zend doesn't support date time $result = $validator->date($value); break; case "Currency": $result = $validator->date($value); break; */ case "Phone": $result = $validator->phone($value); break; default: $result = true; break; } return $result; }
/** * Get record array by converting input indexed-Value array to Field-Value pairs * * @param array $sqlArr column value pair array * @return array record array * */ public final function getRecordArr($sqlArr = null) { if ($sqlArr) { $this->_setSqlRecord($sqlArr); } $recArr = array(); foreach ($this->varValue as $key => $field) { if ($field->encrypted == 'Y') { $svcobj = Openbizx::getService(CRYPT_SERVICE); $value = $svcobj->decrypt($field->getValue()); $recArr[$key] = $value; } else { $recArr[$key] = $field->getValue(); } } return $recArr; }