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/items / libs / Item.php
Size: Mime:
<?php

namespace Evsmash\Items;

use Evsmash\Core\Database\Eloquent;

use Evsmash\Core\Database\Query;
use Evsmash\Core\Database\SoftDeleting;
use Evsmash\Core\Database\SearchableTrait;
use Evsmash\Core\Helpers\Sanitize;
use Evsmash\Core\Input\Flash;
use Evsmash\Core\Input\Route;
use Evsmash\Core\Services\Currency;
use Evsmash\Core\Simpy\Element;
use Evsmash\Core\Simpy\Params;

use Evsmash\Items\Schemas\Items as Schema;

class Item extends Eloquent {

	use SoftDeleting;
	use SearchableTrait;
	protected $searchable = [
		'columns' => [
			'name' => 10, 
			'content' => 5
		]
	];

	// validate
	static public function validate() {

		return Schema::validate();

	}

	// belongs to type
	public function type() { 
		
		return $this->belongsTo('Evsmash\Items\ItemsType');
	
	}

	// belongs to many categories 
	public function categories() {
	
		return $this->belongsToMany('Evsmash\Items\ItemsCategory', 'items_to_categories', 'item_id', 'category_id');

	}

	// has many parameters
	public function parameters() { 
		
		return $this->hasMany('Evsmash\Items\ItemsParameter', 'item_id');
	
	}

	// params
	public function scopeParams($query, $params = false) {
		
		// order
		$order = ['created_at' => 'desc'];
		if(Route::get('level') == 'site') {
			$order = cfg('evsmash-items-items-order');
		}

		// scope
		$scope = new Params();
		$scope->scope($query, $params);
		$scope->where('new');
		$scope->where('published');
		$scope->where('promoted');
		$scope->where('type', 'type_id');
		$scope->searchable();
		$scope->order($order);
		$scope->with(['type', 'categories']);
		return $scope->query;

	}

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

		// parameters
		$parameters = cfg('evsmash-items-items-parameters');
		if(!isset($parameters['all'])) {
			return $query;
		}
		
		// groups
		$groups = $parameters['all'];
		foreach($groups as $param => $details) {
			if(Route::get('param-'.$param)) {

				// value
				$value = Sanitize::slug(Route::get('param-'.$param));
			
				// ids
				$ids = Query::table('items_parameters')->where('param', $param)->where('value', $value)->pluck('item_id')->toArray();

				// query
				$query->whereIn('id', $ids);

				// noindex, follow
				Flash::set('robots', 'noindex, follow');

			}
		}

		// query
		return $query;

	}

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

		  return $query->where('published', 1);

	}

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

		  return $query->where('promoted', 1);

	}

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

		  return $query->where('new', 1);

	}

	// link final
	public function getLinkFinalAttribute() {

		return Route::map('evsmash/items/items/show').'/'.$this->id.'/'.$this->slug;

	}

	// price formatted
	public function getPriceFormattedAttribute() {

		// currency
		$currency = cfg('evsmash-items-currencies')[0];
		if(!empty($this->currency)) {
			$currency = $this->currency;
		}

		// html
		$html = '';
		$html .= '<span class="price-current">'.Currency::show($this->price, $currency).'</span>';
		if(!is_null($this->price_old)) {
			$html .= '<span class="price-old old-price">'.Currency::show($this->price_old, $currency).'</span>';
		}
		return $html;

	}

	// ribbon
	public function getRibbonAttribute() {

		// new
		if(cfg('evsmash-items-ribbons-new')) {
			if($this->new == 1) {
				return '<span class="ribbon ribbon-new">'.cfg('evsmash-items-ribbons-new').'</span>';
			}
		}

		// no ribbons
		return false;

	}

}