Quantcast
Channel: YouChew Community Blog List
Viewing all articles
Browse latest Browse all 480

tabull's Blog - Showing All Those Name Changes

$
0
0
Here's something you might not have noticed. Did you know that you can hover over a person's name to view their previous names? I did (albeit I made it, but that's besides the point). This was something I made after noticing a lot of people changing their names and trying to keep up with those changes proved difficult. However, once the feature was done, I never mentioned it since it was pretty much just a small change.

Another thing about wanting to make it was I didn't want to see all the event-only name changes, because what kind of fun is it to go on someone's page to view their previous names only to see this:

Spoiler


Since IP.Board stores all the name changes and the dates they were done, all that would need to be done is filter out any name change that lasted less than some period of time, say a week. And since we can use hooks to place the name without changing the core code-base, the results can be placed somewhere convenient, say the member hover card.

That's where this bit of code comes into play:

<?php
class previous_names_hover_card
{

    protected $registry;
    protected $DB;
    protected $memberData;
    
    public function __construct()
    {
        /* Make registry objects */
        $this->registry   =  ipsRegistry::instance();
        $this->DB = $this->registry->DB();
        $this->memberData =& $this->registry->member()->fetchMemberData();
    }
    
    public function getOutput()
    {
        if(!$this->memberData['member_id'] || $this->memberData['member_banned'])
            return '';
                        
        if(is_array($this->registry->output->getTemplate('profile')->functionData['showCard']) && count($this->registry->output->getTemplate('profile')->functionData['showCard']))
        {
            $member = IPSMember::load($this->registry->output->getTemplate('profile')->functionData['showCard'][0]['member']['member_id']);
            
            $memberID = intval($member['member_id']);
            
            if($memberID)
            {
                $names = array();
            
                $this->DB->build(   array(  'select'    =>  'dname_previous, dname_current, dname_date',
                                            'from'      =>  'dnames_change',
                                            'where'     =>  "dname_member_id = $memberID",
                                            'order'     =>  'dname_date ASC' ));
                                            
                $previousDate = $member['joined'];
                
                $e = $this->DB->execute();
                
                while($r = $this->DB->fetch($e))
                {
                    
                    if($r['dname_date'] - $previousDate > 604800 /* 1 week */ 
                        && $r['dname_previous'] != $member['members_display_name']
                        && !in_array($r['dname_previous'], $names)) 
                    {
                        $names[] = $r['dname_previous'];
                    }
                    
                    $previousDate = $r['dname_date'];
                }
                
                if(count($names) > 0)
                {
                    $names = implode(', ', $names);
                    return "<dt>Previous Names</dt><dd>$names</dd>";
                }           
            }           
        }       
        return '';
    }
}
I already went into some detail about hooks and constructors here, so I won't go into what __construct() and getOutput() are used for. However, I will say that whatever is returned in getOutput() will be placed depending on the hook's key, which is also discussed in the linked blog post. So, what should be returned exactly? If you take your favorite html viewer such as Firebug, you would see that the hover card utilizes a good old definition list, so that's what will be used. Between the <dt> tags will be what it we're defining (which will be "Previous Names") and between the <dd> tags will be a comma separated list of all the person's names.

So, now that that is figured out, all that needs to be done is determining which of all the previous names we really care about. Certainty there's no need to output the current username, so a check should be made before adding it to the list of names. The other check should be to take the previous time the person had a name change (or the join date if looking the very first name change) and if it's less than a week, don't add it to the list of names. Finally, if the person changed their name to something they had previously, obviously, there's no need to display that name twice, so a check should be done to ensure that doesn't happen.

Thus, after determining the member whose card we just hovered over thanks to the skin's function data, we should query the database will all the name changes that person had in ascending order based on the name change date. After that, go through all of the name changes and make our checks. Did the name change last longer than a week or 604800 seconds ($r['dname_date'] - $previousDate > 604800)? Is the name change different from the current one being used ($r['dname_previous'] != $member['members_display_name'])? Is the name not in the list of names (!in_array($r['dname_previous'], $names))? If yes to all of these, then we can add the name to our list of names and set the previous date to the name change date we are currently on and look at the next name.

After that is done, we have an array of names that we'd like to display, so let's display them. However, it's an array, not a string, so let's convert that with php's implode function. All that does it takes an array and puts a string in between each element, in this case a comma and a space to make a comma separated list and outputs the concatenated string. Throw that string between some <dd> tags and return the html we just created.

And voilà.


Posted Image

Now, isn't that much better?


Viewing all articles
Browse latest Browse all 480

Trending Articles