Ejemplo n.º 1
0
 /**
  * @param PlayerPlaylistFiles $model
  * @param CUploadedFile $file
  */
 public function __construct(PlayerPlaylistFiles $model, CUploadedFile $file)
 {
     $name = PlayerUploadedFile::createName($model, $file);
     $fullPath = PlayerUploadedFile::getSavePath() . '/' . $name;
     $file->saveAs($fullPath);
     @chmod($fullPath, 0666);
     $model->file = $name;
     $model->save();
 }
Ejemplo n.º 2
0
 protected function saveFiles($model)
 {
     $dontDelete = array();
     if (!empty($_POST['files'])) {
         $sort = 0;
         foreach ($_POST['files'] as $key => $val) {
             if (empty($val) || !isset($val['name'][0]) || empty($val['name'][0])) {
                 continue;
             }
             $index = 0;
             $file = PlayerPlaylistFiles::model()->findByAttributes(array('id' => $key));
             if (!$file) {
                 $file = new PlayerPlaylistFiles();
                 $file->playlist_id = $model->id;
             }
             $file->sort = $sort;
             $file->save(false);
             $uploadedFile = CUploadedFile::getInstanceByName('file_' . $key);
             if (!$file->file || $uploadedFile) {
                 if (!PlayerUploadedFile::hasErrors($uploadedFile)) {
                     $file->addFile($uploadedFile);
                 } else {
                     $this->setFlashMessage(Yii::t('PlayerModule.admin', 'Ошибка загрузки изображения {name}', array('{name}' => $uploadedFile->getName())));
                 }
             }
             foreach (Yii::app()->languageManager->languages as $lang) {
                 $playlistFile = PlayerPlaylistFiles::model()->language($lang->id)->findByAttributes(array('id' => $file->id));
                 $playlistFile->name = $val['name'][$index];
                 $playlistFile->save(false);
                 ++$index;
             }
             array_push($dontDelete, $file->id);
             $sort++;
         }
     }
     if ($dontDelete) {
         $cr = new CDbCriteria();
         $cr->addNotInCondition('t.id', $dontDelete);
         $filesToDelete = PlayerPlaylistFiles::model()->findAllByAttributes(array('playlist_id' => $model->id), $cr);
     } else {
         // Clear all attribute file
         $filesToDelete = PlayerPlaylistFiles::model()->findAllByAttributes(array('playlist_id' => $model->id));
     }
     if (!empty($filesToDelete)) {
         foreach ($filesToDelete as $f) {
             $f->delete();
         }
     }
 }