/app/Models/Export.php

https://github.com/jonphipps/Metadata-Registry · PHP · 210 lines · 89 code · 23 blank · 98 comment · 3 complexity · 6e39b757c20f5f86058bd597f3b5cc81 MD5 · raw file

  1. <?php
  2. /**
  3. * Created by Reliese Model.
  4. * Date: Tue, 09 May 2017 17:01:29 +0000.
  5. */
  6. namespace App\Models;
  7. use App\Models\Traits\BelongsToElementset;
  8. use App\Models\Traits\BelongsToProfile;
  9. use App\Models\Traits\BelongsToVocabulary;
  10. use Backpack\CRUD\CrudTrait;
  11. use Culpa\Traits\Blameable;
  12. use Culpa\Traits\CreatedBy;
  13. use Illuminate\Database\Eloquent\Model;
  14. use Illuminate\Database\Eloquent\Relations\HasMany;
  15. use const true;
  16. use function is_iterable;
  17. use function unserialize;
  18. /**
  19. * App\Models\Export
  20. *
  21. * @property int $id
  22. * @property \Carbon\Carbon|null $created_at
  23. * @property \Carbon\Carbon|null $updated_at
  24. * @property int $user_id
  25. * @property int $vocabulary_id
  26. * @property int $schema_id
  27. * @property bool $exclude_deprecated
  28. * @property bool $include_generated
  29. * @property bool $include_deleted
  30. * @property bool $include_not_accepted
  31. * @property \Illuminate\Support\Collection|null $selected_columns
  32. * @property string|null $selected_language
  33. * @property string|null $published_english_version
  34. * @property string|null $published_language_version
  35. * @property \Carbon\Carbon|null $last_vocab_update
  36. * @property int $profile_id
  37. * @property string|null $file
  38. * @property \Illuminate\Support\Collection|null $map
  39. * @property-read \App\Models\Access\User\User|null $creator
  40. * @property-read \App\Models\Elementset|null $elementset
  41. * @property-read mixed $languages
  42. * @property-read mixed $worksheet
  43. * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Import[] $imports
  44. * @property-read \App\Models\Profile|null $profile
  45. * @property-read \App\Models\Vocabulary|null $vocabulary
  46. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereCreatedAt($value)
  47. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereExcludeDeprecated($value)
  48. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereFile($value)
  49. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereId($value)
  50. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereIncludeDeleted($value)
  51. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereIncludeGenerated($value)
  52. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereIncludeNotAccepted($value)
  53. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereLastVocabUpdate($value)
  54. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereMap($value)
  55. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereProfileId($value)
  56. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export wherePublishedEnglishVersion($value)
  57. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export wherePublishedLanguageVersion($value)
  58. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereSchemaId($value)
  59. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereSelectedColumns($value)
  60. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereSelectedLanguage($value)
  61. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereUpdatedAt($value)
  62. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereUserId($value)
  63. * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Export whereVocabularyId($value)
  64. * @mixin \Eloquent
  65. */
  66. class Export extends Model
  67. {
  68. const TABLE = 'reg_export_history';
  69. public $table = self::TABLE;
  70. use CrudTrait;
  71. use Blameable, CreatedBy;
  72. use BelongsToProfile, BelongsToVocabulary, BelongsToElementset;
  73. protected $blameable = [
  74. 'created' => 'user_id',
  75. ];
  76. protected $dates = [
  77. 'last_vocab_update',
  78. ];
  79. protected $guarded = ['id'];
  80. protected $casts = [
  81. 'user_id' => 'int',
  82. 'vocabulary_id' => 'int',
  83. 'schema_id' => 'int',
  84. 'exclude_deprecated' => 'bool',
  85. 'include_generated' => 'bool',
  86. 'include_deleted' => 'bool',
  87. 'include_not_accepted' => 'bool',
  88. 'profile_id' => 'int',
  89. ];
  90. public static $rules = [];
  91. /*
  92. |--------------------------------------------------------------------------
  93. | FUNCTIONS
  94. |--------------------------------------------------------------------------
  95. */
  96. public static function findByExportFileName(string $name, $extension = '.csv'): ?self
  97. {
  98. //strip the csv before searching
  99. if (ends_with($name, $extension)) {
  100. $name = str_before($name, $extension);
  101. }
  102. return self::whereFile($name . $extension)->first();
  103. }
  104. /**
  105. * @param Import|iterable $imports
  106. *
  107. * @return $this
  108. */
  109. public function addImports($imports)
  110. {
  111. if (is_iterable($imports)) {
  112. $this->imports()->saveMany($imports);
  113. } else {
  114. $this->imports()->save($imports);
  115. }
  116. return $this;
  117. }
  118. /**
  119. * @return Import|null
  120. */
  121. public function getLatestImport(): ?Import
  122. {
  123. return $this->imports()->latest()->first();
  124. }
  125. /*
  126. |--------------------------------------------------------------------------
  127. | RELATIONS
  128. |--------------------------------------------------------------------------
  129. */
  130. public function imports(): ?HasMany
  131. {
  132. return $this->hasMany(Import::class);
  133. }
  134. /*
  135. |--------------------------------------------------------------------------
  136. | SCOPES
  137. |--------------------------------------------------------------------------
  138. */
  139. /*
  140. |--------------------------------------------------------------------------
  141. | ACCESSORS
  142. |--------------------------------------------------------------------------
  143. */
  144. /**
  145. * @return \Illuminate\Support\Collection|null
  146. */
  147. public function getMapAttribute($value)
  148. {
  149. return $value ? collect(unserialize($value, [true])) : null;
  150. }
  151. public function getWorksheetAttribute(): ?string
  152. {
  153. $arr = explode('_', $this->attributes['file']);
  154. return $arr[0] ?? null;
  155. }
  156. public function getLanguagesAttribute(): ?string
  157. {
  158. $arr = explode('_', $this->attributes['file']);
  159. return $arr[1] ?? null;
  160. }
  161. /**
  162. * @return \Illuminate\Support\Collection|null
  163. */
  164. public function getSelectedColumnsAttribute($value)
  165. {
  166. return $value ? collect(unserialize($value, [true])) : null;
  167. }
  168. /*
  169. |--------------------------------------------------------------------------
  170. | MUTATORS
  171. |--------------------------------------------------------------------------
  172. */
  173. /**
  174. * @param $value
  175. */
  176. public function setMapAttribute($value): void
  177. {
  178. $value ? $this->attributes['map'] = serialize($value) : null;
  179. }
  180. /**
  181. * @param $value
  182. */
  183. public function setSelectedColumnsAttribute($value): void
  184. {
  185. $value ? $this->attributes['selected_columns'] = serialize($value) : null;
  186. }
  187. }