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

namespace Evsmash\Articles;

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\Articles\Schemas\ArticlesCategories as Schema;


class ArticlesCategory extends Baum {

	use SoftDeleting;

	// validate
	static public function validate() {

		return Schema::validate();

	}

	// belongs to many articles 
	public function articles() {
	
		return $this->belongsToMany('Evsmash\Articles\Article', 'articles_to_categories', 'category_id', 'article_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
	public function getLinkAttribute() {

		return Route::map('evsmash/articles/articles/category').'/'.$this->id.'/'.Str::slug($this->name);

	}

	// parents
	static public function parents() {

		// get
		$root = ArticlesCategory::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 class="link-category-'.$node->id.'">'.Dec::a($node->link, $node->name).'</li>';
		} else {
			$html = '<li>'.Dec::a($node->link, $node->name);
			$html .= '<ul class="list-unstyled">';
			foreach($node->children as $child) {
				$html .= ArticlesCategory::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 .= ArticlesCategory::treesort($child);
			}
			$html .= '</ol>';
			$html .= '</li>';
		}
		return $html;

	}

}