PageRenderTime 22ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/controllers/module.controller.php

https://github.com/mconway/NanobyteCMS
PHP | 244 lines | 209 code | 21 blank | 14 comment | 27 complexity | 81e4f32324fd8c60ed1a4ed5979f5a8f MD5 | raw file
  1. <?php
  2. /*
  3. *Copyright (c) 2009, Michael Conway
  4. *All rights reserved.
  5. *Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  6. *Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  7. *Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  8. *Neither the name of the Nanobyte CMS nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
  9. *THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  10. */
  11. class ModuleController extends BaseController{
  12. public static function admin(){
  13. $Core = parent::getCore();
  14. $tabs = array();
  15. $content = '';
  16. array_push($tabs, Core::l('Modules','admin/module/list'));
  17. array_push($tabs, Core::l('Blocks','admin/block/list'));
  18. $Core->smarty->assign('tabs',$tabs);
  19. if($Core->ajax){$Core->json_obj->tabs = $Core->smarty->fetch('tabs.tpl');}
  20. if(isset($Core->args[1])){
  21. switch($Core->args[1]){
  22. //We call the same function for Disable and Enable.
  23. case 'enable':
  24. case 'disable':
  25. self::UpdateStatus($Core->args,$Core->json_obj);
  26. break;
  27. // Default is to display the module list
  28. case 'list':
  29. $func = 'List'.$Core->args[0];
  30. $Core->smarty->assign(self::$func($Core->args[1]));
  31. $content = $Core->smarty->fetch('list.tpl');
  32. break;
  33. case 'details':
  34. $module = new Module($Core->args[2]);
  35. $content = <<<EOF
  36. Author: {$module->conf->author}<br/>
  37. URL: {$module->conf->author->attributes()->url}<br/>
  38. Email: {$module->conf->author->attributes()->email}<br/>
  39. EOF;
  40. $Core->json_obj->callback = 'Dialog';
  41. $Core->json_obj->title = 'Module Information for: '.ucfirst($Core->args[2]);
  42. break;
  43. case 'down':
  44. case 'up':
  45. //$Core->args[1] = up/down $Core->args[2] = id $Core->args[3] = weight
  46. $module = new Module();
  47. if($module->moveBlock($Core->args)){
  48. $Core->json_obj->callback = 'nanobyte.moveRow';
  49. $Core->json_obj->args = 'block_'.$Core->args[2].'|'.$Core->args[1];
  50. }
  51. }
  52. $Core->json_obj->content = $content;
  53. }
  54. }
  55. public static function getAll(){
  56. $dirs = glob('modules/*', GLOB_ONLYDIR);
  57. foreach($dirs as $dir){
  58. $mod[] = new Module($dir);
  59. }
  60. return $mod;
  61. }
  62. public static function getBlocks($filter=null){
  63. $Core = parent::getCore();
  64. $enabled = Module::GetBlocks($filter);
  65. foreach($enabled as $block){
  66. $position = explode("_",$block['position']);
  67. $module = 'Mod_'.$block['providedby'];
  68. $tmp = new $module;
  69. $blockobj = call_user_func(array('Mod_'.$block['providedby'], $block['name'].'_Block'));
  70. $Core->smarty->assign(array(
  71. 'block_title' => $blockobj->title,
  72. 'block_body' => $Core->smarty->fetch($blockobj->template)
  73. ));
  74. if (isset($blocks[$position[0]])){
  75. $blocks[$position[0]] .= $Core->smarty->fetch('block.tpl');
  76. }else{
  77. $blocks[$position[0]] = $Core->smarty->fetch('block.tpl');
  78. }
  79. }
  80. $Core->smarty->assign('blocks', $blocks);
  81. }
  82. public static function installModule(&$module){
  83. $Core = parent::getCore();
  84. require_once($module->modpath."mod_".$module->name.'.php');
  85. $mod_class = 'Mod_'.$module->name;
  86. $m = new $mod_class();
  87. $m->install();
  88. if(isset($m->setup['menus'])){
  89. if(!isset($m->setup['menus']['menu'])){
  90. foreach($m->setup['menus'] as $menu){
  91. $menu_obj = new Menu($menu['menu']);
  92. $menu_obj->data = array($menu);
  93. $menu_obj->commit($menu_obj->menu[0]->mid,true);
  94. }
  95. }else{
  96. $menu_obj = new Menu($m->setup['menus']['menu']);
  97. $menu_obj->data = array($m->setup['menus']);
  98. $menu_obj->commit($menu_obj->menu[0]->mid,true);
  99. }
  100. }
  101. if(isset($m->setup['permissions'])){
  102. $perms = new Perms();
  103. $perms->createPerm($m->setup['permissions'],$module->name);
  104. }
  105. if(isset($m->setup['tables'])){
  106. $Core->createTable($m->setup['tables']);
  107. }
  108. if(isset($m->setup['folders'])){
  109. $module->createFolders($m->setup['folders']);
  110. }
  111. if(isset($m->setup['category_types'])){
  112. foreach($m->setup['category_types'] as $type){
  113. $Core->addCategoryType($type);
  114. }
  115. }
  116. }
  117. public static function listBlock(){
  118. $Core = parent::getCore();
  119. $blocks = Module::GetBlocks();
  120. $options['image'] = '16';
  121. $options['class'] = 'action-link noloader';
  122. foreach($blocks as &$block){
  123. $options['id'] = 'a_block_'.$block['id'];
  124. $s = $block['status'] == 1 ? 'Disable' : 'Enable';
  125. $options['title'] = $s;
  126. $block['actions'] = Core::l($s,'admin/block/'.strtolower($s).'/'.$block['id'],$options)
  127. ." | ".Core::l('Up','admin/block/up/'.$block['id']."/".($block['weight']-1) ,array_merge($options,array('id'=>'','title'=>'Move up')))
  128. ." | ".Core::l('Down','admin/block/down/'.$block['id']."/".($block['weight']+1) ,array_merge($options,array('id'=>'','title'=>'Move down')));
  129. $block['status'] = "<center><img src='".THEME_PATH."/images/{$block['status']}-25.png'/></center>";
  130. }
  131. $Core->smartyVars = array(
  132. 'list'=>$blocks,
  133. 'formAction'=>'admin/blocks',
  134. 'tableclass' => 'sortable'
  135. );
  136. return $Core->smartyVars;
  137. }
  138. public static function listModule($page){
  139. //create list
  140. $modsList = self::GetAll(); //array of objects
  141. $list = array();
  142. foreach ($modsList as $module){
  143. $s = $module->status == 1 ? 'Disable' : 'Enable';
  144. $options = array(
  145. 'image' => '16',
  146. 'class' => 'action-link noloader',
  147. 'id' => 'mod_'.$module->name,
  148. 'title' => $s
  149. );
  150. $list[] = array(
  151. 'title'=>$module->conf->title,
  152. 'version'=>$module->conf->version.'-'.$module->conf->status,
  153. 'description' => $module->conf->description,
  154. 'enabled'=>"<center><img src='".THEME_PATH."/images/{$module->status}-25.png'/></center>",
  155. 'actions'=> Core::l($s,'admin/module/'.strtolower($s).'/'.$module->name,$options).' | '
  156. );
  157. $list[count($list)-1]['actions'] .= Core::l('Info','admin/module/details/'.$module->name,array('id'=>'','title'=>'Info', 'class'=>'action-link','image'=>'16'));
  158. }
  159. $smarty_vars = array(
  160. 'pager'=>parent::Paginate(LIMIT, count($modsList), 'admin/module/list/', $page),
  161. 'list'=>$list,
  162. );
  163. return $smarty_vars;
  164. }
  165. public static function uninstallModule(&$module){
  166. $Core = parent::getCore();
  167. $module->disableBlocks();
  168. $mod_class = 'Mod_'.$module->name;
  169. $m = new $mod_class();
  170. if(isset($m->setup['menus'])){
  171. if(!isset($m->setup['menus']['menu'])){
  172. foreach($m->setup['menus'] as $menu){
  173. $menu_obj = new Menu($menu['menu']);
  174. foreach($menu_obj->menu as $tmp){
  175. if ($tmp->linkpath==$menu['linkpath'] && $tmp->linktext==$menu['linktext']){
  176. Admin::deleteObject('menu_links','id',$tmp->id);
  177. }
  178. }
  179. }
  180. }else{
  181. $menu_obj = new Menu($m->setup['menus']['menu']);
  182. foreach($menu_obj->menu as $tmp){
  183. if ($tmp->linkpath==$m->setup['menus']['linkpath'] && $tmp->linktext==$m->setup['menus']['linktext']){
  184. Admin::deleteObject('menu_links','id',$tmp->id);
  185. }
  186. }
  187. }
  188. }
  189. if(isset($m->setup['permissions'])){
  190. Admin::deleteObject('perms','category',$module->name);
  191. }
  192. $m->uninstall();
  193. }
  194. public static function updateStatus(){
  195. $Core = parent::getCore();
  196. if($Core->args[0]=='block'){
  197. $module = new Module();
  198. if($module->updateBlockStatus($Core->args[2])){
  199. $Core->json_obj->callback = 'nanobyte.changeLink';
  200. $str = $Core->args[1] == 'enable' ? 'disable' : 'enable';
  201. $Core->json_obj->args = $Core->args[1]."|".$str."|a_block_".$Core->args[2]."|status";
  202. }
  203. }else{
  204. $module = new Module($Core->args[2]);
  205. if($module->Commit()){
  206. $Core->enabledMods();
  207. $Core->SetMessage(strtoupper($module->name). ' has been '.$Core->args[1].'d.','info');
  208. if ($Core->args[1] == "enable"){
  209. self::InstallModule($module);
  210. }else{
  211. self::UninstallModule($module);
  212. call_user_func(array('Mod_'.$module->name, 'Uninstall'));
  213. }
  214. $Core->json_obj->callback = 'nanobyte.changeLink';
  215. $str = $Core->args[1] == 'enable' ? 'disable' : 'enable';
  216. $Core->json_obj->args = $Core->args[1]."|".$str."|mod_".$module->name."|enabled";
  217. }else{
  218. $Core->SetMessage("An Error was encountered while trying to {$Core->args[1]}".strtoupper($module->name),'error');
  219. }
  220. }
  221. // UserController::Redirect();
  222. }
  223. }