Browse Source
* master: (157 commits) Fix phpdoc. fixed test break. refactor Exception::toArrayRecursive(). Fix StringHelper::substr() call. Incorrect array representation when has previous Add data padding and key derivation. Fixed beforeCopy option. doc fix. Added missing beforeCopy option to FileHelper::copyDirectory Renamed ListViewBase → BaseListView Fixes #915: helper classes renamed again Renamed YiiBase to AbstractYii reverted doc change: the original one is correct. Renamed base helper classes Advanced application: added applying migrations to readme fixed typos Advanced application: init is now automatically called when installing via Composer Advanced application: init script can now be executed in non-interactive input mode Advanced application: fixed console return codes Fixed phpdoc ...tags/2.0.0-beta
Carsten Brandt
11 years ago
253 changed files with 9451 additions and 6548 deletions
@ -0,0 +1,29 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use yii\helpers\Html; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var string $name |
||||||
|
* @var string $message |
||||||
|
* @var Exception $exception |
||||||
|
*/ |
||||||
|
|
||||||
|
$this->title = $name; |
||||||
|
?> |
||||||
|
<div class="site-error"> |
||||||
|
|
||||||
|
<h1><?php echo Html::encode($this->title); ?></h1>
|
||||||
|
|
||||||
|
<div class="alert alert-danger"> |
||||||
|
<?php echo nl2br(Html::encode($message)); ?> |
||||||
|
</div> |
||||||
|
|
||||||
|
<p> |
||||||
|
The above error occurred while the Web server was processing your request. |
||||||
|
</p> |
||||||
|
<p> |
||||||
|
Please contact us if you think this is a server error. Thank you. |
||||||
|
</p> |
||||||
|
|
||||||
|
</div> |
@ -1,17 +1,11 @@ |
|||||||
<?php |
<?php |
||||||
return array( |
return array( |
||||||
|
'preload' => array( |
||||||
|
//'debug', |
||||||
|
), |
||||||
'modules' => array( |
'modules' => array( |
||||||
// 'debug' => array( |
// 'debug' => array( |
||||||
// 'class' => 'yii\debug\Module', |
// 'class' => 'yii\debug\Module', |
||||||
// ), |
// ), |
||||||
), |
), |
||||||
'components' => array( |
|
||||||
'log' => array( |
|
||||||
'targets' => array( |
|
||||||
// array( |
|
||||||
// 'class' => 'yii\log\DebugTarget', |
|
||||||
// ) |
|
||||||
), |
|
||||||
), |
|
||||||
), |
|
||||||
); |
); |
||||||
|
@ -1,17 +1,11 @@ |
|||||||
<?php |
<?php |
||||||
return array( |
return array( |
||||||
|
'preload' => array( |
||||||
|
//'debug', |
||||||
|
), |
||||||
'modules' => array( |
'modules' => array( |
||||||
// 'debug' => array( |
// 'debug' => array( |
||||||
// 'class' => 'yii\debug\Module', |
// 'class' => 'yii\debug\Module', |
||||||
// ), |
// ), |
||||||
), |
), |
||||||
'components' => array( |
|
||||||
'log' => array( |
|
||||||
'targets' => array( |
|
||||||
// array( |
|
||||||
// 'class' => 'yii\log\DebugTarget', |
|
||||||
// ) |
|
||||||
), |
|
||||||
), |
|
||||||
), |
|
||||||
); |
); |
||||||
|
@ -0,0 +1,29 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use yii\helpers\Html; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var string $name |
||||||
|
* @var string $message |
||||||
|
* @var Exception $exception |
||||||
|
*/ |
||||||
|
|
||||||
|
$this->title = $name; |
||||||
|
?> |
||||||
|
<div class="site-error"> |
||||||
|
|
||||||
|
<h1><?php echo Html::encode($this->title); ?></h1>
|
||||||
|
|
||||||
|
<div class="alert alert-danger"> |
||||||
|
<?php echo nl2br(Html::encode($message)); ?> |
||||||
|
</div> |
||||||
|
|
||||||
|
<p> |
||||||
|
The above error occurred while the Web server was processing your request. |
||||||
|
</p> |
||||||
|
<p> |
||||||
|
Please contact us if you think this is a server error. Thank you. |
||||||
|
</p> |
||||||
|
|
||||||
|
</div> |
@ -0,0 +1,117 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yii\db\cubrid; |
||||||
|
|
||||||
|
use yii\base\InvalidParamException; |
||||||
|
|
||||||
|
/** |
||||||
|
* QueryBuilder is the query builder for CUBRID databases (version 9.1.x and higher). |
||||||
|
* |
||||||
|
* @author Carsten Brandt <mail@cebe.cc> |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
class QueryBuilder extends \yii\db\QueryBuilder |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var array mapping from abstract column types (keys) to physical column types (values). |
||||||
|
*/ |
||||||
|
public $typeMap = array( |
||||||
|
Schema::TYPE_PK => 'int NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||||||
|
Schema::TYPE_BIGPK => 'bigint NOT NULL AUTO_INCREMENT PRIMARY KEY', |
||||||
|
Schema::TYPE_STRING => 'varchar(255)', |
||||||
|
Schema::TYPE_TEXT => 'varchar', |
||||||
|
Schema::TYPE_SMALLINT => 'smallint', |
||||||
|
Schema::TYPE_INTEGER => 'int', |
||||||
|
Schema::TYPE_BIGINT => 'bigint', |
||||||
|
Schema::TYPE_FLOAT => 'float(7)', |
||||||
|
Schema::TYPE_DECIMAL => 'decimal(10,0)', |
||||||
|
Schema::TYPE_DATETIME => 'datetime', |
||||||
|
Schema::TYPE_TIMESTAMP => 'timestamp', |
||||||
|
Schema::TYPE_TIME => 'time', |
||||||
|
Schema::TYPE_DATE => 'date', |
||||||
|
Schema::TYPE_BINARY => 'blob', |
||||||
|
Schema::TYPE_BOOLEAN => 'smallint', |
||||||
|
Schema::TYPE_MONEY => 'decimal(19,4)', |
||||||
|
); |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a SQL statement for resetting the sequence value of a table's primary key. |
||||||
|
* The sequence will be reset such that the primary key of the next new row inserted |
||||||
|
* will have the specified value or 1. |
||||||
|
* @param string $tableName the name of the table whose primary key sequence will be reset |
||||||
|
* @param mixed $value the value for the primary key of the next new row inserted. If this is not set, |
||||||
|
* the next new row's primary key will have a value 1. |
||||||
|
* @return string the SQL statement for resetting sequence |
||||||
|
* @throws InvalidParamException if the table does not exist or there is no sequence associated with the table. |
||||||
|
*/ |
||||||
|
public function resetSequence($tableName, $value = null) |
||||||
|
{ |
||||||
|
$table = $this->db->getTableSchema($tableName); |
||||||
|
if ($table !== null && $table->sequenceName !== null) { |
||||||
|
$tableName = $this->db->quoteTableName($tableName); |
||||||
|
if ($value === null) { |
||||||
|
$key = reset($table->primaryKey); |
||||||
|
$value = (int)$this->db->createCommand("SELECT MAX(`$key`) FROM " . $this->db->schema->quoteTableName($tableName))->queryScalar() + 1; |
||||||
|
} else { |
||||||
|
$value = (int)$value; |
||||||
|
} |
||||||
|
return "ALTER TABLE " . $this->db->schema->quoteTableName($tableName) . " AUTO_INCREMENT=$value;"; |
||||||
|
} elseif ($table === null) { |
||||||
|
throw new InvalidParamException("Table not found: $tableName"); |
||||||
|
} else { |
||||||
|
throw new InvalidParamException("There is not sequence associated with table '$tableName'."); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Generates a batch INSERT SQL statement. |
||||||
|
* For example, |
||||||
|
* |
||||||
|
* ~~~ |
||||||
|
* $connection->createCommand()->batchInsert('tbl_user', array('name', 'age'), array( |
||||||
|
* array('Tom', 30), |
||||||
|
* array('Jane', 20), |
||||||
|
* array('Linda', 25), |
||||||
|
* ))->execute(); |
||||||
|
* ~~~ |
||||||
|
* |
||||||
|
* Note that the values in each row must match the corresponding column names. |
||||||
|
* |
||||||
|
* @param string $table the table that new rows will be inserted into. |
||||||
|
* @param array $columns the column names |
||||||
|
* @param array $rows the rows to be batch inserted into the table |
||||||
|
* @return string the batch INSERT SQL statement |
||||||
|
*/ |
||||||
|
public function batchInsert($table, $columns, $rows) |
||||||
|
{ |
||||||
|
if (($tableSchema = $this->db->getTableSchema($table)) !== null) { |
||||||
|
$columnSchemas = $tableSchema->columns; |
||||||
|
} else { |
||||||
|
$columnSchemas = array(); |
||||||
|
} |
||||||
|
|
||||||
|
foreach ($columns as $i => $name) { |
||||||
|
$columns[$i] = $this->db->quoteColumnName($name); |
||||||
|
} |
||||||
|
|
||||||
|
$values = array(); |
||||||
|
foreach ($rows as $row) { |
||||||
|
$vs = array(); |
||||||
|
foreach ($row as $i => $value) { |
||||||
|
if (!is_array($value) && isset($columnSchemas[$columns[$i]])) { |
||||||
|
$value = $columnSchemas[$columns[$i]]->typecast($value); |
||||||
|
} |
||||||
|
$vs[] = is_string($value) ? $this->db->quoteValue($value) : $value; |
||||||
|
} |
||||||
|
$values[] = '(' . implode(', ', $vs) . ')'; |
||||||
|
} |
||||||
|
|
||||||
|
return 'INSERT INTO ' . $this->db->quoteTableName($table) |
||||||
|
. ' (' . implode(', ', $columns) . ') VALUES ' . implode(', ', $values); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,240 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yii\db\cubrid; |
||||||
|
|
||||||
|
use yii\db\Expression; |
||||||
|
use yii\db\TableSchema; |
||||||
|
use yii\db\ColumnSchema; |
||||||
|
|
||||||
|
/** |
||||||
|
* Schema is the class for retrieving metadata from a CUBRID database (version 9.1.x and higher). |
||||||
|
* |
||||||
|
* @author Carsten Brandt <mail@cebe.cc> |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
class Schema extends \yii\db\Schema |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var array mapping from physical column types (keys) to abstract column types (values) |
||||||
|
* Please refer to [CUBRID manual](http://www.cubrid.org/manual/91/en/sql/datatype.html) for |
||||||
|
* details on data types. |
||||||
|
*/ |
||||||
|
public $typeMap = array( |
||||||
|
// Numeric data types |
||||||
|
'short' => self::TYPE_SMALLINT, |
||||||
|
'smallint' => self::TYPE_SMALLINT, |
||||||
|
'int' => self::TYPE_INTEGER, |
||||||
|
'integer' => self::TYPE_INTEGER, |
||||||
|
'bigint' => self::TYPE_BIGINT, |
||||||
|
'numeric' => self::TYPE_DECIMAL, |
||||||
|
'decimal' => self::TYPE_DECIMAL, |
||||||
|
'float' => self::TYPE_FLOAT, |
||||||
|
'real' => self::TYPE_FLOAT, |
||||||
|
'double' => self::TYPE_FLOAT, |
||||||
|
'double precision' => self::TYPE_FLOAT, |
||||||
|
'monetary' => self::TYPE_MONEY, |
||||||
|
// Date/Time data types |
||||||
|
'date' => self::TYPE_DATE, |
||||||
|
'time' => self::TYPE_TIME, |
||||||
|
'timestamp' => self::TYPE_TIMESTAMP, |
||||||
|
'datetime' => self::TYPE_DATETIME, |
||||||
|
// String data types |
||||||
|
'char' => self::TYPE_STRING, |
||||||
|
'varchar' => self::TYPE_STRING, |
||||||
|
'char varying' => self::TYPE_STRING, |
||||||
|
'nchar' => self::TYPE_STRING, |
||||||
|
'nchar varying' => self::TYPE_STRING, |
||||||
|
'string' => self::TYPE_STRING, |
||||||
|
// BLOB/CLOB data types |
||||||
|
'blob' => self::TYPE_BINARY, |
||||||
|
'clob' => self::TYPE_BINARY, |
||||||
|
// Bit string data types |
||||||
|
'bit' => self::TYPE_STRING, |
||||||
|
'bit varying' => self::TYPE_STRING, |
||||||
|
// Collection data types (considered strings for now) |
||||||
|
'set' => self::TYPE_STRING, |
||||||
|
'multiset' => self::TYPE_STRING, |
||||||
|
'list' => self::TYPE_STRING, |
||||||
|
'sequence' => self::TYPE_STRING, |
||||||
|
'enum' => self::TYPE_STRING, |
||||||
|
); |
||||||
|
|
||||||
|
/** |
||||||
|
* Quotes a table name for use in a query. |
||||||
|
* A simple table name has no schema prefix. |
||||||
|
* @param string $name table name |
||||||
|
* @return string the properly quoted table name |
||||||
|
*/ |
||||||
|
public function quoteSimpleTableName($name) |
||||||
|
{ |
||||||
|
return strpos($name, '"') !== false ? $name : '"' . $name . '"'; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Quotes a column name for use in a query. |
||||||
|
* A simple column name has no prefix. |
||||||
|
* @param string $name column name |
||||||
|
* @return string the properly quoted column name |
||||||
|
*/ |
||||||
|
public function quoteSimpleColumnName($name) |
||||||
|
{ |
||||||
|
return strpos($name, '"') !== false || $name === '*' ? $name : '"' . $name . '"'; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Quotes a string value for use in a query. |
||||||
|
* Note that if the parameter is not a string, it will be returned without change. |
||||||
|
* @param string $str string to be quoted |
||||||
|
* @return string the properly quoted string |
||||||
|
* @see http://www.php.net/manual/en/function.PDO-quote.php |
||||||
|
*/ |
||||||
|
public function quoteValue($str) |
||||||
|
{ |
||||||
|
if (!is_string($str)) { |
||||||
|
return $str; |
||||||
|
} |
||||||
|
|
||||||
|
$this->db->open(); |
||||||
|
// workaround for broken PDO::quote() implementation in CUBRID 9.1.0 http://jira.cubrid.org/browse/APIS-658 |
||||||
|
if (version_compare($this->db->pdo->getAttribute(\PDO::ATTR_CLIENT_VERSION), '9.1.0', '<=')) { |
||||||
|
return "'" . addcslashes(str_replace("'", "''", $str), "\000\n\r\\\032") . "'"; |
||||||
|
} else { |
||||||
|
return $this->db->pdo->quote($str); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a query builder for the CUBRID database. |
||||||
|
* @return QueryBuilder query builder instance |
||||||
|
*/ |
||||||
|
public function createQueryBuilder() |
||||||
|
{ |
||||||
|
return new QueryBuilder($this->db); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Loads the metadata for the specified table. |
||||||
|
* @param string $name table name |
||||||
|
* @return TableSchema driver dependent table metadata. Null if the table does not exist. |
||||||
|
*/ |
||||||
|
protected function loadTableSchema($name) |
||||||
|
{ |
||||||
|
$this->db->open(); |
||||||
|
$tableInfo = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE, $name); |
||||||
|
|
||||||
|
if (isset($tableInfo[0]['NAME'])) { |
||||||
|
$table = new TableSchema(); |
||||||
|
$table->name = $tableInfo[0]['NAME']; |
||||||
|
|
||||||
|
$sql = 'SHOW FULL COLUMNS FROM ' . $this->quoteSimpleTableName($table->name); |
||||||
|
$columns = $this->db->createCommand($sql)->queryAll(); |
||||||
|
|
||||||
|
foreach ($columns as $info) { |
||||||
|
$column = $this->loadColumnSchema($info); |
||||||
|
$table->columns[$column->name] = $column; |
||||||
|
if ($column->isPrimaryKey) { |
||||||
|
$table->primaryKey[] = $column->name; |
||||||
|
if ($column->autoIncrement) { |
||||||
|
$table->sequenceName = ''; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
$foreignKeys = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_IMPORTED_KEYS, $table->name); |
||||||
|
foreach($foreignKeys as $key) { |
||||||
|
if (isset($table->foreignKeys[$key['FK_NAME']])) { |
||||||
|
$table->foreignKeys[$key['FK_NAME']][$key['FKCOLUMN_NAME']] = $key['PKCOLUMN_NAME']; |
||||||
|
} else { |
||||||
|
$table->foreignKeys[$key['FK_NAME']] = array( |
||||||
|
$key['PKTABLE_NAME'], |
||||||
|
$key['FKCOLUMN_NAME'] => $key['PKCOLUMN_NAME'] |
||||||
|
); |
||||||
|
} |
||||||
|
} |
||||||
|
$table->foreignKeys = array_values($table->foreignKeys); |
||||||
|
|
||||||
|
return $table; |
||||||
|
} else { |
||||||
|
return null; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Loads the column information into a [[ColumnSchema]] object. |
||||||
|
* @param array $info column information |
||||||
|
* @return ColumnSchema the column schema object |
||||||
|
*/ |
||||||
|
protected function loadColumnSchema($info) |
||||||
|
{ |
||||||
|
$column = new ColumnSchema(); |
||||||
|
|
||||||
|
$column->name = $info['Field']; |
||||||
|
$column->allowNull = $info['Null'] === 'YES'; |
||||||
|
$column->isPrimaryKey = strpos($info['Key'], 'PRI') !== false; |
||||||
|
$column->autoIncrement = stripos($info['Extra'], 'auto_increment') !== false; |
||||||
|
|
||||||
|
$column->dbType = strtolower($info['Type']); |
||||||
|
$column->unsigned = strpos($column->dbType, 'unsigned') !== false; |
||||||
|
|
||||||
|
$column->type = self::TYPE_STRING; |
||||||
|
if (preg_match('/^([\w ]+)(?:\(([^\)]+)\))?/', $column->dbType, $matches)) { |
||||||
|
$type = $matches[1]; |
||||||
|
if (isset($this->typeMap[$type])) { |
||||||
|
$column->type = $this->typeMap[$type]; |
||||||
|
} |
||||||
|
if (!empty($matches[2])) { |
||||||
|
if ($type === 'enum') { |
||||||
|
$values = explode(',', $matches[2]); |
||||||
|
foreach ($values as $i => $value) { |
||||||
|
$values[$i] = trim($value, "'"); |
||||||
|
} |
||||||
|
$column->enumValues = $values; |
||||||
|
} else { |
||||||
|
$values = explode(',', $matches[2]); |
||||||
|
$column->size = $column->precision = (int)$values[0]; |
||||||
|
if (isset($values[1])) { |
||||||
|
$column->scale = (int)$values[1]; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
$column->phpType = $this->getColumnPhpType($column); |
||||||
|
|
||||||
|
if ($column->type === 'timestamp' && $info['Default'] === 'CURRENT_TIMESTAMP' || |
||||||
|
$column->type === 'datetime' && $info['Default'] === 'SYS_DATETIME' || |
||||||
|
$column->type === 'date' && $info['Default'] === 'SYS_DATE' || |
||||||
|
$column->type === 'time' && $info['Default'] === 'SYS_TIME' |
||||||
|
) { |
||||||
|
$column->defaultValue = new Expression($info['Default']); |
||||||
|
} else { |
||||||
|
$column->defaultValue = $column->typecast($info['Default']); |
||||||
|
} |
||||||
|
|
||||||
|
return $column; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns all table names in the database. |
||||||
|
* @param string $schema the schema of the tables. Defaults to empty string, meaning the current or default schema. |
||||||
|
* @return array all table names in the database. The names have NO schema name prefix. |
||||||
|
*/ |
||||||
|
protected function findTableNames($schema = '') |
||||||
|
{ |
||||||
|
$this->db->open(); |
||||||
|
$tables = $this->db->pdo->cubrid_schema(\PDO::CUBRID_SCH_TABLE); |
||||||
|
$tableNames = array(); |
||||||
|
foreach($tables as $table) { |
||||||
|
// do not list system tables |
||||||
|
if ($table['TYPE'] != 0) { |
||||||
|
$tableNames[] = $table['NAME']; |
||||||
|
} |
||||||
|
} |
||||||
|
return $tableNames; |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,23 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright © 2008-2011 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yii\db\mssql; |
||||||
|
|
||||||
|
/** |
||||||
|
* TableSchema represents the metadata of a database table. |
||||||
|
* |
||||||
|
* @author Qiang Xue <qiang.xue@gmail.com> |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
class TableSchema extends \yii\db\TableSchema |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var string name of the catalog (database) that this table belongs to. |
||||||
|
* Defaults to null, meaning no catalog (or the current database). |
||||||
|
*/ |
||||||
|
public $catalogName; |
||||||
|
} |
@ -1,8 +1,152 @@ |
|||||||
<?php |
<?php |
||||||
|
|
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
/** |
/** |
||||||
* Created by JetBrains PhpStorm. |
* This is the template for generating a CRUD controller class file. |
||||||
* User: qiang |
* |
||||||
* Date: 8/26/13 |
* @var yii\base\View $this |
||||||
* Time: 5:22 PM |
* @var yii\gii\generators\crud\Generator $generator |
||||||
* To change this template use File | Settings | File Templates. |
|
||||||
*/ |
*/ |
||||||
|
|
||||||
|
$controllerClass = StringHelper::basename($generator->controllerClass); |
||||||
|
$modelClass = StringHelper::basename($generator->modelClass); |
||||||
|
$searchModelClass = StringHelper::basename($generator->searchModelClass); |
||||||
|
|
||||||
|
$pks = $generator->getTableSchema()->primaryKey; |
||||||
|
$urlParams = $generator->generateUrlParams(); |
||||||
|
$actionParams = $generator->generateActionParams(); |
||||||
|
$actionParamComments = $generator->generateActionParamComments(); |
||||||
|
|
||||||
|
echo "<?php\n";
|
||||||
|
?> |
||||||
|
|
||||||
|
namespace <?php echo StringHelper::dirname(ltrim($generator->controllerClass, '\\')); ?>;
|
||||||
|
|
||||||
|
use <?php echo ltrim($generator->modelClass, '\\'); ?>;
|
||||||
|
use <?php echo ltrim($generator->searchModelClass, '\\'); ?>;
|
||||||
|
use yii\data\ActiveDataProvider; |
||||||
|
use <?php echo ltrim($generator->baseControllerClass, '\\'); ?>;
|
||||||
|
use yii\web\HttpException; |
||||||
|
use yii\web\VerbFilter; |
||||||
|
|
||||||
|
/** |
||||||
|
* <?php echo $controllerClass; ?> implements the CRUD actions for <?php echo $modelClass; ?> model.
|
||||||
|
*/ |
||||||
|
class <?php echo $controllerClass; ?> extends <?php echo StringHelper::basename($generator->baseControllerClass) . "\n"; ?> |
||||||
|
{ |
||||||
|
public function behaviors() |
||||||
|
{ |
||||||
|
return array( |
||||||
|
'verbs' => array( |
||||||
|
'class' => VerbFilter::className(), |
||||||
|
'actions' => array( |
||||||
|
'delete' => array('post'), |
||||||
|
), |
||||||
|
), |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Lists all <?php echo $modelClass; ?> models.
|
||||||
|
* @return mixed |
||||||
|
*/ |
||||||
|
public function actionIndex() |
||||||
|
{ |
||||||
|
$searchModel = new <?php echo $searchModelClass; ?>;
|
||||||
|
$dataProvider = $searchModel->search($_GET); |
||||||
|
|
||||||
|
return $this->render('index', array( |
||||||
|
'dataProvider' => $dataProvider, |
||||||
|
'searchModel' => $searchModel, |
||||||
|
)); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Displays a single <?php echo $modelClass; ?> model.
|
||||||
|
* <?php echo implode("\n\t * ", $actionParamComments) . "\n"; ?> |
||||||
|
* @return mixed |
||||||
|
*/ |
||||||
|
public function actionView(<?php echo $actionParams; ?>)
|
||||||
|
{ |
||||||
|
return $this->render('view', array( |
||||||
|
'model' => $this->findModel(<?php echo $actionParams; ?>),
|
||||||
|
)); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a new <?php echo $modelClass; ?> model.
|
||||||
|
* If creation is successful, the browser will be redirected to the 'view' page. |
||||||
|
* @return mixed |
||||||
|
*/ |
||||||
|
public function actionCreate() |
||||||
|
{ |
||||||
|
$model = new <?php echo $modelClass; ?>;
|
||||||
|
|
||||||
|
if ($model->load($_POST) && $model->save()) { |
||||||
|
return $this->redirect(array('view', <?php echo $urlParams; ?>));
|
||||||
|
} else { |
||||||
|
return $this->render('create', array( |
||||||
|
'model' => $model, |
||||||
|
)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Updates an existing <?php echo $modelClass; ?> model.
|
||||||
|
* If update is successful, the browser will be redirected to the 'view' page. |
||||||
|
* <?php echo implode("\n\t * ", $actionParamComments) . "\n"; ?> |
||||||
|
* @return mixed |
||||||
|
*/ |
||||||
|
public function actionUpdate(<?php echo $actionParams; ?>)
|
||||||
|
{ |
||||||
|
$model = $this->findModel(<?php echo $actionParams; ?>);
|
||||||
|
|
||||||
|
if ($model->load($_POST) && $model->save()) { |
||||||
|
return $this->redirect(array('view', <?php echo $urlParams; ?>));
|
||||||
|
} else { |
||||||
|
return $this->render('update', array( |
||||||
|
'model' => $model, |
||||||
|
)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Deletes an existing <?php echo $modelClass; ?> model.
|
||||||
|
* If deletion is successful, the browser will be redirected to the 'index' page. |
||||||
|
* <?php echo implode("\n\t * ", $actionParamComments) . "\n"; ?> |
||||||
|
* @return mixed |
||||||
|
*/ |
||||||
|
public function actionDelete(<?php echo $actionParams; ?>)
|
||||||
|
{ |
||||||
|
$this->findModel(<?php echo $actionParams; ?>)->delete();
|
||||||
|
return $this->redirect(array('index')); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Finds the <?php echo $modelClass; ?> model based on its primary key value.
|
||||||
|
* If the model is not found, a 404 HTTP exception will be thrown. |
||||||
|
* <?php echo implode("\n\t * ", $actionParamComments) . "\n"; ?> |
||||||
|
* @return <?php echo $modelClass; ?> the loaded model
|
||||||
|
* @throws HttpException if the model cannot be found |
||||||
|
*/ |
||||||
|
protected function findModel(<?php echo $actionParams; ?>)
|
||||||
|
{ |
||||||
|
<?php |
||||||
|
if (count($pks) === 1) { |
||||||
|
$condition = '$id'; |
||||||
|
} else { |
||||||
|
$condition = array(); |
||||||
|
foreach ($pks as $pk) { |
||||||
|
$condition[] = "'$pk' => \$$pk"; |
||||||
|
} |
||||||
|
$condition = 'array(' . implode(', ', $condition) . ')'; |
||||||
|
} |
||||||
|
?> |
||||||
|
if (($model = <?php echo $modelClass; ?>::find(<?php echo $condition; ?>)) !== null) {
|
||||||
|
return $model; |
||||||
|
} else { |
||||||
|
throw new HttpException(404, 'The requested page does not exist.'); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
@ -1,8 +0,0 @@ |
|||||||
<?php |
|
||||||
/** |
|
||||||
* Created by JetBrains PhpStorm. |
|
||||||
* User: qiang |
|
||||||
* Date: 8/26/13 |
|
||||||
* Time: 5:22 PM |
|
||||||
* To change this template use File | Settings | File Templates. |
|
||||||
*/ |
|
@ -0,0 +1,83 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
|
/** |
||||||
|
* This is the template for generating a CRUD controller class file. |
||||||
|
* |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var yii\gii\generators\crud\Generator $generator |
||||||
|
*/ |
||||||
|
|
||||||
|
$modelClass = StringHelper::basename($generator->modelClass); |
||||||
|
$searchModelClass = StringHelper::basename($generator->searchModelClass); |
||||||
|
$rules = $generator->generateSearchRules(); |
||||||
|
$labels = $generator->generateSearchLabels(); |
||||||
|
$searchAttributes = $generator->getSearchAttributes(); |
||||||
|
$searchConditions = $generator->generateSearchConditions(); |
||||||
|
|
||||||
|
echo "<?php\n";
|
||||||
|
?> |
||||||
|
|
||||||
|
namespace <?php echo StringHelper::dirname(ltrim($generator->searchModelClass, '\\')); ?>;
|
||||||
|
|
||||||
|
use yii\base\Model; |
||||||
|
use yii\data\ActiveDataProvider; |
||||||
|
use <?php echo ltrim($generator->modelClass, '\\'); ?>;
|
||||||
|
|
||||||
|
/** |
||||||
|
* <?php echo $searchModelClass; ?> represents the model behind the search form about <?php echo $modelClass; ?>.
|
||||||
|
*/ |
||||||
|
class <?php echo $searchModelClass; ?> extends Model
|
||||||
|
{ |
||||||
|
public $<?php echo implode(";\n\tpublic $", $searchAttributes); ?>;
|
||||||
|
|
||||||
|
public function rules() |
||||||
|
{ |
||||||
|
return array( |
||||||
|
<?php echo implode(",\n\t\t\t", $rules); ?>,
|
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @inheritdoc |
||||||
|
*/ |
||||||
|
public function attributeLabels() |
||||||
|
{ |
||||||
|
return array( |
||||||
|
<?php foreach ($labels as $name => $label): ?> |
||||||
|
<?php echo "'$name' => '" . addslashes($label) . "',\n"; ?> |
||||||
|
<?php endforeach; ?> |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
public function search($params) |
||||||
|
{ |
||||||
|
$query = <?php echo $modelClass; ?>::find();
|
||||||
|
$dataProvider = new ActiveDataProvider(array( |
||||||
|
'query' => $query, |
||||||
|
)); |
||||||
|
|
||||||
|
if (!($this->load($params) && $this->validate())) { |
||||||
|
return $dataProvider; |
||||||
|
} |
||||||
|
|
||||||
|
<?php echo implode("\n\t\t", $searchConditions); ?> |
||||||
|
|
||||||
|
return $dataProvider; |
||||||
|
} |
||||||
|
|
||||||
|
protected function addCondition($query, $attribute, $partialMatch = false) |
||||||
|
{ |
||||||
|
$value = $this->$attribute; |
||||||
|
if (trim($value) === '') { |
||||||
|
return; |
||||||
|
} |
||||||
|
if ($partialMatch) { |
||||||
|
$value = '%' . strtr($value, array('%'=>'\%', '_'=>'\_', '\\'=>'\\\\')) . '%'; |
||||||
|
$query->andWhere(array('like', $attribute, $value)); |
||||||
|
} else { |
||||||
|
$query->andWhere(array($attribute => $value)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,44 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use yii\helpers\Inflector; |
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var yii\gii\generators\crud\Generator $generator |
||||||
|
*/ |
||||||
|
|
||||||
|
/** @var \yii\db\ActiveRecord $model */ |
||||||
|
$model = new $generator->modelClass; |
||||||
|
$safeAttributes = $model->safeAttributes(); |
||||||
|
if (empty($safeAttributes)) { |
||||||
|
$safeAttributes = $model->getTableSchema()->columnNames; |
||||||
|
} |
||||||
|
|
||||||
|
echo "<?php\n";
|
||||||
|
?> |
||||||
|
|
||||||
|
use yii\helpers\Html; |
||||||
|
use yii\widgets\ActiveForm; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var <?php echo ltrim($generator->modelClass, '\\'); ?> $model
|
||||||
|
* @var yii\widgets\ActiveForm $form |
||||||
|
*/ |
||||||
|
?> |
||||||
|
|
||||||
|
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-form">
|
||||||
|
|
||||||
|
<?php echo '<?php'; ?> $form = ActiveForm::begin(); ?>
|
||||||
|
|
||||||
|
<?php foreach ($safeAttributes as $attribute) { |
||||||
|
echo "\t\t<?php echo " . $generator->generateActiveField($attribute) . " ?>\n\n";
|
||||||
|
} ?> |
||||||
|
<div class="form-group"> |
||||||
|
<?php echo '<?php'; ?> echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', array('class' => 'btn btn-primary')); ?>
|
||||||
|
</div> |
||||||
|
|
||||||
|
<?php echo '<?php'; ?> ActiveForm::end(); ?>
|
||||||
|
|
||||||
|
</div> |
@ -0,0 +1,45 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use yii\helpers\Inflector; |
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var yii\gii\generators\crud\Generator $generator |
||||||
|
*/ |
||||||
|
|
||||||
|
echo "<?php\n";
|
||||||
|
?> |
||||||
|
|
||||||
|
use yii\helpers\Html; |
||||||
|
use yii\widgets\ActiveForm; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var <?php echo ltrim($generator->searchModelClass, '\\'); ?> $model
|
||||||
|
* @var yii\widgets\ActiveForm $form |
||||||
|
*/ |
||||||
|
?> |
||||||
|
|
||||||
|
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-search">
|
||||||
|
|
||||||
|
<?php echo '<?php'; ?> $form = ActiveForm::begin(array('method' => 'get')); ?>
|
||||||
|
|
||||||
|
<?php |
||||||
|
$count = 0; |
||||||
|
foreach ($generator->getTableSchema()->getColumnNames() as $attribute) { |
||||||
|
if (++$count < 6) { |
||||||
|
echo "\t\t<?php echo " . $generator->generateActiveSearchField($attribute) . " ?>\n";
|
||||||
|
} else { |
||||||
|
echo "\t\t<?php // echo " . $generator->generateActiveSearchField($attribute) . " ?>\n";
|
||||||
|
} |
||||||
|
} |
||||||
|
?> |
||||||
|
<div class="form-group"> |
||||||
|
<?php echo '<?php'; ?> echo Html::submitButton('Search', array('class' => 'btn btn-primary')); ?>
|
||||||
|
<?php echo '<?php'; ?> echo Html::resetButton('Reset', array('class' => 'btn btn-default')); ?>
|
||||||
|
</div> |
||||||
|
|
||||||
|
<?php echo '<?php'; ?> ActiveForm::end(); ?>
|
||||||
|
|
||||||
|
</div> |
@ -0,0 +1,33 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use yii\helpers\Inflector; |
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var yii\gii\generators\crud\Generator $generator |
||||||
|
*/ |
||||||
|
|
||||||
|
echo "<?php\n";
|
||||||
|
?> |
||||||
|
|
||||||
|
use yii\helpers\Html; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var <?php echo ltrim($generator->modelClass, '\\'); ?> $model
|
||||||
|
*/ |
||||||
|
|
||||||
|
$this->title = 'Create <?php echo Inflector::camel2words(StringHelper::basename($generator->modelClass)); ?>';
|
||||||
|
$this->params['breadcrumbs'][] = array('label' => '<?php echo Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass))); ?>', 'url' => array('index'));
|
||||||
|
$this->params['breadcrumbs'][] = $this->title; |
||||||
|
?> |
||||||
|
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-create">
|
||||||
|
|
||||||
|
<h1><?php echo "<?php"; ?> echo Html::encode($this->title); ?></h1>
|
||||||
|
|
||||||
|
<?php echo "<?php"; ?> echo $this->render('_form', array(
|
||||||
|
'model' => $model, |
||||||
|
)); ?> |
||||||
|
|
||||||
|
</div> |
@ -0,0 +1,73 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use yii\helpers\Inflector; |
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var yii\gii\generators\crud\Generator $generator |
||||||
|
*/ |
||||||
|
|
||||||
|
$urlParams = $generator->generateUrlParams(); |
||||||
|
$nameAttribute = $generator->getNameAttribute(); |
||||||
|
|
||||||
|
echo "<?php\n";
|
||||||
|
?> |
||||||
|
|
||||||
|
use yii\helpers\Html; |
||||||
|
use <?php echo $generator->indexWidgetType === 'grid' ? 'yii\grid\GridView' : 'yii\widgets\ListView'; ?>;
|
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var yii\data\ActiveDataProvider $dataProvider |
||||||
|
* @var <?php echo ltrim($generator->searchModelClass, '\\'); ?> $searchModel
|
||||||
|
*/ |
||||||
|
|
||||||
|
$this->title = '<?php echo Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass))); ?>';
|
||||||
|
$this->params['breadcrumbs'][] = $this->title; |
||||||
|
?> |
||||||
|
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-index">
|
||||||
|
|
||||||
|
<h1><?php echo "<?php"; ?> echo Html::encode($this->title); ?></h1>
|
||||||
|
|
||||||
|
<?php echo '<?php' . ($generator->indexWidgetType === 'grid' ? ' //' : ''); ?> echo $this->render('_search', array('model' => $searchModel)); ?>
|
||||||
|
|
||||||
|
<p> |
||||||
|
<?php echo '<?php'; ?> echo Html::a('Create <?php echo StringHelper::basename($generator->modelClass); ?>', array('create'), array('class' => 'btn btn-danger')); ?>
|
||||||
|
</p> |
||||||
|
|
||||||
|
<?php if ($generator->indexWidgetType === 'grid'): ?> |
||||||
|
<?php echo "<?php"; ?> echo GridView::widget(array(
|
||||||
|
'dataProvider' => $dataProvider, |
||||||
|
'filterModel' => $searchModel, |
||||||
|
'columns' => array( |
||||||
|
array('class' => 'yii\grid\SerialColumn'), |
||||||
|
|
||||||
|
<?php |
||||||
|
$count = 0; |
||||||
|
foreach ($generator->getTableSchema()->columns as $column) { |
||||||
|
$format = $generator->generateColumnFormat($column); |
||||||
|
if (++$count < 6) { |
||||||
|
echo "\t\t\t'" . $column->name . ($format === 'text' ? '' : ':' . $format) . "',\n"; |
||||||
|
} else { |
||||||
|
echo "\t\t\t// '" . $column->name . ($format === 'text' ? '' : ':' . $format) . "',\n"; |
||||||
|
} |
||||||
|
} |
||||||
|
?> |
||||||
|
|
||||||
|
array('class' => 'yii\grid\ActionColumn'), |
||||||
|
), |
||||||
|
)); ?> |
||||||
|
<?php else: ?> |
||||||
|
<?php echo "<?php"; ?> echo ListView::widget(array(
|
||||||
|
'dataProvider' => $dataProvider, |
||||||
|
'itemOptions' => array( |
||||||
|
'class' => 'item', |
||||||
|
), |
||||||
|
'itemView' => function ($model, $key, $index, $widget) { |
||||||
|
return Html::a(Html::encode($model-><?php echo $nameAttribute; ?>), array('view', <?php echo $urlParams; ?>));
|
||||||
|
}, |
||||||
|
)); ?> |
||||||
|
<?php endif; ?> |
||||||
|
|
||||||
|
</div> |
@ -0,0 +1,36 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use yii\helpers\Inflector; |
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var yii\gii\generators\crud\Generator $generator |
||||||
|
*/ |
||||||
|
|
||||||
|
$urlParams = $generator->generateUrlParams(); |
||||||
|
|
||||||
|
echo "<?php\n";
|
||||||
|
?> |
||||||
|
|
||||||
|
use yii\helpers\Html; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var <?php echo ltrim($generator->modelClass, '\\'); ?> $model
|
||||||
|
*/ |
||||||
|
|
||||||
|
$this->title = 'Update <?php echo Inflector::camel2words(StringHelper::basename($generator->modelClass)); ?>: ' . $model-><?php echo $generator->getNameAttribute(); ?>;
|
||||||
|
$this->params['breadcrumbs'][] = array('label' => '<?php echo Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass))); ?>', 'url' => array('index'));
|
||||||
|
$this->params['breadcrumbs'][] = array('label' => $model-><?php echo $generator->getNameAttribute(); ?>, 'url' => array('view', <?php echo $urlParams; ?>));
|
||||||
|
$this->params['breadcrumbs'][] = 'Update'; |
||||||
|
?> |
||||||
|
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-update">
|
||||||
|
|
||||||
|
<h1><?php echo "<?php"; ?> echo Html::encode($this->title); ?></h1>
|
||||||
|
|
||||||
|
<?php echo "<?php"; ?> echo $this->render('_form', array(
|
||||||
|
'model' => $model, |
||||||
|
)); ?> |
||||||
|
|
||||||
|
</div> |
@ -0,0 +1,53 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
use yii\helpers\Inflector; |
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var yii\gii\generators\crud\Generator $generator |
||||||
|
*/ |
||||||
|
|
||||||
|
$urlParams = $generator->generateUrlParams(); |
||||||
|
|
||||||
|
echo "<?php\n";
|
||||||
|
?> |
||||||
|
|
||||||
|
use yii\helpers\Html; |
||||||
|
use yii\widgets\DetailView; |
||||||
|
|
||||||
|
/** |
||||||
|
* @var yii\base\View $this |
||||||
|
* @var <?php echo ltrim($generator->modelClass, '\\'); ?> $model
|
||||||
|
*/ |
||||||
|
|
||||||
|
$this->title = $model-><?php echo $generator->getNameAttribute(); ?>;
|
||||||
|
$this->params['breadcrumbs'][] = array('label' => '<?php echo Inflector::pluralize(Inflector::camel2words(StringHelper::basename($generator->modelClass))); ?>', 'url' => array('index'));
|
||||||
|
$this->params['breadcrumbs'][] = $this->title; |
||||||
|
?> |
||||||
|
<div class="<?php echo Inflector::camel2id(StringHelper::basename($generator->modelClass)); ?>-view">
|
||||||
|
|
||||||
|
<h1><?php echo "<?php"; ?> echo Html::encode($this->title); ?></h1>
|
||||||
|
|
||||||
|
<p> |
||||||
|
<?php echo '<?php'; ?> echo Html::a('Update', array('update', <?php echo $urlParams; ?>), array('class' => 'btn btn-danger')); ?>
|
||||||
|
<?php echo '<?php'; ?> echo Html::a('Delete', array('delete', <?php echo $urlParams; ?>), array(
|
||||||
|
'class' => 'btn btn-danger', |
||||||
|
'data-confirm' => Yii::t('app', 'Are you sure to delete this item?'), |
||||||
|
'data-method' => 'post', |
||||||
|
)); ?> |
||||||
|
</p> |
||||||
|
|
||||||
|
<?php echo '<?php'; ?> echo DetailView::widget(array(
|
||||||
|
'model' => $model, |
||||||
|
'attributes' => array( |
||||||
|
<?php |
||||||
|
foreach ($generator->getTableSchema()->columns as $column) { |
||||||
|
$format = $generator->generateColumnFormat($column); |
||||||
|
echo "\t\t\t'" . $column->name . ($format === 'text' ? '' : ':' . $format) . "',\n"; |
||||||
|
} |
||||||
|
?> |
||||||
|
), |
||||||
|
)); ?> |
||||||
|
|
||||||
|
</div> |
@ -0,0 +1,102 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yii\grid; |
||||||
|
|
||||||
|
use Yii; |
||||||
|
use Closure; |
||||||
|
use yii\helpers\Html; |
||||||
|
use yii\helpers\Inflector; |
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Qiang Xue <qiang.xue@gmail.com> |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
class ActionColumn extends Column |
||||||
|
{ |
||||||
|
public $template = '{view} {update} {delete}'; |
||||||
|
public $buttons = array(); |
||||||
|
public $urlCreator; |
||||||
|
|
||||||
|
public function init() |
||||||
|
{ |
||||||
|
parent::init(); |
||||||
|
$this->initDefaultButtons(); |
||||||
|
} |
||||||
|
|
||||||
|
protected function initDefaultButtons() |
||||||
|
{ |
||||||
|
if (!isset($this->buttons['view'])) { |
||||||
|
$this->buttons['view'] = function ($model, $column) { |
||||||
|
/** @var ActionColumn $column */ |
||||||
|
$url = $column->createUrl($model, 'view'); |
||||||
|
return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, array( |
||||||
|
'title' => Yii::t('yii', 'View'), |
||||||
|
)); |
||||||
|
}; |
||||||
|
} |
||||||
|
if (!isset($this->buttons['update'])) { |
||||||
|
$this->buttons['update'] = function ($model, $column) { |
||||||
|
/** @var ActionColumn $column */ |
||||||
|
$url = $column->createUrl($model, 'update'); |
||||||
|
return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, array( |
||||||
|
'title' => Yii::t('yii', 'Update'), |
||||||
|
)); |
||||||
|
}; |
||||||
|
} |
||||||
|
if (!isset($this->buttons['delete'])) { |
||||||
|
$this->buttons['delete'] = function ($model, $column) { |
||||||
|
/** @var ActionColumn $column */ |
||||||
|
$url = $column->createUrl($model, 'delete'); |
||||||
|
return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, array( |
||||||
|
'title' => Yii::t('yii', 'Delete'), |
||||||
|
'data-confirm' => Yii::t('yii', 'Are you sure to delete this item?'), |
||||||
|
'data-method' => 'post', |
||||||
|
)); |
||||||
|
}; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @param \yii\db\ActiveRecord $model |
||||||
|
* @param string $action |
||||||
|
* @return string |
||||||
|
*/ |
||||||
|
public function createUrl($model, $action) |
||||||
|
{ |
||||||
|
if ($this->urlCreator instanceof Closure) { |
||||||
|
return call_user_func($this->urlCreator, $model, $action); |
||||||
|
} else { |
||||||
|
$route = Inflector::camel2id(StringHelper::basename(get_class($model))) . '/' . $action; |
||||||
|
$params = $model->getPrimaryKey(true); |
||||||
|
if (count($params) === 1) { |
||||||
|
$params = array('id' => reset($params)); |
||||||
|
} |
||||||
|
return Yii::$app->getUrlManager()->createUrl($route, $params); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Renders the data cell content. |
||||||
|
* @param mixed $model the data model |
||||||
|
* @param integer $index the zero-based index of the data model among the models array returned by [[dataProvider]]. |
||||||
|
* @return string the rendering result |
||||||
|
*/ |
||||||
|
protected function renderDataCellContent($model, $index) |
||||||
|
{ |
||||||
|
$column = $this; |
||||||
|
return preg_replace_callback('/\\{(\w+)\\}/', function ($matches) use ($model, $column) { |
||||||
|
$name = $matches[1]; |
||||||
|
if (isset($column->buttons[$name])) { |
||||||
|
return call_user_func($column->buttons[$name], $model, $column); |
||||||
|
} else { |
||||||
|
return ''; |
||||||
|
} |
||||||
|
}, $this->template); |
||||||
|
} |
||||||
|
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue