206 lines
6.1 KiB
PHP
206 lines
6.1 KiB
PHP
|
|
<?php
|
||
|
|
/*+**********************************************************************************
|
||
|
|
* The content of this file is subject to the ITS4GoogleCalendarSync license.
|
||
|
|
* ("License"); You may not use this file except in compliance with the License
|
||
|
|
* The Initial Developer of the Original Code is IT-Solutions4You s.r.o.
|
||
|
|
* Portions created by IT-Solutions4You s.r.o. are Copyright(C) IT-Solutions4You s.r.o.
|
||
|
|
* All Rights Reserved.
|
||
|
|
************************************************************************************/
|
||
|
|
|
||
|
|
class ITS4YouGoogleCalendarSync_UserCredentials_Model extends Vtiger_Base_Model
|
||
|
|
{
|
||
|
|
const BOTH_WAYS = 11;
|
||
|
|
const GOOGLE_TO_VTIGER = 10;
|
||
|
|
const VTIGER_TO_GOOGLE = 01;
|
||
|
|
protected $attr_type = false;
|
||
|
|
protected $clientId;
|
||
|
|
protected $clientSecret;
|
||
|
|
protected $syncDirection;
|
||
|
|
protected $adb;
|
||
|
|
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
parent::__construct();
|
||
|
|
$this->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)
|
||
|
|
{
|
||
|
|
}
|
||
|
|
}
|