Beispiel #1
0
 /**
  * Removes a file or folder
  *
  * @param string $strResource The path to the file or folder
  */
 public static function deleteResource($strResource)
 {
     $objModel = \FilesModel::findByPath($strResource);
     // Remove the resource
     if ($objModel !== null) {
         $objModel->delete();
     }
     // Look for subfolders and files
     $objFiles = \FilesModel::findMultipleByBasepath($strResource . '/');
     // Remove subfolders and files as well
     if ($objFiles !== null) {
         while ($objFiles->next()) {
             $objFiles->delete();
         }
     }
     static::updateFolderHashes(dirname($strResource));
 }
Beispiel #2
0
 /**
  * Purge the folder
  */
 public function purge()
 {
     $this->Files->rrdir($this->strFolder, true);
     // Update the database
     if ($this->blnSyncDb) {
         $objFiles = \FilesModel::findMultipleByBasepath($this->strFolder . '/');
         if ($objFiles !== null) {
             while ($objFiles->next()) {
                 $objFiles->delete();
             }
         }
         \Dbafs::updateFolderHashes($this->strFolder);
     }
 }
Beispiel #3
0
 /**
  * Save the current value
  * @param mixed
  * @throws \Exception
  */
 protected function save($varValue)
 {
     if (\Input::post('FORM_SUBMIT') != $this->strTable) {
         return;
     }
     $arrData = $GLOBALS['TL_DCA'][$this->strTable]['fields'][$this->strField];
     // File names
     if ($this->strField == 'name') {
         if (!file_exists(TL_ROOT . '/' . $this->strPath . '/' . $this->varValue . $this->strExtension) || !$this->isMounted($this->strPath . '/' . $this->varValue . $this->strExtension) || $this->varValue == $varValue) {
             return;
         }
         $this->import('Files');
         $varValue = utf8_romanize($varValue);
         // Trigger the save_callback
         if (is_array($arrData['save_callback'])) {
             foreach ($arrData['save_callback'] as $callback) {
                 $this->import($callback[0]);
                 $varValue = $this->{$callback}[0]->{$callback}[1]($varValue, $this);
             }
         }
         $this->Files->rename($this->strPath . '/' . $this->varValue . $this->strExtension, $this->strPath . '/' . $varValue . $this->strExtension);
         // Update the database
         if ($this->blnIsDbAssisted) {
             // Get the parent ID
             if ($this->strPath == $GLOBALS['TL_CONFIG']['uploadPath']) {
                 $pid = 0;
             } else {
                 $objFolder = \FilesModel::findByPath($this->strPath);
                 $pid = $objFolder->id;
             }
             // New folders
             if (stristr($this->intId, '__new__') == true) {
                 // Create the DB entry
                 $objFile = new \FilesModel();
                 $objFile->pid = $pid;
                 $objFile->tstamp = time();
                 $objFile->type = 'folder';
                 $objFile->path = $this->strPath . '/' . $varValue;
                 $objFile->name = $varValue;
                 $objFile->hash = md5('');
                 $objFile->save();
                 $this->objActiveRecord = $objFile;
                 // Add a log entry
                 $this->log('Folder "' . $this->strPath . '/' . $varValue . $this->strExtension . '" has been created', 'DC_Folder save()', TL_FILES);
             } else {
                 // Find the corresponding DB entry
                 $objFile = \FilesModel::findByPath($this->strPath . '/' . $this->varValue . $this->strExtension);
                 // Update the data
                 $objFile->pid = $pid;
                 $objFile->path = $this->strPath . '/' . $varValue . $this->strExtension;
                 $objFile->name = $varValue . $this->strExtension;
                 $objFile->save();
                 $this->objActiveRecord = $objFile;
                 // Add a log entry
                 $this->log('File or folder "' . $this->strPath . '/' . $this->varValue . $this->strExtension . '" has been renamed to "' . $this->strPath . '/' . $varValue . $this->strExtension . '"', 'DC_Folder save()', TL_FILES);
             }
             // Also update all child records
             if ($objFile->type == 'folder') {
                 $strPath = $this->strPath . '/' . $this->varValue . '/';
                 $objFiles = \FilesModel::findMultipleByBasepath($strPath);
                 if ($objFiles !== null) {
                     while ($objFiles->next()) {
                         $objFiles->path = preg_replace('@^' . $strPath . '@', $this->strPath . '/' . $varValue . '/', $objFiles->path);
                         $objFiles->save();
                     }
                 }
             }
             // Also update the MD5 hash of the parent folder
             if ($objFile->pid > 0) {
                 $objModel = \FilesModel::findByPk($objFile->pid);
                 $objFolder = new \Folder($objModel->path);
                 $objModel->hash = $objFolder->hash;
                 $objModel->save();
             }
         }
         // Set the new value so the input field can show it
         if (\Input::get('act') == 'editAll') {
             $session = $this->Session->getData();
             if (($index = array_search($this->urlEncode($this->strPath . '/' . $this->varValue) . $this->strExtension, $session['CURRENT']['IDS'])) !== false) {
                 $session['CURRENT']['IDS'][$index] = $this->urlEncode($this->strPath . '/' . $varValue) . $this->strExtension;
                 $this->Session->setData($session);
             }
         }
         $this->varValue = $varValue;
     } elseif ($this->blnIsDbAssisted) {
         // Convert date formats into timestamps
         if ($varValue != '' && in_array($arrData['eval']['rgxp'], array('date', 'time', 'datim'))) {
             $objDate = new \Date($varValue, $GLOBALS['TL_CONFIG'][$arrData['eval']['rgxp'] . 'Format']);
             $varValue = $objDate->tstamp;
         }
         // Make sure unique fields are unique
         if ($varValue != '' && $arrData['eval']['unique']) {
             $objUnique = $this->Database->prepare("SELECT * FROM " . $this->strTable . " WHERE " . $this->strField . "=? AND id!=?")->execute($varValue, $this->objActiveRecord->id);
             if ($objUnique->numRows) {
                 throw new \Exception(sprintf($GLOBALS['TL_LANG']['ERR']['unique'], $arrData['label'][0] ?: $this->strField));
             }
         }
         // Handle multi-select fields in "override all" mode
         if (\Input::get('act') == 'overrideAll' && ($arrData['inputType'] == 'checkbox' || $arrData['inputType'] == 'checkboxWizard') && $arrData['eval']['multiple']) {
             if ($this->objActiveRecord !== null) {
                 $new = deserialize($varValue, true);
                 $old = deserialize($this->objActiveRecord->{$this->strField}, true);
                 switch (\Input::post($this->strInputName . '_update')) {
                     case 'add':
                         $varValue = array_values(array_unique(array_merge($old, $new)));
                         break;
                     case 'remove':
                         $varValue = array_values(array_diff($old, $new));
                         break;
                     case 'replace':
                         $varValue = $new;
                         break;
                 }
                 if (!is_array($varValue) || empty($varValue)) {
                     $varValue = '';
                 } elseif (isset($arrData['eval']['csv'])) {
                     $varValue = implode($arrData['eval']['csv'], $varValue);
                     // see #2890
                 } else {
                     $varValue = serialize($varValue);
                 }
             }
         }
         // Convert arrays (see #2890)
         if ($arrData['eval']['multiple'] && isset($arrData['eval']['csv'])) {
             $varValue = implode($arrData['eval']['csv'], deserialize($varValue, true));
         }
         // Trigger the save_callback
         if (is_array($arrData['save_callback'])) {
             foreach ($arrData['save_callback'] as $callback) {
                 $this->import($callback[0]);
                 $varValue = $this->{$callback}[0]->{$callback}[1]($varValue, $this);
             }
         }
         // Save the value if there was no error
         if (($varValue != '' || !$arrData['eval']['doNotSaveEmpty']) && ($this->varValue != $varValue || $arrData['eval']['alwaysSave'])) {
             // If the field is a fallback field, empty all other columns
             if ($arrData['eval']['fallback'] && $varValue != '') {
                 $this->Database->execute("UPDATE " . $this->strTable . " SET " . $this->strField . "=''");
             }
             $this->objActiveRecord->{$this->strField} = $varValue;
             $this->objActiveRecord->save();
             if (!$arrData['eval']['submitOnChange']) {
                 $this->blnCreateNewVersion = true;
             }
             $this->varValue = deserialize($varValue);
         }
     }
 }
Beispiel #4
0
 protected function getOrnamentItems($path)
 {
     $elements = array('normal' => array(), 'light' => array());
     $objFiles = \FilesModel::findMultipleByBasepath($path);
     if ($objFiles->count() > 0) {
         while ($objFiles->next()) {
             $objFile = $objFiles->current();
             /* @var $objFile \Contao\FilesModel */
             if (!preg_match('/light/', $objFile->name)) {
                 $elements['normal'][] = $objFile->path;
             } else {
                 $elements['light'][] = $objFile->path;
             }
         }
     }
     return $elements;
 }