Created
June 22, 2018 12:44
-
-
Save goellner/4395770bf3f085e3b53d4b7e2ec892ec to your computer and use it in GitHub Desktop.
AssetsOrganized
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Statamic\Addons\AssetsOrganized; | |
| use Statamic\Addons\Assets\AssetsFieldtype; | |
| use Statamic\Extend\Fieldtype; | |
| use Statamic\API\Helper; | |
| use Statamic\API\Asset; | |
| use Statamic\API\AssetContainer; | |
| use Statamic\API\Folder; | |
| use Statamic\API\File; | |
| class AssetsOrganizedFieldtype extends AssetsFieldtype | |
| { | |
| public $category = ['media', 'relationship']; | |
| private $asset_container_url; | |
| public function canHaveDefault() | |
| { | |
| return false; | |
| } | |
| public function blank() | |
| { | |
| return []; | |
| } | |
| public function preProcess($data) | |
| { | |
| error_log('preprocess run!'); | |
| // error_log('preProcess data: ' . print_r($data, true)); | |
| $new_data = []; | |
| $new_data['assets'] = Helper::ensureArray($data); | |
| $new_data['initialFolder'] = $this->getFieldConfig('folder'); | |
| $new_data['slug'] = ''; | |
| $new_data['asset_container'] = ''; | |
| $max_files = (int) $this->getFieldConfig('max_files'); | |
| if ($max_files === 1 && empty($data)) { | |
| return $new_data; | |
| } | |
| return Helper::ensureArray($new_data); | |
| } | |
| public function process($data) | |
| { | |
| $initial_folder = $data['initialFolder']; | |
| $slug = $data['slug']; | |
| $this->asset_container_url = $this->getAssetContainer($data['asset_container']); | |
| $ret = []; | |
| if(is_array($data['assets'])) { | |
| foreach($data['assets'] as $single_asset) { | |
| $ret[] = $this->moveAndRenameAsset($single_asset, $initial_folder, $slug); | |
| } | |
| } else { | |
| $ret[] = $this->moveAndRenameAsset($data['assets'], $initial_folder, $slug); | |
| } | |
| $this->cleanUp($initial_folder); | |
| $max_files = (int) $this->getFieldConfig('max_files'); | |
| if ($max_files === 1) { | |
| return array_get($ret, 0); | |
| } | |
| return $ret; | |
| } | |
| private function moveAndRenameAsset($single_asset, $initial_folder, $slug) { | |
| $exploded_path = explode('/', $single_asset); | |
| $file_name = array_pop($exploded_path); | |
| $new_path = $this->asset_container_url . '/' . $initial_folder . '/' . $slug . '/' . $file_name; | |
| $full_file_path = $this->asset_container_url . '/' . $initial_folder . '/' . $slug . '/' . $file_name; | |
| $move_path = $initial_folder . '/' . $slug . '/'; | |
| $asset_factory = Asset::find($single_asset); | |
| if(!File::exists($full_file_path)) { | |
| $asset_factory->move($move_path); | |
| } else { | |
| return $single_asset; | |
| } | |
| return $new_path; | |
| } | |
| private function getAssetContainer($asset_container) { | |
| $container = AssetContainer::find($asset_container); | |
| $ret = $container->url(); | |
| return $ret; | |
| } | |
| private function cleanUp($initial_folder) { | |
| Folder::deleteEmptySubfolders($this->asset_container_url . '/' . $initial_folder . '/' ); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Vue.component('assets_organized-fieldtype', { | |
| mixins: [ Fieldtype ], | |
| template: ` | |
| <div> | |
| <assets-fieldtype :slug="getSlug" :assetContainer="getAssetContainer" :name="name" :data.sync="data.assets" :config="config"></assets-fieldtype> | |
| </div> | |
| `, | |
| data: function() { | |
| return { | |
| } | |
| }, | |
| computed: { | |
| getSlug: function() { | |
| var publish = this.$root.$children.filter(function(filter) { return filter.$options.name === 'publish'; })[0]; | |
| this.data.slug = publish.formData.fields.slug; | |
| return publish.formData.fields.slug; | |
| }, | |
| getAssetContainer: function() { | |
| this.data.asset_container = this.config.container; | |
| return this.config.container; | |
| } | |
| }, | |
| methods: { | |
| updateOldSlug: function(event) { | |
| this.updateDataAssetsPath(); | |
| this.data.oldSlug = this.data.slug; | |
| }, | |
| updateDataAssetsPath: function() { | |
| if(this.data.oldSlug !== this.data.slug) { | |
| var self = this; | |
| this.data.assets.forEach(function(element, index, array) { | |
| var splitElem = element.split('/'); | |
| var oldSlug = splitElem[splitElem.length - 2]; | |
| splitElem[splitElem.length - 2] = self.data.slug; | |
| var ret = splitElem.join('/'); | |
| array[index] = ret; | |
| }); | |
| } | |
| } | |
| }, | |
| ready: function() { | |
| var publish = this.$root.$children.filter(function(filter) { return filter.$options.name === 'publish'; })[0]; | |
| publish.$on('setFlashSuccess', this.updateOldSlug ); | |
| this.data.oldSlug = this.data.slug; | |
| this.updateDataAssetsPath(); | |
| if(this.data.slug !== null && this.data.slug.length > 0) { | |
| this.config.folder = this.config.folder + '/' + this.data.slug; | |
| } | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment