public function serviceSyncTable($oData) { FlexiLogger::debug(__METHOD__, "Running"); //var_dump($aData["type"]); //FlexiLogger::error(__METHOD__, print_r($oData, true)); $aTable = $oData->tables; $sType = $oData->type; $aMessage = array(); FlexiLogger::info(__METHOD__, "Sync type: " . $sType); $oUtil = FlexiModelUtil::getInstance(); foreach ($aTable as $sTable => $aTableSetting) { $aRow = $aTableSetting->rows; $sIDField = $aTableSetting->idfield; //FlexiLogger::error(__METHOD__, $sTable); $oModel = $oUtil->getRedBeanModel($sTable); if ($sType == "fullsync") { FlexiLogger::info(__METHOD__, "Clearing table: " . $sTable); if ($oUtil->getRedBeanDB()->tableExists($sTable)) { $oUtil->getRedBeanExecute("delete from " . $sTable); } } if (count($aRow) > 0) { $sFields = implode(",", array_keys((array) $aRow[0])); } $oUtil->setRedBeanTableIdField($sTable, $sIDField); FlexiLogger::debug(__METHOD__, $sTable . ", fields: " . $sFields); foreach ($aRow as $oRow) { $oRowData = (array) $oRow; $oModel = $oUtil->getRedBeanModel($sTable); // $sPrimaryField = "id"; // if (isset($oRowData[$sPrimaryField])) { // $oModel = $oUtil->getRedBeanModel($sTable, $oRowData[$sPrimaryField]); // } else { // $oModel = $oUtil->getRedBeanModel($sTable); // } //FlexiLogger::info(__METHOD__, "Type: " . $oModel->getMeta("type")); $oModel->import($oRowData, $sFields); FlexiLogger::info(__METHOD__, "model: " . $sTable . ", values: " . print_r($oModel->export(), true)); $oUtil->insertOrUpdateRedBean($oModel); unset($oModel); } //each records $aMessage[] = "Imported: " . $sTable . ", cnt: " . count($aRow); FlexiLogger::info(__METHOD__, "Imported: " . $sTable . ", cnt: " . count($aRow)); //if (1==1) { break; } } //each table $this->unsetToken(); FlexiLogger::info(__METHOD__, "Done"); return array("msg" => implode("\r\n<br/>", $aMessage)); }
/** * get table rows * @param array $aCond ['field:condition'=>'value'], eg: 'id:=' => 1 * @param String $sOrderby * @param String $sSelect, eg: select id, name */ public function getFetchOne($sTable, $aCond = array(), $sOrderby = null, $sSelect = null) { $select = empty($sSelect) ? "select * " : $sSelect; $aWhere = FlexiService::getWhere($aCond); $sSQL = $select . " from " . FlexiService::cleanField($sTable); $aParams = array(); if (!empty($aWhere["where"])) { $aParams = $aWhere["params"]; $sSQL .= " where " . $aWhere["where"]; } if (!empty($sOrderby)) { $sSQL .= " order by " . $sOrderby; } $sSQL .= " limit 1"; return FlexiModelUtil::getInstance()->getRedbeanFetchOne($sSQL, $aParams); }
public static function log($asType, $sMessage, $sEventId = "") { $iType = -1; switch ($asType) { case "warn": $iType = 2; break; case "error": $iType = 3; break; case "info": case "notice": $iType = 1; break; case "debug": $iType = 1; break; default: $iType = 0; } if (strlen($sEventId) > 50) { $sEventId = substr($sEventId, -50); } switch (FlexiConfig::$sFramework) { case "modx": case "modx2": global $modx; //echo "$sType:$sMessage<br/>\n"; $modx->logEvent(0, $iType, $sMessage, substr($sMessage, 0, 47) . (strlen($sMessage) > 47 ? "..." : "")); break; case "wirenet": $aLog = array("source" => $sEventId, "type" => $iType, "createdon" => gmmktime(), "user" => !empty($_SESSION['screenname']) ? $_SESSION['screenname'] : (isset($_SESSION['activenodeid']) ? $_SESSION['activenodeid'] : 0), "description" => $sMessage); FlexiModelUtil::getInstance()->insertXPDO("modx_event_log", $aLog); break; default: file_put_contents(FlexiConfig::$sLogFile, date("Ymd.H:m:s") . ":" . $sMessage . "\r\n", FILE_APPEND); } }
public function _doQuery($sTable = "", &$aCond = array(), &$aGroupBy = null, &$aOrderby = null, &$sSelect = null, &$iLimit = null, &$iOffset = 0) { //echo __METHOD__ . ": select: " . $sSelect . "<br/>\n"; $xpdo = FlexiModelUtil::getInstance()->getXPDO(); $sSQL = $this->getQuery($sTable, $aCond, $aGroupBy, $aOrderby, $sSelect, $iLimit, $iOffset); $this->onQuery($sSQL); $stmt = $xpdo->query($sSQL); if ($stmt) { return $stmt; } else { $aError = $xpdo->errorInfo(); throw new Exception("Query failed: " . $aError[2] . ":" . $sSQL); } }
public function getUserByLoginId($asLoginId, $sUserType = "user") { static $oUser = null; if ($oUser != null && $this->getUserType() == $sUserType && $this->getUserLoginId() == $asLoginId) { return $oUser; } //echo "Getting: " . $asLoginId . ", type: " . $sUserType; if ($sUserType == "seller") { $oUser = FlexiModelUtil::getInstance()->getRedbeanFetchOne("select * from " . FlexiConfig::$sDBPrefix . "artists" . " where artist_name=:user_id AND deleted = 'N'", array(":user_id" => $asLoginId)); if (empty($oUser["artist_id"])) { return null; } } else { if ($sUserType == "user") { $oUser = FlexiModelUtil::getInstance()->getRedbeanFetchOne("select * from " . FlexiConfig::$sDBPrefix . "users" . " where user_id=:user_id AND deleted = 'N'", array(":user_id" => $asLoginId)); if (empty($oUser["user_id"])) { return null; } } } return $oUser; }
public function updateTable(FlexiObject $oObject) { $bDebug = false; $aList = FlexiModelUtil::getTableSchema($oObject->sTableName); $aFieldSQL = array(); $aPrimary = array(); $sLastField = ""; $aLastPrimary = array(); foreach ($oObject->aChild["field"] as $oFieldObject) { $oField = $oFieldObject->toArray(); //finding field by old name and new name $bHasField = false; foreach ($aList as $oTableField) { $sFindName = empty($oField["oldname"]) ? $oField["sName"] : $oField["oldname"]; if ($bDebug) { echo "[" . $oTableField["Field"] . "]vs[" . $sFindName . "]"; } if ($oTableField["Field"] == $sFindName) { $bHasField = true; break; } } //if old name not found, use new name if (!$bHasField && $oField["sName"] != $oField["oldname"]) { foreach ($aList as $oTableField) { $sFindName = $oField["sName"]; if ($bDebug) { echo "[" . $oTableField["Field"] . "]vs[" . $sFindName . "]"; } if ($oTableField["Field"] == $sFindName) { $bHasField = true; break; } } } if ($oField["iStatus"] == 1) { //update or add $sSQLDefault = FlexiModelUtil::getDefaultSQL($oField["default"], $oField["cannull"]); if ($oField["primary"]) { $aPrimary[] = $oField["sName"]; } if (strpos($oTableField["Key"], "PRI") !== false) { $aLastPrimary[] = $oTableField["Field"]; } //$this->doLog("Field: " . $oField["sName"] . ", " . $oFieldObject->options . ", enum: " . $oFieldObject->getEnum()); $sFieldTypeSQL = FlexiModelUtil::getSQLName($oField["sName"]) . " " . strtoupper($oField["dbtype"]) . "" . (empty($oField["precision"]) ? "" : "(" . $oField["precision"] . ") ") . (!empty($oField["options"]) && $oField["dbtype"] == "enum" ? "(" . $oFieldObject->getEnum() . ")" : "") . " " . $sSQLDefault . " " . ($oField["autonumber"] ? "AUTO_INCREMENT" : ""); if ($bHasField) { $sFieldSQL = "CHANGE COLUMN " . FlexiModelUtil::getSQLName($sFindName) . " " . $sFieldTypeSQL; } else { $sFieldSQL = "ADD COLUMN " . $sFieldTypeSQL; } $sFieldSQL .= empty($sLastField) ? " FIRST " : " AFTER " . FlexiModelUtil::getSQLName($sLastField); $aFieldSQL[] = $sFieldSQL; $sLastField = $oField["sName"]; } else { //do delete if ($bHasField) { $aFieldSQL[] = "DROP COLUMN " . FlexiModelUtil::getSQLName($sFindName); } } } //for $sSQL = "ALTER TABLE " . FlexiModelUtil::getSQLName($oObject->sTableName) . "\n"; $sSQL .= implode(",\n", $aFieldSQL); if (!empty($aPrimary)) { $bIsSame = false; if (!empty($aLastPrimary)) { foreach ($aPrimary as $sPKey) { $bFoundKey = false; foreach ($aLastPrimary as $sLastKey) { if ($sLastKey == $sPKey) { $bFoundKey = true; $bIsSame = true; break; } } //once found a single not same, time to redo primary key if (!$bFoundKey) { $bIsSame = false; break; } } } if (!$bIsSame) { $sPrimarySQL = FlexiModelUtil::getSQLName($aPrimary); if (!empty($aLastPrimary)) { $sSQL .= "\n,DROP PRIMARY KEY"; } $sSQL .= "\n,ADD PRIMARY KEY (" . $sPrimarySQL . ")"; } } else { //new no primary key if (!empty($aLastPrimary)) { //last has primary, time to drop it $sSQL .= "\n,DROP PRIMARY KEY"; } } //$sSQL .= ";"; FlexiLogger::info(__METHOD__, "SQL: " . $sSQL); $this->logSQL($sSQL); return FlexiModelUtil::getInstance()->getXPDOExecute($sSQL); }
<?php global $aFlexiSetting; require_once $aFlexiSetting["basedir"] . "/lib/Doctrine/Doctrine.php"; if ($aFlexiSetting["doctrine"]) { spl_autoload_register(array('Doctrine', 'autoload')); } require_once $aFlexiSetting["basedir"] . "/base/Flexi/objectclass/FlexiConfig.php"; FlexiConfig::configure($aFlexiSetting); //include this file to integrate flexi controller require_once FlexiConfig::$sBaseDir . "/functions.php"; spl_autoload_register(array("FlexiConfig", "autoLoad")); require_once FlexiConfig::$sBaseDir . "/base/Flexi/controller.php"; FlexiController::$iLogLevel = FlexiConfig::$iLogLevel; //setting up DB Module $oFlexiModelUtil = FlexiModelUtil::getInstance(); $oFlexiModelUtil->setDBSetting(FlexiConfig::$sDBType, FlexiConfig::$sDBHost, FlexiConfig::$iDBPort, FlexiConfig::$sDBUser, FlexiConfig::$sDBPass, FlexiConfig::$sDBName); FlexiConfig::finalize(); //initialising main controller $oFlexi = FlexiController::getInstance();
public function getAllRoles() { $aList = FlexiModelUtil::getInstance()->getRedBeanFetchAll("select name from modx_membergroup_names order by name asc"); return $aList; }
/** * finalize setting for particular framework */ public static function finalize() { //initialise db FlexiModelUtil::getInstance()->setDBSetting(self::$sDBType, self::$sDBHost, self::$iDBPort, self::$sDBUser, self::$sDBPass, self::$sDBName); if (self::$bEnableDoctrine && !empty(self::$sDBType)) { FlexiModelUtil::getDBInstance(); } if (is_null(self::$oLoginHandler)) { if (!empty(self::$sLoginHandlerClass)) { self::setLoginHandler(new self::$sLoginHandlerClass()); } else { switch (self::$sFramework) { case "drupal": //TODO break; case "modx": self::setLoginHandler(new FlexiModXLoginHandler()); break; case "modx2": self::setLoginHandler(new FlexiModX2LoginHandler()); break; case "iscript": self::setLoginHandler(new FlexiIScriptLoginHandler()); break; case "joomla": //TODO break; case "": self::setLoginHandler(new FlexiLoginHandler()); break; default: $sClass = "Flexi" . ucfirst(self::$sFramework) . "LoginHandler"; self::setLoginHandler(new $sClass()); } //switch } } //end if }
function dbReconnect(&$pdo = null) { if (!is_null($pdo)) { return $pdo; } //there is no reconnect in pdo return FlexiModelUtil::getInstance()->getXPDO(); }
public function doLoginByToken($sToken) { //restoring based on token if (empty($sToken)) { return; } $oToken = FlexiModelUtil::getInstance()->getRedBeanFetchOne("select userid from service_token where token=:token", array(":token" => $sToken)); if ($oToken != null && $oToken !== false) { if (!$this->forceLogin($oToken["userid"])) { throw new Exception("Unable to force login: "******"userid"]); } } else { FlexiLogger::error(__METHOD__, "Invalid token: " . $sToken); } }
public function getUserById($iId) { $oModel = FlexiModelUtil::getInstance()->getRedBeanModel(FlexiConfig::$sDBPrefix . "users", $iId); if (empty($oModel->user_id)) { throw new FlexiException("User not found: " . $iId, ERROR_EOF); } return $oModel; }