/**
  * @param $configId
  * @param $serverIndex
  * @return bool|Scalr\Farm\Role\FarmRoleStorageDevice
  */
 public static function getByConfigIdAndIndex($configId, $serverIndex)
 {
     $db = \Scalr::getDb();
     $id = $db->GetOne("SELECT storage_id FROM farm_role_storage_devices WHERE storage_config_id = ? AND server_index = ? AND status = ? LIMIT 1", array($configId, $serverIndex, self::STATUS_ACTIVE));
     if (!$id) {
         return false;
     }
     $device = new self();
     return $device->loadById($id);
 }
Example #2
0
 /**
  * Copies a directory with all sub-directories and files
  * to new destination directory.
  * The idparent in $fiels must be different from the current
  * value idparent, but may not a iddirectory of a sub-directory
  * (in case of recursion).
  * @see API/INTERFACES/SF_INTERFACE_Filesystem#copy($fields)
  * @param array $fields 
  * @return boolean Returns TRUE on success or FALSE on failure.
  */
 public function copy($fields)
 {
     if ($this->getId() <= 0) {
         throw sf_exception('error', 'directory_is_not_loaded');
         return FALSE;
     }
     if (!array_key_exists('parentid', $fields)) {
         throw sf_exception('error', 'missing_destination_parent_directory');
         return FALSE;
     }
     // check perm
     if ($this->hasPerm('copy') == FALSE) {
         throw sf_exception('error', 'permission_denied');
         return FALSE;
     }
     // check if new parent directory is a sub directory
     $tree = sf_api('MODEL', 'DirectorySqlTree');
     $tree->setIdclient($this->getField('idclient'));
     //$tree->setIdlang($this->getField('idlang'));
     $tree->setArea($this->getField('area'));
     $tree->generate();
     $children_recursive = $tree->getChildrenRecursive($this->getId());
     $children = $tree->getChildren($this->getId());
     if (in_array($fields['parentid'], $children_recursive) == TRUE) {
         throw sf_exception('error', 'destination_is_a_child_of_source');
         return FALSE;
     }
     unset($tree, $children_recursive);
     // take the current name if nothing is set
     if (!array_key_exists('name', $fields)) {
         $fields['name'] = $this->getField('name');
     }
     // check if invalid directorynames are allowed
     $sanitize_name = $this->_sanitizeDirectoryname($fields['name']);
     if ($sanitize_name === FALSE) {
         throw sf_exception('error', 'validation_error_directoryname');
         return FALSE;
     }
     $fields['name'] = $sanitize_name;
     unset($sanitize_name);
     $parentdirectory = $this->_getDirnameByIddirectory($fields['parentid']);
     $parentdirectory = $parentdirectory === FALSE ? '' : $parentdirectory;
     $source = $this->getPath();
     $dest = $this->getPath('', $parentdirectory, $fields['name']);
     $fsm = sf_api('LIB', 'FilesystemManipulation');
     if ($fields['parentid'] == $this->getField('parentid') || $fsm->isDir($dest)) {
         throw sf_exception('error', 'directory_exists_in_destination', array('source' => $source, 'dest' => $dest));
         return FALSE;
     }
     if ($fsm->copyDirectory($source, $dest) == FALSE) {
         throw sf_exception('error', 'copy_directory_failed', array('source' => $source, 'dest' => $dest));
         return FALSE;
     }
     // clone this object to hold the data
     $newitem = clone $this;
     foreach ($fields as $field => $value) {
         $newitem->setField($field, $value);
     }
     // reset ids to save as new item
     $newitem->setField('id', 0, $this->table_directory);
     $newitem->setField('id', 0, $this->table_directory_lang);
     $newitem->setField('iddirectory', 0, $this->table_directory_lang);
     // update dirname manually
     $newitem->setField('dirname', $this->getRelativePath($parentdirectory, $fields['name']));
     // save table_directory first ...
     if ($newitem->save($this->table_directory) == FALSE) {
         // try to delete file from destination
         $fsm->deleteDirectory($dest);
         throw sf_exception('error', 'save_directory_to_db_failed', array('dirname' => $fields['dirname']));
         return FALSE;
     }
     // ... and copy all available language afterwards
     $tablefields = $this->getMappedFieldsForTable($this->table_directory_lang);
     $insert_fields = implode(",", $tablefields);
     // reset iddirectorylang
     $index = array_search($this->mapFieldToRow('id', $this->table_directory_lang), $tablefields);
     if ($index !== FALSE) {
         $tablefields[$index] = '0';
     }
     // set iddirectory to new item id
     $index = array_search($this->mapFieldToRow('id', $this->table_directory), $tablefields);
     if ($index !== FALSE) {
         $tablefields[$index] = $newitem->getId();
     }
     $select_fields = implode(",", $tablefields);
     $sql = "INSERT INTO\r\n\t\t\t\t\t" . $this->table_directory_lang . "\r\n\t\t\t\t\t(" . $insert_fields . ")\r\n\t\t\t\t\tSELECT " . $select_fields . "\r\n\t\t\t\t\t\tFROM " . $this->table_directory_lang . "\r\n\t\t\t\t\t\tWHERE " . $this->mapFieldToRow('id', $this->table_directory) . " = '" . $this->getId() . "';";
     //echo $sql."<br />";
     $rs = $this->db->Execute($sql);
     if ($rs === FALSE) {
         // try to delete file from destination
         $fsm->deleteDirectory($dest);
         throw sf_exception('error', 'save_directory_to_db_failed', array('dirname' => $fields['dirname']));
         return FALSE;
     }
     $rs->Close();
     // copy rights
     if ($this->copyPerms($newitem->getId(), 0, $fields['idlang'], TRUE) == FALSE) {
         throw sf_exception('warning', 'copy_rights_failed', array('source' => $source, 'dest' => $dest));
         return FALSE;
     }
     // after creating the root directory copy the sub directories and files
     try {
         // copy files of directory
         $filecol = sf_api('MODEL', 'FileSqlCollection');
         $filecol->setIdclient($this->getField('idclient'));
         $filecol->setIdlang($this->getField('idlang'));
         $filecol->setFreefilter('area', $this->getField('area'));
         $filecol->setFreefilter('iddirectory', $this->getId());
         $filecol->generate();
         if ($filecol->getCount() > 0) {
             $iter = $filecol->getItemsAsIterator();
             while ($iter->valid()) {
                 $file = $iter->current();
                 $filedata = array('iddirectory' => $newitem->getId());
                 //echo "copy file ".$file->getField('filename')." from ".$file->getField('iddirectory')." to ".$newitem->getId()."<br />";
                 $file->copy($filedata);
                 $iter->next();
             }
         }
         unset($filecol);
         // copy sub directories
         foreach ($children as $childdir) {
             $dir = new self();
             $dir->loadById($childdir);
             $dirdata = array('parentid' => $newitem->getId());
             //echo "copy dir ".$dir->getField('filename')." from ".$dir->getId()." to ".$newitem->getId()."<br />";
             $dir->copy($dirdata);
         }
     } catch (Exception $e) {
         // TODO collect the recursive exeptions and create a new one in the top item
         echo $e->getMessage() . "<br />";
     }
     return TRUE;
 }