<?php

if (!defined('BASEPATH'))

  exit('No direct script access allowed');



function locationName($loc) {

  $ci = CI();

  $cnt = $ci->db->select('id')->from('wps_countries_list')->where("name = '" . $loc . "'")->get()->num_rows();

  if ($cnt > 0) {

    return ucwords(strtolower(get_db_field_value('wps_countries_list', 'name', "WHERE country_temp_name = '" . $loc . "'")));

  } else {

    $cnt = $ci->db->select('id')->from('wps_states_list')->where("temp_title = '" . $loc . "'")->get()->num_rows();

    if ($cnt > 0) {

      return ucwords(strtolower(get_db_field_value('wps_states_list', 'name', "WHERE temp_title = '" . $loc . "'")));

    } else {

      $cnt = $ci->db->select('id')->from('wps_cities_list')->where("temp_title = '" . $loc . "'")->get()->num_rows();

      if ($cnt > 0) {

        return ucwords(strtolower(get_db_field_value('wps_cities_list', 'city', "WHERE temp_title = '" . $loc . "'")));

      }

    }

  }

}



function locationId($loc) {

  $ci = CI();

  $cnt = $ci->db->select('id')->from('wps_countries_list')->where("name = '" . $loc . "'")->get()->num_rows();

  if ($cnt > 0) {

    return get_db_field_value('wps_countries_list', 'id', "WHERE country_temp_name = '" . $loc . "'");

  } else {

    $cnt = $ci->db->select('id')->from('wps_states_list')->where("temp_title = '" . $loc . "'")->get()->num_rows();

    if ($cnt > 0) {

      return get_db_field_value('wps_states_list', 'id', "WHERE temp_title = '" . $loc . "'");

    } else {

      $cnt = $ci->db->select('id')->from('wps_cities_list')->where("temp_title = '" . $loc . "'")->get()->num_rows();

      if ($cnt > 0) {

        return get_db_field_value('wps_cities_list', 'id', "WHERE temp_title = '" . $loc . "'");

      }

    }

  }

}



function imagealtTitle($pretext, $name, $posttext) {

  $ci = CI();

  //Check if it is subdomain

  $uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

  $uri_segments = explode('/', $uri_path);

  $st = $uri_segments[1];

  if (strstr($st, '.html')) {

    $st = substr($st, 0, -5);

  }

  $stArray = $ci->db->query("SELECT meta_id, page_url FROM wps_meta_tags WHERE is_fixed='L' AND page_url='" . $st . "'")->row_array();

  if (is_array($stArray) & !empty($stArray)) {

    echo $pretext . ' ' . $name . ' ' . $posttext . ' from ' . ucwords(locationName($st));

  } else {

    echo $pretext . ' ' . $name . ' ' . $posttext . ' from India';

  }

}



/* Get meta tags  from  wps_meta_tags */

