1: <?php
2:
3: 4: 5: 6: 7: 8:
9:
10: namespace rsanchez\Deep\Model;
11:
12: use Illuminate\Database\Eloquent\Model;
13: use Illuminate\Database\Eloquent\Builder;
14: use rsanchez\Deep\Model\JoinableTrait;
15: use rsanchez\Deep\Model\FileInterface;
16: use rsanchez\Deep\Collection\AssetCollection;
17: use rsanchez\Deep\Model\UploadPref;
18: use Carbon\Carbon;
19:
20: 21: 22:
23: class Asset extends Model implements FileInterface
24: {
25: use JoinableTrait;
26:
27: 28: 29: 30: 31:
32: protected $table = 'assets_files';
33:
34: 35: 36: 37: 38:
39: protected $primaryKey = 'file_id';
40:
41: 42: 43:
44: protected $hidden = array('file_id', 'folder_id', 'source_type', 'source_id', 'filedir_id', 'entry_id', 'field_id', 'col_id', 'row_id', 'var_id', 'element_id', 'content_type', 'sort_order', 'is_draft', 'uploadPref', 'source_type', 'folder_name', 'full_path', 'parent_id', 'name', 'settings', );
45:
46: 47: 48:
49: protected $appends = array('url');
50:
51: 52: 53: 54:
55: protected $uploadPref;
56:
57: 58: 59: 60: 61:
62: public function setUploadPref(UploadPref $uploadPref = null)
63: {
64: $this->uploadPref = $uploadPref;
65: }
66:
67: 68: 69: 70: 71: 72:
73: public function newCollection(array $assets = array())
74: {
75: return new AssetCollection($assets);
76: }
77:
78: 79: 80:
81: public function getUrlAttribute()
82: {
83: if (is_null($this->uploadPref) && $this->source_settings) {
84: $base = $this->source_settings->url_prefix.$this->full_path;
85: } else {
86: $base = $this->uploadPref->url.$this->full_path;
87: }
88:
89: return $base.$this->file_name;
90: }
91:
92: 93: 94:
95: public function getServerPathAttribute()
96: {
97: if (is_null($this->uploadPref) && $this->source_settings) {
98: return null;
99: }
100:
101: return $this->uploadPref->server_path.$this->full_path.$this->file_name;
102: }
103:
104: 105: 106: 107: 108: 109:
110: public function getDateAttribute($value)
111: {
112: return Carbon::createFromFormat('U', $value);
113: }
114:
115: 116: 117: 118: 119: 120:
121: public function getDateModifiedAttribute($value)
122: {
123: return Carbon::createFromFormat('U', $value);
124: }
125:
126: 127: 128: 129: 130: 131: 132:
133: public function scopeEntryId(Builder $query, $entryId)
134: {
135: $entryId = is_array($entryId) ? $entryId : array($entryId);
136:
137: return $this->requireTable($query, 'assets_selections')->whereIn('assets_selections.entry_id', $entryId);
138: }
139:
140: 141: 142: 143: 144:
145: public function getSourceSettingsAttribute($value)
146: {
147: return json_decode($this->settings);
148: }
149:
150: 151: 152:
153: public function attributesToArray()
154: {
155: $attributes = parent::attributesToArray();
156:
157: foreach (array('date', 'date_modified') as $key) {
158: if (isset($attributes[$key]) && $attributes[$key] instanceof Carbon) {
159: $attributes[$key] = (string) $attributes[$key];
160: }
161: }
162:
163: return $attributes;
164: }
165:
166: 167: 168:
169: public function __toString()
170: {
171: return $this->getUrlAttribute();
172: }
173:
174: 175: 176:
177: public static function defaultJoinTables()
178: {
179: return ['assets_folders', 'assets_sources', ];
180: }
181:
182: 183: 184:
185: protected static function joinTables()
186: {
187: return array(
188: 'assets_selections' => function ($query) {
189: $query->join('assets_selections', 'assets_selections.file_id', '=', 'assets_files.file_id');
190: },
191: 'assets_folders' => function ($query) {
192: $query->join('assets_folders', 'assets_folders.folder_id', '=', 'assets_files.folder_id');
193: },
194: 'assets_sources' => function ($query) {
195: $query->leftJoin('assets_sources', 'assets_sources.source_id', '=', 'assets_files.source_id');
196: },
197: );
198: }
199: }
200: