PageRenderTime 80ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/sitemap/classes/model/sitemap.php

https://bitbucket.org/seyar/kinda.local
PHP | 192 lines | 153 code | 35 blank | 4 comment | 16 complexity | 6f8a8a8ee27786b3e7c99f5946cb6c3c MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. class Model_Sitemap extends Model {
  3. static public $sitemap = array();
  4. public static function get_from_content($parent_id = 0)
  5. {
  6. $query = DB::select('id', 'page_name','page_url', 'parent_url', 'page_title')
  7. ->from('content')
  8. ->where('status', '=', 1)
  9. ->and_where('languages_id', '=', CURRENT_LANG_ID)
  10. ->and_where('visible_to', '=', 'all')
  11. ->and_where('hide_from_map', '!=', '1')
  12. ->and_where('parent_id', '=', $parent_id)
  13. // ->order_by( DB::expr('parent_id ASC, page_order ASC') )
  14. // ->cached(60)
  15. ;
  16. $result = $query->execute();
  17. if ($result->count() == 0) return array();
  18. else
  19. {
  20. $return = array();
  21. $res = $result->as_array();
  22. foreach( $res as $item )
  23. {
  24. if( substr($item['parent_url'],-1) == '/' ) $item['parent_url'] = substr($item['parent_url'],0, -1);
  25. $item['depth'] = substr_count($item['parent_url'],'/');
  26. $item['page_name'] = $item['page_name'] ? $item['page_name'] : $item['page_title'];
  27. // $return[] = $item;
  28. Model_Sitemap::$sitemap[] = $item;
  29. if( Model_Sitemap::hasChilds($item['id']) ) //есть дети
  30. {
  31. Model_Sitemap::get_from_content($item['id']);
  32. }
  33. }
  34. return $return;
  35. }
  36. }
  37. static public function hasChilds($id)
  38. {
  39. $query = DB::select('id')
  40. ->from('content')
  41. ->where('status', '=', 1)
  42. ->and_where('languages_id', '=', CURRENT_LANG_ID)
  43. ->and_where('visible_to', '=', 'all')
  44. ->and_where('hide_from_map', '!=', '1')
  45. ->and_where('id', '=', $id)
  46. ;
  47. $result = $query->execute();
  48. if ($result->count() == 0) return false;
  49. else
  50. return true;
  51. }
  52. public static function get_pages_from_modules()
  53. {
  54. $ret = Controller_Admin::get_sitemap();
  55. $contollers_list = Model_Sitemap::get_controllers_list();
  56. foreach( $contollers_list as $item )
  57. {
  58. $method = array('Controller_'.$item['fs_name'], 'get_sitemap');
  59. try
  60. {
  61. $part_array = call_user_func( $method, false );
  62. $ret = is_array($part_array) ? array_merge($part_array, $ret) : $ret;
  63. }
  64. catch(Exception $e){}
  65. }
  66. $return = array();
  67. foreach( $ret as $item )
  68. {
  69. $return[] = array(
  70. 'page_url' => $item['url'],
  71. 'depth' => ($item['depth'] ? $item['depth'] : 0),
  72. 'page_name' => ( $item['title_page'] ? $item['title_page'] : $item['title_new'] )
  73. );
  74. }
  75. return $return;
  76. }
  77. static function get_controllers_list()
  78. {
  79. $controllers = array(
  80. array('fs_name' => 'content')
  81. );
  82. $result = DB::select()
  83. ->from('modules')
  84. ->execute();
  85. if( $result->count() == 0 ) return array();
  86. else return array_merge($controllers, $result->as_array());
  87. }
  88. static public function prepareArray($array)
  89. {
  90. $ret = array();
  91. foreach ($array as $value)
  92. {
  93. $ret[$value['parent_id']][$value['id']] =
  94. array(
  95. 'id' => $value['id'],
  96. 'parent_id' => $value['parent_id'],
  97. 'name' => $value['name'],
  98. 'page_url' => str_replace("//", "/", $value['page_url']),
  99. );
  100. }
  101. return $ret;
  102. }
  103. static function generate_sitemap_xml($lang_id = 1)
  104. {
  105. $siteMap = array();
  106. foreach (Model_Sitemap::get_controllers_list() as $controller){
  107. $htmlMethod = array('Controller_'.ucfirst($controller['fs_name']), 'sitemap');
  108. $xmlMethod = array('Controller_'.ucfirst($controller['fs_name']), 'sitemapXML');
  109. $htmlRes = call_user_func( $htmlMethod, false );
  110. if($htmlRes) $siteMap[] = $htmlRes;
  111. $xmlRes = call_user_func( $xmlMethod, false );
  112. if($xmlRes) $siteMap[] = $xmlRes;
  113. }
  114. // return Model_Sitemap::generate_sitemap_xml( $siteMap );
  115. $weights = array(0.4, 0.8, 0.64, 0.5 );
  116. $lastmods = array(0=>30, 1=>7, 2=>14, 3=>21 );
  117. $xml = '<?xml version="1.0" encoding="UTF-8"?>
  118. <urlset
  119. xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
  120. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  121. xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
  122. http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
  123. <url>
  124. <loc>http://'.$_SERVER['HTTP_HOST'].($lang_id != 1 ? '/en/':'/').'</loc>
  125. <priority>'.number_format($weights['1'],2,'.','').'</priority>
  126. <lastmod>'.date('Y-m-d', mktime(0, 0, 0, date("m") , date("d")-$lastmods['1'], date("Y"))).'</lastmod>
  127. </url>';
  128. foreach ($siteMap as $data)
  129. {
  130. foreach ($data as $item)
  131. {
  132. $xml .= '<url>
  133. <loc>http://'.str_replace("//", "/", $_SERVER['HTTP_HOST'].'/'.$item['page_url']).'</loc>
  134. <priority>'.number_format($item['priority'],2,'.','').'</priority>
  135. <lastmod>'.date('Y-m-d', mktime(0, 0, 0, date("m") , date("d")-$lastmods['1'], date("Y"))).'</lastmod>
  136. </url>';
  137. }
  138. }
  139. $xml .= '</urlset>';
  140. return $xml;
  141. }
  142. public function get_robots_txt()
  143. {
  144. $file = str_replace('//', '/', DOCROOT.'/robots.txt');
  145. if( file_exists($file) )
  146. return file_get_contents($file);
  147. }
  148. public function save($type_file)
  149. {
  150. $file = $type_file == 1 ? str_replace('//', '/', DOCROOT.'/sitemap.xml') : str_replace('//', '/', DOCROOT.'/robots.txt');
  151. $handle = fopen($file, 'w');
  152. if(!fwrite($handle, $_POST['sitemap_xml'])) return "writing error";
  153. fclose($handle);
  154. return 1;
  155. }
  156. }