if (!function_exists('getMeta')) {



  function getMeta() {

    $ci = CI();

    $ci->load->config('seo/config');

    $ci->load->helper('category/category');



    //Check if it is subdomain

    $uri_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);

    $uri_segments = explode('/', $uri_path);

    $st = $uri_segments[1];

    if (strstr($st, '.html')) {

      $st = substr($st, 0, -5);

    }

    $locName = locationName($st);



    //Check if in subdomain

    $stArray = $ci->db->query("SELECT meta_id, page_url FROM wps_meta_tags WHERE is_fixed='L' AND page_url='" . $st . "'")->row_array();



    if (is_array($stArray) & !empty($stArray)) {

      $uri_page = $ci->uri->uri_string != '' ? $ci->uri->uri_string : "home";

      $uri_page = str_replace($uri_segments[1], '', $uri_page);

      $uri_page = trim($uri_page, '/');

      $urlSegment = $uri_page;



      $res = $ci->db->query("SELECT * FROM wps_meta_tags WHERE page_url='" . $st . "' ")->row();

      $location = $uri_page;

      $key1 = "";

      $key2 = "";

      $key3 = "";



      if (is_object($res)) {



        $uri_aligs_string_product = str_replace($st, '', $uri_page);

        $uri_aligs_string_product = trim($uri_aligs_string_product, '/');

        //get url Id

        $respro = $ci->db->query("SELECT * FROM wps_meta_tags WHERE page_url='" . $uri_aligs_string_product . "' ")->row();

        //trace($respro);

        if (is_object($respro) && !empty($respro)) {

          $resprosub = $ci->db->query("SELECT * FROM wps_subcontent WHERE status = '1' AND FIND_IN_SET($respro->entity_id,category_id)")->row();

          //trace($resprosub);

          if (is_object($resprosub) && !empty($resprosub)) {



            $catlink = get_parent_categories($respro->entity_id);

            $catArray = array_reverse($catlink);

            //trace($catArray);

            //get cat name

            $catName = "";

            if ($respro->entity_type == 'category/index') {

              $catName = get_db_field_value('wps_categories', 'category_name', "WHERE category_id = '" . $respro->entity_id . "'");

            }

            $locId = get_db_field_value("wps_meta_tags", "meta_id", "WHERE page_url = '" . $st . "'");

            $resprosubloc = $ci->db->query("SELECT * FROM wps_subcontent WHERE status = '1' AND FIND_IN_SET($respro->entity_id,category_id) AND FIND_IN_SET($locId,location_id)")->row();

            //trace($resprosubloc);

            //With location and category

            if (is_object($resprosubloc) && !empty($resprosubloc)) {//With location and category

              $key1 = "";

              $key2 = "";

              $key3 = "";

              if ($resprosubloc->meta_title && $resprosubloc->meta_description) {

                //echo "1"; die;

                return array(

                    "meta_title" => str_replace('{catname}', $catName, str_replace('{location}', $locName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, $resprosubloc->meta_title))))),

                    "meta_keyword" => str_replace('{catname}', $catName, str_replace('{location}', $locName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, $resprosubloc->meta_keyword))))),

                    "meta_description" => str_replace('{catname}', $catName, str_replace('{location}', $locName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, $resprosubloc->meta_description))))),

                    "entity_type" => $respro->entity_type,

                    "entity_id" => $respro->entity_id,

                    "page_url" => $respro->page_url

                );

              } else {

                return array(

                    "meta_title" => "$catName Factories in $locName - Manufacturers & Suppliers",

                    "meta_keyword" => "$catName Manufacturers, $catName Manufacturers $locName, $catName Factory, $catName Factory in $locName",

                    "meta_description" => "Presenting $catName Genuine Manufacturers in $locName with Video of Factory as proof, with address & Phone Number. Save by buying at Factory Price. Manufacturers who are also Wholesale $catName Suppliers.",

                    "entity_type" => $respro->entity_type,

                    "entity_id" => $respro->entity_id,

                    "page_url" => $respro->page_url

                );

              }

            } else {//With category only

              $key1 = "";

              $key2 = "";

              $key3 = "";

              if ($resprosub->meta_title && $resprosub->meta_description) {

                return array(

                    "meta_title" => str_replace('{catname}', $catName, str_replace('{location}', $locName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, $resprosub->meta_title))))),

                    "meta_keyword" => str_replace('{catname}', $catName, str_replace('{location}', $locName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, $resprosub->meta_keyword))))),

                    "meta_description" => str_replace('{catname}', $catName, str_replace('{location}', $locName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, $resprosub->meta_description))))),

                    "entity_type" => $respro->entity_type,

                    "entity_id" => $respro->entity_id,

                    "page_url" => $respro->page_url

                );

              } else {

                return array(

                    "meta_title" => "$catName Factories in $locName - Manufacturers & Suppliers",

                    "meta_keyword" => "$catName Manufacturers, $catName Manufacturers $locName, $catName Factory, $catName Factory in $locName",

                    "meta_description" => "Presenting $catName Genuine Manufacturers in $locName with Video of Factory as proof, with address & Phone Number. Save by buying at Factory Price. Manufacturers who are also Wholesale $catName Suppliers.",

                    "entity_type" => $respro->entity_type,

                    "entity_id" => $respro->entity_id,

                    "page_url" => $respro->page_url

                );

              }

            }

          } else {

            $resprosubloc = $ci->db->query("SELECT * FROM wps_subloccontent WHERE status = '1' AND FIND_IN_SET($respro->meta_id,location_id)")->row();

            if (is_object($resprosubloc) && !empty($resprosubloc)) {

              $key1 = "";

              $key2 = "";

              $key3 = "";

              return array(

                  "meta_title" => str_replace('{location}', $locName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, $resprosubloc->meta_title)))),

                  "meta_keyword" => str_replace('{location}', $locName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, $resprosubloc->meta_keyword)))),

                  "meta_description" => str_replace('{location}', $locName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, $resprosubloc->meta_description)))),

                  "entity_type" => $respro->entity_type,

                  "entity_id" => $respro->entity_id,

                  "page_url" => $respro->page_url

              );

            } else {

              $key1 = "";

              $key2 = "";

              $key3 = "";

              $catName = "";

              //print_r($respro); die;

              if ($respro->entity_type == 'category/index') {

                $catName = get_db_field_value('wps_categories', 'category_name', "WHERE category_id = '" . $respro->entity_id . "'");

              }



              if ($respro->meta_title && $respro->meta_description) {

                //echo "hi"; die;

                return array(

                    "meta_title" => str_replace('{catname}', $catName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, str_replace('{location}', $locName, $respro->meta_title))))),

                    "meta_keyword" => str_replace('{catname}', $catName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, str_replace('{location}', $locName, $respro->meta_keyword))))),

                    "meta_description" => str_replace('{catname}', $catName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, str_replace('{location}', $locName, $respro->meta_description))))),

                    "entity_type" => $respro->entity_type,

                    "entity_id" => $respro->entity_id,

                    "page_url" => $respro->page_url

                );

              } else {

                return array(

                    "meta_title" => "$catName Factories in $locName - Manufacturers & Suppliers",

                    "meta_keyword" => "$catName Manufacturers, $catName Manufacturers $locName, $catName Factory, $catName Factory in $locName",

                    "meta_description" => "Presenting $catName Genuine Manufacturers in $locName with Video of Factory as proof, with address & Phone Number. Save by buying at Factory Price. Manufacturers who are also Wholesale $catName Suppliers.",

                    "entity_type" => $respro->entity_type,

                    "entity_id" => $respro->entity_id,

                    "page_url" => $respro->page_url

                );

              }

            }

          }

        } else {



          $resprosubloc = $ci->db->query("SELECT * FROM wps_subloccontent WHERE status = '1' AND FIND_IN_SET($res->meta_id,location_id)")->row();

          if (is_object($resprosubloc) && !empty($resprosubloc)) {



            $key1 = "";

            $key2 = "";

            $key3 = "";

            return array(

                "meta_title" => str_replace('{location}', $locName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, $resprosubloc->meta_title)))),

                "meta_keyword" => str_replace('{location}', $locName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, $resprosubloc->meta_keyword)))),

                "meta_description" => str_replace('{location}', $locName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, $resprosubloc->meta_description)))),

                "entity_type" => $res->entity_type,

                "entity_id" => $res->entity_id,

                "page_url" => $res->page_url

            );

          } else {

            $catName = "";

            if ($res->entity_type == 'category/index') {

              $catName = get_db_field_value('wps_categories', 'category_name', "WHERE category_id = '" . $res->entity_id . "'");

            }

            return array(

                "meta_title" => str_replace('{catname}', $catName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, str_replace('{location}', $locName, $res->meta_title))))),

                "meta_keyword" => str_replace('{catname}', $catName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, str_replace('{location}', $locName, $res->meta_keyword))))),

                "meta_description" => str_replace('{catname}', $catName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, str_replace('{location}', $locName, $res->meta_description))))),

                "entity_type" => $res->entity_type,

                "entity_id" => $res->entity_id,

                "page_url" => $res->page_url

            );

          }

        }

      } else {

        $uri_aligs_string = str_replace($st, '', $uri_page);

        $uri_aligs_string = trim($uri_aligs_string, '/');

        //check in database

        $res = $ci->db->query("SELECT * FROM wps_meta_tags WHERE page_url='" . $uri_aligs_string . "' ")->row();

        if (is_object($res)) {

          $catName = "";

          if ($res->entity_type == 'category/index') {

            $catName = get_db_field_value('wps_categories', 'category_name', "WHERE category_id = '" . $res->entity_id . "'");

          }

          $key1 = "";

          $key2 = "";

          $key3 = "";

          return array(

              "meta_title" => str_replace('{catname}', $catName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, str_replace('{location}', $locName, $res->meta_title))))),

              "meta_keyword" => str_replace('{catname}', $catName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, str_replace('{location}', $locName, $res->meta_keyword))))),

              "meta_description" => str_replace('{catname}', $catName, str_replace('{key3}', $key3, str_replace('{key2}', $key2, str_replace('{key1}', $key1, str_replace('{location}', $locName, $res->meta_description))))),

              "entity_type" => $res->entity_type,

              "entity_id" => $res->entity_id,

              "page_url" => $res->page_url

          );

        } else {

          return $ci->config->item('default_meta');

        }

      }

    } else {

      $isCompanyUrls = $ci->db->query("SELECT page_url FROM wps_meta_tags WHERE is_fixed='C' AND page_url='" . $st . "'")->row_array();

      if (isset($uri_segments[2]) && $uri_segments[2] != '' && is_array($isCompanyUrls) && !empty($isCompanyUrls)) {

        $uri_page = $ci->uri->uri_string != '' ? $ci->uri->uri_string : "home";

        $res = $ci->db->query("SELECT * FROM wps_meta_tags_company WHERE is_fixed='N' AND page_url='" . $uri_page . "'")->row();

        //trace($res);

        if (is_object($res) && !empty($res)) {

          return array(

              "meta_title" => $res->meta_title,

              "meta_keyword" => $res->meta_keyword,

              "meta_description" => $res->meta_description,

              "entity_type" => $res->entity_type,

              "entity_id" => $res->entity_id,

              "page_url" => $res->page_url

          );

        }

      } else {

        $uri_page = $ci->uri->uri_string != '' ? $ci->uri->uri_string : "home";

        $res = $ci->db->query("SELECT * FROM wps_meta_tags WHERE page_url='" . $uri_page . "'")->row();

        //echo "SELECT * FROM wps_meta_tags WHERE page_url='" . $uri_page . "'";

        if ($ci->router->fetch_class() == 'category' || $ci->router->fetch_class() == 'products') {

          if ($ci->router->fetch_class() == 'category') {

            $nm = get_db_field_value("wps_categories", 'category_name', "WHERE category_id = '" . @$res->entity_id . "'");

            if ($ci->uri->segment(1) == 'directory') {

              return array(

                  "meta_title" => "Indian Natural Therapy Companies - Manufacturers, Practitioners, Courses",

                  "meta_description" => "Find here listing of Indian companies dealing in Natural Therapy Products, Services & Courses from all over India with their phone number, addresses.",

                  "meta_keyword" => "Indian Natural Health Companies,Natural Therapists India, Natural Therapy Practitioners India,Natural Therapy  Training Schools India, Natural Therapy Service Providers India",

                  "entity_type" => $res->entity_type,

                  "entity_id" => $res->entity_id,

                  "page_url" => $res->page_url

              );

            } else {

              if (@$res->meta_title) {

                return array(

                    "meta_title" => $res->meta_title,

                    "meta_keyword" => $res->meta_keyword,

                    "meta_description" => $res->meta_description,

                    "entity_type" => @$res->entity_type,

                    "entity_id" => @$res->entity_id,

                    "page_url" => @$res->page_url

                );

              } else {

                return array(

                    "meta_title" => "$nm Factories in India - Manufacturers & Suppliers",

                    "meta_description" => "Presenting $nm Genuine Manufacturers with Video of Factory as proof, with address & Phone Number. Save by buying at Factory Price. Manufacturers who are also Wholesale $nm Suppliers.",

                    "meta_keyword" => "$nm Manufacturers,$nm Manufacturers India, $nm Factory,$nm Factory in India",

                    "entity_type" => @$res->entity_type,

                    "entity_id" => @$res->entity_id,

                    "page_url" => @$res->page_url

                );

              }

            }

          } else {

            if (isset($res->entity_id) && $res->entity_id > 0) {

              $nm = get_db_field_value("wps_products", 'product_name', "WHERE products_id = '" . $res->entity_id . "'");

              return array(

                  "meta_title" => "Buy $nm online in Delhi, India",

                  "meta_keyword" => "Buy $nm online in Delhi",

                  "meta_description" => "Buy $nm online at bets price from RJ Attires, leading ladies wear store in Delhi. Free Shipping & FREE returns!, Cash on Delivery. Call @ +91-9810090130.",

                  "entity_type" => $res->entity_type,

                  "entity_id" => $res->entity_id,

                  "page_url" => $res->page_url

              );

            } else {

              return $ci->config->item('default_meta');

            }

          }

        } else {

          if (is_object($res) && !empty($res)) {

            return array(

                "meta_title" => $res->meta_title,

                "meta_keyword" => $res->meta_keyword,

                "meta_description" => $res->meta_description,

                "entity_type" => $res->entity_type,

                "entity_id" => $res->entity_id,

                "page_url" => $res->page_url

            );

          } else {

            return $ci->config->item('default_meta');

          }

        }

      }

    }

  }



}



/* Get meta tags  from  wps_meta_tags */

if (!function_exists('getMeta_old')) {



  function getMeta_old() {

    $ci = CI();

    $ci->load->config('seo/config');

    $uri_page = $ci->uri->uri_string != '' ? $ci->uri->uri_string : "home";

    $res = $ci->db->query("SELECT * FROM wps_meta_tags WHERE page_url='" . $uri_page . "' ")->row();

    if (is_object($res)) {

      if ($res->meta_title) {

        return array(

            "meta_title" => $res->meta_title,

            "meta_keyword" => $res->meta_keyword,

            "meta_description" => $res->meta_description,

            "entity_type" => $res->entity_type,

            "entity_id" => $res->entity_id,

            "page_url" => $res->page_url

        );

      } else {

        if ($ci->router->fetch_class() == 'category' || $ci->router->fetch_class() == 'products') {

          if ($ci->router->fetch_class() == 'category') {

            $nm = get_db_field_value("wps_categories", 'category_name', "WHERE category_id = '" . $res->entity_id . "'");

            if ($ci->uri->segment(1) == 'directory') {

              return array(

                  "meta_title" => "Indian Manufacturers Directory, Manufacturing Companies India",

                  "meta_description" => "Natural Therapy India - Directory of verified Indian manufacturers, updated database of genuine manufacturers and manufacturing companies from India.",

                  "meta_keyword" => "Indian Manufacturers Directory, Manufacturing Companies India, Indian Manufacturers",

                  "entity_type" => $res->entity_type,

                  "entity_id" => $res->entity_id,

                  "page_url" => $res->page_url

              );

            } else {

              return array(

                  "meta_title" => "Genuine Manufacturers of $nm from India,Wholesale $nm Manufacturer India",

                  "meta_description" => "Looking for Genuine Manufacturers of $nm from India? Natural Therapy India is one and only directory which offers list of Verified Wholesale $nm Manufacturer from India.",

                  "meta_keyword" => "$nm Manufacturers, $nm Manufacturer,Wholesale $nm, Wholesale $nm Manufacturers",

                  "entity_type" => $res->entity_type,

                  "entity_id" => $res->entity_id,

                  "page_url" => $res->page_url

              );

            }

          } else {

            $nm = get_db_field_value("wps_products", 'product_name', "WHERE products_id = '" . $res->entity_id . "'");

            return array(

                "meta_title" => "Buy $nm online in Delhi, India",

                "meta_keyword" => "Buy $nm online in Delhi",

                "meta_description" => "Buy $nm online at bets price from RJ Attires, leading ladies wear store in Delhi. Free Shipping & FREE returns!, Cash on Delivery. Call @ +91-9810090130.",

                "entity_type" => $res->entity_type,

                "entity_id" => $res->entity_id,

                "page_url" => $res->page_url

            );

          }

        } else {

          return array(

              "meta_title" => $res->meta_title,

              "meta_keyword" => $res->meta_keyword,

              "meta_description" => $res->meta_description,

              "entity_type" => $res->entity_type,

              "entity_id" => $res->entity_id,

              "page_url" => $res->page_url

          );

        }

      }

    } else {

      return $ci->config->item('default_meta');

    }

  }



}



