Airship\Cabin\Bridge\Blueprint\Files::moveFile PHP Method

moveFile() public method

Move/rename a file
public moveFile ( array $fileInfo, array $post, string $cabin ) : boolean
$fileInfo array
$post array
$cabin string
return boolean
    public function moveFile(array $fileInfo, array $post, string $cabin) : bool
    {
        $this->db->beginTransaction();
        // Get the directory IDs...
        $nc = empty($root) ? $post['new_dir'] : $root . '/' . $post['new_dir'];
        try {
            if (empty($nc)) {
                $newDir = null;
            } else {
                $newDir = $this->getDirectoryId(\Airship\chunk($nc), $cabin);
            }
        } catch (FileNotFound $ex) {
            $this->db->rollBack();
            return false;
        }
        if ($newDir === $fileInfo['directory'] && $post['new_name'] === $fileInfo['filename']) {
            // NOP
            $this->db->rollBack();
            return false;
        }
        if ($newDir === null) {
            $exists = $this->db->exists('SELECT 
                     count(*) 
                 FROM
                     airship_files
                 WHERE
                         directory IS NULL
                     AND cabin = ?
                     AND filename = ?
                     AND fileid != ?', $cabin, $post['new_name'], $fileInfo['fileid']);
            $update = ['directory' => null, 'cabin' => $cabin, 'filename' => $post['new_name']];
        } else {
            $exists = $this->db->exists('SELECT
                     count(*)
                 FROM
                     airship_files
                 WHERE
                         directory = ?
                     AND filename = ?
                     AND fileid != ?', $newDir, $post['new_name'], $fileInfo['fileid']);
            $update = ['directory' => $newDir, 'filename' => $post['new_name']];
        }
        if ($exists) {
            // There's already a directory here with the same name
            $this->db->rollBack();
            return false;
        }
        $this->db->update('airship_files', $update, ['fileid' => $fileInfo['fileid']]);
        return $this->db->commit();
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Move/rename a file
  *
  * @param string $file
  * @param string $path
  * @param string $cabin
  */
 protected function commonMoveFile(string $file, string $path, string $cabin)
 {
     if (!$this->permCheck()) {
         \Airship\redirect($this->airship_cabin_prefix);
     }
     list($publicPath, $root) = $this->loadCommonData($path, $cabin);
     if (empty($root)) {
         $fileInfo = $this->files->getFileInfo($cabin, null, $file);
     } else {
         $fileInfo = $this->files->getFileInfo($cabin, $root, $file);
     }
     $post = $this->post(new MoveFileFilter());
     if (!empty($post)) {
         $this->files->moveFile($fileInfo, $post, $cabin);
         \Airship\redirect($this->airship_cabin_prefix . '/' . $this->path_middle . '/' . $cabin, ['dir' => $path]);
     }
     $this->lens('files/move', ['cabins' => $this->getCabinNamespaces(), 'file' => $fileInfo, 'root_dir' => $this->root_dir, 'all_dirs' => $this->files->getDirectoryTree($cabin, $this->root_dir), 'dir' => $path, 'cabin' => $cabin, 'pathinfo' => $publicPath]);
 }