Newscoop 4 Cookbook

Subscriptions, registration and content access

Newscoop templates can be constructed in a way that they deliver content depending on the status of the user (logged in or not) and their subscription (does the user have access to this article, section, or language?)

If you decide to use subscriptions, this chapter explains how control access to the content, and how to set up the registration process and forms. In a previous chapter we discussed the strategy of using subscriptions in your publication. Further below, you can find a quick introduction to managing subscriptions through the administration interface.

Using a subscription mechanism allows you to manage content access:

  • Some content is accessible only to those users who are logged in
  • An additional requirement for paid subscriptions may be that the reader has to have a valid subscription for the content they are attempting to access

Journalists and editors then need to switch off the 'Visible to non-subscribers' option on the article edit screen, in order to restrict access to the article.

Controlling content and functionality access

You can check if the reader is logged in with $gimme->user->logged_in:

{{ if $gimme->user->logged_in }}   {{ include file="_tpl/article-comments.tpl" }} {{ else }}   Register and log in to comment {{ /if }} 

You can check if the user has access to content with $gimme->article->content_accessible. What you do with this information is up to you. You can still deliver the same content if you want, or you can only deliver parts of it. Here are two examples to illustrate this. First, truncate the article for readers who are not allowed access yet:

{{ if $gimme->article->content_accessible }}   {{ include file="_tpl/article-fullcontent.tpl" }} {{ else }}    {{ $gimme->article->full_text|truncate:300 }}    Buy a subscription today to get full access! {{ /if }} 

Or display relevant information in classified ads to readers who have subscribed, like this:

{{ $gimme->article->ad_text }}<br/>Contact: {{ if $gimme->article->content_accessible }}   <a href="mailto:{{ $gimme->article->ad_email }}">{{ $gimme->article->ad_email }}</a> {{ else }}   Subscribe for contact information. {{ /if }} 

Registration, login and subscription templates

Your job gets much more complicated if you need to provide the entire functionality circle:

  • Login option for your publication's existing subscribers
  • Register option for new subscribers
  • Account page for users to check or change their personal information, including changing their password
  • Option to resend lost or forgotten passwords
  • Logout option

If you're also working with paid/trial subscriptions, it's not enough for the reader just to be registered; they may be required to have a valid subscription to some, or all, sections or publications. You will need to develop functionality which:

  • ensures that the reader is able to choose what they want to subscribe to (which language if more than one is available; which sections; which publications)
  • gives the reader information on what they can choose, and what the price will be

Login and Registration Box

The first thing that a reader needs is the option to login or register. This can be displayed in a box placed somewhere near the top of your publication's pages.

The Newscoop template for this is:

<div class="logintop"> {{ if ! $gimme->user->logged_in }}     {{ login_form submit_button="login" }}         <p class="fields"><a href="{{ uri options="template register.tpl" }}">Register</a> | {{ if $gimme->login_action->is_error }}<span >{{ $gimme->login_action->error_message }}!</span>{{ else }}Sign in:{{ /if }}         <label for="uname"></label> {{ camp_edit object="login" attribute="uname" html_code="value=\"username\"" }}         <label for="uname"></label> {{camp_edit object="login" attribute="password" html_code="value=\"password\"" }}     {{ /login_form }}</p> {{ else }}<form><p class="fields">Welcome, <a href="{{ uri options="template register.tpl" }}">{{ $gimme->user->name }}</a> | <a href="?logout=true">logout</a></p></form> {{ /if }} </div><!-- /.login-top --> 

This template will check to see if the user is already logged in, and if they are, it will display 'Welcome, <Full Name>', where Full Name is also a link to the page where the reader can review their personal information. Also, an option for logging out is displayed.

Logging out works by generating a new URL parameter when the link is clicked:

<a href="?logout=true">logout</a> 

This will reload the current page, but the hook is in the _html-head.tpl template. When this parameter in the URL is provided, the currently logged-in user is logged out. Here is the code:

{{ if $gimme->url->get_parameter('logout') == 'true' }} <META HTTP-EQUIV="Set-Cookie" CONTENT="LoginUserId=; path=/"> <META HTTP-EQUIV="Set-Cookie" CONTENT="LoginUserKey=; path=/"> {{ $gimme->url->reset_parameter('logout') }} <META HTTP-EQUIV="Refresh" content="0;url={{ uri }}"> {{ /if }} 

