<?php

  if (!function_exists('load_polymorphic_dependencies')) {
    function load_polymorphic_dependencies(Illuminate\Database\Eloquent\Collection $collection, $polymorphic_name, $model_type, array $relation, $poly_type = NULL, $poly_id = NULL)
    {
      if (empty($poly_type)) {
        $poly_type = $polymorphic_name . "_type";
      }

      if (empty($poly_id)) {
        $poly_id = $polymorphic_name . "_id";
      }
      $new_collection = new \Illuminate\Database\Eloquent\Collection();

      foreach ($collection as $model) {
        if ($model->$polymorphic_name instanceof $model_type) {
          $new_collection->add($model->$polymorphic_name);
        }
      }

      $new_collection->load($relation);

      return $collection;
    }
  }

  if (!function_exists('fulltext_search_term')) {
    function fulltext_search_term($term, $allInclusive = TRUE, $wildcard = TRUE)
    {
      if (empty($term)) {
        return $term;
      }
      $term = str_replace("@", " ", $term);
      $term = str_replace("+", " ", $term);
      $term = str_replace("-", " ", $term);
      $term = str_replace("<", " ", $term);
      $term = str_replace(">", " ", $term);
      $term = str_replace("(", " ", $term);
      $term = str_replace(")", " ", $term);
      $term = str_replace("~", " ", $term);
      $term = str_replace("*", " ", $term);

      $words = explode(" ", trim($term));

      $terms = [];

      foreach ($words as $word) {
        if ($wildcard) {
          $word = $word . "*";
        }

        if (!empty($word)) {
          if ($allInclusive) {
            $word = "+" . $word;
          }

          $terms[] = $word;
        }
      }

      return implode(" ", $terms);
    }
  }




