Example #1
0
 /**
  * Deletes an asset
  *
  * @apiMethod POST
  * @apiUri    /courses/asset/delete
  * @apiParameter {
  * 		"name":        "asset_id",
  * 		"description": "ID of asset to delete",
  * 		"type":        "integer",
  * 		"required":    true,
  * 		"default":     null
  * }
  * @apiParameter {
  * 		"name":        "scope",
  * 		"description": "Asset scope",
  * 		"type":        "string",
  * 		"required":    true,
  * 		"default":     null
  * }
  * @apiParameter {
  * 		"name":        "scope_id",
  * 		"description": "Asset scope ID",
  * 		"type":        "integer",
  * 		"required":    true,
  * 		"default":     null
  * }
  * @return    void
  */
 public function deleteTask()
 {
     // Require authentication and authorization
     $this->authorizeOrFail();
     // First, delete the asset association
     $database = App::get('db');
     $assocObj = new AssetAssociation($database);
     // Get vars
     $asset_id = Request::getInt('asset_id', 0);
     $scope = Request::getCmd('scope', 'asset_group');
     $scope_id = Request::getInt('scope_id', 0);
     // Make sure we're not missing anything
     if (!$asset_id || !$scope || !$scope_id) {
         // Missing needed variables to identify asset association
         App::abort(404, 'Missing one of asset id, scope, or scope id');
     } else {
         // Try to load the association
         if (!$assocObj->loadByAssetScope($asset_id, $scope_id, $scope)) {
             App::abort(500, 'Loading asset association failed');
         } else {
             // Delete the association
             if (!$assocObj->delete()) {
                 App::abort(500, $assocObj->getError());
             }
         }
     }
     // Then, lookup whether or not there are other assocations connected to this asset
     $assetObj = new AssetTbl($database);
     if (!$assetObj->load($asset_id)) {
         App::abort(500, "Loading asset {$id} failed");
     }
     // See if the asset is orphaned
     if (!$assetObj->isOrphaned()) {
         // Asset isn't an orphan (i.e. it's still being used elsewhere), so we're done
         $this->send(['asset_id' => $assetObj->id]);
         return;
     }
     // If no other associations exist, we'll delete the asset file and folder on the file system
     $deleted = [];
     $params = Component::params('com_courses');
     $path = DS . trim($params->get('uploadpath', '/site/courses'), DS) . DS . $this->course_id . DS . $assetObj->id;
     // If the path exists, delete it!
     if (Filesystem::exists($path)) {
         $deleted = Filesystem::listFolderTree($path);
         Filesystem::deleteDirectory($path);
     }
     // Then we'll delete the asset entry itself
     if (!$assetObj->delete()) {
         App::abort(500, $assetObj->getError());
     }
     // Return message
     $this->send(['asset_id' => $assetObj->id, 'deleted' => $deleted]);
 }
Example #2
0
 /**
  * Save a course page
  *
  * @return  void
  */
 public function saveTask()
 {
     // Check for request forgeries
     Request::checkToken();
     // load the request vars
     $fields = Request::getVar('fields', array(), 'post', 'none', 2);
     $tmpl = Request::getVar('tmpl', '');
     // instatiate course page object for saving
     $row = new Tables\Asset($this->database);
     if (!$row->bind($fields)) {
         $this->setError($row->getError());
         $this->editTask($row);
         return;
     }
     if (!$row->check()) {
         $this->setError($row->getError());
         $this->editTask($row);
         return;
     }
     if (!$row->store()) {
         $this->setError($row->getError());
         $this->editTask($row);
         return;
     }
     $fields['asset_id'] = $row->get('id');
     $row2 = new Tables\AssetAssociation($this->database);
     $row2->loadByAssetScope($fields['asset_id'], $fields['scope_id'], $fields['scope']);
     if (!$row2->id) {
         if (!$row2->bind($fields)) {
             $this->setError($row2->getError());
             $this->editTask($row);
             return;
         }
         if (!$row2->check()) {
             $this->setError($row2->getError());
             $this->editTask($row);
             return;
         }
         if (!$row2->store()) {
             $this->setError($row2->getError());
             $this->editTask($row);
             return;
         }
     }
     // Rename the temporary upload directory if it exist
     $lid = $fields['lid'];
     if ($lid != $row->get('id')) {
         $path = PATH_APP . DS . trim($this->config->get('uploadpath', '/site/courses'), DS) . DS . $fields['course_id'];
         if (is_dir($path . DS . $lid)) {
             if (!Filesystem::move($path . DS . $lid, $path . DS . $row->get('id'))) {
                 $this->setError(Lang::txt('UNABLE_TO_MOVE_PATH'));
             }
         }
     }
     // Incoming file
     /*$file = Request::getVar('upload', '', 'files', 'array');
     		if ($file['name'])
     		{
     			$path = PATH_APP . DS . trim($this->config->get('uploadpath', '/site/courses'), DS) . DS . $fields['course_id'] . DS . $row->id;
     			// Make sure the upload path exist
     			if (!is_dir($path))
     			{
     				if (!\Filesystem::makeDirectory($path))
     				{
     					$this->setError(Lang::txt('UNABLE_TO_CREATE_UPLOAD_PATH').' '.$path);
     					$this->editTask($row);
     					return;
     				}
     			}
     
     			// Make the filename safe
     			$file['name'] = Filesystem::clean($file['name']);
     			// Ensure file names fit.
     			$ext = Filesystem::extension($file['name']);
     			$file['name'] = str_replace(' ', '_', $file['name']);
     			if (strlen($file['name']) > 230)
     			{
     				$file['name'] = substr($file['name'], 0, 230);
     				$file['name'] .= '.' . $ext;
     			}
     
     			// Perform the upload
     			if (!\Filesystem::upload($file['tmp_name'], $path . DS . $file['name']))
     			{
     				$this->setError(Lang::txt('ERROR_UPLOADING'));
     			}
     			else
     			{
     				if (strtolower($ext) == 'zip')
     				{
     					require_once(PATH_CORE . DS . 'includes' . DS . 'pcl' . DS . 'pclzip.lib.php');
     
     					if (!extension_loaded('zlib'))
     					{
     						$this->setError(Lang::txt('ZLIB_PACKAGE_REQUIRED'));
     					}
     					else
     					{
     						$zip = new PclZip($path . DS . $file['name']);
     
     						// unzip the file
     						if (!($do = $zip->extract($path)))
     						{
     							$this->setError(Lang::txt('UNABLE_TO_EXTRACT_PACKAGE'));
     						}
     						else
     						{
     							@unlink($path . DS . $file['name']);
     							$file['name'] = 'presentation.json';
     						}
     					}
     				}
     
     				// Set the url
     				$row->set('url', $file['name']);
     				$row->store();
     			}
     		}*/
     if ($tmpl == 'component') {
         if ($this->getError()) {
             echo '<p class="error">' . $this->getError() . '</p>';
         } else {
             echo '<p class="message">' . Lang::txt('COM_COURSES_ITEM_SAVED') . '</p>';
         }
         return;
     }
     App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller . '&tmpl=' . $tmpl . '&scope=' . $fields['scope'] . '&scope_id=' . $fields['scope_id'] . '&course_id=' . $fields['course_id'], false));
 }