/* Check  meta url already exits  */



if (!function_exists('check_meta')) {



  function check_meta($page_url) {

    $num = 0;

    if ($page_url != '') {

      $ci = CI();

      $ci->db->from('wps_meta_tags');

      $ci->db->where(array('page_url' => $page_url));

      $num = $ci->db->count_all_results();

    }

    return $num;

  }



}



/* Use  to insert all default page meta into  wps_meta_tags  */

if (!function_exists('create_listing_page_meta')) {



  function create_listing_page_meta() {

    $ci = CI();

    $ci->load->config('seo/config');

    $page_array = $ci->config->item('controller_name');



    if (is_array($page_array) && !empty($page_array)) {

      foreach ($page_array as $k => $v) {



        $meta_array = array(

            'is_fixed' => 'Y',

            'entity_type' => $v,

            'entity_id' => 0,

            'page_url' => $k,

            'meta_title' => $k,

            'meta_description' => $k,

            'meta_keyword' => $k

        );

        create_meta($meta_array);

      }

    }

  }



}



/* Upade meta url */

if (!function_exists('update_meta_page_url')) {



  function update_meta_page_url($type, $entity_id, $page_url) {

    $ci = CI();

    if ($entity_id != '' && $type != '' && $page_url != '') {

      $where = "entity_type ='" . $type . "' AND entity_id = $entity_id  ";

      $cnt = count_record('wps_meta_tags', "entity_type ='" . $type . "' AND entity_id = $entity_id  ");

      //echo $cnt; exit;

      if ($cnt > 0) {

        $ci->db->update("wps_meta_tags", array('page_url' => $page_url), $where, FALSE);

      } else {

        $ci->db->query("INSERT INTO wps_meta_tags SET entity_type ='" . $type . "', entity_id = '" . $entity_id . "', page_url = '" . $page_url . "'");

      }

    }

  }



}





