1
hsalazar
Instructions on how to hack a block [Warning: looong post]
  • 2003/10/8 5:53

  • hsalazar

  • Just popping in

  • Posts: 78

  • Since: 2003/2/6 1


All:

For those avid xoopsers in search for a natural high, here's a slightly modified version of a post I made in Dr. Clone's site, this time in English for those who either don't speak Cervantes' language or don't visit Dr. Clone's site. The post was an answer to this query (please excuse my very free translation):
Quote:

Hi, everyone:
I know the answer to this has to be easy but I don't have a clue about PHP.
Those of you who use the phenomenal WF-Section will have seen that the blocks showing articles display the title and the date. I'd like to also show the author, just as in the index.php page of WF-Section.
Does anybody know how to do this...?


So, this is the (edited) answer... Let's say that it's not E-A-S-Y as in A-B-C, but neither is this too complicated (me and my big mouth). So let's take advantage of this opportunity to create this micro-tutorial we could call... ta-daaaa! "Instructions on how to hack a block". Ok? Well, then, grab yourself half a ton of patience and try to follow this logic.

(1) Let's begin assuming you've installed WF-Section verion 1.0.1. If you open xoops_version.php, you'll read in lines 68-74:

$modversion['blocks'][6]['file'] = "wfs_new.php";
$modversion['blocks'][6]['name'] = _MI_WFS_BNAME5;
$modversion['blocks'][6]['description'] = "Shows recent articles";
$modversion['blocks'][6]['show_func'] = "b_wfs_new_show";
$modversion['blocks'][6]['edit_func'] = "b_wfs_new_edit";
$modversion['blocks'][6]['options'] = "published|10|25";
$modversion['blocks'][6]['template'] = 'wfs_block_new.html';


(2) What does it tell us? It says that the content displayed through the template called wfs_block_new.html:

<ul>
  <{foreach 
item=wfs from=$block.new}>
    <
li><a href="<{$xoops_url}>/modules/wfsection/article.php?articleid=<{$wfs.id}>"><{$wfs.title}></a> (<{$wfs.new}>)</li>
  <{/foreach}>
</
ul>


is defined functionally in the file wfs_new.php, and that the function involved in this definition, the one in charge of retrieving the most recent articles, is the function called b_wfs_new_show.

(3) Armed with this fearsome knowledge, let's take a look at the said function (lines 29-56 of blocks/wfs_new.php):

function b_wfs_new_show($options) {
    global 
$xoopsDB;
    
$myts =& MyTextSanitizer::getInstance();
    
$block = array();
    
$sql "SELECT articleid, title, published, expired, counter, groupid FROM ".$xoopsDB->prefix("wfs_article")." WHERE published < ".time()." AND published > 0 AND (expired = 0 OR expired > ".time().") AND noshowart = 0 AND offline = 0 ORDER BY ".$options[0]." DESC";
    
$result $xoopsDB->query($sql,$options[1],0);
    while ( 
$myrow $xoopsDB->fetchArray($result) ) {
        
    if(
checkAccess($myrow["groupid"])) {    
        
$wfs = array();
        
$title $myts->makeTboxData4Show($myrow["title"]);
        if ( !
XOOPS_USE_MULTIBYTES ) {
            if (
strlen($myrow['title']) >= $options[2]) {
                
$title $myts->makeTboxData4Show(substr($myrow['title'],0,($options[2] -1)))."...";
            }
        }
        
$wfs['title'] = $title;
        
$wfs['id'] = $myrow['articleid'];
        if ( 
$options[0] == "published" ) {
            
$wfs['new'] = formatTimestamp($myrow['published'],"s");
        } elseif ( 
$options[0] == "counter" ) {
            
$wfs['new'] = $myrow['counter'];
        }
        
$block['new'][] = $wfs;
    }
    }
    return 
$block;
}


(4) As you can probably read in this code, we're retrieving an article's Id, its title, its publication date, its expiration date, its counter and the id of the group with read privileges over the item. No author here. So? Well, do we have any clue about the article's author in the table xoops_wfs_article? If we cheat a little and take a look at its structure, we'll soon find the table includes the user Id of the article's author (uid). This should serve our evil purpose well, hehe. <-- This is supposed to be a diabolical laugh...

(5) Ok. To retrieve the user's name, first we need the database query to retrieve also the field uid. In other words, we need the query to say:

