2

I'm managing a Joomla website that uses DJ Classifieds as the main component; the 'ads' are actually resumes, and employers buy a subscription to access an ad's details.

In the past, users who didn't have a subscription were redirected to a sign up blurb (an article) when they clicked on the listing's title; users who did have an active subscription were taken to the ad's details page. This process was handled by OS Membership, which added users to an 'authorised' user group upon purchasing a subscription, and in DJ Classifieds, I set access to ad details to that same user group. It all worked perfectly.

The problem was, though, that the browser was creating 301 redirects; any user who clicked on an ad BEFORE buying a subscription was redirected to the sign up blurb but, then even after they bought a subscription, the browser continued to redirect them, since a 301 had been created. (that's my layman's understanding of it)

The component's developer fixed this by adding a 'no-cache' 'must re-validate' line of code and the problem was solved. However, it also meant I couldn't cache anything and now the site is slow.

And so, I had a better idea but I can't quite make it work.

I'm trying to add some code that does the following:

if the user belongs to 'authorised' group {
    show ad title with link to ad detail
} else {
    show ad title with link to sign up page
}

It sounds easy, but the hard part is knowing how to use apostrophes and quotations; the code from the PHP file is below and it already contains PHP snippets, so my question is, how can I write this to make it work?

=== my idea ===

At top of the php file:

$user = JFactory::getUser();

In the page itself:

<th style="" class="name first <?php echo $class; ?>">
    <?php } ?>
    $group_id = 3;
    if(in_array($group_id, $user->getAuthorisedGroups()))
    { 
        //when the user is in the authorised group

        echo '<a <?php echo $title_a_style;?> class="<?php echo $class; ?>" href="index.php?option=com_djclassifieds2&view=items&cid=<?php echo $main_id; ?>&order=title&ord_t=<?php echo $ord_t.'&Itemid='.$Itemid;?><?php echo $se_link;if($uid){ echo '&uid='.$uid; }?>">
        <?php echo JText::_('COM_DJCLASSIFIEDS2_TITLE');
        if($order=="title"){
            if($ord_t=='asc'){
                echo '<img src="'.JURI::base().'/components/com_djclassifieds2/assets/images/sort_desc.gif" alt="" />';
            }else{
                echo '<img src="'.JURI::base().'/components/com_djclassifieds2/assets/images/sort_asc.gif" alt="" />';
            }
        }else{
            echo '<img src="'.JURI::base().'/components/com_djclassifieds2/assets/images/sort.gif" alt="" />';
        }
    ?>
    </a>;'
    }else{
        echo '<a <?php echo $title_a_style;?> class="<?php echo $class; ?>" href="/link-to-signup-article">
        <?php echo JText::_('COM_DJCLASSIFIEDS2_TITLE');
        if($order=="title"){
            if($ord_t=='asc'){
                echo '<img src="'.JURI::base().'/components/com_djclassifieds2/assets/images/sort_desc.gif" alt="" />';
            }else{
                echo '<img src="'.JURI::base().'/components/com_djclassifieds2/assets/images/sort_asc.gif" alt="" />';
            }
        }else{
            echo '<img src="'.JURI::base().'/components/com_djclassifieds2/assets/images/sort.gif" alt="" />';
        }
    ?>
    </a>;'
    ?>
</th>

I feel embarrassed knowing so little about the above, and I've tried all different permutations but I don't know how to use apostrophes and quotations within the query itself.

Can anyone help me make this work?

2

2 Answers 2

2

Welcome to JSE. Please do not feel embarassed. We're all here to help and remember we were all in your shoes once....sometimes I find myself there these days :)

Anyway as for the code, you had some general PHP issues which would have most likely resulted in fatal errors, such as quotes, double usage of <?php ?>, etc.

I've rewritten your code in a more simplistic way and also added some inline comments to help show you the process:

<th style="" class="name first <?php echo $class; ?>">
<?php
    $group_id = 3;

    // Lets get the image
    // Define the default image
    $img = 'sort.gif';

    if ($order === 'title')
    {
        $img = 'sort_asc.gif';

        if ($ord_t === 'asc')
        {
            $img = 'sort_desc.gif';
        }
    }

    // This is the final image path that will be used
    $img = '<img src="' . JUri::base() . '/components/com_djclassifieds2/assets/images/' . $img . '" alt="">';

    // Now lets do the link
    //This is the default one
    $link = '/link-to-signup-article';

    // When the user is in the authorised group, we use a different link
    if (in_array($group_id, $user->getAuthorisedGroups()))
    {
        $link = 'index.php?option=com_djclassifieds2&view=items&cid=' . $main_id . '&order=title&ord_t=' . $ord_t . '&Itemid=' . $Itemid . $se_link;

        // If the link has a "uid", add it to the rest of the link
        if ($uid)
        {
            $link .= '&uid=' . $uid;
        }
    }

    echo '<a ' . $title_a_style . ' class="' . $class . '" href="' . $link . '">' . JText::_('COM_DJCLASSIFIEDS2_TITLE') . $img . '</a>';
?>
</th>

I was a little unsure about the $title_a_style and $se_link variables, as they were being used in your code, but I don't know the where they're being defined.

Hope this helps

3
  • Thank you, Lodder! That method seems way more logical than what I was envisioning, thank you so much! I just added into the default.php file, in place of the old <th> but it breaks the page :( I'm not sure why that is. It's a '500' error. I opened the inspector and there's no information as to why. Regarding the above, the references to images aren't important as I don't use them on that particular page, so if they are causing problems, they could simply be removed. Do you have any ideas as to why the above would cause a problem? Thank you again, you made my day!
    – ben goldie
    Oct 12, 2017 at 11:16
  • @bengoldie - In your Joomla Global Configuration, set error reporting to development then visit your site again. If not PHP errors are displays, check your PHP error log and report any errors back here.
    – Lodder
    Oct 13, 2017 at 10:29
  • Thank you, Lodder, I finally did it! Please see the answer posted below!
    – ben goldie
    Oct 16, 2017 at 23:18
1

@Lodder -- thank you, Lodder, you gave me the code I needed to make this work.

I had been altering the wrong lines; I toiled for hours and finally realized the fact, so it ended up being much easier than expected to add your code to the file. It now looks like this:

$group_id = 12;

if (in_array($group_id, $user->getAuthorisedGroups()))
{
    echo '<h3><a class="title" href="'.DJClassifieds2SEO::getItemRoute($i->id.':'.$i->alias,$i->cat_id.':'.$i->c_alias).$anch.'" >'.$i->name.'</a></h3>';
} else {
    echo '<h3><a class="title" href="/signup" >'.$i->name.'</a></h3>';
}

I did a little research online and added the else part, it seems to work fine!

I'm so happy (and proud of myself) -- but @Lodder made it happen, and I'm eternally grateful. Thank you again for your support @Lodder, you're a star!

1
  • Glad you finally got it sorted out Ben :) And best of luck with the rest of your site. Feel free to post any more questions, should you run into any more issues.
    – Lodder
    Oct 16, 2017 at 23:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.