PageRenderTime 57ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/www/application/modules/admin/components.php

https://github.com/kelios/imshop
PHP | 354 lines | 247 code | 84 blank | 23 comment | 38 complexity | 6f4264d45569c69eb9dfdd89cb671a21 MD5 | raw file
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Image CMS
  4. * Components Class
  5. */
  6. class Components extends MY_Controller{
  7. function __construct()
  8. {
  9. parent::__construct();
  10. $this->load->library('DX_Auth');
  11. admin_or_redirect();
  12. $this->load->library('lib_admin');
  13. $this->lib_admin->init_settings();
  14. }
  15. function index()
  16. {
  17. // index
  18. }
  19. function modules_table()
  20. {
  21. $not_installed = array();
  22. $fs_modules = $this->find_components();
  23. $db_modules = $this->db->get('components')->result_array();
  24. // Find not installed modules
  25. $count = count($fs_modules);
  26. for ($i = 0; $i < $count; $i++)
  27. {
  28. if ( $this->is_installed($fs_modules[$i]['com_name']) == 0 )
  29. {
  30. $info = $this->get_module_info( $fs_modules[$i]['com_name'] );
  31. $fs_modules[$i]['name'] = $info['menu_name'];
  32. $fs_modules[$i]['version'] = $info['version'];
  33. $fs_modules[$i]['description'] = $info['description'];
  34. array_push($not_installed,$fs_modules[$i]);
  35. }
  36. }
  37. // process modules info
  38. $count = count($db_modules);
  39. for ($i = 0; $i < $count; $i++)
  40. {
  41. $info = $this->get_module_info( $db_modules[$i]['name'] );
  42. $db_modules[$i]['menu_name'] = $info['menu_name'];
  43. $db_modules[$i]['version'] = $info['version'];
  44. $db_modules[$i]['description'] = $info['description'];
  45. $db_modules[$i]['identif'] = anchor($db_modules[$i]['identif'],$db_modules[$i]['identif']);
  46. if ( file_exists(APPPATH.'modules/'.$db_modules[$i]['name'].'/admin.php') )
  47. {
  48. $db_modules[$i]['admin_file'] = 1;
  49. }else{
  50. $db_modules[$i]['admin_file'] = 0;
  51. }
  52. }
  53. $this->template->assign('installed',$db_modules);
  54. $this->template->assign('not_installed',$not_installed);
  55. $this->template->show('module_table',FALSE);
  56. }
  57. function is_installed($mod_name)
  58. {
  59. return $this->db->get_where('components',array('name' => $mod_name),1)->num_rows();
  60. }
  61. function install($module = '')
  62. {
  63. cp_check_perm('module_install');
  64. $module = strtolower($module);
  65. ($hook = get_hook('admin_install_module')) ? eval($hook) : NULL;
  66. if ( file_exists(APPPATH.'modules/'.$module.'/'.$module.'.php') AND $this->is_installed($module) == 0 )
  67. {
  68. // Make module install
  69. $data = array(
  70. 'name' => $module,
  71. 'identif' => $module
  72. );
  73. $this->db->insert('components',$data);
  74. $this->load->module($module);
  75. if ( method_exists($module, '_install') === TRUE )
  76. {
  77. $this->$module->_install();
  78. }
  79. // Update hooks
  80. $this->load->library('cms_hooks');
  81. $this->cms_hooks->build_hooks();
  82. $this->lib_admin->log('Установил модуль '.$data['name']);
  83. //showMessage('Модуль Установлен');
  84. return TRUE;
  85. }
  86. else
  87. {
  88. //showMessage('Ошибка установки модуля.');
  89. return FALSE;
  90. }
  91. }
  92. function deinstall($module = '')
  93. {
  94. cp_check_perm('module_deinstall');
  95. $module = strtolower($module);
  96. ($hook = get_hook('admin_deinstall_module')) ? eval($hook) : NULL;
  97. if ( file_exists(APPPATH.'modules/'.$module.'/'.$module.'.php') AND $this->is_installed($module) == 1 )
  98. {
  99. $this->load->module($module);
  100. if ( method_exists($module, '_deinstall') === TRUE )
  101. {
  102. $this->$module->_deinstall();
  103. }
  104. $this->db->limit(1);
  105. $this->db->delete('components',array('name' => $module));
  106. $this->lib_admin->log('Удалил модуль '.$module);
  107. }
  108. else
  109. {
  110. showMessage('Ошибка удаления модуля.');
  111. }
  112. // Update hooks
  113. $this->load->library('cms_hooks');
  114. $this->cms_hooks->build_hooks();
  115. $this->modules_table();
  116. }
  117. function find_components($in_menu = FALSE)
  118. {
  119. $components = array();
  120. if ($in_menu == TRUE)
  121. {
  122. $this->db->where('in_menu', 1);
  123. }
  124. $installed = $this->db->get('components')->result_array();
  125. if ($com_path = opendir(APPPATH.'modules/')) {
  126. while (false !== ($file = readdir($com_path))) {
  127. if ($file != "." && $file != ".." && $file != "index.html" && !is_file($file) )
  128. {
  129. $info_file = APPPATH.'modules/'.$file.'/module_info.php';
  130. $com_file_admin = APPPATH.'modules/'.$file.'/admin.php';
  131. if(file_exists($info_file))
  132. {
  133. include ($info_file);
  134. if(file_exists($com_file_admin))
  135. $admin_file = 1;
  136. else
  137. $admin_file = 0;
  138. $ins = FALSE;
  139. foreach($installed as $k)
  140. {
  141. if ($k['name'] == $file)
  142. {
  143. $ins = TRUE;
  144. }
  145. }
  146. $new_com = array(
  147. 'menu_name' => $com_info['menu_name'],
  148. 'com_name' => $file,
  149. 'admin_file' => $admin_file,
  150. 'installed' => $ins
  151. );
  152. array_push($components, $new_com);
  153. }
  154. }
  155. }
  156. closedir($com_path);
  157. }
  158. return $components;
  159. }
  160. function component_settings($component)
  161. {
  162. cp_check_perm('module_admin');
  163. $this->db->where('name', $component);
  164. $query = $this->db->get('components', 1);
  165. if ($query->num_rows() == 1)
  166. {
  167. $com = $query->row_array();
  168. $this->template->add_array($com);
  169. }else{
  170. $this->template->assign('com_name', $component);
  171. $this->template->assign('identif', $component);
  172. $this->template->assign('status', 0);
  173. }
  174. $this->template->show('component_settings', FALSE);
  175. }
  176. // Save component settings
  177. function save_settings($component)
  178. {
  179. cp_check_perm('module_admin');
  180. $this->db->where('name', $component);
  181. $query = $this->db->get('components',1);
  182. $com = $query->row_array();
  183. ($hook = get_hook('admin_component_save_settings')) ? eval($hook) : NULL;
  184. if ($query->num_rows() >= 1)
  185. {
  186. $data = array(
  187. 'enabled' => (int)$this->input->post('status'),
  188. //'identif' => $this->input->post('identif'),
  189. 'identif' => $com['name'],
  190. 'autoload' => (int)$this->input->post('autoload'),
  191. 'in_menu' => (int)$this->input->post('in_menu')
  192. );
  193. $this->db->where('name',$component);
  194. $this->db->update('components',$data);
  195. $this->lib_admin->log('Изменил настройки модуля '.$com['name']);
  196. //showMessage('Настройки сохранены');
  197. }else{
  198. // Error, module not found
  199. }
  200. jsCode("ajax_div('modules_table',base_url + 'admin/components/modules_table/');");
  201. }
  202. // Load component admin class in iframe/xhr
  203. function init_window()
  204. {
  205. // buildWindow($id,$title,$contentURL,$width,$height,$method = 'iframe')
  206. $module = $this->input->post('component');
  207. $info_file = realpath(APPPATH.'modules/'.$module).'/module_info.php';
  208. if(file_exists($info_file))
  209. {
  210. include_once ($info_file);
  211. switch($com_info['admin_type'])
  212. {
  213. case 'window':
  214. buildWindow($module.'_window','Модуль: '.$com_info['menu_name'],site_url('admin/components/cp/'.$module),$com_info['w'],$com_info['h'],$com_info['window_type']);
  215. break;
  216. case 'inside':
  217. updateDiv('page',site_url('admin/components/cp/'.$module));
  218. break;
  219. }
  220. }
  221. }
  222. function cp($module)
  223. {
  224. $func = $this->uri->segment(5);
  225. if($func == FALSE) $func = 'index';
  226. ($hook = get_hook('admin_run_module_panel')) ? eval($hook) : NULL;
  227. $this->load->module('core/core');
  228. $args = $this->core->grab_variables(6);
  229. $this->template->assign('SELF_URL',site_url('admin/components/cp/'.$module));
  230. echo '<div id="'.$module.'_module_block">'.modules::run($module.'/admin/'.$func,$args).'</div>';
  231. //ajax_links($module);
  232. }
  233. /**
  234. * Run module
  235. */
  236. function run($module)
  237. {
  238. $func = $this->uri->segment(5);
  239. if($func == FALSE) $func = 'index';
  240. ($hook = get_hook('admin_run_module_admin')) ? eval($hook) : NULL;
  241. $this->load->module('core/core');
  242. $args = $this->core->grab_variables(6);
  243. $this->template->assign('SELF_URL',site_url('admin/components/cp/'.$module));
  244. echo modules::run($module.'/admin/'.$func, $args);
  245. }
  246. function com_info()
  247. {
  248. $com_info = $this->get_module_info($this->input->post('component'));
  249. if($com_info != FALSE)
  250. {
  251. $info_text = '<h1>'.$com_info['menu_name'].'</h1><p>'.$com_info['description'].'</p><p><b>Автор:</b> '.$com_info['author'].'<br/><b>Версия:</b> '.$com_info['version'].'</p>';
  252. jsCode("alertBox.info('".$info_text."');");
  253. }else{
  254. showMessage('Can\'t load module info file');
  255. }
  256. }
  257. function get_module_info($mod_name)
  258. {
  259. ($hook = get_hook('admin_get_module_info')) ? eval($hook) : NULL;
  260. $info_file = realpath(APPPATH.'modules/'.$mod_name).'/module_info.php';
  261. if(file_exists($info_file))
  262. {
  263. include ($info_file);
  264. return $com_info;
  265. }else{
  266. return FALSE;
  267. }
  268. }
  269. }
  270. /* End of components.php */