File: D:/HostingSpaces/SBogers10/zelfverkopen.komma.pro/app/KommaApp/Posts/Kms/PostService.php
<?php
namespace App\KommaApp\Posts\Kms;
use App\KommaApp\Kms\Core\Attributes\Attribute;
use App\KommaApp\Kms\Core\NestedSets\Nodes\EloquentNode;
use App\KommaApp\Kms\Core\Sections\SectionService;
use App\KommaApp\Kms\Core\Sections\SectionTabItem;
use App\KommaApp\Languages\Models\Language;
use App\KommaApp\Pages\Models\PageTranslation;
use App\KommaApp\Posts\Models\Post;
use App\KommaApp\Routes\Models\RedirectRoute;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
class PostService extends SectionService
{
protected $sortable = false;
private static $XML_POSTS = 'old_posts'; //without extension
private $xmlPostCounter = 1500000000000; //without extension
/**
* @var string $orderBy
*/
protected $orderBy = 'date';
/**
* @var bool $orderReverse
*/
protected $orderReverse = true;
function __construct()
{
$this->forModelName = Post::class;
parent::__construct();
}
/**
* This method will save an model
*
* @param $model Model or null
* @param Collection $sectionTabItems These must be filled with data. This is something you need to do yourself.
*
* @return mixed
*/
public function saveModel(Model $model = null, Collection $sectionTabItems): Model
{
/** @var EloquentNode $model */
//Process Page Specific attributes
// dd($model);
$sectionTabItems->each(function ($sectionTabItem, $key) use ($model) {
/** @var SectionTabItem $sectionTabItem */
$attribute = $sectionTabItem->getAttribute();
$reference = $attribute->getsValueFromReference();
switch ($attribute->getsValueFrom()) {
case Attribute::ValueFromModel:
if ($reference == 'parent_id') {
// Get parent_id of the input
$parentId = $attribute->getValue();
$parentPage = $model->find($parentId);
// Set default to false
$currentParentId = null;
// Check if model id isset so if it is a new model or not
// if true then get current parent_id
if ($model->id) {
$currentParentId = $model->getParentId();
}
//Check if parent page is found, model isset and parent page id isn't the current parent id
if ($parentPage && $model && $parentPage->id !== $currentParentId) {
$model->makeLastChildOf($parentPage);
}
$attribute->mapValueFrom(Attribute::ValueFromItself,
''); //unset parent_id it so it won't get saved directly on the model in the parent_id field. The makeLastChildOf call above does save the parent id via lft rgt
}elseif ($reference == 'code_name') {
}
break;
case Attribute::ValueFromTranslationModel;
/** @var Language $language */
$language = $attribute->getAssociatedLanguage();
if ( ! $model->exists) {
$model->save();
}
$translation = $this->getOrCreateTranslationModelForModel($model, $language);
if ($reference == 'name') {
$slug = $this->createOrGetUniqueSlug($translation, $attribute->getValue());
$translation['slug'] = $slug;
$translation->save();
}
break;
}
});
$model->save(); //Save the page
$model = parent::saveModel($model,
$sectionTabItems); //First make sure we have a model and save the attributes in them from the SectionTabItem attributes
//Return the page
return $model;
}
/**
* Import post from xml
*/
public function importPostsFromXML()
{
$xmlFile = file_get_contents(self::$XML_POSTS . '.xml');
$xml = simplexml_load_string($xmlFile, "SimpleXMLElement", LIBXML_NOCDATA);
foreach ($xml->channel->item as $item) {
$postObject = (object)[];
$postObject->name = $item->title;
$postObject->date = $item->xpath('wp:post_date')[0];
$postObject->slug = $item->xpath('wp:post_name')[0];
$postObject->link = $item->guid;
$postObject->link = str_replace('http://www.zelfverkopen.nl/','', $postObject->link);
foreach ($item->xpath('wp:postmeta') as $postMeta) {
if ($postMeta->xpath('wp:meta_key')[0] == 'auteur') {
$postObject->author = $postMeta->xpath('wp:meta_value')[0];
}
if ($postMeta->xpath('wp:meta_key')[0] == 'link') {
$postObject->linkTo = $postMeta->xpath('wp:meta_value')[0];
}
}
// Get content
$content = $item->xpath('content:encoded')[0];
// Remove wordpress tag
$content = str_replace('<!--more-->', '', $content);
$content = nl2br($content);
$postObject->content = $content;
// If post content is empty, it's a link so we need to generate this also
if ( ! isset($content) || $content === '') {
$postObject->content = "<p>Bekijk hier het artikel: <a href='" . $postObject->linkTo . "'>" . $postObject->linkTo . "</a></p>";
};
$postObject->content = $this->createContentBlock($postObject->content);
switch ($postObject->author) {
case 'Michiel Lensink':
$postObject->author_id = 1;
break;
case 'Pieter Valk':
$postObject->author_id = 3;
break;
case 'Benno Kolt':
$postObject->author_id = 4;
break;
case 'Juul Dijkhuis':
$postObject->author_id = 5;
break;
case 'Emma van Opstal':
$postObject->author_id = 6;
break;
default:
$postObject->author_id = 2;
break;
}
$post = Post::create([
'active' => '1',
'author_id' => $postObject->author_id,
'date' => $postObject->date
]);
$post->translations()->create([
'name' => $postObject->name,
'slug' => $postObject->slug,
'description' => $postObject->content,
'language_id' => '104'
]);
$post->sites()->sync([1]);
//Create Redirect routes
RedirectRoute::create([
'route' => 'posts/'.$post->id,
'site_id' => 1,
'language_id' => 104,
'alias' => $postObject->slug,
'routeable_type' => 'App\\KommaApp\\Posts\\Models\\PostTranslation',
'routeable_id' => $post->translations->first()->id,
'redirect_code' => RedirectRoute::Http10PermanentCanBeCached
]);
}
}
/**
* Create dynamic content block
*
* @param $content
* @return string
*/
private function createContentBlock($content)
{
$this->xmlPostCounter++;
return '['.json_encode([
'typeSlug' => 'full-text-block',
"code_name" => "",
"view" => "",
"typeName" => "Een tekstkolom",
"description" => $content,
"status" => true,
"link" => "",
"link_text" => "Lees meer",
"blockId" => '.$this->xmlPostCounter.'
]).']';
}
}