/wt/classes/mmcontroller.php

https://github.com/vmen015/Victor · PHP · 154 lines · 123 code · 26 blank · 5 comment · 34 complexity · 4e7441ee531b8416f9720502813ebd34 MD5 · raw file

  1. <?php
  2. class mmcontroller extends controller {
  3. public $template = '_default';
  4. public $data = array();
  5. public $autorender = false;
  6. public $config;
  7. public $cache;
  8. public $cache_refresh = false;
  9. public $session;
  10. public $me = array();
  11. public $loggedin = false;
  12. public function __construct($config=null) {
  13. parent::__construct($config);
  14. if ($config) {
  15. $this->config = $config;
  16. $this->data['config'] = $config;
  17. if (isset($this->config['assets']) && is_array($this->config['assets'])) {
  18. $this->_loadAssets();
  19. }
  20. if (isset($this->config['metatags']) && is_array($this->config['metatags'])) {
  21. $this->_loadMetatags();
  22. }
  23. $this->session = session::instance($this->config);
  24. }
  25. $this->_loadHelpers();
  26. $this->cache = new Cache($this->config['cache']['path']);
  27. if (isset($_GET) && isset($_GET['refreshall'])) {
  28. $this->_emptyCache();
  29. }
  30. if (isset($_GET) && isset($_GET['refresh'])) {
  31. $this->cache_refresh = true;
  32. }
  33. }
  34. public function before() {
  35. // echo 'before';
  36. $this->data['controller'] = '';
  37. $this->data['page'] = '';
  38. $this->data['scripts_custom'] = array();
  39. $this->data['errors'] = array();
  40. if ($this->session->get('loggedin',false)) {
  41. $this->loggedin = $this->session->get('loggedin');
  42. }
  43. $this->data['loggedin'] = $this->loggedin;
  44. $this->data['menu_links'] = $this->page_model->get_menu();
  45. }
  46. public function after() {
  47. $this->data['controller'] = get_class($this);
  48. // don't supply full config to js (security)
  49. $this->data['js_runtime'] = array(
  50. 'controller' => $this->data['controller']
  51. , 'page' => $this->data['page']
  52. , 'site_url' => SITE_URL
  53. , 'site_dir' => SITE_DIR
  54. );
  55. $this->data['scripts_custom'][] = '<script id="jsRuntime" type="text/javascript" charset="UTF-8"> mm.runtime='.json_encode($this->data['js_runtime']).'; </script>';
  56. $this->render();
  57. }
  58. public function render() {
  59. $this->data['render_time'] = microtime(true) - START_TIME;
  60. if (isset($this->config['outline']) && isset($this->config['outline']['integration']) && $this->config['outline']['integration']) {
  61. $this->_outlineView('views/'.$this->template, $this->data);
  62. } else {
  63. $this->view($this->template, $this->data);
  64. }
  65. }
  66. // function used to compile the view with outline template syntax
  67. protected function _outlineView($template, $data) {
  68. mmoutline::init();
  69. $o = new mmoutline($template, $data);
  70. $o->render();
  71. }
  72. public function _loadHelpers() {
  73. // function load_class($class=null, $params=null, $path='models', $instantiate = TRUE) {
  74. load_class('Arr', null, 'libraries/helpers', false);
  75. load_class('Cache', null, 'libraries/cache', false);
  76. load_class('file', null, 'libraries/helpers', false);
  77. load_class('str', null, 'libraries/helpers', false);
  78. load_class('Validate', null, 'libraries/helpers', false);
  79. }
  80. public function _loadAssets() {
  81. if (isset($this->config['assets']['styles']) && is_array($this->config['assets']['styles'])) {
  82. $this->data['styles'] = array();
  83. foreach($this->config['assets']['styles'] as $key => $style) {
  84. if (strpos($style, 'http://') !== false || strpos($style, 'https://') !== false || strpos($style, '://') !== false) {
  85. $this->data['styles'][] = $style;
  86. } else {
  87. $this->data['styles'][] = SITE_URL.$style;
  88. }
  89. }
  90. }
  91. if (isset($this->config['assets']['scripts']) && is_array($this->config['assets']['scripts'])) {
  92. $this->data['scripts'] = array();
  93. foreach($this->config['assets']['scripts'] as $key => $script) {
  94. if (strpos($script, 'http://') !== false || strpos($script, 'https://') !== false || strpos($script, '://') !== false) {
  95. $this->data['scripts'][] = $script;
  96. } else {
  97. $this->data['scripts'][] = SITE_URL.$script;
  98. }
  99. }
  100. }
  101. }
  102. public function _loadMetatags() {
  103. if (isset($this->config['metatags']) && is_array($this->config['metatags']) && !empty($this->config['metatags'])) {
  104. $this->data['metatags'] = $this->config['metatags'];
  105. }
  106. }
  107. public function _emptyCache($key = null) {
  108. $this->cache->clearall();
  109. }
  110. public function _getYahooBlogsFeed($which) {
  111. if (isset($this->config['yahooblogfeeds'][$which]) && !empty($this->config['yahooblogfeeds'][$which])) {
  112. $feed = $this->cache->get('yahooblogsfeed_'.$which, $this->config['cache']['blogsfeed_expiration']);
  113. if ($this->cache_refresh || $feed === false) {
  114. $ch = curl_init();
  115. // curl_setopt($ch, CURLOPT_URL, "http://pipes.yahoo.com/pipes/pipe.run?_id=0c92b34341bb61e94f06e4cf8839537c&_render=php");
  116. curl_setopt($ch, CURLOPT_URL, $this->config['yahooblogfeeds'][$which]);
  117. curl_setopt($ch, CURLOPT_HEADER, 0);
  118. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  119. $feed = curl_exec($ch);
  120. curl_close($ch);
  121. $this->cache->set('yahooblogsfeed_'.$which, $feed);
  122. }
  123. $this->data['blogfeed'] = unserialize($feed);
  124. } else {
  125. $this->data['blogfeed'] = array();
  126. }
  127. }
  128. }