Skip to content

Commit

Permalink
Separated mailing and regular node types
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyleeh committed Sep 4, 2016
1 parent 3cfe7da commit 29a6460
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 23 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"laravel",
"laravel5"
],
"version": "2.0.25",
"version": "2.1.0",
"authors": [
{
"name": "Hyleeh",
Expand Down
16 changes: 16 additions & 0 deletions src/MailingNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php


namespace Nuclear\Hierarchy;


class MailingNode extends Node {

/**
* Determines if the model type is mailing
*
* @var bool
*/
protected $isTypeMailing = true;

}
24 changes: 24 additions & 0 deletions src/MailingScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Nuclear\Hierarchy;


use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Scope;

class MailingScope implements Scope {

/**
* Apply the scope to a given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function apply(Builder $builder, Model $model)
{
return $model->scopeTypeMailing($builder);
}

}
36 changes: 29 additions & 7 deletions src/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ class Node extends Eloquent implements TrackableInterface {
*/
protected $nodeTypeName = null;

/**
* Determines if the model type is mailing
*
* @var bool
*/
protected $isTypeMailing = false;

/**
* Status codes
*
Expand All @@ -175,6 +182,8 @@ protected static function boot()
{
parent::boot();

static::addGlobalScope(new MailingScope());

static::creating(function ($node)
{
if (empty($node->published_at))
Expand Down Expand Up @@ -344,9 +353,11 @@ public function setNodeTypeKey($id)
*/
public function setNodeTypeByKey($id)
{
$this->nodeType()->associate(
NodeType::findOrFail($id)
);
$nodeType = NodeType::findOrFail($id);

$this->nodeType()->associate($nodeType);

$this->mailing = $nodeType->isTypeMailing();
}

/**
Expand Down Expand Up @@ -1089,7 +1100,7 @@ protected function remakeSources(NodeType $nodeType)
* @param int|null $limit
* @return Builder
*/
public function scopeMostVisited($query, $limit = null)
public function scopeMostVisited(Builder $query, $limit = null)
{
$query->select(\DB::raw('nodes.*, count(*) as `aggregate`'))
->join('node_site_view', 'nodes.id', '=', 'node_site_view.node_id')
Expand All @@ -1111,7 +1122,7 @@ public function scopeMostVisited($query, $limit = null)
* @param int|null $limit
* @return Builder
*/
public function scopeRecentlyVisited($query, $limit = null)
public function scopeRecentlyVisited(Builder $query, $limit = null)
{
$query
->select(\DB::raw('nodes.*, MAX(node_site_view.site_view_id) as `aggregate`'))
Expand All @@ -1134,7 +1145,7 @@ public function scopeRecentlyVisited($query, $limit = null)
* @param int|null $limit
* @return Builder
*/
public function scopeRecentlyEdited($query, $limit = null)
public function scopeRecentlyEdited(Builder $query, $limit = null)
{
$query->orderBy('updated_at', 'desc');

Expand All @@ -1153,7 +1164,7 @@ public function scopeRecentlyEdited($query, $limit = null)
* @param int|null $limit
* @return Builder
*/
public function scopeRecentlyCreated($query, $limit = null)
public function scopeRecentlyCreated(Builder $query, $limit = null)
{
$query->orderBy('created_at', 'desc');

Expand All @@ -1165,6 +1176,17 @@ public function scopeRecentlyCreated($query, $limit = null)
return $query;
}

/**
* Scopes the model for regular and mailing nodes
*
* @param Builder $query
* @return Builder
*/
public function scopeTypeMailing(Builder $query)
{
return $query->where('mailing', $this->isTypeMailing);
}

/**
* Gets the full url for node
*
Expand Down
2 changes: 1 addition & 1 deletion src/Providers/HierarchyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class HierarchyServiceProvider extends ServiceProvider {

const version = '2.0.25';
const version = '2.1.0';

/**
* Indicates if loading of the provider is deferred.
Expand Down
16 changes: 2 additions & 14 deletions src/PublishedScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ScopeInterface;
use Illuminate\Database\Eloquent\Scope;

class PublishedScope implements ScopeInterface {
class PublishedScope implements Scope {

/**
* Apply the scope to a given Eloquent query builder.
Expand All @@ -21,16 +21,4 @@ public function apply(Builder $builder, Model $model)
return $model->scopePublished($builder);
}

/**
* Remove the scope from the given Eloquent query builder.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return void
*/
public function remove(Builder $builder, Model $model)
{
return $builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function up()

NestedSet::columns($table);

$table->boolean('mailing')->default(0);
$table->boolean('visible')->default(1);
$table->boolean('sterile')->default(0);
$table->boolean('home')->default(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function up()

NestedSet::columns($table);

$table->boolean('mailing')->default(0);
$table->boolean('visible')->default(1);
$table->boolean('sterile')->default(0);
$table->boolean('home')->default(0);
Expand Down

0 comments on commit 29a6460

Please sign in to comment.