Again. . . WOW!

The Demo looks fantastic and works great.

This might show how clueless I am but does elFinder add uploaded files to the database so that they can be used programatically elsewhere by Drupal?
Or would a user have to add them to the DB (and create file nodes) for use elsewhere?

Comments

ph0enix’s picture

Assigned: Unassigned » ph0enix
Category: support » feature

Currently module don't make changes to database. Filesystem connector using third party library for file manipulation and I didn't checked yet if there possible to execute additional commands after file upload.

This feature will be useful. I'll mark this issue as feature request and will research how this feature can be implemented.

scroogie’s picture

Subscribing.

troex’s picture

We will try implement minimal callback/event methods http://elrte.org/redmine/issues/435

jeffwidman’s picture

subscribe

jduguid’s picture

subscribe

trotskyicepick’s picture

You could always use WebFM to add files to the database, it adds them to a table called webfm_file.

scroogie’s picture

That doesn't help. The module needs to add them to the Drupal core "files" table.

BenVercammen’s picture

Disclaimer:

  1. the fix I'm about to post is for D7, not D6.
  2. it's quite a dirty hack, as I'm waiting for integration of 7.x-1.x-dev with elFinder 2.0 (https://drupal.org/node/1204972)

So here goes:
In the latest D7 dev version, we spotted the _upload_callback() and _remove_callback() methods in the elFinderDrupal class in the elfinder.module file. These functions were not yet being called however, so we had to override the protected _upload() and _remove() methods of the parent class (elFinder).

Check out the following code snippets (extra lines are accompanied by // PATCH :: comments):

  class elFinderDrupal extends elFinder {

	...

    /**
     * Overridden method only to add the $this->_upload_callback($file) call.
     */
    protected function _upload() {
      //parent::_upload();
      if (empty($_POST['current']) || false == ($dir = $this->_findDir(trim($_POST['current'])))) {
        return $this->_result['error'] = 'Invalid parameters';
      } 
      if (!$this->_isAllowed($dir, 'write')) {
        return $this->_result['error'] = 'Access denied';
      }
      if (empty($_FILES['upload'])) {
        return $this->_result['error'] = 'No file to upload';
      }

      $this->_logContext['upload'] = array();
      $this->_result['select'] = array();
      $total = 0;
      for ($i=0, $s = count($_FILES['upload']['name']); $i < $s; $i++) { 
        if (!empty($_FILES['upload']['name'][$i])) {
          $total++;
          $this->_logContext['upload'][] = $_FILES['upload']['name'][$i];
          if ($_FILES['upload']['error'][$i] > 0) {
            $error = 'Unable to upload file';
            switch ($_FILES['upload']['error'][$i]) {
              case UPLOAD_ERR_INI_SIZE:
              case UPLOAD_ERR_FORM_SIZE:
                $error = 'File exceeds the maximum allowed filesize';
                break;
              case UPLOAD_ERR_EXTENSION:
                $error = 'Not allowed file type';
                break;
              }
            $this->_errorData($_FILES['upload']['name'][$i], $error);
          } elseif (false == ($name = $this->_checkName($_FILES['upload']['name'][$i]))) {
            $this->_errorData($_FILES['upload']['name'][$i], 'Invalid name');
          } elseif (!$this->_isUploadAllow($_FILES['upload']['name'][$i], $_FILES['upload']['tmp_name'][$i])) {
            $this->_errorData($_FILES['upload']['name'][$i], 'Not allowed file type');					
          } else {
            $name = $this->_checkName($_FILES['upload']['name'][$i]);
            $file = $dir.DIRECTORY_SEPARATOR.$name;
            if (!@move_uploaded_file($_FILES['upload']['tmp_name'][$i], $file)) {
              $this->_errorData($_FILES['upload']['name'][$i], 'Unable to save uploaded file');	
            } else {
              @chmod($file, $this->_options['fileMode']);
              $this->_result['select'][] = $this->_hash($file);

              // PATCH :: call the callback function to add the file to the DB
              $this->_upload_callback($file);
            }
          }
        }
      }

      $errCnt = !empty($this->_result['errorData']) ? count($this->_result['errorData']) : 0;

      if ($errCnt == $total) {
      	$this->_result['error'] = 'Unable to upload files';
      } else {
      	if ($errCnt>0) {
      	  $this->_result['error'] = 'Some files was not uploaded';
      	}
        $this->_content($dir);
      }
    }
    
    /**
     * Overridden method to add the $this->_remove_callback($path) call.
     */
    protected function _remove($path) {
		if (!$this->_isAllowed($path, 'rm')) {
			return $this->_errorData($path, 'Access denied');
		}
		if (!is_dir($path)) {
			if (!@unlink($path)) {
				$this->_errorData($path, 'Unable to remove file');
			} else {
				$this->_rmTmb($path);
			}
		} else {
			$ls = scandir($path);
			for ($i=0; $i < count($ls); $i++) { 
				if ('.' != $ls[$i] && '..' != $ls[$i]) {
					$this->_remove($path.DIRECTORY_SEPARATOR.$ls[$i]);
				}
			}
			if (!@rmdir($path)) {
				return $this->_errorData($path, 'Unable to remove file');
			}
		}

		// PATCH :: call the callback function to remove the file from the DB
		$this->_remove_callback($path);
		
		return true;
	}
    

    protected function _upload_callback($file) {
      ... // the code in this method is working
    }


    protected function _remove_callback($file) {
      global $user;
      $dir = $this->_options['root'];
      $relative_path = str_replace($this->_options['root'], '', $file);
      
      // PATCH :: added the code below to this (unfinished) method.
      $uri = file_build_uri($relative_path);
      $conditions['uri'] = $uri;
      $file = file_load_multiple(array(), $conditions);
      if (!empty($file)) {
      	$fid = array_keys($file);
      	$fid = $fid[0];
      	$file = $file[$fid];
      	file_delete($file); 
      }
    }

  };

If there's already a clean way to use the callback methods, I'd love to hear of it. As long as it's not yet available, I hope this post helps someone else. The same principle can be used to add a callback for moving or renaming files via elFinder.

ph0enix’s picture

Thanks for solution! I didn't look into this feature on 1.x since elFinder 2.0 have more developer friendly api (and not only this :).

If you are interesting, you can play with development 2.x branch based on upcoming elFinder 2.0. It have full file_managed table support (upload, delete, rename, copy, move, extract, archive).

http://drupal.org/node/1204972#comment-5454564

ph0enix’s picture

Version: 6.x-0.4-beta3 » 6.x-2.x-dev
ph0enix’s picture

Status: Active » Fixed
ph0enix’s picture

Status: Fixed » Closed (fixed)