Best Way To Customize Cmenu

Hi everyone.

I’m building a custom CMenu like this, that I use instead of standard “/layouts/column2.php”. I’ve read different articles and discussions online, but I need a little help.

Here my example:




<?php /* @var $this Controller */ ?>

<?php $this->beginContent('//layouts/main'); ?>

<div class="span-19">

	<div id="content">

		<?php echo $content; ?>

	</div><!-- content -->

</div>

<div class="span-5 last">

	<div id="sidebar">

	<?php

		$this->beginWidget('zii.widgets.CPortlet', array(

			'title'=>'Operations',

		));

                

        $this->widget('zii.widgets.CMenu', array(

                       'items' => array(

                           array(

                               'label' => '',

                               'items' => array(

                                   array(

                                       'label' => 'New item',

                                       'url' => 'index.php?r=item/create'

                                   ),

                                   array(

                                       'label' => 'Update item',

                                       'url' => 'index.php?r=item/update&id='.[<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/huh.gif' class='bbc_emoticon' alt='???' />]

                                   ),

                                   array(

                                       'label' => 'Join item',

                                       'url' => [Other operation... (to do)]

                                   ),

                               )

                           ),

                       ),

                       'encodeLabel' => false,

                       'htmlOptions' => array(

                           'class'=>'nav pull-right',

                               ),

                       'submenuHtmlOptions' => array(

                           'class' => 'dropdown-menu',

                       )

                   ));

                

		$this->endWidget();

	?>

	</div><!-- sidebar -->

</div>



Basically, I have three questions.

  1. What is the best way to get id of “active” model. I can easily get it with Php instruction, using $_GET[‘id’], but I don’t like it, because I’m pretty sure there are better ways with Yii. Additionally, in future, url like “index.php?r=item/update&id=12” could be “item/update/12”, so I have to set it pretty well.

  2. I would to show some menu options, based on view. If I’m watching item list I would to show the “New item” option only, because id isn’t set. When I’m watching one item (id is set), selected from the preview list, I would have “new”, “edit” and every operation I can do on this item. Any idea?

Of course I can check before




if (isset($_GET['id'])) {

// print complete menu...

} else {

// print partial menu...

}



but I don’t think is great.

  1. I would to create a new operation like "Join item", ie join actual item (id=12) with another one that could be selected. How can I build this operation, step by step? Can you link me some article?

Very thanks.

Use $model->id instead of $_POST[‘id’]

I don’t understand then point 3? What is the purpose of joining, can you explain in more detail.

Hi. I’m talking about “\protected\views\layouts\my_menu.php”. I have to correct your post, because $_POST of course doesn’t work. $_GET is what we need.

However, $model->id doesn’t work too, because I’m calling $model in this layout script and I get error Undefined variable: model . How can I get this id, not using $_GET, but using Yii instruments?

I’ll explain point 3 later. I would solve point 1, first.

Thanks again

Ok. I explain 3.

My controlloer, contains public function actionCreate(), public function actionIpdate(), public function actionDelete(), public function actionIndex(). I can create a new record, edit it, delete it. Really standard.

What I have to do is another action to link items (records). So I would have another menu (lets say "Join") and associate an action like actionJoin() to create another record in another table (M to N relation). I hope you understand. However I would like to have some link with articles and/or discussions to read.

Hi,

you can send your $model from the controller to the view using the render method.

Maybe this topic can help you view