adb = PearDatabase::getInstance(); } public static function getInstanceForCurrentUser() { $currentUser = Users_Record_Model::getCurrentUserModel(); $instance = self::getInstance($currentUser->getId()); return $instance; } public static function getInstance($userId) { $adb = PearDatabase::getInstance(); $sql = "SELECT * FROM its4you_google_credentials WHERE userid=?"; $result = $adb->pquery($sql, array($userId)); if ($adb->num_rows($result) > 0) { $instance = new self(); $row = $adb->query_result_rowdata($result, 0); $type = "user"; $client_id = $row['client_id']; $client_secret = $row['client_secret']; if ($client_id == "" || $client_secret == "") { $defaultSyncAttributes = ITS4YouGoogleCalendarSync_Utils_Helper::getSyncAttributesForUser('0'); $client_id = $defaultSyncAttributes["client_id"]; $client_secret = $defaultSyncAttributes["client_secret"]; $type = "default"; if ($client_id == "" || $client_secret == "") { return false; } } $instance->setType($type); $instance->setClientId($client_id); $instance->setClientSecret($client_secret); $instance->setSyncDirection($row['sync_direction']); return $instance; } return false; } /** * @param mixed $attr_type */ public function setType($attr_type) { $this->attr_type = $attr_type; } public static function getActiveUsers() { $adb = PearDatabase::getInstance(); $sql = "SELECT * FROM its4you_google_credentials WHERE userid != ?"; $result = $adb->pquery($sql, array("0")); $instances = array(); $defaultSyncAttributes = ITS4YouGoogleCalendarSync_Utils_Helper::getSyncAttributesForUser('0'); if ($adb->num_rows($result) > 0) { while ($row = $adb->fetchByAssoc($result)) { $type = "user"; $client_id = $row['client_id']; $client_secret = $row['client_secret']; if ($client_id == "" || $client_secret == "") { $client_id = $defaultSyncAttributes["client_id"]; $client_secret = $defaultSyncAttributes["client_secret"]; $type = "default"; } if ($client_id == "" || $client_secret == "") { continue; } $instance = new self(); $instance->setType($type); $instance->setClientId($client_id); $instance->setClientSecret($client_secret); $instance->setSyncDirection($row['sync_direction']); $instance->set('userid', $row['userid']); $instances[$row['userid']] = $instance; } } return $instances; } public function save() { $adb = PearDatabase::getInstance(); $currentUser = Users_Record_Model::getCurrentUserModel(); $type = $this->getType(); $client_id = $client_secret = ""; if ($type != "default") { $client_id = $this->getClientId(); $client_secret = $this->getClientSecret(); } $result = $adb->pquery("SELECT * FROM its4you_google_credentials WHERE userid=?", array($currentUser->getId())); if ($adb->num_rows($result) > 0) { $sql = "UPDATE its4you_google_credentials SET client_id = ?, client_secret=?, sync_direction=? WHERE userid = ?"; } else { $sql = "INSERT INTO its4you_google_credentials (client_id, client_secret, sync_direction, userid) VALUES (?,?,?,?)"; } $adb->pquery($sql, array($client_id, $client_secret, $this->getSyncDirection(), $currentUser->getId())); } /** * @return array */ public function getType() { return $this->attr_type; } /** * @return array */ public function getClientId() { return $this->clientId; } /** * @param mixed $clientId */ public function setClientId($clientId) { $this->clientId = $clientId; } /** * @return mixed */ public function getClientSecret() { return $this->clientSecret; } /** * @param mixed $clientSecret */ public function setClientSecret($clientSecret) { $this->clientSecret = $clientSecret; } /** * @return mixed */ public function getSyncDirection() { return $this->syncDirection; } /** * @param mixed $syncDirection */ public function setSyncDirection($syncDirection) { $this->syncDirection = $syncDirection; } public function deactivate() { $userId = $this->get("userid"); ITS4YouGoogleCalendarSync_Oauth2_Connector::removeConnector($userId, false); } public function remove($userId) { } }