$sql "SELECT articleid, uid, title, published, expired, counter, groupid FROM ".$xoopsDB->prefix("wfs_article")." WHERE published < ".time()." AND published > 0 AND (expired = 0 OR expired > ".time().") AND noshowart = 0 AND offline = 0 ORDER BY ".$options[0]." DESC";


(6) What next? Now we need to assign to a variable not the user Id, but the user's name. In order to do this, we'll execute a little "surgery" from another of Catzwolf's modules, WF-FAQ, and with the swiftness of experienced thiefs, we'll steal a very useful function called getLinkedUnameFromId. This is that function:

function getLinkedUnameFromId($userid)
     {
        
$userid intval($userid);
              if (
$userid 0) {
                
$member_handler =& xoops_gethandler('member');
                  
$user =& $member_handler->getUser($userid);
                      if (
is_object($user)) {
                          
$ts =& MyTextSanitizer::getInstance();
                          
$linkeduser "<a href='".XOOPS_URL."/userinfo.php?uid=".$userid."'>".$ts->htmlSpecialChars($user->getVar('uname')) ."</a>";
                          return 
$linkeduser;
                      }
            }
              return 
$GLOBALS['xoopsConfig']['anonymous'];
      }


(7) I suggest you paste this snippet into the file include/functions.php. Now, for the module to be able to access this function, you need to include in blocks/wfs_new.php, just after the first include_once, the following line:

include_once XOOPS_ROOT_PATH '/modules/wfsection/include/functions.php';


Now the module will know what to do when you use the function, which, as you can deduce from its syntax, is called with the user Id as parameter. What does the function return when called? The user's name, but not in plain old text, but with a hyperlink to the user's profile. Neat!

(8) So let's return to the file blocks/wfs_new.php. Just after the line $wfs['title'] = $title; let's add another line:

$wfs['author'] = getLinkedUnameFromId($myrow['uid']);


Now you'll see a sequence containing the following lines...

$wfs['title'] = $title;
        
$wfs['author'] = getLinkedUnameFromId($myrow['uid']);
        
$wfs['id'] = $myrow['articleid'];


(9) Logic says we're done. The function kicks all variables within the array $wfs, so in the template, where up to this point we were using <{$wfs.id}>, <{$wfs.title}> and <{$wfs.new}>, we can add now <{$wfs.author}>. And your template could say something like:

<ul>
  <{foreach 
item=wfs from=$block.new}>
    <
li><a href="<{$xoops_url}>/modules/wfsection/article.php?articleid=<{$wfs.id}>"><{$wfs.title}></a>, by <{$wfs.author}> (<{$wfs.new}>)</li>
  <{/foreach}>
</
ul>


(10) Initially I posted this sequence out of pure logic, with the utmost lack of good taste that's one of my personal trademarks. Of course there were bugs. Gentle readers made me aware of this, so I humbly ate crow, followed my own advice and hacked the block. Ahem. Had to do a little repair, ahem, a tiny repair, but eventually it worked, so now I can say this hack... works!

Dear xoopsers: if you have the curiosity and the means to follow these instructions, you'll not only be able to hack the block we've been talking about: you'll also learn a more or less general procedure to enjoy playing with XOOPS. And of course, there should be swifter ways, more elegant ways to accomplish the same result. If you know about one, please post it here so we can all benefit and the lesson will grow.

Cheers and best regards.

2
skalpa
Re: Instructions on how to hack a block [Warning: looong post]
  • 2003/10/8 6:06

  • skalpa

  • Quite a regular

  • Posts: 300

  • Since: 2003/4/16


It's quite nice you took the time to explain

Would be cool if you could copy that to the wiki so people can find it easily later.

Cheers

Skalpa.>

3
hsalazar
Re: Instructions on how to hack a block [Warning: looong post]
  • 2003/10/8 15:19

  • hsalazar

  • Just popping in

  • Posts: 78

  • Since: 2003/2/6 1



4
DFBH13
Re: Instructions on how to hack a block [Warning: looong post]
  • 2003/10/8 17:25

  • DFBH13

  • Just popping in

  • Posts: 61

  • Since: 2003/6/26


lol that must have took a while to Explain

Login

Who's Online

214 user(s) are online (107 user(s) are browsing Support Forums)


Members: 0


Guests: 214


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Mar 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits