PageRenderTime 65ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/application/classes/model/content.php

https://bitbucket.org/seyar/kinda.local
PHP | 222 lines | 161 code | 33 blank | 28 comment | 19 complexity | da2356aa14ad6642546931930db74b5b MD5 | raw file
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. class Model_Content extends Model {
  3. static public $request;
  4. static public function error()
  5. {
  6. echo Request::factory('/error/404')->execute();
  7. exit();
  8. Model_Content::$request = Request::instance();
  9. Model_Content::$request->status = 404;
  10. Model_Content::$request->title = '404 : Page not found';
  11. Model_Content::$request->response = View::factory('errors/404');
  12. if (!IN_PRODUCTION)
  13. {
  14. Model_Content::$request->response .= "<br>Current routes: <br><br>";
  15. $uri = Request::instance()->uri();
  16. foreach (Route::all() as $rn=>$r)
  17. {
  18. Model_Content::$request->response .= $rn . Kohana::debug($r->matches($uri));
  19. }
  20. }
  21. echo Model_Content::$request->send_headers()->response;
  22. exit();
  23. }
  24. static public function get_languages_array()
  25. {
  26. $query = DB::select('id','prefix', 'shortname')
  27. ->from('languages')
  28. ->order_by('id')
  29. ->as_assoc()
  30. ->cached(120)
  31. ;
  32. $result = $query->execute();
  33. if ($result->count() == 0) return array();
  34. else
  35. {
  36. return $result->as_array('id');
  37. }
  38. }
  39. static public function parse_uri($request_uri)
  40. {
  41. $lang_id = 0;
  42. $uri_parts = explode('/', $request_uri);
  43. $lang_array = Model_Content::get_languages_array();
  44. foreach ($lang_array as $value)
  45. {
  46. if($value['prefix'] == Request::instance()->param('lang') )
  47. $lang_id = $value['id'];
  48. }
  49. unset($uri_parts[0]);
  50. $uri_rest = implode('/', $uri_parts);
  51. return (string)$uri_rest;
  52. }
  53. public function find_static_page($uri)
  54. {
  55. $uri = $uri == '/' ? '' : '/'.rtrim($uri,'/');
  56. $uri = str_replace('//','/',$uri);
  57. if (!defined('CURRENT_LANG_ID'))
  58. Model_Content::error();
  59. $query = DB::query(Database::SELECT,
  60. 'SELECT content.* FROM content '
  61. .' WHERE status > 0 '
  62. .($uri ? " AND REPLACE( CONCAT(parent_url,'/',page_url), '//', '/') = '$uri'" : ' and is_default = 1' )
  63. ." AND languages_id = " . CURRENT_LANG_ID
  64. .' LIMIT 1')
  65. ->cached(10)
  66. ;
  67. $result = $query->execute();
  68. if ($result->count() == 0) self::error();
  69. $page_info = $result->current();
  70. $this->template = $page_info["page_template"];
  71. $this->page_info = $page_info;
  72. $this->blocks = Model_Content::load_global_blocks();
  73. return true;
  74. }
  75. static public function load_domain_info()
  76. {
  77. $query = DB::select()
  78. ->from('domains')
  79. ->where('http_host', '=', $_SERVER['HTTP_HOST'])
  80. ->limit(1)
  81. ->cached(7200)
  82. ;
  83. $result = $query->execute();
  84. if ($result->count() == 0) return array(
  85. 'http_host' => $_SERVER['HTTP_HOST'],
  86. 'languages_id' => CURRENT_LANG_ID,
  87. 'domain_title' => 'Default Template',
  88. 'domain_keywords' => 'Keywords',
  89. 'domain_description' => 'Description',
  90. 'domain_template' => 'default',
  91. );
  92. else
  93. {
  94. return $result->current();
  95. }
  96. }
  97. static public function load_global_blocks()
  98. {
  99. $query = DB::select('id','name','content')
  100. ->from('globalblocks')
  101. ->cached(60)
  102. ;
  103. $query->where('languages_id', '=', CURRENT_LANG_ID);
  104. $result = $query->execute();
  105. if ($result->count() == 0) return array();
  106. else
  107. {
  108. $return = array();
  109. foreach($result->as_array() as $value)
  110. $return[$value['name']] = $value['content'];
  111. return $return;
  112. }
  113. }
  114. static public function sitemap($lang_id = 1)
  115. {
  116. $res = DB::select(
  117. 'id',
  118. 'parent_id',
  119. array(DB::expr('0.8'), 'priority'),
  120. array('page_name', 'name'),
  121. array(DB::expr("CONCAT(parent_url, page_url)"), 'page_url')
  122. )
  123. ->from('content')
  124. ->where('status', '=', 1)
  125. ->and_where('languages_id', '=', $lang_id)
  126. ->and_where('visible_to', '=', 'all')
  127. ->and_where('hide_from_map', '!=', '1')
  128. ->execute()
  129. ->as_array('id');
  130. return $res;
  131. }
  132. /**
  133. * function generate html select from custom keys, fork of Form::select
  134. *
  135. * @param string $name
  136. * @param array $options
  137. * @param int,string $selected
  138. * @param array $attributes
  139. * @param int,string $valuekey
  140. * @param int,string $titleKey
  141. * @return htmlselect
  142. */
  143. static public function customSelect($name, array $options = NULL, $selected = NULL, array $attributes = NULL, $valuekey = NULL, $titleKey = NULL)
  144. {
  145. if( $valuekey || $titleKey)
  146. {
  147. $newoptions = array();
  148. foreach($options as $key=>$item)
  149. {
  150. if( is_array($titleKey) && count($titleKey) > 0 )
  151. {
  152. $val = '';
  153. foreach( $titleKey as $i ) $val .= array_key_exists($i, $item) ? $item[$i] : $i;
  154. $newoptions[$item[$valuekey]] = $val;
  155. }
  156. elseif(isset($valuekey) && !empty($valuekey) )
  157. $newoptions[$item[$valuekey]] = $item[$titleKey];
  158. else
  159. $newoptions[$key] = $item[$titleKey];
  160. }
  161. }
  162. else
  163. {
  164. $newoptions = $options;
  165. }
  166. return Form::select($name, $newoptions, $selected, $attributes);
  167. }
  168. /**
  169. * forked from Arr::get, add !empty value
  170. *
  171. * Retrieve a single key from an array. If the key does not exist in the
  172. * array, the default value will be returned instead.
  173. *
  174. * // Get the value "username" from $_POST, if it exists
  175. * $username = Arr::get($_POST, 'username');
  176. *
  177. * // Get the value "sorting" from $_GET, if it exists
  178. * $sorting = Arr::get($_GET, 'sorting');
  179. *
  180. * @param array array to extract from
  181. * @param string key name
  182. * @param mixed default value
  183. * @return mixed
  184. */
  185. public static function arrGet($array, $key, $default = NULL)
  186. {
  187. return isset($array[$key]) && !empty($array[$key]) ? $array[$key] : $default;
  188. }
  189. }