/** * Get meta from eloquent . * * @param MetaSeoable $metaable * @param null $locale * @param array $placeholders * @return \Illuminate\Foundation\Application|mixed */ public static function fromEloquent(MetaSeoable $metaable, $locale = null, $placeholders = []) { $meta = app('meta'); if (empty($placeholders)) { $placeholders = $meta->configurations['placeholders']; } $locale = isset($locale) ? $locale : Locale\get_active_locale(); $metaTranslatedRow = []; if ($metaRow = $metaable->metaSeo()) { if ($translation = $metaRow->translate($locale)) { $metaTranslatedRow = $translation->toArray(); } } foreach ($meta->templates as $key => $template) { $value = isset($metaTranslatedRow[$key]) ? $metaTranslatedRow[$key] : ''; $placeholder = $value; if (!$value || $value == '') { if ($metaable->allowGlobalPlaceholders()) { $placeholder = $placeholders[$key]; $value = $placeholder; } } if (preg_match_all("/%%([a-zA-Z-_]+)%%/", $placeholder, $matches)) { foreach ($matches[1] as $place) { if (!($replaced = $metaable->{$place})) { if ($metaable instanceof Translatable) { $replaced = isset($metaable->translate($locale)->{$place}) ? $metaable->translate($locale)->{$place} : ''; } } $value = str_replace('%%' . $place . '%%', $replaced, $value); } } $meta->set($key, trim($value)); } return $meta; }
/** * Get the default locale being used. * * @param null $default * @return string */ public function getLocale($default = null) { return !is_null(Localization\get_active_locale()) ? Localization\get_active_locale() : $default; }
/** * Get new translation instance . * * @param null $locale * @param array $attributes * @return mixed */ protected function newTranslation($locale = null, array $attributes) { $locale = isset($locale) ? $locale : Locale\get_active_locale(); $language = $this->getByLocale($locale); $class = $this->classTranslation(); $update = ['language_id' => $language->id, isset($this->translation_id) ? $this->translation_id : str_singular($this->getModel()->getTable()) . '_id' => $this->id]; $attributes = array_merge($update, $attributes); return $class::updateOrCreate($update, $attributes); }