Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
evsmash/files / libs / File.php
Size: Mime:
<?php

namespace Evsmash\Files;

use Evsmash\Core\Database\Eloquent;

use Evsmash\Core\Database\SoftDeleting;
use Evsmash\Core\Dec\Dec;
use Evsmash\Core\Dec\Image;
use Evsmash\Core\Dec\Thumb;
use Evsmash\Core\Helpers\Str;
use Evsmash\Core\Input\Route;
use Evsmash\Core\Simpy\Params;

use Evsmash\Files\Schemas\Files as Schema;

class File extends Eloquent {

	use SoftDeleting;

	// validate
	static public function validate() {

		return Schema::validate();

	}

	// belongs to directory
	public function directory() { 
		
		return $this->belongsTo('Evsmash\Files\FilesDirectory');
	
	}

	// params
	public function scopeParams($query, $params = false) {

		$scope = new Params();
		$scope->scope($query, $params);
		$scope->where('ext');
		if(Route::get('type') !== 'file') {
			$scope->where('type');
		}
		$scope->search(['name', 'description']);
		$scope->order(['created_at' => 'desc']);
		return $scope->query;

	}

	// extensions
	static public function extensions() {

		return File::where('ext', '!=', '')->orderBy('ext', 'asc')->groupBy('ext')->pluck('ext', 'ext');

	}

	// form
	static public function form($file) {

		if($file->type == 'image' AND Route::get('type') == 'image') {
			if(empty($file->resolution)) {

				// get
				$img = Image::make(storage.'/uploads/'.$file->file);
				$resolution = $img->width().'x'.$img->height();

				// update
				$file->resolution = $resolution;
				$file->save();

			}

			// output
			$html = '<form class="form-inline">';
			$html .= '<div class="custom-control custom-checkbox"><input type="checkbox" class="insert-keep form-control custom-control-input" checked id="file'.$file->id.'" /><label class="custom-control-label" style="display: block; margin-right: 10px;" for="file'.$file->id.'">'.t('Ratio').'</label></div>';
			$html .= '&nbsp;';
			$html .= '<input type="text" value="'.explode('x', $file->resolution)[0].'" class="insert-width form-control" style="width: 70px;" />';
			$html .= '&nbsp;x&nbsp;';
			$html .= '<input type="text" value="'.explode('x', $file->resolution)[1].'" class="insert-height form-control" style="width: 70px;" />';
			$html .= '&nbsp;';
			$html .= '<input type="hidden" value="'.explode('x', $file->resolution)[0]/explode('x', $file->resolution)[1].'" class="insert-ratio" />';
			$html .= '<input type="hidden" value="'.str_replace('/', '::', $file->file).'" class="insert-name" />';
			$html .= '<button class="btn btn-xs btn-warning insert-file">'.t('Insert').'</button>';
			$html .= '</form>';
			
		} else {

			// output
			$html = '<form style="float: left;">';
			$html .= '<input type="hidden" value="/download/'.$file->id.'/'.Str::slug($file->name).'" class="insert-value" />';
			$html .= '<button class="btn btn-xs btn-warning insert-file">'.t('Link').'</button>';
			$html .= '</form>';
			if(\Evsmash\Core\Files\File::ext($file->file) == 'jpg' OR \Evsmash\Core\Files\File::ext($file->file) == 'png') { 
				$html .= '<form style="float: left;">';
				$html .= '<input type="hidden" value="/thumbs/1000xauto/'.str_replace('/', '::', $file->file).'" class="insert-value" />';
				$html .= '<button class="btn btn-xs btn-default insert-file">'.t('Image').'</button>';
				$html .= '</form>';
			}

		}

		// output
		return $html;

	}

	// link
	static public function link($file) {

		if($file->type == 'image') {
			return str_replace('<a', '<a data-lightbox="roadtrip"', Dec::a(Thumb::fly($file->file, '1000xauto'), Thumb::show($file->file, 'autox30'), false));
		}
		return Dec::a(cfg('protocol').cfg('domain').Route::map('evsmash/files/files/show').'/'.$file->id.'/'.Str::slug($file->name), File::icon($file), false)->blank();

	}

	// icon
	static public function icon($file, $size = '25') {

		// thumbs
		if($file->type == 'video') { return Dec::i('file-video-o', false,  $size); }
		if($file->type == 'audio') { return Dec::i('file-audio-o', false,  $size); }
		if($file->type == 'archive') { return Dec::i('file-archive-o', false,  $size); }
		if($file->ext == 'pdf') { return Dec::i('file-pdf-o', false,  $size); }
		return Dec::i('file-o', false,  $size);

	}

}