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

moveDir() public method

Move/rename a directory
public moveDir ( string $cabin, string $root, string $subdirectory, array $post = [] ) : boolean
$cabin string
$root string
$subdirectory string
$post array
return boolean
    public function moveDir(string $cabin, string $root, string $subdirectory, array $post = []) : bool
    {
        $this->db->beginTransaction();
        // Get the directory IDs...
        $dir = empty($root) ? $subdirectory : $root . '/' . $subdirectory;
        $nc = empty($root) ? $post['new_dir'] : $root . '/' . $post['new_dir'];
        try {
            if (empty($dir)) {
                $dirId = null;
            } else {
                $dirId = $this->getDirectoryId(\Airship\chunk($dir), $cabin);
            }
            if (empty($nc)) {
                $newDir = null;
            } else {
                $newDir = $this->getDirectoryId(\Airship\chunk($nc), $cabin);
            }
        } catch (FileNotFound $ex) {
            $this->db->rollBack();
            return false;
        }
        // Grab some facts about the existing directory
        $oldParent = $this->db->cell('SELECT parent FROM airship_dirs WHERE directoryid = ?', $dirId);
        $dirName = $this->db->cell('SELECT name FROM airship_dirs WHERE directoryid = ?', $dirId);
        // Did we move directories?
        if ($oldParent !== $newDir) {
            // Detect collisions then update if there are none
            if ($newDir) {
                $exists = $this->db->exists('SELECT
                         count(*)
                     FROM
                         airship_dirs
                     WHERE
                             parent = ?
                         AND name = ?
                         AND directoryid != ?', $newDir, $post['new_name'], $dirId);
                if ($exists) {
                    // There's already a directory here with the same name
                    $this->db->rollBack();
                    return false;
                }
                // Let's move it and, optionally, change the name too
                $this->db->update('airship_dirs', ['parent' => $newDir, 'name' => $post['new_name']], ['directoryid' => $dirId]);
            } else {
                $exists = $this->db->exists('SELECT
                         count(*)
                     FROM
                         airship_dirs
                     WHERE
                             parent IS NULL
                         AND cabin = ?
                         AND name = ?
                         AND directoryid != ?', $cabin, $post['new_name'], $dirId);
                if ($exists) {
                    // There's already a directory here with the same name
                    $this->db->rollBack();
                    return false;
                }
                // Let's move it and, optionally, change the name too
                $this->db->update('airship_dirs', ['parent' => null, 'name' => $post['new_name']], ['directoryid' => $dirId]);
            }
        } elseif ($post['new_name'] !== $dirName) {
            // Detect name collisions
            if ($newDir) {
                $exists = $this->db->exists('SELECT
                         count(*)
                     FROM
                         airship_dirs
                     WHERE
                             parent = ?
                         AND name = ?
                         AND directoryid != ?', $newDir, $post['new_name'], $dirId);
            } else {
                $exists = $this->db->exists('SELECT
                         count(*)
                     FROM
                         airship_dirs
                     WHERE 
                             parent IS NULL
                         AND cabin = ?
                         AND name = ?
                         AND directoryid != ?', $cabin, $post['new_name'], $dirId);
            }
            if ($exists) {
                // There's already a directory here with the same name
                $this->db->rollBack();
                return false;
            }
            // Change the name
            $this->db->update('airship_dirs', ['name' => $post['new_name']], ['directoryid' => $dirId]);
        } else {
            // Nothing was changed!
            $this->db->rollBack();
            return false;
        }
        return $this->db->commit();
    }

Usage Example

Example #1
0
 /**
  * Move/Rename a directory
  *
  * @param string $path
  * @param string $cabin
  */
 protected function commonMoveDir(string $path, string $cabin)
 {
     if (!$this->permCheck()) {
         \Airship\redirect($this->airship_cabin_prefix);
     }
     list($publicPath, $root) = $this->loadCommonData($path, $cabin);
     if (empty($root)) {
         \Airship\redirect($this->airship_cabin_prefix);
     }
     $forParent = \Airship\chunk($path);
     $dir_name = \array_pop($forParent);
     $parent = \implode('/', $forParent);
     $contents = $this->files->getContentsTree($cabin, $this->root_dir, $path);
     $post = $this->post(new MoveDirFilter());
     if (!empty($post)) {
         if ($this->files->moveDir($cabin, $this->root_dir, $path, $post)) {
             \Airship\redirect($this->airship_cabin_prefix . '/' . $this->path_middle . '/' . $cabin, ['dir' => $parent]);
         }
     }
     $ignore = $path . '/';
     $this->lens('files/move_dir', ['cabins' => $this->getCabinNamespaces(), 'root_dir' => $this->root_dir, 'dir_contents' => $contents, 'all_dirs' => $this->files->getDirectoryTree($cabin, $this->root_dir, $ignore), 'parent_dir' => $parent, 'dir' => $path, 'dir_name' => $dir_name, 'cabin' => $cabin, 'pathinfo' => $publicPath]);
 }