/_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
- <?php
- /**
- * File containing the skeleton controller
- *
- * @package Seven Kevins
- * @copyright Copyright (C) 2009 PHPRO.ORG. All rights reserved.
- *
- */
- namespace sevenkevins;
- class skeletonController extends baseController implements IController
- {
- public function __construct()
- {
- parent::__construct();
- // a new config
- $config = config::getInstance();
- $this->view->version = $config->config_values['application']['version'];
- }
- public function index()
- {
- /*** a new view instance ***/
- $tpl = new view;
- /*** turn caching on for this page ***/
- $tpl->setCaching(true);
- /*** set the template dir ***/
- $tpl->setTemplateDir(APP_PATH . '/modules/skeleton/views');
- /*** the include template ***/
- $tpl->include_tpl = APP_PATH . '/views/skeleton/index.phtml';
- /*** a view variable ***/
- $this->view->title = 'Skeleton Builder';
- $errors = '';
- // did something get posted
- if( isset( $_POST['module_name'], $_POST['module_build'] ) )
- {
- // validate
- $val = new validation;
- $val->source = $_POST;
- $val->addValidator( array( 'name'=>'module_name', 'type'=>'string', 'required'=>true, 'min'=>1, 'max'=>50 ) );
- $val->run();
- if( sizeof( $val->errors ) > 0 )
- {
- foreach( $val->errors as $err )
- {
- $errors .= $err.'<br />';
- }
- }
- else
- {
- // attempt to build the skeleton
- if( !is_writeable( APP_PATH.'/modules' ) )
- {
- $errors .= APP_PATH .'/modules is not writable by the system!';
- }
- else
- {
- if( !mkdir( APP_PATH.'/modules/'.$val->sanitized['module_name'] ) )
- {
- $errors .= 'Unable to create module '.$val->sanitized['module_name'];
- }
- else
- {
- $module_name = $val->sanitized['module_name'];
- $module_title = ucfirst( $module_name );
- mkdir( APP_PATH . '/modules/' . $val->sanitized['module_name'] . '/controllers' );
- mkdir( APP_PATH . '/modules/' . $val->sanitized['module_name'] . '/config' );
- mkdir( APP_PATH.'/modules/'.$val->sanitized['module_name'].'/views' );
- mkdir( APP_PATH.'/modules/'.$val->sanitized['module_name'].'/assets' );
- mkdir( APP_PATH.'/modules/'.$val->sanitized['module_name'].'/assets/js' );
- mkdir( APP_PATH.'/modules/'.$val->sanitized['module_name'].'/assets/css' );
- // write the main controller
- $name = $val->sanitized['module_name'].'controller.php';
- $path = APP_PATH . '/modules/' . $val->sanitized['module_name'] . '/controllers/'.$name;
- $text = file_get_contents( APP_PATH.'/modules/skeleton/controllers/skeleton_controller.txt' );
- $text = str_replace( 'MODULE_NAME', $val->sanitized['module_name'], $text );
- $text = str_replace( 'MODULE_TITLE', $module_title, $text );
- file_put_contents( $path, $text );
- // write the index view file
- $path = APP_PATH . '/modules/' . $val->sanitized['module_name'] . '/views/index.phtml';
- $text = file_get_contents( APP_PATH.'/modules/skeleton/controllers/index.txt' );
- $text = str_replace( 'MODULE_TITLE', $module_title, $text );
- $text = str_replace( 'MODULE_NAME', $module_name, $text );
- file_put_contents( $path, $text );
- // write the admin controller
- $name = $val->sanitized['module_name'].'_admincontroller.php';
- $path = APP_PATH . '/modules/' . $val->sanitized['module_name'] . '/controllers/'.$name;
- $text = file_get_contents( APP_PATH.'/modules/skeleton/controllers/skeleton_admin_controller.txt' );
- $text = str_replace( 'MODULE_NAME', $val->sanitized['module_name'], $text );
- $text = str_replace( 'MODULE_TITLE', $module_title, $text );
- $view_path = str_replace( 'VIEW_PATH', $val->sanitized['module_name'], $text );
- file_put_contents( $path, $text );
-
- // write the admin_index.phtml view file
- $path = APP_PATH . '/modules/' . $val->sanitized['module_name'] . '/views/admin_index.phtml';
- $text = file_get_contents( APP_PATH.'/modules/skeleton/controllers/admin_index.txt' );
- $text = str_replace( 'MODULE_NAME', $val->sanitized['module_name'], $text );
- $text = str_replace( 'MODULE_TITLE', $module_title, $text );
- file_put_contents( $path, $text );
- // write the admin controller view file
- $errors = $val->sanitized['module_name'] . ' module created successfully.';
- }
- }
- }
- }
- $tpl->errors = $errors;
- /*** the cache id is based on the file name ***/
- $cache_id = md5( 'skeleton/index.phtml' );
- /*** fetch the template ***/
- $this->content = $tpl->fetch( 'index.phtml', $cache_id);
- }
- }