public function getParent()
 {
     if (!isset($this->_parent) && parent::getParent()) {
         $TheParent = new self();
         $this->_parent = $TheParent->copy($this->_parent);
     }
     return $this->_parent;
 }
Example #2
0
 public static function getNew($IDParent, $lastID)
 {
     $Messaggi = parent::getNew($IDParent, $lastID);
     $Ret = array();
     foreach ($Messaggi as $Messaggio) {
         $TheChat = new self();
         $Ret[] = $TheChat->copy($Messaggio);
     }
     return $Ret;
 }
Example #3
0
 public static function getNewsByID($IDNews)
 {
     $Blog = parent::getNewsByID($IDNews);
     $TheBlog = new self();
     $TheBlog->copy($Blog);
     $Author = new App_Model_User();
     $Author->copy($Blog->Author);
     $TheBlog->Author = $Author;
     return $TheBlog;
 }
Example #4
0
 public static function searchUser($Search)
 {
     $Users = parent::searchUser($Search);
     $Ret = array();
     foreach ($Users as $User) {
         $TheUser = new self();
         $Ret[] = $TheUser->copy($User);
     }
     return $Ret;
 }
Example #5
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;
 }