/* Create  meta tags  */

if (!function_exists('create_meta')) {



  function create_meta($data = array()) {

    $ci = CI();

    if (is_array($data) && !empty($data) && array_key_exists('page_url', $data)) {

      $ci->db->insert("wps_meta_tags", $data);

    }

  }



}



// create meta for company website url

if (!function_exists('create_meta_company')) {



  function create_meta_company($data = array()) {

    $ci = CI();

    if (is_array($data) && !empty($data) && array_key_exists('page_url', $data)) {

      $ci->db->insert("wps_meta_tags_company", $data);

    }

  }



}





/* Upade meta url */

if (!function_exists('update_comp_meta_page_url')) {



  function update_comp_meta_page_url($type, $entity_id, $page_url, $companyId) {

    $ci = CI();

    if ($entity_id != '' && $type != '' && $page_url != '') {

      $where = "entity_type ='" . $type . "' AND entity_id = '" . $entity_id . "' AND companyId = '" . $companyId . "'";

      $cnt = count_record('wps_meta_tags_company', "entity_type ='" . $type . "' AND entity_id = '" . $entity_id . "'  AND companyId = '" . $companyId . "'");

      if ($cnt > 0) {

        $ci->db->update("wps_meta_tags_company", array('page_url' => $page_url), $where, FALSE);

      } else {

        $ci->db->query("INSERT INTO wps_meta_tags_company SET entity_type ='" . $type . "', entity_id = '" . $entity_id . "', page_url = '" . $page_url . "', companyId = '" . $companyId . "'");

      }

    }

  }



}





if (!function_exists('seo_url_title')) {



  function seo_url_title($str) {

    $str = preg_replace("~[^a-zA-Z0-9-_/]~", "-", $str);

    $str = preg_replace("~[/]{2,15}~", "/", $str);

    $str = preg_replace("~[-]{2,15}~", "-", $str);

    $str = strtolower($str);

    $str = trim($str, "-");

    return $str;

  }



}



function seo_add_form_element($params) {

  $default_params = array(

      'heading_element' => array(

          'field_heading' => "Page Name",

          'field_name' => "page_name",

          'field_placeholder' => "Your Page Name",

          'exparams' => 'size="60"'

      ),

      'url_element' => array(

          'field_heading' => "Page URL",

          'field_name' => "friendly_url",

          'field_placeholder' => "Your Page URL",

          'exparams' => 'size="30"',

          'pre_seo_url' => '',

          'pre_url_tag' => TRUE

      ),

      'product_url_element' => array(

          'show_component' => FALSE,

          'field_heading' => "Page URL",

          'field_name' => "product_friendly_url",

          'field_placeholder' => "Your Page URL",

          'exparams' => 'size="30"',

          'pre_seo_url' => '',

          'pre_url_tag' => FALSE

      )

  );



  if (array_key_exists('heading_element', $params)) {

    $default_params['heading_element'] = array_merge($default_params['heading_element'], $params['heading_element']);

  }

  if (array_key_exists('url_element', $params)) {

    $default_params['url_element'] = array_merge($default_params['url_element'], $params['url_element']);

  }

  if (array_key_exists('product_url_element', $params)) {

    $default_params['product_url_element'] = array_merge($default_params['product_url_element'], $params['product_url_element']);

  }





  $heading_element_field_heading = $default_params['heading_element']['field_heading'];



  $heading_element_field_name = $default_params['heading_element']['field_name'];



  $heading_element_field_placeholder = $default_params['heading_element']['field_placeholder'];



  $heading_element_field_exparams = $default_params['heading_element']['exparams'];



  $url_element_field_heading = $default_params['url_element']['field_heading'];



  $url_element_field_name = $default_params['url_element']['field_name'];



  $url_element_field_placeholder = $default_params['url_element']['field_placeholder'];



  $url_element_field_exparams = $default_params['url_element']['exparams'];



  $pre_seo_url = $default_params['url_element']['pre_seo_url'];



  $pre_url_tag = $default_params['url_element']['pre_url_tag'];





  $url_pelement_field_heading = $default_params['product_url_element']['field_heading'];



  $url_pelement_field_name = $default_params['product_url_element']['field_name'];



  $url_pelement_field_placeholder = $default_params['product_url_element']['field_placeholder'];



  $url_pelement_field_exparams = $default_params['product_url_element']['exparams'];



  $pre_pseo_url = $default_params['product_url_element']['pre_seo_url'];



  $pre_purl_tag = $default_params['product_url_element']['pre_url_tag'];

  ?>



  <div class="form-group">

    <label class="col-md-3 control-label"><span class="required">*</span><?php echo $heading_element_field_heading; ?></label>

    <div class="col-md-6">                                            





      <input type="text" name="<?php echo $heading_element_field_name; ?>" value="<?php echo set_value($heading_element_field_name); ?>"  class="url_creator form-control" placeholder="<?php echo $heading_element_field_placeholder; ?>" <?php echo $heading_element_field_exparams; ?> /> <a href="#" class="url_from_title">Create URL</a>

      <div id="error_url_creator" class="red"><?php echo form_error($heading_element_field_name); ?></div>





    </div>

  </div>



  <div class="form-group">                                        

    <label class="col-md-3 control-label"><span class="required">**</span><?php echo $url_element_field_heading; ?></label>

    <div class="col-md-6 col-xs-12">

      <div class="seo_url">

        <?php

        if ($pre_url_tag === TRUE) {

          ?>

          <input type="hidden" name="pre_seo_url" id="pre_seo_url" value="<?php echo $pre_seo_url; ?>" title="<?php echo $pre_seo_url; ?>" readonly />

          <?php

        }

        ?>

        <input type="text" name="<?php echo $url_element_field_name; ?>" value="<?php echo set_value($url_element_field_name); ?>"  class="seo_friendly_url form-control" placeholder="<?php echo $url_element_field_placeholder; ?>" <?php echo $url_element_field_exparams; ?> readonly/> <a href="#" class="change_url">Change</a>

      </div>

      <div id="error_friendly_url" class="red"><?php echo form_error($url_element_field_name); ?></div>

    </div>

  </div>



  <?php

  if ($default_params['product_url_element']['show_component'] === TRUE) {

    ?>

    <tr class="trOdd">

      <td height="26" align="right"><span class="required">**</span> <?php echo $url_pelement_field_heading; ?> :</td>

      <td>

        <div class="seo_url">

          <?php

          if ($pre_purl_tag === TRUE) {

            ?>

            <input type="text" name="pre_product_seo_url" id="pre_product_seo_url" value="<?php echo $pre_pseo_url; ?>"  size="40" title="<?php echo $pre_pseo_url; ?>" readonly />

            <?php

          }

          ?>

          <input type="text" name="<?php echo $url_pelement_field_name; ?>" value="<?php echo set_value($url_pelement_field_name); ?>"  class="seo_pfriendly_url" id="seo_pfriendly_url" placeholder="<?php echo $url_pelement_field_placeholder; ?>" <?php echo $url_pelement_field_exparams; ?> readonly/> <a href="#" class="change_url">Change</a>

        </div>

        <div id="error_pfriendly_url" class="red"><?php echo form_error($url_pelement_field_name); ?></div>

      </td>

    </tr>

    <?php

  }

}



