/**
  * @return \yii\db\ActiveQuery
  */
 public function getMediafile()
 {
     return $this->hasOne(Mediafile::className(), ['id' => 'mediafile_id']);
 }
 public function down()
 {
     $this->dropIndex('temp_id', \derekisbusy\filemanager\models\Mediafile::tableName());
     $this->dropColumn(\derekisbusy\filemanager\models\Mediafile::tableName(), 'temp_id');
 }
 /**
  * Load model by id
  * @param int $id
  * @return Mediafile
  */
 private function loadModel($id)
 {
     return Mediafile::findOne($id);
 }
 /**
  * Moves the files from a temporary folder to a folder for the related item.
  * 
  * @param type $related
  * @param type $itemId
  * @param type $tempId
  * @param type $defaultImage
  * @return type
  * @throws \yii\web\BadRequestHttpException
  */
 public function assignTempToItem($related, $itemId, $tempId, $defaultImage = null)
 {
     if (!isset($this->relations[$related]) || !$itemId && !$tempId) {
         throw new \yii\web\BadRequestHttpException();
     }
     // move files to item folder
     foreach (Mediafile::find()->where(['temp_id' => $tempId])->each() as $file) {
         if ($file->url == $defaultImage) {
             $file->moveTempFileToItemFolder($this->routes, $related, $itemId, $tempId);
             $defaultImage = $file->url;
         } else {
             $file->moveTempFileToItemFolder($this->routes, $related, $itemId, $tempId);
         }
         $file->createThumbs($this->routes, $this->thumbs);
         // create relation
         $thru = new $this->relations[$related]['class']();
         $thru->{$this->relations[$related]['model_id']} = $itemId;
         $thru->{$this->relations[$related]['file_id']} = $file->id;
         $thru->save();
     }
     // remove temp directory
     $uploadPath = $this->routes['uploadPath'];
     $uploadPath .= '/' . $this->routes['tempPath'] . '/' . $tempId;
     $structure = "{$this->routes['baseUrl']}/{$uploadPath}";
     $basePath = Yii::getAlias($this->routes['basePath']);
     $dir = $basePath . $structure;
     $it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
     $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
     foreach ($files as $file) {
         if ($file->isDir()) {
             rmdir($file->getRealPath());
         } else {
             unlink($file->getRealPath());
         }
     }
     rmdir($dir);
     return $defaultImage;
 }
 /** Render model info
  * @param int $id
  * @param string $strictThumb only this thumb will be selected
  * @return string
  */
 public function actionInfo($id, $strictThumb = null)
 {
     $event = new \derekisbusy\filemanager\events\InfoEvent();
     $this->module->trigger(Module::EVENT_INFO, $event);
     $model = Mediafile::findOne($id);
     return $this->renderPartial('info', ['model' => $model, 'strictThumb' => $strictThumb]);
 }