pquery($sql, array($user->getId())); $tableValues = array(); $usedCalendars = array(); if ($adb->getRowCount($res) > 0) { while ($row = $adb->fetchByAssoc($res)) { $tableValues[$row['google_calendarid']] = $row['activitytype']; $usedCalendars[$row['google_calendarid']] = $row['google_calendarid']; } $instance = new self(); $instance->setData($tableValues); $instance->setUsedCalendarIds($usedCalendars); return $instance; } return null; } public static function getActivityTypeByCalendarId($calendarId, $user = false) { $adb = PearDatabase::getInstance(); if (!$user) { $user = Users_Record_Model::getCurrentUserModel(); } $sql = "SELECT activitytype FROM its4you_google_googleVtigerMap WHERE google_calendarid=? AND userid= ?"; $res = $adb->pquery($sql, array($calendarId, $user->getId())); $activityType = null; if ($adb->getRowCount($res) > 0) { while ($row = $adb->fetchByAssoc($res)) { $activityType = $row['activitytype']; } } return $activityType; } /** * @return array */ public function getUsedCalendarIds() { return $this->usedCalendarIds; } /** * @param array $usedCalendarIds */ public function setUsedCalendarIds($usedCalendarIds) { $this->usedCalendarIds = $usedCalendarIds; } public function initializeData() { $adb = PearDatabase::getInstance(); $sql = "SELECT * FROM its4you_google_googleVtigerMap WHERE userid = ?"; $result = $adb->pquery($sql, array($this->getCurrentUserModel()->getId())); while ($row = $adb->fetchByAssoc($result)) { $this->set($row['google_calendarid'], $row['activitytype']); $this->usedCalendarIds[$row['google_calendarid']] = $row['google_calendarid']; } } //TODO rename to getCalendarsFromGoogle() public function existVtigerToGoogleMapping($userId = null) { if (empty($userId)) { $userId = $this->getCurrentUserModel()->getId(); } $adb = PearDatabase::getInstance(); $sql = "SELECT 1 FROM its4you_google_vtigerGoogleMap WHERE userid = ?"; $result = $adb->pquery($sql, array($userId)); return ($adb->getRowCount($result) > 0) ? true : false; } /** * @param $client ITS4YouGoogleCalendarSync_GoogleClient_Model * @param null $userId * @return array */ public function getCalendarList($client, $userId = null) { $calendarList = $client->getCalendarList(); $usedCalendars = $this->getUsedCalendarsInVtigerToGoogleMapping(); if (empty($userId)) { $userId = $this->getCurrentUserModel()->getId(); } $result = array(); /** * @var $calendar Google_Service_Calendar_Calendar */ foreach ($calendarList as $calendar) { $myCalendar = ITS4YouGoogleCalendarSync_GoogleCalendar_Model::getInstanceFromGoogleCalendarInstace($calendar); if (in_array($calendar->getId(), $usedCalendars)) { $myCalendar->initializePicklistValuesForUser($userId); } $result[$myCalendar->getId()] = $myCalendar; } return $result; } private function getUsedCalendarsInVtigerToGoogleMapping() { $adb = PearDatabase::getInstance(); $currentUser = parent::getCurrentUserModel(); $sql = "SELECT google_calendarid FROM " . parent::getTableName() . " 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; } public function save() { $adb = PearDatabase::getInstance(); $currentUser = $this->getCurrentUserModel(); $delete = 'DELETE FROM its4you_google_googleVtigerMap WHERE userid=?'; $adb->pquery($delete, array($currentUser->getId())); foreach ($this->getData() as $key => $value) { $insert = 'INSERT INTO its4you_google_googleVtigerMap (activitytype, google_calendarid, userid) VALUES (?,?,?)'; $adb->pquery($insert, array($value, $key, $currentUser->getId())); } } public function remove($calendarId) { $adb = PearDatabase::getInstance(); $adb->setDebug(true); $currentUser = $this->getCurrentUserModel(); $select = "SELECT 1 FROM its4you_google_googleVtigerMap WHERE google_calendarid=? AND userid=?"; $result = $adb->pquery($select, array($calendarId, $currentUser->getId())); if ($adb->getRowCount($result) > 0) { $delete = "DELETE FROM its4you_google_googleVtigerMap WHERE google_calendarid=? and userid=?"; $adb->pquery($delete, array($calendarId, $currentUser->getId())); } } }