aztech
(Tomasz Suchanek)
April 25, 2010, 2:03pm
1
I’ve backed module ‘wksAdmin’. This module has several controllers. Each of it has admin action which looks very similar. The only difference is the list of fields displayed in CGridView. Therefore I decided to create admin action as child of CAction.
The problem is if I put working code from previously mentioned controller(s) into new CAction class it displays exactly the same code as defined as controllers method with one difference: module layout is not applied. Any ideas why?
aztech
(Tomasz Suchanek)
April 25, 2010, 6:22pm
2
I found solution for this. I need to add
$this->beginContent(); $this->endContent();
at beginning and at end of run method.
Unfortunately new problem comes!
When rendering CGridView assets are not published. The same is for
Yii::app()->clientscript->registerScript(...);
Vince
(Vadimg88)
April 25, 2010, 7:41pm
3
That’s because CAction does not have any rendering method. If you take a look at it’s documentation http://www.yiiframework.com/doc/api/CAction you’ll notice that. How do you apply the action to a controller? by overriding the actions() method?
aztech
(Tomasz Suchanek)
April 26, 2010, 6:52am
4
My action look following
class BasicAdminAction extends CAction
{
public $breadcrumbLabel = '';
public $activeMenuAtom = '';
public $title = '';
public $gridColumns = array( );
public $modelClassName = '';
public function run()
{
Yii::app()->getModule('wksAdmin')->layout = $this->getController()->layout;
$model = $this->getController()->createModel( 'search' );
if( isset( $_GET[$this->modelClassName] ) )
$model->attributes = $_GET[$this->modelClassName];
$this->render( $model );
}
/**
* Content generated by acyion
* @var CModel $model
*/
protected function render( CModel $model )
{
$this->getController()->beginContent();
$this->registerSearchScript( );
$this->getController()->title = $this->title;
#initialize breadcrumb
$this->getController()->breadcrumbs = array(
$this->breadcrumbLabel => array( $this->getId( ) ),
'zarządzaj',
);
#initialize menu
$this->getController()->menuConfig = array(
'activeAtom' => $this->activeMenuAtom,
'type' => CrudMenu::TYPE_ADMIN,
'model' => $model
);
$this->renderHelp( );
$this->renderSearch( $model );
$this->renderGrid( $model );
$this->getController()->endContent();
}
/* register script fto toogling advanced search link */
protected function registerSearchScript()
{
Yii::app()->getClientScript()->registerScript('search',
"$('.search-button').click(function(){
$('.search-form').toggle();
return false;
});
$('.search-form form').submit(function(){
$.fn.yiiGridView.update('lookup-grid', {
data: $(this).serialize()
});
return false;
});
");
}
/**
* Renders admin grid
* @var CModel $model
*/
protected function renderGrid( CModel $model )
{
$this->getController()->widget( 'zii.widgets.grid.CGridView' , array(
'id' => 'model-grid',
'summaryText' => 'Wyświetlam {start} - {end} z {count} wpisów',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => $this->gridColumns
) );
}
/* some other methods */
Instantiation of action in controller (note that layout property is defined in parent class WksAdminBasecController
<?php
class LookupController extends WksAdminBaseController
{
/* layout property is defined in parent class WksAdminBaseController */
/* model class name */
public $modelClassName = 'Lookup';
public function actions()
{
return array(
'admin' => array(
'class' => 'wksAdmin.controllers.actions.BasicAdminAction',
'breadcrumbLabel' => 'tabela przeglądowa',
'activeMenuAtom' => 'lookup',
'modelClassName' => $this->modelClassName,
'title' => 'Zarządzanie tabelą przeglądową',
'gridColumns' => array(
'id',
'type',
'position',
'name',
'code',
array(
'class' => 'CButtonColumn',
'header' => 'operacje',
'template' => '{update} {delete}',
) ) ) );
}
}