• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Blank avatars in my callback, wrong track?

gordy

Well-known member
#1
I've cobbled together bits and pieces from other code from this forum but I'm just getting blank avatars returned, so I'm apparently missing something.

What I have so far, the bits in green are added to my known working good code:

Code:
<?php
class PFvbench_vlist extends XenForo_Model
{
    public static function showVlist(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
    {
        $vbenchlists = XenForo_Application::get('db')->fetchAll('SELECT m.*, username, field_value
                FROM xf_user m LEFT JOIN xf_user_field_value p on p.user_id = m.user_id
                WHERE field_value = "vb3nch"
                ORDER BY username ASC');
 
[COLOR=GREEN]                $user = XenForo_Model::create('XenForo_Model_User');[/COLOR]
[COLOR=GREEN]                $me = $user->getUserById(1);[/COLOR]
 
                foreach ($vbenchlists AS &$vlists)
                {
                        $vlists = array(
                        'userid' => $vlists['user_id'],
                        'vname' => $vlists['username'],
[COLOR=GREEN]                      'avatar' => XenForo_Template_Helper_Core::callHelper('avatar', array($vlists, 'm', null, true)));[/COLOR]
 
                $response->params['vbenchlists'] = $vbenchlists;
                $response->templateName = 'vbench_list_template';
 
                }
    }
}
Here are the template bits:
Code:
<xen:foreach loop="$vbenchlists" value="$vlists">
[COLOR=GREEN] <xen:avatar user="{$vlists.userid}" size="s" img="true" />[/COLOR]<a href="/members/{$vlists.vname}.{$vlists.userid}/#vbench">{$vlists.vname}</a><br /><br />
</xen:foreach>
Any help in the right direction would be great!

thank you
 

Bob

Well-known member
#2
on your <xen:avatar user="{$vlists.userid}" change to this so it gets all the fields it needs.. <xen:avatar user="{$vlists}"
 

gordy

Well-known member
#3
on your <xen:avatar user="{$vlists.userid}" change to this so it gets all the fields it needs.. <xen:avatar user="{$vlists}"
Still blank, thank you Bob for looking in.

I've tried your suggestion and variants of it as well. I'm starting to think it has to be something with the call back. :confused:
 

Jake Bunce

XenForo moderator
Staff member
#4
xen:avatar needs standard fields like user_id, but you are overwriting those standard fields in your callback. You should preserve the standard user fields if you want to use user functions in the templates like xen:avatar.

I see you are also defining {$vlists.avatar} but then not using it. Try adding that variable to your template.
 

gordy

Well-known member
#5
I'm still getting blanks with this :/

The page is output is looking better but not showing an avatar

PHP:
<?php
class PFvbench_vlist extends XenForo_Model
{
    public static function showVlist(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
    {
        $vbenchlists = XenForo_Application::get('db')->fetchAll('SELECT m.*, username, field_value
                FROM xf_user m LEFT JOIN xf_user_field_value p on p.user_id = m.user_id
                WHERE field_value = "vb3nch"
                ORDER BY username ASC');
 
                $user = XenForo_Model::create('XenForo_Model_User');
                $me = $user->getUserById(1);
 
                foreach ($vbenchlists AS &$vlists)
                {
                        $vlists = array(
                        'userid' => $vlists['user_id'],
                        'userid2' => $vlists['user_id'],
                        'vname' => $vlists['username'],
                        'avatar' => XenForo_Template_Helper_Core::callHelper('avatar', array('userid2', 'm', null, true)));
 
                $response->params['vbenchlists'] = $vbenchlists;
                $response->templateName = 'vbench_list_template';
 
                }
    }
}
Here's my template:

PHP:
<xen:foreach loop="$vbenchlists" value="$vlists">
<li>
<xen:avatar user="{$vlists.avatar}" size="s" img="true" /><br />
<a href="/members/{$vlists.vname}.{$vlists.userid}/#vbench">{$vlists.vname}</a><br />
<br />
</li>
</xen:foreach>
The output :
http://www.planetfigure.com/pages/vbench/

I've got to be missing something!
 

Sadik B

Well-known member
#6
There are several things wrong with your code. Do you still need help?

1. You should be extending Xenforo_Model_User and not Xenforo Model
2. Your Model should return to the controller which should send the response to the template
3. Move the foreach loop to the template. Have two templates and get the first template include the second foreach loop.
4. Don't overwrite the key user_id in the user object that you send to your template, that way you will be able to use xen:avatar with the user object.



PHP:
<?php
class PFvbench_vlist_User extends XenForo_Model_User
{
    public static function showVlist(XenForo_ControllerPublic_Abstract $controller, XenForo_ControllerResponse_Abstract &$response)
    {
        $vbenchlists = XenForo_Application::get('db')->fetchAll('SELECT m.*, p.field_value
                FROM xf_user m, xf_user_field_value p
                WHERE p.user_id = m.user_id
                AND field_value = "vb3nch"
                ORDER BY username ASC');
 
               
                /*
                $user = XenForo_Model::create('XenForo_Model_User');
                $me = $user->getUserById(1); // Why do you have this?
 
               
                foreach ($vbenchlists AS &$vlists)
                {
                        $vlists = array(
                        'userid' => $vlists['user_id'],
                        'userid2' => $vlists['user_id'],
                        'vname' => $vlists['username'],
                        'avatar' => XenForo_Template_Helper_Core::callHelper('avatar', array('userid2', 'm', null, true)));
               
                $response->params['vbenchlists'] = $vbenchlists;
                $response->templateName = 'vbench_list_template';
 
                }
                */
               
                return $vbenchlists;
    }
}

In the Controller


PHP:
class PFvbench_vlist_User extends XFCP_PFvbench_vlist_User // Extend the Load class Controller event listener through your Listener
{
    public function actionvlist()
    {
        $vlistModel = $this->getModelFromCache('PFvbench_vlist_User');
        $vlist = $vlistModel->showVlist();
       
        return $this->responseView(
          'PFvbench_vlist_User', 'vbench_list_template',
            array('vlist'=>$vlist)
        );
    }
}
And in your template vbench_list_template


HTML:
<div class="section">
    <xen:if is="{$vlist}">
        <ol>
            <xen:foreach loop="$vlist" value="$vlist_item">
                <xen:include template="vbench_list_template_item" />
            </xen:foreach>
        </ol>
    </xen:if>
</div>

Then create a new template vbench_list_template_item with this code


HTML:
<li>
    <xen:avatar user="{$vlist_item}" size="s" img="true" />
    <br />
    <a href="/members/{$vlist_item.username}.{$vlist_item.user_id}/#vbench">{$vlist_item.username}</a><br />
    <br />
</li>
 

gordy

Well-known member
#7
There are several things wrong with your code. Do you still need help?
Sure do!

Thank you very much Sadik (y) I'm stumped at the controller part, do I create a new controller in library/XFCP/PFvbench/vlist/User ? I don't have an existing XFCP directory under /library .

Thank you again!
 

Sadik B

Well-known member
#8
Sure do!

Thank you very much Sadik (y) I'm stumped at the controller part, do I create a new controller in library/XFCP/PFvbench/vlist/User ? I don't have an existing XFCP directory under /library .

Thank you again!
Ok, i was not paying much attention to the class names and directory path. Follow this thread by Lawrence to understand what the listener should do and how the controllers can be extended -> http://xenforo.com/community/threads/creating-an-addon.5416/

I will be out for the day, so if u can't figure it out, I will post the paths and class names later when I get back based on your code.

And no, there is no XFCP directory, it stands for xenforo proxy class and is the method by which xenforo allows infinite extensions of classes. Follow the thread I linked.
 

gordy

Well-known member
#9
I will be out for the day, so if u can't figure it out, I will post the paths and class names later when I get back based on your code.
I'm getting my wires crossed, ;) Yes that would help, and may help others too :) Thank you
 

Sadik B

Well-known member
#11
Ok here goes, note that I don't really know what you want to do and these are just general guidelines but they should help you out.

1. UserCP->Development->Create the Addon from AdminCP Development. Let's say your Addon title is PFvbench
2. Now go to the library folder and create the folder in the library and name in PFvbench
3. Inside the PFvbench folder, create a php file and name Listener.php (Note the Camel Case in title)
4. Open Listener.php and put in this code and save

PHP:
<?
    class PFvbench_Listener
    {
        public static function extendControllers ($class, array &$extend)
        {
            if ($class== 'XenForo_ControllerPublic_Member')
                {
                    $extend[] = 'PFvbench_ControllerPublic_Member';
                }
        }
    }
5. Now go to AdminCP->Development->Code Event Listener->Create New Code Event Listener
6. Listen to Event load_class_controller, Execute Callback PFvbench_Listener Method extendControllers select your Addon PFvbench from dropdown and save.
7. Now inside PFvbench folder, create a new folder called vlist
8. Inside the folder vlist create a file called Member.php
9. Inside that file put in the code and save,

PHP:
class PFvbench_vlist_Member extends XFCP_PFvbench_vlist_Member
{
    public function actionvlist()
    {
        $vlistModel = $this->getModelFromCache('PFvbench_vlist_User');
        $vlist = $vlistModel->showVlist();
     
        return $this->responseView(
          'PFvbench_vlist_ViewPublic_Member ', 'vbench_list_template',
            array('vlist'=>$vlist)
        );
    }
}
10. Now inside the vlist folder, create a file called Model.php and paste in this code

PHP:
<?php
class PFvbench_vlist_User extends XenForo_Model_User
{
    public function showVlist()
    {
        $query = 'SELECT m.*, p.field_value
                FROM xf_user m, xf_user_field_value p
                WHERE p.user_id = m.user_id
                AND field_value = "vb3nch"
                ORDER BY username ASC';
return $this->fetchAllKeyed($query, 'user_id');
    }
}

Now the templates, in Master Style

vbench_list_template

HTML:
<div class="section">
    <xen:if is="{$vlist}">
        <ol>
            <xen:foreach loop="$vlist" value="$vlist_item">
                <xen:include template="vbench_list_template_item" />
            </xen:foreach>
        </ol>
    </xen:if>
</div>
vbench_list_template_item

HTML:
<li>
    <xen:avatar user="{$vlist_item}" size="s" img="true" />
    <br />
    <a href="/members/{$vlist_item.username}.{$vlist_item.user_id}/#vbench">{$vlist_item.username}</a><br />
    <br />
</li>
Now going to site.com/members/vlist should have your list.
 

gordy

Well-known member
#12
Incredible! Thank you very much for taking the time to spell it out for me, I really appreciate it and hopefully others as well.

Thanks again for the immense help, it opens up many other doors of things I want to do. (y)
 

gordy

Well-known member
#13
Yikes, I'm receiving an error:

Fatal error: Class 'PFvbench_ControllerPublic_Member' not found in /home/webhosts/devsite/htdocs/library/XenForo/Application.php(397) : eval()'d code on line 1

Is there already a Member class that might be clashing ?
 

Sadik B

Well-known member
#14
oops my bad (and this is what happens when copy pasting)

The Listener.php should be this

PHP:
<?
    class PFvbench_Listener
    {
        public static function extendControllers ($class, array &$extend)
        {
            if ($class== 'XenForo_ControllerPublic_Member')
                {
                    $extend[] = 'PFvbench_vlist_Member';
                }
        }
    }
 

gordy

Well-known member
#15
oops my bad (and this is what happens when copy pasting)

The Listener.php should be this
Got it ;) the Eval error was from the Member.php missing the "<?php"

No I'm really stumped with the newest error:

Code:
Argument 1 passed to XenForo_Controller::__construct() must be an instance of Zend_Controller_Request_Http, none given, called in /home/webhosts/planetfigure.com/htdocs/library/XenForo/Model.php on line 195 and defined
 
XenForo_Application::handlePhpError() in XenForo/Controller.php at line 81
XenForo_Controller->__construct() in XenForo/Model.php at line 195
XenForo_Model::create() in XenForo/Controller.php at line 101
XenForo_Controller->getModelFromCache() in PFvbench/vlist/Member.php at line 6
PFvbench_vlist_Member->actionvlist() in XenForo/FrontController.php at line 310
XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 132
XenForo_FrontController->run() in /home/webhosts/planetfigure.com/htdocs/index.php at line 13
 

Sadik B

Well-known member
#16
Strange... I will have to try this on my localhost. If I got this right, you are creating a custom profile field and selecting users who have value "vb3nch" for this custom profiel field, right?
 

gordy

Well-known member
#17
Strange... I will have to try this on my localhost. If I got this right, you are creating a custom profile field and selecting users who have value "vb3nch" for this custom profiel field, right?
Yes, that is correct, I needed some unique identifier for my members to elect to be shown or not (if that makes sense) ;)
 

Sadik B

Well-known member
#20
Here you go... :)

It was again a silly mistake on my part. We are naming the file Model.php and calling the class as PFvbench_vlist_User instead of PFvbench_vlist_Model

Also there is a slight mistake in the xen:avatar call in vbench_list_template_item, where instead of

<xen:avatar user="{$vlist_item}" size="s" img="true" />

it should be,

<xen:avatar user="$vlist_item" size="s" img="true" />

so I also noticed you probably wanted a link to username, so vbench_list_template_item should be,


HTML:
<li>
    <xen:avatar user="$vlist_item" size="s" img="true" />
    <br />
    <xen:username user="$vlist_item" />
    <br />
</li>
 

Attachments