/_examples/mvc2/application/modules/skeleton/controllers/skeletoncontroller.php

https://bitbucket.org/tumivn/phpexamples · PHP · 129 lines · 86 code · 20 blank · 23 comment · 4 complexity · e4ceb36e715137d204c4113bb354aa89 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the skeleton controller
  4. *
  5. * @package Seven Kevins
  6. * @copyright Copyright (C) 2009 PHPRO.ORG. All rights reserved.
  7. *
  8. */
  9. namespace sevenkevins;
  10. class skeletonController extends baseController implements IController
  11. {
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. // a new config
  16. $config = config::getInstance();
  17. $this->view->version = $config->config_values['application']['version'];
  18. }
  19. public function index()
  20. {
  21. /*** a new view instance ***/
  22. $tpl = new view;
  23. /*** turn caching on for this page ***/
  24. $tpl->setCaching(true);
  25. /*** set the template dir ***/
  26. $tpl->setTemplateDir(APP_PATH . '/modules/skeleton/views');
  27. /*** the include template ***/
  28. $tpl->include_tpl = APP_PATH . '/views/skeleton/index.phtml';
  29. /*** a view variable ***/
  30. $this->view->title = 'Skeleton Builder';
  31. $errors = '';
  32. // did something get posted
  33. if( isset( $_POST['module_name'], $_POST['module_build'] ) )
  34. {
  35. // validate
  36. $val = new validation;
  37. $val->source = $_POST;
  38. $val->addValidator( array( 'name'=>'module_name', 'type'=>'string', 'required'=>true, 'min'=>1, 'max'=>50 ) );
  39. $val->run();
  40. if( sizeof( $val->errors ) > 0 )
  41. {
  42. foreach( $val->errors as $err )
  43. {
  44. $errors .= $err.'<br />';
  45. }
  46. }
  47. else
  48. {
  49. // attempt to build the skeleton
  50. if( !is_writeable( APP_PATH.'/modules' ) )
  51. {
  52. $errors .= APP_PATH .'/modules is not writable by the system!';
  53. }
  54. else
  55. {
  56. if( !mkdir( APP_PATH.'/modules/'.$val->sanitized['module_name'] ) )
  57. {
  58. $errors .= 'Unable to create module '.$val->sanitized['module_name'];
  59. }
  60. else
  61. {
  62. $module_name = $val->sanitized['module_name'];
  63. $module_title = ucfirst( $module_name );
  64. mkdir( APP_PATH . '/modules/' . $val->sanitized['module_name'] . '/controllers' );
  65. mkdir( APP_PATH . '/modules/' . $val->sanitized['module_name'] . '/config' );
  66. mkdir( APP_PATH.'/modules/'.$val->sanitized['module_name'].'/views' );
  67. mkdir( APP_PATH.'/modules/'.$val->sanitized['module_name'].'/assets' );
  68. mkdir( APP_PATH.'/modules/'.$val->sanitized['module_name'].'/assets/js' );
  69. mkdir( APP_PATH.'/modules/'.$val->sanitized['module_name'].'/assets/css' );
  70. // write the main controller
  71. $name = $val->sanitized['module_name'].'controller.php';
  72. $path = APP_PATH . '/modules/' . $val->sanitized['module_name'] . '/controllers/'.$name;
  73. $text = file_get_contents( APP_PATH.'/modules/skeleton/controllers/skeleton_controller.txt' );
  74. $text = str_replace( 'MODULE_NAME', $val->sanitized['module_name'], $text );
  75. $text = str_replace( 'MODULE_TITLE', $module_title, $text );
  76. file_put_contents( $path, $text );
  77. // write the index view file
  78. $path = APP_PATH . '/modules/' . $val->sanitized['module_name'] . '/views/index.phtml';
  79. $text = file_get_contents( APP_PATH.'/modules/skeleton/controllers/index.txt' );
  80. $text = str_replace( 'MODULE_TITLE', $module_title, $text );
  81. $text = str_replace( 'MODULE_NAME', $module_name, $text );
  82. file_put_contents( $path, $text );
  83. // write the admin controller
  84. $name = $val->sanitized['module_name'].'_admincontroller.php';
  85. $path = APP_PATH . '/modules/' . $val->sanitized['module_name'] . '/controllers/'.$name;
  86. $text = file_get_contents( APP_PATH.'/modules/skeleton/controllers/skeleton_admin_controller.txt' );
  87. $text = str_replace( 'MODULE_NAME', $val->sanitized['module_name'], $text );
  88. $text = str_replace( 'MODULE_TITLE', $module_title, $text );
  89. $view_path = str_replace( 'VIEW_PATH', $val->sanitized['module_name'], $text );
  90. file_put_contents( $path, $text );
  91. // write the admin_index.phtml view file
  92. $path = APP_PATH . '/modules/' . $val->sanitized['module_name'] . '/views/admin_index.phtml';
  93. $text = file_get_contents( APP_PATH.'/modules/skeleton/controllers/admin_index.txt' );
  94. $text = str_replace( 'MODULE_NAME', $val->sanitized['module_name'], $text );
  95. $text = str_replace( 'MODULE_TITLE', $module_title, $text );
  96. file_put_contents( $path, $text );
  97. // write the admin controller view file
  98. $errors = $val->sanitized['module_name'] . ' module created successfully.';
  99. }
  100. }
  101. }
  102. }
  103. $tpl->errors = $errors;
  104. /*** the cache id is based on the file name ***/
  105. $cache_id = md5( 'skeleton/index.phtml' );
  106. /*** fetch the template ***/
  107. $this->content = $tpl->fetch( 'index.phtml', $cache_id);
  108. }
  109. }