modelClass = $class; parent::__construct(); } protected function newModel() { $model = new $this->modelClass(); $model = $model->isPublished() ->getDocuments($this->modelType); return $model; } protected function getModelClassString($modelClass) { return 'post'; } protected function hook_before_model_save(&$model, Request &$request) { // Wenn es ein neuer Post ist und noch kein Post-Typ angegeben wurde, // wird auch der slug neu erstellt if($model->getAttribute('type') == '') { $model->type = $this->modelType; $model->slug = ''; } } protected function hook_after_model_save(&$model, Request &$request) { // Wenn die Validierung erfolgreich war und der Post gespeichert wurde, // wird überprüft, ob der Post veröffentlicht werden soll // Poste den Beitrag in den Sozialen Netzwerken if(isset($request['publish_website'])) { $publish_website = $request['publish_website'] == "true" ? 1 : 0; $model->published = $publish_website; } $model->save(); if($this->modelClass === Post::class) { $publishDate = $this->calculatePublicationDate($model); foreach($this->publishPlatforms as $platform) { $this->publishOnPlatform($platform, $model, $publishDate, $request); } } $platforms = array(); // Ermittle die Platformen, auf denen der Posts gerade veröffentlicht wurde foreach($this->messages as $message) { if(key_exists('code', $message)) { if($message['code'] == 202) { $platforms[] = $message['platform']; } } } // Wenn der Posts auf einer Platform veröffentlicht wurde, werden die Info-Mails versendet if(count($platforms)) { PublishHelper::publishNotification($model->id, $platforms); } } protected function hook_after_action_validateFormData(&$model) { $publishDate = $this->calculatePublicationDate($model); foreach($this->publishPlatforms as $platform) { $publication = $model->publication($platform); $publication->setPublishDate($publishDate); $publication->save(); } } protected function hook_after_action_show(&$model, View &$view) { /*********************************/ /* Aktuelle Posts mit selbem Typ */ /*********************************/ if(key_exists('newest', $this->detailsOptions)) { // Aktuelle Posts mit selbem Typ und Bild ermitteln $newestImageModels = $this->getNewestPosts($model, true); if(count($newestImageModels)) { $view->with('newestImageModels', $newestImageModels); } $newestLabel = ""; if(key_exists('newest', $this->detailsOptions)) { // Eventuelles Label hinzufügen if(key_exists('label', $this->detailsOptions['newest'])) { $newestLabel = $this->detailsOptions['newest']['label']; } $view->with('newestLabel', $newestLabel); } // Weitere Posts mit selbem Typ aber ohne Bild ermitteln $newestModels = $this->getNewestPosts($model, false); if(count($newestModels)) { // $sidebar[] = $this->createSidebarLinks($newestModels, $newestLabel); } } /*********************************/ /* Ähnliche Posts mit selbem Tag */ /*********************************/ if(key_exists('similar', $this->detailsOptions)) { // Eventuelles Label hinzufügen $similarLabel = ""; if(key_exists('similar', $this->detailsOptions)) { if(key_exists('label', $this->detailsOptions['similar'])) { $similarLabel = $this->detailsOptions['similar']['label']; } $view->with('similarLabel', $similarLabel); } // Ähnliche Posts mit selbem Tag und Bild ermitteln $similarImageModels = $this->getSimilarPosts($model, true); if(count($similarImageModels)) { $view->with('similarImageModels', $similarImageModels); } // Ähnliche Posts mit selbem Tag aber ohne Bild ermitteln $similarModels = $this->getSimilarPosts($model, false); if(count($similarModels)) { // $sidebar[] = $this->createSidebarLinks($similarModels, $similarLabel); } } /*******************************/ /* Ältere Posts mit selbem Typ */ /*******************************/ if(key_exists('further', $this->detailsOptions)) { // Weitere ältere Posts mit selbem Typ und Bild ermitteln $furtherImageModels = $this->getFurtherPosts($model, true); $furtherLabel = ""; if(key_exists('further', $this->detailsOptions)) { // Eventuelles Label hinzufügen if(key_exists('label', $this->detailsOptions['further'])) { $furtherLabel = $this->detailsOptions['further']['label']; } $view->with('furtherLabel', $furtherLabel); } if(count($furtherImageModels)) { $view->with('furtherImageModels', $furtherImageModels); } // Weitere Posts mit selbem Typ aber ohne Bild ermitteln $furtherModels = $this->getFurtherPosts($model, false); if(count($furtherModels)) { // $sidebar[] = $this->createSidebarLinks($furtherModels, $furtherLabel); } } } protected function hook_after_action_update(&$model, Request &$request) { $model = $this->prepareModel($model, $request); // Eingabewerte aufbereiten und ggfs. Array für Multidate-Werte erstellen $dateTimesValues = array(); $mutlidateKey = ""; if($request->has('multidate')) { $mutlidateKey = $request->multidate; $key_date = $mutlidateKey . '_multidates'; $dateTimesValues = explode(',', $model->$key_date); } else { $dateTimesValues[] = false; } // Wenn es sich um eine Massenanlage von Posts handelt, werden hier // die nachfolgenden Posts angelegt for($i = 1; $i < count($dateTimesValues); $i++) { if($i > 0) { $model = new $this->modelClass(); } $model->update($request->all()); $model = $this->modelFill($model, $request); if($dateTimesValues[$i] != false) { $model->$mutlidateKey = $dateTimesValues[$i]; } $this->saveModel($model, $request); } } /*******************/ /* Veröffentlichen */ /*******************/ private function publishOnPlatform($platform, $model, $publishDate, Request $request) { $isMuldidate = false; if($request->has('multidate')) { $isMuldidate = true; } $publish_request_name = 'publish_' . $platform; if(isset($request[$publish_request_name])) { // Benutzereingabe aufbereiten $publish_user_action = $request[$publish_request_name] == "true" ? true : false; // Wenn es sich um eine Massenerstellung "Multidate" handelt, werden nur die Posts veröffentlicht, // die auch in der Zukunft liegen oder mindestens am heutigen Tag sind if($isMuldidate == true && $publish_user_action == true && $model->datetime < Carbon::now()->startOfDay()) { $publish_user_action = false; } $publication = $model->publication($platform); $publication->setPublishDate($publishDate); $publication->noteForPublication($publish_user_action); $publication->publishIfReady(); $publication->save(); if($publication->stateHasChanged()) { switch($publication->getState()) { case '': $this->addMessageInfo( ucfirst($platform), __("messages.publication withdrawn") ); break; case 'PENDING': $this->addMessageInfo( ucfirst($platform), __("messages.publication bookmarked for :date", ["date" => DateHelper::toHumanDate($publishDate)]) ); break; default: $this->addMessageInfo( ucfirst($platform), __("messages.published") ); break; } } } } public function calculatePublicationDate($model, $import = false) { // Vorberechnungen zum Veröffentlichungsdatum $daysToPublishBefore = -999999; $publishDate = Carbon::now()->startOfDay(); if(is_array($this->publishOptions)) { if(key_exists('date', $this->publishOptions)) { $publishDateString = strtolower($this->publishOptions['date']); if($publishDateString == 'now') { if($import) { $daysToPublishBefore = 0; } else { $daysToPublishBefore = Carbon::now()->diffInDays(Carbon::parse($model->datetime)); } } else { $daysToPublishBefore = preg_replace('/[^0-9]/', '', $publishDateString); if(substr($publishDateString, -1) == "w") { // Umrechnung von Wochen in Tage $daysToPublishBefore *= 7; } if(substr($publishDateString, 0, 1) == "-") { $daysToPublishBefore *= -1; } } // Berechne das Datum, an dem der Post veröffentlicht werden soll $publishDate = Carbon::parse($model->datetime)->addDays($daysToPublishBefore)->startOfDay(); } } return $publishDate; } protected function getNewestPosts($model, $withImages) { // Weitere aktuelle Posts mit selbem Typ ermitteln if(key_exists('further', $this->detailsOptions)) { $limit = $this->detailsOptions['further']['limit']; $tagIDs = array(); $furtherImageModels = new $this->modelClass(); $type = $model->type; if(key_exists('type', $this->detailsOptions['further'])) { $type = $this->detailsOptions['further']['type']; $post_id = $model->id; $furtherImageModels = $furtherImageModels->whereHas('posts', function ($query) use ($post_id) { $query->where('parent_id', $post_id) ->orWhere('post_id', $post_id); }); } $furtherImageModels = $furtherImageModels->where('type', $type)->limit($limit); if($withImages) { $furtherImageModels = $furtherImageModels->withImages(); } else { $furtherImageModels = $furtherImageModels->withoutImages(); } $furtherImageModels = $furtherImageModels->isPublished() ->orderBy('datetime', 'DESC') ->get(); return $furtherImageModels; } return array(); } protected function getSimilarPosts($model, $withImages) { // Ähnliche Posts mit selbem Tag ermitteln if(key_exists('similar', $this->detailsOptions)) { $typeName = $this->detailsOptions['similar']['tag']; $tagIDs = array(); if(key_exists($typeName, $this->modelData)) { $conditions = $this->modelData[$typeName]['foreign']['conditions']; foreach($model->tags as $key => $values) { $match = true; foreach($conditions as $condition) { $attribute = $condition['column']; $val = $condition['value']; if($values->$attribute != $val) { $match = false; break; } } if($match) { $tagIDs[] = $values->id; } } if($match) { $similarImageModels = new $this->modelClass(); if(key_exists('limit', $this->detailsOptions['similar'])) { $limit = $this->detailsOptions['similar']['limit']; $similarImageModels = $similarImageModels->limit($limit); } if($withImages) { $similarImageModels = $similarImageModels->withImages($this->modelType); } else { $similarImageModels = $similarImageModels->withoutImages($this->modelType); } $similarImageModels = $similarImageModels->where('type', $model->type) ->whereHas('tags', function ($query) use ($tagIDs) { $query->where('tag_id', $tagIDs); }) ->inRandomOrder() ->get(); } } if(!isset($similarImageModels)) { $similarImageModels = array(); } return $similarImageModels; } return array(); } protected function getFurtherPosts($model, $withImages) { // Weitere Posts mit selbem Typ ermitteln if(key_exists('further', $this->detailsOptions)) { $limit = $this->detailsOptions['further']['limit']; $tagIDs = array(); $furtherImageModels = new $this->modelClass(); $type = $model->type; if(key_exists('type', $this->detailsOptions['further'])) { $type = $this->detailsOptions['further']['type']; $post_id = $model->id; $furtherImageModels = $furtherImageModels->whereHas('posts', function ($query) use ($post_id) { $query->where('parent_id', $post_id) ->orWhere('post_id', $post_id); }); } $furtherImageModels = $furtherImageModels->where('type', $type)->limit($limit); if($withImages) { $furtherImageModels = $furtherImageModels->withImages($this->modelType); } else { $furtherImageModels = $furtherImageModels->withoutImages($this->modelType); } $furtherImageModels = $furtherImageModels->isPublished() ->inRandomOrder() ->get(); return $furtherImageModels; } return array(); } }