DefaultController.php 2.92 KB
Newer Older
Qiang Xue committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?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';
Qiang Xue committed
20 21 22 23 24 25 26
	/**
	 * @var \yii\gii\Module
	 */
	public $module;
	/**
	 * @var \yii\gii\Generator
	 */
Qiang Xue committed
27 28 29 30 31 32 33 34 35 36 37
	public $generator;

	public function actionIndex()
	{
		$this->layout = 'main';
		return $this->render('index');
	}

	public function actionView($id)
	{
		$generator = $this->loadGenerator($id);
Qiang Xue committed
38 39 40
		$params = array('generator' => $generator);
		if (isset($_POST['preview']) || isset($_POST['generate'])) {
			if ($generator->validate()) {
Qiang Xue committed
41
				$generator->saveStickyAttributes();
42
				$files = $generator->generate();
Qiang Xue committed
43
				if (isset($_POST['generate']) && !empty($_POST['answers'])) {
44 45
					$params['hasError'] = $generator->save($files, (array)$_POST['answers'], $results);
					$params['results'] = $results;
Qiang Xue committed
46 47
				} else {
					$params['files'] = $files;
Qiang Xue committed
48
					$params['answers'] = isset($_POST['answers']) ? $_POST['answers'] : null;
Qiang Xue committed
49 50 51 52 53
				}
			}
		}

		return $this->render('view', $params);
Qiang Xue committed
54 55
	}

Qiang Xue committed
56
	public function actionPreview($id, $file)
Qiang Xue committed
57
	{
Qiang Xue committed
58 59
		$generator = $this->loadGenerator($id);
		if ($generator->validate()) {
60
			foreach ($generator->generate() as $f) {
Qiang Xue committed
61
				if ($f->id === $file) {
62 63 64 65 66 67
					$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>';
					}
Qiang Xue committed
68 69 70 71
				}
			}
		}
		throw new HttpException(404, "Code file not found: $file");
Qiang Xue committed
72 73
	}

Qiang Xue committed
74
	public function actionDiff($id, $file)
Qiang Xue committed
75
	{
Qiang Xue committed
76 77
		$generator = $this->loadGenerator($id);
		if ($generator->validate()) {
78
			foreach ($generator->generate() as $f) {
Qiang Xue committed
79 80 81 82 83 84 85 86 87
				if ($f->id === $file) {
					return $this->renderPartial('diff', array(
						'diff' => $f->diff(),
					));
				}
			}
		}
		throw new HttpException(404, "Code file not found: $file");
	}
Qiang Xue committed
88

Qiang Xue committed
89 90 91 92 93 94 95 96 97 98 99
	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);
Qiang Xue committed
100 101
	}

Qiang Xue committed
102 103 104 105 106 107
	/**
	 * 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
	 */
Qiang Xue committed
108 109 110
	protected function loadGenerator($id)
	{
		if (isset($this->module->generators[$id])) {
Qiang Xue committed
111
			$this->generator = $this->module->generators[$id];
Qiang Xue committed
112
			$this->generator->loadStickyAttributes();
Qiang Xue committed
113 114
			$this->generator->load($_POST);
			return $this->generator;
Qiang Xue committed
115 116 117 118 119
		} else {
			throw new HttpException(404, "Code generator not found: $id");
		}
	}
}