<?php
/**
 * This file contains the QGoogleFile class.
 *
 * @author David Maliska
 */

/**
 * Require DocBlock
 */
require_once('QGoogleDrive.class.php');

/**
 * This class allows working with files on Google Drive
 *
 * @property Google_Permission $permission contains Google permission from specific file
 * @property Google_DriveFile $file contains Google file from Google Drive 
 */
class QGoogleFile extends QGoogleDrive {
    ///////////////////////////
    // Private Member Variables
    ///////////////////////////
    
    /** @var Google_Permission Google permission */
    protected $permission;
    
    /** @var Google_DriveFile Google file */
    protected $file;
    
    //////////
    // Methods
    //////////
    /**
     * Create the QGoogleFile object.
     *
     * @return void
     */
    public function __construct() {
        parent::__construct();
        try {
            $this->permission = new Google_Permission();
            $this->file = new Google_DriveFile();
        } catch (Google_Exception $eG) {
            $this->errorGoogle = $eG->getMessage();
        } catch (Exception $e) {
            $this->error = $e->getMessage();; 
        }
    }
    
    /**
     * Delete pernamently the file.
     *
     * @param string  $fileId        The ID of the file to delete.
     *
     * @return void
     */
    public function deleteFile($fileId) {
        try {
            $this->service->files->delete($fileId);
        } catch (Google_Exception $eG) {
            $this->errorGoogle = $eG->getMessage();
        } catch (Exception $e) {
            $this->error = $e->getMessage();; 
        }
    }
    
    /**
     * Move the file to the trash.
     *
     * @param string  $fileId        The ID of the file to move to the trash.
     *
     * @return void
     */
    public function trashFile($fileId) {
        try {
            $this->service->files->trash($fileId);
        } catch (Google_Exception $eG) {
            $this->errorGoogle = $eG->getMessage();
        } catch (Exception $e) {
            $this->error = $e->getMessage(); 
        }
    }
    
    /**
     * Move the file from the trash.
     *
     * @param string  $fileId        The ID of the file to move from the trash.
     *
     * @return void
     */
    public function untrashFile($fileId) {
        try {
            $this->service->files->untrash($fileId);
        } catch (Google_Exception $eG) {
            $this->errorGoogle = $eG->getMessage();
        } catch (Exception $e) {
            $this->error = $e->getMessage(); 
        }
    }
    
    /**
     * Share the file (insert new permission).
     *
     * @param string  $fileId        The ID of the file to share.
     * @param string  $shared        The value "public", "anyone" or e-mail adress.
     *
     * @return void
     */
    public function shareFile($fileId, $shared) {
        try {
            if ($shared == 'public') {
                $this->permission->setValue('user');
                $this->permission->setType('anyone');
                $this->permission->setRole('reader');
            } elseif ($shared == 'anyone') {
                $this->permission->setValue('user');
                $this->permission->setType('anyone');
                $this->permission->setRole('reader');
                $this->permission->setWithLink(true);
            } else {
                $this->permission->setValue($shared);
                $this->permission->setType('user');
                $this->permission->setRole('reader');
            }
            $this->service->permissions->insert($fileId, $this->permission);
        } catch (Google_Exception $eG) {
            $this->errorGoogle = $eG->getMessage();
        } catch (Exception $e) {
            $this->error = $e->getMessage();
        }
    }
    
    /**
     * Retrieve a list of permissions for the file.
     *
     * @param string  $fileId        The ID of the file to retrieve permissions for.
     *
     * @return Array List of Google_Permission|null
     */
    public function retrievePermissions($fileId) {
        $result = null;
        try {
            $permissions = $this->service->permissions->listPermissions($fileId);
            $result = $permissions->getItems();
        } catch (Google_Exception $eG) {
            $this->errorGoogle = $eG->getMessage();
        } catch (Exception $e) {
            $this->error = $e->getMessage(); 
        }
        return $result;
    }
    
    /**
     * Not share the file (delete all permissions from other users).
     *
     * @param string  $fileId        The ID of the file to not share.
     *
     * @return void
     */
    public function noShareFile($fileId) {
        $arrayPermissionsId = array();
        try {
            $permissionsList = $this->retrievePermissions($fileId);
            //First element of array contains owner permission
            array_shift($permissionsList);
            foreach ($permissionsList as $permission) {
                $arrayRow = json_encode($permission);
                $arrayRow = json_decode($arrayRow , true);
                $temp = json_encode($arrayRow["id"]);
                $temp = stripslashes(str_replace('"', '', $temp));
                array_push($arrayPermissionsId, $temp);
            }
            foreach ($arrayPermissionsId as $permissionId) {
                $this->service->permissions->delete($fileId, $permissionId);
            }
        } catch (Google_Exception $eG) {
            $this->errorGoogle = $eG->getMessage();
        } catch (Exception $e) {
            $this->error = $e->getMessage(); 
        }
    }
    
    /**
     * Get download url for the file.
     *
     * @param string  $fileId        The ID of the file to download.
     *
     * @return string|null
     */
    public function downloadFile($fileId) {
        $result = null;
        try {
            $this->file = $this->service->files->get($fileId);
            $result = $this->file->getDownloadUrl();
        } catch (Google_Exception $eG) {
            $this->errorGoogle = $eG->getMessage();
        } catch (Exception $e) {
            $this->error = $e->getMessage(); 
        }
        return $result;
    }
    
    /**
     * Copty the existing file.
     *
     * @param string  $fileId        The ID of the origin file to copy.
     * @param string  $copyTitle     The Title for the copied file.
     *
     * @return void
     */
    public function copyFile($fileId, $copyTitle) {
        try {
            $this->file->setTitle($copyTitle);
            $this->service->files->copy($fileId, $this->file);
        } catch (Google_Exception $eG) {
            $this->errorGoogle = $eG->getMessage();
        } catch (Exception $e) {
            $this->error = $e->getMessage(); 
        }
    }
    
    /**
     * Rename the file.
     *
     * @param string  $fileId        The ID of the file to rename.
     * @param string  $newTitle      New title for the file.
     *
     * @return void
     */
    public function renameFile($fileId, $newTitle) {
        try {
            $this->file->setTitle($newTitle);
            $this->service->files->patch($fileId, $this->file, array(
                'fields' => 'title'
                ));
        } catch (Google_Exception $eG) {
            $this->errorGoogle = $eG->getMessage();
        } catch (Exception $e) {
            $this->error = $e->getMessage(); 
        }
    }
    
    /////////////////////////
    // Public Properties: GET
    /////////////////////////
    /**
    * PHP Magic __get method implementation.
    * @param string $strName Name of the property to be fetched.
    *
    * @return Google_Permission|Google_DriveFile|Google_Client|Google_DriveService|string|null
    * @throws Exception|QCallerException
    */
    public function __get($strName) {
        switch ($strName) {
            case 'Permission': return $this->permission;
            case 'File': return $this->file;
                
            default:
                try {
                    return parent::__get($strName);
                } catch (QCallerException $objExc) {
                    $objExc->IncrementOffset();
                    throw $objExc;
                }
        }
    }
}
?>

