/traits/Relations.php

https://gitlab.com/nitm/yii2-filemanager · PHP · 226 lines · 157 code · 27 blank · 42 comment · 0 complexity · adbd1db6df973dd26cf9637b90a82d12 MD5 · raw file

  1. <?php
  2. namespace nitm\filemanager\traits;
  3. use nitm\filemanager\models\Image;
  4. use nitm\helpers\ArrayHelper;
  5. /**
  6. * Class Replies.
  7. */
  8. trait Relations
  9. {
  10. /**
  11. * File based relations.
  12. */
  13. protected function getFileRelationQuery($className, $link = null, $options = [], $many = false)
  14. {
  15. $link = !is_array($link) ? ['remote_id' => 'id'] : $link;
  16. $options = is_array($options) ? $options : (array) $options;
  17. $options['select'] = isset($options['select']) ? $options['select'] : '*';
  18. $options['with'] = array_merge(ArrayHelper::getValue($options, 'with', []), ['author', 'last', 'count', 'newCount']);
  19. $options['andWhere'] = isset($options['andWhere']) ? $options['andWhere'] : ['remote_type' => $this->isWhat()];
  20. $options['groupBy'] = isset($options['groupBy']) ? $options['groupBy'] : array_merge($link, [
  21. 'remote_type' => $this->isWhat(),
  22. 'hash' => null,
  23. ]);
  24. return $this->getRelationQuery($className, $link, $options, $many);
  25. }
  26. /**
  27. * File based relations.
  28. */
  29. protected function getFileRelationModelQuery($className, $link = null, $options = [])
  30. {
  31. $link = !is_array($link) ? ['remote_id' => 'id'] : $link;
  32. $options['select'] = isset($options['select']) ? $options['select'] : '*';
  33. $options['with'] = array_merge(ArrayHelper::getValue($options, 'with', []), ['count', 'newCount']);
  34. $options['andWhere'] = isset($options['andWhere']) ? $options['andWhere'] : ['remote_type' => $this->isWhat()];
  35. $options['groupBy'] = isset($options['groupBy']) ? $options['groupBy'] : array_keys(array_merge($link, [
  36. 'remote_type' => $this->isWhat(),
  37. 'id' => $this->id
  38. ]));
  39. return $this->getRelationQuery($className, $link, $options);
  40. }
  41. protected function getCachedFileRelationModel($className, $idKey = null, $many = false, $options = [])
  42. {
  43. $relation = \nitm\helpers\Helper::getCallerName();
  44. $options['construct'] = isset($options['construct']) ? $options['construct'] : [
  45. 'remote_id' => $this->getId(),
  46. 'remote_type' => $this->isWhat()
  47. ];
  48. $idKey = is_null($idKey) ? ['getId', 'isWhat'] : $idKey;
  49. return $this->getCachedRelation($idKey, $className, $options, $many, $relation);
  50. }
  51. protected function getFileRelationModel($className, $relation = null, $idKey = null, $many = false, $options = [])
  52. {
  53. $relation = $relation ?: \nitm\helpers\Helper::getCallerName();
  54. $class = get_class($this);
  55. $options['construct'] = isset($options['construct']) ? $options['construct'] : [
  56. 'remote_id' => $this->getId(),
  57. 'remote_type' => $this->isWhat(),
  58. ];
  59. $idKey = is_null($idKey) ? ['getId', 'isWhat'] : $idKey;
  60. return $this->resolveRelation($idKey, $className, true, $options, $many, $relation);
  61. }
  62. /**
  63. * Get all the images for this entity.
  64. *
  65. * @param bool $thumbnails Get thumbnails as well?
  66. * @param bool $default Get the default image as well?
  67. */
  68. public function getImages($thumbnails = false, $default = false)
  69. {
  70. return Image::getImagesFor($this, $thumbnails, $default);
  71. }
  72. public function imageList($idsOnly = false)
  73. {
  74. return ArrayHelper::filter($this->images, $idsOnly, function ($image) {
  75. $thumb = $image->getIcon('medium');
  76. /*if(!$thumb->height || !$thumb->width)
  77. $image->updateMetadataSizes('medium');
  78. if(!$image->height || !$image->width)
  79. $image->updateSizes();*/
  80. return [
  81. 'id' => $image->getId(),
  82. 'title' => ucfirst($image->remote_type).' Image',
  83. 'thumb' => $thumb->url('medium'),
  84. 'src' => $thumb->url('medium'),
  85. 'url' => $image->url(),
  86. 'height' => $thumb->height,
  87. 'width' => $thumb->width,
  88. ];
  89. });
  90. }
  91. public function images($useCache = false)
  92. {
  93. return $this->resolveRelation('id', \nitm\filemanager\models\Image::className(), $useCache, [], true, 'images');
  94. }
  95. public function image()
  96. {
  97. return $this->getFileRelationModel(\nitm\filemanager\models\Image::className(), 'image');
  98. }
  99. /**
  100. * @return \yii\db\ActiveQuery
  101. */
  102. public function getImage()
  103. {
  104. return $this->getFileRelationModelQuery(\nitm\filemanager\models\Image::className())
  105. ->with('metadata');
  106. }
  107. /**
  108. * Get the main icon for this entity.
  109. */
  110. public function getIcon()
  111. {
  112. return Image::getIconFor($this);
  113. }
  114. /**
  115. * Get metadata, either from key or all metadata.
  116. *
  117. * @param string $key
  118. *
  119. * @return mixed
  120. */
  121. public function icon()
  122. {
  123. return $this->getFileRelationModel(\nitm\filemanager\models\Image::className(), 'icon');
  124. }
  125. /**
  126. * Get files relation.
  127. *
  128. * @param array $options Options for the relation
  129. *
  130. * @return \yii\db\ActiveQuery
  131. */
  132. public function getFiles($options = [])
  133. {
  134. $options = array_merge([
  135. 'orderBy' => ['id' => SORT_DESC],
  136. 'select' => '*',
  137. ], $options);
  138. return $this->getFileRelationQuery(\nitm\filemanager\models\File::className(), null, $options, true);
  139. }
  140. public function files($useCache = false)
  141. {
  142. return $this->resolveRelation('id', \nitm\filemanager\models\File::className(), $useCache, [], true, 'files');
  143. }
  144. public function fileList($idsOnly = false)
  145. {
  146. return ArrayHelper::filter($this->files(), $idsOnly, function ($file) {
  147. return [
  148. 'title' => ucfirst($file->remote_type).' Image',
  149. 'src' => $file->url(),
  150. 'url' => $file->url(),
  151. ];
  152. });
  153. }
  154. public function file()
  155. {
  156. return $this->getFileRelationModel(\nitm\filemanager\models\File::className(), 'file');
  157. }
  158. /**
  159. * @return \yii\db\ActiveQuery
  160. */
  161. public function getFile()
  162. {
  163. return $this->getFileRelationModelQuery(\nitm\filemanager\models\File::className());
  164. }
  165. public function getImageMeta()
  166. {
  167. return $this->hasOne(Image::className(), ['remote_id' => 'id'])
  168. ->select(['remote_type', 'remote_id'])
  169. ->where(['remote_type' => $this->isWhat()])
  170. ->groupBy(['remote_id', 'remote_id'])
  171. ->with([
  172. 'count',
  173. ]);
  174. }
  175. public function imageMeta()
  176. {
  177. return $this->resolveRelation('id', Image::className(), false, [
  178. 'remote_type' => $this->isWhat(),
  179. 'remote_id' => $this->getId(),
  180. ], false, 'imageMeta');
  181. }
  182. public function getFileMeta()
  183. {
  184. return $this->hasOne(Image::className(), ['remote_id' => 'id'])
  185. ->select(['remote_type', 'remote_id'])
  186. ->where(['remote_type' => $this->isWhat()])
  187. ->groupBy(['remote_id', 'remote_id'])
  188. ->with([
  189. 'count',
  190. ]);
  191. }
  192. public function fileMeta()
  193. {
  194. return $this->resolveRelation('id', Image::className(), false, [
  195. 'remote_type' => $this->isWhat(),
  196. 'remote_id' => $this->getId(),
  197. ], false, 'fileMeta');
  198. }
  199. }