Advertisement
Guest User

after attachmentbehavior

a guest
Mar 27th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.99 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\behaviors;
  4.  
  5. use Codeception\Module\Filesystem;
  6. use Imagick;
  7. use Yii;
  8. use yii\base\Behavior;
  9. use yii\helpers\FileHelper;
  10. use yii\db\ActiveRecord;
  11. use yii\web\UploadedFile;
  12.  
  13. /**
  14.  * Attachment which stored in separate field
  15.  */
  16. class AttachmentBehavior extends Behavior
  17. {
  18.     public $dir;
  19.  
  20.     public $fields = [];
  21.  
  22.     public $filesizeFields = [];
  23.  
  24.     protected $_processed_fields = [];
  25.  
  26.     protected $_dir;
  27.  
  28.  
  29.     public function events()
  30.     {
  31.         return [
  32.             ActiveRecord::EVENT_BEFORE_VALIDATE => 'validate',
  33.             ActiveRecord::EVENT_AFTER_INSERT    => 'attachEvent',
  34.             ActiveRecord::EVENT_BEFORE_UPDATE   => 'attachEvent',
  35.             ActiveRecord::EVENT_BEFORE_DELETE   => 'detachEvent',
  36.         ];
  37.     }
  38.  
  39.     public function validate($event)
  40.     {
  41.         $model = $this->owner;
  42.  
  43.         foreach ((array)$this->fields as $field) {
  44.             if (empty($model->$field)) {
  45.                 $model->$field = $model->getOldAttribute($field);
  46.                 if ($model->getIsNewRecord() && UploadedFile::getInstance($model, $field)) {
  47.                     $model->$field = 'TODO';
  48.                 }
  49.             }
  50.         }
  51.     }
  52.  
  53.     public function attachEvent($event)
  54.     {
  55.         $model = $this->owner;
  56.  
  57.         foreach ((array)$this->fields as $key => $field) {
  58.             if (in_array($field, $this->_processed_fields)) {
  59.                 continue;
  60.             }
  61.             $this->_processed_fields[] = $field;
  62.  
  63.             $old_filename = $model->getOldAttribute($field);
  64.             $upload = UploadedFile::getInstance($model, $field);
  65.             if ($upload) {
  66.  
  67.                 if ($old_filename) {
  68.                     $dir = $this->getPath($field, true);
  69.                     @unlink($dir . $old_filename);
  70.                 }
  71.  
  72.                 list($filename, $filesize) = $this->saveUploaded($field, $upload);
  73.  
  74.                 $model->{$field} = $filename;
  75.  
  76.                 if (!empty($this->filesizeFields[$key])) {
  77.                     $model->{$this->filesizeFields[$key]} = $filesize;
  78.                 }
  79.  
  80.                 if ($event->name == ActiveRecord::EVENT_AFTER_INSERT) {
  81.                     $model->save();
  82.                 }
  83.             } else {
  84.                 $model->{$field} = $old_filename;
  85.  
  86.             }
  87.         }
  88.     }
  89.  
  90.     public function detachEvent($event)
  91.     {
  92.         foreach ((array)$this->fields as $key => $field) {
  93.             $file = $this->getPath($field);
  94.             if (file_exists($file)) {
  95.                 @unlink($file);
  96.             }
  97.         }
  98.     }
  99.  
  100.     public function getPath($field = null, $only_dir = false)
  101.     {
  102.         $model = $this->owner;
  103.  
  104.         $path = $this->getDir();
  105.  
  106.         if (!$only_dir) {
  107.             if (is_null($field)) {
  108.                 $fields = (array)$this->fields;
  109.                 $field = reset($fields);
  110.             }
  111.  
  112.             $path .= $model->$field;
  113.         }
  114.  
  115.         return $path;
  116.     }
  117.  
  118.     protected function getDir()
  119.     {
  120.         if (!$this->_dir) {
  121.             $dir = preg_replace_callback('/\{([a-z0-9_]+)\}/Sui', function($m) {
  122.                 return $this->owner->{$m[1]};
  123.             }, $this->dir);
  124.  
  125.             $dir = Yii::getAlias($dir);
  126.             $this->_dir = rtrim($dir, '/') . '/';
  127.         }
  128.  
  129.         return $this->_dir;
  130.     }
  131.  
  132.     protected function saveUploaded($field, UploadedFile $file)
  133.     {
  134.  
  135.         $dir = $this->getPath($field, true);
  136.  
  137.         $file_name = $file->getBaseName();
  138.         $file_ext = $file->getExtension();
  139.  
  140.         $filename = $field .'.'. $file_ext;
  141.         $index = 0;
  142.         while (file_exists($dir . $filename)) {
  143.             $index ++;
  144.             $filename = $file_name . '-' . $index . '.' . $file_ext;
  145.         }
  146.  
  147.         FileHelper::createDirectory($dir, 0777, true);
  148.         $path = $dir . $filename;
  149.  
  150.         if ($file->saveAs($path)) {
  151.             return [$filename, filesize($path)];
  152.         }
  153.  
  154.         return false;
  155.     }
  156.  
  157.  
  158.  
  159.  
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement