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 / ItemsCategory.php
Size: Mime:
<?php

namespace Evsmash\Items;

use Evsmash\Core\Database\Baum;
use Evsmash\Core\Database\SoftDeleting;

use Evsmash\Core\Dec\Dec;
use Evsmash\Core\Helpers\Str;
use Evsmash\Core\Input\Route;
use Evsmash\Core\Simpy\Params;
use Evsmash\Core\System\Lang;

use Evsmash\Items\Schemas\ItemsCategories as Schema;

class ItemsCategory extends Baum {

	use SoftDeleting;

	// validate
	static public function validate() {

		return Schema::validate();

	}

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

	}

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

		$scope = new Params();
		$scope->scope($query, $params);
		$scope->where('published');
		$scope->search(['name']);
		$scope->order(['lft' => 'asc']);
		return $scope->query;

	}

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

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

	}

	// link final
	public function getLinkFinalAttribute() {

		if(cfg('evsmash-items-slugs') AND cfg('locale') == Lang::chosen()) {
			if(isset(cfg('evsmash-items-slugs')['items-categories'])) {	
				return '/'.Str::slug($this->name);
			}
		}
		return Route::map('evsmash/items/items/category').'/'.$this->id.'/'.Str::slug($this->name);

	}

	// parents
	static public function parents() {

		// get
		$root = ItemsCategory::where('depth', 0)->first();
		$elements = $root->getDescendantsAndSelf();

		// output
		$output = [];
		foreach($elements as $row) {

			// prefix
			$prefix = '';
			for($i = 0; $i < $row->depth; $i++) {
				$prefix .= '--';
			}

			// output
			$output[$row->id] = $prefix.' '.$row->name;

		}
		return $output;

	}

	// tree
	static public function tree($node) {

		// name and slug
		$name = $node->name;
		if(!empty($node->name_nav)) {
			$name = $node->name_nav;
		}
		
		// url
		$url = $node->link_final;
		
		// loop
		if($node->isLeaf()) {
			return '<li>'.Dec::a($url, $name).'</li>';
		} else {
			$html = '<li>'.Dec::a($url, $name);
			$html .= '<ul class="list-unstyled">';
			foreach($node->children as $child) {
				$html .= ItemsCategory::tree($child);
			}
			$html .= '</ul>';
			$html .= '</li>';
		}
		return $html;

	}

	// tree
	static public function treesort($node) {
		
		if($node->isLeaf()) {
			return '<li id="list_'.$node->id.'"><div>'.$node->name.'</div></li>';
		} else {
			$html = '<li id="list_'.$node->id.'"><div>'.$node->name.'</div>';
			$html .= '<ol class="list-unstyled">';
			foreach($node->children as $child) {
				$html .= ItemsCategory::treesort($child);
			}
			$html .= '</ol>';
			$html .= '</li>';
		}
		return $html;

	}

}