/**
  * Renders the widget.
  */
 public function run()
 {
     if (is_null($this->variation)) {
         return '';
     }
     $asset = DisplayWidgetAsset::register($this->getView());
     $uploads = Uploads::findByReference($this->type, $this->type_id);
     return $this->render('display_widget', ['uploads' => $uploads, 'variation' => $this->variation, 'htmlOptions' => $this->htmlOptions]);
 }
 /**
  * Return array of already uploaded files. Used for display uploads in update form.
  * @param $type
  * @param $type_id
  * @param string $variation
  * @return array
  */
 public function getAlreadyUploadedByReference($type, $type_id, $variation = '_thumb')
 {
     if (is_null($type_id)) {
         return [];
     }
     $uploads = [];
     $array = Uploads::findByReference($type, $type_id);
     if (!is_null($array)) {
         foreach ($array as $item) {
             /**
              * @var $item Uploads
              */
             array_push($uploads, array('src' => $item->getPublicFileUrl($variation), 'type' => $item->mime, 'name' => $item->original, 'size' => $item->size, 'data' => array('id' => $item->id, 'type' => $item->type, 'type_id' => $item->type_id)));
         }
     }
     return $uploads;
 }
 public function getFiles($type = null, $selectFileType = null)
 {
     if ($type === null) {
         $type = $this->defaultType;
     }
     if (!is_null($selectFileType)) {
         $this->selectFileType = $selectFileType;
     }
     switch ($this->selectFileType) {
         case self::SELECT_IMAGES:
             $condition = 'width IS NOT NULL';
             break;
         case self::SELECT_FILES:
             $condition = 'width IS NULL';
             break;
         default:
             $condition = '';
     }
     return $this->owner->hasMany(Uploads::className(), ['type_id' => $this->owner->primaryKey()[0]])->andOnCondition('type =:type', [':type' => $type])->where($condition)->orderBy('ord')->all();
 }
Пример #4
0
 /**
  * @return bool
  *
  * Remove file from file system and database
  */
 public function removeFile()
 {
     $config = VariationHelper::getConfigOfType($this->type);
     $error = false;
     if ($this->unlink_files) {
         foreach ($config as $variation_name => $variation_config) {
             if ($variation_name == '_original') {
                 if (!$variation_config) {
                     continue;
                 }
                 $variation_name = 'original';
             }
             if (substr($variation_name, 0, 1) !== '_' || $variation_name == '_thumb') {
                 // delete file
                 $file = $this->getUploadFilePath($variation_name);
                 if (file_exists($file)) {
                     if (!@unlink($file)) {
                         Yii::warning('Can not unlink file: ' . $file, 'file-processor');
                         $error = true;
                     } else {
                         Yii::trace('Unlinked file: ' . $file, 'file-processor');
                     }
                 }
             }
         }
     }
     // clear attribute value in model (if needed)
     Uploads::updateConnectedModelAttribute($config, $this->type_id, null);
     if (!$error) {
         return $this->delete() ? true : false;
     }
     return false;
 }
 public function actionSort()
 {
     $sort = Yii::$app->request->post('sort', []);
     if (!is_array($sort)) {
         return false;
     }
     foreach ($sort as $k => $v) {
         $file = Uploads::findOne($v);
         if (is_null($file)) {
             continue;
         }
         $file->ord = $k;
         $file->save();
     }
     return '';
 }