<?php

class Cron extends Public_Controller {

  public function __construct() {
    parent::__construct();
    $this->load->model(array('sitepanel/setting_model'));
    $this->load->library(array('safe_encrypt', 'securimage_library', 'Auth', 'Dmailer', 'cart'));
  }

  public function index() {

    $t = "\t";
    $n = "\n";
    $root = $_SERVER['DOCUMENT_ROOT'];
    $date = gmdate("Y-m-d\TH:i:s\Z");
    $xml = '<?xml version="1.0" encoding="UTF-8"?>' . "$n";
    $xml .= '<urlset
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	  xmlns:xhtml="http://www.w3.org/1999/xhtml"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "$n";
    $xml .= '<url>
      <loc>https://www.getmanufacturers.com/</loc>
      <lastmod>' . $date . '</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
      </url>
    ' . $n;
    
    $resall = $this->db->query("SELECT * FROM wps_meta_tags WHERE is_fixed = 'Y'")->result_array();
    foreach ($resall as $row) {
      $xml .= '<url>
        <loc>https://www.getmanufacturers.com/' . $row['page_url'] . '</loc>
        <lastmod>' . $date . '</lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
        </url>
      ' . $n;
    }
    
    $res = $this->db->query("SELECT mt.* FROM wps_meta_tags as mt LEFT JOIN wps_categories as ca ON mt.entity_id = ca.category_id WHERE mt.is_fixed  NOT IN ('Y','L') AND ca.status = '1' AND entity_type = 'category/index'")->result_array();
    foreach ($res as $row) {
      $xml .= '<url>
        <loc>https://www.getmanufacturers.com/' . $row['page_url'] . '</loc>
        <lastmod>' . $date . '</lastmod>
        <changefreq>daily</changefreq>
        <priority>1.0</priority>
        </url>
      ' . $n;
    }

    //add additional sitemaps
    $location = $this->db->query("SELECT * FROM wps_meta_tags WHERE is_fixed = 'L' AND meta_id = '961'")->result_array();
    $counter = 1;
    foreach ($location as $locres) {
      $xml .= '<url>
      <loc>https://www.getmanufacturers.com/sitemap' . $counter . '.xml</loc>
      <lastmod>' . $date . '</lastmod>
      <priority>1.0</priority>
      </url>' . $n;
      $counter++;
    }

    $xml .= '</urlset>';
    $handle = fopen($root . '/sitemap.xml', 'w+');
    fwrite($handle, $xml);
    fclose($handle);

    //for subdomain
    $location = $this->db->query("SELECT * FROM wps_meta_tags WHERE is_fixed = 'L' AND meta_id = '961'")->result_array();
    $cnt = 1;
    foreach ($location as $res) {
      $xml = '<?xml version="1.0" encoding="UTF-8"?>' . "$n";
      $xml .= '<urlset
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	  xmlns:xhtml="http://www.w3.org/1999/xhtml"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "$n";
      $xml .= '<url>
      <loc>https://www.getmanufacturers.com/' . $res['page_url'] . '</loc>
      <lastmod>' . $date . '</lastmod>
      <changefreq>daily</changefreq>
      <priority>1.0</priority>
      </url>
    ' . $n;
      $result = $this->db->query("SELECT mt.* FROM wps_meta_tags as mt LEFT JOIN wps_categories as ca ON mt.entity_id = ca.category_id WHERE mt.is_fixed  NOT IN ('Y','L') AND ca.status = '1' AND mt.entity_type = 'category/index'")->result_array();
      foreach ($result as $row) {
        $xml .= '<url>
        <loc>https://www.getmanufacturers.com/' . $res['page_url'] . '/' . $row['page_url'] . '</loc>
        <lastmod>' . $date . '</lastmod>
        <changefreq>weekly</changefreq>
        <priority>1.0</priority>
        </url>
      ' . $n;
      }
      $xml .= '</urlset>';
      $handle = fopen($root . '/sitemap' . $cnt . '.xml', 'w+');
      fwrite($handle, $xml);
      fclose($handle);
      $cnt++;
      //break;
    }
  }

}
