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

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

/**
 * This class upload a file to Google Drive
 *
 * @property Google_DriveFile $file contains Google file
 * @property string $mimeType contains mime type of the file
 */
class QGoogleUploadFile extends QGoogleDrive {
    ///////////////////////////
    // Private Member Variables
    ///////////////////////////
    
    /** @var Google_DriveFile Google Drive file */
    protected $file;
    
    /** @var string mime type of the file*/
    protected $mimeType;

    //////////
    // Methods
    //////////
    /**
     * Create the QGoogleUploadFile object.
     *
     * @return void
     */
    public function __construct() {
        parent::__construct();
        try {
            $this->file = new Google_DriveFile();
        } catch (Google_Exception $eG) {
            $this->errorGoogle = $eG->getMessage();
        } catch (Exception $e) {
            $this->error = $e->getMessage();
        }
        
    }
    
    /**
     * Upload the file on the Google Drive.
     *
     * @param string  $path        The path of the file to upload on the Google Drive.
     * @param string  $file        The name of the file to to upload on the Google Drive (if is null, the whole folder is upload).
     * @param string  $description The description of the upload file.
     *
     * @return void
     */
    public function uploadFile($path, $file = null, $description = null) {
        $arrayFileName = array();
        try {
            //Upload all files from folder 
            if ($file == null) {
                $dir = dir($path);
                while ($this->file = $dir->read()) {
                    if ($this->file != '.' && $this->file != '..') {
                        $arrayFileName[] = $this->file;
                    }
                }
                $dir->close();    
            }
            //Upload only one file
            else {
                $arrayFileName[] = $file; 
            }
            
            foreach ($arrayFileName as $fileName) {
                if(!$this->file) $this->file = new Google_DriveFile();
                $filePath = $path.'/'.$fileName;
                //Supported list of mime type with Google Drive
                $fileMimeType = new QGoogleMimeType($fileName);
                //Set mime type of file
                $this->mimeType = $fileMimeType->GetType();
                $this->file->setTitle($fileName);
                $this->file->setDescription($description);
                $this->file->setMimeType($this->mimeType);
                $this->service->files->insert(
                    $this->file,
                    array(
                        'data' => file_get_contents($filePath),
                        'mimeType' => $this->mimeType
                    )
                );
            }
        } 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_DriveFile|Google_Client|Google_DriveService|string|null
    * @throws Exception|QCallerException
    */
    public function __get($strName) {
        switch ($strName) {
            case 'File': return $this->file;
            case 'MimeType': return $this->mimeType;
                
            default:
                try {
                    return parent::__get($strName);
                } catch (QCallerException $objExc) {
                    $objExc->IncrementOffset();
                    throw $objExc;
                }
        }
    }
}
?>