function seo_edit_form_element($params) {



  //trace($params);



  $default_params = array(

      'heading_element' => array(

          'field_heading' => "Page Name",

          'field_name' => "page_name",

          'field_value' => "",

          'field_placeholder' => "Your Page Name",

          'exparams' => 'size="60"'

      ),

      'url_element' => array(

          'field_heading' => "Page URL",

          'field_name' => "friendly_url",

          'field_value' => "",

          'field_placeholder' => "Your Page URL",

          'exparams' => 'size="30"',

          'pre_seo_url' => '',

          'pre_url_tag' => FALSE

      )

  );



  if (array_key_exists('heading_element', $params)) {

    $default_params['heading_element'] = array_merge($default_params['heading_element'], $params['heading_element']);

  }

  if (array_key_exists('url_element', $params)) {

    $default_params['url_element'] = array_merge($default_params['url_element'], $params['url_element']);

  }







  $heading_element_field_heading = $default_params['heading_element']['field_heading'];



  $heading_element_field_name = $default_params['heading_element']['field_name'];



  $heading_element_field_value = $default_params['heading_element']['field_value'];



  $heading_element_field_placeholder = $default_params['heading_element']['field_placeholder'];



  $heading_element_field_exparams = $default_params['heading_element']['exparams'];



  if (array_key_exists('cat_id', $params['heading_element'])) {

    //echo 'ssss';

    $heading_cat_id = $default_params['heading_element']['cat_id'];

  } else {

    $heading_cat_id = "";

  }



  $url_element_field_heading = $default_params['url_element']['field_heading'];



  $url_element_field_name = $default_params['url_element']['field_name'];



  $url_element_field_value = $default_params['url_element']['field_value'];



  $url_element_field_placeholder = $default_params['url_element']['field_placeholder'];



  $url_element_field_exparams = $default_params['url_element']['exparams'];



  $pre_seo_url = $default_params['url_element']['pre_seo_url'];



  $pre_url_tag = $default_params['url_element']['pre_url_tag'];

  ?>

  <div class="form-group">

    <label class="col-md-3 control-label"><span class="required">*</span> <?php echo $heading_element_field_heading; ?> :</label>

    <div class="col-md-6">

      <input type="text" name="<?php echo $heading_element_field_name; ?>" value="<?php echo set_value($heading_element_field_name, $heading_element_field_value); ?>"  placeholder="<?php echo $heading_element_field_placeholder; ?>" <?php echo $heading_element_field_exparams; ?> /> <br />

      <div id="error_url_creator" class="red"><?php echo form_error($heading_element_field_name); ?></div>

    </div>

  </div>



  <div class="form-group">

    <label class="col-md-3 control-label"><span class="required">**</span> <?php echo $url_element_field_heading; ?> :</label>

    <div class="col-md-6 col-xs-12">

      <div class="seo_url">

        <?php

        if ($pre_url_tag === TRUE) {

          ?>

          <input type="text" name="pre_seo_url" id="pre_seo_url" <?php echo $heading_element_field_exparams; ?> value="<?php echo $pre_seo_url; ?>"  title="<?php echo $pre_seo_url; ?>" <?php echo ($heading_cat_id <= 4 && $heading_cat_id != '') ? 'readonly="readonly"' : ''; ?> />

          <?php

        }

        ?>

        <input type="text" name="<?php echo $url_element_field_name; ?>" value="<?php echo set_value($url_element_field_name, $url_element_field_value); ?>"  class="seo_friendly_url edit_url form-control" placeholder="<?php echo $url_element_field_placeholder; ?>" <?php echo $url_element_field_exparams; ?> <?php echo ($heading_cat_id <= 4 && $heading_cat_id != '') ? 'readonly="readonly"' : ''; ?> /> 

      </div>

      <div id="error_friendly_url" class="red"><?php echo form_error($url_element_field_name); ?></div>

      <script type="text/javascript">

        prev_url_val = '<?php echo set_value($url_element_field_name, $url_element_field_value); ?>';

      </script>

    </div>

  </div>



  <?php

}



