Created
September 4, 2015 20:38
-
-
Save raank/fb1844cc3b1a84f57af6 to your computer and use it in GitHub Desktop.
Template Page Single Product
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace Fonix\Products\Models; | |
use App; | |
use Str; | |
use Html; | |
use Lang; | |
use Model; | |
use Markdown; | |
use ValidationException; | |
use Fonix\Products\Classes\TagProcessor; | |
use Backend\Models\User; | |
class Post extends Model | |
{ | |
use \October\Rain\Database\Traits\Validation; | |
public $table = 'fonix_products_posts'; | |
/* | |
* Validation | |
*/ | |
public $rules = [ | |
'title' => 'required', | |
'slug' => ['required', 'regex:/^[a-z0-9\/\:_\-\*\[\]\+\?\|]*$/i'], | |
'content' => 'required', | |
'excerpt' => '' | |
]; | |
/** | |
* The attributes that should be mutated to dates. | |
* @var array | |
*/ | |
protected $dates = ['published_at']; | |
/** | |
* @var array Guarded fields | |
*/ | |
protected $guarded = ['*']; | |
/** | |
* @var array Fillable fields | |
*/ | |
protected $fillable = []; | |
/** | |
* The attributes on which the post list can be ordered | |
* @var array | |
*/ | |
public static $allowedSortingOptions = array( | |
'title asc' => 'Title (ascending)', | |
'title desc' => 'Title (descending)', | |
'created_at asc' => 'Created (ascending)', | |
'created_at desc' => 'Created (descending)', | |
'updated_at asc' => 'Updated (ascending)', | |
'updated_at desc' => 'Updated (descending)', | |
'published_at asc' => 'Published (ascending)', | |
'published_at desc' => 'Published (descending)', | |
); | |
/* | |
* Relations | |
*/ | |
public $belongsTo = [ | |
'user' => ['Backend\Models\User'] | |
]; | |
/* | |
* $belongsToMany - Seleciona várias categorias | |
* $hasOne - Somente uma categoria | |
*/ | |
public $belongsToMany = [ | |
'categories' => ['Fonix\Products\Models\Category', 'table' => 'fonix_products_posts_categories', 'order' => 'name'] | |
]; | |
public $attachMany = [ | |
'featured_images' => ['System\Models\File', 'order' => 'sort_order'], | |
'thumbnail' => ['System\Models\File'], | |
'share' => ['System\Models\File'], | |
'banner' => ['System\Models\File'], | |
'content_images' => ['System\Models\File'] | |
]; | |
/** | |
* @var array The accessors to append to the model's array form. | |
*/ | |
protected $appends = ['summary', 'has_summary']; | |
public $preview = null; | |
/** | |
* Lists posts for the front end | |
* @param array $options Display options | |
* @return self | |
*/ | |
public function scopeListFrontEnd($query, $options) | |
{ | |
/* | |
* Default options | |
*/ | |
extract(array_merge([ | |
'page' => 1, | |
'perPage' => 30, | |
'sort' => 'created_at', | |
'categories' => null, | |
'search' => '', | |
'published' => true | |
], $options)); | |
$searchableFields = ['title', 'slug', 'excerpt', 'content']; | |
if ($published) | |
$query->isPublished(); | |
/* | |
* Sorting | |
*/ | |
if (!is_array($sort)) $sort = [$sort]; | |
foreach ($sort as $_sort) { | |
if (in_array($_sort, array_keys(self::$allowedSortingOptions))) { | |
$parts = explode(' ', $_sort); | |
if (count($parts) < 2) array_push($parts, 'desc'); | |
list($sortField, $sortDirection) = $parts; | |
$query->orderBy($sortField, $sortDirection); | |
} | |
} | |
/* | |
* Search | |
*/ | |
$search = trim($search); | |
if (strlen($search)) { | |
$query->searchWhere($search, $searchableFields); | |
} | |
/* | |
* Categories | |
*/ | |
if ($categories !== null) { | |
if (!is_array($categories)) $categories = [$categories]; | |
$query->whereHas('categories', function($q) use ($categories) { | |
$q->whereIn('id', $categories); | |
}); | |
} | |
return $query->paginate($perPage, $page); | |
} | |
/** | |
* Allows filtering for specifc categories | |
* @param Illuminate\Query\Builder $query QueryBuilder | |
* @param array $categories List of category ids | |
* @return Illuminate\Query\Builder QueryBuilder | |
*/ | |
public function scopeFilterCategories($query, $categories) | |
{ | |
return $query->whereHas('categories', function($q) use ($categories) { | |
$q->whereIn('id', $categories); | |
}); | |
} | |
public function afterValidate() | |
{ | |
if ($this->published && !$this->published_at) { | |
throw new ValidationException([ | |
'published_at' => Lang::get('fonix.products::lang.post.published_validation') | |
]); | |
} | |
} | |
public function getPublishedOptions($keyValue = null) | |
{ | |
return [1 => 'Publicado', 0 => 'Rascunho']; | |
} | |
public function beforeSave() | |
{ | |
#dd($this->published); | |
$this->content_html = self::formatHtml($this->content); | |
} | |
/** | |
* Sets the "url" attribute with a URL to this object | |
* @param string $pageName | |
* @param Cms\Classes\Controller $controller | |
*/ | |
public function setUrl($pageName, $controller) | |
{ | |
$params = [ | |
'id' => $this->id, | |
'slug' => $this->slug, | |
]; | |
if (array_key_exists('categories', $this->getRelations())) { | |
$params['category'] = $this->categories->count() ? $this->categories->first()->slug : null; | |
} | |
return $this->url = $controller->pageUrl($pageName, $params); | |
} | |
/** | |
* Used to test if a certain user has permission to edit post, | |
* returns TRUE if the user is the owner or has other posts access. | |
* @param User $user | |
* @return bool | |
*/ | |
public function canEdit(User $user) | |
{ | |
return ($this->user_id == $user->id) || $user->hasAnyAccess(['fonix.products.access_other_posts']); | |
} | |
public static function formatHtml($input, $preview = false) | |
{ | |
$result = Markdown::parse(trim($input)); | |
if ($preview) | |
$result = str_replace('<pre>', '<pre class="prettyprint">', $result); | |
$result = TagProcessor::instance()->processTags($result, $preview); | |
return $result; | |
} | |
// | |
// Scopes | |
// | |
public function scopeIsPublished($query) | |
{ | |
return $query | |
->whereNotNull('published') | |
->where('published', true) | |
; | |
} | |
// | |
// Summary / Excerpt | |
// | |
/** | |
* Used by "has_summary", returns true if this post uses a summary (more tag) | |
* @return boolean | |
*/ | |
public function getHasSummaryAttribute() | |
{ | |
return strlen($this->getSummaryAttribute()) < strlen($this->content_html); | |
} | |
/** | |
* Used by "summary", if no excerpt is provided, generate one from the content. | |
* Returns the HTML content before the <!-- more --> tag or a limited 600 | |
* character version. | |
* | |
* @return string | |
*/ | |
public function getSummaryAttribute() | |
{ | |
$excerpt = array_get($this->attributes, 'excerpt'); | |
if (strlen(trim($excerpt))) { | |
return $excerpt; | |
} | |
$more = '<!-- more -->'; | |
if (strpos($this->content_html, $more) !== false) { | |
$parts = explode($more, $this->content_html); | |
return array_get($parts, 0); | |
} | |
return Str::limit(Html::strip($this->content_html), 600); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
title = "Produtos" | |
url = "/produtos/:slug" | |
layout = "products" | |
is_hidden = "0" | |
[productsPosts] | |
[productsPost] | |
slug = "{{ :slug }}" | |
== | |
<?php | |
use Fonix\Products\Classes\PostColors; | |
function onStart() | |
{ | |
$this->page->id = "produtos"; | |
$this->page->classBody = "products product-single product-" . $this->current["id"]; | |
} | |
function onEnd() | |
{ | |
$this->page->type = "single"; | |
$this->page->banner = $this->current->banner[0]; | |
$this->page->title = PostColors::getCategoryTitle( $this->current["id"] ); | |
$this->page->description = $this->current["title"] . ' - ' . $this->current["cap"] . "ml"; | |
$this->page->color = PostColors::getCategoryColor( $this->current["id"] ); | |
} | |
?> | |
== | |
<div class="page-products"> | |
<article id="product-{{ current.id }}" class="product product-{{ current.id }}"> | |
<div class="col-left"> | |
{% if current.featured_images[0] != '' %} | |
<div class="product-images"> | |
<div class="product-slide"> | |
{% for thumb in current.featured_images %} | |
<a href="{{ thumb.path }}" data-lightbox="product-{{ current.id }}"> | |
<img src="{{ thumb.getThumb(526, 370, 'crop') }}"> | |
</a> | |
{% endfor %} | |
</div> | |
<div class="navarrows"> | |
<a href="javascript:;" style="color: #4CAF50;" class="btn waves-effect waves-darken white green-text prev"><i class="mdi mdi-arrow-left"></i></a> | |
<a href="javascript:;" style="color: #4CAF50;" class="btn waves-effect waves-darken white green-text next"><i class="mdi mdi-arrow-right"></i></a> | |
</div> | |
</div> | |
{% endif %} | |
<div class="product-relateds"> | |
<h6>Produtos Relacionados</h6> | |
<ul class="row"> | |
<li> | |
<a href="#"> | |
<img src="{{ asset('themes/plastjet/build/images/produto-demo-1.jpg') }}" alt=""> | |
<div style="height: 100px;" class="item-title valign-wrapper {{ this.page.color }}"> | |
<span class="valign">Tigela Multiuso Single 1800ml</span> | |
</div> | |
</a> | |
</li> | |
<li> | |
<a href="#"> | |
<img src="{{ asset('themes/plastjet/build/images/produto-demo-1.jpg') }}" alt=""> | |
<div style="height: 100px;" class="item-title valign-wrapper {{ this.page.color }}"> | |
<span class="valign">Tigela Multiuso Single Duplex 1800ml</span> | |
</div> | |
</a> | |
</li> | |
<li> | |
<a href="#"> | |
<img src="{{ asset('themes/plastjet/build/images/produto-demo-1.jpg') }}" alt=""> | |
<div style="height: 100px;" class="item-title valign-wrapper {{ this.page.color }}"> | |
<span class="valign">Tigela Multiuso Single Triplex Removivel, fundo falso 1800ml</span> | |
</div> | |
</a> | |
</li> | |
</ul> | |
</div> | |
</div> | |
<div class="col-right"> | |
<h5>{{ current.title }}</h5> | |
<ul class="infos"> | |
{% if current.ref %}<li class="item info-1">Ref.: {{ current.ref }}</li>{% endif %} | |
{% if current.colors %}<li class="item info-2">Cores: {{ current.colors }}</li>{% endif %} | |
{% if current.size %}<li class="item info-3">Dimensões: {{ current.size }}</li>{% endif %} | |
{% if current.qntd %}<li class="item info-4">Quantidade por Caixa: {{ current.qntd }} uni.</li>{% endif %} | |
</ul> | |
<p>{{- current.content|raw -}}</p> | |
</div> | |
</article> | |
</div> | |
{% put scripts %} | |
<script type="text/javascript" src="{{['build/js/libs/slick.min.js']|theme }}"></script> | |
<script type="text/javascript" src="{{['build/js/pages/products.min.js']|theme }}"></script> | |
{% endput %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment