Files
crm.clientright.ru/modules/ITS4YouGoogleCalendarSync/models/VtigerToGoogleMapping.php

178 lines
6.4 KiB
PHP
Raw Normal View History

<?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_VtigerToGoogleMapping_Model extends Vtiger_Base_Model
{
/**
* @var bool|Users_Record_Model
*/
private $currentUserModel = false;
/**
* @var string
*/
private $table_name = "its4you_google_vtigerGoogleMap";
private function existMapping($activityType, $userId)
{
$adb = PearDatabase::getInstance();
$sql = "SELECT 1 FROM its4you_google_vtigerGoogleMap WHERE activitytype=? AND userid= ?";
$res = $adb->pquery($sql, array($activityType, $userId));
return ($adb->getRowCount($res) == 1) ? true : false;
}
public static function getCalendarIdByActivityType($activityType, $userId)
{
$adb = PearDatabase::getInstance();
$result = $adb->pquery(
'SELECT google_calendarid FROM its4you_google_vtigerGoogleMap WHERE (activitytype=? OR activitytype=?) AND userid=?',
[
str_replace('_', ' ', $activityType),
str_replace(' ', '_', $activityType),
$userId,
]
);
$calendarId = null;
if ($adb->num_rows($result) > 0) {
while ($row = $adb->fetchByAssoc($result)) {
$calendarId = $row['google_calendarid'];
}
}
return $calendarId;
}
/**
* @return bool|Users_Record_Model
*/
public function getCurrentUserModel()
{
return $this->currentUserModel;
}
/**
* @return ITS4YouGoogleCalendarSync_VtigerToGoogleMapping_Model
*/
public static function getInstance()
{
$adb = PearDatabase::getInstance();
$currentUser = Users_Record_Model::getCurrentUserModel();
$sql = 'SELECT * FROM its4you_google_vtigerGoogleMap WHERE userid= ?';
$res = $adb->pquery($sql, array($currentUser->getId()));
$tableValues = array();
$instance = new self();
if ($adb->getRowCount($res) > 0) {
while ($row = $adb->fetchByAssoc($res)) {
$tableValues[$row['activitytype']] = $row['google_calendarid'];
}
$instance->setData($tableValues);
}
return $instance;
}
/**
* function returns picklist values for activity field of Calendar module
* values are in shape [0] => picklistValue
* @return array
*/
public function getPicklistValues()
{
$picklistValues = [];
$moduleName = 'Calendar';
if (ITS4YouGoogleCalendarSync_Utils_Helper::isModuleActive($moduleName)) {
$moduleInstance = Vtiger_Module_Model::getInstance($moduleName);
$fieldInstance = Vtiger_Field_Model::getInstance('activitytype', $moduleInstance);
$typeValues = Vtiger_Util_Helper::getRoleBasedPicklistValues($fieldInstance->getName(), $this->currentUserModel->get('roleid'));
foreach ($typeValues as $typeValue) {
$picklistValues[str_replace(' ', '_', $typeValue)] = vtranslate($typeValue, $moduleName);
}
$picklistValues['Task'] = vtranslate('Task', $moduleName);
}
$moduleName = 'ITS4YouCalendar';
if (ITS4YouGoogleCalendarSync_Utils_Helper::isModuleActive($moduleName)) {
$moduleInstance = Vtiger_Module_Model::getInstance($moduleName);
$fieldInstance = Vtiger_Field_Model::getInstance('calendar_type', $moduleInstance);
$typeValues = Vtiger_Util_Helper::getRoleBasedPicklistValues($fieldInstance->getName(), $this->currentUserModel->get('roleid'));
foreach ($typeValues as $typeValue) {
$picklistValues[$moduleName . '_' . str_replace(' ', '_', $typeValue)] = vtranslate($moduleName, $moduleName) . ': ' . vtranslate($typeValue, $moduleName);
}
}
return $picklistValues;
}
/**
* @return string
*/
public function getTableName()
{
return $this->table_name;
}
public static function getUsedGoogleCalendars()
{
$adb = PearDatabase::getInstance();
$currentUser = Users_Record_Model::getCurrentUserModel();
$sql = "SELECT google_calendarid FROM its4you_google_vtigerGoogleMap WHERE userid = ?";
$result = $adb->pquery($sql, array($currentUser->getId()));
$usedCalendars = array();
if ($adb->num_rows($result) > 0) {
while ($row = $adb->fetchByAssoc($result)) {
if ($row['google_calendarid'] != 'none') {
$usedCalendars[$row['google_calendarid']] = $row['google_calendarid'];
}
}
}
return $usedCalendars;
}
protected function initialize()
{
$this->currentUserModel = Users_Record_Model::getCurrentUserModel();
}
public function save()
{
$adb = PearDatabase::getInstance();
$currentUser = $this->getCurrentUserModel();
$insert = "INSERT into its4you_google_vtigerGoogleMap (activitytype, google_calendarid, userid) VALUES (?,?,?)";
$update = "UPDATE its4you_google_vtigerGoogleMap SET google_calendarid=? WHERE activitytype=? AND userid = ?";
foreach ($this->getPicklistValues() as $picklistKey => $picklistValue) {
if (array_key_exists($picklistKey, $this->getData())) {
if (!$this->existMapping($picklistKey, $currentUser->getId())) {
$adb->pquery($insert, array($picklistKey, $this->get($picklistKey), $currentUser->getId()));
} else {
$adb->pquery($update, array($this->get($picklistKey), $picklistKey, $currentUser->getId()));
}
}
}
}
public function __construct(array $values = array())
{
parent::__construct($values);
$this->initialize();
}
}