Printable Membership Card - PHP implementation

2 years 1 month ago - 2 years 1 month ago #328295 by MarylandShallIssue
Printable Membership Card - PHP implementation was created by MarylandShallIssue
Hello,

The below article was a great entry point in regards to generating membership cards - but since I'm developing my own membership card in PHP, unlike OP who used a Joomla article, there's some serious gaps to fill:

www.joomlapolis.com/forum/255-developer-members-support/243473-printable-membership-card

I am able to generate a card and display it on the page but the route I took was unorthodox, adds a delay to page load, and I would like my code to be more in-line with Joomla's environment.

Here's a summary of where I'm at so far:
  • My attempt at writing a Code Action failed. I could not find documentation/examples of how this could work.
  • I ended up writing some stand-alone functions in a PHP file, outside the public html folder, with the proper header:
    defined('_JEXEC') or die('Restricted access');
  • On the user account page, I simply include the PHP file/call a function, and echo the returned base64 image, via sourcerer.
  • Since this is called within Joomla, my functions have access to CBuser and cbpaidUserExtension
  • The membership card generates - but the page waits for the server to render the image.

At this point V1 of this card is ok. I could use the following for V2:
  • Whichever solution I go with, it must not prevent page load while the image generates
  • My first instinct here is to rewrite this as a Joomla MVC component - is this overkill? If not, I wouldn't mind pointers here.
  • Could you please provide an example of how CB Auto Actions would call my code to generate the image?
  • Lastly, I could not find any API documentation on the CB Paid Subscriptions Add-On:
  1. Is there API documentation of CB / CB Add-Ons? Integrating code would be much easier with this.
  2. I wrote the following to get the user's expiration date. Is this the correct way to go about it?
    function get_user_exp_date($member_id)
    {
        $subs = cbpaidUserExtension::getInstance($member_id)->getUserSubscriptions();
        $exp_dates = array_map(function ($sub) {
    	    return date_create($sub->expiry_date);
        }, $subs);
        return max($exp_dates);
    }

Please Log in to join the conversation.

2 years 1 month ago - 2 years 1 month ago #328303 by krileon
A Code action in CB Auto Actions would be the best approach. It just needs to set the proper headers for being an image. You'd then just use the auto actions URL in the img element as the src. You absolutely don't need to write and entire component for this. Below is a simple example that outputs a PNG image.

Global
Triggers: None
Type: Code
User: Automatic
Access: Everybody
Action
Method: PHP
Code:
// Create a 100x30 image
$im = imagecreate(100, 30);

// White background and blue text
$bg = imagecolorallocate( $im, 255, 255, 255 );
$textcolor = imagecolorallocate( $im, 0, 0, 255 );

// Write the string at the top left
imagestring( $im, 5, 0, 0, 'Hello world!', $textcolor );

// Output the image
header( 'Content-Type: image/png; charset=utf-8' );

imagepng( $im );

exit();

That will output a 100x30 image image with a white background and blue text saying "Hello world!". The URL under the Global tab can be used as the img src. To have it render specific users other than yourself be sure to add &users=[user_id] when using this in a Custom HTML field or anywhere that supports substitutions.

Is there API documentation of CB / CB Add-Ons? Integrating code would be much easier with this.
I wrote the following to get the user's expiration date. Is this the correct way to go about it?

You should be able to just use CBSubs content plugin functionality for that. Anywhere Joomla content plugins are supported you can use the following substitution usages.

[cbsubs:subscriptions plan="PLAN_ID_HERE" output="expiry_date" /]

Replace PLAN_ID_HERE with the ID of your plan. See the description of the "cbpaidsubsbot" system plugin in Extensions > Plugins for further usage information for this content plugin.


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

2 years 1 month ago - 2 years 1 month ago #328337 by MarylandShallIssue
Replied by MarylandShallIssue on topic Printable Membership Card - PHP implementation
Thank you, that worked splendidly - very few changes needed to get exactly what I was describing before. I did restrict access to registered users though fyi.

To have it render specific users other than yourself be sure to add &users=[user_id] when using this in a Custom HTML field or anywhere that supports substitutions.

I tried adding that to the end of the auto action URL in a new tab; it still generated the card for my own user. It might be because I access user via CBuser::getMyUserDataInstance() .

You should be able to just use CBSubs content plugin functionality for that. Anywhere Joomla content plugins are supported you can use the following substitution usages.

[cbsubs:subscriptions plan="PLAN_ID_HERE" output="expiry_date" /]


I don't think that would work for me. I need the subscription expiration date generated in PHP onto the card image. The user might have multiple subscriptions, with only one being active; the function I wrote above was able to figure out which arbitrary subscription has the longest expiration date. It works great, just thought it was a bit tricky figuring out where this data was tucked away.

Please Log in to join the conversation.

2 years 1 month ago - 2 years 1 month ago #328343 by krileon

I tried adding that to the end of the auto action URL in a new tab; it still generated the card for my own user. It might be because I access user via CBuser::getMyUserDataInstance() .

I assume you're outputting the URL in a new tab via a Custom HTML field? The below is an example of what the URL should look like in a Custom HTML field.

index.php?option=com_comprofiler&view=pluginclass&plugin=cbautoactions&action=action&actions=ACTION_ID&users=[user_id]

That should tell the auto action to use the user id in the URL, which should be that of the profile being displayed due to the substitution.

I don't think that would work for me. I need the subscription expiration date generated in PHP onto the card image. The user might have multiple subscriptions, with only one being active; the function I wrote above was able to figure out which arbitrary subscription has the longest expiration date. It works great, just thought it was a bit tricky figuring out where this data was tucked away.

CB Auto Actions supports substitutions and content plugins directly inside of it. Simply turn content plugins on under Parameters and that usage will work in your code that's responsible for generating your image. Just be sure to always treat substitutions and content plugin responses as strings. Example as follows would work.

$expiryDate = '[cbsubs:subscriptions plan="PLAN_ID_HERE" output="expiry_date" /]';


Kyle (Krileon)
Community Builder Team Member
Before posting on forums: Read FAQ thoroughly + Read our Documentation + Search the forums
CB links: Documentation - Localization - CB Quickstart - CB Paid Subscriptions - Add-Ons - Forge
--
If you are a Professional, Developer, or CB Paid Subscriptions subscriber and have a support issue please always post in your respective support forums for best results!
--
If I've missed your support post with a delay of 3 days or greater and are a Professional, Developer, or CBSubs subscriber please send me a private message with your thread and will reply when possible!
--
Please note I am available Monday - Friday from 8:00 AM CST to 4:00 PM CST. I am away on weekends (Saturday and Sunday) and if I've missed your post on or before a weekend after business hours please wait for the next following business day (Monday) and will get to your issue as soon as possible, thank you.
--
My role here is to provide guidance and assistance. I cannot provide custom code for each custom requirement. Please do not inquire me about custom development.

Please Log in to join the conversation.

Moderators: beatnantkrileon
Time to create page: 0.438 seconds

Facebook Twitter LinkedIn