Twitter
Wed
12
Nov
'08
PDF
Print
Send
 
Hits (3342) | Comments (5) | Favorited (1) | Votes (0)

This article for those who wants integrate the ability of restrict something in their components with Mighty Member.

This is very easy task that you can accomplish in 2 steps.

Step 1: Prepare restriction parameter.

The main idea for step 1 is to allow admin to chose what plan should restrict what action and save it.

First you need to provide to admin of your component ability to set what subscriptions restrict what. No matter if you have it as a component parameter, component configuration or component element parameters. You need to show list of available subscription plans. We use parameters for that. First you need to add element to any folder you like but better to JPATH_ADMINISTRATOR.DS.'elements' to do this please save code below in file /administrator/components/com_mycomponent/elements/jssubscriptionplanlist.php

<?php
defined('JPATH_BASE') or die();
class JElementJSSubscriptionPlanList extends JElement
{
    function fetchElement($name, $value, &amp;$node, $control_name)
    {
        if( !is_dir(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_jcs') ){
            return '<b>'.JText::sprintf('%sJomsuite Member%s is not installed on your 
                Joomla site', '<a href="index.php?option=com_resource
            &amp;view=article&amp;article=15&amp;Itemid=9">', '</a>').'</b>';
        }
        if( !is_array($value) ) {
            $value = array( $value );
        }
        $db = &amp;JFactory::getDBO();
        $query = "SELECT sp.id AS value, 
                         sp.name AS label, 
                         sp.catid, 
                         c.title AS cat_title
                    FROM #__jcs_plans  AS sp
               LEFT JOIN #__categories AS c ON c.id = sp.catid
                      WHERE sp.published = 1
                ORDER BY c.ordering, c.id, sp.name";
        $db->setQuery($query);
        $plans = $db->loadObjectList();
        $options[] = JHTML::_('select.option', '-1', JText::_( 'Everybody' ) );
        if( !$plans ) $plans = Array();
        $ccatid = '';
        foreach ( $plans as $key => $plan ) {
            if ( $ccatid != $plan->catid ) {
                $options[] = JHTML::_('select.optgroup', $plan->cat_title );
                $ccatid = $plan->catid;
            }
            $options[] = JHTML::_('select.option', $plan->value, $plan->label );
        }
        if( count( $options ) >= 11 ) {
            $size = 11;
        } else {
            $size = count( $options );
        }
        return JHTML::_('select.genericlist',   $options, 
            $control_name.'['.$name.'][]', 
            'size="'.$size.'" multiple="multiple"', 
            'value', 'text', $value );
    }
}
?>

This is ready param type. Now I will describe how to use it in params but you may use this code as base to find out how to create list of plans.

This is how you can use this param element.

<params addpath="/administrator/components/com_mycomponent/elements">
        <param name="plans" type="jssubscriptionplanlist"
            default="-1" label="Restrict Plans" description="" />
        <param name="count_mode" default="0" label="Count Mode">
             <option value="0">Do not count</option>
             <option value="1">Count URL Unique Access</option>
             <option value="2">Count Every Access</option>
        </param>
</params>

This param xml rendered through JParameter class will show list of plans. You may use it in your component parameters of component item parameters or anywhere.  User chose it and you save it somewhere.

Also not that wee need other parameter Count Mode to support subscription times access limitation.

You may approach  it differently but follow of main idea of step 1.

Step 2: Protection

To protect you need simple function that check if current user have one of the selected subscriptions active. And count if needed.

From Member v.2.3.1 member have api.php file and you do not need to care about functions. Your example code will looks like this

$API = JPATH_ROOT.DS.'components'.DS.'com_jcs'.DS.'api.php';
if(file_exists($API) &amp;&amp; $params->get('plans'))
{
  $res = JSMemberApi::jscCheckSubscription($params->get('plans'), 
    JText::_('You need subscription to access this page'), 
    $itemid, true, $params->get('count_mode'));
  if($res == 2) JSMemberApi::jscCountLimit($params->get('plans'), 
    $params->get('count_mode'))
}

jscCheckSubscription() will redirect to subscription plans or return FALSE if nothing to restrict. 1 if redirection off and user do not have subscription to access and 2 if youse have subscription.  So we use jscCountLimit() to increase subscription usage.


Comments
micahmitcAvatar
Quote
- -
Written by micahmitc VIP on 16 December 2008

Is this required? Meaning if I configure the JoomSuite Member component with a subscription that restricts access to something, do I have to do this to? Is that the idea?

-
SerhioAvatar
Quote
- -
Written by Serhio STAFF on 23 January 2009

This article for those who wants hardcode integration of Member into their component. So you as a Member user do not need to do anything of above.

-
avatar
Quote
- -
Written by George Jones on 02 December 2010

Is there a simple Protection code snippet that does nothing more than verifies that an exisiting logged in users id has a valid/non-expired subscription based on subscription_id?

The above code uses param settings for a component, and I simply need to hard code with IF Statements without need for a full component.

 

Thanks

-
GaboinkedAvatar
Quote
- -
Written by Gaboinked VIP on 02 December 2010

Adding to my question above, I am currently having to create a Joomla DB Query to perform these checks and would prefer to use the API for a cleaner usage.

I'm currently capturing the Logged in user's id and check the jos_jcs_user_subscr table for the user_id, subscription_id, and extime.  I then use a series of IF STATEMENTS for verification of a valid subscription, but I'd like to use the Member API for this if possible.  Can you assist with a sample code snippet that uses the API to accomplish the same thing I'm doing with DB calls?

Thanks

 

-
GaboinkedAvatar
Quote
- -
Written by Gaboinked VIP on 03 December 2010

Anyone there?

-
Add New Comment
Name:
Email:
Comment:
Attachment
Hide Comment
Security code:
Enter text as you see on image
 
 
What is best way for Download/Installation MightyExtensions?
 


Member Area



Member Activity

We have 1945 guests and 7 members online


MightyTemplate - Professional Joomla Templates