PageRenderTime 29ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/pirozek-yaps/core/class_theme.php

http://pirozek-yaps.googlecode.com/
PHP | 514 lines | 272 code | 64 blank | 178 comment | 36 complexity | 118d13b4b80e52153f3724e794d6bc32 MD5 | raw file
  1. <?php
  2. /**
  3. * Theme class
  4. * Very simple template system and theme loading
  5. * @version $Id$
  6. * @copyright 2010
  7. */
  8. class Theme
  9. {
  10. public $themes = Array();
  11. public $themes_path = Array();
  12. public $theme_name = '';
  13. public $render = true;
  14. private $root_path = '';
  15. private $index = '';
  16. private $theme_info = '';
  17. private $index_backup = ''; //for Theme::purge() function
  18. private $head = ''; //<head> for including JS and CSS and other stuff
  19. private $assign_array = Array(); //variables for assigning in template
  20. private $jquery_included = false; //prevent multiple jquery inclusion
  21. /**
  22. * Constructor
  23. */
  24. function __construct($theme = '')
  25. {
  26. global $core;
  27. $this->load_themes();
  28. if(strlen($theme) > 0) //we want theme
  29. {
  30. if(in_array($theme,$this->themes)) //we know it
  31. {
  32. $this->set_theme($theme); //set it
  33. }
  34. else
  35. {
  36. $core->err('Sorry, unknown theme selected!');
  37. }
  38. }
  39. else //use default theme
  40. {
  41. $value = mysql_fetch_array(mysql_query("SELECT value FROM ".DB_PREFIX."yaps_config WHERE name = 'default_theme'"));
  42. $this->set_theme($value['value']);
  43. }
  44. }
  45. /**
  46. * Theme::get_root_path()
  47. * Geter for root_path
  48. * @return
  49. */
  50. public function get_root_path()
  51. {
  52. return $this->root_path;
  53. }
  54. /**
  55. * Theme::get_content()
  56. * Useful when developer dont want to echo theme, just get content
  57. * @return
  58. */
  59. public function get_content()
  60. {
  61. $out = $this->index;
  62. foreach ($this->assign_array as $name => $value)
  63. {
  64. $out = str_ireplace('{'.$name.'}',$value,$out);
  65. }
  66. return $out;
  67. }
  68. /**
  69. * Theme::get_raw_content()
  70. * Returns raw, unprocessed content of theme, with no assigns
  71. * @return
  72. */
  73. public function get_raw_content()
  74. {
  75. return $this->index;
  76. }
  77. /**
  78. * Theme::set_defaults()
  79. * Sets default values for template, if they haven`t been set before by plugin
  80. * @return
  81. */
  82. public function set_defaults()
  83. {
  84. global $core;
  85. if(method_exists($core->used_plugin,'rss'))
  86. $this->add_to_head('<link rel="alternate" type="application/rss+xml" title="{title}" href="rss.php" />');
  87. //default values
  88. if(!$this->assign_exists('logo')) $this->assign('logo',PAGE_NAME);
  89. if(!$this->assign_exists('underlogo')) $this->assign('underlogo',PAGE_DESC);
  90. if(!$this->assign_exists('sidebar')) $this->assign('sidebar','');
  91. if(!$this->assign_exists('footer')) $this->assign('footer','Hi, I am footer:)');
  92. if(!$this->assign_exists('copyright')) $this->assign('copyright','Powered by Yaps!');
  93. if(!$this->assign_exists('head')) $this->assign('head', $this->head);
  94. if(!$this->assign_exists('title')) $this->assign('title',PAGE_NAME);
  95. }
  96. public function assign_exists($name)
  97. {
  98. return (strlen($this->assign_array[$name]) > 0) ? true : false;
  99. }
  100. /**
  101. * Theme::generate_menu()
  102. * As name suggests, this will generate menu from DB table
  103. * @return
  104. */
  105. public function generate_menu()
  106. {
  107. $query = mysql_query("SELECT * FROM ".DB_PREFIX."yaps_menu ORDER BY position DESC");
  108. $out = '<ul>';
  109. if(mysql_num_rows($query) > 0)
  110. {
  111. while($item = mysql_fetch_array($query))
  112. {
  113. $out .= '<li><a href="'.$item['link'].'">'.$item['name'].'</a></li>';
  114. }
  115. }
  116. else
  117. {
  118. $out .= '<li><a href=".">Home</a></li>';
  119. }
  120. $out .= '</ul>';
  121. $this->assign('menu',$out);
  122. }
  123. /**
  124. * Theme::set_theme()
  125. * This will set theme according to $theme
  126. * @param mixed $theme
  127. * @return
  128. */
  129. public function set_theme($theme)
  130. {
  131. //set up paths
  132. $this->root_path = $this->themes_path[$theme]['path'];
  133. $this->index = file_get_contents($this->root_path.'index.html');
  134. $this->theme_info = $this->themes_path[$theme]['info'];
  135. $this->theme_name = $theme;
  136. //and set root_path
  137. $this->assign('root_path',WEB_URL.$this->root_path);
  138. //backup index
  139. $this->index_backup = $this->index;
  140. }
  141. /**
  142. * Theme::set_theme_file()
  143. * Set up theme as a specified file. Usable for blogpost, gallery etc
  144. * @param mixed $path
  145. * @param mixed $file
  146. * @return
  147. */
  148. public function set_theme_file($path = '',$file)
  149. {
  150. if(strlen($path) < 1) $path = $this->root_path; //default path to current plugin
  151. //set up paths
  152. $this->root_path = $path;
  153. $this->index = file_get_contents($this->root_path.$file);
  154. $this->theme_info = 'File: '.$file;
  155. //and set root_path
  156. $this->assign('root_path',WEB_URL.$this->root_path);
  157. //backup index
  158. $this->index_backup = $this->index;
  159. }
  160. /**
  161. * Theme::purge()
  162. * Purge theme content by loading new one from backup
  163. * This file save file_get_contents usage (HD is sooo slow)
  164. * @return
  165. */
  166. public function purge()
  167. {
  168. $this->index = $this->index_backup;
  169. }
  170. /**
  171. * Theme::render()
  172. * Renders loaded theme
  173. * @return
  174. */
  175. public function render()
  176. {
  177. global $core;
  178. if(strlen($this->index) > 0)
  179. {
  180. foreach ($this->assign_array as $name => $value)
  181. {
  182. $this->index = str_ireplace('{'.$name.'}',$value,$this->index);
  183. }
  184. echo $this->index;
  185. }
  186. else
  187. {
  188. $core->err('You have no theme selected or this theme has corrupted index.html');
  189. }
  190. }
  191. /**
  192. * Theme::assign()
  193. * Replace template tag {$name} with $value
  194. * @param mixed $name
  195. * @param mixed $value
  196. * @param mixed $append is set to zero, if it is true, than assign will append to assign array instead of replacing the value
  197. * @return
  198. */
  199. public function assign($name,$value,$append = 0)
  200. {
  201. //$this->index = str_ireplace('{'.$name.'}',$value,$this->index);
  202. if(!$append)
  203. {
  204. $this->assign_array[$name] = $value;
  205. }
  206. else
  207. {
  208. $this->assign_array[$name] .= $value;
  209. }
  210. }
  211. /**
  212. * Theme::load_themes()
  213. * Load all themes that we have in themes directory
  214. * @return
  215. */
  216. private function load_themes()
  217. {
  218. if($handle = opendir(THEMES_DIR)) //lets try to open the themes dir
  219. {
  220. while(false !== ($file = readdir($handle)))
  221. {
  222. if($file != '.' && $file != '..' && $file != '.svn') //we don`t want this
  223. {
  224. $this->themes[] = strtolower($file);
  225. }
  226. }
  227. closedir($handle); //close dir handler
  228. //loop through themes and read their info
  229. for($i = 0;$i < sizeof($this->themes);$i++)
  230. {
  231. $path = THEMES_DIR.$this->themes[$i].'/';
  232. $index = $path.'index.html';
  233. $theme_info = $path.FILE_THEME_INFO;
  234. //add all paths to array, we will use em later
  235. $this->themes_path[$this->themes[$i]] = Array(
  236. 'path' => $path,
  237. 'index' => $index,
  238. 'info' => $theme_info);
  239. }
  240. }
  241. else
  242. {
  243. $this->err('Can`t open themes directory. Chmod maybe?');
  244. }
  245. }
  246. /**
  247. * Theme::add_to_head()
  248. * Useful for including javascript libraries and other stuff
  249. * @param mixed $what
  250. * @return
  251. */
  252. public function add_to_head($what)
  253. {
  254. $this->head .= $what;
  255. }
  256. /**
  257. * Theme::include_jquery()
  258. * Include popular javascript framework
  259. * @param mixed $css
  260. * @return
  261. */
  262. public function include_jquery($css = true)
  263. {
  264. if(!$this->jquery_included)
  265. {
  266. $this->add_to_head('<script type="text/javascript" src="'.JS_DIR.'jquery-1.4.2.min.js"></script>
  267. <script type="text/javascript" src="'.JS_DIR.'jquery-ui-1.8.2.custom.min.js"></script>');
  268. if($css)
  269. {
  270. $this->add_to_head('<link type="text/css" href="'.JS_DIR.'ui-lightness/jquery-ui-1.8.2.custom.css" rel="stylesheet" />');
  271. }
  272. $this->jquery_included = true;
  273. }
  274. }
  275. /**
  276. * Theme::js()
  277. * Shortcut for adding javascript
  278. * @param mixed $text
  279. * @return
  280. */
  281. public function js($text,$jquery_wrapper = true)
  282. {
  283. //see if we can add jquery onLoad wrapper
  284. if($jquery_wrapper)
  285. {
  286. $text = '$(function(){'.$text.'});';
  287. }
  288. $this->add_to_head('<script type="text/javascript">'.$text.'</script>
  289. ');
  290. }
  291. /**
  292. * Theme::css()
  293. * Shortcut for adding infile css
  294. * @param mixed $text
  295. * @return
  296. */
  297. public function css($text)
  298. {
  299. $this->add_to_head('<style>'.$text.'</style>');
  300. }
  301. /**
  302. * Theme::include_css()
  303. * Shortcut for including CSS from external file
  304. * @param mixed $path
  305. * @return
  306. */
  307. public function include_css($path)
  308. {
  309. $this->add_to_head('<link href="'.$path.'" rel="stylesheet" type="text/css" media="screen" />');
  310. }
  311. /**
  312. * Theme::include_js()
  313. * Shortcut for including JS from external file
  314. * @param mixed $path
  315. * @return
  316. */
  317. public function include_js($path)
  318. {
  319. $this->add_to_head('<script type="text/javascript" src="'.$path.'"></script>');
  320. }
  321. /**
  322. * Theme::include_validation()
  323. * Include validation plugin for jQuery
  324. * @return
  325. */
  326. public function include_validation()
  327. {
  328. $this->include_jquery();
  329. $this->add_to_head('<script type="text/javascript" src="'.JS_DIR.'jquery.validate.pack.js"></script>');
  330. }
  331. /**
  332. * Theme::include_tooltip()
  333. * Include tooltip plugin for jQuery
  334. * @return
  335. */
  336. public function include_tooltip()
  337. {
  338. $this->include_jquery();
  339. $this->add_to_head('<script type="text/javascript" src="'.JS_DIR.'jquery.tipsy.js"></script>');
  340. $this->include_css(JS_DIR.'tipsy.css');
  341. $this->js('$("img[title]").not(".buttons img").tipsy({fade: true});
  342. $("a[title]").tipsy({fade: true});');
  343. }
  344. /**
  345. * Theme::icon()
  346. * Shortcut for icon from template
  347. * @param mixed $name
  348. * @param string $alt
  349. * @param bool $return
  350. * @return
  351. */
  352. public function icon($name,$alt = '',$return = false)
  353. {
  354. if($return) return '<img src="images/icons/'.$name.'" alt="'.$alt.'" title="'.$alt.'" class="yaps_icon"/>';
  355. else echo '<img src="images/icons/',$name,'" alt="',$alt,'" title="',$alt,'" class="yaps_icon"/>';
  356. }
  357. /**
  358. * Theme::set_title()
  359. * Shortcut for webpage title
  360. * @param mixed $title
  361. * @return
  362. */
  363. public function set_title($title)
  364. {
  365. $this->assign('title',PAGE_NAME.' - '.$title);
  366. }
  367. /**
  368. * Theme::do_other_plugins_stuff()
  369. * Do other plugins things, like display twitter plugin, etc.
  370. * @return
  371. */
  372. public function do_other_plugins_stuff()
  373. {
  374. global $core;
  375. include $this->theme_info;
  376. if($twitter == true && $core->is_plugin('twitter')) //does this theme supports twitter?
  377. {
  378. $plugin = $core->load_plugin('twitter');
  379. $plugin->display_tweets();
  380. }
  381. if($core->is_plugin('sidebar')) //do we have sidebar plugin?
  382. {
  383. $plugin = $core->load_plugin('sidebar');
  384. $plugin->generate_sidebar();
  385. }
  386. }
  387. /**
  388. * Theme::get_theme_list()
  389. * Just for better reading of code
  390. * @return
  391. */
  392. public function get_theme_list()
  393. {
  394. return $this->themes;
  395. }
  396. public function link($text,$plugin,$action = '',$id = '',$seo_stuff = '')
  397. {
  398. global $core;
  399. if($action) $rest_of_it .= '-'.$action;
  400. if($id) $rest_of_it .= '-'.$id;
  401. if($seo_stuff) $rest_of_it .= '-'.$core->encode_url($seo_stuff);
  402. return '<a href="'.WEB_URL.$plugin.$rest_of_it.'" id="'.$plugin.'_'.$action.'">'.$text.'</a>';
  403. }
  404. /**
  405. * Theme::button()
  406. * Create nice button :)
  407. * @param mixed $text Text on button
  408. * @param mixed $link
  409. * @param string $image Standard Yaps! icon
  410. * @param string $class What class should we add to link?
  411. * @param mixed $return Should we return(true) or echo(false). Defaul is echo
  412. * @return
  413. */
  414. public function button($text,$link,$image = '',$class = '', $return = false)
  415. {
  416. $link_string = '<a href="'.$link.'" class="buttons '.$class.'">'.(($image) ? $this->icon($image,$text,true) : '').$text.'</a>';
  417. if($return) return $link_string;
  418. echo $link_string;
  419. }
  420. /**
  421. * Theme::back_button()
  422. * Go back in history with no JS
  423. * @param string $text
  424. * @return
  425. */
  426. public function back_button($text = 'ZpÄ&#x203A;t')
  427. {
  428. global $core;
  429. echo '<br />';
  430. $this->button($text,$core->load_last_page(),'arrow_left.png');
  431. }
  432. /**
  433. * Theme::yesno()
  434. * Create simple yes/no dialog
  435. * @param mixed $answer Text of answer
  436. * @param mixed $yes_link Where to redirect if YES
  437. * @param mixed $no_link Where to redirect if NO
  438. * @param string $yes_text Yes button text
  439. * @param string $no_msg No button text
  440. * @return
  441. */
  442. public function yesno($answer,$yes_link,$no_link,$yes_text = 'Yes',$no_text = 'No')
  443. {
  444. echo '<div><div class="yesno_answer">',$answer,'</div>';
  445. $this->button($yes_text,$yes_link,'accept.png','positive');
  446. $this->button($no_text,$no_link,'cross.png','negative');
  447. echo '</div>';
  448. }
  449. }
  450. ?>