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

namespace Evsmash\Events;

use Evsmash\Core\Database\Eloquent;

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

use Evsmash\Events\Schemas\EventsCategories as Schema;


class EventsCategory extends Baum {

	use SoftDeleting;

	// validate
	static public function validate() {

		return Schema::validate();

	}

	// belongs to many events 
	public function events() {
	
		return $this->belongsToMany('Evsmash\Events\Event', 'events_to_categories', 'category_id', 'event_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);

	}

	// parents
	static public function parents() {

		// get
		$root = EventsCategory::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) {
		
		if($node->isLeaf()) {
			return '<li>'.Dec::a(Route::map('evsmash/events/events/category').'/'.$node->id.'/'.Str::slug($node->name), $node->name).'</li>';
		} else {
			$html = '<li>'.Dec::a(Route::map('evsmash/events/events/category').'/'.$node->id.'/'.Str::slug($node->name), $node->name);
			$html .= '<ul class="list-unstyled">';
			foreach($node->children as $child) {
				$html .= EventsCategory::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 .= EventsCategory::treesort($child);
			}
			$html .= '</ol>';
			$html .= '</li>';
		}
		return $html;

	}

}