/** * Send file back or from to remote service for remote editing * Local repo only * * @return void redirect */ protected function _share() { // Check permission if (!$this->model->access('content')) { App::abort(403, Lang::txt('ALERTNOTAUTH')); } // Incoming $converted = Request::getInt('converted', 0); $service = Request::getVar('service', 'google'); $sync = false; $shared = 0; // Get stored remote connections $remotes = $this->_getRemoteConnections(false); // Params for repo call $params = array('subdir' => $this->subdir, 'remoteConnections' => $remotes); // Combine file and folder data $items = $this->_sortIncoming(); if (empty($items)) { $this->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_NO_FILES_TO_SHARE')); } else { // Sharing for a single file $type = key($items[0]); $item = $items[0][$type]; // Build metadata object $file = $this->repo->getMetadata($item, $type, $params); } // Build return url $url = $this->model->link('files'); $url .= $this->repo->isLocal() ? '' : '&repo=' . $this->repo->get('name'); $url .= $this->subdir ? '&subdir=' . urlencode($this->subdir) : ''; // Check user is connected if (!empty($this->_remoteService)) { $connected = $this->_connect->getStoredParam($this->_remoteService . '_token', $this->_uid); if (!$connected) { // Redirect to connect screen \Notify::message(Lang::txt('PLG_PROJECTS_FILES_REMOTE_PLEASE_CONNECT'), 'success', 'projects'); App::redirect(Route::url($this->model->link('files') . '&action=connect')); } } else { $this->setError(Lang::txt('PLG_PROJECTS_FILES_ERROR_REMOTE_NOT_ENABLED')); } // Confirmation screen if ($this->_task == 'share') { // Output HTML $view = new \Hubzero\Plugin\View(array('folder' => 'projects', 'element' => 'files', 'name' => 'share')); $view->option = $this->_option; $view->model = $this->model; $view->repo = $this->repo; $view->uid = $this->_uid; $view->subdir = $this->subdir; $view->path = $this->_path; if ($this->getError()) { $view->setError($this->getError()); } else { $view->file = !empty($file) ? $file : NULL; $view->connect = $this->_connect; $view->service = $this->_remoteService; } return $view->loadTemplate(); } // On error if ($this->getError()) { // Output error $view = new \Hubzero\Plugin\View(array('folder' => 'projects', 'element' => 'files', 'name' => 'error')); $view->title = ''; $view->option = $this->_option; $view->setError($this->getError()); return $view->loadTemplate(); } // Send file for remote editing on Google if ($this->_task == 'shareit') { // Get convertable formats $formats = \Components\Projects\Helpers\Google::getGoogleConversionExts(); // Import remote file if ($file->get('converted')) { // Load remote resource $resource = $this->_connect->loadRemoteResource($service, $this->_uid, $file->get('remoteId')); if (!$resource) { $this->setError(Lang::txt('PLG_PROJECTS_FILES_SHARING_NO_REMOTE')); } else { // Incoming $importExt = Request::getVar('format', 'pdf', 'post'); // Remove Google native extension from title $title = $file->get('name'); if (in_array($file->get('ext'), array('gdoc', 'gsheet', 'gslides', 'gdraw'))) { $title = preg_replace("/." . $file->get('ext') . "\\z/", "", $file->get('name')); } // Do we have extention in name already? - take it out $n_parts = explode('.', $title); $n_ext = count($n_parts) > 1 ? array_pop($n_parts) : ''; $title = implode($n_parts); $title .= '.' . $importExt; $newpath = $this->subdir ? $this->subdir . DS . $title : $title; $file->set('localPath', $newpath); $file->set('name', $title); $file->set('fullPath', $this->_path . DS . $newpath); // Replacing file? $exists = file_exists($file->get('fullPath')) ? 1 : 0; // Download remote file if ($this->_connect->importFile($this->_remoteService, $this->_uid, $resource, $file->get('localPath'), $this->repo->get('path'), $importExt)) { // Checkin into repo $params['file'] = $file; $this->repo->call('checkin', $params); // Remove remote resource $deleted = $this->_connect->deleteRemoteItem($this->model->get('id'), $service, $this->_uid, $file->get('remoteId'), false); // Create remote file for imported file $created = NULL; if (!$exists) { $created = $this->_connect->addRemoteFile($this->model->get('id'), $this->_remoteService, $this->_uid, $file); } // Update connection record $this->_connect->savePairing($this->model->get('id'), $this->_remoteService, $created, $file->get('localPath'), $file->get('recordId'), $file->get('originalPath'), $file->get('originalFormat'), $file->get('remoteId')); } // Output message \Notify::message(Lang::txt('PLG_PROJECTS_FILES_UNSHARE_SUCCESS') . ' ' . $title, 'success', 'projects'); // Force sync $sync = true; } } else { // Check that local file exists if (!$this->repo->fileExists($file->get('localPath'))) { $this->setError(Lang::txt('PLG_PROJECTS_FILES_SHARING_LOCAL_FILE_MISSING')); } else { // LaTeX? $tex = Components\Projects\Helpers\Compiler::isTexFile($file->get('name'), $file->getMimeType()); // Check format if (!in_array($file->get('ext'), $formats) && !$tex) { $this->setError(Lang::txt('PLG_PROJECTS_FILES_SHARING_NOT_CONVERTABLE')); } if (!$this->getError()) { if ($tex) { // LaTeX? Convert to text file first $file->set('mimeType', 'text/plain'); } if ($file->get('ext') == 'wmf') { // WMF files need this mime type specified for conversion to Google drawing $file->set('mimeType', 'application/x-msmetafile'); } if ($file->get('ext') == 'ppt' || $file->get('ext') == 'pps' || $file->get('ext') == 'pptx') { $file->set('mimeType', 'application/vnd.openxmlformats-officedocument.presentationml.presentation'); } // Convert file $added = $this->_connect->addRemoteFile($this->model->get('id'), $this->_remoteService, $this->_uid, $file, $file->get('remoteParent'), true); if ($added) { // Remove original local file $params['file'] = $file; $this->repo->deleteItem($params); // Remove original remote file $deleted = $this->_connect->deleteRemoteItem($this->model->get('id'), $this->_remoteService, $this->_uid, $file->get('remoteId'), false); $file->clear('mimeType'); $file->setMimeType(); // Update connection record $this->_connect->savePairing($this->model->get('id'), $this->_remoteService, $added, '', $file->get('recordId'), $file->get('originalPath'), $file->get('mimeType'), $file->get('remoteId')); // Output message \Notify::message(Lang::txt('PLG_PROJECTS_FILES_SHARE_SUCCESS'), 'success', 'projects'); // Force sync $sync = true; } else { // Something went wrong $this->setError(Lang::txt('PLG_PROJECTS_FILES_SHARE_ERROR_NO_CONVERT')); if ($this->_connect->getError()) { $this->setError($this->_connect->getError()); } } } } } } // Pass success or error message if ($this->getError()) { \Notify::message($this->getError(), 'error', 'projects'); } // Force sync if ($sync && $this->repo->isLocal()) { $this->model->saveParam('google_sync_queue', 1); } // Redirect to file list App::redirect(Route::url($url)); }