/traits/FileTraits.php

https://gitlab.com/nitm/yii2-filemanager · PHP · 206 lines · 150 code · 23 blank · 33 comment · 8 complexity · d67295d226ae66ca1d31a73c2a348783 MD5 · raw file

  1. <?php
  2. namespace nitm\filemanager\traits;
  3. use Yii;
  4. use nitm\models\User as Users;
  5. use nitm\filemanager\models\Image;
  6. use nitm\filemanager\helpers\Storage;
  7. use nitm\helpers\ArrayHelper;
  8. /**
  9. * This is the traits class for files.
  10. */
  11. trait FileTraits
  12. {
  13. public function fields()
  14. {
  15. $src = function ($model) {
  16. return $model->url('large');
  17. };
  18. return [
  19. 'id',
  20. 'name' => 'file_name',
  21. 'icon' => function ($model) {
  22. return $model->icon()->url('small');
  23. },
  24. 'link' => $src,
  25. 'url' => $src,
  26. 'title' => 'file_name',
  27. 'size' => function ($model) {
  28. return $model->getSize();
  29. }
  30. ];
  31. }
  32. public function extraFields()
  33. {
  34. return [];
  35. }
  36. public function getRemoteClass()
  37. {
  38. $class = $this->remote_class;
  39. return class_exists($class) ? $class::className() : \nitm\models\Data::className();
  40. }
  41. public function getSize()
  42. {
  43. if (is_numeric($this->size)) {
  44. return \Yii::$app->formatter->asShortSize($this->size);
  45. }
  46. return $this->size;
  47. }
  48. public function getFileExists()
  49. {
  50. return $this->exists();
  51. }
  52. public function exists()
  53. {
  54. return file_exists($this->getRealPath());
  55. }
  56. public function getRealPath()
  57. {
  58. try {
  59. return Storage::getUrl($this->url);
  60. } catch (\Exception $e) {
  61. \Yii::warning($e);
  62. return '';
  63. }
  64. }
  65. public function getPath()
  66. {
  67. return $this->url('file');
  68. }
  69. public function getContents()
  70. {
  71. if (Storage::exists($this->getRealPath())) {
  72. return Storage::getContents($this->getRealPath());
  73. }
  74. }
  75. public function indexUrl($as='html', $options=[])
  76. {
  77. switch ($as) {
  78. case 'modal':
  79. $options += ['_format' => 'modal'];
  80. break;
  81. default:
  82. $options += ['_format' => 'html'];
  83. break;
  84. }
  85. return \Yii::$app->urlManager->createAbsoluteUrl(array_merge([implode('/', [$this->isWhat(), 'index', $this->remote_type, $this->remote_id])], $options));
  86. }
  87. /**
  88. * Get the main icon for this entity
  89. */
  90. public function url($mode=null, $text=null, $url=null, $options=[])
  91. {
  92. return \Yii::$app->urlManager->createAbsoluteUrl("/".$this->is."/get/".$this->geturlKey($mode));
  93. }
  94. protected function getUrlKey($size='medium', $mode=null)
  95. {
  96. $ret_val = '';
  97. $filename = pathinfo($this->file_name, PATHINFO_FILENAME);
  98. switch ($mode) {
  99. case 'remote':
  100. $ret_val = implode(':', [$this->remote_type, $this->remote_id]).'/'.$filename;
  101. break;
  102. case 'file':
  103. $ret_val = $this->file_name;
  104. break;
  105. default:
  106. $ret_val = $this->getId().'/'.urlencode($filename);
  107. break;
  108. }
  109. return $ret_val;
  110. }
  111. /**
  112. * @param string $subject
  113. */
  114. public static function getSafeName($subject)
  115. {
  116. $s = (!empty($s)) ? $s : array("/([^a-zA-Z0-9\\+])/", "/([^a-zA-Z0-9]){1,}$/", "/([\s]){1,}/");
  117. $r = (!empty($r)) ? $r : array("-", "", "-");
  118. return substr(strtolower(preg_replace($s, $r, $subject)), 0, 254);
  119. }
  120. /**
  121. * @return \yii\db\ActiveQuery
  122. */
  123. public function getMetadata()
  124. {
  125. $metadataClass = $this->getMetadataClass();
  126. return $this->hasMany($metadataClass, $metadataClass::metadataLink())
  127. ->indexBy('key_name');
  128. }
  129. /**
  130. * Get all the images for this entity
  131. * @param boolean $thumbnails Get thumbnails as well?
  132. * @param boolean $default Get the default image as well?
  133. */
  134. public function getImages($thumbnails=false, $default=false)
  135. {
  136. return Image::getImagesFor($this, $thumbnails, $default);
  137. }
  138. /**
  139. * Get the main icon for this entity
  140. */
  141. public function getIcon()
  142. {
  143. return Image::getIconFor($this);
  144. }
  145. /**
  146. * Get metadata, either from key or all metadata
  147. * @param string $key
  148. * @return mixed
  149. */
  150. public function icon()
  151. {
  152. return $this->resolveRelation('id', Image::className(), true, [], false, 'icon');
  153. }
  154. /**
  155. * Get metadata, either from key or all metadata
  156. * @param string $key
  157. * @return mixed
  158. */
  159. public function metadata($key=null, $default=null)
  160. {
  161. $parts = explode('.', $key);
  162. $metadataClass = $this->getMetadataClass();
  163. $metadata = $this->resolveRelation('id', $metadataClass, false, [], true, 'metadata');
  164. if ($key == null) {
  165. return $metadata;
  166. } else {
  167. return ArrayHelper::getValue($metadata, $key, (strpos($key, '.') === false ? new $metadataClass : $default));
  168. }
  169. }
  170. /**
  171. * @return string
  172. */
  173. protected function getMetadataClass()
  174. {
  175. $class = array_flip(explode('\\', static::className()));
  176. unset($class['search']);
  177. $metadataClass = implode('\\', array_flip($class))."Metadata";
  178. if (!class_exists($metadataClass)) {
  179. $metadataClass = \nitm\filemanager\models\FileMetadata::className();
  180. }
  181. return $metadataClass;
  182. }
  183. }