Change model function

Spiro

Member
Hi,

I have uploaded a theme and all seems to be going well. All the css, images, and views are beautifully preceding the default MW functionality.

I now am faced with a slightly more complex requirement. I would like a model function to take preference over the default function.

As an example. I want to make the GravatorUrl to have a difference default thumbnail. So, i need to change the getGravatarUrl function in \apps\customer\models\Customer. I obviously do not want to amend it there. I would like to amend it in my theme directory.

Firstly, is it possibly to only have that single function amended in the theme directory. Secondly, how do I call that from the theme run function. Is it something like: Yii::import('theme-mytheme.models.*');

I have checked out Landond and blue advanced theme for help with this, but am a bit stuck!
 
@Spiro - Just create your MyCustomer model file which extends the original Customer model and in it, place the getGravatarUrl method and change it as you wish. Then instead of calling the Customer model, call the MyCustomer model.
You can also use a helper class instead of extending the Customer model.

Secondly, how do I call that from the theme run function. Is it something like: Yii::import('theme-mytheme.models.*');
Yes, this is correct.
 
I assumed as much but it was throwing out an error for me: Property "CWebApplication.customerGravatar" is not defined.

This is my theme run function:
// Import Models
Yii::import('theme-mb-customer.models.*');

this is how i call it from the view:
<img src="<?php echo Yii::app()->customergravatar->getModel()->getAvatarUrl(90, 90);?>" class="img-circle"/>

and this is the new model file:
<?php defined('MW_PATH') || exit('No direct script access allowed');

class CustomerGravatar extends Customer
{

public function getGravatarUrl($size = 50)
{
$gravatar = sprintf('//www.gravatar.com/avatar/%s?s=%d&d=%s', md5(strtolower(trim($this->email))), (int)$size, 'http://example/customer/themes/mb-customer/assets/images/fire_asadthumddb_lrg.jpg');
return Yii::app()->hooks->applyFilters('customer_get_gravatar_url', $gravatar, $this, $size);
}

public function getAvatarUrl($width = 50, $height = 50, $forceSize = false)
{
if (empty($this->avatar)) {
return $this->getGravatarUrl($width);
}
return ImageHelper::resize($this->avatar, $width, $height, $forceSize);
}


}

--

I have a feeling it's something basic i have neglected :-$
 
@Spiro - the error is justified.
If you access Yii::app()->whatever, then whatever should be a component defined in the configuration file (main-custom.php or main.php)

Why not doing something super simple like creating a folder called helpers in your theme and load it:
PHP:
Yii::import('theme-mb-customer.helpers.*');
In the helpers folder have a file CustomerGravatarHelper.php with following contents:
PHP:
<?php

class CustomerGravatarHelper
{
    public static function getGravatarUrl(Customer $customer, $size = 50)
    {
        $gravatar = sprintf('//www.gravatar.com/avatar/%s?s=%d&d=%s', md5(strtolower(trim($customer->email))), (int)$size, 'http://example/customer/themes/mb-customer/assets/images/fire_asadthumddb_lrg.jpg');
        return Yii::app()->hooks->applyFilters('customer_get_gravatar_url', $gravatar, $customer, $size);
    }

    public static function getAvatarUrl(Customer $customer, $width = 50, $height = 50, $forceSize = false)
    {
        if (empty($customer->avatar)) {
            return self::getGravatarUrl($customer, $width);
        }
        return ImageHelper::resize($customer->avatar, $width, $height, $forceSize);
    }
}

Then call:
PHP:
<img src="<?php echo CustomerGravatarHelper::getAvatarUrl(Yii::app()->customer->getModel(), 90, 90);?>" class="img-circle"/>
 
Cool, tried that out but still getting an error:
Code:
Error 500!
Class 'CustomerGravatarHelper' not found
 
Did you place Yii::import('theme-mb-customer.helpers.*'); in the run() method of the theme?
 
I have a similar question. I also want to amend a widget (apps/customer/components/web/widgets/LeftSideNavigation), but don't want to overwrite the core files. How would i go about doing this?

Again, i might only want to amend one function. Should I do this with helpers too?
 
The LeftSideNavigationWidget is used in the main layout, so you can create your MyLeftSideNavigationWidget and copy the original code in it, alter it as you need to, then reference it in the main layout ;)
 
Back
Top