Now, for some more serious stuff: If the user is not logged in, this template provides the options for logging in or registering. Registering calls the register.tpl template (the same one is used for modifying an existing reader's data).

Registration Template register.tpl

First, here's the whole code:

{{ if !$gimme->edit_user_action->defined && !$gimme->edit_subscription_action->defined }}     {{ include file="_tpl/user-form.tpl" }} {{ /if }}  {{ if $gimme->edit_user_action->defined && $gimme->edit_user_action->is_error }}     <h5>Error registering your account: {{ $gimme->edit_user_action->error_message }}</h5>     {{ include file="_tpl/user-form.tpl" }} {{ /if }}  {{ if $gimme->edit_user_action->defined && $gimme->edit_user_action->ok }}     {{ if $gimme->edit_user_action->type == "add" }}         <h5>Your user account was successfully added. You will soon receive a confirmation about how to access your subscription.</h5>     {{else }}         <h5>User data successfully added/modified</h5>     {{ /if }} {{ /if }} 

The code should be self-explanatory - it basically checks whether it should load the user form immediately (if no previous action was taken, it means that the reader wants to perform a new account registration), and additionally it checks to see if an error was generated in the registration process. Finally, if the previous action was a successful registration or account modification, the template will send the appropriate message.

Registration and Profile Form: user-form.tpl

The template called in the process of registration or account modification is user-form.tpl. We're using a table in this case because it's faster to make, but you can create your own CSS-based solution if you prefer:

{{ user_form template="subscription.tpl" submit_button="submit" }} <p style="margin: 15px 0">Please fill in the following form in order to create the subscription account.</p>     <table id="contact" cellspacing="0" cellpadding="0">         <tr>             <td><label for="contact-name">Full name:</label></td>             <td>{{ camp_edit object="user" attribute="name" html_code="class=\"tablefield widerone\" id=\"contact-name\"" }}</td>         </tr>         <tr>             <td><label for="contact-email">E-mail:</label></td>             <td>{{ camp_edit object="user" attribute="email" html_code="class=\"tablefield widerone\" id=\"contact-email\"" }}</td>         </tr>         <tr>             <td><label for="contact-uname">Username:</label></td>             <td>{{ camp_edit object="user" attribute="uname" html_code="class=\"tablefield widerone\" id=\"contact-uname\"" }}</td>         </tr> {{ if ! $gimme->user->logged_in }}         <tr>             <td><label for="contact-password">Password:</label></td>             <td>{{ camp_edit object="user" attribute="password" html_code="class=\"tablefield widerone\" id=\"contact-password\"" }}</td>         </tr>         <tr>             <td><label for="contact-passwordagain">Password (again):</label></td>             <td>{{ camp_edit object="user" attribute="passwordagain" html_code="class=\"tablefield widerone\" id=\"contact-passwordagain\"" }}</td>         </tr> {{ /if }}         <tr>             <td><label for="contact-city">City, Country:</label></td>             <td>{{ camp_edit object="user" attribute="city" html_code="class=\"tablefield widerone\" id=\"contact-city\"" }}</td>         </tr>         <tr>             <td><label for="contact-phone">Phone:</label></td>             <td>{{ camp_edit object="user" attribute="phone" html_code="class=\"tablefield widerone\" id=\"contact-phone\"" }}</td>         </tr>         <tr>             <td><label for="second_phone">Phone (cell):</label></td>             <td>{{ camp_edit object="user" attribute="second_phone" html_code="class=\"tablefield widerone\" id=\"second_phone\"" }}</td>         </tr>         <input type="hidden" name="SubsType" value="paid" />                     </table>      <div id="submitformdiv">         {{ /user_form }}     </div> {{ if $gimme->user->logged_in }}<p style="margin: 15px 0"> To change your password, go <a href="{{ uri options="template user-chgpass.tpl" }}">here</a></p> {{ /if }} 

This is the universal user form which, in the case of user registration, also includes fields to define and double-check the user's password. In the other case, when a user is logged in when submitting this form, it means that the user is modifying their data and not creating a new user. The option for changing the password is then offered in a separate template (so as not to mess with changing the other data).

After registering or modifying the data, the user is redirected to the subscribe.tpl template, where the final step of creating or changing the subscription is performed.

In this example we are presuming that your publication intends to work with paid subscriptions, so we have one hidden parameter:

<input type="hidden" name="SubsType" value="paid" /> 

You can change it to work with trial subscriptions as well. Or, you can create a drop-down menu so the reader can choose what sort of subscription they wants to have:

Subscription type:     <select name="SubsType">         <option value="trial">Trial</option>         <option value="paid">Paid</option>     </select> 

Please note that this Newscoop functionality isn't payment processing; it is for the system's internal purposes to determine whether or not the reader is subscribed to the content. Action is required from the site's staff to approve or decline the subscription, based on the reader's payment data.

Subscription Areas: subscription.tpl

The final step in this process, after creating a new user, is to define the content the user is interested in registering for. In Newscoop, publishers can set different prices for different languages, sections, or publications, and you can offer potential subscribers either a global publication subscription, or subscription by sections.

But before offering these forms, you need to check if everything went well, or if there were some errors... So here is the subscription.tpl template:

{{* no user form submitted, no subscription form submitted: display the user form *}} {{ if !$gimme->edit_user_action->defined       && !$gimme->edit_subscription_action->defined }}           {{ include file="_tpl/user-form.tpl" }} {{ /if }} {{* user form submitted with errors: display the error and the user form *}} {{ if $gimme->edit_user_action->defined       && $gimme->edit_user_action->is_error }}           <h5 style="margin-bottom: 20px">There was an error submitting the account creation form:           {{ $gimme->edit_user_action->error_message }}</h5>           {{ include file="_tpl/user-form.tpl" }} {{ /if }} {{* user form submitted ok: display the subscription form *}} {{ if $gimme->edit_user_action->defined       && $gimme->edit_user_action->ok }}           {{ include file="_tpl/subscription-form.tpl" }} {{ /if }} {{* subscription form submitted with errors: display the error and the submit form *}} {{ if $gimme->edit_subscription_action->defined       && $gimme->edit_subscription_action->is_error }}           <p style="margin: 15px 0">There was an error submitting the subscription form:           {{ $gimme->edit_subscription_action->error_message }}</p>           {{ include file="_tpl/subscription-form.tpl" }} {{ /if }} {{* subscription form submitted ok: display success message *}} {{ if $gimme->edit_subscription_action->defined       && $gimme->edit_subscription_action->ok }}           <p style="margin: 15px 0">Your subscription was created successfully.</p> {{ /if }} 

The subscription form itself:

{{ subscription_form type="by_section" total="Total" template="subscription.tpl" button_html_code="class=\"submitbutton\"" }} <table class="userform">         <tr>             <th colspan="2">Please fill in the following form in order to create the subscription.</th>         </tr>         <tr>             <td colspan="2">                 Subscription time:                 {{ $gimme->publication->subscription_time }} {{ $gimme->publication->subscription_time_unit }}             </td>         </tr>         <tr>             <td colspan="2">                 {{ camp_select object="subscription" attribute="alllanguages" }}                 <span class="formtext">Subscribe to all languages</span>             </td>         </tr>         <tr>             <td>Languages:</td>             <td>{{ camp_select object="subscription" attribute="languages" }}         </tr>         <tr>                 <td colspan="2">Sections</td>         </tr>         {{ list_sections }}         <tr>                 <td colspan="2">                         {{ camp_select object="subscription" attribute="section" }}                         <input name="tx_subs{{ $gimme->section->number }}" type="hidden" value="{{ $gimme->publication->subscription_time }}">                         {{ $gimme->section->name }}                 </td>         </tr>         {{ /list_sections }} </table> {{ /subscription_form }} 

All subscription options (price, time, time unit, currency...) are set on the Publication configuration page in Newscoop's administration interface (you can find it in Content -> Publications, then choose the wrench-and-screwdriver 'Configure' icon)

Then on the section level, you can decide to Add section to all subscriptions or to Delete section from all subscriptions.

The following user-chgpass.tpl template is only used to change existing Newscoop readers' passwords.

{{ user_form submit_button="submit" template="register.tpl" }}     <table id="contact" cellspacing="0" cellpadding="0">         <tr>             <td><label for="contact-passowrd">Password:</label></td>             <td>{{ camp_edit object="user" attribute="password" html_code="class=\"tablefield widerone\" id=\"contact-password\"" }}</td>         </tr>         <tr>             <td><label for="contact-passowrdagain">Password (again):</label></td>             <td>{{ camp_edit object="user" attribute="passwordagain" html_code="class=\"tablefield widerone\" id=\"contact-passwordagain\"" }}</td>         </tr>     </table>     <div style="margin: 15px 0;">         {{ /user_form }}     </div> 

After changing the password, this template redirects the reader back to the register.tpl template.

At this point, you might be asking yourself: "Why the hell is the closing tag for the user_form set inside the div?" Put down your laser blaster nice and slow, and we'll explain: It's because the closing tag defines the position of the submit button.