Defining a CJuiDatePicker creator

Hello everyone,

I’ve seen that I always make the same modifications to the CJuiDatePicker attributes, so I’d like to define a function that I can call to create a CJuiDatePicker in my views. I’d like to be able to use something like this, in my views:


<?php MyGeneralClass::createDatePicker('myDatePickerName', $myDatePickerValue); ?>

so I tried defining this class in my "models" folder:


<?php

class MyGeneralClass

{

	public static function createDatePicker($name, $value)

	{

		return $this->widget('zii.widgets.jui.CJuiDatePicker', array(

		    'name'=>$name,

		    'value'=>$value,

		    // etc...

		));

	}

}

?>

But, “$this” can’t be used there, so I tried receiving the “$this” reference, this way:


<?php

class MyGeneralClass

{

	public static function createDatePicker($name, $value, $owner)

	{

		return $owner->widget('zii.widgets.jui.CJuiDatePicker', array(

		    'name'=>$name,

		    'value'=>$value,

		    // etc...

		));

	}

}

?>

And it’s working calling the function this way:


<?php MyGeneralClass::createDatePicker('myDatePickerName', $myDatePickerValue, $this); ?>

But I’m not sure about passing the whole “$this” reference from my views all the time. Is there a way to avoid this? Is there a way to create a CJuiDatePicker in my views without sending the whole “$this” reference to my class (model)?

Your help is appreciated. Thank you in advance.

Studying the source code, I think you can use Yii::app()->getController(). That’s in fact what will be used in the end, in case no owner is passed to the CWidget constructor.

Edit: But that’s of course the same object as $this refers to. You may try a freshly created instance of CBaseController instead. (BTW There’s no reference at all to $this->_owner inside CJuiDatePicker)

(untested)

/Tommy

Thank you very much Tommy. Your suggestion seems to work fine.

The function was defined this way:


<?php

class MyGeneralClass

{

        public static function createDatePicker($name, $value)

        {

		$controller = Yii::app()->getController();

                return $controller->widget('zii.widgets.jui.CJuiDatePicker', array(

                    'name'=>$name,

                    'value'=>$value,

                    // etc...

                ));

        }

}

?>

And “$owner” was just the name of the parameter of my custom function. I didn’t expect it to be defined inside CJuiDatePicker.

I may try later your suggestion of creating an instance of CBaseController instead.

Thanks again!