You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
2.9 KiB

12 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\gii\controllers;
use yii\web\Controller;
use yii\web\HttpException;
/**
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class DefaultController extends Controller
{
public $layout = 'generator';
12 years ago
/**
* @var \yii\gii\Module
*/
public $module;
/**
* @var \yii\gii\Generator
*/
12 years ago
public $generator;
public function actionIndex()
{
$this->layout = 'main';
return $this->render('index');
}
public function actionView($id)
{
$generator = $this->loadGenerator($id);
12 years ago
$params = array('generator' => $generator);
if (isset($_POST['preview']) || isset($_POST['generate'])) {
if ($generator->validate()) {
12 years ago
$generator->saveStickyAttributes();
$files = $generator->generate();
12 years ago
if (isset($_POST['generate']) && !empty($_POST['answers'])) {
$params['hasError'] = $generator->save($files, (array)$_POST['answers'], $results);
$params['results'] = $results;
12 years ago
} else {
$params['files'] = $files;
12 years ago
$params['answers'] = isset($_POST['answers']) ? $_POST['answers'] : null;
12 years ago
}
}
}
return $this->render('view', $params);
12 years ago
}
12 years ago
public function actionPreview($id, $file)
12 years ago
{
12 years ago
$generator = $this->loadGenerator($id);
if ($generator->validate()) {
foreach ($generator->generate() as $f) {
12 years ago
if ($f->id === $file) {
$content = $f->preview();
if ($content !== false) {
return '<div class="content">' . $content . '</content>';
} else {
return '<div class="error">Preview is not available for this file type.</div>';
}
12 years ago
}
}
}
throw new HttpException(404, "Code file not found: $file");
12 years ago
}
12 years ago
public function actionDiff($id, $file)
12 years ago
{
12 years ago
$generator = $this->loadGenerator($id);
if ($generator->validate()) {
foreach ($generator->generate() as $f) {
12 years ago
if ($f->id === $file) {
return $this->renderPartial('diff', array(
'diff' => $f->diff(),
));
}
}
}
throw new HttpException(404, "Code file not found: $file");
}
12 years ago
12 years ago
public function createUrl($route, $params = array())
{
if (!isset($params['id']) && $this->generator !== null) {
foreach ($this->module->generators as $id => $generator) {
if ($generator === $this->generator) {
$params['id'] = $id;
break;
}
}
}
return parent::createUrl($route, $params);
12 years ago
}
12 years ago
/**
* Loads the generator with the specified ID.
* @param string $id the ID of the generator to be loaded.
* @return \yii\gii\Generator the loaded generator
* @throws \yii\web\HttpException
*/
12 years ago
protected function loadGenerator($id)
{
if (isset($this->module->generators[$id])) {
12 years ago
$this->generator = $this->module->generators[$id];
12 years ago
$this->generator->loadStickyAttributes();
12 years ago
$this->generator->load($_POST);
return $this->generator;
12 years ago
} else {
throw new HttpException(404, "Code generator not found: $id");
}
}
}