Overview

Namespaces

  • rsanchez
    • Deep
      • App
        • EE
        • Laravel
          • Facade
      • Collection
      • Hydrator
      • Model
      • Plugin
      • Relations
      • Repository

Classes

  • AbstractEntity
  • AbstractField
  • AbstractProperty
  • Asset
  • Category
  • CategoryField
  • CategoryPosts
  • Channel
  • Comment
  • Entry
  • Field
  • Fieldtype
  • File
  • GridCol
  • GridRow
  • JoinableScope
  • MatrixCol
  • MatrixRow
  • Member
  • MemberField
  • PlayaEntry
  • RelationshipEntry
  • Site
  • Title
  • UploadPref

Interfaces

  • FileInterface

Traits

  • GlobalAttributeVisibilityTrait
  • JoinableTrait
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Deep
  5:  *
  6:  * @package      rsanchez\Deep
  7:  * @author       Rob Sanchez <info@robsanchez.com>
  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:  * Model for the assets_files table, joined with assets_selections
 22:  */
 23: class Asset extends Model implements FileInterface
 24: {
 25:     use JoinableTrait;
 26: 
 27:     /**
 28:      * {@inheritdoc}
 29:      *
 30:      * @var string
 31:      */
 32:     protected $table = 'assets_files';
 33: 
 34:     /**
 35:      * {@inheritdoc}
 36:      *
 37:      * @var string
 38:      */
 39:     protected $primaryKey = 'file_id';
 40: 
 41:     /**
 42:      * {@inheritdoc}
 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:      * {@inheritdoc}
 48:      */
 49:     protected $appends = array('url');
 50: 
 51:     /**
 52:      * UploadPref model
 53:      * @var \rsanchez\Deep\Model\UploadPref
 54:      */
 55:     protected $uploadPref;
 56: 
 57:     /**
 58:      * Set the UploadPref
 59:      * @var \rsanchez\Deep\Model\UploadPref $uploadPref|null
 60:      * @return void
 61:      */
 62:     public function setUploadPref(UploadPref $uploadPref = null)
 63:     {
 64:         $this->uploadPref = $uploadPref;
 65:     }
 66: 
 67:     /**
 68:      * {@inheritdoc}
 69:      *
 70:      * @param  array                                     $assets
 71:      * @return \rsanchez\Deep\Collection\AssetCollection
 72:      */
 73:     public function newCollection(array $assets = array())
 74:     {
 75:         return new AssetCollection($assets);
 76:     }
 77: 
 78:     /**
 79:      * {@inheritdoc}
 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:      * {@inheritdoc}
 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:      * Get the date column as a Carbon object
106:      *
107:      * @param  int            $value unix time
108:      * @return \Carbon\Carbon
109:      */
110:     public function getDateAttribute($value)
111:     {
112:         return Carbon::createFromFormat('U', $value);
113:     }
114: 
115:     /**
116:      * Get the date_modified column as a Carbon object
117:      *
118:      * @param  int            $value unix time
119:      * @return \Carbon\Carbon
120:      */
121:     public function getDateModifiedAttribute($value)
122:     {
123:         return Carbon::createFromFormat('U', $value);
124:     }
125: 
126:     /**
127:      * Filter by Entry ID
128:      *
129:      * @param  \Illuminate\Database\Eloquent\Builder $query
130:      * @param  string|array                          $entryId
131:      * @return \Illuminate\Database\Eloquent\Builder
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:      * Get the json decoded settings for the source
142:      * @param  string $value json settings
143:      * @return array
144:      */
145:     public function getSourceSettingsAttribute($value)
146:     {
147:         return json_decode($this->settings);
148:     }
149: 
150:     /**
151:      * {@inheritdoc}
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:      * {@inheritdoc}
168:      */
169:     public function __toString()
170:     {
171:         return $this->getUrlAttribute();
172:     }
173: 
174:     /**
175:      * {@inheritdoc}
176:      */
177:     public static function defaultJoinTables()
178:     {
179:         return ['assets_folders', 'assets_sources', ];
180:     }
181: 
182:     /**
183:      * {@inheritdoc}
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: 
API documentation generated by ApiGen 2.8.0