sparqapi/api/application/libraries/Googleapi.php
2022-05-26 12:03:55 +05:30

235 lines
7.7 KiB
PHP

<?php
/**
* @package Google API : Google Client API
*
* @author Velz
*
*
* Description of Contact Controller
*/
if (!defined('BASEPATH'))
exit('No direct script access allowed');
include_once APPPATH.'/vendor/autoload.php';
// use Google\Service\Calendar\Event;
// use Google\Service\Calendar\EventDateTime;
class Googleapi
{
private $client;
private $calender;
private $drive;
function __construct()
{
$this->client = new Google_Client();
$this->client->setApplicationName('GmeetPOC App');
$this->client->setScopes(Google_Service_Calendar::CALENDAR_EVENTS);
$this->client->setAuthConfig($_SERVER['DOCUMENT_ROOT'].'/env/pdgenie_credentials.json');
// $this->client->setDeveloperKey('AIzaSyBFuekMZZFvQFdA4I1GLTMgYUg1vMqRR0Q');
$this->client->setAccessType('offline');
// $this->client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = $_SERVER['DOCUMENT_ROOT'].'/env/pdgenie_token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$this->client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($this->client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($this->client->getRefreshToken()) {
$this->client->fetchAccessTokenWithRefreshToken($this->client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $this->client->createAuthUrl();
echo "Open the following link in your browser:\n%s\n", $authUrl;
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $this->client->fetchAccessTokenWithAuthCode($authCode);
$this->client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($this->client->getAccessToken()));
}
// return //$this->client;
//create new calender service
$this->calender = new Google_Service_Calendar($this->client);
$this->drive = new Google_Service_Drive($this->client);
}
public function getEventListFromGoogleCalender()
{
$optParams = array(
'maxResults' => 25,
'orderBy' => 'startTime',
'singleEvents' => true,
// 'timeMin' => $timeMin,
// 'timeMax' => $timeMax,
//'timeZone' => 'Asia/Kolkata',
);
$events_list = $this->calender->events->listEvents('primary', $optParams);
$result = [];
if(count($events_list))
{
foreach ($events_list as $key => $event) {
$result[($key+1)]['id'] = $event->getId();
$result[($key+1)]['summary'] = $event->getSummary();
$result[($key+1)]['description'] = $event->getDescription();
// $result[($key+1)]['creator'] = $event->getCreator();
// $result[($key+1)]['start'] = $event->getStart()->dateTime;
// $result[($key+1)]['end'] = $event->getEnd()->dateTime;
$result[($key+1)]['gmeetlink'] = $event->getHangoutLink();
// $result[($key+1)]['link'] = $event->getHtmlLink();
}
return $result;
}
else
{
return $result;
}
}
public function getGoogleCalenderEvent($eventId)
{
try
{
$event = $events_list = $this->calender->events->get('primary', $eventId);
return $event;
}
catch(Exception $e) {
//echo 'Message: ' .$e->getMessage();
return [];
}
}
public function addGoogleCalenterEvent($eventdata)
{
// $eventObj = new Event();//cast('A',$b);
// $eventTimeObj = new EventDateTime();//cast('A',$b);
// $eventTimeObj->setDateTime('2022-05-21T00:00:00');
// $eventTimeObj->setTimeZone('Asia/Kolkata');
// $eventObj->setSummary($eventdata['summary']);
// $eventObj->setStart($eventTimeObj);
// $eventObj->setEnd($eventTimeObj);
$event = new Google_Service_Calendar_Event(array(
'summary' => $eventdata['summary'],
'location' => 'Online',
'description' => '',
'start' => array(
'dateTime' => $eventdata['start'],
'timeZone' => 'Asia/Kolkata',
),
'end' => array(
'dateTime' => $eventdata['end'],
'timeZone' => 'Asia/Kolkata',
),"conferenceData" => [
"createRequest"=> [
"requestId" => $eventdata['pdid'].'_'.$eventdata['start'].'-'.rand()]]
));
try
{
$event = $this->calender->events->insert('primary',$event,['conferenceDataVersion' => 1]);
return $event;
}
catch(Exception $e) {
echo 'Message: ' .$e->getMessage();
return [];
}
}
public function deleteGoogleCalenderEvent($eventId)
{
try
{
$event = $events_list = $this->calender->events->delete('primary', $eventId);
return $event;
}
catch(Exception $e) {
//echo 'Message: ' .$e->getMessage();
return [];
}
}
public function getDriveFilesList($gmeet_code)
{
$filter = ['q' => "mimeType='video/mp4' and name contains '".$gmeet_code."'"];
try
{
$files = $this->drive->files->listFiles($filter);
if(count($files->getFiles()))
{
// echo count($files->getFiles());
$res = [];
foreach($files->getFiles() as $key => $file)
{
$res[($key+1)]['id'] = $file->getId();
$res[($key+1)]['name'] = $file->getName();
$res[($key+1)]['type'] = $file->getMimeType();
}
return $res;
}
return [];
}
catch(Exception $e) {
//echo 'Message: ' .$e->getMessage();
return [];
}
}
public function downloadDriveFile($fileID)
{
$files = $this->drive->files->get($fileID,['alt' => 'media']);
if($files->getStatusCode() == 200)
{
return $files->getBody();
}
return false;
// echo $files->getStatusCode();
// echo $files->getContentType();
// $files = file_put_contents(__DIR__.'/temp',$files->getBody());
}
public function deleteDriveFile($fileID)
{
$files = $this->drive->files->delete($fileID);
return true;
// echo $files->getStatusCode();
// echo $files->getContentType();
// $files = file_put_contents(__DIR__.'/temp',$files->getBody());
}
}
?>