Browse Source
			
			
			
			
				
		* master: (30 commits) Added SerialColumn to crud generated code. Added ActionColumn. crud generator WIP. Fixes #823: consistent interface naming Advanced application template: Delete flash message after it was displayed Fixes #901: Added $delete parameter to Session::getFlash(). Polished up the basic discussion of template alternatives Advanced application template: removed unused scenario from User model porting the fix from https://github.com/yiisoft/yii/pull/2894 Edited introduction Fixes #898: supported different signature of MemCache::addServer(). Fixes #897. Use str_replace() rather than implode-explode Fix parenthesis typo in CRUD index template Doing more editing... test break fix. Support ajax redirection. Enable CSRF validation by default. Supports more elements to use data-confirm and data-method attributes. refactored Request::validateCsrfToken(). Fixed CSRF validation bug. ...tags/2.0.0-beta
				 51 changed files with 727 additions and 340 deletions
			
			
		| @ -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); | ||||||
|  | 	} | ||||||
|  | } | ||||||
| @ -0,0 +1,39 @@ | |||||||
|  | <?php | ||||||
|  | 
 | ||||||
|  | namespace yiiunit\framework\web; | ||||||
|  | 
 | ||||||
|  | use Yii; | ||||||
|  | use yii\caching\FileCache; | ||||||
|  | use yii\web\CacheSession; | ||||||
|  | 
 | ||||||
|  | /** | ||||||
|  |  * @group web | ||||||
|  |  */ | ||||||
|  | class CacheSessionTest extends \yiiunit\TestCase | ||||||
|  | { | ||||||
|  | 	protected function setUp() | ||||||
|  | 	{ | ||||||
|  | 		parent::setUp(); | ||||||
|  | 		$this->mockApplication(); | ||||||
|  | 		Yii::$app->setComponent('cache', new FileCache()); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	public function testCacheSession() | ||||||
|  | 	{ | ||||||
|  | 		$session = new CacheSession(); | ||||||
|  | 
 | ||||||
|  | 		$session->writeSession('test', 'sessionData'); | ||||||
|  | 		$this->assertEquals('sessionData', $session->readSession('test')); | ||||||
|  | 		$session->destroySession('test'); | ||||||
|  | 		$this->assertEquals('', $session->readSession('test')); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	public function testInvalidCache() | ||||||
|  | 	{ | ||||||
|  | 		$this->setExpectedException('yii\base\InvalidConfigException'); | ||||||
|  | 
 | ||||||
|  | 		$session = new CacheSession(array( | ||||||
|  | 			'cache' => 'invalid', | ||||||
|  | 		)); | ||||||
|  | 	} | ||||||
|  | } | ||||||
					Loading…
					
					
				
		Reference in new issue