Skip to content

Commit

Permalink
Merge pull request #344 from spira/chore/downstream-merge
Browse files Browse the repository at this point in the history
Updated app with downstream updates
  • Loading branch information
Jeremy Sik committed Jan 5, 2016
2 parents 1888c2f + 301b3fc commit 52e9726
Show file tree
Hide file tree
Showing 170 changed files with 3,061 additions and 1,452 deletions.
43 changes: 43 additions & 0 deletions api/app/Console/Commands/ForumCleanupCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/*
* This file is part of the Spira framework.
*
* @link https://github.com/spira/spira
*
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
*/

namespace App\Console\Commands;

use Illuminate\Console\Command;

class ForumCleanupCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'forum:cleanup';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Init of Vanilla Forum & database refresh';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$dir = realpath(base_path().'/../forum');
exec('composer run-script post-install-cmd --working-dir "'.$dir.'"', $output, $exitCode);

return $exitCode;
}
}
1 change: 1 addition & 0 deletions api/app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class Kernel extends ConsoleKernel
'App\Console\Commands\ApiaryValidateCommand',
'App\Console\Commands\GenerateKeysCommand',
'App\Console\Commands\CreateUserCommand',
'App\Console\Commands\ForumCleanupCommand',
];

/**
Expand Down
3 changes: 3 additions & 0 deletions api/app/Http/Controllers/ArticleCommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

class ArticleCommentController extends PostCommentController
{
protected $permissionsEnabled = true;
protected $defaultRole = false;

/**
* Set dependencies.
*
Expand Down
5 changes: 4 additions & 1 deletion api/app/Http/Controllers/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@
class ArticleController extends EntityController
{
use LocalizableTrait;

use TagCategoryTrait;

protected $permissionsEnabled = true;
protected $defaultRole = false;

protected $rootCategoryTagName = \SeedTags::articleGroupTagName;

/**
Expand Down
3 changes: 3 additions & 0 deletions api/app/Http/Controllers/ArticleMetaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class ArticleMetaController extends ChildEntityController
{
protected $relationName = 'metas';

protected $permissionsEnabled = true;
protected $defaultRole = false;

public function __construct(Article $parentModel, EloquentModelTransformer $transformer)
{
parent::__construct($parentModel, $transformer);
Expand Down
3 changes: 3 additions & 0 deletions api/app/Http/Controllers/ArticleSectionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@

class ArticleSectionController extends AbstractSectionController
{
protected $permissionsEnabled = true;
protected $defaultRole = false;

use LocalizableTrait;

protected $relationName = 'sections';
Expand Down
3 changes: 3 additions & 0 deletions api/app/Http/Controllers/ArticleTagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

class ArticleTagController extends AbstractTagController
{
protected $permissionsEnabled = true;
protected $defaultRole = false;

public function __construct(Article $parentModel, EloquentModelTransformer $transformer)
{
parent::__construct($parentModel, $transformer);
Expand Down
2 changes: 1 addition & 1 deletion api/app/Http/Controllers/Traits/TagCategoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function getAllTagCategories(Request $request)
{
$collection = $this->getTagsFromRoot($this->getRootTagName());
$collection = $this->getWithNested($collection, $request);
$this->checkPermission(static::class.'@getAllTags', ['model' => $collection]);
$this->checkPermission(static::class.'@getAllTagCategories', ['model' => $collection]);

return $this->getResponse()
->transformer($this->getTransformer())
Expand Down
37 changes: 21 additions & 16 deletions api/app/Models/AbstractPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

namespace App\Models;

use App\Models\Traits\CommentableInterface;
use App\Models\Traits\CommentableTrait;
use Illuminate\Database\Eloquent\Collection;
use Rhumsaa\Uuid\Uuid;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -51,9 +53,9 @@
* @property Rating[]|Collection $userRatings
* @property Bookmark[]|Collection $bookmarks
*/
class AbstractPost extends IndexedModel implements LocalizableModelInterface
class AbstractPost extends IndexedModel implements LocalizableModelInterface, CommentableInterface
{
use RevisionableTrait, LocalizableModelTrait, TagTrait, RateableTrait, BookmarkableTrait;
use RevisionableTrait, LocalizableModelTrait, TagTrait, RateableTrait, BookmarkableTrait, CommentableTrait;

const defaultExcerptWordCount = 30;

Expand Down Expand Up @@ -100,6 +102,7 @@ class AbstractPost extends IndexedModel implements LocalizableModelInterface
'status',
'users_can_comment',
'public_access',
'short_title',
];