/* * ***************** Making the Meta tags  much better  ************** */



if (!function_exists('clean')) {



  function clean($text) {

    $text = html_entity_decode($text, ENT_QUOTES, 'UTF-8');

    $text = strip_tags($text); //erases any html markup

    $text = preg_replace('/\s\s+/', ' ', $text); //erase possible duplicated white spaces

    $text = str_replace(array('\r\n', '\n', '+'), ',', $text); //replace possible returns 

    return trim($text);

  }



}



if (!function_exists('cmp')) {



  function cmp($a, $b) {

    if ($a == $b)

      return 0;



    return ($a < $b) ? 1 : -1;

  }



}



if (!function_exists('get_text')) {



  function get_text($text, $length = 220) {

    return limit_chars(clean($text), $length, '', TRUE);

  }



}



if (!function_exists('get_keywords')) {



  function get_keywords($text, $max_keys = 20) {

    $ci = CI();

    $ci->load->config('seo/config');

    $min_word_length = $ci->config->item('min_word_length');

    $banned_words = $ci->config->item('banned_words');

    //array to keep word->number of repetitions 

    $wordcount = array_count_values(str_word_count(clean($text), 1));



    foreach ($wordcount as $key => $value) {

      if ((strlen($key) <= $min_word_length) || in_array($key, $banned_words))

        unset($wordcount[$key]);

    }

    //sort keywords from most repetitions to less 

    uasort($wordcount, 'cmp');

    //keep only X keywords

    $wordcount = array_slice($wordcount, 0, $max_keys);

    //return keywords on a string

    return implode(', ', array_keys($wordcount));

  }



}



if (!function_exists('limit_chars')) {



  function limit_chars($str, $limit = 100, $end_char = NULL, $preserve_words = FALSE) {

    $end_char = ($end_char === NULL) ? 'â€¦' : $end_char;



    $limit = (int) $limit;



    if (trim($str) === '' || strlen($str) <= $limit)

      return $str;



    if ($limit <= 0)

      return $end_char;



    if ($preserve_words === FALSE)

      return rtrim(mb_substr($str, 0, $limit)) . $end_char;



    // Don't preserve words. The limit is considered the top limit.

    // No strings with a length longer than $limit should be returned.

    if (!preg_match('/^.{0,' . $limit . '}\s/us', $str, $matches))

      return $end_char;



    return rtrim($matches[0]) . ((strlen($matches[0]) === strlen($str)) ? '' : $end_char);

  }



}