protected $hidden = ['permalinks'];
Expand All @@ -113,6 +116,8 @@ class AbstractPost extends IndexedModel implements LocalizableModelInterface
'sections_display' => 'json',
];

protected static $permalinkModel = PostPermalink::class;

protected $indexRelations = ['tags', 'permalinks', 'author', 'metas'];

public static function getValidationRules($entityId = null, array $requestEntity = [])
Expand Down Expand Up @@ -190,7 +195,7 @@ public static function boot()
static::bootScope();
static::saving(function (AbstractPost $model) {
if ($model->getOriginal('permalink') !== $model->permalink && ! is_null($model->permalink)) {
$permalink = new PostPermalink(['permalink' => $model->permalink]);
$permalink = new static::$permalinkModel(['permalink' => $model->permalink]);
$permalink->save();
}

Expand All @@ -199,7 +204,7 @@ public static function boot()

static::saved(function (AbstractPost $model) {
if ($model->getOriginal('permalink') !== $model->permalink && ! is_null($model->permalink)) {
$permalink = PostPermalink::findOrFail($model->permalink);
$permalink = call_user_func_array(static::$permalinkModel.'::findOrFail', [$model->permalink]);
$model->permalinks()->save($permalink);
}

Expand Down Expand Up @@ -247,7 +252,7 @@ public function findByIdentifier($id)
} catch (ModelNotFoundException $e) { //id or permalink not found, try permalink history
$name = class_basename($this);

return PostPermalink::findOrFail($id)->{$name};
return call_user_func_array(static::$permalinkModel.'::findOrFail', [$id])->{$name};
}
}

Expand Down Expand Up @@ -276,24 +281,14 @@ public function getExcerptAttribute($excerpt)

public function permalinks()
{
return $this->hasMany(PostPermalink::class, 'post_id', 'post_id');
return $this->hasMany(static::$permalinkModel, 'post_id', 'post_id');
}

public function metas()
{
return $this->morphMany(Meta::class, 'metaable');
}

/**
* Get comment relationship.
*
* @return PostDiscussion
*/
public function comments()
{
return (new PostDiscussion())->setPost($this);
}

public function thumbnailImage()
{
return $this->hasOne(Image::class, 'image_id', 'thumbnail_image_id');
Expand All @@ -316,4 +311,14 @@ public function setPostTypeAttribute()
{
throw new \InvalidArgumentException('No direct attribute assignment');
}

public function getTitle()
{
return $this->title;
}

public function getExcerpt()
{
return $this->excerpt;
}
}
18 changes: 9 additions & 9 deletions api/app/Models/PostDiscussion.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public static function getValidationRules($entityId = null, array $requestEntity
public function createDiscussion()
{
$this->getClient()->api('discussions')->create(
$this->post->title,
$this->post->excerpt,
$this->post->getTitle(),
$this->post->getExcerpt(),
1,
['ForeignID' => $this->post->post_id]
['ForeignID' => $this->post->getKey()]
);
}

Expand All @@ -73,7 +73,7 @@ public function createDiscussion()
public function deleteDiscussion()
{
$this->getClient()->api('discussions')->removeByForeignId(
$this->post->post_id
$this->post->getKey()
);
}

Expand All @@ -85,7 +85,7 @@ public function deleteDiscussion()
protected function getDiscussionId()
{
$discussion = $this->getClient()->api('discussions')->findByForeignId(
$this->post->post_id
$this->post->getKey()
);

return $discussion['Discussion']['DiscussionID'];
Expand Down Expand Up @@ -151,11 +151,11 @@ public function getRelated()
public function getResults()
{
// First a minimal call to the discussion for the total comment count
$discussion = $this->getDiscussion($this->post->post_id, 1);
$discussion = $this->getDiscussion($this->post->getKey(), 1);
$count = $discussion['Discussion']['CountComments'];

// Now get the entire batch of comments
$discussion = $this->getDiscussion($this->post->post_id, $count);
$discussion = $this->getDiscussion($this->post->getKey(), $count);

// And turn them into a collection of models
$comments = $this->prepareCommentsForHydrate($discussion['Comments']);
Expand Down Expand Up @@ -321,11 +321,11 @@ public function match(array $models, Collection $results, $relation)
/**
* Sets the post the discussion belongs to.
*
* @param AbstractPost $post
* @param $post
*
* @return $this
*/
public function setPost(AbstractPost $post)
public function setPost($post)
{
$this->post = $post;

Expand Down
2 changes: 0 additions & 2 deletions api/app/Models/PostPermalink.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ class PostPermalink extends BaseModel

protected static $validationRules = [
'permalink' => 'string|required',
'check_entity_id' => 'uuid',
'value' => 'required|string',
];

public function article()
Expand Down
9 changes: 5 additions & 4 deletions api/app/Models/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class Tag extends IndexedModel

public function articles()
{
return $this->belongsToMany(Article::class, 'post_tag', 'tag_id', 'post_id', 'articles')->withPivot('tag_group_id', 'tag_group_parent_id');
return $this->getBelongsRelation(Article::class, 'post_tag', null, 'post_id');
}

/**
Expand All @@ -79,7 +79,8 @@ public function findByIdentifier($id)
public static function getGroupedTagPivots(Collection $tags, $parentTagName, array $tagDefinition = null)
{
if (! $tagDefinition) {
$tagDefinition = SeedTags::$tagHierarchy[$parentTagName];
$seededTags = SeedTags::getSeedTags();
$tagDefinition = $seededTags[$parentTagName];
}

array_walk($tagDefinition['children'], function (&$value, $key) {
Expand Down Expand Up @@ -130,8 +131,8 @@ public function parentTags()
]);
}

protected function getBelongsRelation($related, $relation)
protected function getBelongsRelation($related, $table = null, $foreignKey = null, $otherKey = null)
{
return $this->belongsToMany($related, null, null, null, $relation)->withPivot('tag_group_id', 'tag_group_parent_id');
return $this->belongsToMany($related, $table, $foreignKey, $otherKey)->withPivot('tag_group_id', 'tag_group_parent_id');
}
}
2 changes: 1 addition & 1 deletion api/app/Models/Traits/BookmarkableTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace App\Models\Traits;

use App\Models\Bookmark;
use Spira\Model\Model\BaseModel;
use Spira\Core\Model\Model\BaseModel;

trait BookmarkableTrait
{
Expand Down
39 changes: 39 additions & 0 deletions api/app/Models/Traits/CommentableInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of the Spira framework.
*
* @link https://github.com/spira/spira
*
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
*/

namespace App\Models\Traits;

/**
* Interface CommentableInterface
* A model should implement this interface if it uses CommentableTrait.
*/
interface CommentableInterface
{
/**
* Get the value of the model's primary key.
*
* @return mixed
*/
public function getKey();

/**
* Get the title which will be set as the discussion title.
*
* @return string
*/
public function getTitle();

/**
* Get the excerpt which will be set as the discussion excerpt.
*
* @return string
*/
public function getExcerpt();
}
26 changes: 26 additions & 0 deletions api/app/Models/Traits/CommentableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the Spira framework.
*
* @link https://github.com/spira/spira
*
* For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
*/

namespace App\Models\Traits;

use App\Models\PostDiscussion;

trait CommentableTrait
{
/**
* Get comment relationship.
*
* @return PostDiscussion
*/
public function comments()
{
return (new PostDiscussion())->setPost($this);
}
}
Loading

0 comments on commit 52e9726

Please sign in to comment.